|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00026 require_once $CFG->dirroot.'/grade/lib.php'; 00027 require_once $CFG->dirroot.'/grade/querylib.php'; 00028 00032 class completion_criteria_grade extends completion_criteria { 00033 00038 public $criteriatype = COMPLETION_CRITERIA_TYPE_GRADE; 00039 00047 public static function fetch($params) { 00048 $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_GRADE; 00049 return self::fetch_helper('course_completion_criteria', __CLASS__, $params); 00050 } 00051 00059 public function config_form_display(&$mform, $data = null) { 00060 $mform->addElement('checkbox', 'criteria_grade', get_string('enable')); 00061 $mform->addElement('text', 'criteria_grade_value', get_string('passinggrade', 'completion')); 00062 $mform->setDefault('criteria_grade_value', $data); 00063 00064 if ($this->id) { 00065 $mform->setDefault('criteria_grade', 1); 00066 $mform->setDefault('criteria_grade_value', $this->gradepass); 00067 } 00068 } 00069 00076 public function update_config(&$data) { 00077 00078 // TODO validation 00079 if (!empty($data->criteria_grade) && is_numeric($data->criteria_grade_value)) 00080 { 00081 $this->course = $data->id; 00082 $this->gradepass = $data->criteria_grade_value; 00083 $this->insert(); 00084 } 00085 } 00086 00094 private function get_grade($completion) { 00095 $grade = grade_get_course_grade($completion->userid, $this->course); 00096 return $grade->grade; 00097 } 00098 00106 public function review($completion, $mark = true) { 00107 // Get user's course grade 00108 $grade = $this->get_grade($completion); 00109 00110 // If user's current course grade is higher than the required pass grade 00111 if ($this->gradepass && $this->gradepass <= $grade) { 00112 if ($mark) { 00113 $completion->gradefinal = $grade; 00114 $completion->mark_complete(); 00115 } 00116 00117 return true; 00118 } 00119 00120 return false; 00121 } 00122 00128 public function get_title() { 00129 return get_string('grade'); 00130 } 00131 00137 public function get_title_detailed() { 00138 return (float) $this->gradepass . '% required'; 00139 } 00140 00146 public function get_type_title() { 00147 return get_string('grade'); 00148 } 00149 00156 public function get_status($completion) { 00157 // Cast as floats to get rid of excess decimal places 00158 $grade = (float) $this->get_grade($completion); 00159 $gradepass = (float) $this->gradepass; 00160 00161 if ($grade) { 00162 return $grade.'% ('.$gradepass.'% to pass)'; 00163 } else { 00164 return $gradepass.'% to pass'; 00165 } 00166 } 00167 00173 public function cron() { 00174 global $DB; 00175 00176 // Get all users who meet this criteria 00177 $sql = ' 00178 SELECT DISTINCT 00179 c.id AS course, 00180 cr.id AS criteriaid, 00181 ra.userid AS userid, 00182 gg.finalgrade AS gradefinal, 00183 gg.timemodified AS timecompleted 00184 FROM 00185 {course_completion_criteria} cr 00186 INNER JOIN 00187 {course} c 00188 ON cr.course = c.id 00189 INNER JOIN 00190 {context} con 00191 ON con.instanceid = c.id 00192 INNER JOIN 00193 {role_assignments} ra 00194 ON ra.contextid = con.id 00195 INNER JOIN 00196 {grade_items} gi 00197 ON gi.courseid = c.id 00198 AND gi.itemtype = \'course\' 00199 INNER JOIN 00200 {grade_grades} gg 00201 ON gg.itemid = gi.id 00202 AND gg.userid = ra.userid 00203 LEFT JOIN 00204 {course_completion_crit_compl} cc 00205 ON cc.criteriaid = cr.id 00206 AND cc.userid = ra.userid 00207 WHERE 00208 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_GRADE.' 00209 AND con.contextlevel = '.CONTEXT_COURSE.' 00210 AND c.enablecompletion = 1 00211 AND cc.id IS NULL 00212 AND gg.finalgrade >= cr.gradepass 00213 '; 00214 00215 // Loop through completions, and mark as complete 00216 $rs = $DB->get_recordset_sql($sql); 00217 foreach ($rs as $record) { 00218 $completion = new completion_criteria_completion((array)$record); 00219 $completion->mark_complete($record->timecompleted); 00220 } 00221 $rs->close(); 00222 } 00223 00230 public function get_details($completion) { 00231 $details = array(); 00232 $details['type'] = get_string('coursegrade', 'completion'); 00233 $details['criteria'] = get_string('passinggrade', 'completion'); 00234 $details['requirement'] = ((float)$this->gradepass).'%'; 00235 $details['status'] = ''; 00236 00237 $grade = (float)$this->get_grade($completion); 00238 if ($grade) { 00239 $details['status'] = $grade.'%'; 00240 } 00241 00242 return $details; 00243 } 00244 }