MDL-15007 is_aggregationcoef_used should be public
[moodle.git] / mod / choice / lib.php
CommitLineData
1c61c8d6 1<?php // $Id$
0d22e7cc 2
6d6c9389 3$COLUMN_HEIGHT = 300;
4
8f7dc7f1 5define('CHOICE_PUBLISH_ANONYMOUS', '0');
6define('CHOICE_PUBLISH_NAMES', '1');
6d6c9389 7
c100a341 8define('CHOICE_SHOWRESULTS_NOT', '0');
9define('CHOICE_SHOWRESULTS_AFTER_ANSWER', '1');
10define('CHOICE_SHOWRESULTS_AFTER_CLOSE', '2');
11define('CHOICE_SHOWRESULTS_ALWAYS', '3');
f9af7fd0 12
6fd87e3b 13define('CHOICE_DISPLAY_HORIZONTAL', '0');
14define('CHOICE_DISPLAY_VERTICAL', '1');
15
8f7dc7f1 16$CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS => get_string('publishanonymous', 'choice'),
17 CHOICE_PUBLISH_NAMES => get_string('publishnames', 'choice'));
6d6c9389 18
c100a341 19$CHOICE_SHOWRESULTS = array (CHOICE_SHOWRESULTS_NOT => get_string('publishnot', 'choice'),
20 CHOICE_SHOWRESULTS_AFTER_ANSWER => get_string('publishafteranswer', 'choice'),
21 CHOICE_SHOWRESULTS_AFTER_CLOSE => get_string('publishafterclose', 'choice'),
22 CHOICE_SHOWRESULTS_ALWAYS => get_string('publishalways', 'choice'));
f9af7fd0 23
6fd87e3b 24$CHOICE_DISPLAY = array (CHOICE_DISPLAY_HORIZONTAL => get_string('displayhorizontal', 'choice'),
25 CHOICE_DISPLAY_VERTICAL => get_string('displayvertical','choice'));
6d6c9389 26
27/// Standard functions /////////////////////////////////////////////////////////
28
0d22e7cc 29function choice_user_outline($course, $user, $mod, $choice) {
d1a67362 30 if ($answer = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id)) {
7cfc54b9 31 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
d1a67362 32 $result->time = $answer->timemodified;
0d22e7cc 33 return $result;
34 }
35 return NULL;
36}
37
38
39function choice_user_complete($course, $user, $mod, $choice) {
d1a67362 40 if ($answer = get_record('choice_answers', "choiceid", $choice->id, "userid", $user->id)) {
7cfc54b9 41 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
d1a67362 42 $result->time = $answer->timemodified;
7cfc54b9 43 echo get_string("answered", "choice").": $result->info. ".get_string("updated", '', userdate($result->time));
0d22e7cc 44 } else {
c4016bc1 45 print_string("notanswered", "choice");
0d22e7cc 46 }
47}
48
04eba58f 49
50function choice_add_instance($choice) {
ebd3c7ac 51// Given an object containing all the necessary data,
52// (defined by the form in mod.html) this function
53// will create a new instance and return the id number
04eba58f 54// of the new instance.
55
56 $choice->timemodified = time();
57
ebd3c7ac 58 if (empty($choice->timerestrict)) {
d30772b2 59 $choice->timeopen = 0;
60 $choice->timeclose = 0;
61 }
62
ff8d654f 63 //insert answers
a77a6009 64 if ($choice->id = insert_record("choice", $choice)) {
ebd3c7ac 65 foreach ($choice->option as $key => $value) {
66 $value = trim($value);
67 if (isset($value) && $value <> '') {
68 $option = new object();
69 $option->text = $value;
70 $option->choiceid = $choice->id;
71 if (isset($choice->limit[$key])) {
72 $option->maxanswers = $choice->limit[$key];
a77a6009 73 }
ebd3c7ac 74 $option->timemodified = time();
75 insert_record("choice_options", $option);
6fd87e3b 76 }
77 }
78 }
a77a6009 79 return $choice->id;
04eba58f 80}
81
82
83function choice_update_instance($choice) {
ebd3c7ac 84// Given an object containing all the necessary data,
85// (defined by the form in mod.html) this function
04eba58f 86// will update an existing instance with new data.
87
88 $choice->id = $choice->instance;
89 $choice->timemodified = time();
90
d30772b2 91
ebd3c7ac 92 if (empty($choice->timerestrict)) {
d30772b2 93 $choice->timeopen = 0;
94 $choice->timeclose = 0;
95 }
ff8d654f 96
ebd3c7ac 97 //update, delete or insert answers
98 foreach ($choice->option as $key => $value) {
a77a6009 99 $value = trim($value);
ebd3c7ac 100 $option = new object();
101 $option->text = $value;
102 $option->choiceid = $choice->id;
103 if (isset($choice->limit[$key])) {
104 $option->maxanswers = $choice->limit[$key];
105 }
106 $option->timemodified = time();
a2e67a9a 107 if (isset($choice->optionid[$key]) && !empty($choice->optionid[$key])){//existing choice record
ebd3c7ac 108 $option->id=$choice->optionid[$key];
af834f3e 109 if (isset($value) && $value <> '') {
c9ff813c 110 update_record("choice_options", $option);
b9135eca 111 } else { //empty old option - needs to be deleted.
ebd3c7ac 112 delete_records("choice_options", "id", $option->id);
c9ff813c 113 }
ebd3c7ac 114 } else {
115 if (isset($value) && $value <> '') {
ff8d654f 116 insert_record("choice_options", $option);
a77a6009 117 }
ff8d654f 118 }
6fd87e3b 119 }
d30772b2 120
a77a6009 121 return update_record('choice', $choice);
ff8d654f 122
04eba58f 123}
124
a61e4188 125function choice_show_form($choice, $user, $cm, $allresponses) {
ebd3c7ac 126
348630d8 127//$cdisplay is an array of the display info for a choice $cdisplay[$optionid]->text - text name of option.
128// ->maxanswers -maxanswers for this option
129// ->full - whether this option is full or not. 0=not full, 1=full
39b3e7de 130 $cdisplay = array();
348630d8 131
132 $aid = 0;
3c56a7cc 133 $choicefull = false;
a61e4188 134 $cdisplay = array();
3c56a7cc 135
136 if ($choice->limitanswers) { //set choicefull to true by default if limitanswers.
137 $choicefull = true;
138 }
a61e4188 139
3c56a7cc 140 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
141
348630d8 142 foreach ($choice->option as $optionid => $text) {
a5f35b9f 143 if (isset($text)) { //make sure there are no dud entries in the db with blank text values.
dabfd0ed 144 $cdisplay[$aid]->optionid = $optionid;
145 $cdisplay[$aid]->text = $text;
a61e4188 146 $cdisplay[$aid]->maxanswers = $choice->maxanswers[$optionid];
55963a3a 147 if (isset($allresponses[$optionid])) {
148 $cdisplay[$aid]->countanswers = count($allresponses[$optionid]);
149 } else {
150 $cdisplay[$aid]->countanswers = 0;
151 }
dabfd0ed 152 if ($current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id, 'optionid', $optionid)) {
ff8d654f 153 $cdisplay[$aid]->checked = ' checked="checked" ';
dabfd0ed 154 } else {
ff8d654f 155 $cdisplay[$aid]->checked = '';
dabfd0ed 156 }
a61e4188 157 if ( $choice->limitanswers &&
158 ($cdisplay[$aid]->countanswers >= $cdisplay[$aid]->maxanswers) &&
159 (empty($cdisplay[$aid]->checked)) ) {
ff8d654f 160 $cdisplay[$aid]->disabled = ' disabled="disabled" ';
dabfd0ed 161 } else {
ebd3c7ac 162 $cdisplay[$aid]->disabled = '';
a61e4188 163 if ($choice->limitanswers && ($cdisplay[$aid]->countanswers < $cdisplay[$aid]->maxanswers)) {
3c56a7cc 164 $choicefull = false; //set $choicefull to false - as the above condition hasn't been set.
165 }
dabfd0ed 166 }
348630d8 167 $aid++;
168 }
169 }
170
39b3e7de 171 switch ($choice->display) {
172 case CHOICE_DISPLAY_HORIZONTAL:
d71bc1ab 173 echo "<table cellpadding=\"20\" cellspacing=\"20\" class=\"boxaligncenter\"><tr>";
ebd3c7ac 174
175 foreach ($cdisplay as $cd) {
39b3e7de 176 echo "<td align=\"center\" valign=\"top\">";
ff8d654f 177 echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";
ebd3c7ac 178 if (!empty($cd->disabled)) {
ff8d654f 179 echo format_text($cd->text."<br /><strong>".get_string('full', 'choice')."</strong>");
39b3e7de 180 } else {
181 echo format_text($cd->text);
348630d8 182 }
ff8d654f 183 echo "</td>";
39b3e7de 184 }
185 echo "</tr>";
186 echo "</table>";
187 break;
348630d8 188
39b3e7de 189 case CHOICE_DISPLAY_VERTICAL:
190 $displayoptions->para = false;
d71bc1ab 191 echo "<table cellpadding=\"10\" cellspacing=\"10\" class=\"boxaligncenter\">";
ff8d654f 192 foreach ($cdisplay as $cd) {
39b3e7de 193 echo "<tr><td align=\"left\">";
ff8d654f 194 echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";
348630d8 195
39b3e7de 196 echo format_text($cd->text. ' ', FORMAT_MOODLE, $displayoptions); //display text for option.
ff8d654f 197
39b3e7de 198 if ($choice->limitanswers && ($choice->showresults==CHOICE_SHOWRESULTS_ALWAYS) ){ //if limit is enabled, and show results always has been selected, display info beside each choice.
199 echo "</td><td>";
ff8d654f 200
39b3e7de 201 if (!empty($cd->disabled)) {
202 echo get_string('full', 'choice');
203 } elseif(!empty($cd->checked)) {
dabfd0ed 204 //currently do nothing - maybe some text could be added here to signfy that the choice has been 'selected'
39b3e7de 205 } elseif ($cd->maxanswers-$cd->countanswers==1) {
206 echo ($cd->maxanswers - $cd->countanswers);
207 echo " ".get_string('spaceleft', 'choice');
208 } else {
209 echo ($cd->maxanswers - $cd->countanswers);
210 echo " ".get_string('spacesleft', 'choice');
211 }
212 echo "</td>";
213 } else if ($choice->limitanswers && ($cd->countanswers >= $cd->maxanswers)) { //if limitanswers and answers exceeded, display "full" beside the choice.
214 echo " <strong>".get_string('full', 'choice')."</strong>";
348630d8 215 }
39b3e7de 216 echo "</td>";
217 echo "</tr>";
218 }
219 echo "</table>";
220 break;
221 }
222 //show save choice button
d71bc1ab 223 echo '<div class="button">';
39b3e7de 224 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
3c56a7cc 225 if (has_capability('mod/choice:choose', $context, $user->id, false)) { //don't show save button if the logged in user is the guest user.
226 if ($choicefull) {
227 print_string('choicefull', 'choice');
228 echo "</br>";
229 } else {
230 echo "<input type=\"submit\" value=\"".get_string("savemychoice","choice")."\" />";
231 }
243ac1fe 232 if ($choice->allowupdate && $aaa = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id)) {
41daad76 233 echo "<br /><a href='view.php?id=".$cm->id."&amp;action=delchoice'>".get_string("removemychoice","choice")."</a>";
243ac1fe 234 }
39b3e7de 235 } else {
236 print_string('havetologin', 'choice');
237 }
d71bc1ab 238 echo "</div>";
348630d8 239}
240
acaf7b86 241function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $cm) {
348630d8 242
39b3e7de 243 $current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $userid);
dabfd0ed 244 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
245 $countanswers = get_records("choice_answers", "optionid", $formanswer);
39b3e7de 246 if ($countanswers) {
247 $countans = 0;
248 foreach ($countanswers as $ca) { //only return enrolled users.
770778a5 249 if (has_capability('mod/choice:choose', $context, $ca->userid, false)) {
39b3e7de 250 $countans = $countans+1;
251 }
ff8d654f 252 }
253
254 $countanswers = $countans;
39b3e7de 255 } else {
256 $countanswers = 0;
257 }
258 $maxans = $choice->maxanswers[$formanswer];
ff8d654f 259
260 if (!($choice->limitanswers && ($countanswers >= $maxans) )) {
39b3e7de 261 if ($current) {
ebd3c7ac 262
39b3e7de 263 $newanswer = $current;
264 $newanswer->optionid = $formanswer;
265 $newanswer->timemodified = time();
266 if (! update_record("choice_answers", $newanswer)) {
c7281355 267 print_error('cannotupdatechoice', 'choice');
348630d8 268 }
39b3e7de 269 add_to_log($courseid, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id);
270 } else {
271 $newanswer = NULL;
272 $newanswer->choiceid = $choice->id;
273 $newanswer->userid = $userid;
274 $newanswer->optionid = $formanswer;
275 $newanswer->timemodified = time();
276 if (! insert_record("choice_answers", $newanswer)) {
c7281355 277 print_error('cannotsavechoice', 'choice');
39b3e7de 278 }
279 add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id);
280 }
281 } else {
282 if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error
c7281355 283 print_error('choicefull', 'choice');
39b3e7de 284 }
285 }
348630d8 286}
287
a61e4188 288function choice_show_reportlink($user, $cm) {
289 $responsecount =0;
290 foreach($user as $optionid => $userlist) {
291 if ($optionid) {
292 $responsecount += count($userlist);
293 }
7b360f33 294 }
5c880488 295
39b3e7de 296 echo '<div class="reportlink">';
5c880488 297 echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
39b3e7de 298 echo '</div>';
348630d8 299}
300
5d9d5227 301function choice_show_results($choice, $course, $cm, $allresponses, $forcepublish='') {
a61e4188 302 global $CFG, $COLUMN_HEIGHT;
303
ff8d654f 304 print_heading(get_string("responses", "choice"));
39b3e7de 305 if (empty($forcepublish)) { //alow the publish setting to be overridden
306 $forcepublish = $choice->publish;
ff8d654f 307 }
308
5d9d5227 309 if (!$allresponses) {
39b3e7de 310 print_heading(get_string("nousersyet"));
311 }
c2a4d016 312
313 $totalresponsecount = 0;
5d9d5227 314 foreach ($allresponses as $optionid => $userlist) {
c2a4d016 315 if ($choice->showunanswered || $optionid) {
316 $totalresponsecount += count($userlist);
317 }
318 }
a61e4188 319
320 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
348630d8 321
a61e4188 322 $hascapfullnames = has_capability('moodle/site:viewfullnames', $context);
323
324 $viewresponses = has_capability('mod/choice:readresponses', $context);
39b3e7de 325 switch ($forcepublish) {
326 case CHOICE_PUBLISH_NAMES:
dabfd0ed 327 echo '<div id="tablecontainer">';
348630d8 328 echo '<form id="attemptsform" method="post" action="'.$_SERVER['PHP_SELF'].'" onsubmit="var menu = document.getElementById(\'menuaction\'); return (menu.options[menu.selectedIndex].value == \'delete\' ? \''.addslashes(get_string('deleteattemptcheck','quiz')).'\' : true);">';
95aa949b 329 echo '<div>';
348630d8 330 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
331 echo '<input type="hidden" name="mode" value="overview" />';
ff8d654f 332
d71bc1ab 333 echo "<table cellpadding=\"5\" cellspacing=\"10\" class=\"results names\">";
348630d8 334 echo "<tr>";
a61e4188 335
aa4836f4 336 $columncount = array(); // number of votes in each column
a61e4188 337 if ($choice->showunanswered) {
338 $columncount[0] = 0;
339 echo "<th class=\"col0 header\" scope=\"col\">";
340 print_string('notanswered', 'choice');
341 echo "</th>";
342 }
343 $count = 1;
344 foreach ($choice->option as $optionid => $optiontext) {
aa4836f4 345 $columncount[$optionid] = 0; // init counters
a61e4188 346 echo "<th class=\"col$count header\" scope=\"col\">";
347 echo format_string($optiontext);
348630d8 348 echo "</th>";
349 $count++;
350 }
351 echo "</tr><tr>";
352
a61e4188 353 if ($choice->showunanswered) {
354 echo "<td class=\"col$count data\" >";
95aa949b 355 // added empty row so that when the next iteration is empty,
356 // we do not get <table></table> erro from w3c validator
357 // MDL-7861
358 echo "<table class=\"choiceresponse\"><tr><td></td></tr>";
5d9d5227 359 foreach ($allresponses[0] as $user) {
a61e4188 360 echo "<tr>";
361 echo "<td class=\"picture\">";
362 print_user_picture($user->id, $course->id, $user->picture);
363 echo "</td><td class=\"fullname\">";
364 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
365 echo fullname($user, $hascapfullnames);
366 echo "</a>";
367 echo "</td></tr>";
348630d8 368 }
a61e4188 369 echo "</table></td>";
370 }
371 $count = 0;
372 foreach ($choice->option as $optionid => $optiontext) {
373 echo '<td class="col'.$count.' data" >';
374
375 // added empty row so that when the next iteration is empty,
376 // we do not get <table></table> erro from w3c validator
377 // MDL-7861
378 echo '<table class="choiceresponse"><tr><td></td></tr>';
5d9d5227 379 if (isset($allresponses[$optionid])) {
380 foreach ($allresponses[$optionid] as $user) {
a61e4188 381 $columncount[$optionid] += 1;
382 echo '<tr><td class="attemptcell">';
cb4db90d 383 if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
a61e4188 384 echo '<input type="checkbox" name="attemptid[]" value="'. $user->id. '" />';
385 }
386 echo '</td><td class="picture">';
387 print_user_picture($user->id, $course->id, $user->picture);
388 echo '</td><td class="fullname">';
389 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
390 echo fullname($user, $hascapfullnames);
391 echo '</a>';
392 echo '</td></tr>';
393 }
394 }
395 $count++;
396 echo '</table></td>';
348630d8 397 }
398 echo "</tr><tr>";
399 $count = 0;
a61e4188 400
401 if ($choice->showunanswered) {
402 echo "<td></td>";
403 }
404
405 foreach ($choice->option as $optionid => $optiontext) {
348630d8 406 echo "<td align=\"center\" class=\"count\">";
a61e4188 407 if ($choice->limitanswers) {
348630d8 408 echo get_string("taken", "choice").":";
aa4836f4 409 echo $columncount[$optionid];
71502268 410 echo "<br/>";
348630d8 411 echo get_string("limit", "choice").":";
412 $choice_option = get_record("choice_options", "id", $optionid);
413 echo $choice_option->maxanswers;
19e559e4 414 } else {
a61e4188 415 if (isset($columncount[$optionid])) {
416 echo $columncount[$optionid];
417 }
348630d8 418 }
419 echo "</td>";
420 $count++;
421 }
3402a6f3 422 echo "</tr>";
a61e4188 423
39b3e7de 424 /// Print "Select all" etc.
cb4db90d 425 if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
3402a6f3 426 echo '<tr><td></td><td>';
39b3e7de 427 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
428 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
429 echo '&nbsp;&nbsp;';
430 $options = array('delete' => get_string('delete'));
431 echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
432 echo '<noscript id="noscriptmenuaction" style="display: inline;">';
95aa949b 433 echo '<div>';
434 echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
39b3e7de 435 echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
3402a6f3 436 echo '</td><td></td></tr>';
39b3e7de 437 }
a61e4188 438
3402a6f3 439 echo "</table>";
a61e4188 440 if ($viewresponses) {
95aa949b 441 echo "</div></form></div>";
dabfd0ed 442 }
348630d8 443 break;
a61e4188 444
445
39b3e7de 446 case CHOICE_PUBLISH_ANONYMOUS:
ff8d654f 447
d71bc1ab 448 echo "<table cellpadding=\"5\" cellspacing=\"0\" class=\"results anonymous\">";
348630d8 449 echo "<tr>";
a61e4188 450 $maxcolumn = 0;
451 if ($choice->showunanswered) {
452 echo "<th class=\"col0 header\" scope=\"col\">";
453 print_string('notanswered', 'choice');
348630d8 454 echo "</th>";
a61e4188 455 $column[0] = 0;
5d9d5227 456 foreach ($allresponses[0] as $user) {
a61e4188 457 $column[0]++;
458 }
459 $maxcolumn = $column[0];
348630d8 460 }
a61e4188 461 $count = 1;
348630d8 462
a61e4188 463 foreach ($choice->option as $optionid => $optiontext) {
464 echo "<th class=\"col$count header\" scope=\"col\">";
465 echo format_string($optiontext);
466 echo "</th>";
467
348630d8 468 $column[$optionid] = 0;
5d9d5227 469 if (isset($allresponses[$optionid])) {
470 $column[$optionid] = count($allresponses[$optionid]);
a61e4188 471 if ($column[$optionid] > $maxcolumn) {
472 $maxcolumn = $column[$optionid];
473 }
474 } else {
475 $column[$optionid] = 0;
348630d8 476 }
477 }
a61e4188 478 echo "</tr><tr>";
348630d8 479
a61e4188 480 $height = 0;
481
482 if ($choice->showunanswered) {
483 if ($maxcolumn) {
484 $height = $COLUMN_HEIGHT * ((float)$column[0] / (float)$maxcolumn);
348630d8 485 }
a61e4188 486 echo "<td style=\"vertical-align:bottom\" align=\"center\" class=\"col0 data\">";
487 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
488 echo "</td>";
489 }
490 $count = 1;
491 foreach ($choice->option as $optionid => $optiontext) {
348630d8 492 if ($maxcolumn) {
493 $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
494 }
41daad76 495 echo "<td style=\"vertical-align:bottom\" align=\"center\" class=\"col$count data\">";
348630d8 496 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
497 echo "</td>";
498 $count++;
499 }
a61e4188 500 echo "</tr><tr>";
348630d8 501
a61e4188 502
503 if ($choice->showunanswered) {
c2a4d016 504 echo '<td align="center" class="col0 count">';
505 if (!$choice->limitanswers) {
506 echo $column[0];
507 echo '<br />('.format_float(((float)$column[0]/(float)$totalresponsecount)*100.0,1).'%)';
508 }
509 echo '</td>';
a61e4188 510 }
511 $count = 1;
512 foreach ($choice->option as $optionid => $optiontext) {
348630d8 513 echo "<td align=\"center\" class=\"col$count count\">";
a61e4188 514 if ($choice->limitanswers) {
348630d8 515 echo get_string("taken", "choice").":";
c2a4d016 516 echo $column[$optionid].'<br />';
348630d8 517 echo get_string("limit", "choice").":";
518 $choice_option = get_record("choice_options", "id", $optionid);
519 echo $choice_option->maxanswers;
520 } else {
521 echo $column[$optionid];
c2a4d016 522 echo '<br />('.format_float(((float)$column[$optionid]/(float)$totalresponsecount)*100.0,1).'%)';
348630d8 523 }
524 echo "</td>";
525 $count++;
526 }
527 echo "</tr></table>";
a61e4188 528
529 break;
ebd3c7ac 530 }
348630d8 531}
532
533
a61e4188 534function choice_delete_responses($attemptids, $choiceid) {
ebd3c7ac 535
dabfd0ed 536 if(!is_array($attemptids) || empty($attemptids)) {
348630d8 537 return false;
538 }
539
540 foreach($attemptids as $num => $attemptid) {
541 if(empty($attemptid)) {
542 unset($attemptids[$num]);
543 }
544 }
545
546 foreach($attemptids as $attemptid) {
a61e4188 547 if ($todelete = get_record('choice_answers', 'choiceid', $choiceid, 'userid', $attemptid)) {
548 delete_records('choice_answers', 'choiceid', $choiceid, 'userid', $attemptid);
348630d8 549 }
ebd3c7ac 550 }
348630d8 551 return true;
552}
553
04eba58f 554
555function choice_delete_instance($id) {
ebd3c7ac 556// Given an ID of an instance of this module,
557// this function will permanently delete the instance
558// and any data that depends on it.
04eba58f 559
560 if (! $choice = get_record("choice", "id", "$id")) {
561 return false;
562 }
563
564 $result = true;
565
a77a6009 566 if (! delete_records("choice_answers", "choiceid", "$choice->id")) {
04eba58f 567 $result = false;
568 }
569
a77a6009 570 if (! delete_records("choice_options", "choiceid", "$choice->id")) {
04eba58f 571 $result = false;
572 }
a77a6009 573
574 if (! delete_records("choice", "id", "$choice->id")) {
6fd87e3b 575 $result = false;
ebd3c7ac 576 }
04eba58f 577
578 return $result;
579}
580
05855091 581function choice_get_participants($choiceid) {
582//Returns the users with data in one choice
6fd87e3b 583//(users with records in choice_responses, students)
05855091 584
585 global $CFG;
586
587 //Get students
95e72c12 588 $students = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 589 FROM {$CFG->prefix}user u,
a77a6009 590 {$CFG->prefix}choice_answers a
591 WHERE a.choiceid = '$choiceid' and
592 u.id = a.userid");
05855091 593
594 //Return students array (it contains an array of unique users)
595 return ($students);
596}
597
04eba58f 598
a77a6009 599function choice_get_option_text($choice, $id) {
600// Returns text string which is the answer that matches the id
601 if ($result = get_record("choice_options", "id", $id)) {
602 return $result->text;
6fd87e3b 603 } else {
604 return get_string("notanswered", "choice");
ff8d654f 605 }
c4016bc1 606}
607
cd3fccff 608function choice_get_choice($choiceid) {
ff8d654f 609// Gets a full choice record
a77a6009 610
611 if ($choice = get_record("choice", "id", $choiceid)) {
612 if ($options = get_records("choice_options", "choiceid", $choiceid, "id")) {
ff8d654f 613 foreach ($options as $option) {
ebd3c7ac 614 $choice->option[$option->id] = $option->text;
c9ff813c 615 $choice->maxanswers[$option->id] = $option->maxanswers;
ff8d654f 616 }
a77a6009 617 return $choice;
618 }
619 }
620 return false;
cd3fccff 621}
622
f3221af9 623function choice_get_view_actions() {
624 return array('view','view all','report');
625}
626
627function choice_get_post_actions() {
628 return array('choose','choose again');
629}
630
0b5a80a1 631
632/**
633 * Implementation of the function for printing the form elements that control
634 * whether the course reset functionality affects the choice.
635 * @param $mform form passed by reference
636 */
637function choice_reset_course_form_definition(&$mform) {
638 $mform->addElement('header', 'choiceheader', get_string('modulenameplural', 'choice'));
639 $mform->addElement('advcheckbox', 'reset_choice', get_string('removeresponses','choice'));
640}
641
642/**
643 * Course reset form defaults.
644 */
645function choice_reset_course_form_defaults($course) {
646 return array('reset_choice'=>1);
647}
648
649/**
650 * Actual implementation of the rest coures functionality, delete all the
651 * choice responses for course $data->courseid.
652 * @param $data the data submitted from the reset course.
653 * @return array status array
654 */
655function choice_reset_userdata($data) {
656 global $CFG;
657
658 $componentstr = get_string('modulenameplural', 'choice');
659 $status = array();
660
661 if (!empty($data->reset_choice)) {
662 $choicessql = "SELECT ch.id
663 FROM {$CFG->prefix}choice ch
664 WHERE ch.course={$data->courseid}";
665
666 delete_records_select('choice_answers', "choiceid IN ($choicessql)");
667 $status[] = array('component'=>$componentstr, 'item'=>get_string('removeresponses', 'choice'), 'error'=>false);
668 }
669
670 /// updating dates - shift may be negative too
671 if ($data->timeshift) {
672 shift_course_mod_dates('choice', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
673 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
674 }
675
676 return $status;
677}
678
a61e4188 679function choice_get_response_data($choice, $cm, $groupmode) {
680 global $CFG, $USER;
681
682 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
683
684/// Get the current group
685 if ($groupmode > 0) {
686 $currentgroup = groups_get_activity_group($cm);
687 } else {
688 $currentgroup = 0;
689 }
690
5d9d5227 691/// Initialise the returned array, which is a matrix: $allresponses[responseid][userid] = responseobject
692 $allresponses = array();
a61e4188 693
694/// First get all the users who have access here
695/// To start with we assume they are all "unanswered" then move them later
5d9d5227 696 $allresponses[0] = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC', '', '', $currentgroup, '', false, true);
a61e4188 697
698/// Get all the recorded responses for this choice
5d9d5227 699 $rawresponses = get_records('choice_answers', 'choiceid', $choice->id);
a61e4188 700
701/// Use the responses to move users into the correct column
702
5d9d5227 703 if ($rawresponses) {
704 foreach ($rawresponses as $response) {
705 if (isset($allresponses[0][$response->userid])) { // This person is enrolled and in correct group
706 $allresponses[0][$response->userid]->timemodified = $response->timemodified;
707 $allresponses[$response->optionid][$response->userid] = clone($allresponses[0][$response->userid]);
708 unset($allresponses[0][$response->userid]); // Remove from unanswered column
a61e4188 709 }
710 }
711 }
712
5d9d5227 713 return $allresponses;
a61e4188 714
715}
5a9726e7 716?>