Possible fix to MDL-12093 : give window without heading and navigation special css...
[moodle.git] / mod / choice / lib.php
CommitLineData
1c61c8d6 1<?php // $Id$
0d22e7cc 2
6d6c9389 3$COLUMN_HEIGHT = 300;
4
8f7dc7f1 5define('CHOICE_PUBLISH_ANONYMOUS', '0');
6define('CHOICE_PUBLISH_NAMES', '1');
6d6c9389 7
c100a341 8define('CHOICE_SHOWRESULTS_NOT', '0');
9define('CHOICE_SHOWRESULTS_AFTER_ANSWER', '1');
10define('CHOICE_SHOWRESULTS_AFTER_CLOSE', '2');
11define('CHOICE_SHOWRESULTS_ALWAYS', '3');
f9af7fd0 12
6fd87e3b 13define('CHOICE_DISPLAY_HORIZONTAL', '0');
14define('CHOICE_DISPLAY_VERTICAL', '1');
15
8f7dc7f1 16$CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS => get_string('publishanonymous', 'choice'),
17 CHOICE_PUBLISH_NAMES => get_string('publishnames', 'choice'));
6d6c9389 18
c100a341 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'));
f9af7fd0 23
6fd87e3b 24$CHOICE_DISPLAY = array (CHOICE_DISPLAY_HORIZONTAL => get_string('displayhorizontal', 'choice'),
25 CHOICE_DISPLAY_VERTICAL => get_string('displayvertical','choice'));
6d6c9389 26
27/// Standard functions /////////////////////////////////////////////////////////
28
0d22e7cc 29function choice_user_outline($course, $user, $mod, $choice) {
d1a67362 30 if ($answer = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id)) {
7cfc54b9 31 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
d1a67362 32 $result->time = $answer->timemodified;
0d22e7cc 33 return $result;
34 }
35 return NULL;
36}
37
38
39function choice_user_complete($course, $user, $mod, $choice) {
d1a67362 40 if ($answer = get_record('choice_answers', "choiceid", $choice->id, "userid", $user->id)) {
7cfc54b9 41 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
d1a67362 42 $result->time = $answer->timemodified;
7cfc54b9 43 echo get_string("answered", "choice").": $result->info. ".get_string("updated", '', userdate($result->time));
0d22e7cc 44 } else {
c4016bc1 45 print_string("notanswered", "choice");
0d22e7cc 46 }
47}
48
04eba58f 49
50function choice_add_instance($choice) {
ebd3c7ac 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
04eba58f 54// of the new instance.
55
56 $choice->timemodified = time();
57
ebd3c7ac 58 if (empty($choice->timerestrict)) {
d30772b2 59 $choice->timeopen = 0;
60 $choice->timeclose = 0;
61 }
62
ff8d654f 63 //insert answers
a77a6009 64 if ($choice->id = insert_record("choice", $choice)) {
ebd3c7ac 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];
a77a6009 73 }
ebd3c7ac 74 $option->timemodified = time();
75 insert_record("choice_options", $option);
6fd87e3b 76 }
77 }
78 }
a77a6009 79 return $choice->id;
04eba58f 80}
81
82
83function choice_update_instance($choice) {
ebd3c7ac 84// Given an object containing all the necessary data,
85// (defined by the form in mod.html) this function
04eba58f 86// will update an existing instance with new data.
87
88 $choice->id = $choice->instance;
89 $choice->timemodified = time();
90
d30772b2 91
ebd3c7ac 92 if (empty($choice->timerestrict)) {
d30772b2 93 $choice->timeopen = 0;
94 $choice->timeclose = 0;
95 }
ff8d654f 96
ebd3c7ac 97 //update, delete or insert answers
98 foreach ($choice->option as $key => $value) {
a77a6009 99 $value = trim($value);
ebd3c7ac 100 $option = new object();
101 $option->text = $value;
102 $option->choiceid = $choice->id;
103 if (isset($choice->limit[$key])) {
104 $option->maxanswers = $choice->limit[$key];
105 }
106 $option->timemodified = time();
a2e67a9a 107 if (isset($choice->optionid[$key]) && !empty($choice->optionid[$key])){//existing choice record
ebd3c7ac 108 $option->id=$choice->optionid[$key];
af834f3e 109 if (isset($value) && $value <> '') {
c9ff813c 110 update_record("choice_options", $option);
b9135eca 111 } else { //empty old option - needs to be deleted.
ebd3c7ac 112 delete_records("choice_options", "id", $option->id);
c9ff813c 113 }
ebd3c7ac 114 } else {
115 if (isset($value) && $value <> '') {
ff8d654f 116 insert_record("choice_options", $option);
a77a6009 117 }
ff8d654f 118 }
6fd87e3b 119 }
d30772b2 120
a77a6009 121 return update_record('choice', $choice);
ff8d654f 122
04eba58f 123}
124
348630d8 125function choice_show_form($choice, $user, $cm) {
ebd3c7ac 126
348630d8 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
39b3e7de 130 $cdisplay = array();
348630d8 131
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));
ebd3c7ac 136 $countans = 0;
bbbf2d40 137 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
acaf7b86 138 if (!empty($countanswers)) {
ff8d654f 139 foreach ($countanswers as $ca) { //only return enrolled users.
97b294f8 140 if (has_capability('mod/choice:choose', $context, $ca->userid, false)) {
acaf7b86 141 $countans = $countans+1;
142 }
143 }
144 }
348630d8 145 if ($countanswers) {
146 $countanswers = count($countanswers);
147 } else {
148 $countanswers = 0;
149 }
150 $maxans = $choice->maxanswers[$optionid];
151
dabfd0ed 152 $cdisplay[$aid]->optionid = $optionid;
153 $cdisplay[$aid]->text = $text;
154 $cdisplay[$aid]->maxanswers = $maxans;
155 $cdisplay[$aid]->countanswers = $countans;
348630d8 156
dabfd0ed 157 if ($current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id, 'optionid', $optionid)) {
ff8d654f 158 $cdisplay[$aid]->checked = ' checked="checked" ';
dabfd0ed 159 } else {
ff8d654f 160 $cdisplay[$aid]->checked = '';
dabfd0ed 161 }
90ca2ce8 162 if ($choice->limitanswers && ($countans >= $maxans) && (empty($cdisplay[$aid]->checked)) ) {
ff8d654f 163 $cdisplay[$aid]->disabled = ' disabled="disabled" ';
dabfd0ed 164 } else {
ebd3c7ac 165 $cdisplay[$aid]->disabled = '';
dabfd0ed 166 }
348630d8 167 $aid++;
168 }
169 }
170
39b3e7de 171 switch ($choice->display) {
172 case CHOICE_DISPLAY_HORIZONTAL:
d71bc1ab 173 echo "<table cellpadding=\"20\" cellspacing=\"20\" class=\"boxaligncenter\"><tr>";
ebd3c7ac 174
175 foreach ($cdisplay as $cd) {
39b3e7de 176 echo "<td align=\"center\" valign=\"top\">";
ff8d654f 177 echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";
ebd3c7ac 178 if (!empty($cd->disabled)) {
ff8d654f 179 echo format_text($cd->text."<br /><strong>".get_string('full', 'choice')."</strong>");
39b3e7de 180 } else {
181 echo format_text($cd->text);
348630d8 182 }
ff8d654f 183 echo "</td>";
39b3e7de 184 }
185 echo "</tr>";
186 echo "</table>";
187 break;
348630d8 188
39b3e7de 189 case CHOICE_DISPLAY_VERTICAL:
190 $displayoptions->para = false;
d71bc1ab 191 echo "<table cellpadding=\"10\" cellspacing=\"10\" class=\"boxaligncenter\">";
ff8d654f 192 foreach ($cdisplay as $cd) {
39b3e7de 193 echo "<tr><td align=\"left\">";
ff8d654f 194 echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";
348630d8 195
39b3e7de 196 echo format_text($cd->text. ' ', FORMAT_MOODLE, $displayoptions); //display text for option.
ff8d654f 197
39b3e7de 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>";
ff8d654f 200
39b3e7de 201 if (!empty($cd->disabled)) {
202 echo get_string('full', 'choice');
203 } elseif(!empty($cd->checked)) {
dabfd0ed 204 //currently do nothing - maybe some text could be added here to signfy that the choice has been 'selected'
39b3e7de 205 } elseif ($cd->maxanswers-$cd->countanswers==1) {
206 echo ($cd->maxanswers - $cd->countanswers);
207 echo " ".get_string('spaceleft', 'choice');
208 } else {
209 echo ($cd->maxanswers - $cd->countanswers);
210 echo " ".get_string('spacesleft', 'choice');
211 }
212 echo "</td>";
213 } else if ($choice->limitanswers && ($cd->countanswers >= $cd->maxanswers)) { //if limitanswers and answers exceeded, display "full" beside the choice.
214 echo " <strong>".get_string('full', 'choice')."</strong>";
348630d8 215 }
39b3e7de 216 echo "</td>";
217 echo "</tr>";
218 }
219 echo "</table>";
220 break;
221 }
222 //show save choice button
d71bc1ab 223 echo '<div class="button">';
39b3e7de 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")."\" />";
243ac1fe 227
228 if ($choice->allowupdate && $aaa = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id)) {
41daad76 229 echo "<br /><a href='view.php?id=".$cm->id."&amp;action=delchoice'>".get_string("removemychoice","choice")."</a>";
243ac1fe 230 }
231
39b3e7de 232 } else {
233 print_string('havetologin', 'choice');
234 }
d71bc1ab 235 echo "</div>";
348630d8 236}
237
acaf7b86 238function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $cm) {
348630d8 239
39b3e7de 240 $current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $userid);
dabfd0ed 241 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
242 $countanswers = get_records("choice_answers", "optionid", $formanswer);
39b3e7de 243 if ($countanswers) {
244 $countans = 0;
245 foreach ($countanswers as $ca) { //only return enrolled users.
770778a5 246 if (has_capability('mod/choice:choose', $context, $ca->userid, false)) {
39b3e7de 247 $countans = $countans+1;
248 }
ff8d654f 249 }
250
251 $countanswers = $countans;
39b3e7de 252 } else {
253 $countanswers = 0;
254 }
255 $maxans = $choice->maxanswers[$formanswer];
ff8d654f 256
257 if (!($choice->limitanswers && ($countanswers >= $maxans) )) {
39b3e7de 258 if ($current) {
ebd3c7ac 259
39b3e7de 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");
348630d8 265 }
39b3e7de 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");
275 }
276 add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id);
277 }
278 } else {
279 if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error
ebd3c7ac 280 error("this choice is full!");
39b3e7de 281 }
282 }
348630d8 283}
284
285
08103c93
ML
286function 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)) {
39b3e7de 290 $responsecount = 0;
291 foreach ($allanswers as $aa) {
97b294f8 292 $context = get_context_instance(CONTEXT_MODULE, $cmid);
293 if (has_capability('mod/choice:choose', $context, $aa->userid, false)) { //check to make sure user is enrolled/has this capability.
294 if ($groupmode and $currentgroup) {
295 if (groups_is_member($currentgroup, $aa->userid)) {
296 $responsecount++;
297 }
298 } else {
08103c93
ML
299 $responsecount++;
300 }
348630d8 301 }
348630d8 302 }
39b3e7de 303 } else {
304 $responsecount = 0;
305 }
306 echo '<div class="reportlink">';
307 echo "<a href=\"report.php?id=$cmid\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
308 echo '</div>';
348630d8 309}
310
311function choice_show_results($choice, $course, $cm, $forcepublish='') {
ff8d654f 312
39b3e7de 313 global $CFG, $COLUMN_HEIGHT, $USER;
314 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
08103c93 315
ff8d654f 316 print_heading(get_string("responses", "choice"));
08103c93 317
39b3e7de 318 if (empty($forcepublish)) { //alow the publish setting to be overridden
319 $forcepublish = $choice->publish;
ff8d654f 320 }
321
6d5b7706 322 $groupmode = groups_get_activity_groupmode($cm);
2b85d965 323
324 if ($groupmode > 0) {
6d5b7706 325 $currentgroup = groups_get_activity_group($cm);
2b85d965 326 } else {
327 $currentgroup = 0;
328 }
348630d8 329
fb656c9f 330 $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC', '', '', $currentgroup, '', false, true);
348630d8 331
6d5b7706 332 if (!empty($CFG->enablegroupings) && !empty($cm->groupingid) && !empty($users)) {
333 $groupingusers = groups_get_grouping_members($cm->groupingid, 'u.id', 'u.id');
334 foreach($users as $key => $user) {
335 if (!isset($groupingusers[$user->id])) {
336 unset($users[$key]);
337 }
338 }
339 }
340
39b3e7de 341 if (!$users) {
342 print_heading(get_string("nousersyet"));
343 }
348630d8 344
08103c93 345 $answers = array () ;
39b3e7de 346 if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
347 foreach ($allresponses as $aa) {
08103c93
ML
348 //TODO: rewrite with SQL
349 if ($groupmode and $currentgroup) {
2c386f82 350 if (groups_is_member($currentgroup, $aa->userid)) {
08103c93
ML
351 $answers[$aa->userid] = $aa;
352 }
353 } else {
354 $answers[$aa->userid] = $aa;
355 }
348630d8 356 }
39b3e7de 357 }
348630d8 358
39b3e7de 359 $timenow = time();
348630d8 360
39b3e7de 361 foreach ($choice->option as $optionid => $text) {
362 $useranswer[$optionid] = array();
363 }
364 if (!empty($users)) {
365 foreach ($users as $user) {
366 if (!empty($user->id) and !empty($answers[$user->id])) {
367 $answer = $answers[$user->id];
368 $useranswer[(int)$answer->optionid][] = $user;
369 } else {
97b294f8 370 $usershownotans = get_users_by_capability($context, 'mod/choice:choose', 'u.id', 'u.id ASC', '', '', $currentgroup, '', false);
371 if ($user->id == $usershownotans[$user->id]->id) {
372 $useranswer[0][] = $user;
373 }
348630d8 374 }
375 }
39b3e7de 376 }
377 foreach ($choice->option as $optionid => $text) {
378 if (!$choice->option[$optionid]) {
379 unset($useranswer[$optionid]); // Throw away any data that doesn't apply
348630d8 380 }
39b3e7de 381 }
382 ksort($useranswer);
39b3e7de 383 switch ($forcepublish) {
384 case CHOICE_PUBLISH_NAMES:
348630d8 385
348630d8 386 $tablewidth = (int) (100.0 / count($useranswer));
0468976c 387 if (has_capability('mod/choice:readresponses', $context)) {
dabfd0ed 388 echo '<div id="tablecontainer">';
348630d8 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);">';
95aa949b 390 echo '<div>';
348630d8 391 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
392 echo '<input type="hidden" name="mode" value="overview" />';
393 }
ff8d654f 394
d71bc1ab 395 echo "<table cellpadding=\"5\" cellspacing=\"10\" class=\"results names\">";
348630d8 396 echo "<tr>";
397 $count = 0;
aa4836f4 398 $columncount = array(); // number of votes in each column
348630d8 399 foreach ($useranswer as $optionid => $userlist) {
aa4836f4 400 $columncount[$optionid] = 0; // init counters
348630d8 401 if ($optionid) {
d71bc1ab 402 echo "<th class=\"col$count header\" style=\"width:$tablewidth%\" scope=\"col\">";
348630d8 403 } else if ($choice->showunanswered) {
d71bc1ab 404 echo "<th class=\"col$count header\" style=\"width:$tablewidth%\" scope=\"col\">";
348630d8 405 } else {
406 continue;
407 }
408 echo format_string(choice_get_option_text($choice, $optionid));
409 echo "</th>";
410 $count++;
411 }
412 echo "</tr><tr>";
413
414 $count = 0;
415 foreach ($useranswer as $optionid => $userlist) {
416 if ($optionid) {
95aa949b 417 echo "<td class=\"col$count data\" style=\"width:$tablewidth%;\">";
348630d8 418 } else if ($choice->showunanswered) {
95aa949b 419 echo "<td class=\"col$count data\" style=\"width:$tablewidth%;\">";
348630d8 420 } else {
421 continue;
422 }
423
95aa949b 424 // added empty row so that when the next iteration is empty,
425 // we do not get <table></table> erro from w3c validator
426 // MDL-7861
427 echo "<table class=\"choiceresponse\"><tr><td></td></tr>";
348630d8 428 foreach ($userlist as $user) {
08103c93 429 if ($optionid!=0 or has_capability('mod/choice:choose', $context, $user->id, false)) {
aa4836f4 430 $columncount[$optionid] += 1;
348630d8 431 echo "<tr>";
39b3e7de 432 if (has_capability('mod/choice:readresponses', $context) && $optionid!=0) {
d71bc1ab 433 echo '<td class="attemptcell"><input type="checkbox" name="attemptid[]" value="'. $answers[$user->id]->id. '" /></td>';
348630d8 434 }
95aa949b 435 echo "<td class=\"picture\">";
348630d8 436 print_user_picture($user->id, $course->id, $user->picture);
95aa949b 437 echo "</td><td class=\"fullname\">";
348630d8 438 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
39b3e7de 439 echo fullname($user, has_capability('moodle/site:viewfullnames', $context));
348630d8 440 echo "</a>";
441 echo "</td></tr>";
442 }
443 }
444 $count++;
445 echo "</table>";
446
447 echo "</td>";
448 }
449 echo "</tr><tr>";
450 $count = 0;
451 foreach ($useranswer as $optionid => $userlist) {
452 if (!$optionid and !$choice->showunanswered) {
453 continue;
454 }
455 echo "<td align=\"center\" class=\"count\">";
348630d8 456 if ($choice->limitanswers && !$optionid==0) {
457 echo get_string("taken", "choice").":";
aa4836f4 458 echo $columncount[$optionid];
71502268 459 echo "<br/>";
348630d8 460 echo get_string("limit", "choice").":";
461 $choice_option = get_record("choice_options", "id", $optionid);
462 echo $choice_option->maxanswers;
19e559e4 463 } else {
464 echo $columncount[$optionid];
348630d8 465 }
466 echo "</td>";
467 $count++;
468 }
3402a6f3 469 echo "</tr>";
ebd3c7ac 470
39b3e7de 471 /// Print "Select all" etc.
472 if (has_capability('mod/choice:readresponses', $context)) {
3402a6f3 473 echo '<tr><td></td><td>';
39b3e7de 474 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
475 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
476 echo '&nbsp;&nbsp;';
477 $options = array('delete' => get_string('delete'));
478 echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
479 echo '<noscript id="noscriptmenuaction" style="display: inline;">';
95aa949b 480 echo '<div>';
481 echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
39b3e7de 482 echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
3402a6f3 483 echo '</td><td></td></tr>';
39b3e7de 484 }
ebd3c7ac 485
3402a6f3 486 echo "</table>";
0468976c 487 if (has_capability('mod/choice:readresponses', $context)) {
95aa949b 488 echo "</div></form></div>";
dabfd0ed 489 }
348630d8 490 break;
491
492
39b3e7de 493 case CHOICE_PUBLISH_ANONYMOUS:
ff8d654f 494
348630d8 495 $tablewidth = (int) (100.0 / count($useranswer));
496
d71bc1ab 497 echo "<table cellpadding=\"5\" cellspacing=\"0\" class=\"results anonymous\">";
348630d8 498 echo "<tr>";
499 $count = 0;
500 foreach ($useranswer as $optionid => $userlist) {
501 if ($optionid) {
d71bc1ab 502 echo "<th style=\"width:$tablewidth%\" class=\"col$count header\" scope=\"col\">";
348630d8 503 } else if ($choice->showunanswered) {
d71bc1ab 504 echo "<th style=\"width:$tablewidth%\" class=\"col$count header\" scope=\"col\">";
348630d8 505 } else {
506 continue;
507 }
508 echo format_string(choice_get_option_text($choice, $optionid));
509 echo "</th>";
510 $count++;
511 }
95aa949b 512 echo "</tr>";
348630d8 513
514 $maxcolumn = 0;
515 foreach ($useranswer as $optionid => $userlist) {
516 if (!$optionid and !$choice->showunanswered) {
517 continue;
518 }
519 $column[$optionid] = 0;
520 foreach ($userlist as $user) {
08103c93 521 if ($optionid!=0 or has_capability('mod/choice:choose', $context, $user->id, false)) {
348630d8 522 $column[$optionid]++;
523 }
524 }
525 if ($column[$optionid] > $maxcolumn) {
526 $maxcolumn = $column[$optionid];
527 }
528 }
529
95aa949b 530 echo "<tr>";
348630d8 531 $count = 0;
532 foreach ($useranswer as $optionid => $userlist) {
533 if (!$optionid and !$choice->showunanswered) {
534 continue;
535 }
536 $height = 0;
537 if ($maxcolumn) {
538 $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
539 }
41daad76 540 echo "<td style=\"vertical-align:bottom\" align=\"center\" class=\"col$count data\">";
348630d8 541 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
542 echo "</td>";
543 $count++;
544 }
545 echo "</tr>";
546
547 echo "<tr>";
548 $count = 0;
549 foreach ($useranswer as $optionid => $userlist) {
550 if (!$optionid and !$choice->showunanswered) {
551 continue;
552 }
553 echo "<td align=\"center\" class=\"col$count count\">";
554 if ($choice->limitanswers && !$optionid==0) {
555 echo get_string("taken", "choice").":";
556 echo $column[$optionid];
71502268 557 echo "<br/>";
348630d8 558 echo get_string("limit", "choice").":";
559 $choice_option = get_record("choice_options", "id", $optionid);
560 echo $choice_option->maxanswers;
561 } else {
562 echo $column[$optionid];
563 }
564 echo "</td>";
565 $count++;
566 }
567 echo "</tr></table>";
568
39b3e7de 569 break;
ebd3c7ac 570 }
348630d8 571}
572
573
574function choice_delete_responses($attemptids) {
ebd3c7ac 575
dabfd0ed 576 if(!is_array($attemptids) || empty($attemptids)) {
348630d8 577 return false;
578 }
579
580 foreach($attemptids as $num => $attemptid) {
581 if(empty($attemptid)) {
582 unset($attemptids[$num]);
583 }
584 }
585
586 foreach($attemptids as $attemptid) {
587 if ($todelete = get_record('choice_answers', 'id', $attemptid)) {
588 delete_records('choice_answers', 'id', $attemptid);
589 }
ebd3c7ac 590 }
348630d8 591 return true;
592}
593
04eba58f 594
595function choice_delete_instance($id) {
ebd3c7ac 596// Given an ID of an instance of this module,
597// this function will permanently delete the instance
598// and any data that depends on it.
04eba58f 599
600 if (! $choice = get_record("choice", "id", "$id")) {
601 return false;
602 }
603
604 $result = true;
605
a77a6009 606 if (! delete_records("choice_answers", "choiceid", "$choice->id")) {
04eba58f 607 $result = false;
608 }
609
a77a6009 610 if (! delete_records("choice_options", "choiceid", "$choice->id")) {
04eba58f 611 $result = false;
612 }
a77a6009 613
614 if (! delete_records("choice", "id", "$choice->id")) {
6fd87e3b 615 $result = false;
ebd3c7ac 616 }
04eba58f 617
618 return $result;
619}
620
05855091 621function choice_get_participants($choiceid) {
622//Returns the users with data in one choice
6fd87e3b 623//(users with records in choice_responses, students)
05855091 624
625 global $CFG;
626
627 //Get students
95e72c12 628 $students = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 629 FROM {$CFG->prefix}user u,
a77a6009 630 {$CFG->prefix}choice_answers a
631 WHERE a.choiceid = '$choiceid' and
632 u.id = a.userid");
05855091 633
634 //Return students array (it contains an array of unique users)
635 return ($students);
636}
637
04eba58f 638
a77a6009 639function choice_get_option_text($choice, $id) {
640// Returns text string which is the answer that matches the id
641 if ($result = get_record("choice_options", "id", $id)) {
642 return $result->text;
6fd87e3b 643 } else {
644 return get_string("notanswered", "choice");
ff8d654f 645 }
c4016bc1 646}
647
cd3fccff 648function choice_get_choice($choiceid) {
ff8d654f 649// Gets a full choice record
a77a6009 650
651 if ($choice = get_record("choice", "id", $choiceid)) {
652 if ($options = get_records("choice_options", "choiceid", $choiceid, "id")) {
ff8d654f 653 foreach ($options as $option) {
ebd3c7ac 654 $choice->option[$option->id] = $option->text;
c9ff813c 655 $choice->maxanswers[$option->id] = $option->maxanswers;
ff8d654f 656 }
a77a6009 657 return $choice;
658 }
659 }
660 return false;
cd3fccff 661}
662
f3221af9 663function choice_get_view_actions() {
664 return array('view','view all','report');
665}
666
667function choice_get_post_actions() {
668 return array('choose','choose again');
669}
670
5a9726e7 671?>