forked from davosmith/moodle-checklist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoupdate.php
172 lines (149 loc) · 6.49 KB
/
autoupdate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
// This file is part of the Checklist plugin for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use mod_checklist\local\checklist_check;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot.'/mod/checklist/lib.php');
/**
* Remove the '//' at the start of the next line to output lots of
* helpful information during automatic updates.
*/
// define("DEBUG_CHECKLIST_AUTOUPDATE", 1);
function checklist_completion_update_checks($userid, $itemchecks, $newstate) {
global $DB;
$updatecount = 0;
$updatechecklists = array();
foreach ($itemchecks as $itemcheck) {
list(, $cm) = get_course_and_cm_from_instance($itemcheck->checklist, 'checklist', $itemcheck->course);
$context = context_module::instance($cm->id);
if (!has_capability('mod/checklist:updateown', $context, $userid)) {
continue; // User can't update checklist, so don't save the value.
}
if ($itemcheck->id) {
$check = new checklist_check((array)$itemcheck, false);
} else {
$check = new checklist_check(['item' => $itemcheck->itemid, 'userid' => $userid], false);
}
if ($itemcheck->teacheredit == CHECKLIST_MARKING_TEACHER) {
if ($check->is_checked_teacher() != $newstate) {
$check->set_teachermark(CHECKLIST_TEACHERMARK_YES, null);
$check->save();
$updatechecklists[] = $itemcheck->checklist;
$updatecount++;
}
} else {
if ($newstate != $check->is_checked_student()) {
$check->set_checked_student($newstate);
$check->save();
$updatechecklists[] = $itemcheck->checklist;
$updatecount++;
}
}
}
if (!empty($updatechecklists)) {
$updatechecklists = array_unique($updatechecklists);
list($csql, $cparams) = $DB->get_in_or_equal($updatechecklists);
$checklists = $DB->get_records_select('checklist', 'id '.$csql, $cparams);
foreach ($checklists as $checklist) {
checklist_update_grades($checklist, $userid);
}
}
return $updatecount;
}
/**
* @param int $courseid
* @param string $module
* @param int $cmid
* @param int $userid
* @param object[] $checklists
* @return int
*/
function checklist_autoupdate_internal($courseid, $module, $cmid, $userid) {
global $DB;
if ($userid == 0) {
return 0;
}
if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
mtrace("Possible update needed - courseid: $courseid, module: $module, ".
"cmid: $cmid, userid: $userid");
}
$completion = new completion_info((object)['id' => $courseid]);
$cm = $DB->get_record('course_modules', ['id' => $cmid], 'id, completion');
if ($completion->is_enabled($cm)) {
if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
mtrace("This course module has completion enabled - allow that to control any checklist items");
}
return 0;
}
$sql = "SELECT i.id AS itemid, i.checklist, cl.teacheredit, ck.*, cl.course
FROM {checklist_item} i
JOIN {checklist} cl ON i.checklist = cl.id
LEFT JOIN {checklist_check} ck ON (ck.item = i.id AND ck.userid = :userid)
WHERE cl.autoupdate > 0 AND i.moduleid = :cmid AND i.itemoptional < :heading";
$itemchecks = $DB->get_records_sql($sql, ['userid' => $userid, 'cmid' => $cmid, 'heading' => CHECKLIST_OPTIONAL_HEADING]);
if (empty($itemchecks)) {
if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
mtrace("No checklist items linked to this course module");
}
return 0;
}
$updatecount = checklist_completion_update_checks($userid, $itemchecks, true);
if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
mtrace("$updatecount checklist items updated from this log entry");
}
return 0;
}
function checklist_completion_autoupdate($cmid, $userid, $newstate) {
global $DB, $USER;
if ($userid == 0) {
$userid = $USER->id;
}
if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
mtrace("Completion status change for cmid: $cmid, userid: $userid, newstate: $newstate");
}
$sql = "SELECT i.id AS itemid, i.checklist, cl.teacheredit, ck.*, cl.course
FROM {checklist_item} i
JOIN {checklist} cl ON i.checklist = cl.id
LEFT JOIN {checklist_check} ck ON (ck.item = i.id AND ck.userid = :userid)
WHERE cl.autoupdate > 0 AND i.moduleid = :cmid AND i.itemoptional < :heading";
$itemchecks = $DB->get_records_sql($sql, ['userid' => $userid, 'cmid' => $cmid, 'heading' => CHECKLIST_OPTIONAL_HEADING]);
if (empty($itemchecks)) {
if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
mtrace("No checklist items linked to this course module");
}
return 0;
}
$newstate = ($newstate == COMPLETION_COMPLETE || $newstate == COMPLETION_COMPLETE_PASS); // Not complete if failed.
$updatecount = checklist_completion_update_checks($userid, $itemchecks, $newstate);
if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
mtrace("Updated $updatecount checklist items from this completion status change");
}
return $updatecount;
}
function checklist_course_completion_autoupdate($courseid, $userid) {
global $DB;
$sql = "SELECT i.id AS itemid, i.checklist, cl.teacheredit, ck.*, cl.course
FROM {checklist_item} i
JOIN {checklist} cl ON cl.id = i.checklist
LEFT JOIN {checklist_check} ck ON ck.item = i.id AND ck.userid = :userid
WHERE cl.autoupdate > 0 AND i.linkcourseid = :courseid AND i.itemoptional < :heading";
$params = ['userid' => $userid, 'courseid' => $courseid, 'heading' => 2];
$itemchecks = $DB->get_records_sql($sql, $params);
if (!$itemchecks) {
return;
}
checklist_completion_update_checks($userid, $itemchecks, true);
}