weekly release 3.3dev
[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 63 $modnames = get_module_types_names();
6ae9dbc2 64 $mform->addElement('advcheckbox',
65 'criteria_activity['.$data->id.']',
66 $modnames[self::get_mod_name($data->module)] . ' - ' . format_string($data->name),
67 null,
68 array('group' => 1));
2be4d090
MD
69
70 if ($this->id) {
71 $mform->setDefault('criteria_activity['.$data->id.']', 1);
72 }
73 }
74
75 /**
76 * Update the criteria information stored in the database
95dd54ee 77 *
836375ec 78 * @param stdClass $data Form data
2be4d090
MD
79 */
80 public function update_config(&$data) {
81 global $DB;
82
83 if (!empty($data->criteria_activity) && is_array($data->criteria_activity)) {
84
85 $this->course = $data->id;
86
87 foreach (array_keys($data->criteria_activity) as $activity) {
88
89 $module = $DB->get_record('course_modules', array('id' => $activity));
90 $this->module = self::get_mod_name($module->module);
91 $this->moduleinstance = $activity;
92 $this->id = NULL;
93 $this->insert();
94 }
95 }
96 }
97
98 /**
99 * Get module instance module type
95dd54ee 100 *
836375ec
SH
101 * @param int $type Module type id
102 * @return string
2be4d090
MD
103 */
104 public static function get_mod_name($type) {
105 static $types;
106
107 if (!is_array($types)) {
108 global $DB;
109 $types = $DB->get_records('modules');
110 }
111
112 return $types[$type]->name;
113 }
114
115 /**
836375ec
SH
116 * Gets the module instance from the database and returns it.
117 * If no module instance exists this function returns false.
118 *
95dd54ee 119 * @return stdClass|bool
2be4d090
MD
120 */
121 public function get_mod_instance() {
122 global $DB;
123
124 return $DB->get_record_sql(
125 "
126 SELECT
127 m.*
128 FROM
129 {{$this->module}} m
130 INNER JOIN
131 {course_modules} cm
132 ON cm.id = {$this->moduleinstance}
133 AND m.id = cm.instance
134 "
135 );
136 }
137
138 /**
139 * Review this criteria and decide if the user has completed
836375ec
SH
140 *
141 * @param completion_completion $completion The user's completion record
95dd54ee
AA
142 * @param bool $mark Optionally set false to not save changes to database
143 * @return bool
2be4d090
MD
144 */
145 public function review($completion, $mark = true) {
146 global $DB;
147
148 $course = $DB->get_record('course', array('id' => $completion->course));
149 $cm = $DB->get_record('course_modules', array('id' => $this->moduleinstance));
150 $info = new completion_info($course);
151
152 $data = $info->get_data($cm, false, $completion->userid);
153
154 // If the activity is complete
2cc8d562 155 if (in_array($data->completionstate, array(COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS, COMPLETION_COMPLETE_FAIL))) {
2be4d090
MD
156 if ($mark) {
157 $completion->mark_complete();
158 }
159
160 return true;
161 }
162
163 return false;
164 }
165
166 /**
167 * Return criteria title for display in reports
836375ec
SH
168 *
169 * @return string
2be4d090
MD
170 */
171 public function get_title() {
172 return get_string('activitiescompleted', 'completion');
173 }
174
175 /**
176 * Return a more detailed criteria title for display in reports
836375ec 177 *
2be4d090
MD
178 * @return string
179 */
180 public function get_title_detailed() {
181 global $DB;
182 $module = $DB->get_record('course_modules', array('id' => $this->moduleinstance));
183 $activity = $DB->get_record($this->module, array('id' => $module->instance));
184
bdbe385c
MG
185 return shorten_text(format_string($activity->name, true,
186 array('context' => context_module::instance($module->id))));
2be4d090
MD
187 }
188
189 /**
190 * Return criteria type title for display in reports
836375ec 191 *
2be4d090
MD
192 * @return string
193 */
194 public function get_type_title() {
195 return get_string('activities', 'completion');
196 }
197
198 /**
95dd54ee 199 * Find users who have completed this criteria and mark them accordingly
2be4d090
MD
200 */
201 public function cron() {
202 global $DB;
203
204 // Get all users who meet this criteria
205 $sql = '
206 SELECT DISTINCT
207 c.id AS course,
2be4d090
MD
208 cr.id AS criteriaid,
209 ra.userid AS userid,
210 mc.timemodified AS timecompleted
211 FROM
212 {course_completion_criteria} cr
213 INNER JOIN
214 {course} c
215 ON cr.course = c.id
216 INNER JOIN
217 {context} con
218 ON con.instanceid = c.id
219 INNER JOIN
220 {role_assignments} ra
221 ON ra.contextid = con.id
222 INNER JOIN
223 {course_modules_completion} mc
224 ON mc.coursemoduleid = cr.moduleinstance
225 AND mc.userid = ra.userid
226 LEFT JOIN
227 {course_completion_crit_compl} cc
228 ON cc.criteriaid = cr.id
229 AND cc.userid = ra.userid
230 WHERE
231 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_ACTIVITY.'
232 AND con.contextlevel = '.CONTEXT_COURSE.'
233 AND c.enablecompletion = 1
234 AND cc.id IS NULL
235 AND (
236 mc.completionstate = '.COMPLETION_COMPLETE.'
237 OR mc.completionstate = '.COMPLETION_COMPLETE_PASS.'
2cc8d562 238 OR mc.completionstate = '.COMPLETION_COMPLETE_FAIL.'
2be4d090
MD
239 )
240 ';
241
242 // Loop through completions, and mark as complete
419178d7
EL
243 $rs = $DB->get_recordset_sql($sql);
244 foreach ($rs as $record) {
28e0c239 245 $completion = new completion_criteria_completion((array) $record, DATA_OBJECT_FETCH_BY_KEY);
419178d7 246 $completion->mark_complete($record->timecompleted);
2be4d090 247 }
419178d7 248 $rs->close();
2be4d090
MD
249 }
250
251 /**
252 * Return criteria progress details for display in reports
836375ec
SH
253 *
254 * @param completion_completion $completion The user's completion record
255 * @return array An array with the following keys:
256 * type, criteria, requirement, status
2be4d090
MD
257 */
258 public function get_details($completion) {
2be4d090 259 // Get completion info
30a32f14
MG
260 $modinfo = get_fast_modinfo($completion->course);
261 $cm = $modinfo->get_cm($this->moduleinstance);
2be4d090
MD
262
263 $details = array();
264 $details['type'] = $this->get_title();
30a32f14
MG
265 if ($cm->has_view()) {
266 $details['criteria'] = html_writer::link($cm->url, $cm->get_formatted_name());
267 } else {
268 $details['criteria'] = $cm->get_formatted_name();
269 }
2be4d090
MD
270
271 // Build requirements
272 $details['requirement'] = array();
273
30a32f14 274 if ($cm->completion == COMPLETION_TRACKING_MANUAL) {
2be4d090 275 $details['requirement'][] = get_string('markingyourselfcomplete', 'completion');
30a32f14
MG
276 } elseif ($cm->completion == COMPLETION_TRACKING_AUTOMATIC) {
277 if ($cm->completionview) {
2be4d090
MD
278 $details['requirement'][] = get_string('viewingactivity', 'completion', $this->module);
279 }
280
30a32f14 281 if (!is_null($cm->completiongradeitemnumber)) {
2be4d090
MD
282 $details['requirement'][] = get_string('achievinggrade', 'completion');
283 }
284 }
285
286 $details['requirement'] = implode($details['requirement'], ', ');
287
288 $details['status'] = '';
289
290 return $details;
291 }
7062e7c6
CB
292
293 /**
294 * Return pix_icon for display in reports.
295 *
296 * @param string $alt The alt text to use for the icon
297 * @param array $attributes html attributes
298 * @return pix_icon
299 */
300 public function get_icon($alt, array $attributes = null) {
301 return new pix_icon('icon', $alt, 'mod_'.$this->module, $attributes);
302 }
95dd54ee 303}