adding some strings
[moodle-linuxchix.git] / mod / choice / lib.php
blob75a4d5cd6588c8dd1b156c6312095c01eb40b988
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 (ismember($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 = groupmode($course, $cm);
320 $currentgroup = get_current_group($course->id);
322 $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC', '', '', $currentgroup, '', false);
324 if (!$users) {
325 print_heading(get_string("nousersyet"));
328 $answers = array () ;
329 if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
330 foreach ($allresponses as $aa) {
331 //TODO: rewrite with SQL
332 if ($groupmode and $currentgroup) {
333 if (ismember($currentgroup, $aa->userid)) {
334 $answers[$aa->userid] = $aa;
336 } else {
337 $answers[$aa->userid] = $aa;
342 $timenow = time();
344 foreach ($choice->option as $optionid => $text) {
345 $useranswer[$optionid] = array();
347 if (!empty($users)) {
348 foreach ($users as $user) {
349 if (!empty($user->id) and !empty($answers[$user->id])) {
350 $answer = $answers[$user->id];
351 $useranswer[(int)$answer->optionid][] = $user;
352 } else {
353 $useranswer[0][] = $user;
357 foreach ($choice->option as $optionid => $text) {
358 if (!$choice->option[$optionid]) {
359 unset($useranswer[$optionid]); // Throw away any data that doesn't apply
362 ksort($useranswer);
363 switch ($forcepublish) {
364 case CHOICE_PUBLISH_NAMES:
366 $tablewidth = (int) (100.0 / count($useranswer));
367 if (has_capability('mod/choice:readresponses', $context)) {
368 echo '<div id="tablecontainer">';
369 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);">';
370 echo '<div>';
371 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
372 echo '<input type="hidden" name="mode" value="overview" />';
375 echo "<table cellpadding=\"5\" cellspacing=\"10\" class=\"results names\">";
376 echo "<tr>";
377 $count = 0;
378 $columncount = array(); // number of votes in each column
379 foreach ($useranswer as $optionid => $userlist) {
380 $columncount[$optionid] = 0; // init counters
381 if ($optionid) {
382 echo "<th class=\"col$count header\" style=\"width:$tablewidth%\" scope=\"col\">";
383 } else if ($choice->showunanswered) {
384 echo "<th class=\"col$count header\" style=\"width:$tablewidth%\" scope=\"col\">";
385 } else {
386 continue;
388 echo format_string(choice_get_option_text($choice, $optionid));
389 echo "</th>";
390 $count++;
392 echo "</tr><tr>";
394 $count = 0;
395 foreach ($useranswer as $optionid => $userlist) {
396 if ($optionid) {
397 echo "<td class=\"col$count data\" style=\"width:$tablewidth%;\">";
398 } else if ($choice->showunanswered) {
399 echo "<td class=\"col$count data\" style=\"width:$tablewidth%;\">";
400 } else {
401 continue;
404 // added empty row so that when the next iteration is empty,
405 // we do not get <table></table> erro from w3c validator
406 // MDL-7861
407 echo "<table class=\"choiceresponse\"><tr><td></td></tr>";
408 foreach ($userlist as $user) {
409 if ($optionid!=0 or has_capability('mod/choice:choose', $context, $user->id, false)) {
410 $columncount[$optionid] += 1;
411 echo "<tr>";
412 if (has_capability('mod/choice:readresponses', $context) && $optionid!=0) {
413 echo '<td class="attemptcell"><input type="checkbox" name="attemptid[]" value="'. $answers[$user->id]->id. '" /></td>';
415 echo "<td class=\"picture\">";
416 print_user_picture($user->id, $course->id, $user->picture);
417 echo "</td><td class=\"fullname\">";
418 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
419 echo fullname($user, has_capability('moodle/site:viewfullnames', $context));
420 echo "</a>";
421 echo "</td></tr>";
424 $count++;
425 echo "</table>";
427 echo "</td>";
429 echo "</tr><tr>";
430 $count = 0;
431 foreach ($useranswer as $optionid => $userlist) {
432 if (!$optionid and !$choice->showunanswered) {
433 continue;
435 echo "<td align=\"center\" class=\"count\">";
436 if ($choice->limitanswers && !$optionid==0) {
437 echo get_string("taken", "choice").":";
438 echo $columncount[$optionid];
439 echo "<br/>";
440 echo get_string("limit", "choice").":";
441 $choice_option = get_record("choice_options", "id", $optionid);
442 echo $choice_option->maxanswers;
444 echo "</td>";
445 $count++;
447 echo "</tr>";
449 /// Print "Select all" etc.
450 if (has_capability('mod/choice:readresponses', $context)) {
451 echo '<tr><td></td><td>';
452 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
453 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
454 echo '&nbsp;&nbsp;';
455 $options = array('delete' => get_string('delete'));
456 echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
457 echo '<noscript id="noscriptmenuaction" style="display: inline;">';
458 echo '<div>';
459 echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
460 echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
461 echo '</td><td></td></tr>';
464 echo "</table>";
465 if (has_capability('mod/choice:readresponses', $context)) {
466 echo "</div></form></div>";
468 break;
471 case CHOICE_PUBLISH_ANONYMOUS:
473 $tablewidth = (int) (100.0 / count($useranswer));
475 echo "<table cellpadding=\"5\" cellspacing=\"0\" class=\"results anonymous\">";
476 echo "<tr>";
477 $count = 0;
478 foreach ($useranswer as $optionid => $userlist) {
479 if ($optionid) {
480 echo "<th style=\"width:$tablewidth%\" class=\"col$count header\" scope=\"col\">";
481 } else if ($choice->showunanswered) {
482 echo "<th style=\"width:$tablewidth%\" class=\"col$count header\" scope=\"col\">";
483 } else {
484 continue;
486 echo format_string(choice_get_option_text($choice, $optionid));
487 echo "</th>";
488 $count++;
490 echo "</tr>";
492 $maxcolumn = 0;
493 foreach ($useranswer as $optionid => $userlist) {
494 if (!$optionid and !$choice->showunanswered) {
495 continue;
497 $column[$optionid] = 0;
498 foreach ($userlist as $user) {
499 if ($optionid!=0 or has_capability('mod/choice:choose', $context, $user->id, false)) {
500 $column[$optionid]++;
503 if ($column[$optionid] > $maxcolumn) {
504 $maxcolumn = $column[$optionid];
508 echo "<tr>";
509 $count = 0;
510 foreach ($useranswer as $optionid => $userlist) {
511 if (!$optionid and !$choice->showunanswered) {
512 continue;
514 $height = 0;
515 if ($maxcolumn) {
516 $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
518 echo "<td valign=\"bottom\" align=\"center\" class=\"col$count data\">";
519 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
520 echo "</td>";
521 $count++;
523 echo "</tr>";
525 echo "<tr>";
526 $count = 0;
527 foreach ($useranswer as $optionid => $userlist) {
528 if (!$optionid and !$choice->showunanswered) {
529 continue;
531 echo "<td align=\"center\" class=\"col$count count\">";
532 if ($choice->limitanswers && !$optionid==0) {
533 echo get_string("taken", "choice").":";
534 echo $column[$optionid];
535 echo "<br/>";
536 echo get_string("limit", "choice").":";
537 $choice_option = get_record("choice_options", "id", $optionid);
538 echo $choice_option->maxanswers;
539 } else {
540 echo $column[$optionid];
542 echo "</td>";
543 $count++;
545 echo "</tr></table>";
547 break;
552 function choice_delete_responses($attemptids) {
554 if(!is_array($attemptids) || empty($attemptids)) {
555 return false;
558 foreach($attemptids as $num => $attemptid) {
559 if(empty($attemptid)) {
560 unset($attemptids[$num]);
564 foreach($attemptids as $attemptid) {
565 if ($todelete = get_record('choice_answers', 'id', $attemptid)) {
566 delete_records('choice_answers', 'id', $attemptid);
569 return true;
573 function choice_delete_instance($id) {
574 // Given an ID of an instance of this module,
575 // this function will permanently delete the instance
576 // and any data that depends on it.
578 if (! $choice = get_record("choice", "id", "$id")) {
579 return false;
582 $result = true;
584 if (! delete_records("choice_answers", "choiceid", "$choice->id")) {
585 $result = false;
588 if (! delete_records("choice_options", "choiceid", "$choice->id")) {
589 $result = false;
592 if (! delete_records("choice", "id", "$choice->id")) {
593 $result = false;
596 return $result;
599 function choice_get_participants($choiceid) {
600 //Returns the users with data in one choice
601 //(users with records in choice_responses, students)
603 global $CFG;
605 //Get students
606 $students = get_records_sql("SELECT DISTINCT u.id, u.id
607 FROM {$CFG->prefix}user u,
608 {$CFG->prefix}choice_answers a
609 WHERE a.choiceid = '$choiceid' and
610 u.id = a.userid");
612 //Return students array (it contains an array of unique users)
613 return ($students);
617 function choice_get_option_text($choice, $id) {
618 // Returns text string which is the answer that matches the id
619 if ($result = get_record("choice_options", "id", $id)) {
620 return $result->text;
621 } else {
622 return get_string("notanswered", "choice");
626 function choice_get_choice($choiceid) {
627 // Gets a full choice record
629 if ($choice = get_record("choice", "id", $choiceid)) {
630 if ($options = get_records("choice_options", "choiceid", $choiceid, "id")) {
631 foreach ($options as $option) {
632 $choice->option[$option->id] = $option->text;
633 $choice->maxanswers[$option->id] = $option->maxanswers;
635 return $choice;
638 return false;
641 function choice_get_view_actions() {
642 return array('view','view all','report');
645 function choice_get_post_actions() {
646 return array('choose','choose again');