MDL-14679 fixed references to mod.html
[moodle.git] / mod / choice / lib.php
1 <?php // $Id$
3 $COLUMN_HEIGHT = 300;
5 define('CHOICE_PUBLISH_ANONYMOUS', '0');
6 define('CHOICE_PUBLISH_NAMES',     '1');
8 define('CHOICE_SHOWRESULTS_NOT',          '0');
9 define('CHOICE_SHOWRESULTS_AFTER_ANSWER', '1');
10 define('CHOICE_SHOWRESULTS_AFTER_CLOSE',  '2');
11 define('CHOICE_SHOWRESULTS_ALWAYS',       '3');
13 define('CHOICE_DISPLAY_HORIZONTAL',  '0');
14 define('CHOICE_DISPLAY_VERTICAL',    '1');
16 $CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS  => get_string('publishanonymous', 'choice'),
17                          CHOICE_PUBLISH_NAMES      => get_string('publishnames', 'choice'));
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'));
24 $CHOICE_DISPLAY = array (CHOICE_DISPLAY_HORIZONTAL   => get_string('displayhorizontal', 'choice'),
25                          CHOICE_DISPLAY_VERTICAL     => get_string('displayvertical','choice'));
27 /// Standard functions /////////////////////////////////////////////////////////
29 function choice_user_outline($course, $user, $mod, $choice) {
30     if ($answer = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id)) {
31         $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
32         $result->time = $answer->timemodified;
33         return $result;
34     }
35     return NULL;
36 }
39 function choice_user_complete($course, $user, $mod, $choice) {
40     if ($answer = get_record('choice_answers', "choiceid", $choice->id, "userid", $user->id)) {
41         $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
42         $result->time = $answer->timemodified;
43         echo get_string("answered", "choice").": $result->info. ".get_string("updated", '', userdate($result->time));
44     } else {
45         print_string("notanswered", "choice");
46     }
47 }
50 function choice_add_instance($choice) {
51 // Given an object containing all the necessary data,
52 // (defined by the form in mod_form.php) this function
53 // will create a new instance and return the id number
54 // of the new instance.
56     $choice->timemodified = time();
58     if (empty($choice->timerestrict)) {
59         $choice->timeopen = 0;
60         $choice->timeclose = 0;
61     }
63     //insert answers
64     if ($choice->id = insert_record("choice", $choice)) {
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];
73                 }
74                 $option->timemodified = time();
75                 insert_record("choice_options", $option);
76             }
77         }
78     }
79     return $choice->id;
80 }
83 function choice_update_instance($choice) {
84 // Given an object containing all the necessary data,
85 // (defined by the form in mod_form.php) this function
86 // will update an existing instance with new data.
88     $choice->id = $choice->instance;
89     $choice->timemodified = time();
92     if (empty($choice->timerestrict)) {
93         $choice->timeopen = 0;
94         $choice->timeclose = 0;
95     }
97     //update, delete or insert answers
98     foreach ($choice->option as $key => $value) {
99         $value = trim($value);
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();
107         if (isset($choice->optionid[$key]) && !empty($choice->optionid[$key])){//existing choice record
108             $option->id=$choice->optionid[$key];
109             if (isset($value) && $value <> '') {
110                 update_record("choice_options", $option);
111             } else { //empty old option - needs to be deleted.
112                 delete_records("choice_options", "id", $option->id);
113             }
114         } else {
115             if (isset($value) && $value <> '') {
116                 insert_record("choice_options", $option);
117             }
118         }
119     }
121     return update_record('choice', $choice);
125 function choice_show_form($choice, $user, $cm, $allresponses) {
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
130     $cdisplay = array();
132     $aid = 0;
133     $choicefull = false;
134     $cdisplay = array();
136     if ($choice->limitanswers) { //set choicefull to true by default if limitanswers.
137         $choicefull = true;
138     }
140     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
142     foreach ($choice->option as $optionid => $text) {
143         if (isset($text)) { //make sure there are no dud entries in the db with blank text values.
144             $cdisplay[$aid]->optionid = $optionid;
145             $cdisplay[$aid]->text = $text;
146             $cdisplay[$aid]->maxanswers = $choice->maxanswers[$optionid];
147             if (isset($allresponses[$optionid])) {
148                 $cdisplay[$aid]->countanswers = count($allresponses[$optionid]);
149             } else {
150                 $cdisplay[$aid]->countanswers = 0;
151             }
152             if ($current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id, 'optionid', $optionid)) {
153                 $cdisplay[$aid]->checked = ' checked="checked" ';
154             } else {
155                 $cdisplay[$aid]->checked = '';
156             }
157             if ( $choice->limitanswers && 
158                 ($cdisplay[$aid]->countanswers >= $cdisplay[$aid]->maxanswers) && 
159                 (empty($cdisplay[$aid]->checked)) ) {
160                 $cdisplay[$aid]->disabled = ' disabled="disabled" ';
161             } else {
162                 $cdisplay[$aid]->disabled = '';
163                 if ($choice->limitanswers && ($cdisplay[$aid]->countanswers < $cdisplay[$aid]->maxanswers)) {
164                     $choicefull = false; //set $choicefull to false - as the above condition hasn't been set.
165                 }
166             }
167             $aid++;
168         }
169     }
171     switch ($choice->display) {
172         case CHOICE_DISPLAY_HORIZONTAL:
173             echo "<table cellpadding=\"20\" cellspacing=\"20\" class=\"boxaligncenter\"><tr>";
175             foreach ($cdisplay as $cd) {
176                 echo "<td align=\"center\" valign=\"top\">";
177                 echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";
178                 if (!empty($cd->disabled)) {
179                     echo format_text($cd->text."<br /><strong>".get_string('full', 'choice')."</strong>");
180                 } else {
181                     echo format_text($cd->text);
182                 }
183                 echo "</td>";
184             }
185             echo "</tr>";
186             echo "</table>";
187             break;
189         case CHOICE_DISPLAY_VERTICAL:
190             $displayoptions->para = false;
191             echo "<table cellpadding=\"10\" cellspacing=\"10\" class=\"boxaligncenter\">";
192             foreach ($cdisplay as $cd) {
193                 echo "<tr><td align=\"left\">";
194                 echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";
196                 echo format_text($cd->text. ' ', FORMAT_MOODLE, $displayoptions); //display text for option.
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>";
201                     if (!empty($cd->disabled)) {
202                         echo get_string('full', 'choice');
203                     } elseif(!empty($cd->checked)) {
204                                 //currently do nothing - maybe some text could be added here to signfy that the choice has been 'selected'
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>";
215                 }
216                 echo "</td>";
217                 echo "</tr>";
218             }
219         echo "</table>";
220         break;
221     }
222     //show save choice button
223     echo '<div class="button">';
224     echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
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         }
232         if ($choice->allowupdate && $aaa = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id)) {
233             echo "<br /><a href='view.php?id=".$cm->id."&amp;action=delchoice'>".get_string("removemychoice","choice")."</a>";
234         }
235     } else {
236         print_string('havetologin', 'choice');
237     }
238     echo "</div>";
241 function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $cm) {
243     $current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $userid);
244     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
246     $countanswers=0;
247     if($choice->limitanswers) {
248         // Find out whether groups are being used and enabled
249         if (groups_get_activity_groupmode($cm) > 0) {
250             $currentgroup = groups_get_activity_group($cm);
251         } else {
252             $currentgroup = 0;
253         }
254         if($currentgroup) {
255             // If groups are being used, retrieve responses only for users in
256             // current group
257             global $CFG;
258             $answers = get_records_sql("
259 SELECT 
260     ca.*
261 FROM 
262     {$CFG->prefix}choice_answers ca
263     INNER JOIN {$CFG->prefix}groups_members gm ON ca.userid=gm.userid
264 WHERE
265     optionid=$formanswer
266     AND gm.groupid=$currentgroup");
267         } else {
268             // Groups are not used, retrieve all answers for this option ID
269             $answers = get_records("choice_answers", "optionid", $formanswer);
270         }
272         if ($answers) {
273             foreach ($answers as $a) { //only return enrolled users.
274                 if (has_capability('mod/choice:choose', $context, $a->userid, false)) {
275                     $countanswers++;
276                 }
277             }
278         }
279         $maxans = $choice->maxanswers[$formanswer];
280     }
282     if (!($choice->limitanswers && ($countanswers >= $maxans) )) {
283         if ($current) {
285             $newanswer = $current;
286             $newanswer->optionid = $formanswer;
287             $newanswer->timemodified = time();
288             if (! update_record("choice_answers", $newanswer)) {
289                 print_error('cannotupdatechoice', 'choice');
290             }
291             add_to_log($courseid, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id);
292         } else {
293             $newanswer = NULL;
294             $newanswer->choiceid = $choice->id;
295             $newanswer->userid = $userid;
296             $newanswer->optionid = $formanswer;
297             $newanswer->timemodified = time();
298             if (! insert_record("choice_answers", $newanswer)) {
299                 print_error('cannotsavechoice', 'choice');
300             }
301             add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id);
302         }
303     } else {
304         if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error
305             print_error('choicefull', 'choice');
306         }
307     }
310 function choice_show_reportlink($user, $cm) {
311     $responsecount =0;
312     foreach($user as $optionid => $userlist) {
313         if ($optionid) {
314             $responsecount += count($userlist);
315         }
316     }
318     echo '<div class="reportlink">';
319     echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
320     echo '</div>';
323 function choice_show_results($choice, $course, $cm, $allresponses, $forcepublish='') {
324     global $CFG, $COLUMN_HEIGHT;
325     
326     print_heading(get_string("responses", "choice"));
327     if (empty($forcepublish)) { //alow the publish setting to be overridden
328         $forcepublish = $choice->publish;
329     }
331     if (!$allresponses) {
332         print_heading(get_string("nousersyet"));
333     }
335     $totalresponsecount = 0;
336     foreach ($allresponses as $optionid => $userlist) {
337         if ($choice->showunanswered || $optionid) {
338             $totalresponsecount += count($userlist);
339         }
340     }
341     
342     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
344     $hascapfullnames = has_capability('moodle/site:viewfullnames', $context);
345     
346     $viewresponses = has_capability('mod/choice:readresponses', $context); 
347     switch ($forcepublish) {
348         case CHOICE_PUBLISH_NAMES:
349                 echo '<div id="tablecontainer">';
350                 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);">';
351                 echo '<div>';
352                 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
353                 echo '<input type="hidden" name="mode" value="overview" />';
355             echo "<table cellpadding=\"5\" cellspacing=\"10\" class=\"results names\">";
356             echo "<tr>";
357   
358             $columncount = array(); // number of votes in each column
359             if ($choice->showunanswered) {
360                 $columncount[0] = 0;
361                 echo "<th class=\"col0 header\" scope=\"col\">";
362                 print_string('notanswered', 'choice');
363                 echo "</th>";
364             }
365             $count = 1;
366             foreach ($choice->option as $optionid => $optiontext) {
367                 $columncount[$optionid] = 0; // init counters
368                 echo "<th class=\"col$count header\" scope=\"col\">";
369                 echo format_string($optiontext);
370                 echo "</th>";
371                 $count++;
372             }
373             echo "</tr><tr>";
375             if ($choice->showunanswered) {
376                 echo "<td class=\"col$count data\" >";
377                 // added empty row so that when the next iteration is empty,
378                 // we do not get <table></table> erro from w3c validator
379                 // MDL-7861
380                 echo "<table class=\"choiceresponse\"><tr><td></td></tr>";
381                 foreach ($allresponses[0] as $user) {
382                     echo "<tr>";
383                     echo "<td class=\"picture\">";
384                     print_user_picture($user->id, $course->id, $user->picture);
385                     echo "</td><td class=\"fullname\">";
386                     echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
387                     echo fullname($user, $hascapfullnames);
388                     echo "</a>";
389                     echo "</td></tr>";
390                 }
391                 echo "</table></td>";
392             }
393             $count = 0;
394             foreach ($choice->option as $optionid => $optiontext) {
395                     echo '<td class="col'.$count.' data" >';
397                     // added empty row so that when the next iteration is empty,
398                     // we do not get <table></table> erro from w3c validator
399                     // MDL-7861
400                     echo '<table class="choiceresponse"><tr><td></td></tr>';
401                     if (isset($allresponses[$optionid])) {
402                         foreach ($allresponses[$optionid] as $user) {
403                             $columncount[$optionid] += 1;
404                             echo '<tr><td class="attemptcell">';
405                             if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
406                                 echo '<input type="checkbox" name="attemptid[]" value="'. $user->id. '" />';
407                             }
408                             echo '</td><td class="picture">';
409                             print_user_picture($user->id, $course->id, $user->picture);
410                             echo '</td><td class="fullname">';
411                             echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
412                             echo fullname($user, $hascapfullnames);
413                             echo '</a>';
414                             echo '</td></tr>';
415                        }
416                     }
417                     $count++;
418                     echo '</table></td>';
419             }
420             echo "</tr><tr>";
421             $count = 0;
422             
423             if ($choice->showunanswered) {
424                 echo "<td></td>";
425             }
426             
427             foreach ($choice->option as $optionid => $optiontext) {
428                 echo "<td align=\"center\" class=\"count\">";
429                 if ($choice->limitanswers) {
430                     echo get_string("taken", "choice").":";
431                     echo $columncount[$optionid];
432                     echo "<br/>";
433                     echo get_string("limit", "choice").":";
434                     $choice_option = get_record("choice_options", "id", $optionid);
435                     echo $choice_option->maxanswers;
436                 } else {
437                     if (isset($columncount[$optionid])) {
438                         echo $columncount[$optionid];
439                     }
440                 }
441                 echo "</td>";
442                 $count++;
443             }
444             echo "</tr>";
445             
446             /// Print "Select all" etc.
447             if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
448                 echo '<tr><td></td><td>';
449                 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
450                 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
451                 echo '&nbsp;&nbsp;';
452                 $options = array('delete' => get_string('delete'));
453                 echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
454                 echo '<noscript id="noscriptmenuaction" style="display: inline;">';
455                 echo '<div>';
456                 echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
457                 echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
458                 echo '</td><td></td></tr>';
459             }
460             
461             echo "</table>";
462             if ($viewresponses) {
463                 echo "</div></form></div>";
464             }
465             break;
466         
467         
468         case CHOICE_PUBLISH_ANONYMOUS:
470             echo "<table cellpadding=\"5\" cellspacing=\"0\" class=\"results anonymous\">";
471             echo "<tr>";
472             $maxcolumn = 0;
473             if ($choice->showunanswered) {
474                 echo "<th  class=\"col0 header\" scope=\"col\">";
475                 print_string('notanswered', 'choice');
476                 echo "</th>";
477                 $column[0] = 0;
478                 foreach ($allresponses[0] as $user) {
479                     $column[0]++;
480                 }
481                 $maxcolumn = $column[0];
482             }
483             $count = 1;
485             foreach ($choice->option as $optionid => $optiontext) {
486                 echo "<th class=\"col$count header\" scope=\"col\">";
487                 echo format_string($optiontext);
488                 echo "</th>";
489                 
490                 $column[$optionid] = 0;
491                 if (isset($allresponses[$optionid])) {
492                     $column[$optionid] = count($allresponses[$optionid]);
493                     if ($column[$optionid] > $maxcolumn) {
494                         $maxcolumn = $column[$optionid];
495                     }
496                 } else {
497                     $column[$optionid] = 0;
498                 }
499             }
500             echo "</tr><tr>";
502             $height = 0;
504             if ($choice->showunanswered) {
505                 if ($maxcolumn) {
506                     $height = $COLUMN_HEIGHT * ((float)$column[0] / (float)$maxcolumn);
507                 }
508                 echo "<td style=\"vertical-align:bottom\" align=\"center\" class=\"col0 data\">";
509                 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
510                 echo "</td>";
511             }
512             $count = 1;
513             foreach ($choice->option as $optionid => $optiontext) {
514                 if ($maxcolumn) {
515                     $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
516                 }
517                 echo "<td style=\"vertical-align:bottom\" align=\"center\" class=\"col$count data\">";
518                 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
519                 echo "</td>";
520                 $count++;
521             }
522             echo "</tr><tr>";
525             if ($choice->showunanswered) {
526                 echo '<td align="center" class="col0 count">';
527                 if (!$choice->limitanswers) {
528                     echo $column[0];
529                     echo '<br />('.format_float(((float)$column[0]/(float)$totalresponsecount)*100.0,1).'%)';
530                 }
531                 echo '</td>';
532             }
533             $count = 1;
534             foreach ($choice->option as $optionid => $optiontext) {
535                 echo "<td align=\"center\" class=\"col$count count\">";
536                 if ($choice->limitanswers) {
537                     echo get_string("taken", "choice").":";
538                     echo $column[$optionid].'<br />';
539                     echo get_string("limit", "choice").":";
540                     $choice_option = get_record("choice_options", "id", $optionid);
541                     echo $choice_option->maxanswers;
542                 } else {
543                     echo $column[$optionid];
544                     echo '<br />('.format_float(((float)$column[$optionid]/(float)$totalresponsecount)*100.0,1).'%)';
545                 }
546                 echo "</td>";
547                 $count++;
548             }
549             echo "</tr></table>";
550             
551             break;
552     }
556 function choice_delete_responses($attemptids, $choiceid) {
558     if(!is_array($attemptids) || empty($attemptids)) {
559         return false;
560     }
562     foreach($attemptids as $num => $attemptid) {
563         if(empty($attemptid)) {
564             unset($attemptids[$num]);
565         }
566     }
568     foreach($attemptids as $attemptid) {
569         if ($todelete = get_record('choice_answers', 'choiceid', $choiceid, 'userid', $attemptid)) {
570             delete_records('choice_answers', 'choiceid', $choiceid, 'userid', $attemptid);
571         }
572     }
573     return true;
577 function choice_delete_instance($id) {
578 // Given an ID of an instance of this module,
579 // this function will permanently delete the instance
580 // and any data that depends on it.
582     if (! $choice = get_record("choice", "id", "$id")) {
583         return false;
584     }
586     $result = true;
588     if (! delete_records("choice_answers", "choiceid", "$choice->id")) {
589         $result = false;
590     }
592     if (! delete_records("choice_options", "choiceid", "$choice->id")) {
593         $result = false;
594     }
596     if (! delete_records("choice", "id", "$choice->id")) {
597         $result = false;
598     }
600     return $result;
603 function choice_get_participants($choiceid) {
604 //Returns the users with data in one choice
605 //(users with records in choice_responses, students)
607     global $CFG;
609     //Get students
610     $students = get_records_sql("SELECT DISTINCT u.id, u.id
611                                  FROM {$CFG->prefix}user u,
612                                       {$CFG->prefix}choice_answers a
613                                  WHERE a.choiceid = '$choiceid' and
614                                        u.id = a.userid");
616     //Return students array (it contains an array of unique users)
617     return ($students);
621 function choice_get_option_text($choice, $id) {
622 // Returns text string which is the answer that matches the id
623     if ($result = get_record("choice_options", "id", $id)) {
624         return $result->text;
625     } else {
626         return get_string("notanswered", "choice");
627     }
630 function choice_get_choice($choiceid) {
631 // Gets a full choice record
633     if ($choice = get_record("choice", "id", $choiceid)) {
634         if ($options = get_records("choice_options", "choiceid", $choiceid, "id")) {
635             foreach ($options as $option) {
636                 $choice->option[$option->id] = $option->text;
637                 $choice->maxanswers[$option->id] = $option->maxanswers;
638             }
639             return $choice;
640         }
641     }
642     return false;
645 function choice_get_view_actions() {
646     return array('view','view all','report');
649 function choice_get_post_actions() {
650     return array('choose','choose again');
654 /**
655  * Implementation of the function for printing the form elements that control
656  * whether the course reset functionality affects the choice.
657  * @param $mform form passed by reference
658  */
659 function choice_reset_course_form_definition(&$mform) {
660     $mform->addElement('header', 'choiceheader', get_string('modulenameplural', 'choice'));
661     $mform->addElement('advcheckbox', 'reset_choice', get_string('removeresponses','choice'));
664 /**
665  * Course reset form defaults.
666  */
667 function choice_reset_course_form_defaults($course) {
668     return array('reset_choice'=>1);
671 /**
672  * Actual implementation of the rest coures functionality, delete all the
673  * choice responses for course $data->courseid.
674  * @param $data the data submitted from the reset course.
675  * @return array status array
676  */
677 function choice_reset_userdata($data) {
678     global $CFG;
680     $componentstr = get_string('modulenameplural', 'choice');
681     $status = array();
683     if (!empty($data->reset_choice)) {
684         $choicessql = "SELECT ch.id
685                          FROM {$CFG->prefix}choice ch
686                         WHERE ch.course={$data->courseid}";
688         delete_records_select('choice_answers', "choiceid IN ($choicessql)");
689         $status[] = array('component'=>$componentstr, 'item'=>get_string('removeresponses', 'choice'), 'error'=>false);
690     }
692     /// updating dates - shift may be negative too
693     if ($data->timeshift) {
694         shift_course_mod_dates('choice', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
695         $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
696     }
698     return $status;
701 function choice_get_response_data($choice, $cm, $groupmode) {
702     global $CFG, $USER;
704     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
706 /// Get the current group
707     if ($groupmode > 0) {
708         $currentgroup = groups_get_activity_group($cm);
709     } else {
710         $currentgroup = 0;
711     }
713 /// Initialise the returned array, which is a matrix:  $allresponses[responseid][userid] = responseobject
714     $allresponses = array();
716 /// First get all the users who have access here
717 /// To start with we assume they are all "unanswered" then move them later
718     $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);
720 /// Get all the recorded responses for this choice
721     $rawresponses = get_records('choice_answers', 'choiceid', $choice->id);
723 /// Use the responses to move users into the correct column
725     if ($rawresponses) {
726         foreach ($rawresponses as $response) {
727             if (isset($allresponses[0][$response->userid])) {   // This person is enrolled and in correct group
728                 $allresponses[0][$response->userid]->timemodified = $response->timemodified;
729                 $allresponses[$response->optionid][$response->userid] = clone($allresponses[0][$response->userid]);
730                 unset($allresponses[0][$response->userid]);   // Remove from unanswered column
731             }
732         }
733     }
735     return $allresponses;
738 ?>