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
;
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
));
45 print_string("notanswered", "choice");
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
)) {
59 $choice->timeopen
= 0;
60 $choice->timeclose
= 0;
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];
74 $option->timemodified
= time();
75 insert_record("choice_options", $option);
83 function choice_update_instance($choice) {
84 // Given an object containing all the necessary data,
85 // (defined by the form in mod.html) 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;
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];
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
);
115 if (isset($value) && $value <> '') {
116 insert_record("choice_options", $option);
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
136 if ($choice->limitanswers
) { //set choicefull to true by default if limitanswers.
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]);
150 $cdisplay[$aid]->countanswers
= 0;
152 if ($current = get_record('choice_answers', 'choiceid', $choice->id
, 'userid', $user->id
, 'optionid', $optionid)) {
153 $cdisplay[$aid]->checked
= ' checked="checked" ';
155 $cdisplay[$aid]->checked
= '';
157 if ( $choice->limitanswers
&&
158 ($cdisplay[$aid]->countanswers
>= $cdisplay[$aid]->maxanswers
) &&
159 (empty($cdisplay[$aid]->checked
)) ) {
160 $cdisplay[$aid]->disabled
= ' disabled="disabled" ';
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.
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>");
181 echo format_text($cd->text
);
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.
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');
209 echo ($cd->maxanswers
- $cd->countanswers
);
210 echo " ".get_string('spacesleft', 'choice');
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>";
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.
227 print_string('choicefull', 'choice');
230 echo "<input type=\"submit\" value=\"".get_string("savemychoice","choice")."\" />";
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
."&action=delchoice'>".get_string("removemychoice","choice")."</a>";
236 print_string('havetologin', 'choice');
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
);
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);
255 // If groups are being used, retrieve responses only for users in
258 $answers = get_records_sql("
262 {$CFG->prefix}choice_answers ca
263 INNER JOIN {$CFG->prefix}groups_members gm ON ca.userid=gm.userid
266 AND gm.groupid=$currentgroup");
268 // Groups are not used, retrieve all answers for this option ID
269 $answers = get_records("choice_answers", "optionid", $formanswer);
273 foreach ($answers as $a) { //only return enrolled users.
274 if (has_capability('mod/choice:choose', $context, $a->userid
, false)) {
279 $maxans = $choice->maxanswers
[$formanswer];
282 if (!($choice->limitanswers
&& ($countanswers >= $maxans) )) {
285 $newanswer = $current;
286 $newanswer->optionid
= $formanswer;
287 $newanswer->timemodified
= time();
288 if (! update_record("choice_answers", $newanswer)) {
289 error("Could not update your choice because of a database error");
291 add_to_log($courseid, "choice", "choose again", "view.php?id=$cm->id", $choice->id
, $cm->id
);
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 error("Could not save your choice");
301 add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id
, $cm->id
);
304 if (!($current->optionid
==$formanswer)) { //check to see if current choice already selected - if not display error
305 error("this choice is full!");
310 function choice_show_reportlink($user, $cm) {
312 foreach($user as $optionid => $userlist) {
314 $responsecount +
= count($userlist);
318 echo '<div class="reportlink">';
319 echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
323 function choice_show_results($choice, $course, $cm, $allresponses, $forcepublish='') {
324 global $CFG, $COLUMN_HEIGHT;
326 print_heading(get_string("responses", "choice"));
327 if (empty($forcepublish)) { //alow the publish setting to be overridden
328 $forcepublish = $choice->publish
;
331 if (!$allresponses) {
332 print_heading(get_string("nousersyet"));
335 $totalresponsecount = 0;
336 foreach ($allresponses as $optionid => $userlist) {
337 if ($choice->showunanswered ||
$optionid) {
338 $totalresponsecount +
= count($userlist);
342 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
344 $hascapfullnames = has_capability('moodle/site:viewfullnames', $context);
346 $viewresponses = has_capability('mod/choice:readresponses', $context);
347 switch ($forcepublish) {
348 case CHOICE_PUBLISH_NAMES
:
349 echo '<div id="tablecontainer">';
350 if ($viewresponses) {
351 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);">';
353 echo '<input type="hidden" name="id" value="'.$cm->id
.'" />';
354 echo '<input type="hidden" name="mode" value="overview" />';
357 echo "<table cellpadding=\"5\" cellspacing=\"10\" class=\"results names\">";
360 $columncount = array(); // number of votes in each column
361 if ($choice->showunanswered
) {
363 echo "<th class=\"col0 header\" scope=\"col\">";
364 print_string('notanswered', 'choice');
368 foreach ($choice->option
as $optionid => $optiontext) {
369 $columncount[$optionid] = 0; // init counters
370 echo "<th class=\"col$count header\" scope=\"col\">";
371 echo format_string($optiontext);
377 if ($choice->showunanswered
) {
378 echo "<td class=\"col$count data\" >";
379 // added empty row so that when the next iteration is empty,
380 // we do not get <table></table> erro from w3c validator
382 echo "<table class=\"choiceresponse\"><tr><td></td></tr>";
383 foreach ($allresponses[0] as $user) {
385 echo "<td class=\"picture\">";
386 print_user_picture($user->id
, $course->id
, $user->picture
);
387 echo "</td><td class=\"fullname\">";
388 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">";
389 echo fullname($user, $hascapfullnames);
393 echo "</table></td>";
396 foreach ($choice->option
as $optionid => $optiontext) {
397 echo '<td class="col'.$count.' data" >';
399 // added empty row so that when the next iteration is empty,
400 // we do not get <table></table> erro from w3c validator
402 echo '<table class="choiceresponse"><tr><td></td></tr>';
403 if (isset($allresponses[$optionid])) {
404 foreach ($allresponses[$optionid] as $user) {
405 $columncount[$optionid] +
= 1;
406 echo '<tr><td class="attemptcell">';
407 if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
408 echo '<input type="checkbox" name="attemptid[]" value="'. $user->id
. '" />';
410 echo '</td><td class="picture">';
411 print_user_picture($user->id
, $course->id
, $user->picture
);
412 echo '</td><td class="fullname">';
413 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">";
414 echo fullname($user, $hascapfullnames);
420 echo '</table></td>';
425 if ($choice->showunanswered
) {
429 foreach ($choice->option
as $optionid => $optiontext) {
430 echo "<td align=\"center\" class=\"count\">";
431 if ($choice->limitanswers
) {
432 echo get_string("taken", "choice").":";
433 echo $columncount[$optionid];
435 echo get_string("limit", "choice").":";
436 $choice_option = get_record("choice_options", "id", $optionid);
437 echo $choice_option->maxanswers
;
439 if (isset($columncount[$optionid])) {
440 echo $columncount[$optionid];
448 /// Print "Select all" etc.
449 if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
450 echo '<tr><td></td><td>';
451 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
452 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
454 $options = array('delete' => get_string('delete'));
455 echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
456 echo '<noscript id="noscriptmenuaction" style="display: inline;">';
458 echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
459 echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
460 echo '</td><td></td></tr>';
463 echo "</table></div>";
464 if ($viewresponses) {
465 echo "</form></div>";
470 case CHOICE_PUBLISH_ANONYMOUS
:
472 echo "<table cellpadding=\"5\" cellspacing=\"0\" class=\"results anonymous\">";
475 if ($choice->showunanswered
) {
476 echo "<th class=\"col0 header\" scope=\"col\">";
477 print_string('notanswered', 'choice');
480 foreach ($allresponses[0] as $user) {
483 $maxcolumn = $column[0];
487 foreach ($choice->option
as $optionid => $optiontext) {
488 echo "<th class=\"col$count header\" scope=\"col\">";
489 echo format_string($optiontext);
492 $column[$optionid] = 0;
493 if (isset($allresponses[$optionid])) {
494 $column[$optionid] = count($allresponses[$optionid]);
495 if ($column[$optionid] > $maxcolumn) {
496 $maxcolumn = $column[$optionid];
499 $column[$optionid] = 0;
506 if ($choice->showunanswered
) {
508 $height = $COLUMN_HEIGHT * ((float)$column[0] / (float)$maxcolumn);
510 echo "<td style=\"vertical-align:bottom\" align=\"center\" class=\"col0 data\">";
511 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
515 foreach ($choice->option
as $optionid => $optiontext) {
517 $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
519 echo "<td style=\"vertical-align:bottom\" align=\"center\" class=\"col$count data\">";
520 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
527 if ($choice->showunanswered
) {
528 echo '<td align="center" class="col0 count">';
529 if (!$choice->limitanswers
) {
531 echo '<br />('.format_float(((float)$column[0]/(float)$totalresponsecount)*100.0,1).'%)';
536 foreach ($choice->option
as $optionid => $optiontext) {
537 echo "<td align=\"center\" class=\"col$count count\">";
538 if ($choice->limitanswers
) {
539 echo get_string("taken", "choice").":";
540 echo $column[$optionid].'<br />';
541 echo get_string("limit", "choice").":";
542 $choice_option = get_record("choice_options", "id", $optionid);
543 echo $choice_option->maxanswers
;
545 echo $column[$optionid];
546 echo '<br />('.format_float(((float)$column[$optionid]/(float)$totalresponsecount)*100.0,1).'%)';
551 echo "</tr></table>";
558 function choice_delete_responses($attemptids, $choiceid) {
560 if(!is_array($attemptids) ||
empty($attemptids)) {
564 foreach($attemptids as $num => $attemptid) {
565 if(empty($attemptid)) {
566 unset($attemptids[$num]);
570 foreach($attemptids as $attemptid) {
571 if ($todelete = get_record('choice_answers', 'choiceid', $choiceid, 'userid', $attemptid)) {
572 delete_records('choice_answers', 'choiceid', $choiceid, 'userid', $attemptid);
579 function choice_delete_instance($id) {
580 // Given an ID of an instance of this module,
581 // this function will permanently delete the instance
582 // and any data that depends on it.
584 if (! $choice = get_record("choice", "id", "$id")) {
590 if (! delete_records("choice_answers", "choiceid", "$choice->id")) {
594 if (! delete_records("choice_options", "choiceid", "$choice->id")) {
598 if (! delete_records("choice", "id", "$choice->id")) {
605 function choice_get_participants($choiceid) {
606 //Returns the users with data in one choice
607 //(users with records in choice_responses, students)
612 $students = get_records_sql("SELECT DISTINCT u.id, u.id
613 FROM {$CFG->prefix}user u,
614 {$CFG->prefix}choice_answers a
615 WHERE a.choiceid = '$choiceid' and
618 //Return students array (it contains an array of unique users)
623 function choice_get_option_text($choice, $id) {
624 // Returns text string which is the answer that matches the id
625 if ($result = get_record("choice_options", "id", $id)) {
626 return $result->text
;
628 return get_string("notanswered", "choice");
632 function choice_get_choice($choiceid) {
633 // Gets a full choice record
635 if ($choice = get_record("choice", "id", $choiceid)) {
636 if ($options = get_records("choice_options", "choiceid", $choiceid, "id")) {
637 foreach ($options as $option) {
638 $choice->option
[$option->id
] = $option->text
;
639 $choice->maxanswers
[$option->id
] = $option->maxanswers
;
647 function choice_get_view_actions() {
648 return array('view','view all','report');
651 function choice_get_post_actions() {
652 return array('choose','choose again');
657 * Implementation of the function for printing the form elements that control
658 * whether the course reset functionality affects the choice.
659 * @param $mform form passed by reference
661 function choice_reset_course_form_definition(&$mform) {
662 $mform->addElement('header', 'choiceheader', get_string('modulenameplural', 'choice'));
663 $mform->addElement('advcheckbox', 'reset_choice', get_string('removeresponses','choice'));
667 * Course reset form defaults.
669 function choice_reset_course_form_defaults($course) {
670 return array('reset_choice'=>1);
674 * Actual implementation of the rest coures functionality, delete all the
675 * choice responses for course $data->courseid.
676 * @param $data the data submitted from the reset course.
677 * @return array status array
679 function choice_reset_userdata($data) {
682 $componentstr = get_string('modulenameplural', 'choice');
685 if (!empty($data->reset_choice
)) {
686 $choicessql = "SELECT ch.id
687 FROM {$CFG->prefix}choice ch
688 WHERE ch.course={$data->courseid}";
690 delete_records_select('choice_answers', "choiceid IN ($choicessql)");
691 $status[] = array('component'=>$componentstr, 'item'=>get_string('removeresponses', 'choice'), 'error'=>false);
694 /// updating dates - shift may be negative too
695 if ($data->timeshift
) {
696 shift_course_mod_dates('choice', array('timeopen', 'timeclose'), $data->timeshift
, $data->courseid
);
697 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
703 function choice_get_response_data($choice, $cm, $groupmode) {
706 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
708 /// Get the current group
709 if ($groupmode > 0) {
710 $currentgroup = groups_get_activity_group($cm);
715 /// Initialise the returned array, which is a matrix: $allresponses[responseid][userid] = responseobject
716 $allresponses = array();
718 /// First get all the users who have access here
719 /// To start with we assume they are all "unanswered" then move them later
720 $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);
722 /// Get all the recorded responses for this choice
723 $rawresponses = get_records('choice_answers', 'choiceid', $choice->id
);
725 /// Use the responses to move users into the correct column
728 foreach ($rawresponses as $response) {
729 if (isset($allresponses[0][$response->userid
])) { // This person is enrolled and in correct group
730 $allresponses[0][$response->userid
]->timemodified
= $response->timemodified
;
731 $allresponses[$response->optionid
][$response->userid
] = clone($allresponses[0][$response->userid
]);
732 unset($allresponses[0][$response->userid
]); // Remove from unanswered column
737 return $allresponses;
741 * Returns all other caps used in module
743 function choice_get_extra_capabilities() {
744 return array('moodle/site:accessallgroups');