MDL-38744 completion: add missing setType and properly allow float
[moodle.git] / completion / criteria / completion_criteria_grade.php
CommitLineData
2be4d090 1<?php
2be4d090
MD
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Course completion critieria - completion on achieving course grade
19 *
836375ec
SH
20 * @package core_completion
21 * @category completion
2be4d090 22 * @copyright 2009 Catalyst IT Ltd
836375ec
SH
23 * @author Aaron Barnes <aaronb@catalyst.net.nz>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2be4d090 25 */
836375ec
SH
26
27defined('MOODLE_INTERNAL') || die();
2be4d090
MD
28require_once $CFG->dirroot.'/grade/lib.php';
29require_once $CFG->dirroot.'/grade/querylib.php';
30
31/**
32 * Course completion critieria - completion on achieving course grade
836375ec
SH
33 *
34 * @package core_completion
35 * @category completion
36 * @copyright 2009 Catalyst IT Ltd
37 * @author Aaron Barnes <aaronb@catalyst.net.nz>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2be4d090
MD
39 */
40class completion_criteria_grade extends completion_criteria {
41
95dd54ee 42 /* @var int Criteria type constant [COMPLETION_CRITERIA_TYPE_GRADE] */
2be4d090
MD
43 public $criteriatype = COMPLETION_CRITERIA_TYPE_GRADE;
44
45 /**
46 * Finds and returns a data_object instance based on params.
2be4d090 47 *
ae8530e8 48 * @param array $params associative array varname => value of various
95dd54ee 49 * parameters used to fetch data_object
836375ec 50 * @return data_object data_object instance or false if none found.
2be4d090
MD
51 */
52 public static function fetch($params) {
53 $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_GRADE;
54 return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
55 }
56
57 /**
58 * Add appropriate form elements to the critieria form
836375ec
SH
59 *
60 * @param moodle_form $mform Moodle forms object
95dd54ee 61 * @param stdClass $data containing default values to be set in the form
2be4d090
MD
62 */
63 public function config_form_display(&$mform, $data = null) {
64 $mform->addElement('checkbox', 'criteria_grade', get_string('enable'));
08f19f48 65 $mform->addElement('text', 'criteria_grade_value', get_string('graderequired', 'completion'));
ebe558ac
DP
66 $mform->setType('criteria_grade_value', PARAM_RAW); // Uses unformat_float.
67 $mform->setDefault('criteria_grade_value', format_float($data));
08f19f48 68 $mform->addElement('static', 'criteria_grade_value_note', '', get_string('criteriagradenote', 'completion'));
2be4d090
MD
69
70 if ($this->id) {
71 $mform->setDefault('criteria_grade', 1);
ebe558ac 72 $mform->setDefault('criteria_grade_value', format_float($this->gradepass));
2be4d090
MD
73 }
74 }
75
76 /**
77 * Update the criteria information stored in the database
836375ec
SH
78 *
79 * @param stdClass $data Form data
2be4d090
MD
80 */
81 public function update_config(&$data) {
95dd54ee 82
ebe558ac 83 $formatedgrade = unformat_float($data->criteria_grade_value);
95dd54ee 84 // TODO validation
ebe558ac 85 if (!empty($formatedgrade) && is_numeric($formatedgrade)) {
2be4d090 86 $this->course = $data->id;
ebe558ac 87 $this->gradepass = $formatedgrade;
2be4d090
MD
88 $this->insert();
89 }
90 }
91
92 /**
93 * Get user's course grade in this course
95dd54ee
AA
94 *
95 * @param completion_completion $completion an instance of completion_completion class
836375ec 96 * @return float
2be4d090
MD
97 */
98 private function get_grade($completion) {
99 $grade = grade_get_course_grade($completion->userid, $this->course);
100 return $grade->grade;
101 }
102
103 /**
104 * Review this criteria and decide if the user has completed
836375ec
SH
105 *
106 * @param completion_completion $completion The user's completion record
95dd54ee
AA
107 * @param bool $mark Optionally set false to not save changes to database
108 * @return bool
2be4d090
MD
109 */
110 public function review($completion, $mark = true) {
111 // Get user's course grade
112 $grade = $this->get_grade($completion);
113
114 // If user's current course grade is higher than the required pass grade
115 if ($this->gradepass && $this->gradepass <= $grade) {
116 if ($mark) {
117 $completion->gradefinal = $grade;
118 $completion->mark_complete();
119 }
120
121 return true;
122 }
123
124 return false;
125 }
126
127 /**
128 * Return criteria title for display in reports
836375ec 129 *
2be4d090
MD
130 * @return string
131 */
132 public function get_title() {
08f19f48 133 return get_string('coursegrade', 'completion');
2be4d090
MD
134 }
135
136 /**
137 * Return a more detailed criteria title for display in reports
836375ec
SH
138 *
139 * @return string
2be4d090
MD
140 */
141 public function get_title_detailed() {
08f19f48
AB
142 $graderequired = round($this->gradepass, 2).'%';
143 return get_string('gradexrequired', 'completion', $graderequired);
2be4d090
MD
144 }
145
146 /**
147 * Return criteria type title for display in reports
836375ec
SH
148 *
149 * @return string
2be4d090
MD
150 */
151 public function get_type_title() {
152 return get_string('grade');
153 }
154
155 /**
156 * Return criteria status text for display in reports
836375ec
SH
157 *
158 * @param completion_completion $completion The user's completion record
159 * @return string
2be4d090
MD
160 */
161 public function get_status($completion) {
08f19f48
AB
162 $grade = $this->get_grade($completion);
163 $graderequired = $this->get_title_detailed();
2be4d090
MD
164
165 if ($grade) {
08f19f48 166 $grade = round($grade, 2).'%';
2be4d090 167 } else {
08f19f48 168 $grade = get_string('nograde');
2be4d090 169 }
08f19f48
AB
170
171 return $grade.' ('.$graderequired.')';
2be4d090
MD
172 }
173
174 /**
175 * Find user's who have completed this criteria
2be4d090
MD
176 */
177 public function cron() {
178 global $DB;
179
180 // Get all users who meet this criteria
181 $sql = '
182 SELECT DISTINCT
183 c.id AS course,
2be4d090
MD
184 cr.id AS criteriaid,
185 ra.userid AS userid,
186 gg.finalgrade AS gradefinal,
187 gg.timemodified AS timecompleted
188 FROM
189 {course_completion_criteria} cr
190 INNER JOIN
191 {course} c
192 ON cr.course = c.id
193 INNER JOIN
194 {context} con
195 ON con.instanceid = c.id
196 INNER JOIN
197 {role_assignments} ra
198 ON ra.contextid = con.id
199 INNER JOIN
200 {grade_items} gi
201 ON gi.courseid = c.id
202 AND gi.itemtype = \'course\'
203 INNER JOIN
204 {grade_grades} gg
205 ON gg.itemid = gi.id
206 AND gg.userid = ra.userid
207 LEFT JOIN
208 {course_completion_crit_compl} cc
209 ON cc.criteriaid = cr.id
210 AND cc.userid = ra.userid
211 WHERE
212 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_GRADE.'
213 AND con.contextlevel = '.CONTEXT_COURSE.'
214 AND c.enablecompletion = 1
215 AND cc.id IS NULL
216 AND gg.finalgrade >= cr.gradepass
217 ';
218
219 // Loop through completions, and mark as complete
419178d7
EL
220 $rs = $DB->get_recordset_sql($sql);
221 foreach ($rs as $record) {
28e0c239 222 $completion = new completion_criteria_completion((array) $record, DATA_OBJECT_FETCH_BY_KEY);
ccc604a2 223 $completion->mark_complete($record->timecompleted);
2be4d090 224 }
419178d7 225 $rs->close();
2be4d090
MD
226 }
227
228 /**
229 * Return criteria progress details for display in reports
836375ec
SH
230 *
231 * @param completion_completion $completion The user's completion record
232 * @return array An array with the following keys:
233 * type, criteria, requirement, status
2be4d090
MD
234 */
235 public function get_details($completion) {
236 $details = array();
237 $details['type'] = get_string('coursegrade', 'completion');
08f19f48
AB
238 $details['criteria'] = get_string('graderequired', 'completion');
239 $details['requirement'] = round($this->gradepass, 2).'%';
2be4d090
MD
240 $details['status'] = '';
241
08f19f48 242 $grade = round($this->get_grade($completion), 2);
2be4d090
MD
243 if ($grade) {
244 $details['status'] = $grade.'%';
245 }
246
247 return $details;
248 }
95dd54ee 249}