Possible fix to MDL-12093 : give window without heading and navigation special css...
[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
8f7dc7f1 141 if (!$logs = get_records_select('log', 'time > \''.$timestart.'\' AND '.
142 'course = \''.$course->id.'\' AND '.
143 'module = \'survey\' AND '.
144 'action = \'submit\' ', 'time ASC')) {
1b5910c4 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;
8f7dc7f1 165 print_headline(get_string('newsurveyresponses', 'survey').':');
54634efc 166 foreach ($surveys as $survey) {
583b57b4 167 print_recent_activity_note($survey->time, $survey, $survey->name,
8f7dc7f1 168 $CFG->wwwroot.'/mod/survey/'.$survey->url);
54634efc 169 }
170 }
171
172 return $content;
173}
04eba58f 174
05855091 175function survey_get_participants($surveyid) {
176//Returns the users with data in one survey
177//(users with records in survey_analysis and survey_answers, students)
178
179 global $CFG;
180
181 //Get students from survey_analysis
95e72c12 182 $st_analysis = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 183 FROM {$CFG->prefix}user u,
184 {$CFG->prefix}survey_analysis a
185 WHERE a.survey = '$surveyid' and
186 u.id = a.userid");
187 //Get students from survey_answers
95e72c12 188 $st_answers = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 189 FROM {$CFG->prefix}user u,
190 {$CFG->prefix}survey_answers a
191 WHERE a.survey = '$surveyid' and
192 u.id = a.userid");
193
194 //Add st_answers to st_analysis
195 if ($st_answers) {
196 foreach ($st_answers as $st_answer) {
197 $st_analysis[$st_answer->id] = $st_answer;
198 }
199 }
200 //Return st_analysis array (it contains an array of unique users)
201 return ($st_analysis);
202}
66ea15f3 203
e323955d 204// SQL FUNCTIONS ////////////////////////////////////////////////////////
66ea15f3 205
f9903ed0 206
e323955d 207function survey_log_info($log) {
208 global $CFG;
7c8c335f 209 return get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture
e323955d 210 FROM {$CFG->prefix}survey s,
211 {$CFG->prefix}user u
212 WHERE s.id = '$log->info'
ebc3bd2b 213 AND u.id = '$log->userid'");
e323955d 214}
f9903ed0 215
07aa2057 216function survey_get_responses($surveyid, $groupid, $groupingid) {
e323955d 217 global $CFG;
a9ccbf60 218
219 if ($groupid) {
07aa2057 220 $groupsjoin = "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid AND gm.groupid = '$groupid' ";
1d684195 221
07aa2057 222 } else if ($groupingid) {
223 $groupsjoin = "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid
224 INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid AND gg.groupingid = $groupingid ";
a9ccbf60 225 } else {
07aa2057 226 $groupsjoin = "";
a9ccbf60 227 }
228
87f47bec 229 return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture, MAX(a.time) as time
07aa2057 230 FROM {$CFG->prefix}survey_answers a
231 INNER JOIN {$CFG->prefix}user u ON a.userid = u.id
232 $groupsjoin
87f47bec 233 WHERE a.survey = $surveyid
87f47bec 234 GROUP BY u.id, u.firstname, u.lastname, u.picture
235 ORDER BY time ASC");
e323955d 236}
237
238function survey_get_analysis($survey, $user) {
ebc3bd2b 239 global $CFG;
e323955d 240
241 return get_record_sql("SELECT notes
242 FROM {$CFG->prefix}survey_analysis
243 WHERE survey='$survey'
ebc3bd2b 244 AND userid='$user'");
e323955d 245}
246
247function survey_update_analysis($survey, $user, $notes) {
ebc3bd2b 248 global $CFG;
e323955d 249
ebc3bd2b 250 return execute_sql("UPDATE {$CFG->prefix}survey_analysis
e323955d 251 SET notes='$notes'
252 WHERE survey='$survey'
ebc3bd2b 253 AND userid='$user'");
e323955d 254}
255
256
a9ccbf60 257function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") {
e323955d 258 global $CFG;
259
a9ccbf60 260 if ($groupid) {
261 $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid";
262 } else {
263 $groupsql = "";
264 }
265
e323955d 266 return get_records_sql("SELECT sa.*,u.firstname,u.lastname,u.picture
ebc3bd2b 267 FROM {$CFG->prefix}survey_answers sa,
a9ccbf60 268 {$CFG->prefix}user u,
269 {$CFG->prefix}groups_members gm
e323955d 270 WHERE sa.survey = '$surveyid'
271 AND sa.question = $questionid
a9ccbf60 272 AND u.id = sa.userid $groupsql
7c8c335f 273 ORDER BY $sort");
274}
275
276function survey_get_user_answer($surveyid, $questionid, $userid) {
277 global $CFG;
278
279 return get_record_sql("SELECT sa.*
280 FROM {$CFG->prefix}survey_answers sa
281 WHERE sa.survey = '$surveyid'
282 AND sa.question = '$questionid'
283 AND sa.userid = '$userid'");
e323955d 284}
285
286// MODULE FUNCTIONS ////////////////////////////////////////////////////////
287
ebc3bd2b 288function survey_add_analysis($survey, $user, $notes) {
289 global $CFG;
290
291 $record->survey = $survey;
292 $record->userid = $user;
293 $record->notes = $notes;
294
295 return insert_record("survey_analysis", $record, false);
296}
297
e323955d 298function survey_already_done($survey, $user) {
ebc3bd2b 299 return record_exists("survey_answers", "survey", $survey, "userid", $user);
551b0b98 300}
f9903ed0 301
07aa2057 302function survey_count_responses($surveyid, $groupid, $groupingid) {
303 if ($responses = survey_get_responses($surveyid, $groupid, $groupingid)) {
551b0b98 304 return count($responses);
305 } else {
306 return 0;
307 }
f9903ed0 308}
309
b416a1c3 310
7c8c335f 311function survey_print_all_responses($cmid, $results, $courseid) {
b416a1c3 312
a30f997e 313 $table->head = array ("", get_string("name"), get_string("time"));
314 $table->align = array ("", "left", "left");
315 $table->size = array (35, "", "" );
b416a1c3 316
317 foreach ($results as $a) {
7c8c335f 318 $table->data[] = array(print_user_picture($a->id, $courseid, $a->picture, false, true, false),
839f2456 319 "<a href=\"report.php?action=student&amp;student=$a->id&amp;id=$cmid\">".fullname($a)."</a>",
a30f997e 320 userdate($a->time));
b416a1c3 321 }
7c8c335f 322
323 print_table($table);
b416a1c3 324}
325
b416a1c3 326
551b0b98 327function survey_get_template_name($templateid) {
f9903ed0 328 global $db;
329
330 if ($templateid) {
e323955d 331 if ($ss = get_record("surveys", "id", $templateid)) {
332 return $ss->name;
f9903ed0 333 }
334 } else {
335 return "";
336 }
337}
338
0d22e7cc 339
f9903ed0 340
0e30c987 341function survey_shorten_name ($name, $numwords) {
342 $words = explode(" ", $name);
343 for ($i=0; $i < $numwords; $i++) {
344 $output .= $words[$i]." ";
345 }
346 return $output;
347}
f9903ed0 348
349
0e30c987 350
351function survey_print_multi($question) {
532f494d 352 global $USER, $db, $qnum, $checklist;
0e30c987 353
98899fc0 354 $stripreferthat = get_string("ipreferthat", "survey");
355 $strifoundthat = get_string("ifoundthat", "survey");
532f494d 356 $strdefault = get_string('default');
357 $strresponses = get_string('responses', 'survey');
358
359 print_heading($question->text, null, 3, 'questiontext');
360 echo "\n<table width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\">";
0e30c987 361
362 $options = explode( ",", $question->options);
363 $numoptions = count($options);
364
365 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
8a12a132 366 if ($question->type == 2) {
367 $P = "P";
368 } else {
369 $P = "";
370 }
0e30c987 371
532f494d 372 echo "<tr class=\"smalltext\"><th scope=\"row\">$strresponses</th>";
0e30c987 373 while (list ($key, $val) = each ($options)) {
532f494d 374 echo "<th scope=\"col\" class=\"hresponse\">$val</th>\n";
375 }
376 echo "<th>&nbsp;</th></tr>\n";
377
378 if ($oneanswer) {
379 echo "<tr><th scope=\"col\" colspan=\"6\">$question->intro</th></tr>\n";
380 } else {
381 echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n";
0e30c987 382 }
0e30c987 383
e323955d 384 $subquestions = get_records_list("survey_questions", "id", $question->multi);
532f494d 385
0e30c987 386 foreach ($subquestions as $q) {
387 $qnum++;
f1c1d2f1 388 $rowclass = survey_question_rowclass($qnum);
f762c448 389 if ($q->text) {
390 $q->text = get_string($q->text, "survey");
391 }
392
532f494d 393 echo "<tr class=\"$rowclass rblock\">";
0e30c987 394 if ($oneanswer) {
3a2e0fe9 395
532f494d 396 echo "<th scope=\"row\" class=\"optioncell\">";
397 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
398 echo $q->text ."</th>\n";
399 for ($i=1;$i<=$numoptions;$i++) {
400 $hiddentext = get_accesshide($options[$i-1]);
401 $id = "q$P" . $q->id . "_$i";
402 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 403 }
532f494d 404 $default = get_accesshide($strdefault, 'label', '', "for=\"q$P$q->id\"");
405 echo "<td class=\"whitecell\"><input type=\"radio\" name=\"q$P$q->id\" id=\"q$P" . $q->id . "_D\" value=\"0\" checked=\"checked\" />$default</td>";
0e30c987 406 $checklist["q$P$q->id"] = $numoptions;
407
3a2e0fe9 408 } else {
f40aee6f 409 // yu : fix for MDL-7501, possibly need to use user flag as this is quite ugly.
532f494d 410 echo "<th scope=\"row\" class=\"optioncell\">";
411 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
5d1a5ef4 412 $qnum++;
532f494d 413 echo "<span class=\"preferthat smalltext\">$stripreferthat</span> &nbsp; ";
414 echo "<span class=\"option\">$q->text</span></th>\n";
0e30c987 415 for ($i=1;$i<=$numoptions;$i++) {
532f494d 416 $hiddentext = get_accesshide($options[$i-1]);
417 $id = "qP" . $q->id . "_$i";
418 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"qP$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 419 }
532f494d 420 $default = get_accesshide($strdefault, 'label', '', "for=\"qP$q->id\"");
421 echo "<td><input type=\"radio\" name=\"qP$q->id\" id=\"qP$q->id\" value=\"0\" checked=\"checked\" />$default</td>";
d1290cec 422 echo "</tr>";
0e30c987 423
532f494d 424 echo "<tr class=\"$rowclass rblock\">";
425 echo "<th scope=\"row\" class=\"optioncell\">";
426 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
427 echo "<span class=\"foundthat smalltext\">$strifoundthat</span> &nbsp; ";
428 echo "<span class=\"option\">$q->text</span></th>\n";
0e30c987 429 for ($i=1;$i<=$numoptions;$i++) {
532f494d 430 $hiddentext = get_accesshide($options[$i-1]);
431 $id = "q" . $q->id . "_$i";
432 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 433 }
532f494d 434 $default = get_accesshide($strdefault, 'label', '', "for=\"q$q->id\"");
435 echo "<td class=\"buttoncell\"><input type=\"radio\" name=\"q$q->id\" id=\"q$q->id\" value=\"0\" checked=\"checked\" />$default</td>";
0e30c987 436 $checklist["qP$q->id"] = $numoptions;
5d1a5ef4 437 $checklist["q$q->id"] = $numoptions;
0e30c987 438 }
d1290cec 439 echo "</tr>\n";
0e30c987 440 }
d1290cec 441 echo "</table>";
0e30c987 442}
443
444
445
446function survey_print_single($question) {
532f494d 447 global $db, $qnum;
0e30c987 448
f1c1d2f1 449 $rowclass = survey_question_rowclass(0);
0e30c987 450
451 $qnum++;
452
caf261b0 453 echo "<br />\n";
795b56b3 454 echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n";
f1c1d2f1 455 echo "<tr class=\"$rowclass\">";
532f494d 456 echo "<th scope=\"row\" class=\"optioncell\"><label for=\"q$question->id\"><b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
457 echo "<span class=\"questioncell\">$question->text</span></label></th>\n";
458 echo "<td class=\"questioncell smalltext\">\n";
0e30c987 459
460
461 if ($question->type == 0) { // Plain text field
532f494d 462 echo "<textarea rows=\"3\" cols=\"30\" name=\"q$question->id\" id=\"q$question->id\">$question->options</textarea>";
0e30c987 463
464 } else if ($question->type > 0) { // Choose one of a number
98899fc0 465 $strchoose = get_string("choose");
532f494d 466 echo "<select name=\"q$question->id\" id=\"q$question->id\">";
e0b7b090 467 echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>";
0e30c987 468 $options = explode( ",", $question->options);
469 foreach ($options as $key => $val) {
470 $key++;
d1290cec 471 echo "<option value=\"$key\">$val</option>";
0e30c987 472 }
d1290cec 473 echo "</select>";
0e30c987 474
475 } else if ($question->type < 0) { // Choose several of a number
476 $options = explode( ",", $question->options);
f762c448 477 notify("This question type not supported yet");
0e30c987 478 }
479
532f494d 480 echo "</td></tr></table>";
0e30c987 481
482}
483
f1c1d2f1 484function survey_question_rowclass($qnum) {
0e30c987 485
486 if ($qnum) {
f1c1d2f1 487 return $qnum % 2 ? 'r0' : 'r1';
0e30c987 488 } else {
f1c1d2f1 489 return 'r0';
0e30c987 490 }
491}
492
607809b3 493function survey_print_graph($url) {
494 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
495
496 if (empty($CFG->gdversion)) {
497 echo "(".get_string("gdneed").")";
498
499 } else {
795b56b3 500 echo "<img class='resultgraph' height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\"".
7150b8ae 501 " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"".get_string("surveygraph", "survey")."\" />";
607809b3 502 }
503}
504
f3221af9 505function survey_get_view_actions() {
506 return array('download','view all','view form','view graph','view report');
507}
508
509function survey_get_post_actions() {
510 return array('submit');
511}
512
f9903ed0 513?>