- Not Categorized is now the default view for category tab.
[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) {
ebc3bd2b 81 if ($answers = get_records_select("survey_answers", "survey='$survey->id' AND userid='$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)) {
607809b3 97 survey_print_graph("id=$mod->id&sid=$user->id&type=student.png");
66ea15f3 98 } else {
99 print_string("notdone", "survey");
100 }
101}
102
1b5910c4 103function survey_print_recent_activity($course, $isteacher, $timestart) {
3d891989 104 global $CFG;
54634efc 105
106 $content = false;
107 $surveys = NULL;
108
1b5910c4 109 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
110 "course = '$course->id' AND ".
111 "module = 'survey' AND ".
112 "action = 'submit' ", "time ASC")) {
113 return false;
114 }
115
116 foreach ($logs as $log) {
117 //Create a temp valid module structure (course,id)
118 $tempmod->course = $log->course;
119 $tempmod->id = $log->info;
120 //Obtain the visible property from the instance
121 $modvisible = instance_is_visible($log->module,$tempmod);
122
123 //Only if the mod is visible
124 if ($modvisible) {
125 $surveys[$log->id] = survey_log_info($log);
126 $surveys[$log->id]->time = $log->time;
127 $surveys[$log->id]->url = $log->url;
54634efc 128 }
129 }
130
131 if ($surveys) {
132 $content = true;
dcde9f02 133 $strftimerecent = get_string("strftimerecent");
54634efc 134 print_headline(get_string("newsurveyresponses", "survey").":");
135 foreach ($surveys as $survey) {
dcde9f02 136 $date = userdate($survey->time, $strftimerecent);
1b5910c4 137 echo "<p><font size=1>$date - $survey->firstname $survey->lastname<br>";
138 echo "\"<a href=\"$CFG->wwwroot/mod/survey/$survey->url\">";
54634efc 139 echo "$survey->name";
1b5910c4 140 echo "</a>\"</font></p>";
54634efc 141 }
142 }
143
144 return $content;
145}
04eba58f 146
66ea15f3 147
e323955d 148// SQL FUNCTIONS ////////////////////////////////////////////////////////
66ea15f3 149
f9903ed0 150
e323955d 151function survey_log_info($log) {
152 global $CFG;
153 return get_record_sql("SELECT s.name, u.firstname, u.lastname
154 FROM {$CFG->prefix}survey s,
155 {$CFG->prefix}user u
156 WHERE s.id = '$log->info'
ebc3bd2b 157 AND u.id = '$log->userid'");
e323955d 158}
f9903ed0 159
551b0b98 160function survey_get_responses($survey) {
e323955d 161 global $CFG;
734d2c98 162 return get_records_sql("SELECT MAX(a.time) as time,
163 count(*) as numanswers,
164 u.id, u.firstname, u.lastname
e323955d 165 FROM {$CFG->prefix}survey_answers AS a,
166 {$CFG->prefix}user AS u
167 WHERE a.answer1 <> '0' AND a.answer2 <> '0'
168 AND a.survey = $survey
ebc3bd2b 169 AND a.userid = u.id
734d2c98 170 GROUP BY u.id, u.firstname, u.lastname
171 ORDER BY time ASC");
e323955d 172}
173
174function survey_get_analysis($survey, $user) {
ebc3bd2b 175 global $CFG;
e323955d 176
177 return get_record_sql("SELECT notes
178 FROM {$CFG->prefix}survey_analysis
179 WHERE survey='$survey'
ebc3bd2b 180 AND userid='$user'");
e323955d 181}
182
183function survey_update_analysis($survey, $user, $notes) {
ebc3bd2b 184 global $CFG;
e323955d 185
ebc3bd2b 186 return execute_sql("UPDATE {$CFG->prefix}survey_analysis
e323955d 187 SET notes='$notes'
188 WHERE survey='$survey'
ebc3bd2b 189 AND userid='$user'");
e323955d 190}
191
192
e323955d 193function survey_get_user_answers($surveyid, $questionid) {
194 global $CFG;
195
196 return get_records_sql("SELECT sa.*,u.firstname,u.lastname,u.picture
ebc3bd2b 197 FROM {$CFG->prefix}survey_answers sa,
198 {$CFG->prefix}user u
e323955d 199 WHERE sa.survey = '$surveyid'
200 AND sa.question = $questionid
ebc3bd2b 201 AND u.id = sa.userid
e323955d 202 ORDER BY sa.answer1,sa.answer2 ASC");
203}
204
205// MODULE FUNCTIONS ////////////////////////////////////////////////////////
206
ebc3bd2b 207function survey_add_analysis($survey, $user, $notes) {
208 global $CFG;
209
210 $record->survey = $survey;
211 $record->userid = $user;
212 $record->notes = $notes;
213
214 return insert_record("survey_analysis", $record, false);
215}
216
e323955d 217function survey_already_done($survey, $user) {
ebc3bd2b 218 return record_exists("survey_answers", "survey", $survey, "userid", $user);
551b0b98 219}
f9903ed0 220
362f9c9c 221function survey_count_responses($survey) {
551b0b98 222 if ($responses = survey_get_responses($survey)) {
223 return count($responses);
224 } else {
225 return 0;
226 }
f9903ed0 227}
228
b416a1c3 229
551b0b98 230function survey_print_all_responses($survey, $results) {
b416a1c3 231 global $THEME;
232
dcde9f02 233 $dateformat = get_string("strftimedatetime");
234
b416a1c3 235 echo "<TABLE CELLPADDING=5 CELLSPACING=2 ALIGN=CENTER>";
236 echo "<TR><TD>Name<TD>Time<TD>Answered</TR>";
237
238 foreach ($results as $a) {
239
240 echo "<TR>";
241 echo "<TD><A HREF=\"report.php?action=student&student=$a->id&id=$survey\">$a->firstname $a->lastname</A></TD>";
dcde9f02 242 echo "<TD>".userdate($a->time, $dateformat)."</TD>";
b416a1c3 243 echo "<TD align=right>$a->numanswers</TD>";
244 echo "</TR>";
245 }
246 echo "</TABLE>";
247}
248
b416a1c3 249
551b0b98 250function survey_get_template_name($templateid) {
f9903ed0 251 global $db;
252
253 if ($templateid) {
e323955d 254 if ($ss = get_record("surveys", "id", $templateid)) {
255 return $ss->name;
f9903ed0 256 }
257 } else {
258 return "";
259 }
260}
261
0d22e7cc 262
f9903ed0 263
0e30c987 264function survey_shorten_name ($name, $numwords) {
265 $words = explode(" ", $name);
266 for ($i=0; $i < $numwords; $i++) {
267 $output .= $words[$i]." ";
268 }
269 return $output;
270}
f9903ed0 271
272
0e30c987 273
274function survey_print_multi($question) {
275 GLOBAL $db, $qnum, $checklist, $THEME;
276
277
98899fc0 278 $stripreferthat = get_string("ipreferthat", "survey");
279 $strifoundthat = get_string("ifoundthat", "survey");
0e30c987 280 echo "<P>&nbsp</P>\n";
281 echo "<P><FONT SIZE=4><B>$question->text</B></FONT></P>";
282
283 echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=1 BORDER=0>";
284
285 $options = explode( ",", $question->options);
286 $numoptions = count($options);
287
288 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
289 if ($question->type == 2) {
290 $P = "P";
291 } else {
292 $P = "";
293 }
294
295 if ($oneanswer) {
296 echo "<TR WIDTH=100% ><TD COLSPAN=2><P>$question->intro</P></TD>";
297 } else {
298 echo "<TR WIDTH=100% ><TD COLSPAN=3><P>$question->intro</P></TD>";
299 }
300
301 while (list ($key, $val) = each ($options)) {
302 echo "<TD width=10% ALIGN=CENTER><FONT SIZE=1><P>$val</P></FONT></TD>\n";
303 }
304 echo "<TD ALIGN=CENTER BGCOLOR=\"$THEME->body\">&nbsp</TD></TR>\n";
305
e323955d 306 $subquestions = get_records_list("survey_questions", "id", $question->multi);
0e30c987 307
308 foreach ($subquestions as $q) {
309 $qnum++;
310 $bgcolor = survey_question_color($qnum);
311
f762c448 312 if ($q->text) {
313 $q->text = get_string($q->text, "survey");
314 }
315
0e30c987 316 echo "<TR BGCOLOR=$bgcolor>";
317 if ($oneanswer) {
318 echo "<TD WIDTH=10 VALIGN=top><P><B>$qnum</B></P></TD>";
319 echo "<TD VALIGN=top><P>$q->text</P></TD>";
320 for ($i=1;$i<=$numoptions;$i++) {
321 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$P$q->id VALUE=$i></TD>";
322 }
323 echo "<TD BGCOLOR=white><INPUT TYPE=radio NAME=q$P$q->id VALUE=0 checked></TD>";
324 $checklist["q$P$q->id"] = $numoptions;
325
326 } else {
327 echo "<TD WIDTH=10 VALIGN=middle rowspan=2><P><B>$qnum</B></P></TD>";
98899fc0 328 echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$stripreferthat&nbsp;</FONT></P></TD>";
0e30c987 329 echo "<TD WIDTH=40% VALIGN=middle rowspan=2><P>$q->text</P></TD>";
330 for ($i=1;$i<=$numoptions;$i++) {
331 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=qP$q->id VALUE=$i></TD>";
332 }
333 echo "<TD BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=qP$q->id VALUE=0 checked></TD>";
334 echo "</TR>";
335
336 echo "<TR BGCOLOR=$bgcolor>";
98899fc0 337 echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$strifoundthat&nbsp;</P></TD>";
0e30c987 338 for ($i=1;$i<=$numoptions;$i++) {
339 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$q->id VALUE=$i></TD>";
340 }
341 echo "<TD WIDTH=5% BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=q$q->id VALUE=0 checked></TD>";
342 $checklist["qP$q->id"] = $numoptions;
343 $checklist["q$q->id"] = $numoptions;
344 }
345 echo "</TR>\n";
346 }
347 echo "</TABLE>";
348}
349
350
351
352function survey_print_single($question) {
353 GLOBAL $db, $qnum;
354
355 $bgcolor = survey_question_color(0);
356
357 $qnum++;
358
359 echo "<P>&nbsp</P>\n";
360 echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=0>\n";
361 echo "<TR BGCOLOR=$bgcolor>";
362 echo "<TD VALIGN=top><B>$qnum</B></TD>";
363 echo "<TD WIDTH=50% VALIGN=top><P>$question->text</P></TD>\n";
364 echo "<TD WIDTH=50% VALIGN=top><P><FONT SIZE=+1>\n";
365
366
367 if ($question->type == 0) { // Plain text field
368 echo "<TEXTAREA ROWS=3 COLS=30 WRAP=virtual NAME=\"$question->id\">$question->options</TEXTAREA>";
369
370 } else if ($question->type > 0) { // Choose one of a number
98899fc0 371 $strchoose = get_string("choose");
0e30c987 372 echo "<SELECT NAME=$question->id>";
98899fc0 373 echo "<OPTION VALUE=0 SELECTED>$strchoose...</OPTION>";
0e30c987 374 $options = explode( ",", $question->options);
375 foreach ($options as $key => $val) {
376 $key++;
377 echo "<OPTION VALUE=\"$key\">$val</OPTION>";
378 }
379 echo "</SELECT>";
380
381 } else if ($question->type < 0) { // Choose several of a number
382 $options = explode( ",", $question->options);
f762c448 383 notify("This question type not supported yet");
0e30c987 384 }
385
386 echo "</FONT></TD></TR></TABLE>";
387
388}
389
390function survey_question_color($qnum) {
391 global $THEME;
392
393 if ($qnum) {
394 return $qnum % 2 ? $THEME->cellcontent : $THEME->cellcontent2;
395 //return $qnum % 2 ? "#CCFFCC" : "#CCFFFF";
396 } else {
397 return $THEME->cellcontent;
398 }
399}
400
607809b3 401function survey_print_graph($url) {
402 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
403
404 if (empty($CFG->gdversion)) {
405 echo "(".get_string("gdneed").")";
406
407 } else {
408 echo "<IMG HEIGHT=\"$SURVEY_GHEIGHT\" WIDTH=\"$SURVEY_GWIDTH\" BORDER=1".
409 " SRC=\"$CFG->wwwroot/mod/survey/graph.php?$url\">";
410 }
411}
412
7be61a22 413function survey_get_participants($surveyid) {
414//Returns the users with data in one survey
415//(users with records in survey_analysis and survey_answers, students)
416
417 global $CFG;
418
419 //Get students from survey_analysis
420 $st_analysis = get_records_sql("SELECT DISTINCT u.*
421 FROM {$CFG->prefix}user u,
422 {$CFG->prefix}survey_analysis a
423 WHERE a.survey = '$surveyid' and
424 u.id = a.userid");
425 //Get students from survey_answers
426 $st_answers = get_records_sql("SELECT DISTINCT u.*
427 FROM {$CFG->prefix}user u,
428 {$CFG->prefix}survey_answers a
429 WHERE a.survey = '$surveyid' and
430 u.id = a.userid");
431
432 //Add st_answers to st_analysis
433 if ($st_answers) {
434 foreach ($st_answers as $st_answer) {
435 $st_analysis[$st_answer->id] = $st_answer;
436 }
437 }
438 //Return st_analysis array (it contains an array of unique users)
439 return ($st_analysis);
440}
441
f9903ed0 442?>