weekly release 2.4dev
[moodle.git] / lib / completion / 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 *
95dd54ee
AA
48 * @param array $params associative array varname => value of various
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'));
65 $mform->addElement('text', 'criteria_grade_value', get_string('passinggrade', 'completion'));
66 $mform->setDefault('criteria_grade_value', $data);
67
68 if ($this->id) {
69 $mform->setDefault('criteria_grade', 1);
70 $mform->setDefault('criteria_grade_value', $this->gradepass);
71 }
72 }
73
74 /**
75 * Update the criteria information stored in the database
836375ec
SH
76 *
77 * @param stdClass $data Form data
2be4d090
MD
78 */
79 public function update_config(&$data) {
95dd54ee
AA
80
81 // TODO validation
82 if (!empty($data->criteria_grade) && is_numeric($data->criteria_grade_value))
83 {
2be4d090
MD
84 $this->course = $data->id;
85 $this->gradepass = $data->criteria_grade_value;
86 $this->insert();
87 }
88 }
89
90 /**
91 * Get user's course grade in this course
95dd54ee
AA
92 *
93 * @param completion_completion $completion an instance of completion_completion class
836375ec 94 * @return float
2be4d090
MD
95 */
96 private function get_grade($completion) {
97 $grade = grade_get_course_grade($completion->userid, $this->course);
98 return $grade->grade;
99 }
100
101 /**
102 * Review this criteria and decide if the user has completed
836375ec
SH
103 *
104 * @param completion_completion $completion The user's completion record
95dd54ee
AA
105 * @param bool $mark Optionally set false to not save changes to database
106 * @return bool
2be4d090
MD
107 */
108 public function review($completion, $mark = true) {
109 // Get user's course grade
110 $grade = $this->get_grade($completion);
111
112 // If user's current course grade is higher than the required pass grade
113 if ($this->gradepass && $this->gradepass <= $grade) {
114 if ($mark) {
115 $completion->gradefinal = $grade;
116 $completion->mark_complete();
117 }
118
119 return true;
120 }
121
122 return false;
123 }
124
125 /**
126 * Return criteria title for display in reports
836375ec 127 *
2be4d090
MD
128 * @return string
129 */
130 public function get_title() {
131 return get_string('grade');
132 }
133
134 /**
135 * Return a more detailed criteria title for display in reports
836375ec
SH
136 *
137 * @return string
2be4d090
MD
138 */
139 public function get_title_detailed() {
140 return (float) $this->gradepass . '% required';
141 }
142
143 /**
144 * Return criteria type title for display in reports
836375ec
SH
145 *
146 * @return string
2be4d090
MD
147 */
148 public function get_type_title() {
149 return get_string('grade');
150 }
151
152 /**
153 * Return criteria status text for display in reports
836375ec
SH
154 *
155 * @param completion_completion $completion The user's completion record
156 * @return string
2be4d090
MD
157 */
158 public function get_status($completion) {
291e1688
EL
159 // Cast as floats to get rid of excess decimal places
160 $grade = (float) $this->get_grade($completion);
161 $gradepass = (float) $this->gradepass;
2be4d090
MD
162
163 if ($grade) {
291e1688 164 return $grade.'% ('.$gradepass.'% to pass)';
2be4d090 165 } else {
291e1688 166 return $gradepass.'% to pass';
2be4d090
MD
167 }
168 }
169
170 /**
171 * Find user's who have completed this criteria
2be4d090
MD
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) {
28e0c239 218 $completion = new completion_criteria_completion((array) $record, DATA_OBJECT_FETCH_BY_KEY);
ccc604a2 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
836375ec
SH
226 *
227 * @param completion_completion $completion The user's completion record
228 * @return array An array with the following keys:
229 * type, criteria, requirement, status
2be4d090
MD
230 */
231 public function get_details($completion) {
232 $details = array();
233 $details['type'] = get_string('coursegrade', 'completion');
234 $details['criteria'] = get_string('passinggrade', 'completion');
291e1688 235 $details['requirement'] = ((float)$this->gradepass).'%';
2be4d090
MD
236 $details['status'] = '';
237
291e1688 238 $grade = (float)$this->get_grade($completion);
2be4d090
MD
239 if ($grade) {
240 $details['status'] = $grade.'%';
241 }
242
243 return $details;
244 }
95dd54ee 245}