forked from davosmith/moodle-checklist
/
autoupdate.php
160 lines (138 loc) · 5.87 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
<?php
function checklist_autoupdate($courseid, $module, $action, $cm, $userid) {
global $CFG;
echo "checking module: $module; action: $action";
if ($module == 'course') { return; }
if ($module == 'user') { return; }
if ($module == 'role') { return; }
if ($module == 'notes') { return; }
if ($module == 'calendar') { return; }
if ($module == 'message') { return; }
if ($module == 'admin/mnet') { return; }
if ($module == 'blog') { return; }
if ($module == 'tag') { return; }
if ($module == 'blocks/tag_youtube') { return; }
if ($module == 'login') { return; }
if ($module == 'library') { return; }
if ($module == 'upload') { return; }
echo 'passed first test';
if (
(($module == 'survey') && ($action == 'submit'))
|| (($module == 'quiz') && ($action == 'close attempt'))
|| (($module == 'forum') && (($action == 'add post')||($action == 'add discussion')))
|| (($module == 'resource') && ($action == 'view'))
|| (($module == 'hotpot') && ($action == 'submit'))
|| (($module == 'wiki') && ($action == 'edit'))
|| (($module == 'checklist') && ($action == 'complete'))
|| (($module == 'choice') && ($action == 'choose'))
|| (($module == 'lams') && ($action == 'view'))
|| (($module == 'scorm') && ($action == 'view'))
|| (($module == 'assignment') && ($action == 'upload'))
|| (($module == 'journal') && ($action == 'add entry'))
|| (($module == 'lesson') && ($action == 'end'))
|| (($module == 'realtimequiz') && ($action == 'submit'))
|| (($module == 'workshop') && ($action == 'submit'))
|| (($module == 'glossary') && ($action == 'add entry'))
|| (($module == 'data') && ($action == 'add'))
|| (($module == 'chat') && ($action == 'talk'))
|| (($module == 'feedback') && ($action == 'submit'))
) {
echo 'something to update';
$checklists = get_records_sql("SELECT * FROM {$CFG->prefix}checklist WHERE course = $courseid AND autoupdate > 0");
if (!$checklists) {
return;
}
echo 'found checklists';
// Find all checklist_item records which are related to these $checklists which have a moduleid matching $module
// and do not have a related checklist_check record that is filled in
$checklistids = '('.implode(',', array_keys($checklists)).')';
$items = get_records_sql("SELECT * FROM {$CFG->prefix}checklist_item i WHERE i.checklist IN {$checklistids} AND i.moduleid = $cm AND i.itemoptional < 2 AND i.complete_score = 0");
// itemoptional - 0: required; 1: optional; 2: heading; 3: disabled; 4: disabled heading
// not loading defines from mod/checklist/locallib.php to reduce overhead
if (!$items) {
return;
}
echo 'found items';
$updategrades = false;
foreach ($items as $item) {
$updategrades = checklist_set_check($item->id, $userid, true) || $updategrades;
}
if ($updategrades) {
echo 'grades updated';
require_once($CFG->dirroot.'/mod/checklist/lib.php');
foreach ($checklists as $checklist) {
checklist_update_grades($checklist, $userid);
}
}
}
}
function checklist_set_check($itemid, $userid, $set) {
$check = get_record_select('checklist_check', 'item = '.$itemid.' AND userid = '.$userid);
if ($check) {
if ($set) {
if ($check->usertimestamp) {
return false;
}
$check->usertimestamp = time();
update_record('checklist_check', $check);
return true;
} else {
if (!$check->usertimestamp) {
return false;
}
$check->usertimestamp = 0;
update_record('checklist_check',$check);
return true;
}
} else {
if (!$set) {
return false;
}
$check = new stdClass;
$check->item = $itemid;
$check->userid = $userid;
$check->usertimestamp = time();
$check->teachertimestamp = 0;
$check->teachermark = 0; // CHECKLIST_TEACHERMARK_UNDECIDED - not loading from mod/checklist/lib.php to reduce overhead
$check->id = insert_record('checklist_check', $check);
return true;
}
}
function checklist_autoupdate_score($modname, $courseid, $instanceid, $grades) {
global $CFG;
if (!$grades) {
return;
}
$checklists = get_records_sql("SELECT * FROM {$CFG->prefix}checklist WHERE course = $courseid AND autoupdate > 0 AND autopopulate > 0");
if (!$checklists) {
return;
}
$checklistids = '('.implode(',', array_keys($checklists)).')';
$cm = get_coursemodule_from_instance($modname, $instanceid, $courseid);
$sql = "SELECT * FROM {$CFG->prefix}checklist_item i WHERE i.checklist IN {$checklistids} AND i.moduleid = {$cm->id} AND i.itemoptional < 2 AND i.complete_score > 0";
$items = get_records_sql($sql);
// itemoptional - 0: required; 1: optional; 2: heading; 3: disabled; 4: disabled heading
// not loading defines from mod/checklist/locallib.php to reduce overhead
if (!$items) {
return;
}
if (!is_array($grades)) {
$grades = array($grades);
}
$updategrades = false;
foreach ($grades as $grade) {
foreach ($items as $item) {
$complete = $grade->rawgrade >= $item->complete_score;
$updategrades = checklist_set_check($item->id, $grade->userid, $complete) || $updategrades;
}
}
if ($updategrades) {
require_once($CFG->dirroot.'/mod/checklist/lib.php');
foreach ($checklists as $checklist) {
foreach ($grades as $grade) {
checklist_update_grades($checklist, $grade->userid);
}
}
}
}
?>