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_login($course->id
);
18 if (!$choice = choice_get_choice($cm->instance
)) {
19 error("Course module is incorrect");
22 for ($i=1; $i <= $CHOICE_MAX_NUMBER; $i++
) {
23 $answerchecked[$i] = '';
25 if ($current = get_record("choice_answers", "choice", $choice->id
, "userid", $USER->id
)) {
26 $answerchecked[$current->answer
] = 'CHECKED';
29 if ($form = data_submitted()) {
32 if (empty($form->answer
)) {
33 redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice'));
37 $newanswer = $current;
38 $newanswer->answer
= $form->answer
;
39 $newanswer->timemodified
= $timenow;
40 if (! update_record("choice_answers", $newanswer)) {
41 error("Could not update your choice");
43 add_to_log($course->id
, "choice", "choose again", "view.php?id=$cm->id", $choice->id
, $cm->id
);
45 $newanswer->choice
= $choice->id
;
46 $newanswer->userid
= $USER->id
;
47 $newanswer->answer
= $form->answer
;
48 $newanswer->timemodified
= $timenow;
49 if (! insert_record("choice_answers", $newanswer)) {
50 error("Could not save your choice");
52 add_to_log($course->id
, "choice", "choose", "view.php?id=$cm->id", $choice->id
, $cm->id
);
55 redirect("$CFG->wwwroot/course/view.php?id=$course->id");
59 $strchoice = get_string("modulename", "choice");
60 $strchoices = get_string("modulenameplural", "choice");
62 add_to_log($course->id
, "choice", "view", "view.php?id=$cm->id", $choice->id
, $cm->id
);
64 if ($course->category
) {
65 $navigation = "<A HREF=\"../../course/view.php?id=$course->id\">$course->shortname</A> ->";
69 print_header("$course->shortname: $choice->name", "$course->fullname",
70 "$navigation <A HREF=index.php?id=$course->id>$strchoices</A> -> $choice->name", "", "", true,
71 update_module_button($cm->id
, $course->id
, $strchoice), navmenu($course, $cm));
73 /// Check to see if groups are being used in this choice
74 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
75 $currentgroup = setup_and_print_groups($course, $groupmode, "view.php?id=$cm->id");
77 $currentgroup = false;
80 if (isteacher($course->id
)) {
81 if ( $allanswers = get_records("choice_answers", "choice", $choice->id
)) {
82 $responsecount = count($allanswers);
86 echo "<P align=right><A HREF=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</A></P>";
87 } else if (!$cm->visible
) {
88 notice(get_string("activityiscurrentlyhidden"));
91 print_simple_box( text_to_html($choice->text
) , "center");
93 if (!$current or !$choice->publish
) { // They haven't made their choice yet
94 echo "<CENTER><P><FORM name=\"form\" method=\"post\" action=\"view.php\">";
95 echo "<TABLE CELLPADDING=20 CELLSPACING=20><TR>";
97 foreach ($choice->answer
as $key => $answer) {
99 echo "<TD ALIGN=CENTER>";
100 echo "<INPUT type=radio name=answer value=\"$key\" ".$answerchecked[$key].">";
106 echo "</TR></TABLE>";
107 echo "<INPUT type=hidden name=id value=\"$cm->id\">";
109 echo "<INPUT type=submit value=\"".get_string("savemychoice","choice")."\">";
111 echo "</P></FORM></CENTER>";
113 } else { // Print results.
115 print_heading(get_string("responses", "choice"));
118 $users = get_group_users($currentgroup, "u.firstname ASC");
120 $users = get_course_users($course->id
, "u.firstname ASC");
124 print_heading(get_string("nousersyet"));
125 print_footer($course);
129 if ( $allanswers = get_records("choice_answers", "choice", $choice->id
)) {
130 foreach ($allanswers as $aa) {
131 $answers[$aa->userid
] = $aa;
134 $answers = array () ;
139 foreach ($choice->answer
as $key => $answer) {
140 $useranswer[$key] = array();
142 foreach ($users as $user) {
143 if (!empty($user->id
) and !empty($answers[$user->id
])) {
144 $answer = $answers[$user->id
];
145 $useranswer[(int)$answer->answer
][] = $user;
147 $useranswer[0][] = $user;
150 foreach ($choice->answer
as $key => $answer) {
151 if (!$choice->answer
[$key]) {
152 unset($useranswer[$key]); // Throw away any data that doesn't apply
157 switch ($choice->publish
) {
158 case CHOICE_PUBLISH_NAMES
:
160 $isteacher = isteacher($course->id
);
162 $tablewidth = (int) (100.0 / count($useranswer));
164 echo "<table cellpadding=5 cellspacing=10 align=center>";
166 foreach ($useranswer as $key => $answer) {
168 echo "<th width=\"$tablewidth%\">";
169 } else if ($choice->showunanswered
) {
170 echo "<th bgcolor=\"$THEME->body\" width=\"$tablewidth%\">";
174 echo choice_get_answer($choice, $key);
179 foreach ($useranswer as $key => $answer) {
181 echo "<td width=\"$tablewidth%\" valign=top nowrap bgcolor=\"$theme->cellcontent\">";
182 } else if ($choice->showunanswered
) {
183 echo "<td width=\"$tablewidth%\" valign=top nowrap bgcolor=\"$theme->body\">";
188 echo "<table width=100%>";
189 foreach ($answer as $user) {
190 echo "<tr><td width=10 nowrap>";
191 print_user_picture($user->id
, $course->id
, $user->picture
);
192 echo "</td><td width=100% nowrap>";
193 echo "<p>".fullname($user, $isteacher)."</p>";
200 echo "</tr></table>";
204 case CHOICE_PUBLISH_ANONYMOUS
:
205 $tablewidth = (int) (100.0 / count($useranswer));
207 echo "<table cellpadding=5 cellspacing=10 align=center>";
209 foreach ($useranswer as $key => $answer) {
211 echo "<th width=\"$tablewidth%\">";
212 } else if ($choice->showunanswered
) {
213 echo "<th bgcolor=\"$THEME->body\" width=\"$tablewidth%\">";
217 echo choice_get_answer($choice, $key);
223 foreach ($useranswer as $key => $answer) {
224 if (!$key and !$choice->showunanswered
) {
227 $column[$key] = count($answer);
228 if ($column[$key] > $maxcolumn) {
229 $maxcolumn = $column[$key];
234 foreach ($useranswer as $key => $answer) {
235 if (!$key and !$choice->showunanswered
) {
238 $height = $COLUMN_HEIGHT * ((float)$column[$key] / (float)$maxcolumn);
239 echo "<td valign=\"bottom\" align=\"center\">";
240 echo "<img src=\"column.png\" height=\"$height\" width=\"49\">";
246 foreach ($useranswer as $key => $answer) {
247 if (!$key and !$choice->showunanswered
) {
250 echo "<td align=\"center\">".$column[$key]."</td>";
252 echo "</tr></table>";
258 print_footer($course);