[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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. 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 * This file contains the courseset completion badge award criteria type class 19 * 20 * @package core 21 * @subpackage badges 22 * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com> 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 require_once ('award_criteria_course.php'); 29 require_once($CFG->libdir . '/completionlib.php'); 30 require_once($CFG->dirroot . '/grade/querylib.php'); 31 require_once($CFG->libdir . '/gradelib.php'); 32 33 /** 34 * Badge award criteria -- award on courseset completion 35 * 36 */ 37 class award_criteria_courseset extends award_criteria { 38 39 /* @var int Criteria [BADGE_CRITERIA_TYPE_COURSESET] */ 40 public $criteriatype = BADGE_CRITERIA_TYPE_COURSESET; 41 42 public $required_param = 'course'; 43 public $optional_params = array('grade', 'bydate'); 44 45 /** 46 * Get criteria details for displaying to users 47 * 48 * @return string 49 */ 50 public function get_details($short = '') { 51 global $DB, $OUTPUT; 52 $output = array(); 53 foreach ($this->params as $p) { 54 $coursename = $DB->get_field('course', 'fullname', array('id' => $p['course'])); 55 if (!$coursename) { 56 $str = $OUTPUT->error_text(get_string('error:nosuchcourse', 'badges')); 57 } else { 58 $str = html_writer::tag('b', '"' . $coursename . '"'); 59 if (isset($p['bydate'])) { 60 $str .= get_string('criteria_descr_bydate', 'badges', userdate($p['bydate'], get_string('strftimedate', 'core_langconfig'))); 61 } 62 if (isset($p['grade'])) { 63 $str .= get_string('criteria_descr_grade', 'badges', $p['grade']); 64 } 65 } 66 $output[] = $str; 67 } 68 69 if ($short) { 70 return implode(', ', $output); 71 } else { 72 return html_writer::alist($output, array(), 'ul'); 73 } 74 } 75 76 public function get_courses(&$mform) { 77 global $DB, $CFG; 78 require_once($CFG->dirroot . '/course/lib.php'); 79 $buttonarray = array(); 80 81 // Get courses with enabled completion. 82 $courses = $DB->get_records('course', array('enablecompletion' => COMPLETION_ENABLED)); 83 if (!empty($courses)) { 84 require_once($CFG->libdir . '/coursecatlib.php'); 85 $list = coursecat::make_categories_list(); 86 87 $select = array(); 88 $selected = array(); 89 foreach ($courses as $c) { 90 $select[$c->id] = $list[$c->category] . ' / ' . format_string($c->fullname, true, array('context' => context_course::instance($c->id))); 91 } 92 93 if ($this->id !== 0) { 94 $selected = array_keys($this->params); 95 } 96 $settings = array('multiple' => 'multiple', 'size' => 20, 'style' => 'width:300px'); 97 $mform->addElement('select', 'courses', get_string('addcourse', 'badges'), $select, $settings); 98 $mform->addRule('courses', get_string('requiredcourse', 'badges'), 'required'); 99 $mform->addHelpButton('courses', 'addcourse', 'badges'); 100 101 $buttonarray[] =& $mform->createElement('submit', 'submitcourse', get_string('addcourse', 'badges')); 102 $buttonarray[] =& $mform->createElement('submit', 'cancel', get_string('cancel')); 103 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); 104 105 $mform->addElement('hidden', 'addcourse', 'addcourse'); 106 $mform->setType('addcourse', PARAM_TEXT); 107 if ($this->id !== 0) { 108 $mform->setDefault('courses', $selected); 109 } 110 $mform->setType('agg', PARAM_INT); 111 } else { 112 $mform->addElement('static', 'nocourses', '', get_string('error:nocourses', 'badges')); 113 $buttonarray[] =& $mform->createElement('submit', 'cancel', get_string('continue')); 114 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); 115 } 116 } 117 118 public function add_courses($params = array()) { 119 global $DB; 120 $t = $DB->start_delegated_transaction(); 121 if ($this->id !== 0) { 122 $critid = $this->id; 123 } else { 124 $fordb = new stdClass(); 125 $fordb->criteriatype = $this->criteriatype; 126 $fordb->method = BADGE_CRITERIA_AGGREGATION_ALL; 127 $fordb->badgeid = $this->badgeid; 128 $critid = $DB->insert_record('badge_criteria', $fordb, true, true); 129 } 130 if ($critid) { 131 foreach ($params as $p) { 132 $newp = new stdClass(); 133 $newp->critid = $critid; 134 $newp->name = 'course_' . $p; 135 $newp->value = $p; 136 if (!$DB->record_exists('badge_criteria_param', array('critid' => $critid, 'name' => $newp->name))) { 137 $DB->insert_record('badge_criteria_param', $newp, false, true); 138 } 139 } 140 } 141 $t->allow_commit(); 142 return $critid; 143 } 144 145 /** 146 * Add appropriate new criteria options to the form 147 * 148 */ 149 public function get_options(&$mform) { 150 global $DB; 151 $none = true; 152 153 $mform->addElement('header', 'first_header', $this->get_title()); 154 $mform->addHelpButton('first_header', 'criteria_' . $this->criteriatype, 'badges'); 155 156 if ($courses = $DB->get_records('course', array('enablecompletion' => COMPLETION_ENABLED))) { 157 $mform->addElement('submit', 'addcourse', get_string('addcourse', 'badges'), array('class' => 'addcourse')); 158 } 159 160 // In courseset, print out only the ones that were already selected. 161 foreach ($this->params as $p) { 162 if ($course = $DB->get_record('course', array('id' => $p['course']))) { 163 $coursecontext = context_course::instance($course->id); 164 $param = array( 165 'id' => $course->id, 166 'checked' => true, 167 'name' => format_string($course->fullname, true, array('context' => $coursecontext)), 168 'error' => false 169 ); 170 171 if (isset($p['bydate'])) { 172 $param['bydate'] = $p['bydate']; 173 } 174 if (isset($p['grade'])) { 175 $param['grade'] = $p['grade']; 176 } 177 $this->config_options($mform, $param); 178 $none = false; 179 } else { 180 $this->config_options($mform, array('id' => $p['course'], 'checked' => true, 181 'name' => get_string('error:nosuchcourse', 'badges'), 'error' => true)); 182 } 183 } 184 185 // Add aggregation. 186 if (!$none) { 187 $mform->addElement('header', 'aggregation', get_string('method', 'badges')); 188 $agg = array(); 189 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('allmethodcourseset', 'badges'), 1); 190 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('anymethodcourseset', 'badges'), 2); 191 $mform->addGroup($agg, 'methodgr', '', array('<br/>'), false); 192 if ($this->id !== 0) { 193 $mform->setDefault('agg', $this->method); 194 } else { 195 $mform->setDefault('agg', BADGE_CRITERIA_AGGREGATION_ANY); 196 } 197 } 198 199 return array($none, get_string('noparamstoadd', 'badges')); 200 } 201 202 /** 203 * Review this criteria and decide if it has been completed 204 * 205 * @param int $userid User whose criteria completion needs to be reviewed. 206 * @param bool $filtered An additional parameter indicating that user list 207 * has been reduced and some expensive checks can be skipped. 208 * 209 * @return bool Whether criteria is complete 210 */ 211 public function review($userid, $filtered = false) { 212 foreach ($this->params as $param) { 213 $course = new stdClass(); 214 $course->id = $param['course']; 215 216 $info = new completion_info($course); 217 $check_grade = true; 218 $check_date = true; 219 220 if (isset($param['grade'])) { 221 $grade = grade_get_course_grade($userid, $course->id); 222 $check_grade = ($grade->grade >= $param['grade']); 223 } 224 225 if (!$filtered && isset($param['bydate'])) { 226 $cparams = array( 227 'userid' => $userid, 228 'course' => $course->id, 229 ); 230 $completion = new completion_completion($cparams); 231 $date = $completion->timecompleted; 232 $check_date = ($date <= $param['bydate']); 233 } 234 235 $overall = false; 236 if ($this->method == BADGE_CRITERIA_AGGREGATION_ALL) { 237 if ($info->is_course_complete($userid) && $check_grade && $check_date) { 238 $overall = true; 239 continue; 240 } else { 241 return false; 242 } 243 } else { 244 if ($info->is_course_complete($userid) && $check_grade && $check_date) { 245 return true; 246 } else { 247 $overall = false; 248 continue; 249 } 250 } 251 } 252 253 return $overall; 254 } 255 256 /** 257 * Returns array with sql code and parameters returning all ids 258 * of users who meet this particular criterion. 259 * 260 * @return array list($join, $where, $params) 261 */ 262 public function get_completed_criteria_sql() { 263 $join = ''; 264 $where = ''; 265 $params = array(); 266 267 if ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) { 268 foreach ($this->params as $param) { 269 $coursedata[] = " cc.course = :completedcourse{$param['course']} "; 270 $params["completedcourse{$param['course']}"] = $param['course']; 271 } 272 if (!empty($coursedata)) { 273 $extraon = implode(' OR ', $coursedata); 274 $join = " JOIN {course_completions} cc ON cc.userid = u.id AND 275 cc.timecompleted > 0 AND ({$extraon})"; 276 } 277 return array($join, $where, $params); 278 } else { 279 foreach ($this->params as $param) { 280 $join .= " LEFT JOIN {course_completions} cc{$param['course']} ON 281 cc{$param['course']}.userid = u.id AND 282 cc{$param['course']}.course = :completedcourse{$param['course']} AND 283 cc{$param['course']}.timecompleted > 0 "; 284 $where .= " AND cc{$param['course']}.course IS NOT NULL "; 285 $params["completedcourse{$param['course']}"] = $param['course']; 286 } 287 return array($join, $where, $params); 288 } 289 } 290 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |