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) {
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
133 foreach ($choice->option
as $optionid => $text) {
134 if (isset($text)) { //make sure there are no dud entries in the db with blank text values.
135 $countanswers = (get_records("choice_answers", "optionid", $optionid));
137 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
138 if (!empty($countanswers)) {
139 foreach ($countanswers as $ca) { //only return enrolled users.
140 if (has_capability('mod/choice:choose', $context)) {
141 $countans = $countans+
1;
146 $countanswers = count($countanswers);
150 $maxans = $choice->maxanswers
[$optionid];
152 $cdisplay[$aid]->optionid
= $optionid;
153 $cdisplay[$aid]->text
= $text;
154 $cdisplay[$aid]->maxanswers
= $maxans;
155 $cdisplay[$aid]->countanswers
= $countans;
157 if ($current = get_record('choice_answers', 'choiceid', $choice->id
, 'userid', $user->id
, 'optionid', $optionid)) {
158 $cdisplay[$aid]->checked
= ' checked="checked" ';
160 $cdisplay[$aid]->checked
= '';
162 if ($choice->limitanswers
&& ($countans >= $maxans) && (empty($cdisplay[$aid]->checked
)) ) {
163 $cdisplay[$aid]->disabled
= ' disabled="disabled" ';
165 $cdisplay[$aid]->disabled
= '';
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 (!isguest()) { //don't show save button if the logged in user is the guest user.
226 echo "<input type=\"submit\" value=\"".get_string("savemychoice","choice")."\" />";
228 if ($choice->allowupdate
&& $aaa = get_record('choice_answers', 'choiceid', $choice->id
, 'userid', $user->id
)) {
229 echo "<br /><a href='view.php?id=".$cm->id
."&action=delchoice'>".get_string("removemychoice","choice")."</a>";
233 print_string('havetologin', 'choice');
238 function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $cm) {
240 $current = get_record('choice_answers', 'choiceid', $choice->id
, 'userid', $userid);
241 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
242 $countanswers = get_records("choice_answers", "optionid", $formanswer);
245 foreach ($countanswers as $ca) { //only return enrolled users.
246 if (has_capability('mod/choice:choose', $context)) {
247 $countans = $countans+
1;
251 $countanswers = $countans;
255 $maxans = $choice->maxanswers
[$formanswer];
257 if (!($choice->limitanswers
&& ($countanswers >= $maxans) )) {
260 $newanswer = $current;
261 $newanswer->optionid
= $formanswer;
262 $newanswer->timemodified
= time();
263 if (! update_record("choice_answers", $newanswer)) {
264 error("Could not update your choice because of a database error");
266 add_to_log($courseid, "choice", "choose again", "view.php?id=$cm->id", $choice->id
, $cm->id
);
269 $newanswer->choiceid
= $choice->id
;
270 $newanswer->userid
= $userid;
271 $newanswer->optionid
= $formanswer;
272 $newanswer->timemodified
= time();
273 if (! insert_record("choice_answers", $newanswer)) {
274 error("Could not save your choice");
276 add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id
, $cm->id
);
279 if (!($current->optionid
==$formanswer)) { //check to see if current choice already selected - if not display error
280 error("this choice is full!");
286 function choice_show_reportlink($choice, $courseid, $cmid, $groupmode) {
287 //TODO: rewrite with SQL
288 $currentgroup = get_current_group($courseid);
289 if ($allanswers = get_records("choice_answers", "choiceid", $choice->id
)) {
291 foreach ($allanswers as $aa) {
292 if ($groupmode and $currentgroup) {
293 if (groups_is_member($currentgroup, $aa->userid
)) {
303 echo '<div class="reportlink">';
304 echo "<a href=\"report.php?id=$cmid\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
308 function choice_show_results($choice, $course, $cm, $forcepublish='') {
310 global $CFG, $COLUMN_HEIGHT, $USER;
311 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
313 print_heading(get_string("responses", "choice"));
315 if (empty($forcepublish)) { //alow the publish setting to be overridden
316 $forcepublish = $choice->publish
;
319 $groupmode = groups_get_activity_groupmode($cm);
321 if ($groupmode > 0) {
322 $currentgroup = groups_get_activity_group($cm);
327 $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC', '', '', $currentgroup, '', false);
329 if (!empty($CFG->enablegroupings
) && !empty($cm->groupingid
) && !empty($users)) {
330 $groupingusers = groups_get_grouping_members($cm->groupingid
, 'u.id', 'u.id');
331 foreach($users as $key => $user) {
332 if (!isset($groupingusers[$user->id
])) {
339 print_heading(get_string("nousersyet"));
342 $answers = array () ;
343 if ($allresponses = get_records("choice_answers", "choiceid", $choice->id
)) {
344 foreach ($allresponses as $aa) {
345 //TODO: rewrite with SQL
346 if ($groupmode and $currentgroup) {
347 if (groups_is_member($currentgroup, $aa->userid
)) {
348 $answers[$aa->userid
] = $aa;
351 $answers[$aa->userid
] = $aa;
358 foreach ($choice->option
as $optionid => $text) {
359 $useranswer[$optionid] = array();
361 if (!empty($users)) {
362 foreach ($users as $user) {
363 if (!empty($user->id
) and !empty($answers[$user->id
])) {
364 $answer = $answers[$user->id
];
365 $useranswer[(int)$answer->optionid
][] = $user;
367 $useranswer[0][] = $user;
371 foreach ($choice->option
as $optionid => $text) {
372 if (!$choice->option
[$optionid]) {
373 unset($useranswer[$optionid]); // Throw away any data that doesn't apply
377 switch ($forcepublish) {
378 case CHOICE_PUBLISH_NAMES
:
380 $tablewidth = (int) (100.0 / count($useranswer));
381 if (has_capability('mod/choice:readresponses', $context)) {
382 echo '<div id="tablecontainer">';
383 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);">';
385 echo '<input type="hidden" name="id" value="'.$cm->id
.'" />';
386 echo '<input type="hidden" name="mode" value="overview" />';
389 echo "<table cellpadding=\"5\" cellspacing=\"10\" class=\"results names\">";
392 $columncount = array(); // number of votes in each column
393 foreach ($useranswer as $optionid => $userlist) {
394 $columncount[$optionid] = 0; // init counters
396 echo "<th class=\"col$count header\" style=\"width:$tablewidth%\" scope=\"col\">";
397 } else if ($choice->showunanswered
) {
398 echo "<th class=\"col$count header\" style=\"width:$tablewidth%\" scope=\"col\">";
402 echo format_string(choice_get_option_text($choice, $optionid));
409 foreach ($useranswer as $optionid => $userlist) {
411 echo "<td class=\"col$count data\" style=\"width:$tablewidth%;\">";
412 } else if ($choice->showunanswered
) {
413 echo "<td class=\"col$count data\" style=\"width:$tablewidth%;\">";
418 // added empty row so that when the next iteration is empty,
419 // we do not get <table></table> erro from w3c validator
421 echo "<table class=\"choiceresponse\"><tr><td></td></tr>";
422 foreach ($userlist as $user) {
423 if ($optionid!=0 or has_capability('mod/choice:choose', $context, $user->id
, false)) {
424 $columncount[$optionid] +
= 1;
426 if (has_capability('mod/choice:readresponses', $context) && $optionid!=0) {
427 echo '<td class="attemptcell"><input type="checkbox" name="attemptid[]" value="'. $answers[$user->id
]->id
. '" /></td>';
429 echo "<td class=\"picture\">";
430 print_user_picture($user->id
, $course->id
, $user->picture
);
431 echo "</td><td class=\"fullname\">";
432 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">";
433 echo fullname($user, has_capability('moodle/site:viewfullnames', $context));
445 foreach ($useranswer as $optionid => $userlist) {
446 if (!$optionid and !$choice->showunanswered
) {
449 echo "<td align=\"center\" class=\"count\">";
450 if ($choice->limitanswers
&& !$optionid==0) {
451 echo get_string("taken", "choice").":";
452 echo $columncount[$optionid];
454 echo get_string("limit", "choice").":";
455 $choice_option = get_record("choice_options", "id", $optionid);
456 echo $choice_option->maxanswers
;
463 /// Print "Select all" etc.
464 if (has_capability('mod/choice:readresponses', $context)) {
465 echo '<tr><td></td><td>';
466 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
467 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
469 $options = array('delete' => get_string('delete'));
470 echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
471 echo '<noscript id="noscriptmenuaction" style="display: inline;">';
473 echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
474 echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
475 echo '</td><td></td></tr>';
479 if (has_capability('mod/choice:readresponses', $context)) {
480 echo "</div></form></div>";
485 case CHOICE_PUBLISH_ANONYMOUS
:
487 $tablewidth = (int) (100.0 / count($useranswer));
489 echo "<table cellpadding=\"5\" cellspacing=\"0\" class=\"results anonymous\">";
492 foreach ($useranswer as $optionid => $userlist) {
494 echo "<th style=\"width:$tablewidth%\" class=\"col$count header\" scope=\"col\">";
495 } else if ($choice->showunanswered
) {
496 echo "<th style=\"width:$tablewidth%\" class=\"col$count header\" scope=\"col\">";
500 echo format_string(choice_get_option_text($choice, $optionid));
507 foreach ($useranswer as $optionid => $userlist) {
508 if (!$optionid and !$choice->showunanswered
) {
511 $column[$optionid] = 0;
512 foreach ($userlist as $user) {
513 if ($optionid!=0 or has_capability('mod/choice:choose', $context, $user->id
, false)) {
514 $column[$optionid]++
;
517 if ($column[$optionid] > $maxcolumn) {
518 $maxcolumn = $column[$optionid];
524 foreach ($useranswer as $optionid => $userlist) {
525 if (!$optionid and !$choice->showunanswered
) {
530 $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
532 echo "<td valign=\"bottom\" align=\"center\" class=\"col$count data\">";
533 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
541 foreach ($useranswer as $optionid => $userlist) {
542 if (!$optionid and !$choice->showunanswered
) {
545 echo "<td align=\"center\" class=\"col$count count\">";
546 if ($choice->limitanswers
&& !$optionid==0) {
547 echo get_string("taken", "choice").":";
548 echo $column[$optionid];
550 echo get_string("limit", "choice").":";
551 $choice_option = get_record("choice_options", "id", $optionid);
552 echo $choice_option->maxanswers
;
554 echo $column[$optionid];
559 echo "</tr></table>";
566 function choice_delete_responses($attemptids) {
568 if(!is_array($attemptids) ||
empty($attemptids)) {
572 foreach($attemptids as $num => $attemptid) {
573 if(empty($attemptid)) {
574 unset($attemptids[$num]);
578 foreach($attemptids as $attemptid) {
579 if ($todelete = get_record('choice_answers', 'id', $attemptid)) {
580 delete_records('choice_answers', 'id', $attemptid);
587 function choice_delete_instance($id) {
588 // Given an ID of an instance of this module,
589 // this function will permanently delete the instance
590 // and any data that depends on it.
592 if (! $choice = get_record("choice", "id", "$id")) {
598 if (! delete_records("choice_answers", "choiceid", "$choice->id")) {
602 if (! delete_records("choice_options", "choiceid", "$choice->id")) {
606 if (! delete_records("choice", "id", "$choice->id")) {
613 function choice_get_participants($choiceid) {
614 //Returns the users with data in one choice
615 //(users with records in choice_responses, students)
620 $students = get_records_sql("SELECT DISTINCT u.id, u.id
621 FROM {$CFG->prefix}user u,
622 {$CFG->prefix}choice_answers a
623 WHERE a.choiceid = '$choiceid' and
626 //Return students array (it contains an array of unique users)
631 function choice_get_option_text($choice, $id) {
632 // Returns text string which is the answer that matches the id
633 if ($result = get_record("choice_options", "id", $id)) {
634 return $result->text
;
636 return get_string("notanswered", "choice");
640 function choice_get_choice($choiceid) {
641 // Gets a full choice record
643 if ($choice = get_record("choice", "id", $choiceid)) {
644 if ($options = get_records("choice_options", "choiceid", $choiceid, "id")) {
645 foreach ($options as $option) {
646 $choice->option
[$option->id
] = $option->text
;
647 $choice->maxanswers
[$option->id
] = $option->maxanswers
;
655 function choice_get_view_actions() {
656 return array('view','view all','report');
659 function choice_get_post_actions() {
660 return array('choose','choose again');