Further SQL cleanups
[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
66ea15f3 17// STANDARD FUNCTIONS ////////////////////////////////////////////////////////
04eba58f 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
66ea15f3 80function survey_user_outline($course, $user, $mod, $survey) {
e323955d 81 if ($answers = get_records_select("survey_answers", "survey='$survey->id' AND user='$user->id'")) {
66ea15f3 82
83 $lastanswer = array_pop($answers);
84
85 $result->info = get_string("done", "survey");
86 $result->time = $lastanswer->time;
87 return $result;
88 }
89 return NULL;
90}
91
92
93function survey_user_complete($course, $user, $mod, $survey) {
94 global $CFG;
95
96 if (survey_already_done($survey->id, $user->id)) {
97 echo "<IMG SRC=\"$CFG->wwwroot/mod/survey/graph.php?id=$mod->id&sid=$user->id&type=student.png\">";
98 } else {
99 print_string("notdone", "survey");
100 }
101}
102
54634efc 103function survey_print_recent_activity(&$logs, $isteacher=false) {
104 global $CFG, $COURSE_TEACHER_COLOR;
105
106 $content = false;
107 $surveys = NULL;
108
109 foreach ($logs as $log) {
110 if ($log->module == "survey" and $log->action == "submit") {
e323955d 111 $surveys[$log->id] = survey_log_info($log);
7bca7957 112 $surveys[$log->id]->time = $log->time;
113 $surveys[$log->id]->url = $log->url;
54634efc 114 }
115 }
116
117 if ($surveys) {
118 $content = true;
119 print_headline(get_string("newsurveyresponses", "survey").":");
120 foreach ($surveys as $survey) {
62c13a2f 121 $date = userdate($survey->time, "%d %b, %H:%M");
54634efc 122 echo "<P><FONT SIZE=1>$date - $survey->firstname $survey->lastname<BR>";
123 echo "\"<A HREF=\"$CFG->wwwroot/mod/survey/$survey->url\">";
124 echo "$survey->name";
125 echo "</A>\"</FONT></P>";
126 }
127 }
128
129 return $content;
130}
04eba58f 131
66ea15f3 132
e323955d 133// SQL FUNCTIONS ////////////////////////////////////////////////////////
66ea15f3 134
f9903ed0 135
e323955d 136function survey_log_info($log) {
137 global $CFG;
138 return get_record_sql("SELECT s.name, u.firstname, u.lastname
139 FROM {$CFG->prefix}survey s,
140 {$CFG->prefix}user u
141 WHERE s.id = '$log->info'
142 AND u.id = '$log->user'");
143}
f9903ed0 144
551b0b98 145function survey_get_responses($survey) {
e323955d 146 global $CFG;
551b0b98 147 return get_records_sql("SELECT a.time as time, count(*) as numanswers, u.*
e323955d 148 FROM {$CFG->prefix}survey_answers AS a,
149 {$CFG->prefix}user AS u
150 WHERE a.answer1 <> '0' AND a.answer2 <> '0'
151 AND a.survey = $survey
152 AND a.user = u.id
153 GROUP BY a.user
154 ORDER BY a.time ASC");
155}
156
157function survey_get_analysis($survey, $user) {
158 global $db, $CFG;
159
160 return get_record_sql("SELECT notes
161 FROM {$CFG->prefix}survey_analysis
162 WHERE survey='$survey'
163 AND user='$user'");
164}
165
166function survey_update_analysis($survey, $user, $notes) {
167 global $db, $CFG;
168
169 return $db->Execute("UPDATE {$CFG->prefix}survey_analysis
170 SET notes='$notes'
171 WHERE survey='$survey'
172 AND user='$user'");
173}
174
175
176function survey_add_analysis($survey, $user, $notes) {
177 global $db, $CFG;
178
179 return $db->Execute("INSERT INTO {$CFG->prefix}survey_analysis
180 SET notes='$notes',
181 survey='$survey',
182 user='$user'");
183}
184
185function survey_get_user_answers($surveyid, $questionid) {
186 global $CFG;
187
188 return get_records_sql("SELECT sa.*,u.firstname,u.lastname,u.picture
189 FROM survey_answers sa,
190 user u
191 WHERE sa.survey = '$surveyid'
192 AND sa.question = $questionid
193 AND u.id = sa.user
194 ORDER BY sa.answer1,sa.answer2 ASC");
195}
196
197// MODULE FUNCTIONS ////////////////////////////////////////////////////////
198
199function survey_already_done($survey, $user) {
200 return record_exists("survey_answers", "survey", $survey, "user", $user);
551b0b98 201}
f9903ed0 202
362f9c9c 203function survey_count_responses($survey) {
551b0b98 204 if ($responses = survey_get_responses($survey)) {
205 return count($responses);
206 } else {
207 return 0;
208 }
f9903ed0 209}
210
b416a1c3 211
551b0b98 212function survey_print_all_responses($survey, $results) {
b416a1c3 213 global $THEME;
214
215 echo "<TABLE CELLPADDING=5 CELLSPACING=2 ALIGN=CENTER>";
216 echo "<TR><TD>Name<TD>Time<TD>Answered</TR>";
217
218 foreach ($results as $a) {
219
220 echo "<TR>";
221 echo "<TD><A HREF=\"report.php?action=student&student=$a->id&id=$survey\">$a->firstname $a->lastname</A></TD>";
62c13a2f 222 echo "<TD>".userdate($a->time, "%d %B %Y, %I:%M %p")."</TD>";
b416a1c3 223 echo "<TD align=right>$a->numanswers</TD>";
224 echo "</TR>";
225 }
226 echo "</TABLE>";
227}
228
b416a1c3 229
551b0b98 230function survey_get_template_name($templateid) {
f9903ed0 231 global $db;
232
233 if ($templateid) {
e323955d 234 if ($ss = get_record("surveys", "id", $templateid)) {
235 return $ss->name;
f9903ed0 236 }
237 } else {
238 return "";
239 }
240}
241
0d22e7cc 242
f9903ed0 243
0e30c987 244function survey_shorten_name ($name, $numwords) {
245 $words = explode(" ", $name);
246 for ($i=0; $i < $numwords; $i++) {
247 $output .= $words[$i]." ";
248 }
249 return $output;
250}
f9903ed0 251
252
0e30c987 253
254function survey_print_multi($question) {
255 GLOBAL $db, $qnum, $checklist, $THEME;
256
257
98899fc0 258 $stripreferthat = get_string("ipreferthat", "survey");
259 $strifoundthat = get_string("ifoundthat", "survey");
0e30c987 260 echo "<P>&nbsp</P>\n";
261 echo "<P><FONT SIZE=4><B>$question->text</B></FONT></P>";
262
263 echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=1 BORDER=0>";
264
265 $options = explode( ",", $question->options);
266 $numoptions = count($options);
267
268 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
269 if ($question->type == 2) {
270 $P = "P";
271 } else {
272 $P = "";
273 }
274
275 if ($oneanswer) {
276 echo "<TR WIDTH=100% ><TD COLSPAN=2><P>$question->intro</P></TD>";
277 } else {
278 echo "<TR WIDTH=100% ><TD COLSPAN=3><P>$question->intro</P></TD>";
279 }
280
281 while (list ($key, $val) = each ($options)) {
282 echo "<TD width=10% ALIGN=CENTER><FONT SIZE=1><P>$val</P></FONT></TD>\n";
283 }
284 echo "<TD ALIGN=CENTER BGCOLOR=\"$THEME->body\">&nbsp</TD></TR>\n";
285
e323955d 286 $subquestions = get_records_list("survey_questions", "id", $question->multi);
0e30c987 287
288 foreach ($subquestions as $q) {
289 $qnum++;
290 $bgcolor = survey_question_color($qnum);
291
f762c448 292 if ($q->text) {
293 $q->text = get_string($q->text, "survey");
294 }
295
0e30c987 296 echo "<TR BGCOLOR=$bgcolor>";
297 if ($oneanswer) {
298 echo "<TD WIDTH=10 VALIGN=top><P><B>$qnum</B></P></TD>";
299 echo "<TD VALIGN=top><P>$q->text</P></TD>";
300 for ($i=1;$i<=$numoptions;$i++) {
301 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$P$q->id VALUE=$i></TD>";
302 }
303 echo "<TD BGCOLOR=white><INPUT TYPE=radio NAME=q$P$q->id VALUE=0 checked></TD>";
304 $checklist["q$P$q->id"] = $numoptions;
305
306 } else {
307 echo "<TD WIDTH=10 VALIGN=middle rowspan=2><P><B>$qnum</B></P></TD>";
98899fc0 308 echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$stripreferthat&nbsp;</FONT></P></TD>";
0e30c987 309 echo "<TD WIDTH=40% VALIGN=middle rowspan=2><P>$q->text</P></TD>";
310 for ($i=1;$i<=$numoptions;$i++) {
311 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=qP$q->id VALUE=$i></TD>";
312 }
313 echo "<TD BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=qP$q->id VALUE=0 checked></TD>";
314 echo "</TR>";
315
316 echo "<TR BGCOLOR=$bgcolor>";
98899fc0 317 echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$strifoundthat&nbsp;</P></TD>";
0e30c987 318 for ($i=1;$i<=$numoptions;$i++) {
319 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$q->id VALUE=$i></TD>";
320 }
321 echo "<TD WIDTH=5% BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=q$q->id VALUE=0 checked></TD>";
322 $checklist["qP$q->id"] = $numoptions;
323 $checklist["q$q->id"] = $numoptions;
324 }
325 echo "</TR>\n";
326 }
327 echo "</TABLE>";
328}
329
330
331
332function survey_print_single($question) {
333 GLOBAL $db, $qnum;
334
335 $bgcolor = survey_question_color(0);
336
337 $qnum++;
338
339 echo "<P>&nbsp</P>\n";
340 echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=0>\n";
341 echo "<TR BGCOLOR=$bgcolor>";
342 echo "<TD VALIGN=top><B>$qnum</B></TD>";
343 echo "<TD WIDTH=50% VALIGN=top><P>$question->text</P></TD>\n";
344 echo "<TD WIDTH=50% VALIGN=top><P><FONT SIZE=+1>\n";
345
346
347 if ($question->type == 0) { // Plain text field
348 echo "<TEXTAREA ROWS=3 COLS=30 WRAP=virtual NAME=\"$question->id\">$question->options</TEXTAREA>";
349
350 } else if ($question->type > 0) { // Choose one of a number
98899fc0 351 $strchoose = get_string("choose");
0e30c987 352 echo "<SELECT NAME=$question->id>";
98899fc0 353 echo "<OPTION VALUE=0 SELECTED>$strchoose...</OPTION>";
0e30c987 354 $options = explode( ",", $question->options);
355 foreach ($options as $key => $val) {
356 $key++;
357 echo "<OPTION VALUE=\"$key\">$val</OPTION>";
358 }
359 echo "</SELECT>";
360
361 } else if ($question->type < 0) { // Choose several of a number
362 $options = explode( ",", $question->options);
f762c448 363 notify("This question type not supported yet");
0e30c987 364 }
365
366 echo "</FONT></TD></TR></TABLE>";
367
368}
369
370function survey_question_color($qnum) {
371 global $THEME;
372
373 if ($qnum) {
374 return $qnum % 2 ? $THEME->cellcontent : $THEME->cellcontent2;
375 //return $qnum % 2 ? "#CCFFCC" : "#CCFFFF";
376 } else {
377 return $THEME->cellcontent;
378 }
379}
380
f9903ed0 381?>