course completion MDL-2631 Course completion feature - Thanks to Aaron Barnes and...
[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,
180 cr.date AS date,
181 cr.id AS criteriaid,
182 ra.userid AS userid,
183 gg.finalgrade AS gradefinal,
184 gg.timemodified AS timecompleted
185 FROM
186 {course_completion_criteria} cr
187 INNER JOIN
188 {course} c
189 ON cr.course = c.id
190 INNER JOIN
191 {context} con
192 ON con.instanceid = c.id
193 INNER JOIN
194 {role_assignments} ra
195 ON ra.contextid = con.id
196 INNER JOIN
197 {grade_items} gi
198 ON gi.courseid = c.id
199 AND gi.itemtype = \'course\'
200 INNER JOIN
201 {grade_grades} gg
202 ON gg.itemid = gi.id
203 AND gg.userid = ra.userid
204 LEFT JOIN
205 {course_completion_crit_compl} cc
206 ON cc.criteriaid = cr.id
207 AND cc.userid = ra.userid
208 WHERE
209 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_GRADE.'
210 AND con.contextlevel = '.CONTEXT_COURSE.'
211 AND c.enablecompletion = 1
212 AND cc.id IS NULL
213 AND gg.finalgrade >= cr.gradepass
214 ';
215
216 // Loop through completions, and mark as complete
217 if ($rs = $DB->get_recordset_sql($sql)) {
218 foreach ($rs as $record) {
219
220 $completion = new completion_criteria_completion((array)$record);
221 $completion->mark_complete($record->timecompleted);
222 }
223
224 $rs->close();
225 }
226 }
227
228 /**
229 * Return criteria progress details for display in reports
230 * @access public
231 * @param object $completion The user's completion record
232 * @return array
233 */
234 public function get_details($completion) {
235 $details = array();
236 $details['type'] = get_string('coursegrade', 'completion');
237 $details['criteria'] = get_string('passinggrade', 'completion');
238 $details['requirement'] = ((float)$this->gradepass).'%';
239 $details['status'] = '';
240
241 $grade = (float)$this->get_grade($completion);
242 if ($grade) {
243 $details['status'] = $grade.'%';
244 }
245
246 return $details;
247 }
248}