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