MDL-24316 version bump needed for js cache resetting
[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/**
750eb434 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
RW
202 $cdisplay['limitanswers'] = true;
203 $context = get_context_instance(CONTEXT_MODULE, $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;
210 $option->text = $text;
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;
3c56a7cc 224 }
315d4971 225 $cdisplay['options'][] = $option;
dabfd0ed 226 }
348630d8 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;
348630d8 233 }
348630d8 234
315d4971 235 return $cdisplay;
39b3e7de 236 }
348630d8 237
4636bf83 238/**
239 * @global object
240 * @param int $formanswer
241 * @param object $choice
242 * @param int $userid
243 * @param int $courseid
244 * @param object $cm
245 */
acaf7b86 246function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $cm) {
407caeeb 247 global $DB;
248 $current = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid));
dabfd0ed 249 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
c75ba879 250
251 $countanswers=0;
252 if($choice->limitanswers) {
253 // Find out whether groups are being used and enabled
254 if (groups_get_activity_groupmode($cm) > 0) {
255 $currentgroup = groups_get_activity_group($cm);
256 } else {
257 $currentgroup = 0;
258 }
259 if($currentgroup) {
260 // If groups are being used, retrieve responses only for users in
261 // current group
262 global $CFG;
407caeeb 263 $answers = $DB->get_records_sql("
264SELECT
c75ba879 265 ca.*
407caeeb 266FROM
267 {choice_answers} ca
268 INNER JOIN {groups_members} gm ON ca.userid=gm.userid
c75ba879 269WHERE
407caeeb 270 optionid=?
271 AND gm.groupid=?", array($formanswer, $currentgroup));
c75ba879 272 } else {
273 // Groups are not used, retrieve all answers for this option ID
407caeeb 274 $answers = $DB->get_records("choice_answers", array("optionid" => $formanswer));
ff8d654f 275 }
276
c75ba879 277 if ($answers) {
278 foreach ($answers as $a) { //only return enrolled users.
4f0c2d00 279 if (is_enrolled($context, $a->userid, 'mod/choice:choose')) {
c75ba879 280 $countanswers++;
281 }
282 }
283 }
284 $maxans = $choice->maxanswers[$formanswer];
39b3e7de 285 }
ff8d654f 286
287 if (!($choice->limitanswers && ($countanswers >= $maxans) )) {
39b3e7de 288 if ($current) {
ebd3c7ac 289
39b3e7de 290 $newanswer = $current;
291 $newanswer->optionid = $formanswer;
292 $newanswer->timemodified = time();
7826abc7 293 $DB->update_record("choice_answers", $newanswer);
39b3e7de 294 add_to_log($courseid, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id);
295 } else {
296 $newanswer = NULL;
297 $newanswer->choiceid = $choice->id;
298 $newanswer->userid = $userid;
299 $newanswer->optionid = $formanswer;
300 $newanswer->timemodified = time();
7826abc7 301 $DB->insert_record("choice_answers", $newanswer);
39b3e7de 302 add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id);
303 }
304 } else {
305 if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error
c7281355 306 print_error('choicefull', 'choice');
39b3e7de 307 }
308 }
348630d8 309}
310
4636bf83 311/**
312 * @param array $user
313 * @param object $cm
314 * @return void Output is echo'd
315 */
a61e4188 316function choice_show_reportlink($user, $cm) {
317 $responsecount =0;
318 foreach($user as $optionid => $userlist) {
319 if ($optionid) {
320 $responsecount += count($userlist);
321 }
7b360f33 322 }
5c880488 323
39b3e7de 324 echo '<div class="reportlink">';
5c880488 325 echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
39b3e7de 326 echo '</div>';
348630d8 327}
328
4636bf83 329/**
330 * @global object
4636bf83 331 * @param object $choice
332 * @param object $course
315d4971 333 * @param object $coursemodule
4636bf83 334 * @param array $allresponses
315d4971
RW
335
336 * * @param bool $allresponses
337 * @return object
4636bf83 338 */
315d4971 339function prepare_choice_show_results($choice, $course, $cm, $allresponses, $forcepublish=false) {
db8935ef 340 global $CFG, $CHOICE_COLUMN_HEIGHT, $FULLSCRIPT, $PAGE, $OUTPUT, $DB;
407caeeb 341
315d4971
RW
342 $display = clone($choice);
343 $display->coursemoduleid = $cm->id;
f7e93000
RW
344 $display->courseid = $course->id;
345
315d4971
RW
346 //overwrite options value;
347 $display->options = array();
348 $totaluser = 0;
349 foreach ($choice->option as $optionid => $optiontext) {
350 $display->options[$optionid] = new stdClass;
351 $display->options[$optionid]->text = $optiontext;
352 $display->options[$optionid]->maxanswer = $choice->maxanswers[$optionid];
353
354 if (array_key_exists($optionid, $allresponses)) {
355 $display->options[$optionid]->user = $allresponses[$optionid]; //->user;
356 $totaluser += count($allresponses[$optionid]);
9d624d66 357 }
315d4971
RW
358 }
359 unset($display->option);
360 unset($display->maxanswers);
361
362 $display->numberofuser = $totaluser;
363 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
364 $display->viewresponsecapability = has_capability('mod/choice:readresponses', $context);
365 $display->deleterepsonsecapability = has_capability('mod/choice:deleteresponses',$context);
366 $display->fullnamecapability = has_capability('moodle/site:viewfullnames', $context);
f7e93000 367
9acb7700 368 if (empty($allresponses)) {
5ddd4e06 369 echo $OUTPUT->heading(get_string("nousersyet"));
9acb7700 370 return false;
39b3e7de 371 }
c2a4d016 372
315d4971 373
c2a4d016 374 $totalresponsecount = 0;
5d9d5227 375 foreach ($allresponses as $optionid => $userlist) {
c2a4d016 376 if ($choice->showunanswered || $optionid) {
377 $totalresponsecount += count($userlist);
378 }
379 }
407caeeb 380
a61e4188 381 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
348630d8 382
a61e4188 383 $hascapfullnames = has_capability('moodle/site:viewfullnames', $context);
407caeeb 384
385 $viewresponses = has_capability('mod/choice:readresponses', $context);
39b3e7de 386 switch ($forcepublish) {
387 case CHOICE_PUBLISH_NAMES:
4ec64b4f 388 echo '<div id="tablecontainer">';
389 if ($viewresponses) {
11e7b506 390 echo '<form id="attemptsform" method="post" action="'.$FULLSCRIPT.'" onsubmit="var menu = document.getElementById(\'menuaction\'); return (menu.options[menu.selectedIndex].value == \'delete\' ? \''.addslashes_js(get_string('deleteattemptcheck','quiz')).'\' : true);">';
95aa949b 391 echo '<div>';
348630d8 392 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
afb6f77d 393 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
348630d8 394 echo '<input type="hidden" name="mode" value="overview" />';
4ec64b4f 395 }
ff8d654f 396
d71bc1ab 397 echo "<table cellpadding=\"5\" cellspacing=\"10\" class=\"results names\">";
348630d8 398 echo "<tr>";
407caeeb 399
aa4836f4 400 $columncount = array(); // number of votes in each column
a61e4188 401 if ($choice->showunanswered) {
402 $columncount[0] = 0;
403 echo "<th class=\"col0 header\" scope=\"col\">";
404 print_string('notanswered', 'choice');
405 echo "</th>";
406 }
407 $count = 1;
408 foreach ($choice->option as $optionid => $optiontext) {
aa4836f4 409 $columncount[$optionid] = 0; // init counters
a61e4188 410 echo "<th class=\"col$count header\" scope=\"col\">";
411 echo format_string($optiontext);
348630d8 412 echo "</th>";
413 $count++;
414 }
415 echo "</tr><tr>";
416
82ab7cb5 417 if ($choice->showunanswered) {
a61e4188 418 echo "<td class=\"col$count data\" >";
95aa949b 419 // added empty row so that when the next iteration is empty,
034ef761 420 // we do not get <table></table> error from w3c validator
95aa949b 421 // MDL-7861
422 echo "<table class=\"choiceresponse\"><tr><td></td></tr>";
82ab7cb5 423 if (!empty($allresponses[0])) {
424 foreach ($allresponses[0] as $user) {
425 echo "<tr>";
426 echo "<td class=\"picture\">";
812dbaf7 427 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
82ab7cb5 428 echo "</td><td class=\"fullname\">";
429 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
430 echo fullname($user, $hascapfullnames);
431 echo "</a>";
432 echo "</td></tr>";
433 }
348630d8 434 }
a61e4188 435 echo "</table></td>";
436 }
a82fb0b6 437 $count = 1;
a61e4188 438 foreach ($choice->option as $optionid => $optiontext) {
439 echo '<td class="col'.$count.' data" >';
440
441 // added empty row so that when the next iteration is empty,
034ef761 442 // we do not get <table></table> error from w3c validator
a61e4188 443 // MDL-7861
444 echo '<table class="choiceresponse"><tr><td></td></tr>';
5d9d5227 445 if (isset($allresponses[$optionid])) {
446 foreach ($allresponses[$optionid] as $user) {
a61e4188 447 $columncount[$optionid] += 1;
448 echo '<tr><td class="attemptcell">';
cb4db90d 449 if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
a61e4188 450 echo '<input type="checkbox" name="attemptid[]" value="'. $user->id. '" />';
451 }
452 echo '</td><td class="picture">';
812dbaf7 453 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
a61e4188 454 echo '</td><td class="fullname">';
455 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
456 echo fullname($user, $hascapfullnames);
457 echo '</a>';
458 echo '</td></tr>';
459 }
460 }
461 $count++;
462 echo '</table></td>';
348630d8 463 }
464 echo "</tr><tr>";
a82fb0b6 465 $count = 1;
407caeeb 466
a61e4188 467 if ($choice->showunanswered) {
468 echo "<td></td>";
469 }
3a11c09f 470
a61e4188 471 foreach ($choice->option as $optionid => $optiontext) {
a82fb0b6 472 echo "<td align=\"center\" class=\"col$count count\">";
a61e4188 473 if ($choice->limitanswers) {
348630d8 474 echo get_string("taken", "choice").":";
aa4836f4 475 echo $columncount[$optionid];
71502268 476 echo "<br/>";
348630d8 477 echo get_string("limit", "choice").":";
b723a4f8 478 echo $choice->maxanswers[$optionid];
19e559e4 479 } else {
a61e4188 480 if (isset($columncount[$optionid])) {
481 echo $columncount[$optionid];
482 }
348630d8 483 }
484 echo "</td>";
485 $count++;
486 }
3402a6f3 487 echo "</tr>";
407caeeb 488
39b3e7de 489 /// Print "Select all" etc.
cb4db90d 490 if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
3402a6f3 491 echo '<tr><td></td><td>';
9b854b2d
DM
492 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall').'</a> / ';
493 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('deselectall').'</a> ';
39b3e7de 494 echo '&nbsp;&nbsp;';
9b854b2d
DM
495 echo html_writer::label(get_string('withselected', 'choice'), 'menuaction');
496 echo html_writer::select(array('delete' => get_string('delete')), 'action', '', array(''=>get_string('withselectedusers')), array('id'=>'menuaction'));
edc28287
PS
497 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('attemptsform', 'menuaction', ''));
498 echo '<noscript id="noscriptmenuaction" style="display:inline">';
95aa949b 499 echo '<div>';
500 echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
3402a6f3 501 echo '</td><td></td></tr>';
39b3e7de 502 }
407caeeb 503
4ec64b4f 504 echo "</table></div>";
a61e4188 505 if ($viewresponses) {
4ec64b4f 506 echo "</form></div>";
dabfd0ed 507 }
348630d8 508 break;
9d624d66 509 }
315d4971 510 return $display;
9d624d66 511}
348630d8 512
4636bf83 513/**
514 * @global object
515 * @param array $attemptids
516 * @param int $choiceid
517 * @return bool
518 */
a61e4188 519function choice_delete_responses($attemptids, $choiceid) {
407caeeb 520 global $DB;
dabfd0ed 521 if(!is_array($attemptids) || empty($attemptids)) {
348630d8 522 return false;
523 }
524
525 foreach($attemptids as $num => $attemptid) {
526 if(empty($attemptid)) {
527 unset($attemptids[$num]);
528 }
529 }
530
531 foreach($attemptids as $attemptid) {
407caeeb 532 if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choiceid, 'userid' => $attemptid))) {
533 $DB->delete_records('choice_answers', array('choiceid' => $choiceid, 'userid' => $attemptid));
348630d8 534 }
ebd3c7ac 535 }
348630d8 536 return true;
537}
538
04eba58f 539
4636bf83 540/**
541 * Given an ID of an instance of this module,
542 * this function will permanently delete the instance
543 * and any data that depends on it.
544 *
545 * @global object
546 * @param int $id
547 * @return bool
548 */
04eba58f 549function choice_delete_instance($id) {
c18269c7 550 global $DB;
04eba58f 551
c18269c7 552 if (! $choice = $DB->get_record("choice", array("id"=>"$id"))) {
04eba58f 553 return false;
554 }
555
556 $result = true;
557
c18269c7 558 if (! $DB->delete_records("choice_answers", array("choiceid"=>"$choice->id"))) {
04eba58f 559 $result = false;
560 }
561
c18269c7 562 if (! $DB->delete_records("choice_options", array("choiceid"=>"$choice->id"))) {
04eba58f 563 $result = false;
564 }
a77a6009 565
c18269c7 566 if (! $DB->delete_records("choice", array("id"=>"$choice->id"))) {
6fd87e3b 567 $result = false;
ebd3c7ac 568 }
04eba58f 569
570 return $result;
571}
572
4636bf83 573/**
574 * Returns the users with data in one choice
575 * (users with records in choice_responses, students)
576 *
577 * @param int $choiceid
578 * @return array
579 */
05855091 580function choice_get_participants($choiceid) {
585fb1f4 581 global $DB;
05855091 582
583 //Get students
407caeeb 584 $students = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
585 FROM {user} u,
586 {choice_answers} a
587 WHERE a.choiceid = ? AND
588 u.id = a.userid", array($choiceid));
05855091 589
590 //Return students array (it contains an array of unique users)
591 return ($students);
592}
593
4636bf83 594/**
595 * Returns text string which is the answer that matches the id
596 *
597 * @global object
598 * @param object $choice
599 * @param int $id
600 * @return string
601 */
a77a6009 602function choice_get_option_text($choice, $id) {
407caeeb 603 global $DB;
4636bf83 604
407caeeb 605 if ($result = $DB->get_record("choice_options", array("id" => $id))) {
a77a6009 606 return $result->text;
6fd87e3b 607 } else {
608 return get_string("notanswered", "choice");
ff8d654f 609 }
c4016bc1 610}
611
4636bf83 612/**
613 * Gets a full choice record
614 *
615 * @global object
616 * @param int $choiceid
617 * @return object|bool The choice or false
618 */
cd3fccff 619function choice_get_choice($choiceid) {
407caeeb 620 global $DB;
a77a6009 621
407caeeb 622 if ($choice = $DB->get_record("choice", array("id" => $choiceid))) {
623 if ($options = $DB->get_records("choice_options", array("choiceid" => $choiceid), "id")) {
ff8d654f 624 foreach ($options as $option) {
ebd3c7ac 625 $choice->option[$option->id] = $option->text;
c9ff813c 626 $choice->maxanswers[$option->id] = $option->maxanswers;
ff8d654f 627 }
a77a6009 628 return $choice;
629 }
630 }
631 return false;
cd3fccff 632}
633
4636bf83 634/**
635 * @return array
636 */
f3221af9 637function choice_get_view_actions() {
638 return array('view','view all','report');
639}
640
4636bf83 641/**
642 * @return array
643 */
f3221af9 644function choice_get_post_actions() {
645 return array('choose','choose again');
646}
647
0b5a80a1 648
649/**
650 * Implementation of the function for printing the form elements that control
651 * whether the course reset functionality affects the choice.
1adbd2c3 652 *
4636bf83 653 * @param object $mform form passed by reference
0b5a80a1 654 */
655function choice_reset_course_form_definition(&$mform) {
656 $mform->addElement('header', 'choiceheader', get_string('modulenameplural', 'choice'));
657 $mform->addElement('advcheckbox', 'reset_choice', get_string('removeresponses','choice'));
658}
659
660/**
661 * Course reset form defaults.
4636bf83 662 *
663 * @return array
0b5a80a1 664 */
665function choice_reset_course_form_defaults($course) {
666 return array('reset_choice'=>1);
667}
668
669/**
72d2982e 670 * Actual implementation of the reset course functionality, delete all the
0b5a80a1 671 * choice responses for course $data->courseid.
4636bf83 672 *
673 * @global object
674 * @global object
675 * @param object $data the data submitted from the reset course.
0b5a80a1 676 * @return array status array
677 */
678function choice_reset_userdata($data) {
407caeeb 679 global $CFG, $DB;
0b5a80a1 680
681 $componentstr = get_string('modulenameplural', 'choice');
682 $status = array();
683
684 if (!empty($data->reset_choice)) {
685 $choicessql = "SELECT ch.id
f7e93000
RW
686 FROM {choice} ch
687 WHERE ch.course=?";
0b5a80a1 688
407caeeb 689 $DB->delete_records_select('choice_answers', "choiceid IN ($choicessql)", array($data->courseid));
0b5a80a1 690 $status[] = array('component'=>$componentstr, 'item'=>get_string('removeresponses', 'choice'), 'error'=>false);
691 }
692
693 /// updating dates - shift may be negative too
694 if ($data->timeshift) {
695 shift_course_mod_dates('choice', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
696 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
697 }
698
699 return $status;
700}
701
4636bf83 702/**
703 * @global object
704 * @global object
705 * @global object
706 * @uses CONTEXT_MODULE
707 * @param object $choice
708 * @param object $cm
709 * @param int $groupmode
710 * @return array
711 */
a61e4188 712function choice_get_response_data($choice, $cm, $groupmode) {
407caeeb 713 global $CFG, $USER, $DB;
a61e4188 714
715 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
716
717/// Get the current group
718 if ($groupmode > 0) {
719 $currentgroup = groups_get_activity_group($cm);
720 } else {
721 $currentgroup = 0;
722 }
723
5d9d5227 724/// Initialise the returned array, which is a matrix: $allresponses[responseid][userid] = responseobject
725 $allresponses = array();
a61e4188 726
727/// First get all the users who have access here
728/// To start with we assume they are all "unanswered" then move them later
3a11c09f 729 $allresponses[0] = get_enrolled_users($context, 'mod/choice:choose', $currentgroup, user_picture::fields('u', array('idnumber')), 'u.lastname ASC,u.firstname ASC');
a61e4188 730
731/// Get all the recorded responses for this choice
407caeeb 732 $rawresponses = $DB->get_records('choice_answers', array('choiceid' => $choice->id));
a61e4188 733
734/// Use the responses to move users into the correct column
735
5d9d5227 736 if ($rawresponses) {
737 foreach ($rawresponses as $response) {
738 if (isset($allresponses[0][$response->userid])) { // This person is enrolled and in correct group
739 $allresponses[0][$response->userid]->timemodified = $response->timemodified;
740 $allresponses[$response->optionid][$response->userid] = clone($allresponses[0][$response->userid]);
741 unset($allresponses[0][$response->userid]); // Remove from unanswered column
a61e4188 742 }
743 }
f7e93000 744 }
5d9d5227 745 return $allresponses;
f432bebf 746}
a61e4188 747
f432bebf 748/**
749 * Returns all other caps used in module
4636bf83 750 *
751 * @return array
f432bebf 752 */
db19ec01 753function choice_get_extra_capabilities() {
f432bebf 754 return array('moodle/site:accessallgroups');
a61e4188 755}
f432bebf 756
18a2a0cb 757/**
4636bf83 758 * @uses FEATURE_GROUPS
759 * @uses FEATURE_GROUPINGS
760 * @uses FEATURE_GROUPMEMBERSONLY
761 * @uses FEATURE_MOD_INTRO
762 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
763 * @uses FEATURE_GRADE_HAS_GRADE
764 * @uses FEATURE_GRADE_OUTCOMES
18a2a0cb 765 * @param string $feature FEATURE_xx constant for requested feature
766 * @return mixed True if module supports feature, null if doesn't know
767 */
768function choice_supports($feature) {
769 switch($feature) {
42f103be 770 case FEATURE_GROUPS: return true;
771 case FEATURE_GROUPINGS: return true;
772 case FEATURE_GROUPMEMBERSONLY: return true;
dc5c2bd9 773 case FEATURE_MOD_INTRO: return true;
18a2a0cb 774 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
42f103be 775 case FEATURE_GRADE_HAS_GRADE: return false;
776 case FEATURE_GRADE_OUTCOMES: return false;
98baf0d7 777 case FEATURE_BACKUP_MOODLE2: return true;
42f103be 778
18a2a0cb 779 default: return null;
780 }
781}
97011c64 782
0b29477b
SH
783/**
784 * Adds module specific settings to the settings block
785 *
786 * @param settings_navigation $settings The settings navigation object
787 * @param navigation_node $choicenode The node to add module settings to
788 */
789function choice_extend_settings_navigation(settings_navigation $settings, navigation_node $choicenode) {
790 global $PAGE;
97011c64 791
792 if (has_capability('mod/choice:readresponses', $PAGE->cm->context)) {
793
794 $groupmode = groups_get_activity_groupmode($PAGE->cm);
795 if ($groupmode) {
796 groups_get_activity_group($PAGE->cm, true);
797 }
56115eea
SH
798 // We only actually need the choice id here
799 $choice = new stdClass;
800 $choice->id = $PAGE->cm->instance;
97011c64 801 $allresponses = choice_get_response_data($choice, $PAGE->cm, $groupmode); // Big function, approx 6 SQL calls per user
802
803 $responsecount =0;
804 foreach($allresponses as $optionid => $userlist) {
805 if ($optionid) {
806 $responsecount += count($userlist);
807 }
808 }
0b29477b 809 $choicenode->add(get_string("viewallresponses", "choice", $responsecount), new moodle_url('/mod/choice/report.php', array('id'=>$PAGE->cm->id)));
97011c64 810 }
98baf0d7 811}