Function name change ... hope it doesn't break any third-party code,
[moodle.git] / mod / choice / view.php
blobf03159366c24b2975f1f7c0ea8942e6eb79250d0
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_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()) {
30 $timenow = time();
32 if (empty($form->answer)) {
33 redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice'));
35 } else {
36 if ($current) {
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);
44 } else {
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");
56 exit;
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> ->";
66 } else {
67 $navigation = "";
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");
76 } else {
77 $currentgroup = false;
80 if (isteacher($course->id)) {
81 if ( $allanswers = get_records("choice_answers", "choice", $choice->id)) {
82 $responsecount = count($allanswers);
83 } else {
84 $responsecount = 0;
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) {
98 if ($answer) {
99 echo "<TD ALIGN=CENTER>";
100 echo "<INPUT type=radio name=answer value=\"$key\" ".$answerchecked[$key].">";
101 p($answer);
102 echo "</TD>";
106 echo "</TR></TABLE>";
107 echo "<INPUT type=hidden name=id value=\"$cm->id\">";
108 if (!isguest()) {
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"));
117 if ($currentgroup) {
118 $users = get_group_users($currentgroup, "u.firstname ASC");
119 } else {
120 $users = get_course_users($course->id, "u.firstname ASC");
123 if (!$users) {
124 print_heading(get_string("nousersyet"));
125 print_footer($course);
126 exit;
129 if ( $allanswers = get_records("choice_answers", "choice", $choice->id)) {
130 foreach ($allanswers as $aa) {
131 $answers[$aa->userid] = $aa;
133 } else {
134 $answers = array () ;
137 $timenow = time();
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;
146 } else {
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
155 ksort($useranswer);
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>";
165 echo "<tr>";
166 foreach ($useranswer as $key => $answer) {
167 if ($key) {
168 echo "<th width=\"$tablewidth%\">";
169 } else if ($choice->showunanswered) {
170 echo "<th bgcolor=\"$THEME->body\" width=\"$tablewidth%\">";
171 } else {
172 continue;
174 echo choice_get_answer($choice, $key);
175 echo "</th>";
177 echo "</tr><tr>";
179 foreach ($useranswer as $key => $answer) {
180 if ($key) {
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\">";
184 } else {
185 continue;
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>";
194 echo "</td></tr>";
196 echo "</table>";
198 echo "</td>";
200 echo "</tr></table>";
201 break;
204 case CHOICE_PUBLISH_ANONYMOUS:
205 $tablewidth = (int) (100.0 / count($useranswer));
207 echo "<table cellpadding=5 cellspacing=10 align=center>";
208 echo "<tr>";
209 foreach ($useranswer as $key => $answer) {
210 if ($key) {
211 echo "<th width=\"$tablewidth%\">";
212 } else if ($choice->showunanswered) {
213 echo "<th bgcolor=\"$THEME->body\" width=\"$tablewidth%\">";
214 } else {
215 continue;
217 echo choice_get_answer($choice, $key);
218 echo "</th>";
220 echo "</tr>";
222 $maxcolumn = 0;
223 foreach ($useranswer as $key => $answer) {
224 if (!$key and !$choice->showunanswered) {
225 continue;
227 $column[$key] = count($answer);
228 if ($column[$key] > $maxcolumn) {
229 $maxcolumn = $column[$key];
233 echo "<tr>";
234 foreach ($useranswer as $key => $answer) {
235 if (!$key and !$choice->showunanswered) {
236 continue;
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\">";
241 echo "</td>";
243 echo "</tr>";
245 echo "<tr>";
246 foreach ($useranswer as $key => $answer) {
247 if (!$key and !$choice->showunanswered) {
248 continue;
250 echo "<td align=\"center\">".$column[$key]."</td>";
252 echo "</tr></table>";
254 break;
258 print_footer($course);