mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
completion_criteria_grade.php
247 lines (221 loc) · 7.7 KB
/
completion_criteria_grade.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
// This file is part of 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/>.
/**
* Course completion critieria - completion on achieving course grade
*
* @package moodlecore
* @copyright 2009 Catalyst IT Ltd
* @author Aaron Barnes <aaronb@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once $CFG->dirroot.'/grade/lib.php';
require_once $CFG->dirroot.'/grade/querylib.php';
/**
* Course completion critieria - completion on achieving course grade
*/
class completion_criteria_grade extends completion_criteria {
/**
* Criteria type constant
* @var int
*/
public $criteriatype = COMPLETION_CRITERIA_TYPE_GRADE;
/**
* Finds and returns a data_object instance based on params.
* @static abstract
*
* @param array $params associative arrays varname=>value
* @return object data_object instance or false if none found.
*/
public static function fetch($params) {
$params['criteriatype'] = COMPLETION_CRITERIA_TYPE_GRADE;
return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
}
/**
* Add appropriate form elements to the critieria form
* @access public
* @param object $mform Moodle forms object
* @param mixed $data optional
* @return void
*/
public function config_form_display(&$mform, $data = null) {
$mform->addElement('checkbox', 'criteria_grade', get_string('enable'));
$mform->addElement('text', 'criteria_grade_value', get_string('graderequired', 'completion'));
$mform->setDefault('criteria_grade_value', $data);
$mform->addElement('static', 'criteria_grade_value_note', '', get_string('criteriagradenote', 'completion'));
if ($this->id) {
$mform->setDefault('criteria_grade', 1);
$mform->setDefault('criteria_grade_value', $this->gradepass);
}
}
/**
* Update the criteria information stored in the database
* @access public
* @param array $data Form data
* @return void
*/
public function update_config(&$data) {
// TODO validation
if (!empty($data->criteria_grade) && is_numeric($data->criteria_grade_value))
{
$this->course = $data->id;
$this->gradepass = $data->criteria_grade_value;
$this->insert();
}
}
/**
* Get user's course grade in this course
* @static
* @access private
* @param object $completion
* @return float
*/
private function get_grade($completion) {
$grade = grade_get_course_grade($completion->userid, $this->course);
return $grade->grade;
}
/**
* Review this criteria and decide if the user has completed
* @access public
* @param object $completion The user's completion record
* @param boolean $mark Optionally set false to not save changes to database
* @return boolean
*/
public function review($completion, $mark = true) {
// Get user's course grade
$grade = $this->get_grade($completion);
// If user's current course grade is higher than the required pass grade
if ($this->gradepass && $this->gradepass <= $grade) {
if ($mark) {
$completion->gradefinal = $grade;
$completion->mark_complete();
}
return true;
}
return false;
}
/**
* Return criteria title for display in reports
* @access public
* @return string
*/
public function get_title() {
return get_string('coursegrade', 'completion');
}
/**
* Return a more detailed criteria title for display in reports
* @access public
* @return string
*/
public function get_title_detailed() {
$graderequired = round($this->gradepass, 2).'%';
return get_string('gradexrequired', 'completion', $graderequired);
}
/**
* Return criteria type title for display in reports
* @access public
* @return string
*/
public function get_type_title() {
return get_string('grade');
}
/**
* Return criteria status text for display in reports
* @access public
* @param object $completion The user's completion record
* @return string
*/
public function get_status($completion) {
$grade = $this->get_grade($completion);
$graderequired = $this->get_title_detailed();
if ($grade) {
$grade = round($grade, 2).'%';
} else {
$grade = get_string('nograde');
}
return $grade.' ('.$graderequired.')';
}
/**
* Find user's who have completed this criteria
* @access public
* @return void
*/
public function cron() {
global $DB;
// Get all users who meet this criteria
$sql = '
SELECT DISTINCT
c.id AS course,
cr.id AS criteriaid,
ra.userid AS userid,
gg.finalgrade AS gradefinal,
gg.timemodified AS timecompleted
FROM
{course_completion_criteria} cr
INNER JOIN
{course} c
ON cr.course = c.id
INNER JOIN
{context} con
ON con.instanceid = c.id
INNER JOIN
{role_assignments} ra
ON ra.contextid = con.id
INNER JOIN
{grade_items} gi
ON gi.courseid = c.id
AND gi.itemtype = \'course\'
INNER JOIN
{grade_grades} gg
ON gg.itemid = gi.id
AND gg.userid = ra.userid
LEFT JOIN
{course_completion_crit_compl} cc
ON cc.criteriaid = cr.id
AND cc.userid = ra.userid
WHERE
cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_GRADE.'
AND con.contextlevel = '.CONTEXT_COURSE.'
AND c.enablecompletion = 1
AND cc.id IS NULL
AND gg.finalgrade >= cr.gradepass
';
// Loop through completions, and mark as complete
$rs = $DB->get_recordset_sql($sql);
foreach ($rs as $record) {
$completion = new completion_criteria_completion((array) $record, DATA_OBJECT_FETCH_BY_KEY);
$completion->mark_complete($record->timecompleted);
}
$rs->close();
}
/**
* Return criteria progress details for display in reports
* @access public
* @param object $completion The user's completion record
* @return array
*/
public function get_details($completion) {
$details = array();
$details['type'] = get_string('coursegrade', 'completion');
$details['criteria'] = get_string('graderequired', 'completion');
$details['requirement'] = round($this->gradepass, 2).'%';
$details['status'] = '';
$grade = round($this->get_grade($completion), 2);
if ($grade) {
$details['status'] = $grade.'%';
}
return $details;
}
}