More styles added as the THEME variable is removed from modules
[moodle.git] / mod / survey / lib.php
CommitLineData
d1290cec 1<?php // $Id$
f9903ed0 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
b6b3deb4 17
18define("SURVEY_COLLES_ACTUAL", "1");
19define("SURVEY_COLLES_PREFERRED", "2");
20define("SURVEY_COLLES_PREFERRED_ACTUAL", "3");
21define("SURVEY_ATTLS", "4");
22define("SURVEY_CIQ", "5");
23
24
66ea15f3 25// STANDARD FUNCTIONS ////////////////////////////////////////////////////////
04eba58f 26
27function survey_add_instance($survey) {
28// Given an object containing all the necessary data,
29// (defined by the form in mod.html) this function
30// will create a new instance and return the id number
31// of the new instance.
32
33 if (!$template = get_record("survey", "id", $survey->template)) {
34 return 0;
35 }
36
37 $survey->questions = $template->questions;
38 $survey->timecreated = time();
39 $survey->timemodified = $survey->timecreated;
40
41 return insert_record("survey", $survey);
42
43}
44
45
46function survey_update_instance($survey) {
47// Given an object containing all the necessary data,
48// (defined by the form in mod.html) this function
49// will update an existing instance with new data.
50
51 if (!$template = get_record("survey", "id", $survey->template)) {
52 return 0;
53 }
54
55 $survey->id = $survey->instance;
56 $survey->questions = $template->questions;
57 $survey->timemodified = time();
58
59 return update_record("survey", $survey);
60}
61
62function survey_delete_instance($id) {
63// Given an ID of an instance of this module,
64// this function will permanently delete the instance
65// and any data that depends on it.
66
67 if (! $survey = get_record("survey", "id", "$id")) {
68 return false;
69 }
70
71 $result = true;
72
73 if (! delete_records("survey_analysis", "survey", "$survey->id")) {
74 $result = false;
75 }
76
77 if (! delete_records("survey_answers", "survey", "$survey->id")) {
78 $result = false;
79 }
80
81 if (! delete_records("survey", "id", "$survey->id")) {
82 $result = false;
83 }
84
85 return $result;
86}
87
66ea15f3 88function survey_user_outline($course, $user, $mod, $survey) {
ebc3bd2b 89 if ($answers = get_records_select("survey_answers", "survey='$survey->id' AND userid='$user->id'")) {
66ea15f3 90
91 $lastanswer = array_pop($answers);
92
93 $result->info = get_string("done", "survey");
94 $result->time = $lastanswer->time;
95 return $result;
96 }
97 return NULL;
98}
99
100
101function survey_user_complete($course, $user, $mod, $survey) {
102 global $CFG;
103
104 if (survey_already_done($survey->id, $user->id)) {
b6b3deb4 105 if ($survey->template == SURVEY_CIQ) { // print out answers for critical incidents
106 $table = NULL;
107 $table->align = array("left", "left");
108
109 $questions = get_records_list("survey_questions", "id", $survey->questions);
110 $questionorder = explode(",", $survey->questions);
111
112 foreach ($questionorder as $key=>$val) {
113 $question = $questions[$val];
114 $questiontext = get_string($question->shorttext, "survey");
115
116 if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
117 $answertext = "$answer->answer1";
118 } else {
119 $answertext = "No answer";
120 }
121 $table->data[] = array("<b>$questiontext</b>", $answertext);
122 }
123 print_table($table);
124
125 } else {
126
839f2456 127 survey_print_graph("id=$mod->id&amp;sid=$user->id&amp;type=student.png");
b6b3deb4 128 }
129
66ea15f3 130 } else {
131 print_string("notdone", "survey");
132 }
133}
134
1b5910c4 135function survey_print_recent_activity($course, $isteacher, $timestart) {
3d891989 136 global $CFG;
54634efc 137
138 $content = false;
139 $surveys = NULL;
140
1b5910c4 141 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
142 "course = '$course->id' AND ".
143 "module = 'survey' AND ".
144 "action = 'submit' ", "time ASC")) {
145 return false;
146 }
147
148 foreach ($logs as $log) {
149 //Create a temp valid module structure (course,id)
150 $tempmod->course = $log->course;
151 $tempmod->id = $log->info;
152 //Obtain the visible property from the instance
153 $modvisible = instance_is_visible($log->module,$tempmod);
154
155 //Only if the mod is visible
156 if ($modvisible) {
157 $surveys[$log->id] = survey_log_info($log);
158 $surveys[$log->id]->time = $log->time;
cfa56cff 159 $surveys[$log->id]->url = str_replace('&', '&amp;', $log->url);
54634efc 160 }
161 }
162
163 if ($surveys) {
164 $content = true;
dcde9f02 165 $strftimerecent = get_string("strftimerecent");
54634efc 166 print_headline(get_string("newsurveyresponses", "survey").":");
167 foreach ($surveys as $survey) {
dcde9f02 168 $date = userdate($survey->time, $strftimerecent);
d1290cec 169 echo "<p><font size=\"1\">$date - ".fullname($survey)."<br />";
1b5910c4 170 echo "\"<a href=\"$CFG->wwwroot/mod/survey/$survey->url\">";
54634efc 171 echo "$survey->name";
1b5910c4 172 echo "</a>\"</font></p>";
54634efc 173 }
174 }
175
176 return $content;
177}
04eba58f 178
05855091 179function survey_get_participants($surveyid) {
180//Returns the users with data in one survey
181//(users with records in survey_analysis and survey_answers, students)
182
183 global $CFG;
184
185 //Get students from survey_analysis
186 $st_analysis = get_records_sql("SELECT DISTINCT u.*
187 FROM {$CFG->prefix}user u,
188 {$CFG->prefix}survey_analysis a
189 WHERE a.survey = '$surveyid' and
190 u.id = a.userid");
191 //Get students from survey_answers
192 $st_answers = get_records_sql("SELECT DISTINCT u.*
193 FROM {$CFG->prefix}user u,
194 {$CFG->prefix}survey_answers a
195 WHERE a.survey = '$surveyid' and
196 u.id = a.userid");
197
198 //Add st_answers to st_analysis
199 if ($st_answers) {
200 foreach ($st_answers as $st_answer) {
201 $st_analysis[$st_answer->id] = $st_answer;
202 }
203 }
204 //Return st_analysis array (it contains an array of unique users)
205 return ($st_analysis);
206}
66ea15f3 207
e323955d 208// SQL FUNCTIONS ////////////////////////////////////////////////////////
66ea15f3 209
f9903ed0 210
e323955d 211function survey_log_info($log) {
212 global $CFG;
7c8c335f 213 return get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture
e323955d 214 FROM {$CFG->prefix}survey s,
215 {$CFG->prefix}user u
216 WHERE s.id = '$log->info'
ebc3bd2b 217 AND u.id = '$log->userid'");
e323955d 218}
f9903ed0 219
a9ccbf60 220function survey_get_responses($surveyid, $groupid) {
e323955d 221 global $CFG;
a9ccbf60 222
223 if ($groupid) {
840c1781 224 $groupsdb = ", {$CFG->prefix}groups_members AS gm";
a9ccbf60 225 $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid";
226 } else {
840c1781 227 $groupsdb = "";
a9ccbf60 228 $groupsql = "";
229 }
230
734d2c98 231 return get_records_sql("SELECT MAX(a.time) as time,
7c8c335f 232 u.id, u.firstname, u.lastname, u.picture
e323955d 233 FROM {$CFG->prefix}survey_answers AS a,
840c1781 234 {$CFG->prefix}user AS u $groupsdb
a9ccbf60 235 WHERE a.survey = $surveyid
236 AND a.userid = u.id $groupsql
90c6ded2 237 GROUP BY u.id, u.firstname, u.lastname, u.picture
734d2c98 238 ORDER BY time ASC");
e323955d 239}
240
241function survey_get_analysis($survey, $user) {
ebc3bd2b 242 global $CFG;
e323955d 243
244 return get_record_sql("SELECT notes
245 FROM {$CFG->prefix}survey_analysis
246 WHERE survey='$survey'
ebc3bd2b 247 AND userid='$user'");
e323955d 248}
249
250function survey_update_analysis($survey, $user, $notes) {
ebc3bd2b 251 global $CFG;
e323955d 252
ebc3bd2b 253 return execute_sql("UPDATE {$CFG->prefix}survey_analysis
e323955d 254 SET notes='$notes'
255 WHERE survey='$survey'
ebc3bd2b 256 AND userid='$user'");
e323955d 257}
258
259
a9ccbf60 260function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") {
e323955d 261 global $CFG;
262
a9ccbf60 263 if ($groupid) {
264 $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid";
265 } else {
266 $groupsql = "";
267 }
268
e323955d 269 return get_records_sql("SELECT sa.*,u.firstname,u.lastname,u.picture
ebc3bd2b 270 FROM {$CFG->prefix}survey_answers sa,
a9ccbf60 271 {$CFG->prefix}user u,
272 {$CFG->prefix}groups_members gm
e323955d 273 WHERE sa.survey = '$surveyid'
274 AND sa.question = $questionid
a9ccbf60 275 AND u.id = sa.userid $groupsql
7c8c335f 276 ORDER BY $sort");
277}
278
279function survey_get_user_answer($surveyid, $questionid, $userid) {
280 global $CFG;
281
282 return get_record_sql("SELECT sa.*
283 FROM {$CFG->prefix}survey_answers sa
284 WHERE sa.survey = '$surveyid'
285 AND sa.question = '$questionid'
286 AND sa.userid = '$userid'");
e323955d 287}
288
289// MODULE FUNCTIONS ////////////////////////////////////////////////////////
290
ebc3bd2b 291function survey_add_analysis($survey, $user, $notes) {
292 global $CFG;
293
294 $record->survey = $survey;
295 $record->userid = $user;
296 $record->notes = $notes;
297
298 return insert_record("survey_analysis", $record, false);
299}
300
e323955d 301function survey_already_done($survey, $user) {
ebc3bd2b 302 return record_exists("survey_answers", "survey", $survey, "userid", $user);
551b0b98 303}
f9903ed0 304
a9ccbf60 305function survey_count_responses($surveyid, $groupid) {
306 if ($responses = survey_get_responses($surveyid, $groupid)) {
551b0b98 307 return count($responses);
308 } else {
309 return 0;
310 }
f9903ed0 311}
312
b416a1c3 313
7c8c335f 314function survey_print_all_responses($cmid, $results, $courseid) {
b416a1c3 315 global $THEME;
316
a30f997e 317 $table->head = array ("", get_string("name"), get_string("time"));
318 $table->align = array ("", "left", "left");
319 $table->size = array (35, "", "" );
b416a1c3 320
321 foreach ($results as $a) {
7c8c335f 322 $table->data[] = array(print_user_picture($a->id, $courseid, $a->picture, false, true, false),
839f2456 323 "<a href=\"report.php?action=student&amp;student=$a->id&amp;id=$cmid\">".fullname($a)."</a>",
a30f997e 324 userdate($a->time));
b416a1c3 325 }
7c8c335f 326
327 print_table($table);
b416a1c3 328}
329
b416a1c3 330
551b0b98 331function survey_get_template_name($templateid) {
f9903ed0 332 global $db;
333
334 if ($templateid) {
e323955d 335 if ($ss = get_record("surveys", "id", $templateid)) {
336 return $ss->name;
f9903ed0 337 }
338 } else {
339 return "";
340 }
341}
342
0d22e7cc 343
f9903ed0 344
0e30c987 345function survey_shorten_name ($name, $numwords) {
346 $words = explode(" ", $name);
347 for ($i=0; $i < $numwords; $i++) {
348 $output .= $words[$i]." ";
349 }
350 return $output;
351}
f9903ed0 352
353
0e30c987 354
355function survey_print_multi($question) {
356 GLOBAL $db, $qnum, $checklist, $THEME;
357
358
98899fc0 359 $stripreferthat = get_string("ipreferthat", "survey");
360 $strifoundthat = get_string("ifoundthat", "survey");
caf261b0 361 echo "<br />\n";
362 echo "<font size=\"4\"><b>$question->text</b></font><br />";
0e30c987 363
d1290cec 364 echo "<table align=\"center\" width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\">";
0e30c987 365
366 $options = explode( ",", $question->options);
367 $numoptions = count($options);
368
369 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
370 if ($question->type == 2) {
371 $P = "P";
372 } else {
373 $P = "";
374 }
375
376 if ($oneanswer) {
caf261b0 377 echo "<tr><td colspan=\"2\">$question->intro</td>";
0e30c987 378 } else {
caf261b0 379 echo "<tr><td colspan=\"3\">$question->intro</td>";
0e30c987 380 }
381
382 while (list ($key, $val) = each ($options)) {
caf261b0 383 echo "<td width=\"10%\" align=\"center\"><font size=\"1\">$val</font></td>\n";
0e30c987 384 }
caf261b0 385 echo "<td align=\"center\" bgcolor=\"$THEME->body\">&nbsp;</td></tr>\n";
0e30c987 386
e323955d 387 $subquestions = get_records_list("survey_questions", "id", $question->multi);
0e30c987 388
389 foreach ($subquestions as $q) {
390 $qnum++;
391 $bgcolor = survey_question_color($qnum);
392
f762c448 393 if ($q->text) {
394 $q->text = get_string($q->text, "survey");
395 }
396
caf261b0 397 echo "<tr bgcolor=\"$bgcolor\">";
0e30c987 398 if ($oneanswer) {
caf261b0 399 echo "<td width=\"10\" valign=\"top\"><b>$qnum</b></td>";
400 echo "<td valign=\"top\">$q->text</td>";
0e30c987 401 for ($i=1;$i<=$numoptions;$i++) {
caf261b0 402 echo "<td width=\"10%\" align=\"center\"><input type=\"radio\" name=\"q$P$q->id\" value=\"$i\" alt=\"$i\" /></td>";
0e30c987 403 }
caf261b0 404 echo "<td bgcolor=\"white\"><input type=\"radio\" name=\"q$P$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>";
0e30c987 405 $checklist["q$P$q->id"] = $numoptions;
406
407 } else {
caf261b0 408 echo "<td width=\"10\" valign=\"middle\" rowspan=\"2\"><b>$qnum</b></td>";
409 echo "<td width=\"10%\" nowrap=\"nowrap\"><font size=\"1\">$stripreferthat&nbsp;</font></td>";
410 echo "<td width=\"40%\" valign=\"middle\" rowspan=\"2\">$q->text</td>";
0e30c987 411 for ($i=1;$i<=$numoptions;$i++) {
caf261b0 412 echo "<td width=\"10%\" align=\"center\"><input type=\"radio\" name=\"qP$q->id\" value=\"$i\" alt=\"$i\"/></td>";
0e30c987 413 }
caf261b0 414 echo "<td bgcolor=\"$THEME->body\"><input type=\"radio\" name=\"qP$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>";
d1290cec 415 echo "</tr>";
0e30c987 416
caf261b0 417 echo "<tr bgcolor=\"$bgcolor\">";
418 echo "<td width=\"10%\" nowrap=\"nowrap\"><font size=\"1\">$strifoundthat&nbsp;</font></td>";
0e30c987 419 for ($i=1;$i<=$numoptions;$i++) {
caf261b0 420 echo "<td width=\"10%\" align=\"center\"><input type=\"radio\" name=\"q$q->id\" value=\"$i\" alt=\"$i\" /></td>";
0e30c987 421 }
caf261b0 422 echo "<td width=\"5%\" bgcolor=\"$THEME->body\"><input type=\"radio\" name=\"q$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>";
0e30c987 423 $checklist["qP$q->id"] = $numoptions;
424 $checklist["q$q->id"] = $numoptions;
425 }
d1290cec 426 echo "</tr>\n";
0e30c987 427 }
d1290cec 428 echo "</table>";
0e30c987 429}
430
431
432
433function survey_print_single($question) {
434 GLOBAL $db, $qnum;
435
436 $bgcolor = survey_question_color(0);
437
438 $qnum++;
439
caf261b0 440 echo "<br />\n";
d1290cec 441 echo "<table align=\"center\" width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n";
caf261b0 442 echo "<tr bgcolor=\"$bgcolor\">";
d1290cec 443 echo "<td valign=\"top\"><b>$qnum</b></td>";
caf261b0 444 echo "<td width=\"50%\" valign=\"top\">$question->text</td>\n";
445 echo "<td width=\"50%\" valign=\"top\"><font size=\"+1\">\n";
0e30c987 446
447
448 if ($question->type == 0) { // Plain text field
caf261b0 449 echo "<textarea rows=\"3\" cols=\"30\" name=\"$question->id\">$question->options</textarea>";
0e30c987 450
451 } else if ($question->type > 0) { // Choose one of a number
98899fc0 452 $strchoose = get_string("choose");
caf261b0 453 echo "<select name=\"$question->id\">";
e0b7b090 454 echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>";
0e30c987 455 $options = explode( ",", $question->options);
456 foreach ($options as $key => $val) {
457 $key++;
d1290cec 458 echo "<option value=\"$key\">$val</option>";
0e30c987 459 }
d1290cec 460 echo "</select>";
0e30c987 461
462 } else if ($question->type < 0) { // Choose several of a number
463 $options = explode( ",", $question->options);
f762c448 464 notify("This question type not supported yet");
0e30c987 465 }
466
d1290cec 467 echo "</font></td></tr></table>";
0e30c987 468
469}
470
471function survey_question_color($qnum) {
472 global $THEME;
473
474 if ($qnum) {
475 return $qnum % 2 ? $THEME->cellcontent : $THEME->cellcontent2;
476 //return $qnum % 2 ? "#CCFFCC" : "#CCFFFF";
477 } else {
478 return $THEME->cellcontent;
479 }
480}
481
607809b3 482function survey_print_graph($url) {
483 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
484
485 if (empty($CFG->gdversion)) {
486 echo "(".get_string("gdneed").")";
487
488 } else {
e80a8db7 489 echo "<img height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\" border=\"1\"".
839f2456 490 " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"\" />";
607809b3 491 }
492}
493
f9903ed0 494?>