"MDL-17474, repository plugin, add check_login to construct function"
[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) {
407caeeb 30 global $DB;
31 if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $user->id))) {
7cfc54b9 32 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
d1a67362 33 $result->time = $answer->timemodified;
0d22e7cc 34 return $result;
35 }
36 return NULL;
37}
38
39
40function choice_user_complete($course, $user, $mod, $choice) {
407caeeb 41 global $DB;
42 if ($answer = $DB->get_record('choice_answers', array("choiceid" => $choice->id, "userid" => $user->id))) {
7cfc54b9 43 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
d1a67362 44 $result->time = $answer->timemodified;
7cfc54b9 45 echo get_string("answered", "choice").": $result->info. ".get_string("updated", '', userdate($result->time));
0d22e7cc 46 } else {
c4016bc1 47 print_string("notanswered", "choice");
0d22e7cc 48 }
49}
50
04eba58f 51
52function choice_add_instance($choice) {
c18269c7 53 global $DB;
ebd3c7ac 54// Given an object containing all the necessary data,
7cac0c4b 55// (defined by the form in mod_form.php) this function
ebd3c7ac 56// will create a new instance and return the id number
04eba58f 57// of the new instance.
58
59 $choice->timemodified = time();
60
ebd3c7ac 61 if (empty($choice->timerestrict)) {
d30772b2 62 $choice->timeopen = 0;
63 $choice->timeclose = 0;
64 }
65
ff8d654f 66 //insert answers
c18269c7 67 if ($choice->id = $DB->insert_record("choice", $choice)) {
ebd3c7ac 68 foreach ($choice->option as $key => $value) {
69 $value = trim($value);
70 if (isset($value) && $value <> '') {
71 $option = new object();
72 $option->text = $value;
73 $option->choiceid = $choice->id;
74 if (isset($choice->limit[$key])) {
75 $option->maxanswers = $choice->limit[$key];
a77a6009 76 }
ebd3c7ac 77 $option->timemodified = time();
c18269c7 78 $DB->insert_record("choice_options", $option);
6fd87e3b 79 }
80 }
81 }
a77a6009 82 return $choice->id;
04eba58f 83}
84
85
86function choice_update_instance($choice) {
c18269c7 87 global $DB;
ebd3c7ac 88// Given an object containing all the necessary data,
7cac0c4b 89// (defined by the form in mod_form.php) this function
04eba58f 90// will update an existing instance with new data.
91
92 $choice->id = $choice->instance;
93 $choice->timemodified = time();
94
d30772b2 95
ebd3c7ac 96 if (empty($choice->timerestrict)) {
d30772b2 97 $choice->timeopen = 0;
98 $choice->timeclose = 0;
99 }
ff8d654f 100
ebd3c7ac 101 //update, delete or insert answers
102 foreach ($choice->option as $key => $value) {
a77a6009 103 $value = trim($value);
ebd3c7ac 104 $option = new object();
105 $option->text = $value;
106 $option->choiceid = $choice->id;
107 if (isset($choice->limit[$key])) {
108 $option->maxanswers = $choice->limit[$key];
109 }
110 $option->timemodified = time();
a2e67a9a 111 if (isset($choice->optionid[$key]) && !empty($choice->optionid[$key])){//existing choice record
ebd3c7ac 112 $option->id=$choice->optionid[$key];
af834f3e 113 if (isset($value) && $value <> '') {
c18269c7 114 $DB->update_record("choice_options", $option);
b9135eca 115 } else { //empty old option - needs to be deleted.
c18269c7 116 $DB->delete_records("choice_options", array("id"=>$option->id));
c9ff813c 117 }
ebd3c7ac 118 } else {
119 if (isset($value) && $value <> '') {
c18269c7 120 $DB->insert_record("choice_options", $option);
a77a6009 121 }
ff8d654f 122 }
6fd87e3b 123 }
d30772b2 124
c18269c7 125 return $DB->update_record('choice', $choice);
ff8d654f 126
04eba58f 127}
128
a61e4188 129function choice_show_form($choice, $user, $cm, $allresponses) {
407caeeb 130 global $DB;
348630d8 131//$cdisplay is an array of the display info for a choice $cdisplay[$optionid]->text - text name of option.
132// ->maxanswers -maxanswers for this option
133// ->full - whether this option is full or not. 0=not full, 1=full
39b3e7de 134 $cdisplay = array();
348630d8 135
136 $aid = 0;
3c56a7cc 137 $choicefull = false;
a61e4188 138 $cdisplay = array();
3c56a7cc 139
140 if ($choice->limitanswers) { //set choicefull to true by default if limitanswers.
141 $choicefull = true;
142 }
a61e4188 143
3c56a7cc 144 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
145
348630d8 146 foreach ($choice->option as $optionid => $text) {
a5f35b9f 147 if (isset($text)) { //make sure there are no dud entries in the db with blank text values.
dabfd0ed 148 $cdisplay[$aid]->optionid = $optionid;
149 $cdisplay[$aid]->text = $text;
a61e4188 150 $cdisplay[$aid]->maxanswers = $choice->maxanswers[$optionid];
55963a3a 151 if (isset($allresponses[$optionid])) {
152 $cdisplay[$aid]->countanswers = count($allresponses[$optionid]);
153 } else {
154 $cdisplay[$aid]->countanswers = 0;
155 }
407caeeb 156 if ($current = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $user->id, 'optionid' => $optionid))) {
ff8d654f 157 $cdisplay[$aid]->checked = ' checked="checked" ';
dabfd0ed 158 } else {
ff8d654f 159 $cdisplay[$aid]->checked = '';
dabfd0ed 160 }
407caeeb 161 if ( $choice->limitanswers &&
162 ($cdisplay[$aid]->countanswers >= $cdisplay[$aid]->maxanswers) &&
a61e4188 163 (empty($cdisplay[$aid]->checked)) ) {
ff8d654f 164 $cdisplay[$aid]->disabled = ' disabled="disabled" ';
dabfd0ed 165 } else {
ebd3c7ac 166 $cdisplay[$aid]->disabled = '';
a61e4188 167 if ($choice->limitanswers && ($cdisplay[$aid]->countanswers < $cdisplay[$aid]->maxanswers)) {
3c56a7cc 168 $choicefull = false; //set $choicefull to false - as the above condition hasn't been set.
169 }
dabfd0ed 170 }
348630d8 171 $aid++;
172 }
173 }
174
39b3e7de 175 switch ($choice->display) {
176 case CHOICE_DISPLAY_HORIZONTAL:
d71bc1ab 177 echo "<table cellpadding=\"20\" cellspacing=\"20\" class=\"boxaligncenter\"><tr>";
ebd3c7ac 178
179 foreach ($cdisplay as $cd) {
39b3e7de 180 echo "<td align=\"center\" valign=\"top\">";
ff8d654f 181 echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";
ebd3c7ac 182 if (!empty($cd->disabled)) {
ff8d654f 183 echo format_text($cd->text."<br /><strong>".get_string('full', 'choice')."</strong>");
39b3e7de 184 } else {
185 echo format_text($cd->text);
348630d8 186 }
ff8d654f 187 echo "</td>";
39b3e7de 188 }
189 echo "</tr>";
190 echo "</table>";
191 break;
348630d8 192
39b3e7de 193 case CHOICE_DISPLAY_VERTICAL:
194 $displayoptions->para = false;
d71bc1ab 195 echo "<table cellpadding=\"10\" cellspacing=\"10\" class=\"boxaligncenter\">";
ff8d654f 196 foreach ($cdisplay as $cd) {
39b3e7de 197 echo "<tr><td align=\"left\">";
ff8d654f 198 echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";
348630d8 199
39b3e7de 200 echo format_text($cd->text. ' ', FORMAT_MOODLE, $displayoptions); //display text for option.
ff8d654f 201
39b3e7de 202 if ($choice->limitanswers && ($choice->showresults==CHOICE_SHOWRESULTS_ALWAYS) ){ //if limit is enabled, and show results always has been selected, display info beside each choice.
203 echo "</td><td>";
ff8d654f 204
39b3e7de 205 if (!empty($cd->disabled)) {
206 echo get_string('full', 'choice');
207 } elseif(!empty($cd->checked)) {
dabfd0ed 208 //currently do nothing - maybe some text could be added here to signfy that the choice has been 'selected'
39b3e7de 209 } elseif ($cd->maxanswers-$cd->countanswers==1) {
210 echo ($cd->maxanswers - $cd->countanswers);
211 echo " ".get_string('spaceleft', 'choice');
212 } else {
213 echo ($cd->maxanswers - $cd->countanswers);
214 echo " ".get_string('spacesleft', 'choice');
215 }
216 echo "</td>";
217 } else if ($choice->limitanswers && ($cd->countanswers >= $cd->maxanswers)) { //if limitanswers and answers exceeded, display "full" beside the choice.
218 echo " <strong>".get_string('full', 'choice')."</strong>";
348630d8 219 }
39b3e7de 220 echo "</td>";
221 echo "</tr>";
222 }
223 echo "</table>";
224 break;
225 }
226 //show save choice button
d71bc1ab 227 echo '<div class="button">';
39b3e7de 228 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
3c56a7cc 229 if (has_capability('mod/choice:choose', $context, $user->id, false)) { //don't show save button if the logged in user is the guest user.
230 if ($choicefull) {
231 print_string('choicefull', 'choice');
232 echo "</br>";
233 } else {
234 echo "<input type=\"submit\" value=\"".get_string("savemychoice","choice")."\" />";
235 }
407caeeb 236 if ($choice->allowupdate && $aaa = $DB->get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id)) {
41daad76 237 echo "<br /><a href='view.php?id=".$cm->id."&amp;action=delchoice'>".get_string("removemychoice","choice")."</a>";
243ac1fe 238 }
39b3e7de 239 } else {
240 print_string('havetologin', 'choice');
241 }
d71bc1ab 242 echo "</div>";
348630d8 243}
244
acaf7b86 245function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $cm) {
407caeeb 246 global $DB;
247 $current = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid));
dabfd0ed 248 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
c75ba879 249
250 $countanswers=0;
251 if($choice->limitanswers) {
252 // Find out whether groups are being used and enabled
253 if (groups_get_activity_groupmode($cm) > 0) {
254 $currentgroup = groups_get_activity_group($cm);
255 } else {
256 $currentgroup = 0;
257 }
258 if($currentgroup) {
259 // If groups are being used, retrieve responses only for users in
260 // current group
261 global $CFG;
407caeeb 262 $answers = $DB->get_records_sql("
263SELECT
c75ba879 264 ca.*
407caeeb 265FROM
266 {choice_answers} ca
267 INNER JOIN {groups_members} gm ON ca.userid=gm.userid
c75ba879 268WHERE
407caeeb 269 optionid=?
270 AND gm.groupid=?", array($formanswer, $currentgroup));
c75ba879 271 } else {
272 // Groups are not used, retrieve all answers for this option ID
407caeeb 273 $answers = $DB->get_records("choice_answers", array("optionid" => $formanswer));
ff8d654f 274 }
275
c75ba879 276 if ($answers) {
277 foreach ($answers as $a) { //only return enrolled users.
278 if (has_capability('mod/choice:choose', $context, $a->userid, false)) {
279 $countanswers++;
280 }
281 }
282 }
283 $maxans = $choice->maxanswers[$formanswer];
39b3e7de 284 }
ff8d654f 285
286 if (!($choice->limitanswers && ($countanswers >= $maxans) )) {
39b3e7de 287 if ($current) {
ebd3c7ac 288
39b3e7de 289 $newanswer = $current;
290 $newanswer->optionid = $formanswer;
291 $newanswer->timemodified = time();
407caeeb 292 if (! $DB->update_record("choice_answers", $newanswer)) {
c7281355 293 print_error('cannotupdatechoice', 'choice');
348630d8 294 }
39b3e7de 295 add_to_log($courseid, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id);
296 } else {
297 $newanswer = NULL;
298 $newanswer->choiceid = $choice->id;
299 $newanswer->userid = $userid;
300 $newanswer->optionid = $formanswer;
301 $newanswer->timemodified = time();
407caeeb 302 if (! $DB->insert_record("choice_answers", $newanswer)) {
c7281355 303 print_error('cannotsavechoice', 'choice');
39b3e7de 304 }
305 add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id);
306 }
307 } else {
308 if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error
c7281355 309 print_error('choicefull', 'choice');
39b3e7de 310 }
311 }
348630d8 312}
313
a61e4188 314function choice_show_reportlink($user, $cm) {
315 $responsecount =0;
316 foreach($user as $optionid => $userlist) {
317 if ($optionid) {
318 $responsecount += count($userlist);
319 }
7b360f33 320 }
5c880488 321
39b3e7de 322 echo '<div class="reportlink">';
5c880488 323 echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
39b3e7de 324 echo '</div>';
348630d8 325}
326
5d9d5227 327function choice_show_results($choice, $course, $cm, $allresponses, $forcepublish='') {
11e7b506 328 global $CFG, $COLUMN_HEIGHT, $FULLSCRIPT;
407caeeb 329
ff8d654f 330 print_heading(get_string("responses", "choice"));
39b3e7de 331 if (empty($forcepublish)) { //alow the publish setting to be overridden
332 $forcepublish = $choice->publish;
ff8d654f 333 }
334
9acb7700 335 if (empty($allresponses)) {
39b3e7de 336 print_heading(get_string("nousersyet"));
9acb7700 337 return false;
39b3e7de 338 }
c2a4d016 339
340 $totalresponsecount = 0;
5d9d5227 341 foreach ($allresponses as $optionid => $userlist) {
c2a4d016 342 if ($choice->showunanswered || $optionid) {
343 $totalresponsecount += count($userlist);
344 }
345 }
407caeeb 346
a61e4188 347 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
348630d8 348
a61e4188 349 $hascapfullnames = has_capability('moodle/site:viewfullnames', $context);
407caeeb 350
351 $viewresponses = has_capability('mod/choice:readresponses', $context);
39b3e7de 352 switch ($forcepublish) {
353 case CHOICE_PUBLISH_NAMES:
4ec64b4f 354 echo '<div id="tablecontainer">';
355 if ($viewresponses) {
11e7b506 356 echo '<form id="attemptsform" method="post" action="'.$FULLSCRIPT.'" onsubmit="var menu = document.getElementById(\'menuaction\'); return (menu.options[menu.selectedIndex].value == \'delete\' ? \''.addslashes_js(get_string('deleteattemptcheck','quiz')).'\' : true);">';
95aa949b 357 echo '<div>';
348630d8 358 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
359 echo '<input type="hidden" name="mode" value="overview" />';
4ec64b4f 360 }
ff8d654f 361
d71bc1ab 362 echo "<table cellpadding=\"5\" cellspacing=\"10\" class=\"results names\">";
348630d8 363 echo "<tr>";
407caeeb 364
aa4836f4 365 $columncount = array(); // number of votes in each column
a61e4188 366 if ($choice->showunanswered) {
367 $columncount[0] = 0;
368 echo "<th class=\"col0 header\" scope=\"col\">";
369 print_string('notanswered', 'choice');
370 echo "</th>";
371 }
372 $count = 1;
373 foreach ($choice->option as $optionid => $optiontext) {
aa4836f4 374 $columncount[$optionid] = 0; // init counters
a61e4188 375 echo "<th class=\"col$count header\" scope=\"col\">";
376 echo format_string($optiontext);
348630d8 377 echo "</th>";
378 $count++;
379 }
380 echo "</tr><tr>";
381
82ab7cb5 382 if ($choice->showunanswered) {
a61e4188 383 echo "<td class=\"col$count data\" >";
95aa949b 384 // added empty row so that when the next iteration is empty,
385 // we do not get <table></table> erro from w3c validator
386 // MDL-7861
387 echo "<table class=\"choiceresponse\"><tr><td></td></tr>";
82ab7cb5 388 if (!empty($allresponses[0])) {
389 foreach ($allresponses[0] as $user) {
390 echo "<tr>";
391 echo "<td class=\"picture\">";
392 print_user_picture($user->id, $course->id, $user->picture);
393 echo "</td><td class=\"fullname\">";
394 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
395 echo fullname($user, $hascapfullnames);
396 echo "</a>";
397 echo "</td></tr>";
398 }
348630d8 399 }
a61e4188 400 echo "</table></td>";
401 }
402 $count = 0;
403 foreach ($choice->option as $optionid => $optiontext) {
404 echo '<td class="col'.$count.' data" >';
405
406 // added empty row so that when the next iteration is empty,
407 // we do not get <table></table> erro from w3c validator
408 // MDL-7861
409 echo '<table class="choiceresponse"><tr><td></td></tr>';
5d9d5227 410 if (isset($allresponses[$optionid])) {
411 foreach ($allresponses[$optionid] as $user) {
a61e4188 412 $columncount[$optionid] += 1;
413 echo '<tr><td class="attemptcell">';
cb4db90d 414 if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
a61e4188 415 echo '<input type="checkbox" name="attemptid[]" value="'. $user->id. '" />';
416 }
417 echo '</td><td class="picture">';
418 print_user_picture($user->id, $course->id, $user->picture);
419 echo '</td><td class="fullname">';
420 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
421 echo fullname($user, $hascapfullnames);
422 echo '</a>';
423 echo '</td></tr>';
424 }
425 }
426 $count++;
427 echo '</table></td>';
348630d8 428 }
429 echo "</tr><tr>";
430 $count = 0;
407caeeb 431
a61e4188 432 if ($choice->showunanswered) {
433 echo "<td></td>";
434 }
407caeeb 435
a61e4188 436 foreach ($choice->option as $optionid => $optiontext) {
348630d8 437 echo "<td align=\"center\" class=\"count\">";
a61e4188 438 if ($choice->limitanswers) {
348630d8 439 echo get_string("taken", "choice").":";
aa4836f4 440 echo $columncount[$optionid];
71502268 441 echo "<br/>";
348630d8 442 echo get_string("limit", "choice").":";
407caeeb 443 $choice_option = $DB->get_record("choice_options", array("id" => $optionid));
348630d8 444 echo $choice_option->maxanswers;
19e559e4 445 } else {
a61e4188 446 if (isset($columncount[$optionid])) {
447 echo $columncount[$optionid];
448 }
348630d8 449 }
450 echo "</td>";
451 $count++;
452 }
3402a6f3 453 echo "</tr>";
407caeeb 454
39b3e7de 455 /// Print "Select all" etc.
cb4db90d 456 if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
3402a6f3 457 echo '<tr><td></td><td>';
39b3e7de 458 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
459 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
460 echo '&nbsp;&nbsp;';
461 $options = array('delete' => get_string('delete'));
462 echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
463 echo '<noscript id="noscriptmenuaction" style="display: inline;">';
95aa949b 464 echo '<div>';
465 echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
39b3e7de 466 echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
3402a6f3 467 echo '</td><td></td></tr>';
39b3e7de 468 }
407caeeb 469
4ec64b4f 470 echo "</table></div>";
a61e4188 471 if ($viewresponses) {
4ec64b4f 472 echo "</form></div>";
dabfd0ed 473 }
348630d8 474 break;
407caeeb 475
476
39b3e7de 477 case CHOICE_PUBLISH_ANONYMOUS:
ff8d654f 478
d71bc1ab 479 echo "<table cellpadding=\"5\" cellspacing=\"0\" class=\"results anonymous\">";
348630d8 480 echo "<tr>";
a61e4188 481 $maxcolumn = 0;
482 if ($choice->showunanswered) {
483 echo "<th class=\"col0 header\" scope=\"col\">";
484 print_string('notanswered', 'choice');
348630d8 485 echo "</th>";
a61e4188 486 $column[0] = 0;
5d9d5227 487 foreach ($allresponses[0] as $user) {
a61e4188 488 $column[0]++;
489 }
490 $maxcolumn = $column[0];
348630d8 491 }
a61e4188 492 $count = 1;
348630d8 493
a61e4188 494 foreach ($choice->option as $optionid => $optiontext) {
495 echo "<th class=\"col$count header\" scope=\"col\">";
496 echo format_string($optiontext);
497 echo "</th>";
407caeeb 498
348630d8 499 $column[$optionid] = 0;
5d9d5227 500 if (isset($allresponses[$optionid])) {
501 $column[$optionid] = count($allresponses[$optionid]);
a61e4188 502 if ($column[$optionid] > $maxcolumn) {
503 $maxcolumn = $column[$optionid];
504 }
505 } else {
506 $column[$optionid] = 0;
348630d8 507 }
508 }
a61e4188 509 echo "</tr><tr>";
348630d8 510
a61e4188 511 $height = 0;
512
513 if ($choice->showunanswered) {
514 if ($maxcolumn) {
515 $height = $COLUMN_HEIGHT * ((float)$column[0] / (float)$maxcolumn);
348630d8 516 }
a61e4188 517 echo "<td style=\"vertical-align:bottom\" align=\"center\" class=\"col0 data\">";
518 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
519 echo "</td>";
520 }
521 $count = 1;
522 foreach ($choice->option as $optionid => $optiontext) {
348630d8 523 if ($maxcolumn) {
524 $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
525 }
41daad76 526 echo "<td style=\"vertical-align:bottom\" align=\"center\" class=\"col$count data\">";
348630d8 527 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
528 echo "</td>";
529 $count++;
530 }
a61e4188 531 echo "</tr><tr>";
348630d8 532
a61e4188 533
534 if ($choice->showunanswered) {
c2a4d016 535 echo '<td align="center" class="col0 count">';
536 if (!$choice->limitanswers) {
537 echo $column[0];
538 echo '<br />('.format_float(((float)$column[0]/(float)$totalresponsecount)*100.0,1).'%)';
539 }
540 echo '</td>';
a61e4188 541 }
542 $count = 1;
543 foreach ($choice->option as $optionid => $optiontext) {
348630d8 544 echo "<td align=\"center\" class=\"col$count count\">";
a61e4188 545 if ($choice->limitanswers) {
348630d8 546 echo get_string("taken", "choice").":";
c2a4d016 547 echo $column[$optionid].'<br />';
348630d8 548 echo get_string("limit", "choice").":";
407caeeb 549 $choice_option = $DB->get_record("choice_options", array("id" => $optionid));
348630d8 550 echo $choice_option->maxanswers;
551 } else {
552 echo $column[$optionid];
c2a4d016 553 echo '<br />('.format_float(((float)$column[$optionid]/(float)$totalresponsecount)*100.0,1).'%)';
348630d8 554 }
555 echo "</td>";
556 $count++;
557 }
558 echo "</tr></table>";
407caeeb 559
a61e4188 560 break;
ebd3c7ac 561 }
348630d8 562}
563
564
a61e4188 565function choice_delete_responses($attemptids, $choiceid) {
407caeeb 566 global $DB;
dabfd0ed 567 if(!is_array($attemptids) || empty($attemptids)) {
348630d8 568 return false;
569 }
570
571 foreach($attemptids as $num => $attemptid) {
572 if(empty($attemptid)) {
573 unset($attemptids[$num]);
574 }
575 }
576
577 foreach($attemptids as $attemptid) {
407caeeb 578 if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choiceid, 'userid' => $attemptid))) {
579 $DB->delete_records('choice_answers', array('choiceid' => $choiceid, 'userid' => $attemptid));
348630d8 580 }
ebd3c7ac 581 }
348630d8 582 return true;
583}
584
04eba58f 585
586function choice_delete_instance($id) {
c18269c7 587 global $DB;
ebd3c7ac 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.
04eba58f 591
c18269c7 592 if (! $choice = $DB->get_record("choice", array("id"=>"$id"))) {
04eba58f 593 return false;
594 }
595
596 $result = true;
597
c18269c7 598 if (! $DB->delete_records("choice_answers", array("choiceid"=>"$choice->id"))) {
04eba58f 599 $result = false;
600 }
601
c18269c7 602 if (! $DB->delete_records("choice_options", array("choiceid"=>"$choice->id"))) {
04eba58f 603 $result = false;
604 }
a77a6009 605
c18269c7 606 if (! $DB->delete_records("choice", array("id"=>"$choice->id"))) {
6fd87e3b 607 $result = false;
ebd3c7ac 608 }
04eba58f 609
610 return $result;
611}
612
05855091 613function choice_get_participants($choiceid) {
614//Returns the users with data in one choice
6fd87e3b 615//(users with records in choice_responses, students)
05855091 616
585fb1f4 617 global $DB;
05855091 618
619 //Get students
407caeeb 620 $students = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
621 FROM {user} u,
622 {choice_answers} a
623 WHERE a.choiceid = ? AND
624 u.id = a.userid", array($choiceid));
05855091 625
626 //Return students array (it contains an array of unique users)
627 return ($students);
628}
629
04eba58f 630
a77a6009 631function choice_get_option_text($choice, $id) {
407caeeb 632 global $DB;
a77a6009 633// Returns text string which is the answer that matches the id
407caeeb 634 if ($result = $DB->get_record("choice_options", array("id" => $id))) {
a77a6009 635 return $result->text;
6fd87e3b 636 } else {
637 return get_string("notanswered", "choice");
ff8d654f 638 }
c4016bc1 639}
640
cd3fccff 641function choice_get_choice($choiceid) {
407caeeb 642 global $DB;
ff8d654f 643// Gets a full choice record
a77a6009 644
407caeeb 645 if ($choice = $DB->get_record("choice", array("id" => $choiceid))) {
646 if ($options = $DB->get_records("choice_options", array("choiceid" => $choiceid), "id")) {
ff8d654f 647 foreach ($options as $option) {
ebd3c7ac 648 $choice->option[$option->id] = $option->text;
c9ff813c 649 $choice->maxanswers[$option->id] = $option->maxanswers;
ff8d654f 650 }
a77a6009 651 return $choice;
652 }
653 }
654 return false;
cd3fccff 655}
656
f3221af9 657function choice_get_view_actions() {
658 return array('view','view all','report');
659}
660
661function choice_get_post_actions() {
662 return array('choose','choose again');
663}
664
0b5a80a1 665
666/**
667 * Implementation of the function for printing the form elements that control
668 * whether the course reset functionality affects the choice.
669 * @param $mform form passed by reference
670 */
671function choice_reset_course_form_definition(&$mform) {
672 $mform->addElement('header', 'choiceheader', get_string('modulenameplural', 'choice'));
673 $mform->addElement('advcheckbox', 'reset_choice', get_string('removeresponses','choice'));
674}
675
676/**
677 * Course reset form defaults.
678 */
679function choice_reset_course_form_defaults($course) {
680 return array('reset_choice'=>1);
681}
682
683/**
684 * Actual implementation of the rest coures functionality, delete all the
685 * choice responses for course $data->courseid.
686 * @param $data the data submitted from the reset course.
687 * @return array status array
688 */
689function choice_reset_userdata($data) {
407caeeb 690 global $CFG, $DB;
0b5a80a1 691
692 $componentstr = get_string('modulenameplural', 'choice');
693 $status = array();
694
695 if (!empty($data->reset_choice)) {
696 $choicessql = "SELECT ch.id
407caeeb 697 FROM {choice} ch
698 WHERE ch.course=?";
0b5a80a1 699
407caeeb 700 $DB->delete_records_select('choice_answers', "choiceid IN ($choicessql)", array($data->courseid));
0b5a80a1 701 $status[] = array('component'=>$componentstr, 'item'=>get_string('removeresponses', 'choice'), 'error'=>false);
702 }
703
704 /// updating dates - shift may be negative too
705 if ($data->timeshift) {
706 shift_course_mod_dates('choice', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
707 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
708 }
709
710 return $status;
711}
712
a61e4188 713function choice_get_response_data($choice, $cm, $groupmode) {
407caeeb 714 global $CFG, $USER, $DB;
a61e4188 715
716 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
717
718/// Get the current group
719 if ($groupmode > 0) {
720 $currentgroup = groups_get_activity_group($cm);
721 } else {
722 $currentgroup = 0;
723 }
724
5d9d5227 725/// Initialise the returned array, which is a matrix: $allresponses[responseid][userid] = responseobject
726 $allresponses = array();
a61e4188 727
728/// First get all the users who have access here
729/// To start with we assume they are all "unanswered" then move them later
4b5ee1bf 730 $allresponses[0] = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.lastname ASC,u.firstname ASC', '', '', $currentgroup, '', false, true);
a61e4188 731
732/// Get all the recorded responses for this choice
407caeeb 733 $rawresponses = $DB->get_records('choice_answers', array('choiceid' => $choice->id));
a61e4188 734
735/// Use the responses to move users into the correct column
736
5d9d5227 737 if ($rawresponses) {
738 foreach ($rawresponses as $response) {
739 if (isset($allresponses[0][$response->userid])) { // This person is enrolled and in correct group
740 $allresponses[0][$response->userid]->timemodified = $response->timemodified;
741 $allresponses[$response->optionid][$response->userid] = clone($allresponses[0][$response->userid]);
742 unset($allresponses[0][$response->userid]); // Remove from unanswered column
a61e4188 743 }
744 }
745 }
9acb7700 746 if (empty($allresponses[0])) {
747 unset($allresponses[0]);
748 }
5d9d5227 749 return $allresponses;
f432bebf 750}
a61e4188 751
f432bebf 752/**
753 * Returns all other caps used in module
754 */
db19ec01 755function choice_get_extra_capabilities() {
f432bebf 756 return array('moodle/site:accessallgroups');
a61e4188 757}
f432bebf 758
18a2a0cb 759/**
760 * @param string $feature FEATURE_xx constant for requested feature
761 * @return mixed True if module supports feature, null if doesn't know
762 */
763function choice_supports($feature) {
764 switch($feature) {
42f103be 765 case FEATURE_GROUPS: return true;
766 case FEATURE_GROUPINGS: return true;
767 case FEATURE_GROUPMEMBERSONLY: return true;
dc5c2bd9 768 case FEATURE_MOD_INTRO: return true;
18a2a0cb 769 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
42f103be 770 case FEATURE_GRADE_HAS_GRADE: return false;
771 case FEATURE_GRADE_OUTCOMES: return false;
772
18a2a0cb 773 default: return null;
774 }
775}
5a9726e7 776?>