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