Fixed stupidity
[moodle.git] / mod / survey / lib.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3// Graph size
551b0b98 4$SURVEY_GHEIGHT = 500;
5$SURVEY_GWIDTH = 900;
f9903ed0 6
551b0b98 7$SURVEY_QTYPE = array (
f9903ed0 8 "-3" => "Virtual Actual and Preferred",
9 "-2" => "Virtual Preferred",
10 "-1" => "Virtual Actual",
11 "0" => "Text",
12 "1" => "Actual",
13 "2" => "Preferred",
14 "3" => "Actual and Preferred",
15 );
16
04eba58f 17// FUNCTIONS ////////////////////////////////////////////////////////
18
19function survey_add_instance($survey) {
20// Given an object containing all the necessary data,
21// (defined by the form in mod.html) this function
22// will create a new instance and return the id number
23// of the new instance.
24
25 if (!$template = get_record("survey", "id", $survey->template)) {
26 return 0;
27 }
28
29 $survey->questions = $template->questions;
30 $survey->timecreated = time();
31 $survey->timemodified = $survey->timecreated;
32
33 return insert_record("survey", $survey);
34
35}
36
37
38function survey_update_instance($survey) {
39// Given an object containing all the necessary data,
40// (defined by the form in mod.html) this function
41// will update an existing instance with new data.
42
43 if (!$template = get_record("survey", "id", $survey->template)) {
44 return 0;
45 }
46
47 $survey->id = $survey->instance;
48 $survey->questions = $template->questions;
49 $survey->timemodified = time();
50
51 return update_record("survey", $survey);
52}
53
54function survey_delete_instance($id) {
55// Given an ID of an instance of this module,
56// this function will permanently delete the instance
57// and any data that depends on it.
58
59 if (! $survey = get_record("survey", "id", "$id")) {
60 return false;
61 }
62
63 $result = true;
64
65 if (! delete_records("survey_analysis", "survey", "$survey->id")) {
66 $result = false;
67 }
68
69 if (! delete_records("survey_answers", "survey", "$survey->id")) {
70 $result = false;
71 }
72
73 if (! delete_records("survey", "id", "$survey->id")) {
74 $result = false;
75 }
76
77 return $result;
78}
79
54634efc 80function survey_print_recent_activity(&$logs, $isteacher=false) {
81 global $CFG, $COURSE_TEACHER_COLOR;
82
83 $content = false;
84 $surveys = NULL;
85
86 foreach ($logs as $log) {
87 if ($log->module == "survey" and $log->action == "submit") {
88 $surveys[$log->id] = get_record_sql("SELECT s.name, u.firstname, u.lastname
89 FROM survey s, user u
a30cfe16 90 WHERE s.id = '$log->info' AND u.id = '$log->user'");
7bca7957 91 $surveys[$log->id]->time = $log->time;
92 $surveys[$log->id]->url = $log->url;
54634efc 93 }
94 }
95
96 if ($surveys) {
97 $content = true;
98 print_headline(get_string("newsurveyresponses", "survey").":");
99 foreach ($surveys as $survey) {
100 $date = userdate($survey->time, "%e %b, %H:%M");
101 echo "<P><FONT SIZE=1>$date - $survey->firstname $survey->lastname<BR>";
102 echo "\"<A HREF=\"$CFG->wwwroot/mod/survey/$survey->url\">";
103 echo "$survey->name";
104 echo "</A>\"</FONT></P>";
105 }
106 }
107
108 return $content;
109}
04eba58f 110
f9903ed0 111function survey_already_done($survey, $user) {
112 return record_exists_sql("SELECT * FROM survey_answers WHERE survey='$survey' AND user='$user'");
113}
114
f9903ed0 115
551b0b98 116function survey_get_responses($survey) {
117 return get_records_sql("SELECT a.time as time, count(*) as numanswers, u.*
118 FROM survey_answers AS a, user AS u
119 WHERE a.answer1 <> '0' AND a.answer2 <> '0'
120 AND a.survey = $survey
121 AND a.user = u.id
122 GROUP BY a.user ORDER BY a.time ASC");
123}
f9903ed0 124
362f9c9c 125function survey_count_responses($survey) {
551b0b98 126 if ($responses = survey_get_responses($survey)) {
127 return count($responses);
128 } else {
129 return 0;
130 }
f9903ed0 131}
132
b416a1c3 133
551b0b98 134function survey_print_all_responses($survey, $results) {
b416a1c3 135 global $THEME;
136
137 echo "<TABLE CELLPADDING=5 CELLSPACING=2 ALIGN=CENTER>";
138 echo "<TR><TD>Name<TD>Time<TD>Answered</TR>";
139
140 foreach ($results as $a) {
141
142 echo "<TR>";
143 echo "<TD><A HREF=\"report.php?action=student&student=$a->id&id=$survey\">$a->firstname $a->lastname</A></TD>";
7a302afc 144 echo "<TD>".userdate($a->time, "%e %B %Y, %I:%M %p")."</TD>";
b416a1c3 145 echo "<TD align=right>$a->numanswers</TD>";
146 echo "</TR>";
147 }
148 echo "</TABLE>";
149}
150
b416a1c3 151
551b0b98 152function survey_get_template_name($templateid) {
f9903ed0 153 global $db;
154
155 if ($templateid) {
156 if ($ss = $db->Execute("SELECT name FROM surveys WHERE id = $templateid")) {
157 return $ss->fields["name"];
158 }
159 } else {
160 return "";
161 }
162}
163
0d22e7cc 164
551b0b98 165function survey_get_analysis($survey, $user) {
166 global $db;
167
168 return get_record_sql("SELECT notes from survey_analysis WHERE survey='$survey' and user='$user'");
169}
170
171function survey_update_analysis($survey, $user, $notes) {
f9903ed0 172 global $db;
173
174 return $db->Execute("UPDATE survey_analysis SET notes='$notes' WHERE survey='$survey' and user='$user'");
175}
176
0d22e7cc 177
551b0b98 178function survey_add_analysis($survey, $user, $notes) {
f9903ed0 179 global $db;
180
181 return $db->Execute("INSERT INTO survey_analysis SET notes='$notes', survey='$survey', user='$user'");
182}
183
0e30c987 184function survey_shorten_name ($name, $numwords) {
185 $words = explode(" ", $name);
186 for ($i=0; $i < $numwords; $i++) {
187 $output .= $words[$i]." ";
188 }
189 return $output;
190}
f9903ed0 191
0d22e7cc 192function survey_user_summary($course, $user, $mod, $survey) {
193 global $CFG;
194}
195
196
197function survey_user_outline($course, $user, $mod, $survey) {
198 if ($answers = get_records_sql("SELECT * FROM survey_answers WHERE survey='$survey->id' AND user='$user->id'")) {
199
200 $lastanswer = array_pop($answers);
201
98899fc0 202 $result->info = get_string("done", "survey");
0d22e7cc 203 $result->time = $lastanswer->time;
204 return $result;
0d22e7cc 205 }
206 return NULL;
207}
208
209
210function survey_user_complete($course, $user, $mod, $survey) {
3f4deb2b 211 global $CFG;
0d22e7cc 212
213 if (survey_already_done($survey->id, $user->id)) {
214 echo "<IMG SRC=\"$CFG->wwwroot/mod/survey/graph.php?id=$mod->id&sid=$user->id&type=student.png\">";
215 } else {
98899fc0 216 print_string("notdone", "survey");
0d22e7cc 217 }
218}
f9903ed0 219
0e30c987 220
221function survey_print_multi($question) {
222 GLOBAL $db, $qnum, $checklist, $THEME;
223
224
98899fc0 225 $stripreferthat = get_string("ipreferthat", "survey");
226 $strifoundthat = get_string("ifoundthat", "survey");
0e30c987 227 echo "<P>&nbsp</P>\n";
228 echo "<P><FONT SIZE=4><B>$question->text</B></FONT></P>";
229
230 echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=1 BORDER=0>";
231
232 $options = explode( ",", $question->options);
233 $numoptions = count($options);
234
235 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
236 if ($question->type == 2) {
237 $P = "P";
238 } else {
239 $P = "";
240 }
241
242 if ($oneanswer) {
243 echo "<TR WIDTH=100% ><TD COLSPAN=2><P>$question->intro</P></TD>";
244 } else {
245 echo "<TR WIDTH=100% ><TD COLSPAN=3><P>$question->intro</P></TD>";
246 }
247
248 while (list ($key, $val) = each ($options)) {
249 echo "<TD width=10% ALIGN=CENTER><FONT SIZE=1><P>$val</P></FONT></TD>\n";
250 }
251 echo "<TD ALIGN=CENTER BGCOLOR=\"$THEME->body\">&nbsp</TD></TR>\n";
252
253 $subquestions = get_records_sql("SELECT * FROM survey_questions WHERE id in ($question->multi) ");
254
255 foreach ($subquestions as $q) {
256 $qnum++;
257 $bgcolor = survey_question_color($qnum);
258
259 echo "<TR BGCOLOR=$bgcolor>";
260 if ($oneanswer) {
261 echo "<TD WIDTH=10 VALIGN=top><P><B>$qnum</B></P></TD>";
262 echo "<TD VALIGN=top><P>$q->text</P></TD>";
263 for ($i=1;$i<=$numoptions;$i++) {
264 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$P$q->id VALUE=$i></TD>";
265 }
266 echo "<TD BGCOLOR=white><INPUT TYPE=radio NAME=q$P$q->id VALUE=0 checked></TD>";
267 $checklist["q$P$q->id"] = $numoptions;
268
269 } else {
270 echo "<TD WIDTH=10 VALIGN=middle rowspan=2><P><B>$qnum</B></P></TD>";
98899fc0 271 echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$stripreferthat&nbsp;</FONT></P></TD>";
0e30c987 272 echo "<TD WIDTH=40% VALIGN=middle rowspan=2><P>$q->text</P></TD>";
273 for ($i=1;$i<=$numoptions;$i++) {
274 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=qP$q->id VALUE=$i></TD>";
275 }
276 echo "<TD BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=qP$q->id VALUE=0 checked></TD>";
277 echo "</TR>";
278
279 echo "<TR BGCOLOR=$bgcolor>";
98899fc0 280 echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$strifoundthat&nbsp;</P></TD>";
0e30c987 281 for ($i=1;$i<=$numoptions;$i++) {
282 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$q->id VALUE=$i></TD>";
283 }
284 echo "<TD WIDTH=5% BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=q$q->id VALUE=0 checked></TD>";
285 $checklist["qP$q->id"] = $numoptions;
286 $checklist["q$q->id"] = $numoptions;
287 }
288 echo "</TR>\n";
289 }
290 echo "</TABLE>";
291}
292
293
294
295function survey_print_single($question) {
296 GLOBAL $db, $qnum;
297
298 $bgcolor = survey_question_color(0);
299
300 $qnum++;
301
302 echo "<P>&nbsp</P>\n";
303 echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=0>\n";
304 echo "<TR BGCOLOR=$bgcolor>";
305 echo "<TD VALIGN=top><B>$qnum</B></TD>";
306 echo "<TD WIDTH=50% VALIGN=top><P>$question->text</P></TD>\n";
307 echo "<TD WIDTH=50% VALIGN=top><P><FONT SIZE=+1>\n";
308
309
310 if ($question->type == 0) { // Plain text field
311 echo "<TEXTAREA ROWS=3 COLS=30 WRAP=virtual NAME=\"$question->id\">$question->options</TEXTAREA>";
312
313 } else if ($question->type > 0) { // Choose one of a number
98899fc0 314 $strchoose = get_string("choose");
0e30c987 315 echo "<SELECT NAME=$question->id>";
98899fc0 316 echo "<OPTION VALUE=0 SELECTED>$strchoose...</OPTION>";
0e30c987 317 $options = explode( ",", $question->options);
318 foreach ($options as $key => $val) {
319 $key++;
320 echo "<OPTION VALUE=\"$key\">$val</OPTION>";
321 }
322 echo "</SELECT>";
323
324 } else if ($question->type < 0) { // Choose several of a number
325 $options = explode( ",", $question->options);
326 echo "<P>THIS TYPE OF QUESTION NOT SUPPORTED YET</P>";
327 }
328
329 echo "</FONT></TD></TR></TABLE>";
330
331}
332
333function survey_question_color($qnum) {
334 global $THEME;
335
336 if ($qnum) {
337 return $qnum % 2 ? $THEME->cellcontent : $THEME->cellcontent2;
338 //return $qnum % 2 ? "#CCFFCC" : "#CCFFFF";
339 } else {
340 return $THEME->cellcontent;
341 }
342}
343
f9903ed0 344?>