d7051a93f223dab2ee0b3ca8bbf55521f3be8f2b
[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.html) 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) and $choice->timerestrict) {
59         $choice->timeopen = make_timestamp($choice->openyear, $choice->openmonth, $choice->openday,
60                                      $choice->openhour, $choice->openminute, 0);
61         $choice->timeclose = make_timestamp($choice->closeyear, $choice->closemonth, $choice->closeday,
62                                       $choice->closehour, $choice->closeminute, 0);
63     } else {
64         $choice->timeopen = 0;
65         $choice->timeclose = 0;
66     }
68     //insert answers    
69     if ($choice->id = insert_record("choice", $choice)) {
70         foreach ($choice as $name => $value) {        
71             if (strstr($name, "newoption")) {   /// New option
72                 $value = trim($value);
73                 if (isset($value) && $value <> '') {
74                     $option = NULL;
75                     $option->text = $value;
76                     $option->choiceid = $choice->id;
77                     if (isset($choice->{'newlimit'.substr($name, 9)})) {
78                         $option->maxanswers = $choice->{'newlimit'.substr($name, 9)};
79                     }
80                     $option->timemodified = time();
81                     insert_record("choice_options", $option);                
82                 }
83             }
84         }
85     }
86     return $choice->id;
87 }
90 function choice_update_instance($choice) {
91 // Given an object containing all the necessary data, 
92 // (defined by the form in mod.html) this function 
93 // will update an existing instance with new data.
95     $choice->id = $choice->instance;
96     $choice->timemodified = time();
99     if (!empty($choice->timerestrict) and $choice->timerestrict) {
100         $choice->timeopen = make_timestamp($choice->openyear, $choice->openmonth, $choice->openday,
101                                      $choice->openhour, $choice->openminute, 0);
102         $choice->timeclose = make_timestamp($choice->closeyear, $choice->closemonth, $choice->closeday,
103                                       $choice->closehour, $choice->closeminute, 0);
104     } else {
105         $choice->timeopen = 0;
106         $choice->timeclose = 0;
107     }
108     
109     //update answers
110     
111     foreach ($choice as $name => $value) {        
112         $value = trim($value);
114         if (strstr($name, "oldoption")) {  // Old option
115             if (isset($value) && $value <> '') {
116                 $option = NULL;
117                 $option->id = substr($name, 9); // Get the ID of the answer that needs to be updated.
118                 $option->text = $value;
119                 $option->choiceid = $choice->id;
120                 if (isset($choice->{'oldlimit'.substr($name, 9)})) {
121                     $option->maxanswers = $choice->{'oldlimit'.substr($name, 9)};
122                 }
123                 $option->timemodified = time();
124                 update_record("choice_options", $option);
125             } else { //empty old option - needs to be deleted.
126                 delete_records("choice_options", "id", substr($name, 9));
127             }
128         } else if (strstr($name, "newoption")) {   /// New option
129             if (isset($value)&& $value <> '') {
130                 $option = NULL;
131                 $option->text = $value;
132                 $option->choiceid = $choice->id;
133                 if (isset($choice->{'newlimit'.substr($name, 9)})) {
134                     $option->maxanswers = $choice->{'newlimit'.substr($name, 9)};
135                 }
136                 $option->timemodified = time();
137                 insert_record("choice_options", $option);                
138             }
139         }      
140     }
142     return update_record('choice', $choice);
143       
146 function choice_show_form($choice, $user, $cm) {
147         
148 //$cdisplay is an array of the display info for a choice $cdisplay[$optionid]->text  - text name of option.
149 //                                                                            ->maxanswers -maxanswers for this option
150 //                                                                            ->full - whether this option is full or not. 0=not full, 1=full
151 $cdisplay = array();
153     $aid = 0;
154     foreach ($choice->option as $optionid => $text) {
155         if (isset($text)) { //make sure there are no dud entries in the db with blank text values.
156             $countanswers = (get_records("choice_answers", "optionid", $optionid));
157             $countans = 0;           
158             $context = get_context_instance(CONTEXT_MODULE, $cm->id);
159             if (!empty($countanswers)) {
160                 foreach ($countanswers as $ca) { //only return enrolled users.                                  
161                                     if (has_capability('mod/choice:choose', $context->id)) {
162                                         //if (isstudent($cm->course, $ca->userid) or isteacher($cm->course, $ca->userid)) {     
163                         $countans = $countans+1;
164                     }
165                 }
166             }
167             if ($countanswers) {
168                 $countanswers = count($countanswers);
169             } else {
170                 $countanswers = 0;
171             }
172             $maxans = $choice->maxanswers[$optionid];
174                 $cdisplay[$aid]->optionid = $optionid;
175                     $cdisplay[$aid]->text = $text;
176                     $cdisplay[$aid]->maxanswers = $maxans;
177                     $cdisplay[$aid]->countanswers = $countans;
179                     if ($current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id, 'optionid', $optionid)) {
180                         $cdisplay[$aid]->checked = ' checked="checked" ';       
181                     } else {
182                         $cdisplay[$aid]->checked = '';  
183                 }
184             if ($choice->limitanswers && ($countans >= $maxans) && (empty($cdisplay[$aid]->checked)) ) {
185                                 $cdisplay[$aid]->disabled = ' disabled="disabled" ';    
186                         } else {
187                                 $cdisplay[$aid]->disabled = ''; 
188                     }
189             $aid++;
190         }
191     }
193         switch ($choice->display) {
194             case CHOICE_DISPLAY_HORIZONTAL:
195                 echo "<table cellpadding=\"20\" cellspacing=\"20\" align=\"center\"><tr>";
196                                                     
197                 foreach ($cdisplay as $cd) { 
198                     echo "<td align=\"center\" valign=\"top\">";
199                     echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";                    
200                     if (!empty($cd->disabled)) {               
201                             echo format_text($cd->text."<br /><strong>".get_string('full', 'choice')."</strong>");                                  
202                                     } else {
203                         echo format_text($cd->text);
204                     }
205                         echo "</td>";                    
206                 }
207                 echo "</tr>";
208                 echo "</table>";
209                 break;
211             case CHOICE_DISPLAY_VERTICAL:
212                 $displayoptions->para = false;
213                 echo "<table cellpadding=\"10\" cellspacing=\"10\" align=\"center\">";
214                 foreach ($cdisplay as $cd) {                    
215                         echo "<tr><td align=\"left\">";
216                         echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";                    
218                         echo format_text($cd->text. ' ', FORMAT_MOODLE, $displayoptions); //display text for option.
219                         
220                          if ($choice->limitanswers && ($choice->showresults==CHOICE_SHOWRESULTS_ALWAYS) ){ //if limit is enabled, and show results always has been selected, display info beside each choice.
221                             echo "</td><td>";
222                             
223                             if (!empty($cd->disabled)) {
224                                 echo get_string('full', 'choice');
225                                                     } elseif(!empty($cd->checked)) {
226                                                             //currently do nothing - maybe some text could be added here to signfy that the choice has been 'selected'
227                             } elseif ($cd->maxanswers-$cd->countanswers==1) {
228                                 echo ($cd->maxanswers - $cd->countanswers);
229                                 echo " ".get_string('spaceleft', 'choice');
230                             } else {
231                                 echo ($cd->maxanswers - $cd->countanswers);
232                                 echo " ".get_string('spacesleft', 'choice');
233                             }
234                             echo "</td>";
235                          } else if ($choice->limitanswers && ($cd->countanswers >= $cd->maxanswers)) {  //if limitanswers and answers exceeded, display "full" beside the choice.
236                             echo " <strong>".get_string('full', 'choice')."</strong>";
237                          }
238                         echo "</td>";
239                         echo "</tr>";
241                 }
242                 echo "</table>";
243                 break;
244         }
245         //show save choice button
246         echo "<center>";
247         echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
248         if (!isguest()) { //don't show save button if the logged in user is the guest user.
249             echo "<input type=\"submit\" value=\"".get_string("savemychoice","choice")."\" />";
250         } else {
251             print_string('havetologin', 'choice');
252         }
253         echo "</center>";
256 function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $cm) {
258 $current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $userid);
259         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
260         $countanswers = get_records("choice_answers", "optionid", $formanswer);
261             if ($countanswers) {
262             $countans = 0;
263             foreach ($countanswers as $ca) { //only return enrolled users.
264                                 if (has_capability('mod/choice:choose', $context->id)) {
265                                 //if (isstudent($courseid, $ca->userid) or isteacher($courseid, $ca->userid)) { 
266                                     $countans = $countans+1;
267                             }
268                         }                               
269                                 
270                 $countanswers = count($countans);
271                 } else {
272                     $countanswers = 0;
273                 }
274             $maxans = $choice->maxanswers[$formanswer];
275             
276             if (!($choice->limitanswers && ($countanswers >= $maxans) )) {                              
277                 if ($current) {
278                                         
279                     $newanswer = $current;
280                     $newanswer->optionid = $formanswer;
281                     $newanswer->timemodified = time();
282                     if (! update_record("choice_answers", $newanswer)) {
283                         error("Could not update your choice because of a database error");
284                     }
285                     add_to_log($courseid, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id);
286                 } else {
287                     $newanswer = NULL;
288                     $newanswer->choiceid = $choice->id;
289                     $newanswer->userid = $userid;
290                     $newanswer->optionid = $formanswer;
291                     $newanswer->timemodified = time();
292                     if (! insert_record("choice_answers", $newanswer)) {
293                         error("Could not save your choice");
294                     }
295                     add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id);
296                 }
297             } else {
298                 if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error
299                     error("this choice is full!");    
300                 }
301             }
305 function choice_show_reportlink($choice, $courseid, $cmid) {
306         $context = get_context_instance(CONTEXT_MODULE, $cmid);
307             if ( $allanswers = get_records("choice_answers", "choiceid", $choice->id)) {
308             $responsecount = 0;
309             foreach ($allanswers as $aa) {
310                 if (has_capability('mod/choice:readresponses', $context->id)) {
311                                 //if (isstudent($courseid, $aa->userid) or isteacher($courseid, $aa->userid)) { //check to make sure user is enrolled in course.
312                     $responsecount++;
313                 }
314             }
315         } else {
316             $responsecount = 0;
317         }
318         echo '<div class="reportlink">';
319         echo "<a href=\"report.php?id=$cmid\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
320         echo '</div>';
323 function choice_show_results($choice, $course, $cm, $forcepublish='') {
324             
325         global $CFG, $COLUMN_HEIGHT, $USER;
326                 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
327         print_heading(get_string("responses", "choice"));        
328         if (empty($forcepublish)) { //alow the publish setting to be overridden
329                         $forcepublish = $choice->publish;
330                 }               
331                         
332         /// Check to see if groups are being used in this choice
333     if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
334         $currentgroup = setup_and_print_groups($course, $groupmode, $_SERVER['PHP_SELF']."?id=$cm->id");
335     } else {
336         $currentgroup = false;
337     }
339         if ($currentgroup) {
340             $users = get_group_users($currentgroup, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber');
341         } else {
342             $users = get_course_users($course->id, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber') + get_admins();
343         }
346         if (!$users) {
347             print_heading(get_string("nousersyet"));
348         }
350         if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
351             foreach ($allresponses as $aa) {
352                 $answers[$aa->userid] = $aa;
353             }
354         } else {
355             $answers = array () ;
356         }
358         $timenow = time();
360         foreach ($choice->option as $optionid => $text) {
361             $useranswer[$optionid] = array();
362         }
363         if (!empty($users)) {
364             foreach ($users as $user) {
365                 if (!empty($user->id) and !empty($answers[$user->id])) {
366                     $answer = $answers[$user->id];
367                     $useranswer[(int)$answer->optionid][] = $user;
368                 } else {
369                     $useranswer[0][] = $user;
370                 }
371             }
372         }
373         foreach ($choice->option as $optionid => $text) {
374             if (!$choice->option[$optionid]) {
375                 unset($useranswer[$optionid]);     // Throw away any data that doesn't apply
376             }
377         }
378         ksort($useranswer);
380         switch ($forcepublish) {
381           case CHOICE_PUBLISH_NAMES:
383             //$isteacher = isteacher($course->id);
385             $tablewidth = (int) (100.0 / count($useranswer));
386                         if (has_capability('mod/choice:readresponses', $context->id)) {
387             //if (isteacher($course->id, $USER->id)) {
388                                 echo '<div id="tablecontainer">';
389                 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);">';
390                 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
391                 echo '<input type="hidden" name="mode" value="overview" />';
392             }
393             
394             echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\" class=\"results names\">";            
395             echo "<tr>";
396             $count = 0;
397             foreach ($useranswer as $optionid => $userlist) {
398                 if ($optionid) {
399                     echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
400                 } else if ($choice->showunanswered) {
401                     echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
402                 } else {
403                     continue;
404                 }
405                 echo format_string(choice_get_option_text($choice, $optionid));
406                 echo "</th>";
407                 $count++;
408             }
409             echo "</tr><tr>";
411             $count = 0;
412             foreach ($useranswer as $optionid => $userlist) {
413                 if ($optionid) {
414                     echo "<td class=\"col$count data\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
415                 } else if ($choice->showunanswered) {
416                     echo "<td class=\"col$count data\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
417                 } else {
418                     continue;
419                 }
421                 echo "<table width=\"100%\">";
422                 foreach ($userlist as $user) {
423                         // this needs to be fixed
424                     if (!($optionid==0 && isadmin($user->id)) && !($optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) )  ) { //make sure admins and hidden teachers are not shown in not answered yet column.
425                         echo "<tr>";
426                         if (isteacher($course->id, $USER->id) && !($optionid==0)) {
427                             echo '<td width=\"5\" nowrap=\"nowrap\"><input type="checkbox" name="attemptid[]" value="'. $answers[$user->id]->id. '" /></td>';
428                         }
429                         echo "<td width=\"10\" nowrap=\"nowrap\" class=\"picture\">";
430                         print_user_picture($user->id, $course->id, $user->picture);
431                         echo "</td><td width=\"100%\" nowrap=\"nowrap\" class=\"fullname\">";
432                         echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
433                         echo fullname($user, $isteacher);
434                         echo "</a>";
435                         echo "</td></tr>";
436                     }
437                 }
438                 $count++;
439                 echo "</table>";
441                 echo "</td>";
442             }
443             echo "</tr><tr>";
444             $count = 0;
445             foreach ($useranswer as $optionid => $userlist) {
446                 if (!$optionid and !$choice->showunanswered) {
447                     continue;
448                 }
449                 echo "<td align=\"center\" class=\"count\">";
450                 $countanswers = get_records("choice_answers", "optionid", $optionid);                
451                 $countans = 0;  
452                 if (!empty($countanswers)) {              
453                     foreach ($countanswers as $ca) { //only return enrolled users.              
454                                                 // needs fixing too                                     
455                                         if (isstudent($course->id, $ca->userid) or isteacher($course->id, $ca->userid)) {                                                       
456                                            $countans = $countans+1;
457                                     }                           
458                                 }
459                 }
460                 if ($choice->limitanswers && !$optionid==0) {
461                     echo get_string("taken", "choice").":";
462                     echo $countans;
463                     echo "<br>";
464                     echo get_string("limit", "choice").":";
465                     $choice_option = get_record("choice_options", "id", $optionid);
466                     echo $choice_option->maxanswers;
467                 }
468                 echo "</td>";
469                 $count++;
470             }
471             
472                 /// Print "Select all" etc.
473                 if (has_capability('mod/choice:readresponses', $context->id)) {
474                 //if (isteacher($course->id, $USER->id)) {
475                     echo '<tr><td><p>';
476                     echo '<tr><td>';
477                     echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
478                     echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
479                     echo '&nbsp;&nbsp;';
480                     $options = array('delete' => get_string('delete'));
481                     echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
482                     echo '<noscript id="noscriptmenuaction" style="display: inline;">';
483                     echo '<input type="submit" value="'.get_string('go').'" /></noscript>';
484                     echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
485                     echo '</p></td></tr>';
486                 }
488             
489             
490             echo "</tr></table>";
491             //if (isteacher($course->id, $USER->id)) {
492             if (has_capability('mod/choice:readresponses', $context->id)) {
493                             echo "</form></div>";
494                         }
495             break;
498           case CHOICE_PUBLISH_ANONYMOUS:
499           
500             $tablewidth = (int) (100.0 / count($useranswer));
502             echo "<table cellpadding=\"5\" cellspacing=\"0\" align=\"center\" class=\"results anonymous\">";
503             echo "<tr>";
504             $count = 0;
505             foreach ($useranswer as $optionid => $userlist) {
506                 if ($optionid) {
507                     echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
508                 } else if ($choice->showunanswered) {
509                     echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
510                 } else {
511                     continue;
512                 }
513                 echo format_string(choice_get_option_text($choice, $optionid));
514                 echo "</th>";
515                 $count++;
516             }
517             echo "</tr><tr>";
519             $maxcolumn = 0;
520             foreach ($useranswer as $optionid => $userlist) {
521                 if (!$optionid and !$choice->showunanswered) {
522                     continue;
523                 }
524                 $column[$optionid] = 0;
525                 foreach ($userlist as $user) {
526                     if (!($optionid==0 && isadmin($user->id)) && !($optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) )  ) { //make sure admins and hidden teachers are not shown in not answered yet column.
527                          $column[$optionid]++;
528                     }
529                 }
530                 if ($column[$optionid] > $maxcolumn) {
531                     $maxcolumn = $column[$optionid];
532                 }
533             }
535             echo "</tr><tr>";
536             $count = 0;
537             foreach ($useranswer as $optionid => $userlist) {
538                 if (!$optionid and !$choice->showunanswered) {
539                     continue;
540                 }
541                 $height = 0;
542                 if ($maxcolumn) {
543                     $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
544                 }
545                 echo "<td valign=\"bottom\" align=\"center\" class=\"col$count data\">";
546                 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
547                 echo "</td>";
548                 $count++;
549             }
550             echo "</tr>";
552             echo "<tr>";
553             $count = 0;
554             foreach ($useranswer as $optionid => $userlist) {
555                 if (!$optionid and !$choice->showunanswered) {
556                     continue;
557                 }
558                 echo "<td align=\"center\" class=\"col$count count\">";
559                 if ($choice->limitanswers && !$optionid==0) {
560                     echo get_string("taken", "choice").":";
561                     echo $column[$optionid];
562                     echo "<br>";
563                     echo get_string("limit", "choice").":";
564                     $choice_option = get_record("choice_options", "id", $optionid);
565                     echo $choice_option->maxanswers;
566                 } else {
567                     echo $column[$optionid];
568                 }
569                 echo "</td>";
570                 $count++;
571             }
572             echo "</tr></table>";
574             break;
575         }       
579 function choice_delete_responses($attemptids) {
580         
581         if(!is_array($attemptids) || empty($attemptids)) {
582         return false;
583     }
585     foreach($attemptids as $num => $attemptid) {
586         if(empty($attemptid)) {
587             unset($attemptids[$num]);
588         }
589     }
591     foreach($attemptids as $attemptid) {
592         if ($todelete = get_record('choice_answers', 'id', $attemptid)) {
593              delete_records('choice_answers', 'id', $attemptid);
594         }
595     }    
596     return true;
600 function choice_delete_instance($id) {
601 // Given an ID of an instance of this module, 
602 // this function will permanently delete the instance 
603 // and any data that depends on it.  
605     if (! $choice = get_record("choice", "id", "$id")) {
606         return false;
607     }
609     $result = true;
611     if (! delete_records("choice_answers", "choiceid", "$choice->id")) {
612         $result = false;
613     }
615     if (! delete_records("choice_options", "choiceid", "$choice->id")) {
616         $result = false;
617     }
619     if (! delete_records("choice", "id", "$choice->id")) {
620         $result = false;
621     }
622     
624     return $result;
628 function choice_get_participants($choiceid) {
629 //Returns the users with data in one choice
630 //(users with records in choice_responses, students)
632     global $CFG;
634     //Get students
635     $students = get_records_sql("SELECT DISTINCT u.id, u.id
636                                  FROM {$CFG->prefix}user u,
637                                       {$CFG->prefix}choice_answers a
638                                  WHERE a.choiceid = '$choiceid' and
639                                        u.id = a.userid");
641     //Return students array (it contains an array of unique users)
642     return ($students);
646 function choice_get_option_text($choice, $id) {
647 // Returns text string which is the answer that matches the id
648     if ($result = get_record("choice_options", "id", $id)) {
649         return $result->text;
650     } else {
651         return get_string("notanswered", "choice");
652     }            
655 function choice_get_choice($choiceid) {
656 // Gets a full choice record      
658     if ($choice = get_record("choice", "id", $choiceid)) {
659         if ($options = get_records("choice_options", "choiceid", $choiceid, "id")) {
660             foreach ($options as $option) {                         
661                 $choice->option[$option->id] = $option->text; 
662                 $choice->maxanswers[$option->id] = $option->maxanswers;
663             }        
664             return $choice;
665         }
666     }
667     return false;
670 function choice_get_view_actions() {
671     return array('view','view all','report');
674 function choice_get_post_actions() {
675     return array('choose','choose again');
678 ?>