Merge branch 'MDL-47248-master-rsssitelogo' of git://github.com/mudrd8mz/moodle
[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
407caeeb 250 $current = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid));
327c67a9 251 $context = context_module::instance($cm->id);
c75ba879 252
253 $countanswers=0;
254 if($choice->limitanswers) {
255 // Find out whether groups are being used and enabled
256 if (groups_get_activity_groupmode($cm) > 0) {
257 $currentgroup = groups_get_activity_group($cm);
258 } else {
259 $currentgroup = 0;
260 }
261 if($currentgroup) {
262 // If groups are being used, retrieve responses only for users in
263 // current group
264 global $CFG;
407caeeb 265 $answers = $DB->get_records_sql("
266SELECT
c75ba879 267 ca.*
407caeeb 268FROM
269 {choice_answers} ca
270 INNER JOIN {groups_members} gm ON ca.userid=gm.userid
c75ba879 271WHERE
407caeeb 272 optionid=?
273 AND gm.groupid=?", array($formanswer, $currentgroup));
c75ba879 274 } else {
275 // Groups are not used, retrieve all answers for this option ID
407caeeb 276 $answers = $DB->get_records("choice_answers", array("optionid" => $formanswer));
ff8d654f 277 }
278
c75ba879 279 if ($answers) {
280 foreach ($answers as $a) { //only return enrolled users.
4f0c2d00 281 if (is_enrolled($context, $a->userid, 'mod/choice:choose')) {
c75ba879 282 $countanswers++;
283 }
284 }
285 }
286 $maxans = $choice->maxanswers[$formanswer];
39b3e7de 287 }
ff8d654f 288
289 if (!($choice->limitanswers && ($countanswers >= $maxans) )) {
39b3e7de 290 if ($current) {
ebd3c7ac 291
39b3e7de 292 $newanswer = $current;
293 $newanswer->optionid = $formanswer;
294 $newanswer->timemodified = time();
7826abc7 295 $DB->update_record("choice_answers", $newanswer);
8a941443
AG
296
297 $eventdata = array();
298 $eventdata['context'] = $context;
299 $eventdata['objectid'] = $newanswer->id;
300 $eventdata['userid'] = $userid;
301 $eventdata['courseid'] = $course->id;
302 $eventdata['other'] = array();
303 $eventdata['other']['choiceid'] = $choice->id;
304 $eventdata['other']['optionid'] = $formanswer;
305
306 $event = \mod_choice\event\answer_updated::create($eventdata);
307 $event->add_record_snapshot('choice_answers', $newanswer);
308 $event->add_record_snapshot('course', $course);
309 $event->add_record_snapshot('course_modules', $cm);
310 $event->trigger();
39b3e7de 311 } else {
b09912f8 312 $newanswer = new stdClass();
39b3e7de 313 $newanswer->choiceid = $choice->id;
314 $newanswer->userid = $userid;
315 $newanswer->optionid = $formanswer;
316 $newanswer->timemodified = time();
8a941443 317 $newanswer->id = $DB->insert_record("choice_answers", $newanswer);
bc499733
SM
318
319 // Update completion state
320 $completion = new completion_info($course);
321 if ($completion->is_enabled($cm) && $choice->completionsubmit) {
322 $completion->update_state($cm, COMPLETION_COMPLETE);
323 }
8a941443
AG
324
325 $eventdata = array();
326 $eventdata['context'] = $context;
327 $eventdata['objectid'] = $newanswer->id;
328 $eventdata['userid'] = $userid;
329 $eventdata['courseid'] = $course->id;
330 $eventdata['other'] = array();
331 $eventdata['other']['choiceid'] = $choice->id;
332 $eventdata['other']['optionid'] = $formanswer;
333
334 $event = \mod_choice\event\answer_submitted::create($eventdata);
335 $event->add_record_snapshot('choice_answers', $newanswer);
336 $event->add_record_snapshot('course', $course);
337 $event->add_record_snapshot('course_modules', $cm);
338 $event->trigger();
39b3e7de 339 }
340 } else {
341 if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error
c7281355 342 print_error('choicefull', 'choice');
39b3e7de 343 }
344 }
348630d8 345}
346
4636bf83 347/**
348 * @param array $user
349 * @param object $cm
350 * @return void Output is echo'd
351 */
a61e4188 352function choice_show_reportlink($user, $cm) {
353 $responsecount =0;
354 foreach($user as $optionid => $userlist) {
355 if ($optionid) {
356 $responsecount += count($userlist);
357 }
7b360f33 358 }
5c880488 359
39b3e7de 360 echo '<div class="reportlink">';
5c880488 361 echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
39b3e7de 362 echo '</div>';
348630d8 363}
364
4636bf83 365/**
366 * @global object
4636bf83 367 * @param object $choice
368 * @param object $course
315d4971 369 * @param object $coursemodule
4636bf83 370 * @param array $allresponses
315d4971
RW
371
372 * * @param bool $allresponses
373 * @return object
4636bf83 374 */
634a5c0b
MG
375function prepare_choice_show_results($choice, $course, $cm, $allresponses) {
376 global $OUTPUT;
407caeeb 377
315d4971
RW
378 $display = clone($choice);
379 $display->coursemoduleid = $cm->id;
f7e93000
RW
380 $display->courseid = $course->id;
381
315d4971
RW
382 //overwrite options value;
383 $display->options = array();
384 $totaluser = 0;
385 foreach ($choice->option as $optionid => $optiontext) {
386 $display->options[$optionid] = new stdClass;
387 $display->options[$optionid]->text = $optiontext;
388 $display->options[$optionid]->maxanswer = $choice->maxanswers[$optionid];
389
390 if (array_key_exists($optionid, $allresponses)) {
e076641f 391 $display->options[$optionid]->user = $allresponses[$optionid];
315d4971 392 $totaluser += count($allresponses[$optionid]);
9d624d66 393 }
315d4971
RW
394 }
395 unset($display->option);
396 unset($display->maxanswers);
397
398 $display->numberofuser = $totaluser;
327c67a9 399 $context = context_module::instance($cm->id);
315d4971
RW
400 $display->viewresponsecapability = has_capability('mod/choice:readresponses', $context);
401 $display->deleterepsonsecapability = has_capability('mod/choice:deleteresponses',$context);
402 $display->fullnamecapability = has_capability('moodle/site:viewfullnames', $context);
f7e93000 403
9acb7700 404 if (empty($allresponses)) {
8f0767f1 405 echo $OUTPUT->heading(get_string("nousersyet"), 3, null);
9acb7700 406 return false;
39b3e7de 407 }
c2a4d016 408
315d4971 409 return $display;
9d624d66 410}
348630d8 411
4636bf83 412/**
413 * @global object
414 * @param array $attemptids
bc499733
SM
415 * @param object $choice Choice main table row
416 * @param object $cm Course-module object
417 * @param object $course Course object
4636bf83 418 * @return bool
419 */
bc499733 420function choice_delete_responses($attemptids, $choice, $cm, $course) {
516c5eca
PS
421 global $DB, $CFG;
422 require_once($CFG->libdir.'/completionlib.php');
423
dabfd0ed 424 if(!is_array($attemptids) || empty($attemptids)) {
348630d8 425 return false;
426 }
427
428 foreach($attemptids as $num => $attemptid) {
429 if(empty($attemptid)) {
430 unset($attemptids[$num]);
431 }
432 }
433
bc499733 434 $completion = new completion_info($course);
348630d8 435 foreach($attemptids as $attemptid) {
bc499733
SM
436 if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $attemptid))) {
437 $DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $attemptid));
438 // Update completion state
439 if ($completion->is_enabled($cm) && $choice->completionsubmit) {
440 $completion->update_state($cm, COMPLETION_INCOMPLETE, $attemptid);
441 }
348630d8 442 }
ebd3c7ac 443 }
348630d8 444 return true;
445}
446
04eba58f 447
4636bf83 448/**
449 * Given an ID of an instance of this module,
450 * this function will permanently delete the instance
451 * and any data that depends on it.
452 *
453 * @global object
454 * @param int $id
455 * @return bool
456 */
04eba58f 457function choice_delete_instance($id) {
c18269c7 458 global $DB;
04eba58f 459
c18269c7 460 if (! $choice = $DB->get_record("choice", array("id"=>"$id"))) {
04eba58f 461 return false;
462 }
463
464 $result = true;
465
c18269c7 466 if (! $DB->delete_records("choice_answers", array("choiceid"=>"$choice->id"))) {
04eba58f 467 $result = false;
468 }
469
c18269c7 470 if (! $DB->delete_records("choice_options", array("choiceid"=>"$choice->id"))) {
04eba58f 471 $result = false;
472 }
a77a6009 473
c18269c7 474 if (! $DB->delete_records("choice", array("id"=>"$choice->id"))) {
6fd87e3b 475 $result = false;
ebd3c7ac 476 }
04eba58f 477
478 return $result;
479}
480
4636bf83 481/**
482 * Returns text string which is the answer that matches the id
483 *
484 * @global object
485 * @param object $choice
486 * @param int $id
487 * @return string
488 */
a77a6009 489function choice_get_option_text($choice, $id) {
407caeeb 490 global $DB;
4636bf83 491
407caeeb 492 if ($result = $DB->get_record("choice_options", array("id" => $id))) {
a77a6009 493 return $result->text;
6fd87e3b 494 } else {
495 return get_string("notanswered", "choice");
ff8d654f 496 }
c4016bc1 497}
498
4636bf83 499/**
500 * Gets a full choice record
501 *
502 * @global object
503 * @param int $choiceid
504 * @return object|bool The choice or false
505 */
cd3fccff 506function choice_get_choice($choiceid) {
407caeeb 507 global $DB;
a77a6009 508
407caeeb 509 if ($choice = $DB->get_record("choice", array("id" => $choiceid))) {
510 if ($options = $DB->get_records("choice_options", array("choiceid" => $choiceid), "id")) {
ff8d654f 511 foreach ($options as $option) {
ebd3c7ac 512 $choice->option[$option->id] = $option->text;
c9ff813c 513 $choice->maxanswers[$option->id] = $option->maxanswers;
ff8d654f 514 }
a77a6009 515 return $choice;
516 }
517 }
518 return false;
cd3fccff 519}
520
4636bf83 521/**
b2b4ec30
RT
522 * List the actions that correspond to a view of this module.
523 * This is used by the participation report.
524 *
525 * Note: This is not used by new logging system. Event with
526 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
527 * be considered as view action.
528 *
4636bf83 529 * @return array
530 */
f3221af9 531function choice_get_view_actions() {
cf051cc4 532 return array('view','view all','report');
f3221af9 533}
534
4636bf83 535/**
b2b4ec30
RT
536 * List the actions that correspond to a post of this module.
537 * This is used by the participation report.
538 *
539 * Note: This is not used by new logging system. Event with
540 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
541 * will be considered as post action.
542 *
4636bf83 543 * @return array
544 */
f3221af9 545function choice_get_post_actions() {
546 return array('choose','choose again');
547}
548
0b5a80a1 549
550/**
551 * Implementation of the function for printing the form elements that control
552 * whether the course reset functionality affects the choice.
1adbd2c3 553 *
4636bf83 554 * @param object $mform form passed by reference
0b5a80a1 555 */
556function choice_reset_course_form_definition(&$mform) {
557 $mform->addElement('header', 'choiceheader', get_string('modulenameplural', 'choice'));
558 $mform->addElement('advcheckbox', 'reset_choice', get_string('removeresponses','choice'));
559}
560
561/**
562 * Course reset form defaults.
4636bf83 563 *
564 * @return array
0b5a80a1 565 */
566function choice_reset_course_form_defaults($course) {
567 return array('reset_choice'=>1);
568}
569
570/**
72d2982e 571 * Actual implementation of the reset course functionality, delete all the
0b5a80a1 572 * choice responses for course $data->courseid.
4636bf83 573 *
574 * @global object
575 * @global object
576 * @param object $data the data submitted from the reset course.
0b5a80a1 577 * @return array status array
578 */
579function choice_reset_userdata($data) {
407caeeb 580 global $CFG, $DB;
0b5a80a1 581
582 $componentstr = get_string('modulenameplural', 'choice');
583 $status = array();
584
585 if (!empty($data->reset_choice)) {
586 $choicessql = "SELECT ch.id
f7e93000
RW
587 FROM {choice} ch
588 WHERE ch.course=?";
0b5a80a1 589
407caeeb 590 $DB->delete_records_select('choice_answers', "choiceid IN ($choicessql)", array($data->courseid));
0b5a80a1 591 $status[] = array('component'=>$componentstr, 'item'=>get_string('removeresponses', 'choice'), 'error'=>false);
592 }
593
594 /// updating dates - shift may be negative too
595 if ($data->timeshift) {
596 shift_course_mod_dates('choice', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
597 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
598 }
599
600 return $status;
601}
602
4636bf83 603/**
604 * @global object
605 * @global object
606 * @global object
607 * @uses CONTEXT_MODULE
608 * @param object $choice
609 * @param object $cm
610 * @param int $groupmode
611 * @return array
612 */
a61e4188 613function choice_get_response_data($choice, $cm, $groupmode) {
407caeeb 614 global $CFG, $USER, $DB;
a61e4188 615
327c67a9 616 $context = context_module::instance($cm->id);
a61e4188 617
618/// Get the current group
619 if ($groupmode > 0) {
620 $currentgroup = groups_get_activity_group($cm);
621 } else {
622 $currentgroup = 0;
623 }
624
5d9d5227 625/// Initialise the returned array, which is a matrix: $allresponses[responseid][userid] = responseobject
626 $allresponses = array();
a61e4188 627
628/// First get all the users who have access here
629/// To start with we assume they are all "unanswered" then move them later
9695ff81 630 $allresponses[0] = get_enrolled_users($context, 'mod/choice:choose', $currentgroup, user_picture::fields('u', array('idnumber')));
a61e4188 631
632/// Get all the recorded responses for this choice
407caeeb 633 $rawresponses = $DB->get_records('choice_answers', array('choiceid' => $choice->id));
a61e4188 634
635/// Use the responses to move users into the correct column
636
5d9d5227 637 if ($rawresponses) {
638 foreach ($rawresponses as $response) {
639 if (isset($allresponses[0][$response->userid])) { // This person is enrolled and in correct group
640 $allresponses[0][$response->userid]->timemodified = $response->timemodified;
641 $allresponses[$response->optionid][$response->userid] = clone($allresponses[0][$response->userid]);
642 unset($allresponses[0][$response->userid]); // Remove from unanswered column
a61e4188 643 }
644 }
f7e93000 645 }
5d9d5227 646 return $allresponses;
f432bebf 647}
a61e4188 648
f432bebf 649/**
650 * Returns all other caps used in module
4636bf83 651 *
652 * @return array
f432bebf 653 */
db19ec01 654function choice_get_extra_capabilities() {
f432bebf 655 return array('moodle/site:accessallgroups');
a61e4188 656}
f432bebf 657
18a2a0cb 658/**
4636bf83 659 * @uses FEATURE_GROUPS
660 * @uses FEATURE_GROUPINGS
4636bf83 661 * @uses FEATURE_MOD_INTRO
662 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
663 * @uses FEATURE_GRADE_HAS_GRADE
664 * @uses FEATURE_GRADE_OUTCOMES
18a2a0cb 665 * @param string $feature FEATURE_xx constant for requested feature
666 * @return mixed True if module supports feature, null if doesn't know
667 */
668function choice_supports($feature) {
669 switch($feature) {
42f103be 670 case FEATURE_GROUPS: return true;
671 case FEATURE_GROUPINGS: return true;
dc5c2bd9 672 case FEATURE_MOD_INTRO: return true;
18a2a0cb 673 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
bc499733 674 case FEATURE_COMPLETION_HAS_RULES: return true;
42f103be 675 case FEATURE_GRADE_HAS_GRADE: return false;
676 case FEATURE_GRADE_OUTCOMES: return false;
98baf0d7 677 case FEATURE_BACKUP_MOODLE2: return true;
3e4c2435 678 case FEATURE_SHOW_DESCRIPTION: return true;
42f103be 679
18a2a0cb 680 default: return null;
681 }
682}
97011c64 683
0b29477b
SH
684/**
685 * Adds module specific settings to the settings block
686 *
687 * @param settings_navigation $settings The settings navigation object
688 * @param navigation_node $choicenode The node to add module settings to
689 */
690function choice_extend_settings_navigation(settings_navigation $settings, navigation_node $choicenode) {
691 global $PAGE;
97011c64 692
693 if (has_capability('mod/choice:readresponses', $PAGE->cm->context)) {
694
695 $groupmode = groups_get_activity_groupmode($PAGE->cm);
696 if ($groupmode) {
697 groups_get_activity_group($PAGE->cm, true);
698 }
56115eea
SH
699 // We only actually need the choice id here
700 $choice = new stdClass;
701 $choice->id = $PAGE->cm->instance;
97011c64 702 $allresponses = choice_get_response_data($choice, $PAGE->cm, $groupmode); // Big function, approx 6 SQL calls per user
703
704 $responsecount =0;
705 foreach($allresponses as $optionid => $userlist) {
706 if ($optionid) {
707 $responsecount += count($userlist);
708 }
709 }
0b29477b 710 $choicenode->add(get_string("viewallresponses", "choice", $responsecount), new moodle_url('/mod/choice/report.php', array('id'=>$PAGE->cm->id)));
97011c64 711 }
98baf0d7 712}
bc499733
SM
713
714/**
715 * Obtains the automatic completion state for this choice based on any conditions
716 * in forum settings.
717 *
718 * @param object $course Course
719 * @param object $cm Course-module
720 * @param int $userid User ID
721 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
722 * @return bool True if completed, false if not, $type if conditions not set.
723 */
724function choice_get_completion_state($course, $cm, $userid, $type) {
725 global $CFG,$DB;
726
727 // Get choice details
728 $choice = $DB->get_record('choice', array('id'=>$cm->instance), '*',
729 MUST_EXIST);
730
516c5eca 731 // If completion option is enabled, evaluate it and return true/false
bc499733
SM
732 if($choice->completionsubmit) {
733 return $DB->record_exists('choice_answers', array(
734 'choiceid'=>$choice->id, 'userid'=>$userid));
735 } else {
736 // Completion option is not enabled so just return $type
737 return $type;
738 }
739}
b1627a92
DC
740
741/**
742 * Return a list of page types
743 * @param string $pagetype current page type
744 * @param stdClass $parentcontext Block's parent context
745 * @param stdClass $currentcontext Current context of block
746 */
b38e2e28 747function choice_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
748 $module_pagetype = array('mod-choice-*'=>get_string('page-mod-choice-x', 'choice'));
749 return $module_pagetype;
750}