MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / choice / lib.php
blobdb3e505d4c64291b7d788b416d91c6d7ec20958b
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;
35 return NULL;
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");
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;
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];
74 $option->timemodified = time();
75 insert_record("choice_options", $option);
79 return $choice->id;
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);
114 } else {
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
130 $cdisplay = array();
132 $aid = 0;
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));
136 $countans = 0;
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;
145 if ($countanswers) {
146 $countanswers = count($countanswers);
147 } else {
148 $countanswers = 0;
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" ';
159 } else {
160 $cdisplay[$aid]->checked = '';
162 if ($choice->limitanswers && ($countans >= $maxans) && (empty($cdisplay[$aid]->checked)) ) {
163 $cdisplay[$aid]->disabled = ' disabled="disabled" ';
164 } else {
165 $cdisplay[$aid]->disabled = '';
167 $aid++;
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);
183 echo "</td>";
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');
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>";
216 echo "</td>";
217 echo "</tr>";
219 echo "</table>";
220 break;
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>";
232 } else {
233 print_string('havetologin', 'choice');
235 echo "</div>";
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);
243 if ($countanswers) {
244 $countans = 0;
245 foreach ($countanswers as $ca) { //only return enrolled users.
246 if (has_capability('mod/choice:choose', $context)) {
247 $countans = $countans+1;
251 $countanswers = $countans;
252 } else {
253 $countanswers = 0;
255 $maxans = $choice->maxanswers[$formanswer];
257 if (!($choice->limitanswers && ($countanswers >= $maxans) )) {
258 if ($current) {
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);
267 } else {
268 $newanswer = NULL;
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);
278 } else {
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)) {
290 $responsecount = 0;
291 foreach ($allanswers as $aa) {
292 if ($groupmode and $currentgroup) {
293 if (groups_is_member($currentgroup, $aa->userid)) {
294 $responsecount++;
296 } else {
297 $responsecount++;
300 } else {
301 $responsecount = 0;
303 echo '<div class="reportlink">';
304 echo "<a href=\"report.php?id=$cmid\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
305 echo '</div>';
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);
323 } else {
324 $currentgroup = 0;
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])) {
333 unset($users[$key]);
338 if (!$users) {
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;
350 } else {
351 $answers[$aa->userid] = $aa;
356 $timenow = time();
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;
366 } else {
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
376 ksort($useranswer);
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);">';
384 echo '<div>';
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\">";
390 echo "<tr>";
391 $count = 0;
392 $columncount = array(); // number of votes in each column
393 foreach ($useranswer as $optionid => $userlist) {
394 $columncount[$optionid] = 0; // init counters
395 if ($optionid) {
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\">";
399 } else {
400 continue;
402 echo format_string(choice_get_option_text($choice, $optionid));
403 echo "</th>";
404 $count++;
406 echo "</tr><tr>";
408 $count = 0;
409 foreach ($useranswer as $optionid => $userlist) {
410 if ($optionid) {
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%;\">";
414 } else {
415 continue;
418 // added empty row so that when the next iteration is empty,
419 // we do not get <table></table> erro from w3c validator
420 // MDL-7861
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;
425 echo "<tr>";
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&amp;course=$course->id\">";
433 echo fullname($user, has_capability('moodle/site:viewfullnames', $context));
434 echo "</a>";
435 echo "</td></tr>";
438 $count++;
439 echo "</table>";
441 echo "</td>";
443 echo "</tr><tr>";
444 $count = 0;
445 foreach ($useranswer as $optionid => $userlist) {
446 if (!$optionid and !$choice->showunanswered) {
447 continue;
449 echo "<td align=\"center\" class=\"count\">";
450 if ($choice->limitanswers && !$optionid==0) {
451 echo get_string("taken", "choice").":";
452 echo $columncount[$optionid];
453 echo "<br/>";
454 echo get_string("limit", "choice").":";
455 $choice_option = get_record("choice_options", "id", $optionid);
456 echo $choice_option->maxanswers;
458 echo "</td>";
459 $count++;
461 echo "</tr>";
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> ';
468 echo '&nbsp;&nbsp;';
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;">';
472 echo '<div>';
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>';
478 echo "</table>";
479 if (has_capability('mod/choice:readresponses', $context)) {
480 echo "</div></form></div>";
482 break;
485 case CHOICE_PUBLISH_ANONYMOUS:
487 $tablewidth = (int) (100.0 / count($useranswer));
489 echo "<table cellpadding=\"5\" cellspacing=\"0\" class=\"results anonymous\">";
490 echo "<tr>";
491 $count = 0;
492 foreach ($useranswer as $optionid => $userlist) {
493 if ($optionid) {
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\">";
497 } else {
498 continue;
500 echo format_string(choice_get_option_text($choice, $optionid));
501 echo "</th>";
502 $count++;
504 echo "</tr>";
506 $maxcolumn = 0;
507 foreach ($useranswer as $optionid => $userlist) {
508 if (!$optionid and !$choice->showunanswered) {
509 continue;
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];
522 echo "<tr>";
523 $count = 0;
524 foreach ($useranswer as $optionid => $userlist) {
525 if (!$optionid and !$choice->showunanswered) {
526 continue;
528 $height = 0;
529 if ($maxcolumn) {
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=\"\" />";
534 echo "</td>";
535 $count++;
537 echo "</tr>";
539 echo "<tr>";
540 $count = 0;
541 foreach ($useranswer as $optionid => $userlist) {
542 if (!$optionid and !$choice->showunanswered) {
543 continue;
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];
549 echo "<br/>";
550 echo get_string("limit", "choice").":";
551 $choice_option = get_record("choice_options", "id", $optionid);
552 echo $choice_option->maxanswers;
553 } else {
554 echo $column[$optionid];
556 echo "</td>";
557 $count++;
559 echo "</tr></table>";
561 break;
566 function choice_delete_responses($attemptids) {
568 if(!is_array($attemptids) || empty($attemptids)) {
569 return false;
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);
583 return true;
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")) {
593 return false;
596 $result = true;
598 if (! delete_records("choice_answers", "choiceid", "$choice->id")) {
599 $result = false;
602 if (! delete_records("choice_options", "choiceid", "$choice->id")) {
603 $result = false;
606 if (! delete_records("choice", "id", "$choice->id")) {
607 $result = false;
610 return $result;
613 function choice_get_participants($choiceid) {
614 //Returns the users with data in one choice
615 //(users with records in choice_responses, students)
617 global $CFG;
619 //Get 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
624 u.id = a.userid");
626 //Return students array (it contains an array of unique users)
627 return ($students);
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;
635 } else {
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;
649 return $choice;
652 return false;
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');