Incorrect variable name used for parameter.
[moodle-linuxchix.git] / mod / choice / view.php
blob34dc0ee88421c6de371ccb49ae26818c05743ca4
1 <?php // $Id$
3 require_once("../../config.php");
4 require_once("lib.php");
6 require_variable($id); // Course Module ID
8 if (! $cm = get_record("course_modules", "id", $id)) {
9 error("Course Module ID was incorrect");
12 if (! $course = get_record("course", "id", $cm->course)) {
13 error("Course is misconfigured");
16 require_course_login($course, false, $cm);
18 if (!$choice = choice_get_choice($cm->instance)) {
19 error("Course module is incorrect");
22 if ($choice->option) {
23 foreach ($choice->option as $optionid => $text) {
24 $answerchecked[$optionid] = '';
28 if (isset($USER->id) && $current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $USER->id)) {
29 $answerchecked[$current->optionid] = 'checked="checked"';
30 } else {
31 $current = false;
34 /// Submit any new data if there is any
36 if ($form = data_submitted()) {
37 $timenow = time();
39 if (empty($form->answer)) {
40 redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice'));
42 } else {
43 $countanswers = get_records("choice_answers", "optionid", $form->answer);
44 if ($countanswers) {
45 $countanswers = count($countanswers);
46 } else {
47 $countanswers = 0;
49 $maxans = $choice->maxanswers[$form->answer];
50 if (!($choice->limitanswers && ($countanswers >= $maxans) )) {
51 if ($current) {
52 $newanswer = $current;
53 $newanswer->optionid = $form->answer;
54 $newanswer->timemodified = $timenow;
55 if (! update_record("choice_answers", $newanswer)) {
56 error("Could not update your choice because of a database error");
58 add_to_log($course->id, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id);
59 } else {
60 $newanswer = NULL;
61 $newanswer->choiceid = $choice->id;
62 $newanswer->userid = $USER->id;
63 $newanswer->optionid = $form->answer;
64 $newanswer->timemodified = $timenow;
65 if (! insert_record("choice_answers", $newanswer)) {
66 error("Could not save your choice");
68 add_to_log($course->id, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id);
70 } else {
71 error("this choice is full!");
74 redirect("view.php?id=$cm->id");
75 exit;
79 /// Display the choice and possibly results
81 $strchoice = get_string("modulename", "choice");
82 $strchoices = get_string("modulenameplural", "choice");
84 add_to_log($course->id, "choice", "view", "view.php?id=$cm->id", $choice->id, $cm->id);
86 print_header_simple(format_string($choice->name), "",
87 "<a href=\"index.php?id=$course->id\">$strchoices</a> -> ".format_string($choice->name), "", "", true,
88 update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
90 /// Check to see if groups are being used in this choice
91 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
92 $currentgroup = setup_and_print_groups($course, $groupmode, "view.php?id=$cm->id");
93 } else {
94 $currentgroup = false;
97 if (isteacher($course->id)) {
98 if ( $allanswers = get_records("choice_answers", "choiceid", $choice->id)) {
99 $responsecount = 0;
100 foreach ($allanswers as $aa) {
101 if (isstudent($course->id, $aa->userid) or isteacher($course->id, $aa->userid)) { //check to make sure user is enrolled in course.
102 $responsecount++;
105 } else {
106 $responsecount = 0;
108 echo '<div class="reportlink">';
109 echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
110 echo '</div>';
111 } else if (!$cm->visible) {
112 notice(get_string("activityiscurrentlyhidden"));
115 if ($choice->text) {
116 print_simple_box(format_text($choice->text, $choice->format), 'center', '70%', '', 5, 'generalbox', 'intro');
119 //if user has already made a selection, and they are not allowed to update it, show their selected answer.
120 if (isset($USER->id) && ($current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $USER->id)) && !$choice->allowupdate) {
121 print_simple_box(get_string("yourselection", "choice", userdate($choice->timeopen)).": ".format_string(choice_get_option_text($choice, $current->optionid)), "center");
124 /// Print the form
126 if ($choice->timeopen > time() ) {
127 print_simple_box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "center");
128 print_footer($course);
129 exit;
132 if ( (!$current or $choice->allowupdate) and ($choice->timeclose >= time() or $choice->timeclose == 0) ) {
133 // They haven't made their choice yet or updates allowed and choice is open
135 echo "<form name=\"form\" method=\"post\" action=\"view.php\">";
137 switch ($choice->display) {
138 case CHOICE_DISPLAY_HORIZONTAL:
139 echo "<table cellpadding=\"20\" cellspacing=\"20\" align=\"center\"><tr>";
140 $aid = 0;
141 foreach ($choice->option as $optionid => $text) {
142 if ($text) {
143 echo "<td align=\"center\" valign=\"top\">";
144 echo "<input type=\"radio\" name=\"answer\" value=\"".$optionid."\" ".$answerchecked[$optionid]." alt=\"".strip_tags(format_text($text))."\" />";
145 $countanswers = (get_records("choice_answers", "optionid", $optionid));
146 if ($countanswers) {
147 $countanswers = count($countanswers);
148 } else {
149 $countanswers = 0;
151 $maxans = $choice->maxanswers[$optionid];
152 if ($choice->limitanswers && ($countanswers >= $maxans) ) {
153 if (!($answerchecked[$optionid])) {
154 echo "<script type=\"text/javascript\">";
155 echo "document.form.answer[".$aid."].disabled = true;";
156 echo "</script>";
158 echo format_text($text."<br><strong>".get_string('full', 'choice')."</strong>");
159 } else {
160 echo format_text($text);
163 echo "</td>";
164 $aid++;
167 echo "</tr>";
168 echo "</table>";
169 break;
171 case CHOICE_DISPLAY_VERTICAL:
172 $aid = 0;
173 $displayoptions->para = false;
174 echo "<table cellpadding=\"10\" cellspacing=\"10\" align=\"center\">";
175 foreach ($choice->option as $optionid => $text) {
176 if ($text) {
177 echo "<tr><td align=\"left\">";
178 echo "<input type=\"radio\" name=\"answer\" value=\"".$optionid."\" ".$answerchecked[$optionid]." alt=\"".strip_tags(format_text($text, FORMAT_MOODLE, $displayoptions))."\" />";
179 $countanswers = get_records("choice_answers", "optionid", $optionid);
180 if ($countanswers) {
181 $countanswers = count($countanswers);
182 } else {
183 $countanswers = 0;
185 $maxans = $choice->maxanswers[$optionid];
186 if ($choice->limitanswers && ($countanswers >= $maxans) ) {
187 if (!($answerchecked[$optionid])) {
188 echo "<script type=\"text/javascript\">";
189 echo "document.form.answer[".$aid."].disabled = true;";
190 echo "</script>";
193 echo format_text($text. ' ', FORMAT_MOODLE, $displayoptions);
195 if ($choice->limitanswers && ($choice->release==CHOICE_RELEASE_ALWAYS) ){
196 echo "</td><td>";
198 if ($maxans-$countanswers==0) {
199 echo get_string('full', 'choice');
200 } elseif ($maxans-$countanswers==1) {
201 echo ($maxans - $countanswers);
202 echo " ".get_string('spaceleft', 'choice');
203 } else {
204 echo ($maxans - $countanswers);
205 echo " ".get_string('spacesleft', 'choice');
207 echo "</td>";
208 } else if ($choice->limitanswers && ($countanswers >= $maxans)) {
209 echo " <strong>".get_string('full', 'choice')."</strong>";
212 echo "</td>";
213 echo "</tr>";
214 $aid++;
217 echo "</table>";
218 break;
221 echo "<center>";
222 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
223 if (isstudent($course->id) or isteacher($course->id, 0)) {
224 echo "<input type=\"submit\" value=\"".get_string("savemychoice","choice")."\" />";
225 } else {
226 print_string('havetologin', 'choice');
228 echo "</center>";
229 echo "</form>";
235 // print the results at the bottom of the screen
237 if ( $choice->release == CHOICE_RELEASE_ALWAYS or
238 ( $choice->release == CHOICE_RELEASE_AFTER_ANSWER and $current ) or
239 ( $choice->release == CHOICE_RELEASE_AFTER_CLOSE and $choice->timeclose <= time() ) ) {
241 print_heading(get_string("responses", "choice"));
243 if ($currentgroup) {
244 $users = get_group_users($currentgroup, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname') + get_admins();
245 } else {
246 $users = get_course_users($course->id, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname') + get_admins();
250 if (!$users) {
251 print_heading(get_string("nousersyet"));
252 print_footer($course);
253 exit;
256 if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
257 foreach ($allresponses as $aa) {
258 $answers[$aa->userid] = $aa;
260 } else {
261 $answers = array () ;
264 $timenow = time();
266 foreach ($choice->option as $optionid => $text) {
267 $useranswer[$optionid] = array();
269 foreach ($users as $user) {
270 if (!empty($user->id) and !empty($answers[$user->id])) {
271 $answer = $answers[$user->id];
272 $useranswer[(int)$answer->optionid][] = $user;
273 } else {
274 $useranswer[0][] = $user;
277 foreach ($choice->option as $optionid => $text) {
278 if (!$choice->option[$optionid]) {
279 unset($useranswer[$optionid]); // Throw away any data that doesn't apply
282 ksort($useranswer);
284 switch ($choice->publish) {
285 case CHOICE_PUBLISH_NAMES:
287 $isteacher = isteacher($course->id);
289 $tablewidth = (int) (100.0 / count($useranswer));
291 echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\" class=\"results names\">";
292 echo "<tr>";
293 $count = 0;
294 foreach ($useranswer as $optionid => $userlist) {
295 if ($optionid) {
296 echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
297 } else if ($choice->showunanswered) {
298 echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
299 } else {
300 continue;
302 echo format_string(choice_get_option_text($choice, $optionid));
303 echo "</th>";
304 $count++;
306 echo "</tr><tr>";
308 $count = 0;
309 foreach ($useranswer as $optionid => $userlist) {
310 if ($optionid) {
311 echo "<td class=\"col$count data\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
312 } else if ($choice->showunanswered) {
313 echo "<td class=\"col$count data\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
314 } else {
315 continue;
318 echo "<table width=\"100%\">";
319 foreach ($userlist as $user) {
320 if (!($optionid==0 && isadmin($user->id)) && !($optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) { //make sure admins and hidden teachers are not shown in not answered yet column.
321 echo "<tr><td width=\"10\" nowrap=\"nowrap\" class=\"picture\">";
322 print_user_picture($user->id, $course->id, $user->picture);
323 echo "</td><td width=\"100%\" nowrap=\"nowrap\" class=\"fullname\">";
324 echo fullname($user, $isteacher);
325 echo "</td></tr>";
328 $count++;
329 echo "</table>";
331 echo "</td>";
333 echo "</tr><tr>";
334 $count = 0;
335 foreach ($useranswer as $optionid => $userlist) {
336 if (!$optionid and !$choice->showunanswered) {
337 continue;
339 echo "<td align=\"center\" class=\"count\">";
340 $countanswers = count_records("choice_answers", "optionid", $optionid);
341 if ($choice->limitanswers && !$optionid==0) {
342 echo get_string("taken", "choice").":";
343 echo $countanswers;
344 echo "<br>";
345 echo get_string("limit", "choice").":";
346 $choice_option = get_record("choice_options", "id", $optionid);
347 echo $choice_option->maxanswers;
349 echo "</td>";
350 $count++;
356 echo "</tr></table>";
357 break;
360 case CHOICE_PUBLISH_ANONYMOUS:
361 $tablewidth = (int) (100.0 / count($useranswer));
363 echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\" class=\"results anonymous\">";
364 echo "<tr>";
365 $count = 0;
366 foreach ($useranswer as $optionid => $userlist) {
367 if ($optionid) {
368 echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
369 } else if ($choice->showunanswered) {
370 echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
371 } else {
372 continue;
374 echo format_string(choice_get_option_text($choice, $optionid));
375 echo "</th>";
376 $count++;
378 echo "</tr><tr>";
380 $maxcolumn = 0;
381 foreach ($useranswer as $optionid => $userlist) {
382 if (!$optionid and !$choice->showunanswered) {
383 continue;
385 $column[$optionid] = 0;
386 foreach ($userlist as $user) {
387 if (!($optionid==0 && isadmin($user->id)) && !($optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) { //make sure admins and hidden teachers are not shown in not answered yet column.
388 $column[$optionid]++;
391 if ($column[$optionid] > $maxcolumn) {
392 $maxcolumn = $column[$optionid];
396 echo "</tr><tr>";
397 $count = 0;
398 foreach ($useranswer as $optionid => $userlist) {
399 if (!$optionid and !$choice->showunanswered) {
400 continue;
402 $height = 0;
403 if ($maxcolumn) {
404 $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
406 echo "<td valign=\"bottom\" align=\"center\" class=\"col$count data\">";
407 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
408 echo "</td>";
409 $count++;
411 echo "</tr>";
413 echo "<tr>";
414 $count = 0;
415 foreach ($useranswer as $optionid => $userlist) {
416 if (!$optionid and !$choice->showunanswered) {
417 continue;
419 echo "<td align=\"center\" class=\"col$count count\">";
420 if ($choice->limitanswers && !$optionid==0) {
421 echo get_string("taken", "choice").":";
422 echo $column[$optionid];
423 echo "<br>";
424 echo get_string("limit", "choice").":";
425 $choice_option = get_record("choice_options", "id", $optionid);
426 echo $choice_option->maxanswers;
427 } else {
428 echo $column[$optionid];
430 echo "</td>";
431 $count++;
433 echo "</tr></table>";
435 break;
439 print_footer($course);