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"';
34 /// Submit any new data if there is any
36 if ($form = data_submitted()) {
39 if (empty($form->answer
)) {
40 redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice'));
43 $countanswers = get_records("choice_answers", "optionid", $form->answer
);
45 $countanswers = count($countanswers);
49 $maxans = $choice->maxanswers
[$form->answer
];
50 if (!($choice->limitanswers
&& ($countanswers >= $maxans) )) {
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
);
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
);
71 error("this choice is full!");
74 redirect("view.php?id=$cm->id");
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");
94 $currentgroup = false;
97 if (isteacher($course->id
)) {
98 if ( $allanswers = get_records("choice_answers", "choiceid", $choice->id
)) {
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.
108 echo '<div class="reportlink">';
109 echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
111 } else if (!$cm->visible
) {
112 notice(get_string("activityiscurrentlyhidden"));
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");
126 if ($choice->timeopen
> time() ) {
127 print_simple_box(get_string("notopenyet", "choice", userdate($choice->timeopen
)), "center");
128 print_footer($course);
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>";
141 foreach ($choice->option
as $optionid => $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));
147 $countanswers = count($countanswers);
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;";
158 echo format_text($text."<br><strong>".get_string('full', 'choice')."</strong>");
160 echo format_text($text);
171 case CHOICE_DISPLAY_VERTICAL
:
173 $displayoptions->para
= false;
174 echo "<table cellpadding=\"10\" cellspacing=\"10\" align=\"center\">";
175 foreach ($choice->option
as $optionid => $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);
181 $countanswers = count($countanswers);
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;";
193 echo format_text($text. ' ', FORMAT_MOODLE
, $displayoptions);
195 if ($choice->limitanswers
&& ($choice->release
==CHOICE_RELEASE_ALWAYS
) ){
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');
204 echo ($maxans - $countanswers);
205 echo " ".get_string('spacesleft', 'choice');
208 } else if ($choice->limitanswers
&& ($countanswers >= $maxans)) {
209 echo " <strong>".get_string('full', 'choice')."</strong>";
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")."\" />";
226 print_string('havetologin', 'choice');
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"));
244 $users = get_group_users($currentgroup, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname') +
get_admins();
246 $users = get_course_users($course->id
, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname') +
get_admins();
251 print_heading(get_string("nousersyet"));
252 print_footer($course);
256 if ($allresponses = get_records("choice_answers", "choiceid", $choice->id
)) {
257 foreach ($allresponses as $aa) {
258 $answers[$aa->userid
] = $aa;
261 $answers = array () ;
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;
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
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\">";
294 foreach ($useranswer as $optionid => $userlist) {
296 echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
297 } else if ($choice->showunanswered
) {
298 echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
302 echo format_string(choice_get_option_text($choice, $optionid));
309 foreach ($useranswer as $optionid => $userlist) {
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\">";
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);
335 foreach ($useranswer as $optionid => $userlist) {
336 if (!$optionid and !$choice->showunanswered
) {
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").":";
345 echo get_string("limit", "choice").":";
346 $choice_option = get_record("choice_options", "id", $optionid);
347 echo $choice_option->maxanswers
;
356 echo "</tr></table>";
360 case CHOICE_PUBLISH_ANONYMOUS
:
361 $tablewidth = (int) (100.0 / count($useranswer));
363 echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\" class=\"results anonymous\">";
366 foreach ($useranswer as $optionid => $userlist) {
368 echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
369 } else if ($choice->showunanswered
) {
370 echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
374 echo format_string(choice_get_option_text($choice, $optionid));
381 foreach ($useranswer as $optionid => $userlist) {
382 if (!$optionid and !$choice->showunanswered
) {
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];
398 foreach ($useranswer as $optionid => $userlist) {
399 if (!$optionid and !$choice->showunanswered
) {
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=\"\" />";
415 foreach ($useranswer as $optionid => $userlist) {
416 if (!$optionid and !$choice->showunanswered
) {
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];
424 echo get_string("limit", "choice").":";
425 $choice_option = get_record("choice_options", "id", $optionid);
426 echo $choice_option->maxanswers
;
428 echo $column[$optionid];
433 echo "</tr></table>";
439 print_footer($course);