weekly release 2.9dev
[moodle.git] / mod / choice / lib.php
CommitLineData
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
17da2e6f 24/** @global int $CHOICE_COLUMN_HEIGHT */
25global $CHOICE_COLUMN_HEIGHT;
26$CHOICE_COLUMN_HEIGHT = 300;
6d6c9389 27
315d4971
RW
28/** @global int $CHOICE_COLUMN_WIDTH */
29global $CHOICE_COLUMN_WIDTH;
30$CHOICE_COLUMN_WIDTH = 300;
31
8f7dc7f1 32define('CHOICE_PUBLISH_ANONYMOUS', '0');
33define('CHOICE_PUBLISH_NAMES', '1');
6d6c9389 34
c100a341 35define('CHOICE_SHOWRESULTS_NOT', '0');
36define('CHOICE_SHOWRESULTS_AFTER_ANSWER', '1');
37define('CHOICE_SHOWRESULTS_AFTER_CLOSE', '2');
38define('CHOICE_SHOWRESULTS_ALWAYS', '3');
f9af7fd0 39
6fd87e3b 40define('CHOICE_DISPLAY_HORIZONTAL', '0');
41define('CHOICE_DISPLAY_VERTICAL', '1');
42
4636bf83 43/** @global array $CHOICE_PUBLISH */
17da2e6f 44global $CHOICE_PUBLISH;
8f7dc7f1 45$CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS => get_string('publishanonymous', 'choice'),
46 CHOICE_PUBLISH_NAMES => get_string('publishnames', 'choice'));
6d6c9389 47
4636bf83 48/** @global array $CHOICE_SHOWRESULTS */
17da2e6f 49global $CHOICE_SHOWRESULTS;
c100a341 50$CHOICE_SHOWRESULTS = array (CHOICE_SHOWRESULTS_NOT => get_string('publishnot', 'choice'),
51 CHOICE_SHOWRESULTS_AFTER_ANSWER => get_string('publishafteranswer', 'choice'),
52 CHOICE_SHOWRESULTS_AFTER_CLOSE => get_string('publishafterclose', 'choice'),
53 CHOICE_SHOWRESULTS_ALWAYS => get_string('publishalways', 'choice'));
f9af7fd0 54
4636bf83 55/** @global array $CHOICE_DISPLAY */
17da2e6f 56global $CHOICE_DISPLAY;
6fd87e3b 57$CHOICE_DISPLAY = array (CHOICE_DISPLAY_HORIZONTAL => get_string('displayhorizontal', 'choice'),
58 CHOICE_DISPLAY_VERTICAL => get_string('displayvertical','choice'));
6d6c9389 59
60/// Standard functions /////////////////////////////////////////////////////////
61
4636bf83 62/**
63 * @global object
64 * @param object $course
65 * @param object $user
66 * @param object $mod
67 * @param object $choice
68 * @return object|null
69 */
0d22e7cc 70function choice_user_outline($course, $user, $mod, $choice) {
407caeeb 71 global $DB;
72 if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $user->id))) {
39790bd8 73 $result = new stdClass();
7cfc54b9 74 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
d1a67362 75 $result->time = $answer->timemodified;
0d22e7cc 76 return $result;
77 }
78 return NULL;
79}
80
4636bf83 81/**
82 * @global object
83 * @param object $course
84 * @param object $user
85 * @param object $mod
86 * @param object $choice
87 * @return string|void
88 */
0d22e7cc 89function choice_user_complete($course, $user, $mod, $choice) {
407caeeb 90 global $DB;
91 if ($answer = $DB->get_record('choice_answers', array("choiceid" => $choice->id, "userid" => $user->id))) {
39790bd8 92 $result = new stdClass();
7cfc54b9 93 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
d1a67362 94 $result->time = $answer->timemodified;
7cfc54b9 95 echo get_string("answered", "choice").": $result->info. ".get_string("updated", '', userdate($result->time));
0d22e7cc 96 } else {
c4016bc1 97 print_string("notanswered", "choice");
0d22e7cc 98 }
99}
100
4636bf83 101/**
102 * Given an object containing all the necessary data,
103 * (defined by the form in mod_form.php) this function
104 * will create a new instance and return the id number
105 * of the new instance.
106 *
107 * @global object
108 * @param object $choice
109 * @return int
110 */
04eba58f 111function choice_add_instance($choice) {
c18269c7 112 global $DB;
04eba58f 113
114 $choice->timemodified = time();
115
ebd3c7ac 116 if (empty($choice->timerestrict)) {
d30772b2 117 $choice->timeopen = 0;
118 $choice->timeclose = 0;
119 }
120
ff8d654f 121 //insert answers
9d97f08e
PS
122 $choice->id = $DB->insert_record("choice", $choice);
123 foreach ($choice->option as $key => $value) {
124 $value = trim($value);
125 if (isset($value) && $value <> '') {
39790bd8 126 $option = new stdClass();
9d97f08e
PS
127 $option->text = $value;
128 $option->choiceid = $choice->id;
129 if (isset($choice->limit[$key])) {
130 $option->maxanswers = $choice->limit[$key];
6fd87e3b 131 }
9d97f08e
PS
132 $option->timemodified = time();
133 $DB->insert_record("choice_options", $option);
6fd87e3b 134 }
135 }
9d97f08e 136
a77a6009 137 return $choice->id;
04eba58f 138}
139
4636bf83 140/**
141 * Given an object containing all the necessary data,
142 * (defined by the form in mod_form.php) this function
143 * will update an existing instance with new data.
144 *
145 * @global object
146 * @param object $choice
147 * @return bool
148 */
04eba58f 149function choice_update_instance($choice) {
c18269c7 150 global $DB;
04eba58f 151
152 $choice->id = $choice->instance;
153 $choice->timemodified = time();
154
d30772b2 155
ebd3c7ac 156 if (empty($choice->timerestrict)) {
d30772b2 157 $choice->timeopen = 0;
158 $choice->timeclose = 0;
159 }
ff8d654f 160
ebd3c7ac 161 //update, delete or insert answers
162 foreach ($choice->option as $key => $value) {
a77a6009 163 $value = trim($value);
39790bd8 164 $option = new stdClass();
ebd3c7ac 165 $option->text = $value;
166 $option->choiceid = $choice->id;
167 if (isset($choice->limit[$key])) {
168 $option->maxanswers = $choice->limit[$key];
169 }
170 $option->timemodified = time();
a2e67a9a 171 if (isset($choice->optionid[$key]) && !empty($choice->optionid[$key])){//existing choice record
ebd3c7ac 172 $option->id=$choice->optionid[$key];
af834f3e 173 if (isset($value) && $value <> '') {
c18269c7 174 $DB->update_record("choice_options", $option);
b9135eca 175 } else { //empty old option - needs to be deleted.
c18269c7 176 $DB->delete_records("choice_options", array("id"=>$option->id));
c9ff813c 177 }
ebd3c7ac 178 } else {
179 if (isset($value) && $value <> '') {
c18269c7 180 $DB->insert_record("choice_options", $option);
a77a6009 181 }
ff8d654f 182 }
6fd87e3b 183 }
d30772b2 184
c18269c7 185 return $DB->update_record('choice', $choice);
ff8d654f 186
04eba58f 187}
188
4636bf83 189/**
190 * @global object
191 * @param object $choice
192 * @param object $user
315d4971 193 * @param object $coursemodule
4636bf83 194 * @param array $allresponses
315d4971 195 * @return array
4636bf83 196 */
315d4971 197function choice_prepare_options($choice, $user, $coursemodule, $allresponses) {
407caeeb 198 global $DB;
3c56a7cc 199
315d4971 200 $cdisplay = array('options'=>array());
a61e4188 201
315d4971 202 $cdisplay['limitanswers'] = true;
327c67a9 203 $context = context_module::instance($coursemodule->id);
3c56a7cc 204
348630d8 205 foreach ($choice->option as $optionid => $text) {
a5f35b9f 206 if (isset($text)) { //make sure there are no dud entries in the db with blank text values.
315d4971
RW
207 $option = new stdClass;
208 $option->attributes = new stdClass;
209 $option->attributes->value = $optionid;
ba939078 210 $option->text = format_string($text);
315d4971
RW
211 $option->maxanswers = $choice->maxanswers[$optionid];
212 $option->displaylayout = $choice->display;
213
55963a3a 214 if (isset($allresponses[$optionid])) {
315d4971 215 $option->countanswers = count($allresponses[$optionid]);
55963a3a 216 } else {
315d4971 217 $option->countanswers = 0;
55963a3a 218 }
315d4971
RW
219 if ($DB->record_exists('choice_answers', array('choiceid' => $choice->id, 'userid' => $user->id, 'optionid' => $optionid))) {
220 $option->attributes->checked = true;
dabfd0ed 221 }
315d4971
RW
222 if ( $choice->limitanswers && ($option->countanswers >= $option->maxanswers) && empty($option->attributes->checked)) {
223 $option->attributes->disabled = true;
dabfd0ed 224 }
e076641f 225 $cdisplay['options'][] = $option;
348630d8 226 }
e076641f 227 }
348630d8 228
315d4971 229 $cdisplay['hascapability'] = is_enrolled($context, NULL, 'mod/choice:choose'); //only enrolled users are allowed to make a choice
ebd3c7ac 230
315d4971
RW
231 if ($choice->allowupdate && $DB->record_exists('choice_answers', array('choiceid'=> $choice->id, 'userid'=> $user->id))) {
232 $cdisplay['allowupdate'] = true;
e076641f 233 }
348630d8 234
315d4971 235 return $cdisplay;
e076641f 236}
348630d8 237
4636bf83 238/**
239 * @global object
240 * @param int $formanswer
241 * @param object $choice
242 * @param int $userid
bc499733 243 * @param object $course Course object
4636bf83 244 * @param object $cm
245 */
bc499733 246function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm) {
516c5eca
PS
247 global $DB, $CFG;
248 require_once($CFG->libdir.'/completionlib.php');
249
54bdf1d4
MA
250 if (empty($formanswer)) {
251 print_error('atleastoneoption', 'choice');
252 }
253
254 if (is_array($formanswer)) {
255 if (!$choice->allowmultiple) {
256 print_error('multiplenotallowederror', 'choice');
257 }
258 $formanswers = $formanswer;
259 } else {
260 $formanswers = array($formanswer);
261 }
262
263 $current = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid));
327c67a9 264 $context = context_module::instance($cm->id);
c75ba879 265
cbb1677c 266 $choicesexceeded = false;
54bdf1d4
MA
267 $countanswers = array();
268 foreach ($formanswers as $val) {
269 $countanswers[$val] = 0;
270 }
c75ba879 271 if($choice->limitanswers) {
272 // Find out whether groups are being used and enabled
273 if (groups_get_activity_groupmode($cm) > 0) {
274 $currentgroup = groups_get_activity_group($cm);
275 } else {
276 $currentgroup = 0;
277 }
54bdf1d4
MA
278
279 list ($insql, $params) = $DB->get_in_or_equal($formanswers, SQL_PARAMS_NAMED);
280
c75ba879 281 if($currentgroup) {
282 // If groups are being used, retrieve responses only for users in
283 // current group
284 global $CFG;
54bdf1d4
MA
285
286 $params['groupid'] = $currentgroup;
cbb1677c
SH
287 $sql = "SELECT ca.*
288 FROM {choice_answers} ca
289 INNER JOIN {groups_members} gm ON ca.userid=gm.userid
290 WHERE optionid $insql
291 AND gm.groupid= :groupid";
c75ba879 292 } else {
293 // Groups are not used, retrieve all answers for this option ID
cbb1677c
SH
294 $sql = "SELECT ca.*
295 FROM {choice_answers} ca
296 WHERE optionid $insql";
ff8d654f 297 }
298
cbb1677c 299 $answers = $DB->get_records_sql($sql, $params);
c75ba879 300 if ($answers) {
301 foreach ($answers as $a) { //only return enrolled users.
4f0c2d00 302 if (is_enrolled($context, $a->userid, 'mod/choice:choose')) {
54bdf1d4 303 $countanswers[$a->optionid]++;
c75ba879 304 }
305 }
306 }
54bdf1d4
MA
307 foreach ($countanswers as $opt => $count) {
308 if ($count > $choice->maxanswers[$opt]) {
309 $choicesexceeded = true;
310 break;
311 }
312 }
39b3e7de 313 }
ff8d654f 314
cbb1677c 315 // Check the user hasn't exceeded the maximum selections for the choice(s) they have selected.
54bdf1d4
MA
316 if (!($choice->limitanswers && $choicesexceeded)) {
317 $answersnapshots = array();
39b3e7de 318 if ($current) {
ebd3c7ac 319
54bdf1d4
MA
320 $existingchoices = array();
321 foreach ($current as $c) {
322 if (in_array($c->optionid, $formanswers)) {
323 $existingchoices[] = $c->optionid;
cbb1677c
SH
324 $DB->set_field('choice_answers', 'timemodified', time(), array('id' => $c->id));
325 $answersnapshots[] = $c;
54bdf1d4
MA
326 } else {
327 $DB->delete_records('choice_answers', array('id' => $c->id));
328 }
329 }
330
331 // Add new ones.
332 foreach ($formanswers as $f) {
333 if (!in_array($f, $existingchoices)) {
334 $newanswer = new stdClass();
335 $newanswer->optionid = $f;
336 $newanswer->choiceid = $choice->id;
337 $newanswer->userid = $userid;
338 $newanswer->timemodified = time();
339 $newanswer->id = $DB->insert_record("choice_answers", $newanswer);
340 $answersnapshots[] = $newanswer;
341 }
342 }
8a941443
AG
343
344 $eventdata = array();
345 $eventdata['context'] = $context;
54bdf1d4 346 $eventdata['objectid'] = $choice->id;
8a941443
AG
347 $eventdata['userid'] = $userid;
348 $eventdata['courseid'] = $course->id;
349 $eventdata['other'] = array();
350 $eventdata['other']['choiceid'] = $choice->id;
351 $eventdata['other']['optionid'] = $formanswer;
352
353 $event = \mod_choice\event\answer_updated::create($eventdata);
8a941443
AG
354 $event->add_record_snapshot('course', $course);
355 $event->add_record_snapshot('course_modules', $cm);
54bdf1d4
MA
356 $event->add_record_snapshot('choice', $choice);
357 foreach ($answersnapshots as $record) {
358 $event->add_record_snapshot('choice_answers', $record);
359 }
8a941443 360 $event->trigger();
39b3e7de 361 } else {
54bdf1d4
MA
362
363 foreach ($formanswers as $answer) {
364 $newanswer = new stdClass();
365 $newanswer->choiceid = $choice->id;
366 $newanswer->userid = $userid;
367 $newanswer->optionid = $answer;
368 $newanswer->timemodified = time();
369 $newanswer->id = $DB->insert_record("choice_answers", $newanswer);
370 $answersnapshots[] = $newanswer;
371 }
bc499733
SM
372
373 // Update completion state
374 $completion = new completion_info($course);
375 if ($completion->is_enabled($cm) && $choice->completionsubmit) {
376 $completion->update_state($cm, COMPLETION_COMPLETE);
377 }
8a941443
AG
378
379 $eventdata = array();
380 $eventdata['context'] = $context;
54bdf1d4 381 $eventdata['objectid'] = $choice->id;
8a941443
AG
382 $eventdata['userid'] = $userid;
383 $eventdata['courseid'] = $course->id;
384 $eventdata['other'] = array();
385 $eventdata['other']['choiceid'] = $choice->id;
54bdf1d4 386 $eventdata['other']['optionid'] = $formanswers;
8a941443
AG
387
388 $event = \mod_choice\event\answer_submitted::create($eventdata);
8a941443
AG
389 $event->add_record_snapshot('course', $course);
390 $event->add_record_snapshot('course_modules', $cm);
54bdf1d4
MA
391 $event->add_record_snapshot('choice', $choice);
392 foreach ($answersnapshots as $record) {
393 $event->add_record_snapshot('choice_answers', $record);
394 }
8a941443 395 $event->trigger();
39b3e7de 396 }
397 } else {
54bdf1d4
MA
398 // Check to see if current choice already selected - if not display error.
399 $currentids = array_keys($current);
400 if (array_diff($currentids, $formanswers) || array_diff($formanswers, $currentids) ) {
c7281355 401 print_error('choicefull', 'choice');
39b3e7de 402 }
403 }
348630d8 404}
405
4636bf83 406/**
407 * @param array $user
408 * @param object $cm
409 * @return void Output is echo'd
410 */
a61e4188 411function choice_show_reportlink($user, $cm) {
54bdf1d4 412 $userschosen = array();
a61e4188 413 foreach($user as $optionid => $userlist) {
414 if ($optionid) {
54bdf1d4 415 $userschosen = array_merge($userschosen, array_keys($userlist));
a61e4188 416 }
7b360f33 417 }
54bdf1d4 418 $responsecount = count(array_unique($userschosen));
5c880488 419
39b3e7de 420 echo '<div class="reportlink">';
5c880488 421 echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
39b3e7de 422 echo '</div>';
348630d8 423}
424
4636bf83 425/**
426 * @global object
4636bf83 427 * @param object $choice
428 * @param object $course
315d4971 429 * @param object $coursemodule
4636bf83 430 * @param array $allresponses
315d4971
RW
431
432 * * @param bool $allresponses
433 * @return object
4636bf83 434 */
634a5c0b
MG
435function prepare_choice_show_results($choice, $course, $cm, $allresponses) {
436 global $OUTPUT;
407caeeb 437
315d4971
RW
438 $display = clone($choice);
439 $display->coursemoduleid = $cm->id;
f7e93000
RW
440 $display->courseid = $course->id;
441
315d4971
RW
442 //overwrite options value;
443 $display->options = array();
444 $totaluser = 0;
445 foreach ($choice->option as $optionid => $optiontext) {
446 $display->options[$optionid] = new stdClass;
447 $display->options[$optionid]->text = $optiontext;
448 $display->options[$optionid]->maxanswer = $choice->maxanswers[$optionid];
449
450 if (array_key_exists($optionid, $allresponses)) {
e076641f 451 $display->options[$optionid]->user = $allresponses[$optionid];
315d4971 452 $totaluser += count($allresponses[$optionid]);
9d624d66 453 }
315d4971
RW
454 }
455 unset($display->option);
456 unset($display->maxanswers);
457
458 $display->numberofuser = $totaluser;
327c67a9 459 $context = context_module::instance($cm->id);
315d4971
RW
460 $display->viewresponsecapability = has_capability('mod/choice:readresponses', $context);
461 $display->deleterepsonsecapability = has_capability('mod/choice:deleteresponses',$context);
462 $display->fullnamecapability = has_capability('moodle/site:viewfullnames', $context);
f7e93000 463
9acb7700 464 if (empty($allresponses)) {
8f0767f1 465 echo $OUTPUT->heading(get_string("nousersyet"), 3, null);
9acb7700 466 return false;
39b3e7de 467 }
c2a4d016 468
315d4971 469 return $display;
9d624d66 470}
348630d8 471
4636bf83 472/**
473 * @global object
474 * @param array $attemptids
bc499733
SM
475 * @param object $choice Choice main table row
476 * @param object $cm Course-module object
477 * @param object $course Course object
4636bf83 478 * @return bool
479 */
bc499733 480function choice_delete_responses($attemptids, $choice, $cm, $course) {
516c5eca
PS
481 global $DB, $CFG;
482 require_once($CFG->libdir.'/completionlib.php');
483
dabfd0ed 484 if(!is_array($attemptids) || empty($attemptids)) {
348630d8 485 return false;
486 }
487
488 foreach($attemptids as $num => $attemptid) {
489 if(empty($attemptid)) {
490 unset($attemptids[$num]);
491 }
492 }
493
bc499733 494 $completion = new completion_info($course);
348630d8 495 foreach($attemptids as $attemptid) {
54bdf1d4
MA
496 if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'id' => $attemptid))) {
497 $DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'id' => $attemptid));
bc499733
SM
498 // Update completion state
499 if ($completion->is_enabled($cm) && $choice->completionsubmit) {
500 $completion->update_state($cm, COMPLETION_INCOMPLETE, $attemptid);
501 }
348630d8 502 }
ebd3c7ac 503 }
348630d8 504 return true;
505}
506
04eba58f 507
4636bf83 508/**
509 * Given an ID of an instance of this module,
510 * this function will permanently delete the instance
511 * and any data that depends on it.
512 *
513 * @global object
514 * @param int $id
515 * @return bool
516 */
04eba58f 517function choice_delete_instance($id) {
c18269c7 518 global $DB;
04eba58f 519
c18269c7 520 if (! $choice = $DB->get_record("choice", array("id"=>"$id"))) {
04eba58f 521 return false;
522 }
523
524 $result = true;
525
c18269c7 526 if (! $DB->delete_records("choice_answers", array("choiceid"=>"$choice->id"))) {
04eba58f 527 $result = false;
528 }
529
c18269c7 530 if (! $DB->delete_records("choice_options", array("choiceid"=>"$choice->id"))) {
04eba58f 531 $result = false;
532 }
a77a6009 533
c18269c7 534 if (! $DB->delete_records("choice", array("id"=>"$choice->id"))) {
6fd87e3b 535 $result = false;
ebd3c7ac 536 }
04eba58f 537
538 return $result;
539}
540
4636bf83 541/**
542 * Returns text string which is the answer that matches the id
543 *
544 * @global object
545 * @param object $choice
546 * @param int $id
547 * @return string
548 */
a77a6009 549function choice_get_option_text($choice, $id) {
407caeeb 550 global $DB;
4636bf83 551
407caeeb 552 if ($result = $DB->get_record("choice_options", array("id" => $id))) {
a77a6009 553 return $result->text;
6fd87e3b 554 } else {
555 return get_string("notanswered", "choice");
ff8d654f 556 }
c4016bc1 557}
558
4636bf83 559/**
560 * Gets a full choice record
561 *
562 * @global object
563 * @param int $choiceid
564 * @return object|bool The choice or false
565 */
cd3fccff 566function choice_get_choice($choiceid) {
407caeeb 567 global $DB;
a77a6009 568
407caeeb 569 if ($choice = $DB->get_record("choice", array("id" => $choiceid))) {
570 if ($options = $DB->get_records("choice_options", array("choiceid" => $choiceid), "id")) {
ff8d654f 571 foreach ($options as $option) {
ebd3c7ac 572 $choice->option[$option->id] = $option->text;
c9ff813c 573 $choice->maxanswers[$option->id] = $option->maxanswers;
ff8d654f 574 }
a77a6009 575 return $choice;
576 }
577 }
578 return false;
cd3fccff 579}
580
4636bf83 581/**
b2b4ec30
RT
582 * List the actions that correspond to a view of this module.
583 * This is used by the participation report.
584 *
585 * Note: This is not used by new logging system. Event with
586 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
587 * be considered as view action.
588 *
4636bf83 589 * @return array
590 */
f3221af9 591function choice_get_view_actions() {
cf051cc4 592 return array('view','view all','report');
f3221af9 593}
594
4636bf83 595/**
b2b4ec30
RT
596 * List the actions that correspond to a post of this module.
597 * This is used by the participation report.
598 *
599 * Note: This is not used by new logging system. Event with
600 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
601 * will be considered as post action.
602 *
4636bf83 603 * @return array
604 */
f3221af9 605function choice_get_post_actions() {
606 return array('choose','choose again');
607}
608
0b5a80a1 609
610/**
611 * Implementation of the function for printing the form elements that control
612 * whether the course reset functionality affects the choice.
1adbd2c3 613 *
4636bf83 614 * @param object $mform form passed by reference
0b5a80a1 615 */
616function choice_reset_course_form_definition(&$mform) {
617 $mform->addElement('header', 'choiceheader', get_string('modulenameplural', 'choice'));
618 $mform->addElement('advcheckbox', 'reset_choice', get_string('removeresponses','choice'));
619}
620
621/**
622 * Course reset form defaults.
4636bf83 623 *
624 * @return array
0b5a80a1 625 */
626function choice_reset_course_form_defaults($course) {
627 return array('reset_choice'=>1);
628}
629
630/**
72d2982e 631 * Actual implementation of the reset course functionality, delete all the
0b5a80a1 632 * choice responses for course $data->courseid.
4636bf83 633 *
634 * @global object
635 * @global object
636 * @param object $data the data submitted from the reset course.
0b5a80a1 637 * @return array status array
638 */
639function choice_reset_userdata($data) {
407caeeb 640 global $CFG, $DB;
0b5a80a1 641
642 $componentstr = get_string('modulenameplural', 'choice');
643 $status = array();
644
645 if (!empty($data->reset_choice)) {
646 $choicessql = "SELECT ch.id
f7e93000
RW
647 FROM {choice} ch
648 WHERE ch.course=?";
0b5a80a1 649
407caeeb 650 $DB->delete_records_select('choice_answers', "choiceid IN ($choicessql)", array($data->courseid));
0b5a80a1 651 $status[] = array('component'=>$componentstr, 'item'=>get_string('removeresponses', 'choice'), 'error'=>false);
652 }
653
654 /// updating dates - shift may be negative too
655 if ($data->timeshift) {
656 shift_course_mod_dates('choice', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
657 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
658 }
659
660 return $status;
661}
662
4636bf83 663/**
664 * @global object
665 * @global object
666 * @global object
667 * @uses CONTEXT_MODULE
668 * @param object $choice
669 * @param object $cm
670 * @param int $groupmode
671 * @return array
672 */
a61e4188 673function choice_get_response_data($choice, $cm, $groupmode) {
407caeeb 674 global $CFG, $USER, $DB;
a61e4188 675
327c67a9 676 $context = context_module::instance($cm->id);
a61e4188 677
678/// Get the current group
679 if ($groupmode > 0) {
680 $currentgroup = groups_get_activity_group($cm);
681 } else {
682 $currentgroup = 0;
683 }
684
5d9d5227 685/// Initialise the returned array, which is a matrix: $allresponses[responseid][userid] = responseobject
686 $allresponses = array();
a61e4188 687
688/// First get all the users who have access here
689/// To start with we assume they are all "unanswered" then move them later
9695ff81 690 $allresponses[0] = get_enrolled_users($context, 'mod/choice:choose', $currentgroup, user_picture::fields('u', array('idnumber')));
a61e4188 691
692/// Get all the recorded responses for this choice
407caeeb 693 $rawresponses = $DB->get_records('choice_answers', array('choiceid' => $choice->id));
a61e4188 694
695/// Use the responses to move users into the correct column
696
5d9d5227 697 if ($rawresponses) {
54bdf1d4 698 $answeredusers = array();
5d9d5227 699 foreach ($rawresponses as $response) {
700 if (isset($allresponses[0][$response->userid])) { // This person is enrolled and in correct group
701 $allresponses[0][$response->userid]->timemodified = $response->timemodified;
702 $allresponses[$response->optionid][$response->userid] = clone($allresponses[0][$response->userid]);
54bdf1d4
MA
703 $allresponses[$response->optionid][$response->userid]->answerid = $response->id;
704 $answeredusers[] = $response->userid;
a61e4188 705 }
706 }
54bdf1d4
MA
707 foreach ($answeredusers as $answereduser) {
708 unset($allresponses[0][$answereduser]);
709 }
f7e93000 710 }
5d9d5227 711 return $allresponses;
f432bebf 712}
a61e4188 713
f432bebf 714/**
715 * Returns all other caps used in module
4636bf83 716 *
717 * @return array
f432bebf 718 */
db19ec01 719function choice_get_extra_capabilities() {
f432bebf 720 return array('moodle/site:accessallgroups');
a61e4188 721}
f432bebf 722
18a2a0cb 723/**
4636bf83 724 * @uses FEATURE_GROUPS
725 * @uses FEATURE_GROUPINGS
4636bf83 726 * @uses FEATURE_MOD_INTRO
727 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
728 * @uses FEATURE_GRADE_HAS_GRADE
729 * @uses FEATURE_GRADE_OUTCOMES
18a2a0cb 730 * @param string $feature FEATURE_xx constant for requested feature
731 * @return mixed True if module supports feature, null if doesn't know
732 */
733function choice_supports($feature) {
734 switch($feature) {
42f103be 735 case FEATURE_GROUPS: return true;
736 case FEATURE_GROUPINGS: return true;
dc5c2bd9 737 case FEATURE_MOD_INTRO: return true;
18a2a0cb 738 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
bc499733 739 case FEATURE_COMPLETION_HAS_RULES: return true;
42f103be 740 case FEATURE_GRADE_HAS_GRADE: return false;
741 case FEATURE_GRADE_OUTCOMES: return false;
98baf0d7 742 case FEATURE_BACKUP_MOODLE2: return true;
3e4c2435 743 case FEATURE_SHOW_DESCRIPTION: return true;
42f103be 744
18a2a0cb 745 default: return null;
746 }
747}
97011c64 748
0b29477b
SH
749/**
750 * Adds module specific settings to the settings block
751 *
752 * @param settings_navigation $settings The settings navigation object
753 * @param navigation_node $choicenode The node to add module settings to
754 */
755function choice_extend_settings_navigation(settings_navigation $settings, navigation_node $choicenode) {
756 global $PAGE;
97011c64 757
758 if (has_capability('mod/choice:readresponses', $PAGE->cm->context)) {
759
760 $groupmode = groups_get_activity_groupmode($PAGE->cm);
761 if ($groupmode) {
762 groups_get_activity_group($PAGE->cm, true);
763 }
56115eea
SH
764 // We only actually need the choice id here
765 $choice = new stdClass;
766 $choice->id = $PAGE->cm->instance;
97011c64 767 $allresponses = choice_get_response_data($choice, $PAGE->cm, $groupmode); // Big function, approx 6 SQL calls per user
768
769 $responsecount =0;
770 foreach($allresponses as $optionid => $userlist) {
771 if ($optionid) {
772 $responsecount += count($userlist);
773 }
774 }
0b29477b 775 $choicenode->add(get_string("viewallresponses", "choice", $responsecount), new moodle_url('/mod/choice/report.php', array('id'=>$PAGE->cm->id)));
97011c64 776 }
98baf0d7 777}
bc499733
SM
778
779/**
780 * Obtains the automatic completion state for this choice based on any conditions
781 * in forum settings.
782 *
783 * @param object $course Course
784 * @param object $cm Course-module
785 * @param int $userid User ID
786 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
787 * @return bool True if completed, false if not, $type if conditions not set.
788 */
789function choice_get_completion_state($course, $cm, $userid, $type) {
790 global $CFG,$DB;
791
792 // Get choice details
793 $choice = $DB->get_record('choice', array('id'=>$cm->instance), '*',
794 MUST_EXIST);
795
516c5eca 796 // If completion option is enabled, evaluate it and return true/false
bc499733
SM
797 if($choice->completionsubmit) {
798 return $DB->record_exists('choice_answers', array(
799 'choiceid'=>$choice->id, 'userid'=>$userid));
800 } else {
801 // Completion option is not enabled so just return $type
802 return $type;
803 }
804}
b1627a92
DC
805
806/**
807 * Return a list of page types
808 * @param string $pagetype current page type
809 * @param stdClass $parentcontext Block's parent context
810 * @param stdClass $currentcontext Current context of block
811 */
b38e2e28 812function choice_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
813 $module_pagetype = array('mod-choice-*'=>get_string('page-mod-choice-x', 'choice'));
814 return $module_pagetype;
815}