|
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 class completion_criteria_activity extends completion_criteria { 00027 00032 public $criteriatype = COMPLETION_CRITERIA_TYPE_ACTIVITY; 00033 00041 public static function fetch($params) { 00042 $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_ACTIVITY; 00043 return self::fetch_helper('course_completion_criteria', __CLASS__, $params); 00044 } 00045 00053 public function config_form_display(&$mform, $data = null) { 00054 $mform->addElement('checkbox', 'criteria_activity['.$data->id.']', ucfirst(self::get_mod_name($data->module)).' - '.$data->name); 00055 00056 if ($this->id) { 00057 $mform->setDefault('criteria_activity['.$data->id.']', 1); 00058 } 00059 } 00060 00067 public function update_config(&$data) { 00068 global $DB; 00069 00070 if (!empty($data->criteria_activity) && is_array($data->criteria_activity)) { 00071 00072 $this->course = $data->id; 00073 00074 foreach (array_keys($data->criteria_activity) as $activity) { 00075 00076 $module = $DB->get_record('course_modules', array('id' => $activity)); 00077 $this->module = self::get_mod_name($module->module); 00078 $this->moduleinstance = $activity; 00079 $this->id = NULL; 00080 $this->insert(); 00081 } 00082 } 00083 } 00084 00092 public static function get_mod_name($type) { 00093 static $types; 00094 00095 if (!is_array($types)) { 00096 global $DB; 00097 $types = $DB->get_records('modules'); 00098 } 00099 00100 return $types[$type]->name; 00101 } 00102 00108 public function get_mod_instance() { 00109 global $DB; 00110 00111 return $DB->get_record_sql( 00112 " 00113 SELECT 00114 m.* 00115 FROM 00116 {{$this->module}} m 00117 INNER JOIN 00118 {course_modules} cm 00119 ON cm.id = {$this->moduleinstance} 00120 AND m.id = cm.instance 00121 " 00122 ); 00123 } 00124 00132 public function review($completion, $mark = true) { 00133 global $DB; 00134 00135 $course = $DB->get_record('course', array('id' => $completion->course)); 00136 $cm = $DB->get_record('course_modules', array('id' => $this->moduleinstance)); 00137 $info = new completion_info($course); 00138 00139 $data = $info->get_data($cm, false, $completion->userid); 00140 00141 // If the activity is complete 00142 if (in_array($data->completionstate, array(COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS))) { 00143 if ($mark) { 00144 $completion->mark_complete(); 00145 } 00146 00147 return true; 00148 } 00149 00150 return false; 00151 } 00152 00158 public function get_title() { 00159 return get_string('activitiescompleted', 'completion'); 00160 } 00161 00167 public function get_title_detailed() { 00168 global $DB; 00169 $module = $DB->get_record('course_modules', array('id' => $this->moduleinstance)); 00170 $activity = $DB->get_record($this->module, array('id' => $module->instance)); 00171 00172 return shorten_text(urldecode($activity->name)); 00173 } 00174 00180 public function get_type_title() { 00181 return get_string('activities', 'completion'); 00182 } 00183 00189 public function cron() { 00190 global $DB; 00191 00192 // Get all users who meet this criteria 00193 $sql = ' 00194 SELECT DISTINCT 00195 c.id AS course, 00196 cr.id AS criteriaid, 00197 ra.userid AS userid, 00198 mc.timemodified AS timecompleted 00199 FROM 00200 {course_completion_criteria} cr 00201 INNER JOIN 00202 {course} c 00203 ON cr.course = c.id 00204 INNER JOIN 00205 {context} con 00206 ON con.instanceid = c.id 00207 INNER JOIN 00208 {role_assignments} ra 00209 ON ra.contextid = con.id 00210 INNER JOIN 00211 {course_modules_completion} mc 00212 ON mc.coursemoduleid = cr.moduleinstance 00213 AND mc.userid = ra.userid 00214 LEFT JOIN 00215 {course_completion_crit_compl} cc 00216 ON cc.criteriaid = cr.id 00217 AND cc.userid = ra.userid 00218 WHERE 00219 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_ACTIVITY.' 00220 AND con.contextlevel = '.CONTEXT_COURSE.' 00221 AND c.enablecompletion = 1 00222 AND cc.id IS NULL 00223 AND ( 00224 mc.completionstate = '.COMPLETION_COMPLETE.' 00225 OR mc.completionstate = '.COMPLETION_COMPLETE_PASS.' 00226 ) 00227 '; 00228 00229 // Loop through completions, and mark as complete 00230 $rs = $DB->get_recordset_sql($sql); 00231 foreach ($rs as $record) { 00232 00233 $completion = new completion_criteria_completion((array)$record); 00234 $completion->mark_complete($record->timecompleted); 00235 } 00236 $rs->close(); 00237 } 00238 00245 public function get_details($completion) { 00246 global $DB, $CFG; 00247 00248 // Get completion info 00249 $course = new stdClass(); 00250 $course->id = $completion->course; 00251 $info = new completion_info($course); 00252 00253 $module = $DB->get_record('course_modules', array('id' => $this->moduleinstance)); 00254 $data = $info->get_data($module, false, $completion->userid); 00255 00256 $activity = $DB->get_record($this->module, array('id' => $module->instance)); 00257 00258 $details = array(); 00259 $details['type'] = $this->get_title(); 00260 $details['criteria'] = '<a href="'.$CFG->wwwroot.'/mod/'.$this->module.'/view.php?id='.$this->moduleinstance.'">'.$activity->name.'</a>'; 00261 00262 // Build requirements 00263 $details['requirement'] = array(); 00264 00265 if ($module->completion == 1) { 00266 $details['requirement'][] = get_string('markingyourselfcomplete', 'completion'); 00267 } elseif ($module->completion == 2) { 00268 if ($module->completionview) { 00269 $details['requirement'][] = get_string('viewingactivity', 'completion', $this->module); 00270 } 00271 00272 if (!is_null($module->completiongradeitemnumber)) { 00273 $details['requirement'][] = get_string('achievinggrade', 'completion'); 00274 } 00275 } 00276 00277 $details['requirement'] = implode($details['requirement'], ', '); 00278 00279 $details['status'] = ''; 00280 00281 return $details; 00282 } 00283 }