MDL-15109 glossary dml conversion
[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) {
c18269c7 28 global $DB;
04eba58f 29// Given an object containing all the necessary data,
7cac0c4b 30// (defined by the form in mod_form.php) this function
04eba58f 31// will create a new instance and return the id number
32// of the new instance.
33
c18269c7 34 if (!$template = $DB->get_record("survey", array("id"=>$survey->template))) {
04eba58f 35 return 0;
36 }
37
38 $survey->questions = $template->questions;
39 $survey->timecreated = time();
40 $survey->timemodified = $survey->timecreated;
41
c18269c7 42 return $DB->insert_record("survey", $survey);
04eba58f 43
44}
45
46
47function survey_update_instance($survey) {
c18269c7 48 global $DB;
04eba58f 49// Given an object containing all the necessary data,
7cac0c4b 50// (defined by the form in mod_form.php) this function
04eba58f 51// will update an existing instance with new data.
52
c18269c7 53 if (!$template = $DB->get_record("survey", array("id"=>$survey->template))) {
04eba58f 54 return 0;
55 }
56
57 $survey->id = $survey->instance;
58 $survey->questions = $template->questions;
59 $survey->timemodified = time();
60
c18269c7 61 return $DB->update_record("survey", $survey);
04eba58f 62}
63
64function survey_delete_instance($id) {
c18269c7 65 global $DB;
04eba58f 66// Given an ID of an instance of this module,
67// this function will permanently delete the instance
68// and any data that depends on it.
69
c18269c7 70 if (! $survey = $DB->get_record("survey", array("id"=>$id))) {
04eba58f 71 return false;
72 }
73
74 $result = true;
75
c18269c7 76 if (! $DB->delete_records("survey_analysis", array("survey"=>$survey->id))) {
04eba58f 77 $result = false;
78 }
79
c18269c7 80 if (! $DB->delete_records("survey_answers", array("survey"=>$survey->id))) {
04eba58f 81 $result = false;
82 }
83
c18269c7 84 if (! $DB->delete_records("survey", array("id"=>$survey->id))) {
04eba58f 85 $result = false;
86 }
87
88 return $result;
89}
90
66ea15f3 91function survey_user_outline($course, $user, $mod, $survey) {
ebc3bd2b 92 if ($answers = get_records_select("survey_answers", "survey='$survey->id' AND userid='$user->id'")) {
66ea15f3 93
94 $lastanswer = array_pop($answers);
95
96 $result->info = get_string("done", "survey");
97 $result->time = $lastanswer->time;
98 return $result;
99 }
100 return NULL;
101}
102
103
104function survey_user_complete($course, $user, $mod, $survey) {
44e1b7d7 105 global $CFG, $DB;
66ea15f3 106
107 if (survey_already_done($survey->id, $user->id)) {
b6b3deb4 108 if ($survey->template == SURVEY_CIQ) { // print out answers for critical incidents
109 $table = NULL;
110 $table->align = array("left", "left");
111
44e1b7d7 112 $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
b6b3deb4 113 $questionorder = explode(",", $survey->questions);
114
115 foreach ($questionorder as $key=>$val) {
116 $question = $questions[$val];
117 $questiontext = get_string($question->shorttext, "survey");
118
119 if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
120 $answertext = "$answer->answer1";
121 } else {
122 $answertext = "No answer";
123 }
124 $table->data[] = array("<b>$questiontext</b>", $answertext);
125 }
126 print_table($table);
127
128 } else {
129
839f2456 130 survey_print_graph("id=$mod->id&amp;sid=$user->id&amp;type=student.png");
b6b3deb4 131 }
132
66ea15f3 133 } else {
134 print_string("notdone", "survey");
135 }
136}
137
dd97c328 138function survey_print_recent_activity($course, $viewfullnames, $timestart) {
3d891989 139 global $CFG;
54634efc 140
dd97c328 141 $modinfo = get_fast_modinfo($course);
142 $ids = array();
143 foreach ($modinfo->cms as $cm) {
144 if ($cm->modname != 'survey') {
145 continue;
146 }
147 if (!$cm->uservisible) {
148 continue;
149 }
150 $ids[$cm->instance] = $cm->instance;
151 }
54634efc 152
dd97c328 153 if (!$ids) {
1b5910c4 154 return false;
155 }
156
dd97c328 157 $slist = implode(',', $ids); // there should not be hundreds of glossaries in one course, right?
158
4a005b22 159 if (!$rs = get_recordset_sql("SELECT sa.userid, sa.survey, MAX(sa.time) AS time,
dd97c328 160 u.firstname, u.lastname, u.email, u.picture
161 FROM {$CFG->prefix}survey_answers sa
162 JOIN {$CFG->prefix}user u ON u.id = sa.userid
163 WHERE sa.survey IN ($slist) AND sa.time > $timestart
4a005b22 164 GROUP BY sa.userid, sa.survey, u.firstname, u.lastname, u.email, u.picture
165 ORDER BY time ASC")) {
dd97c328 166 return false;
54634efc 167 }
168
dd97c328 169 $surveys = array();
170
171 while ($survey = rs_fetch_next_record($rs)) {
172 $cm = $modinfo->instances['survey'][$survey->survey];
173 $survey->name = $cm->name;
174 $survey->cmid = $cm->id;
175 $surveys[] = $survey;
176 }
177 rs_close($rs);
178
179 if (!$surveys) {
180 return false;
181 }
182
183 print_headline(get_string('newsurveyresponses', 'survey').':');
184 foreach ($surveys as $survey) {
d5edb961 185 $url = $CFG->wwwroot.'/mod/survey/view.php?id='.$survey->cmid;
dd97c328 186 print_recent_activity_note($survey->time, $survey, $survey->name, $url, false, $viewfullnames);
54634efc 187 }
188
dd97c328 189 return true;
54634efc 190}
04eba58f 191
05855091 192function survey_get_participants($surveyid) {
193//Returns the users with data in one survey
194//(users with records in survey_analysis and survey_answers, students)
195
196 global $CFG;
197
198 //Get students from survey_analysis
95e72c12 199 $st_analysis = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 200 FROM {$CFG->prefix}user u,
201 {$CFG->prefix}survey_analysis a
202 WHERE a.survey = '$surveyid' and
203 u.id = a.userid");
204 //Get students from survey_answers
95e72c12 205 $st_answers = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 206 FROM {$CFG->prefix}user u,
207 {$CFG->prefix}survey_answers a
208 WHERE a.survey = '$surveyid' and
209 u.id = a.userid");
210
211 //Add st_answers to st_analysis
212 if ($st_answers) {
213 foreach ($st_answers as $st_answer) {
214 $st_analysis[$st_answer->id] = $st_answer;
215 }
216 }
217 //Return st_analysis array (it contains an array of unique users)
218 return ($st_analysis);
219}
66ea15f3 220
e323955d 221// SQL FUNCTIONS ////////////////////////////////////////////////////////
66ea15f3 222
f9903ed0 223
e323955d 224function survey_log_info($log) {
225 global $CFG;
7c8c335f 226 return get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture
e323955d 227 FROM {$CFG->prefix}survey s,
228 {$CFG->prefix}user u
229 WHERE s.id = '$log->info'
ebc3bd2b 230 AND u.id = '$log->userid'");
e323955d 231}
f9903ed0 232
07aa2057 233function survey_get_responses($surveyid, $groupid, $groupingid) {
e323955d 234 global $CFG;
a9ccbf60 235
236 if ($groupid) {
07aa2057 237 $groupsjoin = "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid AND gm.groupid = '$groupid' ";
1d684195 238
07aa2057 239 } else if ($groupingid) {
240 $groupsjoin = "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid
241 INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid AND gg.groupingid = $groupingid ";
a9ccbf60 242 } else {
07aa2057 243 $groupsjoin = "";
a9ccbf60 244 }
245
87f47bec 246 return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture, MAX(a.time) as time
07aa2057 247 FROM {$CFG->prefix}survey_answers a
248 INNER JOIN {$CFG->prefix}user u ON a.userid = u.id
249 $groupsjoin
87f47bec 250 WHERE a.survey = $surveyid
87f47bec 251 GROUP BY u.id, u.firstname, u.lastname, u.picture
252 ORDER BY time ASC");
e323955d 253}
254
255function survey_get_analysis($survey, $user) {
ebc3bd2b 256 global $CFG;
e323955d 257
258 return get_record_sql("SELECT notes
259 FROM {$CFG->prefix}survey_analysis
260 WHERE survey='$survey'
ebc3bd2b 261 AND userid='$user'");
e323955d 262}
263
264function survey_update_analysis($survey, $user, $notes) {
ebc3bd2b 265 global $CFG;
e323955d 266
ebc3bd2b 267 return execute_sql("UPDATE {$CFG->prefix}survey_analysis
e323955d 268 SET notes='$notes'
269 WHERE survey='$survey'
ebc3bd2b 270 AND userid='$user'");
e323955d 271}
272
273
a9ccbf60 274function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") {
e323955d 275 global $CFG;
276
a9ccbf60 277 if ($groupid) {
278 $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid";
279 } else {
280 $groupsql = "";
281 }
282
e323955d 283 return get_records_sql("SELECT sa.*,u.firstname,u.lastname,u.picture
ebc3bd2b 284 FROM {$CFG->prefix}survey_answers sa,
a9ccbf60 285 {$CFG->prefix}user u,
286 {$CFG->prefix}groups_members gm
e323955d 287 WHERE sa.survey = '$surveyid'
288 AND sa.question = $questionid
a9ccbf60 289 AND u.id = sa.userid $groupsql
7c8c335f 290 ORDER BY $sort");
291}
292
293function survey_get_user_answer($surveyid, $questionid, $userid) {
294 global $CFG;
295
296 return get_record_sql("SELECT sa.*
297 FROM {$CFG->prefix}survey_answers sa
298 WHERE sa.survey = '$surveyid'
299 AND sa.question = '$questionid'
300 AND sa.userid = '$userid'");
e323955d 301}
302
303// MODULE FUNCTIONS ////////////////////////////////////////////////////////
304
ebc3bd2b 305function survey_add_analysis($survey, $user, $notes) {
306 global $CFG;
307
308 $record->survey = $survey;
309 $record->userid = $user;
310 $record->notes = $notes;
311
312 return insert_record("survey_analysis", $record, false);
313}
314
e323955d 315function survey_already_done($survey, $user) {
ebc3bd2b 316 return record_exists("survey_answers", "survey", $survey, "userid", $user);
551b0b98 317}
f9903ed0 318
07aa2057 319function survey_count_responses($surveyid, $groupid, $groupingid) {
320 if ($responses = survey_get_responses($surveyid, $groupid, $groupingid)) {
551b0b98 321 return count($responses);
322 } else {
323 return 0;
324 }
f9903ed0 325}
326
b416a1c3 327
7c8c335f 328function survey_print_all_responses($cmid, $results, $courseid) {
b416a1c3 329
a30f997e 330 $table->head = array ("", get_string("name"), get_string("time"));
331 $table->align = array ("", "left", "left");
332 $table->size = array (35, "", "" );
b416a1c3 333
334 foreach ($results as $a) {
7c8c335f 335 $table->data[] = array(print_user_picture($a->id, $courseid, $a->picture, false, true, false),
839f2456 336 "<a href=\"report.php?action=student&amp;student=$a->id&amp;id=$cmid\">".fullname($a)."</a>",
a30f997e 337 userdate($a->time));
b416a1c3 338 }
7c8c335f 339
340 print_table($table);
b416a1c3 341}
342
b416a1c3 343
551b0b98 344function survey_get_template_name($templateid) {
fd0e6640 345 global $DB;
f9903ed0 346
347 if ($templateid) {
e323955d 348 if ($ss = get_record("surveys", "id", $templateid)) {
349 return $ss->name;
f9903ed0 350 }
351 } else {
352 return "";
353 }
354}
355
0d22e7cc 356
f9903ed0 357
0e30c987 358function survey_shorten_name ($name, $numwords) {
359 $words = explode(" ", $name);
360 for ($i=0; $i < $numwords; $i++) {
361 $output .= $words[$i]." ";
362 }
363 return $output;
364}
f9903ed0 365
366
0e30c987 367
368function survey_print_multi($question) {
44e1b7d7 369 global $USER, $DB, $qnum, $checklist, $DB;
0e30c987 370
98899fc0 371 $stripreferthat = get_string("ipreferthat", "survey");
372 $strifoundthat = get_string("ifoundthat", "survey");
532f494d 373 $strdefault = get_string('default');
374 $strresponses = get_string('responses', 'survey');
375
376 print_heading($question->text, null, 3, 'questiontext');
377 echo "\n<table width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\">";
0e30c987 378
379 $options = explode( ",", $question->options);
380 $numoptions = count($options);
381
382 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
8a12a132 383 if ($question->type == 2) {
384 $P = "P";
385 } else {
386 $P = "";
387 }
0e30c987 388
532f494d 389 echo "<tr class=\"smalltext\"><th scope=\"row\">$strresponses</th>";
0e30c987 390 while (list ($key, $val) = each ($options)) {
532f494d 391 echo "<th scope=\"col\" class=\"hresponse\">$val</th>\n";
392 }
393 echo "<th>&nbsp;</th></tr>\n";
394
395 if ($oneanswer) {
396 echo "<tr><th scope=\"col\" colspan=\"6\">$question->intro</th></tr>\n";
397 } else {
398 echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n";
0e30c987 399 }
0e30c987 400
44e1b7d7 401 $subquestions = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi));
532f494d 402
0e30c987 403 foreach ($subquestions as $q) {
404 $qnum++;
f1c1d2f1 405 $rowclass = survey_question_rowclass($qnum);
f762c448 406 if ($q->text) {
407 $q->text = get_string($q->text, "survey");
408 }
409
532f494d 410 echo "<tr class=\"$rowclass rblock\">";
0e30c987 411 if ($oneanswer) {
3a2e0fe9 412
532f494d 413 echo "<th scope=\"row\" class=\"optioncell\">";
414 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
415 echo $q->text ."</th>\n";
416 for ($i=1;$i<=$numoptions;$i++) {
417 $hiddentext = get_accesshide($options[$i-1]);
418 $id = "q$P" . $q->id . "_$i";
419 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 420 }
532f494d 421 $default = get_accesshide($strdefault, 'label', '', "for=\"q$P$q->id\"");
422 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 423 $checklist["q$P$q->id"] = $numoptions;
424
3a2e0fe9 425 } else {
f40aee6f 426 // yu : fix for MDL-7501, possibly need to use user flag as this is quite ugly.
532f494d 427 echo "<th scope=\"row\" class=\"optioncell\">";
428 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
5d1a5ef4 429 $qnum++;
532f494d 430 echo "<span class=\"preferthat smalltext\">$stripreferthat</span> &nbsp; ";
431 echo "<span class=\"option\">$q->text</span></th>\n";
0e30c987 432 for ($i=1;$i<=$numoptions;$i++) {
532f494d 433 $hiddentext = get_accesshide($options[$i-1]);
434 $id = "qP" . $q->id . "_$i";
435 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"qP$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 436 }
532f494d 437 $default = get_accesshide($strdefault, 'label', '', "for=\"qP$q->id\"");
438 echo "<td><input type=\"radio\" name=\"qP$q->id\" id=\"qP$q->id\" value=\"0\" checked=\"checked\" />$default</td>";
d1290cec 439 echo "</tr>";
0e30c987 440
532f494d 441 echo "<tr class=\"$rowclass rblock\">";
442 echo "<th scope=\"row\" class=\"optioncell\">";
443 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
444 echo "<span class=\"foundthat smalltext\">$strifoundthat</span> &nbsp; ";
445 echo "<span class=\"option\">$q->text</span></th>\n";
0e30c987 446 for ($i=1;$i<=$numoptions;$i++) {
532f494d 447 $hiddentext = get_accesshide($options[$i-1]);
448 $id = "q" . $q->id . "_$i";
449 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 450 }
532f494d 451 $default = get_accesshide($strdefault, 'label', '', "for=\"q$q->id\"");
452 echo "<td class=\"buttoncell\"><input type=\"radio\" name=\"q$q->id\" id=\"q$q->id\" value=\"0\" checked=\"checked\" />$default</td>";
0e30c987 453 $checklist["qP$q->id"] = $numoptions;
5d1a5ef4 454 $checklist["q$q->id"] = $numoptions;
0e30c987 455 }
d1290cec 456 echo "</tr>\n";
0e30c987 457 }
d1290cec 458 echo "</table>";
0e30c987 459}
460
461
462
463function survey_print_single($question) {
fd0e6640 464 global $DB, $qnum;
0e30c987 465
f1c1d2f1 466 $rowclass = survey_question_rowclass(0);
0e30c987 467
468 $qnum++;
469
caf261b0 470 echo "<br />\n";
795b56b3 471 echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n";
f1c1d2f1 472 echo "<tr class=\"$rowclass\">";
532f494d 473 echo "<th scope=\"row\" class=\"optioncell\"><label for=\"q$question->id\"><b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
474 echo "<span class=\"questioncell\">$question->text</span></label></th>\n";
475 echo "<td class=\"questioncell smalltext\">\n";
0e30c987 476
477
478 if ($question->type == 0) { // Plain text field
532f494d 479 echo "<textarea rows=\"3\" cols=\"30\" name=\"q$question->id\" id=\"q$question->id\">$question->options</textarea>";
0e30c987 480
481 } else if ($question->type > 0) { // Choose one of a number
98899fc0 482 $strchoose = get_string("choose");
532f494d 483 echo "<select name=\"q$question->id\" id=\"q$question->id\">";
e0b7b090 484 echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>";
0e30c987 485 $options = explode( ",", $question->options);
486 foreach ($options as $key => $val) {
487 $key++;
d1290cec 488 echo "<option value=\"$key\">$val</option>";
0e30c987 489 }
d1290cec 490 echo "</select>";
0e30c987 491
492 } else if ($question->type < 0) { // Choose several of a number
493 $options = explode( ",", $question->options);
f762c448 494 notify("This question type not supported yet");
0e30c987 495 }
496
532f494d 497 echo "</td></tr></table>";
0e30c987 498
499}
500
f1c1d2f1 501function survey_question_rowclass($qnum) {
0e30c987 502
503 if ($qnum) {
f1c1d2f1 504 return $qnum % 2 ? 'r0' : 'r1';
0e30c987 505 } else {
f1c1d2f1 506 return 'r0';
0e30c987 507 }
508}
509
607809b3 510function survey_print_graph($url) {
511 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
512
513 if (empty($CFG->gdversion)) {
514 echo "(".get_string("gdneed").")";
515
516 } else {
795b56b3 517 echo "<img class='resultgraph' height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\"".
7150b8ae 518 " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"".get_string("surveygraph", "survey")."\" />";
607809b3 519 }
520}
521
f3221af9 522function survey_get_view_actions() {
523 return array('download','view all','view form','view graph','view report');
524}
525
526function survey_get_post_actions() {
527 return array('submit');
528}
529
0b5a80a1 530
531/**
532 * Implementation of the function for printing the form elements that control
533 * whether the course reset functionality affects the survey.
534 * @param $mform form passed by reference
535 */
536function survey_reset_course_form_definition(&$mform) {
537 $mform->addElement('header', 'surveyheader', get_string('modulenameplural', 'survey'));
538 $mform->addElement('checkbox', 'reset_survey_answers', get_string('deleteallanswers','survey'));
539 $mform->addElement('checkbox', 'reset_survey_analysis', get_string('deleteanalysis','survey'));
540 $mform->disabledIf('reset_survey_analysis', 'reset_survey_answers', 'checked');
541}
542
543/**
544 * Course reset form defaults.
545 */
546function survey_reset_course_form_defaults($course) {
547 return array('reset_survey_answers'=>1, 'reset_survey_analysis'=>1);
548}
549
550/**
551 * Actual implementation of the rest coures functionality, delete all the
552 * survey responses for course $data->courseid.
553 * @param $data the data submitted from the reset course.
554 * @return array status array
555 */
556function survey_reset_userdata($data) {
557 global $CFG;
558
559 $componentstr = get_string('modulenameplural', 'survey');
560 $status = array();
561
562 $surveyssql = "SELECT ch.id
563 FROM {$CFG->prefix}survey ch
564 WHERE ch.course={$data->courseid}";
565
566 if (!empty($data->reset_survey_answers)) {
567 delete_records_select('survey_answers', "survey IN ($surveyssql)");
568 delete_records_select('survey_analysis', "survey IN ($surveyssql)");
569 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
570 }
571
572 if (!empty($data->reset_survey_analysis)) {
573 delete_records_select('survey_analysis', "survey IN ($surveyssql)");
574 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
575 }
576
577 // no date shifting
578 return $status;
579}
580
f9903ed0 581?>