Fixed bug 1379 ....
[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;
7c8c335f 181 return get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture
e323955d 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
a9ccbf60 188function survey_get_responses($surveyid, $groupid) {
e323955d 189 global $CFG;
a9ccbf60 190
191 if ($groupid) {
840c1781 192 $groupsdb = ", {$CFG->prefix}groups_members AS gm";
a9ccbf60 193 $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid";
194 } else {
840c1781 195 $groupsdb = "";
a9ccbf60 196 $groupsql = "";
197 }
198
734d2c98 199 return get_records_sql("SELECT MAX(a.time) as time,
7c8c335f 200 u.id, u.firstname, u.lastname, u.picture
e323955d 201 FROM {$CFG->prefix}survey_answers AS a,
840c1781 202 {$CFG->prefix}user AS u $groupsdb
a9ccbf60 203 WHERE a.survey = $surveyid
204 AND a.userid = u.id $groupsql
734d2c98 205 GROUP BY u.id, u.firstname, u.lastname
206 ORDER BY time ASC");
e323955d 207}
208
209function survey_get_analysis($survey, $user) {
ebc3bd2b 210 global $CFG;
e323955d 211
212 return get_record_sql("SELECT notes
213 FROM {$CFG->prefix}survey_analysis
214 WHERE survey='$survey'
ebc3bd2b 215 AND userid='$user'");
e323955d 216}
217
218function survey_update_analysis($survey, $user, $notes) {
ebc3bd2b 219 global $CFG;
e323955d 220
ebc3bd2b 221 return execute_sql("UPDATE {$CFG->prefix}survey_analysis
e323955d 222 SET notes='$notes'
223 WHERE survey='$survey'
ebc3bd2b 224 AND userid='$user'");
e323955d 225}
226
227
a9ccbf60 228function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") {
e323955d 229 global $CFG;
230
a9ccbf60 231 if ($groupid) {
232 $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid";
233 } else {
234 $groupsql = "";
235 }
236
e323955d 237 return get_records_sql("SELECT sa.*,u.firstname,u.lastname,u.picture
ebc3bd2b 238 FROM {$CFG->prefix}survey_answers sa,
a9ccbf60 239 {$CFG->prefix}user u,
240 {$CFG->prefix}groups_members gm
e323955d 241 WHERE sa.survey = '$surveyid'
242 AND sa.question = $questionid
a9ccbf60 243 AND u.id = sa.userid $groupsql
7c8c335f 244 ORDER BY $sort");
245}
246
247function survey_get_user_answer($surveyid, $questionid, $userid) {
248 global $CFG;
249
250 return get_record_sql("SELECT sa.*
251 FROM {$CFG->prefix}survey_answers sa
252 WHERE sa.survey = '$surveyid'
253 AND sa.question = '$questionid'
254 AND sa.userid = '$userid'");
e323955d 255}
256
257// MODULE FUNCTIONS ////////////////////////////////////////////////////////
258
ebc3bd2b 259function survey_add_analysis($survey, $user, $notes) {
260 global $CFG;
261
262 $record->survey = $survey;
263 $record->userid = $user;
264 $record->notes = $notes;
265
266 return insert_record("survey_analysis", $record, false);
267}
268
e323955d 269function survey_already_done($survey, $user) {
ebc3bd2b 270 return record_exists("survey_answers", "survey", $survey, "userid", $user);
551b0b98 271}
f9903ed0 272
a9ccbf60 273function survey_count_responses($surveyid, $groupid) {
274 if ($responses = survey_get_responses($surveyid, $groupid)) {
551b0b98 275 return count($responses);
276 } else {
277 return 0;
278 }
f9903ed0 279}
280
b416a1c3 281
7c8c335f 282function survey_print_all_responses($cmid, $results, $courseid) {
b416a1c3 283 global $THEME;
284
a30f997e 285 $table->head = array ("", get_string("name"), get_string("time"));
286 $table->align = array ("", "left", "left");
287 $table->size = array (35, "", "" );
b416a1c3 288
289 foreach ($results as $a) {
7c8c335f 290 $table->data[] = array(print_user_picture($a->id, $courseid, $a->picture, false, true, false),
291 "<a href=\"report.php?action=student&student=$a->id&id=$cmid\">".fullname($a)."</a>",
a30f997e 292 userdate($a->time));
b416a1c3 293 }
7c8c335f 294
295 print_table($table);
b416a1c3 296}
297
b416a1c3 298
551b0b98 299function survey_get_template_name($templateid) {
f9903ed0 300 global $db;
301
302 if ($templateid) {
e323955d 303 if ($ss = get_record("surveys", "id", $templateid)) {
304 return $ss->name;
f9903ed0 305 }
306 } else {
307 return "";
308 }
309}
310
0d22e7cc 311
f9903ed0 312
0e30c987 313function survey_shorten_name ($name, $numwords) {
314 $words = explode(" ", $name);
315 for ($i=0; $i < $numwords; $i++) {
316 $output .= $words[$i]." ";
317 }
318 return $output;
319}
f9903ed0 320
321
0e30c987 322
323function survey_print_multi($question) {
324 GLOBAL $db, $qnum, $checklist, $THEME;
325
326
98899fc0 327 $stripreferthat = get_string("ipreferthat", "survey");
328 $strifoundthat = get_string("ifoundthat", "survey");
0e30c987 329 echo "<P>&nbsp</P>\n";
330 echo "<P><FONT SIZE=4><B>$question->text</B></FONT></P>";
331
332 echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=1 BORDER=0>";
333
334 $options = explode( ",", $question->options);
335 $numoptions = count($options);
336
337 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
338 if ($question->type == 2) {
339 $P = "P";
340 } else {
341 $P = "";
342 }
343
344 if ($oneanswer) {
345 echo "<TR WIDTH=100% ><TD COLSPAN=2><P>$question->intro</P></TD>";
346 } else {
347 echo "<TR WIDTH=100% ><TD COLSPAN=3><P>$question->intro</P></TD>";
348 }
349
350 while (list ($key, $val) = each ($options)) {
351 echo "<TD width=10% ALIGN=CENTER><FONT SIZE=1><P>$val</P></FONT></TD>\n";
352 }
353 echo "<TD ALIGN=CENTER BGCOLOR=\"$THEME->body\">&nbsp</TD></TR>\n";
354
e323955d 355 $subquestions = get_records_list("survey_questions", "id", $question->multi);
0e30c987 356
357 foreach ($subquestions as $q) {
358 $qnum++;
359 $bgcolor = survey_question_color($qnum);
360
f762c448 361 if ($q->text) {
362 $q->text = get_string($q->text, "survey");
363 }
364
0e30c987 365 echo "<TR BGCOLOR=$bgcolor>";
366 if ($oneanswer) {
367 echo "<TD WIDTH=10 VALIGN=top><P><B>$qnum</B></P></TD>";
368 echo "<TD VALIGN=top><P>$q->text</P></TD>";
369 for ($i=1;$i<=$numoptions;$i++) {
370 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$P$q->id VALUE=$i></TD>";
371 }
372 echo "<TD BGCOLOR=white><INPUT TYPE=radio NAME=q$P$q->id VALUE=0 checked></TD>";
373 $checklist["q$P$q->id"] = $numoptions;
374
375 } else {
376 echo "<TD WIDTH=10 VALIGN=middle rowspan=2><P><B>$qnum</B></P></TD>";
98899fc0 377 echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$stripreferthat&nbsp;</FONT></P></TD>";
0e30c987 378 echo "<TD WIDTH=40% VALIGN=middle rowspan=2><P>$q->text</P></TD>";
379 for ($i=1;$i<=$numoptions;$i++) {
380 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=qP$q->id VALUE=$i></TD>";
381 }
382 echo "<TD BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=qP$q->id VALUE=0 checked></TD>";
383 echo "</TR>";
384
385 echo "<TR BGCOLOR=$bgcolor>";
98899fc0 386 echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$strifoundthat&nbsp;</P></TD>";
0e30c987 387 for ($i=1;$i<=$numoptions;$i++) {
388 echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$q->id VALUE=$i></TD>";
389 }
390 echo "<TD WIDTH=5% BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=q$q->id VALUE=0 checked></TD>";
391 $checklist["qP$q->id"] = $numoptions;
392 $checklist["q$q->id"] = $numoptions;
393 }
394 echo "</TR>\n";
395 }
396 echo "</TABLE>";
397}
398
399
400
401function survey_print_single($question) {
402 GLOBAL $db, $qnum;
403
404 $bgcolor = survey_question_color(0);
405
406 $qnum++;
407
408 echo "<P>&nbsp</P>\n";
409 echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=0>\n";
410 echo "<TR BGCOLOR=$bgcolor>";
411 echo "<TD VALIGN=top><B>$qnum</B></TD>";
412 echo "<TD WIDTH=50% VALIGN=top><P>$question->text</P></TD>\n";
413 echo "<TD WIDTH=50% VALIGN=top><P><FONT SIZE=+1>\n";
414
415
416 if ($question->type == 0) { // Plain text field
417 echo "<TEXTAREA ROWS=3 COLS=30 WRAP=virtual NAME=\"$question->id\">$question->options</TEXTAREA>";
418
419 } else if ($question->type > 0) { // Choose one of a number
98899fc0 420 $strchoose = get_string("choose");
0e30c987 421 echo "<SELECT NAME=$question->id>";
98899fc0 422 echo "<OPTION VALUE=0 SELECTED>$strchoose...</OPTION>";
0e30c987 423 $options = explode( ",", $question->options);
424 foreach ($options as $key => $val) {
425 $key++;
426 echo "<OPTION VALUE=\"$key\">$val</OPTION>";
427 }
428 echo "</SELECT>";
429
430 } else if ($question->type < 0) { // Choose several of a number
431 $options = explode( ",", $question->options);
f762c448 432 notify("This question type not supported yet");
0e30c987 433 }
434
435 echo "</FONT></TD></TR></TABLE>";
436
437}
438
439function survey_question_color($qnum) {
440 global $THEME;
441
442 if ($qnum) {
443 return $qnum % 2 ? $THEME->cellcontent : $THEME->cellcontent2;
444 //return $qnum % 2 ? "#CCFFCC" : "#CCFFFF";
445 } else {
446 return $THEME->cellcontent;
447 }
448}
449
607809b3 450function survey_print_graph($url) {
451 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
452
453 if (empty($CFG->gdversion)) {
454 echo "(".get_string("gdneed").")";
455
456 } else {
e80a8db7 457 echo "<img height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\" border=\"1\"".
a9ccbf60 458 " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\">";
607809b3 459 }
460}
461
f9903ed0 462?>