Merge branch 'MDL-30166-m23' of git://github.com/ankitagarwal/moodle into MOODLE_23_S...
[moodle.git] / lib / completion / completion_criteria_grade.php
blobd0f976b01ecb53c4537a82d80b8bd3b12869eb0b
1 <?php
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.
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/>.
17 /**
18 * Course completion critieria - completion on achieving course grade
20 * @package core_completion
21 * @category completion
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
27 defined('MOODLE_INTERNAL') || die();
28 require_once $CFG->dirroot.'/grade/lib.php';
29 require_once $CFG->dirroot.'/grade/querylib.php';
31 /**
32 * Course completion critieria - completion on achieving course grade
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
40 class completion_criteria_grade extends completion_criteria {
42 /* @var int Criteria type constant [COMPLETION_CRITERIA_TYPE_GRADE] */
43 public $criteriatype = COMPLETION_CRITERIA_TYPE_GRADE;
45 /**
46 * Finds and returns a data_object instance based on params.
48 * @param array $params associative array varname => value of various
49 * parameters used to fetch data_object
50 * @return data_object data_object instance or false if none found.
52 public static function fetch($params) {
53 $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_GRADE;
54 return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
57 /**
58 * Add appropriate form elements to the critieria form
60 * @param moodle_form $mform Moodle forms object
61 * @param stdClass $data containing default values to be set in the form
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('graderequired', 'completion'));
66 $mform->setDefault('criteria_grade_value', $data);
67 $mform->addElement('static', 'criteria_grade_value_note', '', get_string('criteriagradenote', 'completion'));
69 if ($this->id) {
70 $mform->setDefault('criteria_grade', 1);
71 $mform->setDefault('criteria_grade_value', $this->gradepass);
75 /**
76 * Update the criteria information stored in the database
78 * @param stdClass $data Form data
80 public function update_config(&$data) {
82 // TODO validation
83 if (!empty($data->criteria_grade) && is_numeric($data->criteria_grade_value))
85 $this->course = $data->id;
86 $this->gradepass = $data->criteria_grade_value;
87 $this->insert();
91 /**
92 * Get user's course grade in this course
94 * @param completion_completion $completion an instance of completion_completion class
95 * @return float
97 private function get_grade($completion) {
98 $grade = grade_get_course_grade($completion->userid, $this->course);
99 return $grade->grade;
103 * Review this criteria and decide if the user has completed
105 * @param completion_completion $completion The user's completion record
106 * @param bool $mark Optionally set false to not save changes to database
107 * @return bool
109 public function review($completion, $mark = true) {
110 // Get user's course grade
111 $grade = $this->get_grade($completion);
113 // If user's current course grade is higher than the required pass grade
114 if ($this->gradepass && $this->gradepass <= $grade) {
115 if ($mark) {
116 $completion->gradefinal = $grade;
117 $completion->mark_complete();
120 return true;
123 return false;
127 * Return criteria title for display in reports
129 * @return string
131 public function get_title() {
132 return get_string('coursegrade', 'completion');
136 * Return a more detailed criteria title for display in reports
138 * @return string
140 public function get_title_detailed() {
141 $graderequired = round($this->gradepass, 2).'%';
142 return get_string('gradexrequired', 'completion', $graderequired);
146 * Return criteria type title for display in reports
148 * @return string
150 public function get_type_title() {
151 return get_string('grade');
155 * Return criteria status text for display in reports
157 * @param completion_completion $completion The user's completion record
158 * @return string
160 public function get_status($completion) {
161 $grade = $this->get_grade($completion);
162 $graderequired = $this->get_title_detailed();
164 if ($grade) {
165 $grade = round($grade, 2).'%';
166 } else {
167 $grade = get_string('nograde');
170 return $grade.' ('.$graderequired.')';
174 * Find user's who have completed this criteria
176 public function cron() {
177 global $DB;
179 // Get all users who meet this criteria
180 $sql = '
181 SELECT DISTINCT
182 c.id AS course,
183 cr.id AS criteriaid,
184 ra.userid AS userid,
185 gg.finalgrade AS gradefinal,
186 gg.timemodified AS timecompleted
187 FROM
188 {course_completion_criteria} cr
189 INNER JOIN
190 {course} c
191 ON cr.course = c.id
192 INNER JOIN
193 {context} con
194 ON con.instanceid = c.id
195 INNER JOIN
196 {role_assignments} ra
197 ON ra.contextid = con.id
198 INNER JOIN
199 {grade_items} gi
200 ON gi.courseid = c.id
201 AND gi.itemtype = \'course\'
202 INNER JOIN
203 {grade_grades} gg
204 ON gg.itemid = gi.id
205 AND gg.userid = ra.userid
206 LEFT JOIN
207 {course_completion_crit_compl} cc
208 ON cc.criteriaid = cr.id
209 AND cc.userid = ra.userid
210 WHERE
211 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_GRADE.'
212 AND con.contextlevel = '.CONTEXT_COURSE.'
213 AND c.enablecompletion = 1
214 AND cc.id IS NULL
215 AND gg.finalgrade >= cr.gradepass
218 // Loop through completions, and mark as complete
219 $rs = $DB->get_recordset_sql($sql);
220 foreach ($rs as $record) {
221 $completion = new completion_criteria_completion((array) $record, DATA_OBJECT_FETCH_BY_KEY);
222 $completion->mark_complete($record->timecompleted);
224 $rs->close();
228 * Return criteria progress details for display in reports
230 * @param completion_completion $completion The user's completion record
231 * @return array An array with the following keys:
232 * type, criteria, requirement, status
234 public function get_details($completion) {
235 $details = array();
236 $details['type'] = get_string('coursegrade', 'completion');
237 $details['criteria'] = get_string('graderequired', 'completion');
238 $details['requirement'] = round($this->gradepass, 2).'%';
239 $details['status'] = '';
241 $grade = round($this->get_grade($completion), 2);
242 if ($grade) {
243 $details['status'] = $grade.'%';
246 return $details;