[ 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 activity 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($CFG->libdir . '/completionlib.php'); 29 30 /** 31 * Badge award criteria -- award on activity completion 32 * 33 */ 34 class award_criteria_activity extends award_criteria { 35 36 /* @var int Criteria [BADGE_CRITERIA_TYPE_ACTIVITY] */ 37 public $criteriatype = BADGE_CRITERIA_TYPE_ACTIVITY; 38 39 private $courseid; 40 private $course; 41 42 public $required_param = 'module'; 43 public $optional_params = array('bydate'); 44 45 public function __construct($record) { 46 global $DB; 47 parent::__construct($record); 48 49 $this->course = $DB->get_record_sql('SELECT c.id, c.enablecompletion, c.cacherev, c.startdate 50 FROM {badge} b INNER JOIN {course} c ON b.courseid = c.id 51 WHERE b.id = :badgeid ', array('badgeid' => $this->badgeid)); 52 $this->courseid = $this->course->id; 53 } 54 55 /** 56 * Gets the module instance from the database and returns it. 57 * If no module instance exists this function returns false. 58 * 59 * @return stdClass|bool 60 */ 61 private function get_mod_instance($cmid) { 62 global $DB; 63 $rec = $DB->get_record_sql("SELECT md.name 64 FROM {course_modules} cm, 65 {modules} md 66 WHERE cm.id = ? AND 67 md.id = cm.module", array($cmid)); 68 69 if ($rec) { 70 return get_coursemodule_from_id($rec->name, $cmid); 71 } else { 72 return null; 73 } 74 } 75 76 /** 77 * Get criteria description for displaying to users 78 * 79 * @return string 80 */ 81 public function get_details($short = '') { 82 global $DB, $OUTPUT; 83 $output = array(); 84 foreach ($this->params as $p) { 85 $mod = self::get_mod_instance($p['module']); 86 if (!$mod) { 87 $str = $OUTPUT->error_text(get_string('error:nosuchmod', 'badges')); 88 } else { 89 $str = html_writer::tag('b', '"' . get_string('modulename', $mod->modname) . ' - ' . $mod->name . '"'); 90 if (isset($p['bydate'])) { 91 $str .= get_string('criteria_descr_bydate', 'badges', userdate($p['bydate'], get_string('strftimedate', 'core_langconfig'))); 92 } 93 } 94 $output[] = $str; 95 } 96 97 if ($short) { 98 return implode(', ', $output); 99 } else { 100 return html_writer::alist($output, array(), 'ul'); 101 } 102 } 103 104 /** 105 * Add appropriate new criteria options to the form 106 * 107 */ 108 public function get_options(&$mform) { 109 $none = true; 110 $existing = array(); 111 $missing = array(); 112 113 $course = $this->course; 114 $info = new completion_info($course); 115 $mods = $info->get_activities(); 116 $mids = array_keys($mods); 117 118 if ($this->id !== 0) { 119 $existing = array_keys($this->params); 120 $missing = array_diff($existing, $mids); 121 } 122 123 if (!empty($missing)) { 124 $mform->addElement('header', 'category_errors', get_string('criterror', 'badges')); 125 $mform->addHelpButton('category_errors', 'criterror', 'badges'); 126 foreach ($missing as $m) { 127 $this->config_options($mform, array('id' => $m, 'checked' => true, 128 'name' => get_string('error:nosuchmod', 'badges'), 'error' => true)); 129 $none = false; 130 } 131 } 132 133 if (!empty($mods)) { 134 $mform->addElement('header', 'first_header', $this->get_title()); 135 foreach ($mods as $mod) { 136 $checked = false; 137 if (in_array($mod->id, $existing)) { 138 $checked = true; 139 } 140 $param = array('id' => $mod->id, 141 'checked' => $checked, 142 'name' => get_string('modulename', $mod->modname) . ' - ' . $mod->name, 143 'error' => false 144 ); 145 146 if ($this->id !== 0 && isset($this->params[$mod->id]['bydate'])) { 147 $param['bydate'] = $this->params[$mod->id]['bydate']; 148 } 149 150 if ($this->id !== 0 && isset($this->params[$mod->id]['grade'])) { 151 $param['grade'] = $this->params[$mod->id]['grade']; 152 } 153 154 $this->config_options($mform, $param); 155 $none = false; 156 } 157 } 158 159 // Add aggregation. 160 if (!$none) { 161 $mform->addElement('header', 'aggregation', get_string('method', 'badges')); 162 $agg = array(); 163 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('allmethodactivity', 'badges'), 1); 164 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('anymethodactivity', 'badges'), 2); 165 $mform->addGroup($agg, 'methodgr', '', array('<br/>'), false); 166 if ($this->id !== 0) { 167 $mform->setDefault('agg', $this->method); 168 } else { 169 $mform->setDefault('agg', BADGE_CRITERIA_AGGREGATION_ANY); 170 } 171 } 172 173 return array($none, get_string('error:noactivities', 'badges')); 174 } 175 176 /** 177 * Review this criteria and decide if it has been completed 178 * 179 * @param int $userid User whose criteria completion needs to be reviewed. 180 * @param bool $filtered An additional parameter indicating that user list 181 * has been reduced and some expensive checks can be skipped. 182 * 183 * @return bool Whether criteria is complete 184 */ 185 public function review($userid, $filtered = false) { 186 $completionstates = array(COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS); 187 188 if ($this->course->startdate > time()) { 189 return false; 190 } 191 192 $info = new completion_info($this->course); 193 194 $overall = false; 195 foreach ($this->params as $param) { 196 $cm = new stdClass(); 197 $cm->id = $param['module']; 198 199 $data = $info->get_data($cm, false, $userid); 200 $check_date = true; 201 202 if (isset($param['bydate'])) { 203 $date = $data->timemodified; 204 $check_date = ($date <= $param['bydate']); 205 } 206 207 if ($this->method == BADGE_CRITERIA_AGGREGATION_ALL) { 208 if (in_array($data->completionstate, $completionstates) && $check_date) { 209 $overall = true; 210 continue; 211 } else { 212 return false; 213 } 214 } else { 215 if (in_array($data->completionstate, $completionstates) && $check_date) { 216 return true; 217 } else { 218 $overall = false; 219 continue; 220 } 221 } 222 } 223 224 return $overall; 225 } 226 227 /** 228 * Returns array with sql code and parameters returning all ids 229 * of users who meet this particular criterion. 230 * 231 * @return array list($join, $where, $params) 232 */ 233 public function get_completed_criteria_sql() { 234 $join = ''; 235 $where = ''; 236 $params = array(); 237 238 if ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) { 239 foreach ($this->params as $param) { 240 $moduledata[] = " cmc.coursemoduleid = :completedmodule{$param['module']} "; 241 $params["completedmodule{$param['module']}"] = $param['module']; 242 } 243 if (!empty($moduledata)) { 244 $extraon = implode(' OR ', $moduledata); 245 $join = " JOIN {course_modules_completion} cmc ON cmc.userid = u.id AND 246 ( cmc.completionstate = :completionpass OR cmc.completionstate = :completioncomplete ) AND ({$extraon})"; 247 $params["completionpass"] = COMPLETION_COMPLETE_PASS; 248 $params["completioncomplete"] = COMPLETION_COMPLETE; 249 } 250 return array($join, $where, $params); 251 } else { 252 foreach ($this->params as $param) { 253 $join .= " LEFT JOIN {course_modules_completion} cmc{$param['module']} ON 254 cmc{$param['module']}.userid = u.id AND 255 cmc{$param['module']}.coursemoduleid = :completedmodule{$param['module']} AND 256 ( cmc{$param['module']}.completionstate = :completionpass{$param['module']} OR 257 cmc{$param['module']}.completionstate = :completioncomplete{$param['module']} )"; 258 $where .= " AND cmc{$param['module']}.coursemoduleid IS NOT NULL "; 259 $params["completedmodule{$param['module']}"] = $param['module']; 260 $params["completionpass{$param['module']}"] = COMPLETION_COMPLETE_PASS; 261 $params["completioncomplete{$param['module']}"] = COMPLETION_COMPLETE; 262 } 263 return array($join, $where, $params); 264 } 265 } 266 }
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 |