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