Commit | Line | Data |
---|---|---|
4636bf83 | 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/>. | |
0d22e7cc | 17 | |
4636bf83 | 18 | /** |
c7afe6b7 | 19 | * @package mod_choice |
4636bf83 | 20 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} |
21 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
22 | */ | |
23 | ||
73b1d580 SB |
24 | defined('MOODLE_INTERNAL') || die(); |
25 | ||
17da2e6f | 26 | /** @global int $CHOICE_COLUMN_HEIGHT */ |
27 | global $CHOICE_COLUMN_HEIGHT; | |
28 | $CHOICE_COLUMN_HEIGHT = 300; | |
6d6c9389 | 29 | |
315d4971 RW |
30 | /** @global int $CHOICE_COLUMN_WIDTH */ |
31 | global $CHOICE_COLUMN_WIDTH; | |
32 | $CHOICE_COLUMN_WIDTH = 300; | |
33 | ||
8f7dc7f1 | 34 | define('CHOICE_PUBLISH_ANONYMOUS', '0'); |
35 | define('CHOICE_PUBLISH_NAMES', '1'); | |
6d6c9389 | 36 | |
c100a341 | 37 | define('CHOICE_SHOWRESULTS_NOT', '0'); |
38 | define('CHOICE_SHOWRESULTS_AFTER_ANSWER', '1'); | |
39 | define('CHOICE_SHOWRESULTS_AFTER_CLOSE', '2'); | |
40 | define('CHOICE_SHOWRESULTS_ALWAYS', '3'); | |
f9af7fd0 | 41 | |
6fd87e3b | 42 | define('CHOICE_DISPLAY_HORIZONTAL', '0'); |
43 | define('CHOICE_DISPLAY_VERTICAL', '1'); | |
44 | ||
213dcf51 MN |
45 | define('CHOICE_EVENT_TYPE_OPEN', 'open'); |
46 | define('CHOICE_EVENT_TYPE_CLOSE', 'close'); | |
47 | ||
4636bf83 | 48 | /** @global array $CHOICE_PUBLISH */ |
17da2e6f | 49 | global $CHOICE_PUBLISH; |
8f7dc7f1 | 50 | $CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS => get_string('publishanonymous', 'choice'), |
51 | CHOICE_PUBLISH_NAMES => get_string('publishnames', 'choice')); | |
6d6c9389 | 52 | |
4636bf83 | 53 | /** @global array $CHOICE_SHOWRESULTS */ |
17da2e6f | 54 | global $CHOICE_SHOWRESULTS; |
c100a341 | 55 | $CHOICE_SHOWRESULTS = array (CHOICE_SHOWRESULTS_NOT => get_string('publishnot', 'choice'), |
56 | CHOICE_SHOWRESULTS_AFTER_ANSWER => get_string('publishafteranswer', 'choice'), | |
57 | CHOICE_SHOWRESULTS_AFTER_CLOSE => get_string('publishafterclose', 'choice'), | |
58 | CHOICE_SHOWRESULTS_ALWAYS => get_string('publishalways', 'choice')); | |
f9af7fd0 | 59 | |
4636bf83 | 60 | /** @global array $CHOICE_DISPLAY */ |
17da2e6f | 61 | global $CHOICE_DISPLAY; |
6fd87e3b | 62 | $CHOICE_DISPLAY = array (CHOICE_DISPLAY_HORIZONTAL => get_string('displayhorizontal', 'choice'), |
63 | CHOICE_DISPLAY_VERTICAL => get_string('displayvertical','choice')); | |
6d6c9389 | 64 | |
65 | /// Standard functions ///////////////////////////////////////////////////////// | |
66 | ||
4636bf83 | 67 | /** |
68 | * @global object | |
69 | * @param object $course | |
70 | * @param object $user | |
71 | * @param object $mod | |
72 | * @param object $choice | |
73 | * @return object|null | |
74 | */ | |
0d22e7cc | 75 | function choice_user_outline($course, $user, $mod, $choice) { |
407caeeb | 76 | global $DB; |
77 | if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $user->id))) { | |
39790bd8 | 78 | $result = new stdClass(); |
7cfc54b9 | 79 | $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'"; |
d1a67362 | 80 | $result->time = $answer->timemodified; |
0d22e7cc | 81 | return $result; |
82 | } | |
83 | return NULL; | |
84 | } | |
85 | ||
4636bf83 | 86 | /** |
42dfbb54 MG |
87 | * Callback for the "Complete" report - prints the activity summary for the given user |
88 | * | |
4636bf83 | 89 | * @param object $course |
90 | * @param object $user | |
91 | * @param object $mod | |
92 | * @param object $choice | |
4636bf83 | 93 | */ |
0d22e7cc | 94 | function choice_user_complete($course, $user, $mod, $choice) { |
407caeeb | 95 | global $DB; |
42dfbb54 MG |
96 | if ($answers = $DB->get_records('choice_answers', array("choiceid" => $choice->id, "userid" => $user->id))) { |
97 | $info = []; | |
98 | foreach ($answers as $answer) { | |
99 | $info[] = "'" . format_string(choice_get_option_text($choice, $answer->optionid)) . "'"; | |
100 | } | |
101 | core_collator::asort($info); | |
102 | echo get_string("answered", "choice") . ": ". join(', ', $info) . ". " . | |
103 | get_string("updated", '', userdate($answer->timemodified)); | |
0d22e7cc | 104 | } else { |
c4016bc1 | 105 | print_string("notanswered", "choice"); |
0d22e7cc | 106 | } |
107 | } | |
108 | ||
4636bf83 | 109 | /** |
110 | * Given an object containing all the necessary data, | |
111 | * (defined by the form in mod_form.php) this function | |
112 | * will create a new instance and return the id number | |
113 | * of the new instance. | |
114 | * | |
115 | * @global object | |
116 | * @param object $choice | |
117 | * @return int | |
118 | */ | |
04eba58f | 119 | function choice_add_instance($choice) { |
9be80529 SB |
120 | global $DB, $CFG; |
121 | require_once($CFG->dirroot.'/mod/choice/locallib.php'); | |
04eba58f | 122 | |
123 | $choice->timemodified = time(); | |
124 | ||
ff8d654f | 125 | //insert answers |
9d97f08e PS |
126 | $choice->id = $DB->insert_record("choice", $choice); |
127 | foreach ($choice->option as $key => $value) { | |
128 | $value = trim($value); | |
129 | if (isset($value) && $value <> '') { | |
39790bd8 | 130 | $option = new stdClass(); |
9d97f08e PS |
131 | $option->text = $value; |
132 | $option->choiceid = $choice->id; | |
133 | if (isset($choice->limit[$key])) { | |
134 | $option->maxanswers = $choice->limit[$key]; | |
6fd87e3b | 135 | } |
9d97f08e PS |
136 | $option->timemodified = time(); |
137 | $DB->insert_record("choice_options", $option); | |
6fd87e3b | 138 | } |
139 | } | |
9d97f08e | 140 | |
9be80529 SB |
141 | // Add calendar events if necessary. |
142 | choice_set_events($choice); | |
143 | ||
a77a6009 | 144 | return $choice->id; |
04eba58f | 145 | } |
146 | ||
4636bf83 | 147 | /** |
148 | * Given an object containing all the necessary data, | |
149 | * (defined by the form in mod_form.php) this function | |
150 | * will update an existing instance with new data. | |
151 | * | |
152 | * @global object | |
153 | * @param object $choice | |
154 | * @return bool | |
155 | */ | |
04eba58f | 156 | function choice_update_instance($choice) { |
9be80529 SB |
157 | global $DB, $CFG; |
158 | require_once($CFG->dirroot.'/mod/choice/locallib.php'); | |
04eba58f | 159 | |
160 | $choice->id = $choice->instance; | |
161 | $choice->timemodified = time(); | |
162 | ||
ebd3c7ac | 163 | //update, delete or insert answers |
164 | foreach ($choice->option as $key => $value) { | |
a77a6009 | 165 | $value = trim($value); |
39790bd8 | 166 | $option = new stdClass(); |
ebd3c7ac | 167 | $option->text = $value; |
168 | $option->choiceid = $choice->id; | |
169 | if (isset($choice->limit[$key])) { | |
170 | $option->maxanswers = $choice->limit[$key]; | |
171 | } | |
172 | $option->timemodified = time(); | |
a2e67a9a | 173 | if (isset($choice->optionid[$key]) && !empty($choice->optionid[$key])){//existing choice record |
ebd3c7ac | 174 | $option->id=$choice->optionid[$key]; |
af834f3e | 175 | if (isset($value) && $value <> '') { |
c18269c7 | 176 | $DB->update_record("choice_options", $option); |
025188da SB |
177 | } else { |
178 | // Remove the empty (unused) option. | |
179 | $DB->delete_records("choice_options", array("id" => $option->id)); | |
180 | // Delete any answers associated with this option. | |
181 | $DB->delete_records("choice_answers", array("choiceid" => $choice->id, "optionid" => $option->id)); | |
c9ff813c | 182 | } |
ebd3c7ac | 183 | } else { |
184 | if (isset($value) && $value <> '') { | |
c18269c7 | 185 | $DB->insert_record("choice_options", $option); |
a77a6009 | 186 | } |
ff8d654f | 187 | } |
6fd87e3b | 188 | } |
d30772b2 | 189 | |
9be80529 SB |
190 | // Add calendar events if necessary. |
191 | choice_set_events($choice); | |
192 | ||
c18269c7 | 193 | return $DB->update_record('choice', $choice); |
ff8d654f | 194 | |
04eba58f | 195 | } |
196 | ||
4636bf83 | 197 | /** |
198 | * @global object | |
199 | * @param object $choice | |
200 | * @param object $user | |
315d4971 | 201 | * @param object $coursemodule |
4636bf83 | 202 | * @param array $allresponses |
315d4971 | 203 | * @return array |
4636bf83 | 204 | */ |
315d4971 | 205 | function choice_prepare_options($choice, $user, $coursemodule, $allresponses) { |
407caeeb | 206 | global $DB; |
3c56a7cc | 207 | |
315d4971 | 208 | $cdisplay = array('options'=>array()); |
a61e4188 | 209 | |
315d4971 | 210 | $cdisplay['limitanswers'] = true; |
327c67a9 | 211 | $context = context_module::instance($coursemodule->id); |
3c56a7cc | 212 | |
348630d8 | 213 | foreach ($choice->option as $optionid => $text) { |
a5f35b9f | 214 | if (isset($text)) { //make sure there are no dud entries in the db with blank text values. |
315d4971 RW |
215 | $option = new stdClass; |
216 | $option->attributes = new stdClass; | |
217 | $option->attributes->value = $optionid; | |
ba939078 | 218 | $option->text = format_string($text); |
315d4971 RW |
219 | $option->maxanswers = $choice->maxanswers[$optionid]; |
220 | $option->displaylayout = $choice->display; | |
221 | ||
55963a3a | 222 | if (isset($allresponses[$optionid])) { |
315d4971 | 223 | $option->countanswers = count($allresponses[$optionid]); |
55963a3a | 224 | } else { |
315d4971 | 225 | $option->countanswers = 0; |
55963a3a | 226 | } |
315d4971 RW |
227 | if ($DB->record_exists('choice_answers', array('choiceid' => $choice->id, 'userid' => $user->id, 'optionid' => $optionid))) { |
228 | $option->attributes->checked = true; | |
dabfd0ed | 229 | } |
315d4971 RW |
230 | if ( $choice->limitanswers && ($option->countanswers >= $option->maxanswers) && empty($option->attributes->checked)) { |
231 | $option->attributes->disabled = true; | |
dabfd0ed | 232 | } |
e076641f | 233 | $cdisplay['options'][] = $option; |
348630d8 | 234 | } |
e076641f | 235 | } |
348630d8 | 236 | |
315d4971 | 237 | $cdisplay['hascapability'] = is_enrolled($context, NULL, 'mod/choice:choose'); //only enrolled users are allowed to make a choice |
ebd3c7ac | 238 | |
315d4971 RW |
239 | if ($choice->allowupdate && $DB->record_exists('choice_answers', array('choiceid'=> $choice->id, 'userid'=> $user->id))) { |
240 | $cdisplay['allowupdate'] = true; | |
e076641f | 241 | } |
348630d8 | 242 | |
c30dbd69 TB |
243 | if ($choice->showpreview && $choice->timeopen > time()) { |
244 | $cdisplay['previewonly'] = true; | |
245 | } | |
246 | ||
315d4971 | 247 | return $cdisplay; |
e076641f | 248 | } |
348630d8 | 249 | |
8676e7c3 MG |
250 | /** |
251 | * Modifies responses of other users adding the option $newoptionid to them | |
252 | * | |
253 | * @param array $userids list of users to add option to (must be users without any answers yet) | |
254 | * @param array $answerids list of existing attempt ids of users (will be either appended or | |
255 | * substituted with the newoptionid, depending on $choice->allowmultiple) | |
256 | * @param int $newoptionid | |
257 | * @param stdClass $choice choice object, result of {@link choice_get_choice()} | |
258 | * @param stdClass $cm | |
259 | * @param stdClass $course | |
260 | */ | |
261 | function choice_modify_responses($userids, $answerids, $newoptionid, $choice, $cm, $course) { | |
262 | // Get all existing responses and the list of non-respondents. | |
263 | $groupmode = groups_get_activity_groupmode($cm); | |
264 | $onlyactive = $choice->includeinactive ? false : true; | |
265 | $allresponses = choice_get_response_data($choice, $cm, $groupmode, $onlyactive); | |
266 | ||
267 | // Check that the option value is valid. | |
268 | if (!$newoptionid || !isset($choice->option[$newoptionid])) { | |
269 | return; | |
270 | } | |
271 | ||
272 | // First add responses for users who did not make any choice yet. | |
273 | foreach ($userids as $userid) { | |
274 | if (isset($allresponses[0][$userid])) { | |
275 | choice_user_submit_response($newoptionid, $choice, $userid, $course, $cm); | |
276 | } | |
277 | } | |
278 | ||
279 | // Create the list of all options already selected by each user. | |
280 | $optionsbyuser = []; // Mapping userid=>array of chosen choice options. | |
281 | $usersbyanswer = []; // Mapping answerid=>userid (which answer belongs to each user). | |
282 | foreach ($allresponses as $optionid => $responses) { | |
283 | if ($optionid > 0) { | |
284 | foreach ($responses as $userid => $userresponse) { | |
285 | $optionsbyuser += [$userid => []]; | |
286 | $optionsbyuser[$userid][] = $optionid; | |
287 | $usersbyanswer[$userresponse->answerid] = $userid; | |
288 | } | |
289 | } | |
290 | } | |
291 | ||
292 | // Go through the list of submitted attemptids and find which users answers need to be updated. | |
293 | foreach ($answerids as $answerid) { | |
294 | if (isset($usersbyanswer[$answerid])) { | |
295 | $userid = $usersbyanswer[$answerid]; | |
296 | if (!in_array($newoptionid, $optionsbyuser[$userid])) { | |
297 | $options = $choice->allowmultiple ? | |
298 | array_merge($optionsbyuser[$userid], [$newoptionid]) : $newoptionid; | |
299 | choice_user_submit_response($options, $choice, $userid, $course, $cm); | |
300 | } | |
301 | } | |
302 | } | |
303 | } | |
304 | ||
4636bf83 | 305 | /** |
e776b415 ZD |
306 | * Process user submitted answers for a choice, |
307 | * and either updating them or saving new answers. | |
308 | * | |
309 | * @param int $formanswer users submitted answers. | |
310 | * @param object $choice the selected choice. | |
311 | * @param int $userid user identifier. | |
312 | * @param object $course current course. | |
313 | * @param object $cm course context. | |
314 | * @return void | |
4636bf83 | 315 | */ |
bc499733 | 316 | function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm) { |
8676e7c3 | 317 | global $DB, $CFG, $USER; |
516c5eca PS |
318 | require_once($CFG->libdir.'/completionlib.php'); |
319 | ||
5b83949a ZD |
320 | $continueurl = new moodle_url('/mod/choice/view.php', array('id' => $cm->id)); |
321 | ||
54bdf1d4 | 322 | if (empty($formanswer)) { |
5b83949a | 323 | print_error('atleastoneoption', 'choice', $continueurl); |
54bdf1d4 MA |
324 | } |
325 | ||
326 | if (is_array($formanswer)) { | |
327 | if (!$choice->allowmultiple) { | |
5b83949a | 328 | print_error('multiplenotallowederror', 'choice', $continueurl); |
54bdf1d4 MA |
329 | } |
330 | $formanswers = $formanswer; | |
331 | } else { | |
332 | $formanswers = array($formanswer); | |
333 | } | |
334 | ||
0bca1297 DW |
335 | $options = $DB->get_records('choice_options', array('choiceid' => $choice->id), '', 'id'); |
336 | foreach ($formanswers as $key => $val) { | |
337 | if (!isset($options[$val])) { | |
338 | print_error('cannotsubmit', 'choice', $continueurl); | |
339 | } | |
340 | } | |
e776b415 | 341 | // Start lock to prevent synchronous access to the same data |
5b83949a ZD |
342 | // before it's updated, if using limits. |
343 | if ($choice->limitanswers) { | |
344 | $timeout = 10; | |
345 | $locktype = 'mod_choice_choice_user_submit_response'; | |
346 | // Limiting access to this choice. | |
347 | $resouce = 'choiceid:' . $choice->id; | |
348 | $lockfactory = \core\lock\lock_config::get_lock_factory($locktype); | |
349 | ||
350 | // Opening the lock. | |
0c599426 | 351 | $choicelock = $lockfactory->get_lock($resouce, $timeout, MINSECS); |
5b83949a ZD |
352 | if (!$choicelock) { |
353 | print_error('cannotsubmit', 'choice', $continueurl); | |
354 | } | |
e776b415 ZD |
355 | } |
356 | ||
54bdf1d4 | 357 | $current = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid)); |
327c67a9 | 358 | $context = context_module::instance($cm->id); |
c75ba879 | 359 | |
cbb1677c | 360 | $choicesexceeded = false; |
54bdf1d4 MA |
361 | $countanswers = array(); |
362 | foreach ($formanswers as $val) { | |
363 | $countanswers[$val] = 0; | |
364 | } | |
c75ba879 | 365 | if($choice->limitanswers) { |
366 | // Find out whether groups are being used and enabled | |
367 | if (groups_get_activity_groupmode($cm) > 0) { | |
368 | $currentgroup = groups_get_activity_group($cm); | |
369 | } else { | |
370 | $currentgroup = 0; | |
371 | } | |
54bdf1d4 MA |
372 | |
373 | list ($insql, $params) = $DB->get_in_or_equal($formanswers, SQL_PARAMS_NAMED); | |
374 | ||
c75ba879 | 375 | if($currentgroup) { |
376 | // If groups are being used, retrieve responses only for users in | |
377 | // current group | |
378 | global $CFG; | |
54bdf1d4 MA |
379 | |
380 | $params['groupid'] = $currentgroup; | |
cbb1677c SH |
381 | $sql = "SELECT ca.* |
382 | FROM {choice_answers} ca | |
383 | INNER JOIN {groups_members} gm ON ca.userid=gm.userid | |
384 | WHERE optionid $insql | |
385 | AND gm.groupid= :groupid"; | |
c75ba879 | 386 | } else { |
387 | // Groups are not used, retrieve all answers for this option ID | |
cbb1677c SH |
388 | $sql = "SELECT ca.* |
389 | FROM {choice_answers} ca | |
390 | WHERE optionid $insql"; | |
ff8d654f | 391 | } |
392 | ||
cbb1677c | 393 | $answers = $DB->get_records_sql($sql, $params); |
c75ba879 | 394 | if ($answers) { |
395 | foreach ($answers as $a) { //only return enrolled users. | |
4f0c2d00 | 396 | if (is_enrolled($context, $a->userid, 'mod/choice:choose')) { |
54bdf1d4 | 397 | $countanswers[$a->optionid]++; |
c75ba879 | 398 | } |
399 | } | |
400 | } | |
54bdf1d4 | 401 | foreach ($countanswers as $opt => $count) { |
e776b415 | 402 | if ($count >= $choice->maxanswers[$opt]) { |
54bdf1d4 MA |
403 | $choicesexceeded = true; |
404 | break; | |
405 | } | |
406 | } | |
39b3e7de | 407 | } |
ff8d654f | 408 | |
cbb1677c | 409 | // Check the user hasn't exceeded the maximum selections for the choice(s) they have selected. |
8676e7c3 MG |
410 | $answersnapshots = array(); |
411 | $deletedanswersnapshots = array(); | |
54bdf1d4 | 412 | if (!($choice->limitanswers && $choicesexceeded)) { |
39b3e7de | 413 | if ($current) { |
e776b415 | 414 | // Update an existing answer. |
54bdf1d4 MA |
415 | $existingchoices = array(); |
416 | foreach ($current as $c) { | |
417 | if (in_array($c->optionid, $formanswers)) { | |
418 | $existingchoices[] = $c->optionid; | |
cbb1677c | 419 | $DB->set_field('choice_answers', 'timemodified', time(), array('id' => $c->id)); |
54bdf1d4 | 420 | } else { |
8676e7c3 | 421 | $deletedanswersnapshots[] = $c; |
54bdf1d4 MA |
422 | $DB->delete_records('choice_answers', array('id' => $c->id)); |
423 | } | |
424 | } | |
425 | ||
426 | // Add new ones. | |
427 | foreach ($formanswers as $f) { | |
428 | if (!in_array($f, $existingchoices)) { | |
429 | $newanswer = new stdClass(); | |
430 | $newanswer->optionid = $f; | |
431 | $newanswer->choiceid = $choice->id; | |
432 | $newanswer->userid = $userid; | |
433 | $newanswer->timemodified = time(); | |
434 | $newanswer->id = $DB->insert_record("choice_answers", $newanswer); | |
435 | $answersnapshots[] = $newanswer; | |
436 | } | |
437 | } | |
39b3e7de | 438 | } else { |
e776b415 | 439 | // Add new answer. |
54bdf1d4 MA |
440 | foreach ($formanswers as $answer) { |
441 | $newanswer = new stdClass(); | |
442 | $newanswer->choiceid = $choice->id; | |
443 | $newanswer->userid = $userid; | |
444 | $newanswer->optionid = $answer; | |
445 | $newanswer->timemodified = time(); | |
446 | $newanswer->id = $DB->insert_record("choice_answers", $newanswer); | |
447 | $answersnapshots[] = $newanswer; | |
448 | } | |
bc499733 SM |
449 | |
450 | // Update completion state | |
451 | $completion = new completion_info($course); | |
452 | if ($completion->is_enabled($cm) && $choice->completionsubmit) { | |
453 | $completion->update_state($cm, COMPLETION_COMPLETE); | |
454 | } | |
39b3e7de | 455 | } |
456 | } else { | |
54bdf1d4 MA |
457 | // Check to see if current choice already selected - if not display error. |
458 | $currentids = array_keys($current); | |
e776b415 | 459 | |
54bdf1d4 | 460 | if (array_diff($currentids, $formanswers) || array_diff($formanswers, $currentids) ) { |
e776b415 ZD |
461 | // Release lock before error. |
462 | $choicelock->release(); | |
5b83949a | 463 | print_error('choicefull', 'choice', $continueurl); |
39b3e7de | 464 | } |
465 | } | |
e776b415 ZD |
466 | |
467 | // Release lock. | |
5b83949a ZD |
468 | if (isset($choicelock)) { |
469 | $choicelock->release(); | |
470 | } | |
e776b415 | 471 | |
8676e7c3 MG |
472 | // Trigger events. |
473 | foreach ($deletedanswersnapshots as $answer) { | |
474 | \mod_choice\event\answer_deleted::create_from_object($answer, $choice, $cm, $course)->trigger(); | |
475 | } | |
476 | foreach ($answersnapshots as $answer) { | |
477 | \mod_choice\event\answer_created::create_from_object($answer, $choice, $cm, $course)->trigger(); | |
39b3e7de | 478 | } |
348630d8 | 479 | } |
480 | ||
4636bf83 | 481 | /** |
482 | * @param array $user | |
483 | * @param object $cm | |
484 | * @return void Output is echo'd | |
485 | */ | |
a61e4188 | 486 | function choice_show_reportlink($user, $cm) { |
54bdf1d4 | 487 | $userschosen = array(); |
a61e4188 | 488 | foreach($user as $optionid => $userlist) { |
489 | if ($optionid) { | |
54bdf1d4 | 490 | $userschosen = array_merge($userschosen, array_keys($userlist)); |
a61e4188 | 491 | } |
7b360f33 | 492 | } |
54bdf1d4 | 493 | $responsecount = count(array_unique($userschosen)); |
5c880488 | 494 | |
39b3e7de | 495 | echo '<div class="reportlink">'; |
5c880488 | 496 | echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>"; |
39b3e7de | 497 | echo '</div>'; |
348630d8 | 498 | } |
499 | ||
4636bf83 | 500 | /** |
501 | * @global object | |
4636bf83 | 502 | * @param object $choice |
503 | * @param object $course | |
315d4971 | 504 | * @param object $coursemodule |
4636bf83 | 505 | * @param array $allresponses |
315d4971 RW |
506 | |
507 | * * @param bool $allresponses | |
508 | * @return object | |
4636bf83 | 509 | */ |
634a5c0b MG |
510 | function prepare_choice_show_results($choice, $course, $cm, $allresponses) { |
511 | global $OUTPUT; | |
407caeeb | 512 | |
315d4971 RW |
513 | $display = clone($choice); |
514 | $display->coursemoduleid = $cm->id; | |
f7e93000 RW |
515 | $display->courseid = $course->id; |
516 | ||
8676e7c3 MG |
517 | if (!empty($choice->showunanswered)) { |
518 | $choice->option[0] = get_string('notanswered', 'choice'); | |
519 | $choice->maxanswers[0] = 0; | |
520 | } | |
521 | ||
522 | // Remove from the list of non-respondents the users who do not have access to this activity. | |
523 | if (!empty($display->showunanswered) && $allresponses[0]) { | |
524 | $info = new \core_availability\info_module(cm_info::create($cm)); | |
525 | $allresponses[0] = $info->filter_user_list($allresponses[0]); | |
526 | } | |
527 | ||
315d4971 RW |
528 | //overwrite options value; |
529 | $display->options = array(); | |
540200ae | 530 | $allusers = []; |
315d4971 RW |
531 | foreach ($choice->option as $optionid => $optiontext) { |
532 | $display->options[$optionid] = new stdClass; | |
533 | $display->options[$optionid]->text = $optiontext; | |
534 | $display->options[$optionid]->maxanswer = $choice->maxanswers[$optionid]; | |
535 | ||
536 | if (array_key_exists($optionid, $allresponses)) { | |
e076641f | 537 | $display->options[$optionid]->user = $allresponses[$optionid]; |
540200ae | 538 | $allusers = array_merge($allusers, array_keys($allresponses[$optionid])); |
9d624d66 | 539 | } |
315d4971 RW |
540 | } |
541 | unset($display->option); | |
542 | unset($display->maxanswers); | |
543 | ||
540200ae | 544 | $display->numberofuser = count(array_unique($allusers)); |
327c67a9 | 545 | $context = context_module::instance($cm->id); |
315d4971 RW |
546 | $display->viewresponsecapability = has_capability('mod/choice:readresponses', $context); |
547 | $display->deleterepsonsecapability = has_capability('mod/choice:deleteresponses',$context); | |
548 | $display->fullnamecapability = has_capability('moodle/site:viewfullnames', $context); | |
f7e93000 | 549 | |
9acb7700 | 550 | if (empty($allresponses)) { |
8f0767f1 | 551 | echo $OUTPUT->heading(get_string("nousersyet"), 3, null); |
9acb7700 | 552 | return false; |
39b3e7de | 553 | } |
c2a4d016 | 554 | |
315d4971 | 555 | return $display; |
9d624d66 | 556 | } |
348630d8 | 557 | |
4636bf83 | 558 | /** |
559 | * @global object | |
560 | * @param array $attemptids | |
bc499733 SM |
561 | * @param object $choice Choice main table row |
562 | * @param object $cm Course-module object | |
563 | * @param object $course Course object | |
4636bf83 | 564 | * @return bool |
565 | */ | |
bc499733 | 566 | function choice_delete_responses($attemptids, $choice, $cm, $course) { |
e62f6a01 | 567 | global $DB, $CFG, $USER; |
516c5eca PS |
568 | require_once($CFG->libdir.'/completionlib.php'); |
569 | ||
dabfd0ed | 570 | if(!is_array($attemptids) || empty($attemptids)) { |
348630d8 | 571 | return false; |
572 | } | |
573 | ||
574 | foreach($attemptids as $num => $attemptid) { | |
575 | if(empty($attemptid)) { | |
576 | unset($attemptids[$num]); | |
577 | } | |
578 | } | |
579 | ||
bc499733 | 580 | $completion = new completion_info($course); |
348630d8 | 581 | foreach($attemptids as $attemptid) { |
54bdf1d4 | 582 | if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'id' => $attemptid))) { |
e62f6a01 | 583 | // Trigger the event answer deleted. |
8676e7c3 | 584 | \mod_choice\event\answer_deleted::create_from_object($todelete, $choice, $cm, $course)->trigger(); |
54bdf1d4 | 585 | $DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'id' => $attemptid)); |
348630d8 | 586 | } |
ebd3c7ac | 587 | } |
da826886 MN |
588 | |
589 | // Update completion state. | |
590 | if ($completion->is_enabled($cm) && $choice->completionsubmit) { | |
591 | $completion->update_state($cm, COMPLETION_INCOMPLETE); | |
592 | } | |
593 | ||
348630d8 | 594 | return true; |
595 | } | |
596 | ||
04eba58f | 597 | |
4636bf83 | 598 | /** |
599 | * Given an ID of an instance of this module, | |
600 | * this function will permanently delete the instance | |
601 | * and any data that depends on it. | |
602 | * | |
603 | * @global object | |
604 | * @param int $id | |
605 | * @return bool | |
606 | */ | |
04eba58f | 607 | function choice_delete_instance($id) { |
c18269c7 | 608 | global $DB; |
04eba58f | 609 | |
c18269c7 | 610 | if (! $choice = $DB->get_record("choice", array("id"=>"$id"))) { |
04eba58f | 611 | return false; |
612 | } | |
613 | ||
614 | $result = true; | |
615 | ||
c18269c7 | 616 | if (! $DB->delete_records("choice_answers", array("choiceid"=>"$choice->id"))) { |
04eba58f | 617 | $result = false; |
618 | } | |
619 | ||
c18269c7 | 620 | if (! $DB->delete_records("choice_options", array("choiceid"=>"$choice->id"))) { |
04eba58f | 621 | $result = false; |
622 | } | |
a77a6009 | 623 | |
c18269c7 | 624 | if (! $DB->delete_records("choice", array("id"=>"$choice->id"))) { |
6fd87e3b | 625 | $result = false; |
ebd3c7ac | 626 | } |
9be80529 SB |
627 | // Remove old calendar events. |
628 | if (! $DB->delete_records('event', array('modulename' => 'choice', 'instance' => $choice->id))) { | |
629 | $result = false; | |
630 | } | |
04eba58f | 631 | |
632 | return $result; | |
633 | } | |
634 | ||
4636bf83 | 635 | /** |
636 | * Returns text string which is the answer that matches the id | |
637 | * | |
638 | * @global object | |
639 | * @param object $choice | |
640 | * @param int $id | |
641 | * @return string | |
642 | */ | |
a77a6009 | 643 | function choice_get_option_text($choice, $id) { |
407caeeb | 644 | global $DB; |
4636bf83 | 645 | |
407caeeb | 646 | if ($result = $DB->get_record("choice_options", array("id" => $id))) { |
a77a6009 | 647 | return $result->text; |
6fd87e3b | 648 | } else { |
649 | return get_string("notanswered", "choice"); | |
ff8d654f | 650 | } |
c4016bc1 | 651 | } |
652 | ||
4636bf83 | 653 | /** |
654 | * Gets a full choice record | |
655 | * | |
656 | * @global object | |
657 | * @param int $choiceid | |
658 | * @return object|bool The choice or false | |
659 | */ | |
cd3fccff | 660 | function choice_get_choice($choiceid) { |
407caeeb | 661 | global $DB; |
a77a6009 | 662 | |
407caeeb | 663 | if ($choice = $DB->get_record("choice", array("id" => $choiceid))) { |
664 | if ($options = $DB->get_records("choice_options", array("choiceid" => $choiceid), "id")) { | |
ff8d654f | 665 | foreach ($options as $option) { |
ebd3c7ac | 666 | $choice->option[$option->id] = $option->text; |
c9ff813c | 667 | $choice->maxanswers[$option->id] = $option->maxanswers; |
ff8d654f | 668 | } |
a77a6009 | 669 | return $choice; |
670 | } | |
671 | } | |
672 | return false; | |
cd3fccff | 673 | } |
674 | ||
4636bf83 | 675 | /** |
b2b4ec30 RT |
676 | * List the actions that correspond to a view of this module. |
677 | * This is used by the participation report. | |
678 | * | |
679 | * Note: This is not used by new logging system. Event with | |
680 | * crud = 'r' and edulevel = LEVEL_PARTICIPATING will | |
681 | * be considered as view action. | |
682 | * | |
4636bf83 | 683 | * @return array |
684 | */ | |
f3221af9 | 685 | function choice_get_view_actions() { |
cf051cc4 | 686 | return array('view','view all','report'); |
f3221af9 | 687 | } |
688 | ||
4636bf83 | 689 | /** |
b2b4ec30 RT |
690 | * List the actions that correspond to a post of this module. |
691 | * This is used by the participation report. | |
692 | * | |
693 | * Note: This is not used by new logging system. Event with | |
694 | * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING | |
695 | * will be considered as post action. | |
696 | * | |
4636bf83 | 697 | * @return array |
698 | */ | |
f3221af9 | 699 | function choice_get_post_actions() { |
700 | return array('choose','choose again'); | |
701 | } | |
702 | ||
0b5a80a1 | 703 | |
704 | /** | |
705 | * Implementation of the function for printing the form elements that control | |
706 | * whether the course reset functionality affects the choice. | |
1adbd2c3 | 707 | * |
4636bf83 | 708 | * @param object $mform form passed by reference |
0b5a80a1 | 709 | */ |
710 | function choice_reset_course_form_definition(&$mform) { | |
711 | $mform->addElement('header', 'choiceheader', get_string('modulenameplural', 'choice')); | |
712 | $mform->addElement('advcheckbox', 'reset_choice', get_string('removeresponses','choice')); | |
713 | } | |
714 | ||
715 | /** | |
716 | * Course reset form defaults. | |
4636bf83 | 717 | * |
718 | * @return array | |
0b5a80a1 | 719 | */ |
720 | function choice_reset_course_form_defaults($course) { | |
721 | return array('reset_choice'=>1); | |
722 | } | |
723 | ||
724 | /** | |
72d2982e | 725 | * Actual implementation of the reset course functionality, delete all the |
0b5a80a1 | 726 | * choice responses for course $data->courseid. |
4636bf83 | 727 | * |
728 | * @global object | |
729 | * @global object | |
730 | * @param object $data the data submitted from the reset course. | |
0b5a80a1 | 731 | * @return array status array |
732 | */ | |
733 | function choice_reset_userdata($data) { | |
407caeeb | 734 | global $CFG, $DB; |
0b5a80a1 | 735 | |
736 | $componentstr = get_string('modulenameplural', 'choice'); | |
737 | $status = array(); | |
738 | ||
739 | if (!empty($data->reset_choice)) { | |
740 | $choicessql = "SELECT ch.id | |
f7e93000 RW |
741 | FROM {choice} ch |
742 | WHERE ch.course=?"; | |
0b5a80a1 | 743 | |
407caeeb | 744 | $DB->delete_records_select('choice_answers', "choiceid IN ($choicessql)", array($data->courseid)); |
0b5a80a1 | 745 | $status[] = array('component'=>$componentstr, 'item'=>get_string('removeresponses', 'choice'), 'error'=>false); |
746 | } | |
747 | ||
748 | /// updating dates - shift may be negative too | |
749 | if ($data->timeshift) { | |
750 | shift_course_mod_dates('choice', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid); | |
751 | $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false); | |
752 | } | |
753 | ||
754 | return $status; | |
755 | } | |
756 | ||
4636bf83 | 757 | /** |
758 | * @global object | |
759 | * @global object | |
760 | * @global object | |
761 | * @uses CONTEXT_MODULE | |
762 | * @param object $choice | |
763 | * @param object $cm | |
764 | * @param int $groupmode | |
6de3eee0 | 765 | * @param bool $onlyactive Whether to get response data for active users only. |
4636bf83 | 766 | * @return array |
767 | */ | |
6de3eee0 | 768 | function choice_get_response_data($choice, $cm, $groupmode, $onlyactive) { |
407caeeb | 769 | global $CFG, $USER, $DB; |
a61e4188 | 770 | |
327c67a9 | 771 | $context = context_module::instance($cm->id); |
a61e4188 | 772 | |
773 | /// Get the current group | |
774 | if ($groupmode > 0) { | |
775 | $currentgroup = groups_get_activity_group($cm); | |
776 | } else { | |
777 | $currentgroup = 0; | |
778 | } | |
779 | ||
5d9d5227 | 780 | /// Initialise the returned array, which is a matrix: $allresponses[responseid][userid] = responseobject |
781 | $allresponses = array(); | |
a61e4188 | 782 | |
783 | /// First get all the users who have access here | |
784 | /// To start with we assume they are all "unanswered" then move them later | |
6de3eee0 TB |
785 | $allresponses[0] = get_enrolled_users($context, 'mod/choice:choose', $currentgroup, |
786 | user_picture::fields('u', array('idnumber')), null, 0, 0, $onlyactive); | |
a61e4188 | 787 | |
788 | /// Get all the recorded responses for this choice | |
407caeeb | 789 | $rawresponses = $DB->get_records('choice_answers', array('choiceid' => $choice->id)); |
a61e4188 | 790 | |
791 | /// Use the responses to move users into the correct column | |
792 | ||
5d9d5227 | 793 | if ($rawresponses) { |
54bdf1d4 | 794 | $answeredusers = array(); |
5d9d5227 | 795 | foreach ($rawresponses as $response) { |
796 | if (isset($allresponses[0][$response->userid])) { // This person is enrolled and in correct group | |
797 | $allresponses[0][$response->userid]->timemodified = $response->timemodified; | |
798 | $allresponses[$response->optionid][$response->userid] = clone($allresponses[0][$response->userid]); | |
54bdf1d4 MA |
799 | $allresponses[$response->optionid][$response->userid]->answerid = $response->id; |
800 | $answeredusers[] = $response->userid; | |
a61e4188 | 801 | } |
802 | } | |
54bdf1d4 MA |
803 | foreach ($answeredusers as $answereduser) { |
804 | unset($allresponses[0][$answereduser]); | |
805 | } | |
f7e93000 | 806 | } |
5d9d5227 | 807 | return $allresponses; |
f432bebf | 808 | } |
a61e4188 | 809 | |
f432bebf | 810 | /** |
811 | * Returns all other caps used in module | |
4636bf83 | 812 | * |
813 | * @return array | |
f432bebf | 814 | */ |
db19ec01 | 815 | function choice_get_extra_capabilities() { |
f432bebf | 816 | return array('moodle/site:accessallgroups'); |
a61e4188 | 817 | } |
f432bebf | 818 | |
18a2a0cb | 819 | /** |
4636bf83 | 820 | * @uses FEATURE_GROUPS |
821 | * @uses FEATURE_GROUPINGS | |
4636bf83 | 822 | * @uses FEATURE_MOD_INTRO |
823 | * @uses FEATURE_COMPLETION_TRACKS_VIEWS | |
824 | * @uses FEATURE_GRADE_HAS_GRADE | |
825 | * @uses FEATURE_GRADE_OUTCOMES | |
18a2a0cb | 826 | * @param string $feature FEATURE_xx constant for requested feature |
827 | * @return mixed True if module supports feature, null if doesn't know | |
828 | */ | |
829 | function choice_supports($feature) { | |
830 | switch($feature) { | |
42f103be | 831 | case FEATURE_GROUPS: return true; |
832 | case FEATURE_GROUPINGS: return true; | |
dc5c2bd9 | 833 | case FEATURE_MOD_INTRO: return true; |
18a2a0cb | 834 | case FEATURE_COMPLETION_TRACKS_VIEWS: return true; |
bc499733 | 835 | case FEATURE_COMPLETION_HAS_RULES: return true; |
42f103be | 836 | case FEATURE_GRADE_HAS_GRADE: return false; |
837 | case FEATURE_GRADE_OUTCOMES: return false; | |
98baf0d7 | 838 | case FEATURE_BACKUP_MOODLE2: return true; |
3e4c2435 | 839 | case FEATURE_SHOW_DESCRIPTION: return true; |
42f103be | 840 | |
18a2a0cb | 841 | default: return null; |
842 | } | |
843 | } | |
97011c64 | 844 | |
0b29477b SH |
845 | /** |
846 | * Adds module specific settings to the settings block | |
847 | * | |
848 | * @param settings_navigation $settings The settings navigation object | |
849 | * @param navigation_node $choicenode The node to add module settings to | |
850 | */ | |
851 | function choice_extend_settings_navigation(settings_navigation $settings, navigation_node $choicenode) { | |
852 | global $PAGE; | |
97011c64 | 853 | |
854 | if (has_capability('mod/choice:readresponses', $PAGE->cm->context)) { | |
855 | ||
856 | $groupmode = groups_get_activity_groupmode($PAGE->cm); | |
857 | if ($groupmode) { | |
858 | groups_get_activity_group($PAGE->cm, true); | |
859 | } | |
6de3eee0 TB |
860 | |
861 | $choice = choice_get_choice($PAGE->cm->instance); | |
862 | ||
863 | // Check if we want to include responses from inactive users. | |
864 | $onlyactive = $choice->includeinactive ? false : true; | |
865 | ||
866 | // Big function, approx 6 SQL calls per user. | |
867 | $allresponses = choice_get_response_data($choice, $PAGE->cm, $groupmode, $onlyactive); | |
97011c64 | 868 | |
540200ae | 869 | $allusers = []; |
97011c64 | 870 | foreach($allresponses as $optionid => $userlist) { |
871 | if ($optionid) { | |
540200ae | 872 | $allusers = array_merge($allusers, array_keys($userlist)); |
97011c64 | 873 | } |
874 | } | |
540200ae | 875 | $responsecount = count(array_unique($allusers)); |
0b29477b | 876 | $choicenode->add(get_string("viewallresponses", "choice", $responsecount), new moodle_url('/mod/choice/report.php', array('id'=>$PAGE->cm->id))); |
97011c64 | 877 | } |
98baf0d7 | 878 | } |
bc499733 SM |
879 | |
880 | /** | |
881 | * Obtains the automatic completion state for this choice based on any conditions | |
882 | * in forum settings. | |
883 | * | |
884 | * @param object $course Course | |
885 | * @param object $cm Course-module | |
886 | * @param int $userid User ID | |
887 | * @param bool $type Type of comparison (or/and; can be used as return value if no conditions) | |
888 | * @return bool True if completed, false if not, $type if conditions not set. | |
889 | */ | |
890 | function choice_get_completion_state($course, $cm, $userid, $type) { | |
891 | global $CFG,$DB; | |
892 | ||
893 | // Get choice details | |
894 | $choice = $DB->get_record('choice', array('id'=>$cm->instance), '*', | |
895 | MUST_EXIST); | |
896 | ||
516c5eca | 897 | // If completion option is enabled, evaluate it and return true/false |
bc499733 SM |
898 | if($choice->completionsubmit) { |
899 | return $DB->record_exists('choice_answers', array( | |
900 | 'choiceid'=>$choice->id, 'userid'=>$userid)); | |
901 | } else { | |
902 | // Completion option is not enabled so just return $type | |
903 | return $type; | |
904 | } | |
905 | } | |
b1627a92 DC |
906 | |
907 | /** | |
908 | * Return a list of page types | |
909 | * @param string $pagetype current page type | |
910 | * @param stdClass $parentcontext Block's parent context | |
911 | * @param stdClass $currentcontext Current context of block | |
912 | */ | |
b38e2e28 | 913 | function choice_page_type_list($pagetype, $parentcontext, $currentcontext) { |
b1627a92 DC |
914 | $module_pagetype = array('mod-choice-*'=>get_string('page-mod-choice-x', 'choice')); |
915 | return $module_pagetype; | |
916 | } | |
c04b66a5 SB |
917 | |
918 | /** | |
919 | * Prints choice summaries on MyMoodle Page | |
920 | * | |
921 | * Prints choice name, due date and attempt information on | |
922 | * choice activities that have a deadline that has not already passed | |
923 | * and it is available for completing. | |
e9dfeec9 MN |
924 | * |
925 | * @deprecated since 3.3 | |
063b7ee6 | 926 | * @todo The final deprecation of this function will take place in Moodle 3.7 - see MDL-57487. |
c04b66a5 SB |
927 | * @uses CONTEXT_MODULE |
928 | * @param array $courses An array of course objects to get choice instances from. | |
929 | * @param array $htmlarray Store overview output array( course ID => 'choice' => HTML output ) | |
930 | */ | |
931 | function choice_print_overview($courses, &$htmlarray) { | |
932 | global $USER, $DB, $OUTPUT; | |
933 | ||
e9dfeec9 MN |
934 | debugging('The function choice_print_overview() is now deprecated.', DEBUG_DEVELOPER); |
935 | ||
c04b66a5 SB |
936 | if (empty($courses) || !is_array($courses) || count($courses) == 0) { |
937 | return; | |
938 | } | |
939 | if (!$choices = get_all_instances_in_courses('choice', $courses)) { | |
940 | return; | |
941 | } | |
942 | ||
943 | $now = time(); | |
944 | foreach ($choices as $choice) { | |
945 | if ($choice->timeclose != 0 // If this choice is scheduled. | |
946 | and $choice->timeclose >= $now // And the deadline has not passed. | |
947 | and ($choice->timeopen == 0 or $choice->timeopen <= $now)) { // And the choice is available. | |
948 | ||
949 | // Visibility. | |
950 | $class = (!$choice->visible) ? 'dimmed' : ''; | |
951 | ||
952 | // Link to activity. | |
953 | $url = new moodle_url('/mod/choice/view.php', array('id' => $choice->coursemodule)); | |
954 | $url = html_writer::link($url, format_string($choice->name), array('class' => $class)); | |
955 | $str = $OUTPUT->box(get_string('choiceactivityname', 'choice', $url), 'name'); | |
956 | ||
957 | // Deadline. | |
958 | $str .= $OUTPUT->box(get_string('choicecloseson', 'choice', userdate($choice->timeclose)), 'info'); | |
959 | ||
960 | // Display relevant info based on permissions. | |
961 | if (has_capability('mod/choice:readresponses', context_module::instance($choice->coursemodule))) { | |
540200ae MG |
962 | $attempts = $DB->count_records_sql('SELECT COUNT(DISTINCT userid) FROM {choice_answers} WHERE choiceid = ?', |
963 | [$choice->id]); | |
964 | $url = new moodle_url('/mod/choice/report.php', ['id' => $choice->coursemodule]); | |
965 | $str .= $OUTPUT->box(html_writer::link($url, get_string('viewallresponses', 'choice', $attempts)), 'info'); | |
c04b66a5 SB |
966 | |
967 | } else if (has_capability('mod/choice:choose', context_module::instance($choice->coursemodule))) { | |
968 | // See if the user has submitted anything. | |
969 | $answers = $DB->count_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id)); | |
970 | if ($answers > 0) { | |
971 | // User has already selected an answer, nothing to show. | |
972 | $str = ''; | |
973 | } else { | |
974 | // User has not made a selection yet. | |
975 | $str .= $OUTPUT->box(get_string('notanswered', 'choice'), 'info'); | |
976 | } | |
977 | } else { | |
978 | // Does not have permission to do anything on this choice activity. | |
979 | $str = ''; | |
980 | } | |
981 | ||
982 | // Make sure we have something to display. | |
983 | if (!empty($str)) { | |
984 | // Generate the containing div. | |
985 | $str = $OUTPUT->box($str, 'choice overview'); | |
986 | ||
987 | if (empty($htmlarray[$choice->course]['choice'])) { | |
988 | $htmlarray[$choice->course]['choice'] = $str; | |
989 | } else { | |
990 | $htmlarray[$choice->course]['choice'] .= $str; | |
991 | } | |
992 | } | |
993 | } | |
994 | } | |
995 | return; | |
86e5b0a7 | 996 | } |
b87f31db JL |
997 | |
998 | ||
999 | /** | |
1000 | * Get my responses on a given choice. | |
1001 | * | |
1002 | * @param stdClass $choice Choice record | |
1003 | * @return array of choice answers records | |
1004 | * @since Moodle 3.0 | |
1005 | */ | |
1006 | function choice_get_my_response($choice) { | |
1007 | global $DB, $USER; | |
1008 | return $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id)); | |
1009 | } | |
1010 | ||
4b93c3e3 JL |
1011 | |
1012 | /** | |
1013 | * Get all the responses on a given choice. | |
1014 | * | |
1015 | * @param stdClass $choice Choice record | |
1016 | * @return array of choice answers records | |
1017 | * @since Moodle 3.0 | |
1018 | */ | |
1019 | function choice_get_all_responses($choice) { | |
1020 | global $DB; | |
1021 | return $DB->get_records('choice_answers', array('choiceid' => $choice->id)); | |
1022 | } | |
1023 | ||
1024 | ||
b87f31db JL |
1025 | /** |
1026 | * Return true if we are allowd to view the choice results. | |
1027 | * | |
1028 | * @param stdClass $choice Choice record | |
1029 | * @param rows|null $current my choice responses | |
1030 | * @param bool|null $choiceopen if the choice is open | |
1031 | * @return bool true if we can view the results, false otherwise. | |
1032 | * @since Moodle 3.0 | |
1033 | */ | |
1034 | function choice_can_view_results($choice, $current = null, $choiceopen = null) { | |
1035 | ||
1036 | if (is_null($choiceopen)) { | |
1037 | $timenow = time(); | |
fa19c733 JL |
1038 | |
1039 | if ($choice->timeopen != 0 && $timenow < $choice->timeopen) { | |
1040 | // If the choice is not available, we can't see the results. | |
1041 | return false; | |
1042 | } | |
1043 | ||
b87f31db JL |
1044 | if ($choice->timeclose != 0 && $timenow > $choice->timeclose) { |
1045 | $choiceopen = false; | |
1046 | } else { | |
1047 | $choiceopen = true; | |
1048 | } | |
1049 | } | |
1050 | if (empty($current)) { | |
1051 | $current = choice_get_my_response($choice); | |
1052 | } | |
1053 | ||
1054 | if ($choice->showresults == CHOICE_SHOWRESULTS_ALWAYS or | |
1055 | ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_ANSWER and !empty($current)) or | |
1056 | ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_CLOSE and !$choiceopen)) { | |
1057 | return true; | |
1058 | } | |
1059 | return false; | |
1060 | } | |
4c4d3b73 JL |
1061 | |
1062 | /** | |
1063 | * Mark the activity completed (if required) and trigger the course_module_viewed event. | |
1064 | * | |
1065 | * @param stdClass $choice choice object | |
1066 | * @param stdClass $course course object | |
1067 | * @param stdClass $cm course module object | |
1068 | * @param stdClass $context context object | |
1069 | * @since Moodle 3.0 | |
1070 | */ | |
1071 | function choice_view($choice, $course, $cm, $context) { | |
1072 | ||
1073 | // Trigger course_module_viewed event. | |
1074 | $params = array( | |
1075 | 'context' => $context, | |
1076 | 'objectid' => $choice->id | |
1077 | ); | |
1078 | ||
1079 | $event = \mod_choice\event\course_module_viewed::create($params); | |
1080 | $event->add_record_snapshot('course_modules', $cm); | |
1081 | $event->add_record_snapshot('course', $course); | |
1082 | $event->add_record_snapshot('choice', $choice); | |
1083 | $event->trigger(); | |
1084 | ||
1085 | // Completion. | |
1086 | $completion = new completion_info($course); | |
1087 | $completion->set_module_viewed($cm); | |
1088 | } | |
3dd0ac55 JL |
1089 | |
1090 | /** | |
1091 | * Check if a choice is available for the current user. | |
1092 | * | |
1093 | * @param stdClass $choice choice record | |
1094 | * @return array status (available or not and possible warnings) | |
1095 | */ | |
1096 | function choice_get_availability_status($choice) { | |
1097 | $available = true; | |
1098 | $warnings = array(); | |
1099 | ||
2ff504c6 | 1100 | $timenow = time(); |
3dd0ac55 | 1101 | |
2ff504c6 SB |
1102 | if (!empty($choice->timeopen) && ($choice->timeopen > $timenow)) { |
1103 | $available = false; | |
1104 | $warnings['notopenyet'] = userdate($choice->timeopen); | |
1105 | } else if (!empty($choice->timeclose) && ($timenow > $choice->timeclose)) { | |
1106 | $available = false; | |
1107 | $warnings['expired'] = userdate($choice->timeclose); | |
3dd0ac55 | 1108 | } |
531bcb13 JO |
1109 | if (!$choice->allowupdate && choice_get_my_response($choice)) { |
1110 | $available = false; | |
1111 | $warnings['choicesaved'] = ''; | |
1112 | } | |
3dd0ac55 JL |
1113 | |
1114 | // Choice is available. | |
1115 | return array($available, $warnings); | |
1116 | } | |
9be80529 SB |
1117 | |
1118 | /** | |
1119 | * This standard function will check all instances of this module | |
1120 | * and make sure there are up-to-date events created for each of them. | |
1121 | * If courseid = 0, then every chat event in the site is checked, else | |
1122 | * only chat events belonging to the course specified are checked. | |
1123 | * This function is used, in its new format, by restore_refresh_events() | |
1124 | * | |
1125 | * @param int $courseid | |
1126 | * @return bool | |
1127 | */ | |
1128 | function choice_refresh_events($courseid = 0) { | |
1129 | global $DB, $CFG; | |
1130 | require_once($CFG->dirroot.'/mod/choice/locallib.php'); | |
1131 | ||
1132 | if ($courseid) { | |
1133 | if (! $choices = $DB->get_records("choice", array("course" => $courseid))) { | |
1134 | return true; | |
1135 | } | |
1136 | } else { | |
1137 | if (! $choices = $DB->get_records("choice")) { | |
1138 | return true; | |
1139 | } | |
1140 | } | |
1141 | ||
1142 | foreach ($choices as $choice) { | |
1143 | choice_set_events($choice); | |
1144 | } | |
1145 | return true; | |
1146 | } | |
1147 | ||
02a73d76 JL |
1148 | /** |
1149 | * Check if the module has any update that affects the current user since a given time. | |
1150 | * | |
1151 | * @param cm_info $cm course module data | |
1152 | * @param int $from the time to check updates from | |
1153 | * @param array $filter if we need to check only specific updates | |
1154 | * @return stdClass an object with the different type of areas indicating if they were updated or not | |
1155 | * @since Moodle 3.2 | |
1156 | */ | |
1157 | function choice_check_updates_since(cm_info $cm, $from, $filter = array()) { | |
1158 | global $DB; | |
1159 | ||
1160 | $updates = new stdClass(); | |
1161 | $choice = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST); | |
1162 | list($available, $warnings) = choice_get_availability_status($choice); | |
1163 | if (!$available) { | |
1164 | return $updates; | |
1165 | } | |
1166 | ||
1167 | $updates = course_check_module_updates_since($cm, $from, array(), $filter); | |
1168 | ||
1169 | if (!choice_can_view_results($choice)) { | |
1170 | return $updates; | |
1171 | } | |
1172 | // Check if there are new responses in the choice. | |
65b2669d | 1173 | $updates->answers = (object) array('updated' => false); |
02a73d76 JL |
1174 | $select = 'choiceid = :id AND timemodified > :since'; |
1175 | $params = array('id' => $choice->id, 'since' => $from); | |
65b2669d JL |
1176 | $answers = $DB->get_records_select('choice_answers', $select, $params, '', 'id'); |
1177 | if (!empty($answers)) { | |
1178 | $updates->answers->updated = true; | |
1179 | $updates->answers->itemids = array_keys($answers); | |
1180 | } | |
02a73d76 JL |
1181 | |
1182 | return $updates; | |
1183 | } | |
2b931458 | 1184 | |
213dcf51 | 1185 | /** |
59391e80 MN |
1186 | * This function receives a calendar event and returns the action associated with it, or null if there is none. |
1187 | * | |
1188 | * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event | |
1189 | * is not displayed on the block. | |
213dcf51 | 1190 | * |
e1cd93ce | 1191 | * @param calendar_event $event |
213dcf51 | 1192 | * @param \core_calendar\action_factory $factory |
01f96180 | 1193 | * @return \core_calendar\local\event\entities\action_interface|null |
213dcf51 | 1194 | */ |
e1cd93ce | 1195 | function mod_choice_core_calendar_provide_event_action(calendar_event $event, |
213dcf51 MN |
1196 | \core_calendar\action_factory $factory) { |
1197 | global $DB; | |
1198 | ||
1199 | $cm = get_fast_modinfo($event->courseid)->instances['choice'][$event->instance]; | |
1200 | $choice = $DB->get_record('choice', array('id' => $event->instance), 'id, timeopen, timeclose'); | |
71cd51b8 | 1201 | $now = time(); |
213dcf51 | 1202 | |
71cd51b8 RW |
1203 | if ($choice->timeclose && $choice->timeclose < $now) { |
1204 | // The choice has closed so the user can no longer submit anything. | |
1205 | return null; | |
213dcf51 MN |
1206 | } |
1207 | ||
71cd51b8 RW |
1208 | if (choice_get_my_response($choice)) { |
1209 | // There is no action if the user has already submitted their choice. | |
1210 | return null; | |
1211 | } | |
1212 | ||
1213 | // The choice is actionable if we don't have a start time or the start time is | |
1214 | // in the past. | |
1215 | $actionable = (!$choice->timeopen || $choice->timeopen <= $now); | |
1216 | ||
213dcf51 MN |
1217 | return $factory->create_instance( |
1218 | get_string('viewchoices', 'choice'), | |
1219 | new \moodle_url('/mod/choice/view.php', array('id' => $cm->id)), | |
1220 | 1, | |
1221 | $actionable | |
1222 | ); | |
1223 | } | |
1224 | ||
2b931458 DW |
1225 | /** |
1226 | * Get icon mapping for font-awesome. | |
1227 | */ | |
1228 | function mod_choice_get_fontawesome_icon_map() { | |
1229 | return [ | |
1230 | 'mod_choice:row' => 'fa-info', | |
1231 | 'mod_choice:column' => 'fa-columns', | |
1232 | ]; | |
1233 | } | |
b54bcddd JD |
1234 | |
1235 | /** | |
1236 | * Add a get_coursemodule_info function in case any choice type wants to add 'extra' information | |
1237 | * for the course (see resource). | |
1238 | * | |
1239 | * Given a course_module object, this function returns any "extra" information that may be needed | |
1240 | * when printing this activity in a course listing. See get_array_of_activities() in course/lib.php. | |
1241 | * | |
1242 | * @param stdClass $coursemodule The coursemodule object (record). | |
1243 | * @return cached_cm_info An object on information that the courses | |
1244 | * will know about (most noticeably, an icon). | |
1245 | */ | |
1246 | function choice_get_coursemodule_info($coursemodule) { | |
1247 | global $DB; | |
1248 | ||
1249 | $dbparams = ['id' => $coursemodule->instance]; | |
1250 | $fields = 'id, completionsubmit'; | |
1251 | if (!$choice = $DB->get_record('choice', $dbparams, $fields)) { | |
1252 | return false; | |
1253 | } | |
1254 | ||
1255 | $result = new cached_cm_info(); | |
1256 | ||
1257 | // Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'. | |
1258 | if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) { | |
1259 | $result->customdata['customcompletionrules']['completionsubmit'] = $choice->completionsubmit; | |
1260 | } | |
1261 | ||
1262 | return $result; | |
1263 | } | |
1264 | ||
1265 | /** | |
1266 | * Callback which returns human-readable strings describing the active completion custom rules for the module instance. | |
1267 | * | |
7f53e8aa | 1268 | * @param cm_info|stdClass $cm object with fields ->completion and ->customdata['customcompletionrules'] |
b54bcddd JD |
1269 | * @return array $descriptions the array of descriptions for the custom rules. |
1270 | */ | |
1271 | function mod_choice_get_completion_active_rule_descriptions($cm) { | |
1272 | // Values will be present in cm_info, and we assume these are up to date. | |
7f53e8aa | 1273 | if (empty($cm->customdata['customcompletionrules']) |
b54bcddd JD |
1274 | || $cm->completion != COMPLETION_TRACKING_AUTOMATIC) { |
1275 | return []; | |
1276 | } | |
1277 | ||
1278 | $descriptions = []; | |
1279 | foreach ($cm->customdata['customcompletionrules'] as $key => $val) { | |
1280 | switch ($key) { | |
1281 | case 'completionsubmit': | |
1282 | if (empty($val)) { | |
1283 | continue; | |
1284 | } | |
1285 | $descriptions[] = get_string('completionsubmit', 'choice'); | |
1286 | break; | |
1287 | default: | |
1288 | break; | |
1289 | } | |
1290 | } | |
1291 | return $descriptions; | |
1292 | } |