weekly on-sync release 3.2dev
[moodle.git] / completion / criteria / completion_criteria_activity.php
CommitLineData
2be4d090
MD
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
836375ec
SH
18/**
19 * This file contains the activity completion criteria type class and any
20 * supporting functions it may require.
21 *
22 * @package core_completion
23 * @category completion
24 * @copyright 2009 Catalyst IT Ltd
25 * @author Aaron Barnes <aaronb@catalyst.net.nz>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29defined('MOODLE_INTERNAL') || die();
30
2be4d090
MD
31/**
32 * Course completion critieria - completion on activity completion
33 *
836375ec
SH
34 * @package core_completion
35 * @category completion
2be4d090 36 * @copyright 2009 Catalyst IT Ltd
836375ec
SH
37 * @author Aaron Barnes <aaronb@catalyst.net.nz>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2be4d090
MD
39 */
40class completion_criteria_activity extends completion_criteria {
41
95dd54ee 42 /* @var int Criteria [COMPLETION_CRITERIA_TYPE_ACTIVITY] */
2be4d090
MD
43 public $criteriatype = COMPLETION_CRITERIA_TYPE_ACTIVITY;
44
45 /**
46 * Finds and returns a data_object instance based on params.
2be4d090
MD
47 *
48 * @param array $params associative arrays varname=>value
836375ec 49 * @return completion_criteria_activity data_object instance or false if none found.
2be4d090
MD
50 */
51 public static function fetch($params) {
52 $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_ACTIVITY;
53 return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
54 }
55
56 /**
57 * Add appropriate form elements to the critieria form
95dd54ee 58 *
836375ec 59 * @param moodleform $mform Moodle forms object
95dd54ee 60 * @param stdClass $data details of various modules
2be4d090
MD
61 */
62 public function config_form_display(&$mform, $data = null) {
bdbe385c
MG
63 $modnames = get_module_types_names();
64 $mform->addElement('checkbox', 'criteria_activity['.$data->id.']',
65 $modnames[self::get_mod_name($data->module)].
66 ' - '.
67 format_string($data->name));
2be4d090
MD
68
69 if ($this->id) {
70 $mform->setDefault('criteria_activity['.$data->id.']', 1);
71 }
72 }
73
74 /**
75 * Update the criteria information stored in the database
95dd54ee 76 *
836375ec 77 * @param stdClass $data Form data
2be4d090
MD
78 */
79 public function update_config(&$data) {
80 global $DB;
81
82 if (!empty($data->criteria_activity) && is_array($data->criteria_activity)) {
83
84 $this->course = $data->id;
85
86 foreach (array_keys($data->criteria_activity) as $activity) {
87
88 $module = $DB->get_record('course_modules', array('id' => $activity));
89 $this->module = self::get_mod_name($module->module);
90 $this->moduleinstance = $activity;
91 $this->id = NULL;
92 $this->insert();
93 }
94 }
95 }
96
97 /**
98 * Get module instance module type
95dd54ee 99 *
836375ec
SH
100 * @param int $type Module type id
101 * @return string
2be4d090
MD
102 */
103 public static function get_mod_name($type) {
104 static $types;
105
106 if (!is_array($types)) {
107 global $DB;
108 $types = $DB->get_records('modules');
109 }
110
111 return $types[$type]->name;
112 }
113
114 /**
836375ec
SH
115 * Gets the module instance from the database and returns it.
116 * If no module instance exists this function returns false.
117 *
95dd54ee 118 * @return stdClass|bool
2be4d090
MD
119 */
120 public function get_mod_instance() {
121 global $DB;
122
123 return $DB->get_record_sql(
124 "
125 SELECT
126 m.*
127 FROM
128 {{$this->module}} m
129 INNER JOIN
130 {course_modules} cm
131 ON cm.id = {$this->moduleinstance}
132 AND m.id = cm.instance
133 "
134 );
135 }
136
137 /**
138 * Review this criteria and decide if the user has completed
836375ec
SH
139 *
140 * @param completion_completion $completion The user's completion record
95dd54ee
AA
141 * @param bool $mark Optionally set false to not save changes to database
142 * @return bool
2be4d090
MD
143 */
144 public function review($completion, $mark = true) {
145 global $DB;
146
147 $course = $DB->get_record('course', array('id' => $completion->course));
148 $cm = $DB->get_record('course_modules', array('id' => $this->moduleinstance));
149 $info = new completion_info($course);
150
151 $data = $info->get_data($cm, false, $completion->userid);
152
153 // If the activity is complete
2cc8d562 154 if (in_array($data->completionstate, array(COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS, COMPLETION_COMPLETE_FAIL))) {
2be4d090
MD
155 if ($mark) {
156 $completion->mark_complete();
157 }
158
159 return true;
160 }
161
162 return false;
163 }
164
165 /**
166 * Return criteria title for display in reports
836375ec
SH
167 *
168 * @return string
2be4d090
MD
169 */
170 public function get_title() {
171 return get_string('activitiescompleted', 'completion');
172 }
173
174 /**
175 * Return a more detailed criteria title for display in reports
836375ec 176 *
2be4d090
MD
177 * @return string
178 */
179 public function get_title_detailed() {
180 global $DB;
181 $module = $DB->get_record('course_modules', array('id' => $this->moduleinstance));
182 $activity = $DB->get_record($this->module, array('id' => $module->instance));
183
bdbe385c
MG
184 return shorten_text(format_string($activity->name, true,
185 array('context' => context_module::instance($module->id))));
2be4d090
MD
186 }
187
188 /**
189 * Return criteria type title for display in reports
836375ec 190 *
2be4d090
MD
191 * @return string
192 */
193 public function get_type_title() {
194 return get_string('activities', 'completion');
195 }
196
197 /**
95dd54ee 198 * Find users who have completed this criteria and mark them accordingly
2be4d090
MD
199 */
200 public function cron() {
201 global $DB;
202
203 // Get all users who meet this criteria
204 $sql = '
205 SELECT DISTINCT
206 c.id AS course,
2be4d090
MD
207 cr.id AS criteriaid,
208 ra.userid AS userid,
209 mc.timemodified AS timecompleted
210 FROM
211 {course_completion_criteria} cr
212 INNER JOIN
213 {course} c
214 ON cr.course = c.id
215 INNER JOIN
216 {context} con
217 ON con.instanceid = c.id
218 INNER JOIN
219 {role_assignments} ra
220 ON ra.contextid = con.id
221 INNER JOIN
222 {course_modules_completion} mc
223 ON mc.coursemoduleid = cr.moduleinstance
224 AND mc.userid = ra.userid
225 LEFT JOIN
226 {course_completion_crit_compl} cc
227 ON cc.criteriaid = cr.id
228 AND cc.userid = ra.userid
229 WHERE
230 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_ACTIVITY.'
231 AND con.contextlevel = '.CONTEXT_COURSE.'
232 AND c.enablecompletion = 1
233 AND cc.id IS NULL
234 AND (
235 mc.completionstate = '.COMPLETION_COMPLETE.'
236 OR mc.completionstate = '.COMPLETION_COMPLETE_PASS.'
2cc8d562 237 OR mc.completionstate = '.COMPLETION_COMPLETE_FAIL.'
2be4d090
MD
238 )
239 ';
240
241 // Loop through completions, and mark as complete
419178d7
EL
242 $rs = $DB->get_recordset_sql($sql);
243 foreach ($rs as $record) {
28e0c239 244 $completion = new completion_criteria_completion((array) $record, DATA_OBJECT_FETCH_BY_KEY);
419178d7 245 $completion->mark_complete($record->timecompleted);
2be4d090 246 }
419178d7 247 $rs->close();
2be4d090
MD
248 }
249
250 /**
251 * Return criteria progress details for display in reports
836375ec
SH
252 *
253 * @param completion_completion $completion The user's completion record
254 * @return array An array with the following keys:
255 * type, criteria, requirement, status
2be4d090
MD
256 */
257 public function get_details($completion) {
2be4d090 258 // Get completion info
30a32f14
MG
259 $modinfo = get_fast_modinfo($completion->course);
260 $cm = $modinfo->get_cm($this->moduleinstance);
2be4d090
MD
261
262 $details = array();
263 $details['type'] = $this->get_title();
30a32f14
MG
264 if ($cm->has_view()) {
265 $details['criteria'] = html_writer::link($cm->url, $cm->get_formatted_name());
266 } else {
267 $details['criteria'] = $cm->get_formatted_name();
268 }
2be4d090
MD
269
270 // Build requirements
271 $details['requirement'] = array();
272
30a32f14 273 if ($cm->completion == COMPLETION_TRACKING_MANUAL) {
2be4d090 274 $details['requirement'][] = get_string('markingyourselfcomplete', 'completion');
30a32f14
MG
275 } elseif ($cm->completion == COMPLETION_TRACKING_AUTOMATIC) {
276 if ($cm->completionview) {
2be4d090
MD
277 $details['requirement'][] = get_string('viewingactivity', 'completion', $this->module);
278 }
279
30a32f14 280 if (!is_null($cm->completiongradeitemnumber)) {
2be4d090
MD
281 $details['requirement'][] = get_string('achievinggrade', 'completion');
282 }
283 }
284
285 $details['requirement'] = implode($details['requirement'], ', ');
286
287 $details['status'] = '';
288
289 return $details;
290 }
7062e7c6
CB
291
292 /**
293 * Return pix_icon for display in reports.
294 *
295 * @param string $alt The alt text to use for the icon
296 * @param array $attributes html attributes
297 * @return pix_icon
298 */
299 public function get_icon($alt, array $attributes = null) {
300 return new pix_icon('icon', $alt, 'mod_'.$this->module, $attributes);
301 }
95dd54ee 302}