MDL-29378 Fix the form display in the Survey/COLLES P+A module
[moodle.git] / mod / survey / lib.php
CommitLineData
e5cc530b 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
f9903ed0 17
e5cc530b 18/**
19 * @package mod-survey
20 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24/**
25 * Graph size
26 * @global int $SURVEY_GHEIGHT
27 */
17da2e6f 28global $SURVEY_GHEIGHT;
551b0b98 29$SURVEY_GHEIGHT = 500;
e5cc530b 30/**
31 * Graph size
32 * @global int $SURVEY_GWIDTH
33 */
17da2e6f 34global $SURVEY_GWIDTH;
551b0b98 35$SURVEY_GWIDTH = 900;
e5cc530b 36/**
37 * Question Type
38 * @global array $SURVEY_QTYPE
39 */
17da2e6f 40global $SURVEY_QTYPE;
551b0b98 41$SURVEY_QTYPE = array (
f9903ed0 42 "-3" => "Virtual Actual and Preferred",
43 "-2" => "Virtual Preferred",
44 "-1" => "Virtual Actual",
45 "0" => "Text",
46 "1" => "Actual",
47 "2" => "Preferred",
48 "3" => "Actual and Preferred",
49 );
50
b6b3deb4 51
52define("SURVEY_COLLES_ACTUAL", "1");
53define("SURVEY_COLLES_PREFERRED", "2");
54define("SURVEY_COLLES_PREFERRED_ACTUAL", "3");
55define("SURVEY_ATTLS", "4");
56define("SURVEY_CIQ", "5");
57
58
66ea15f3 59// STANDARD FUNCTIONS ////////////////////////////////////////////////////////
e5cc530b 60/**
61 * Given an object containing all the necessary data,
62 * (defined by the form in mod_form.php) this function
63 * will create a new instance and return the id number
64 * of the new instance.
65 *
66 * @global object
67 * @param object $survey
68 * @return int|bool
69 */
04eba58f 70function survey_add_instance($survey) {
c18269c7 71 global $DB;
04eba58f 72
c18269c7 73 if (!$template = $DB->get_record("survey", array("id"=>$survey->template))) {
04eba58f 74 return 0;
75 }
76
e5dd8e3b 77 $survey->questions = $template->questions;
04eba58f 78 $survey->timecreated = time();
79 $survey->timemodified = $survey->timecreated;
80
c18269c7 81 return $DB->insert_record("survey", $survey);
04eba58f 82
83}
84
e5cc530b 85/**
86 * Given an object containing all the necessary data,
87 * (defined by the form in mod_form.php) this function
88 * will update an existing instance with new data.
89 *
90 * @global object
91 * @param object $survey
92 * @return bool
93 */
04eba58f 94function survey_update_instance($survey) {
c18269c7 95 global $DB;
04eba58f 96
c18269c7 97 if (!$template = $DB->get_record("survey", array("id"=>$survey->template))) {
04eba58f 98 return 0;
99 }
100
e5dd8e3b
PS
101 $survey->id = $survey->instance;
102 $survey->questions = $template->questions;
04eba58f 103 $survey->timemodified = time();
104
c18269c7 105 return $DB->update_record("survey", $survey);
04eba58f 106}
107
e5cc530b 108/**
109 * Given an ID of an instance of this module,
110 * this function will permanently delete the instance
e5dd8e3b 111 * and any data that depends on it.
e5cc530b 112 *
113 * @global object
114 * @param int $id
115 * @return bool
116 */
04eba58f 117function survey_delete_instance($id) {
c18269c7 118 global $DB;
04eba58f 119
c18269c7 120 if (! $survey = $DB->get_record("survey", array("id"=>$id))) {
04eba58f 121 return false;
122 }
123
124 $result = true;
125
c18269c7 126 if (! $DB->delete_records("survey_analysis", array("survey"=>$survey->id))) {
04eba58f 127 $result = false;
128 }
129
c18269c7 130 if (! $DB->delete_records("survey_answers", array("survey"=>$survey->id))) {
04eba58f 131 $result = false;
132 }
133
c18269c7 134 if (! $DB->delete_records("survey", array("id"=>$survey->id))) {
04eba58f 135 $result = false;
136 }
137
138 return $result;
139}
140
e5cc530b 141/**
142 * @global object
143 * @param object $course
144 * @param object $user
145 * @param object $mod
146 * @param object $survey
147 * @return $result
148 */
66ea15f3 149function survey_user_outline($course, $user, $mod, $survey) {
deb3a60d 150 global $DB;
66ea15f3 151
deb3a60d 152 if ($answers = $DB->get_records("survey_answers", array('survey'=>$survey->id, 'userid'=>$user->id))) {
66ea15f3 153 $lastanswer = array_pop($answers);
154
39790bd8 155 $result = new stdClass();
66ea15f3 156 $result->info = get_string("done", "survey");
157 $result->time = $lastanswer->time;
158 return $result;
159 }
160 return NULL;
161}
162
e5cc530b 163/**
164 * @global stdObject
165 * @global object
166 * @uses SURVEY_CIQ
167 * @param object $course
168 * @param object $user
169 * @param object $mod
170 * @param object $survey
171 */
66ea15f3 172function survey_user_complete($course, $user, $mod, $survey) {
02f78a26 173 global $CFG, $DB, $OUTPUT;
66ea15f3 174
175 if (survey_already_done($survey->id, $user->id)) {
b6b3deb4 176 if ($survey->template == SURVEY_CIQ) { // print out answers for critical incidents
02f78a26 177 $table = new html_table();
b6b3deb4 178 $table->align = array("left", "left");
179
44e1b7d7 180 $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
b6b3deb4 181 $questionorder = explode(",", $survey->questions);
e5dd8e3b 182
b6b3deb4 183 foreach ($questionorder as $key=>$val) {
184 $question = $questions[$val];
185 $questiontext = get_string($question->shorttext, "survey");
e5dd8e3b 186
b6b3deb4 187 if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
188 $answertext = "$answer->answer1";
189 } else {
190 $answertext = "No answer";
191 }
192 $table->data[] = array("<b>$questiontext</b>", $answertext);
193 }
16be8974 194 echo html_writer::table($table);
e5dd8e3b 195
b6b3deb4 196 } else {
e5dd8e3b 197
839f2456 198 survey_print_graph("id=$mod->id&amp;sid=$user->id&amp;type=student.png");
b6b3deb4 199 }
e5dd8e3b 200
66ea15f3 201 } else {
202 print_string("notdone", "survey");
203 }
204}
205
e5cc530b 206/**
207 * @global stdClass
208 * @global object
209 * @param object $course
210 * @param mixed $viewfullnames
211 * @param int $timestamp
212 * @return bool
213 */
dd97c328 214function survey_print_recent_activity($course, $viewfullnames, $timestart) {
11e0676a 215 global $CFG, $DB, $OUTPUT;
54634efc 216
dd97c328 217 $modinfo = get_fast_modinfo($course);
218 $ids = array();
219 foreach ($modinfo->cms as $cm) {
220 if ($cm->modname != 'survey') {
221 continue;
222 }
223 if (!$cm->uservisible) {
224 continue;
225 }
226 $ids[$cm->instance] = $cm->instance;
227 }
54634efc 228
dd97c328 229 if (!$ids) {
1b5910c4 230 return false;
231 }
232
dd97c328 233 $slist = implode(',', $ids); // there should not be hundreds of glossaries in one course, right?
234
a1d870aa
EL
235 $rs = $DB->get_recordset_sql("SELECT sa.userid, sa.survey, MAX(sa.time) AS time,
236 u.firstname, u.lastname, u.email, u.picture
237 FROM {survey_answers} sa
238 JOIN {user} u ON u.id = sa.userid
239 WHERE sa.survey IN ($slist) AND sa.time > ?
240 GROUP BY sa.userid, sa.survey, u.firstname, u.lastname, u.email, u.picture
241 ORDER BY time ASC", array($timestart));
242 if (!$rs->valid()) {
243 $rs->close(); // Not going to iterate (but exit), close rs
dd97c328 244 return false;
54634efc 245 }
246
dd97c328 247 $surveys = array();
248
deb3a60d 249 foreach ($rs as $survey) {
dd97c328 250 $cm = $modinfo->instances['survey'][$survey->survey];
251 $survey->name = $cm->name;
252 $survey->cmid = $cm->id;
253 $surveys[] = $survey;
e5dd8e3b 254 }
deb3a60d 255 $rs->close();
dd97c328 256
257 if (!$surveys) {
258 return false;
259 }
260
11e0676a 261 echo $OUTPUT->heading(get_string('newsurveyresponses', 'survey').':');
dd97c328 262 foreach ($surveys as $survey) {
d5edb961 263 $url = $CFG->wwwroot.'/mod/survey/view.php?id='.$survey->cmid;
dd97c328 264 print_recent_activity_note($survey->time, $survey, $survey->name, $url, false, $viewfullnames);
54634efc 265 }
e5dd8e3b 266
dd97c328 267 return true;
54634efc 268}
04eba58f 269
e323955d 270// SQL FUNCTIONS ////////////////////////////////////////////////////////
66ea15f3 271
e5cc530b 272/**
273 * @global object
274 * @param sting $log
275 * @return array
276 */
e323955d 277function survey_log_info($log) {
deb3a60d 278 global $DB;
279 return $DB->get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture
280 FROM {survey} s, {user} u
281 WHERE s.id = ? AND u.id = ?", array($log->info, $log->userid));
e323955d 282}
f9903ed0 283
e5cc530b 284/**
285 * @global object
286 * @param int $surveyid
287 * @param int $groupid
288 * @param int $groupingid
289 * @return array
290 */
07aa2057 291function survey_get_responses($surveyid, $groupid, $groupingid) {
deb3a60d 292 global $DB;
293
e5dd8e3b 294 $params = array('surveyid'=>$surveyid, 'groupid'=>$groupid, 'groupingid'=>$groupingid);
a9ccbf60 295
296 if ($groupid) {
deb3a60d 297 $groupsjoin = "JOIN {groups_members} gm ON u.id = gm.userid AND gm.groupid = :groupid ";
1d684195 298
07aa2057 299 } else if ($groupingid) {
deb3a60d 300 $groupsjoin = "JOIN {groups_members} gm ON u.id = gm.userid
301 JOIN {groupings_groups} gg ON gm.groupid = gg.groupid AND gg.groupingid = :groupingid ";
a9ccbf60 302 } else {
07aa2057 303 $groupsjoin = "";
a9ccbf60 304 }
305
9b8b9b73
AD
306 $userfields = user_picture::fields('u');
307 return $DB->get_records_sql("SELECT $userfields, MAX(a.time) as time
deb3a60d 308 FROM {survey_answers} a
309 JOIN {user} u ON a.userid = u.id
310 $groupsjoin
311 WHERE a.survey = :surveyid
8e1e3c14 312 GROUP BY $userfields
deb3a60d 313 ORDER BY time ASC", $params);
e323955d 314}
315
e5cc530b 316/**
317 * @global object
318 * @param int $survey
319 * @param int $user
320 * @return array
321 */
e323955d 322function survey_get_analysis($survey, $user) {
deb3a60d 323 global $DB;
e323955d 324
e5dd8e3b 325 return $DB->get_record_sql("SELECT notes
deb3a60d 326 FROM {survey_analysis}
327 WHERE survey=? AND userid=?", array($survey, $user));
e323955d 328}
329
e5cc530b 330/**
331 * @global object
332 * @param int $survey
333 * @param int $user
334 * @param string $notes
335 */
e323955d 336function survey_update_analysis($survey, $user, $notes) {
deb3a60d 337 global $DB;
e323955d 338
deb3a60d 339 return $DB->execute("UPDATE {survey_analysis}
e5dd8e3b
PS
340 SET notes=?
341 WHERE survey=?
deb3a60d 342 AND userid=?", array($notes, $survey, $user));
e323955d 343}
344
e5cc530b 345/**
346 * @global object
347 * @param int $surveyid
348 * @param int $groupid
349 * @param string $sort
350 * @return array
351 */
a9ccbf60 352function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") {
deb3a60d 353 global $DB;
354
62ab566e 355 $params = array('surveyid'=>$surveyid, 'questionid'=>$questionid);
e323955d 356
a9ccbf60 357 if ($groupid) {
62ab566e
EL
358 $groupfrom = ', {groups_members} gm';
359 $groupsql = 'AND gm.groupid = :groupid AND u.id = gm.userid';
360 $params['groupid'] = $groupid;
a9ccbf60 361 } else {
62ab566e
EL
362 $groupfrom = '';
363 $groupsql = '';
a9ccbf60 364 }
365
d04d8e96
AD
366 $userfields = user_picture::fields('u');
367 return $DB->get_records_sql("SELECT sa.*, $userfields
62ab566e 368 FROM {survey_answers} sa, {user} u $groupfrom
e5dd8e3b
PS
369 WHERE sa.survey = :surveyid
370 AND sa.question = :questionid
deb3a60d 371 AND u.id = sa.userid $groupsql
372 ORDER BY $sort", $params);
7c8c335f 373}
374
e5cc530b 375/**
376 * @global object
377 * @param int $surveyid
378 * @param int $questionid
379 * @param int $userid
380 * @return array
381 */
7c8c335f 382function survey_get_user_answer($surveyid, $questionid, $userid) {
deb3a60d 383 global $DB;
7c8c335f 384
e5dd8e3b 385 return $DB->get_record_sql("SELECT sa.*
deb3a60d 386 FROM {survey_answers} sa
e5dd8e3b
PS
387 WHERE sa.survey = ?
388 AND sa.question = ?
deb3a60d 389 AND sa.userid = ?", array($surveyid, $questionid, $userid));
e323955d 390}
391
392// MODULE FUNCTIONS ////////////////////////////////////////////////////////
e5cc530b 393/**
394 * @global object
395 * @param int $survey
396 * @param int $user
397 * @param string $notes
398 * @return bool|int
399 */
ebc3bd2b 400function survey_add_analysis($survey, $user, $notes) {
deb3a60d 401 global $DB;
ebc3bd2b 402
39790bd8 403 $record = new stdClass();
ebc3bd2b 404 $record->survey = $survey;
405 $record->userid = $user;
406 $record->notes = $notes;
407
deb3a60d 408 return $DB->insert_record("survey_analysis", $record, false);
ebc3bd2b 409}
e5cc530b 410/**
411 * @global object
412 * @param int $survey
413 * @param int $user
414 * @return bool
415 */
e323955d 416function survey_already_done($survey, $user) {
deb3a60d 417 global $DB;
418
419 return $DB->record_exists("survey_answers", array("survey"=>$survey, "userid"=>$user));
551b0b98 420}
e5cc530b 421/**
422 * @param int $surveyid
423 * @param int $groupid
424 * @param int $groupingid
425 * @return int
426 */
07aa2057 427function survey_count_responses($surveyid, $groupid, $groupingid) {
428 if ($responses = survey_get_responses($surveyid, $groupid, $groupingid)) {
551b0b98 429 return count($responses);
430 } else {
431 return 0;
432 }
f9903ed0 433}
434
e5cc530b 435/**
436 * @param int $cmid
437 * @param array $results
438 * @param int $courseid
439 */
7c8c335f 440function survey_print_all_responses($cmid, $results, $courseid) {
02f78a26 441 global $OUTPUT;
442 $table = new html_table();
a30f997e 443 $table->head = array ("", get_string("name"), get_string("time"));
444 $table->align = array ("", "left", "left");
445 $table->size = array (35, "", "" );
b416a1c3 446
447 foreach ($results as $a) {
812dbaf7 448 $table->data[] = array($OUTPUT->user_picture($a, array('courseid'=>$courseid)),
9bf16314 449 html_writer::link("report.php?action=student&student=$a->id&id=$cmid", fullname($a)),
a30f997e 450 userdate($a->time));
b416a1c3 451 }
7c8c335f 452
16be8974 453 echo html_writer::table($table);
b416a1c3 454}
455
e5cc530b 456/**
457 * @global object
458 * @param int $templateid
459 * @return string
460 */
551b0b98 461function survey_get_template_name($templateid) {
fd0e6640 462 global $DB;
f9903ed0 463
464 if ($templateid) {
deb3a60d 465 if ($ss = $DB->get_record("surveys", array("id"=>$templateid))) {
e323955d 466 return $ss->name;
f9903ed0 467 }
468 } else {
469 return "";
470 }
471}
472
0d22e7cc 473
e5cc530b 474/**
475 * @param string $name
476 * @param array $numwords
477 * @return string
478 */
0e30c987 479function survey_shorten_name ($name, $numwords) {
480 $words = explode(" ", $name);
92701024 481 $output = '';
0e30c987 482 for ($i=0; $i < $numwords; $i++) {
483 $output .= $words[$i]." ";
484 }
485 return $output;
486}
f9903ed0 487
e5cc530b 488/**
489 * @todo Check this function
490 *
491 * @global object
492 * @global object
493 * @global int
494 * @global void This is never defined
495 * @global object This is defined twice?
496 * @param object $question
497 */
0e30c987 498function survey_print_multi($question) {
0a9d1b3b 499 global $USER, $DB, $qnum, $checklist, $DB, $OUTPUT; //TODO: this is sloppy globals abuse
0e30c987 500
98899fc0 501 $stripreferthat = get_string("ipreferthat", "survey");
502 $strifoundthat = get_string("ifoundthat", "survey");
6438c34f 503 $strdefault = get_string('notyetanswered', 'survey');
532f494d 504 $strresponses = get_string('responses', 'survey');
505
8d849394 506 echo $OUTPUT->heading($question->text, 3, 'questiontext');
90723839 507 echo "\n<table width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" class=\"surveytable\">";
0e30c987 508
509 $options = explode( ",", $question->options);
510 $numoptions = count($options);
511
de9a88cd
DM
512 // COLLES Actual (which is having questions of type 1) and COLLES Preferred (type 2)
513 // expect just one answer per question. COLLES Actual and Preferred (type 3) expects
514 // two answers per question. ATTLS (having a single question of type 1) expects one
515 // answer per question. CIQ is not using multiquestions (i.e. a question with subquestions).
516 // Note that the type of subquestions does not really matter, it's the type of the
517 // question itself that determines everything.
0e30c987 518 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
0e30c987 519
de9a88cd
DM
520 // COLLES Preferred (having questions of type 2) will use the radio elements with the name
521 // like qP1, qP2 etc. COLLES Actual and ATTLS have radios like q1, q2 etc.
522 if ($question->type == 2) {
523 $P = "P";
524 } else {
525 $P = "";
526 }
527
532f494d 528 echo "<tr class=\"smalltext\"><th scope=\"row\">$strresponses</th>";
6438c34f 529 echo "<th scope=\"col\" class=\"hresponse\">". get_string('notyetanswered', 'survey'). "</th>";
0e30c987 530 while (list ($key, $val) = each ($options)) {
532f494d 531 echo "<th scope=\"col\" class=\"hresponse\">$val</th>\n";
532 }
6438c34f 533 echo "</tr>\n";
532f494d 534
535 if ($oneanswer) {
6438c34f 536 echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n";
532f494d 537 } else {
e5dd8e3b 538 echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n";
0e30c987 539 }
0e30c987 540
44e1b7d7 541 $subquestions = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi));
532f494d 542
0e30c987 543 foreach ($subquestions as $q) {
544 $qnum++;
7c517929
DM
545 if ($oneanswer) {
546 $rowclass = survey_question_rowclass($qnum);
547 } else {
548 $rowclass = survey_question_rowclass(round($qnum / 2));
549 }
f762c448 550 if ($q->text) {
551 $q->text = get_string($q->text, "survey");
5196df58
AD
552 }
553
532f494d 554 echo "<tr class=\"$rowclass rblock\">";
0e30c987 555 if ($oneanswer) {
532f494d 556 echo "<th scope=\"row\" class=\"optioncell\">";
557 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
558 echo $q->text ."</th>\n";
6438c34f
RW
559
560 $default = get_accesshide($strdefault);
561 echo "<td class=\"whitecell\"><label for=\"q$P$q->id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"q$P" . $q->id . "_D\" value=\"0\" checked=\"checked\" />$default</label></td>";
562
532f494d 563 for ($i=1;$i<=$numoptions;$i++) {
564 $hiddentext = get_accesshide($options[$i-1]);
565 $id = "q$P" . $q->id . "_$i";
566 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
91421f3e
PS
567 }
568 $checklist["q$P$q->id"] = 0;
e5dd8e3b 569
91421f3e 570 } else {
532f494d 571 echo "<th scope=\"row\" class=\"optioncell\">";
572 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
5d1a5ef4 573 $qnum++;
7c517929 574 echo "<span class=\"preferthat\">$stripreferthat</span> &nbsp; ";
532f494d 575 echo "<span class=\"option\">$q->text</span></th>\n";
6438c34f
RW
576
577 $default = get_accesshide($strdefault);
578 echo '<td class="whitecell"><label for="qP'. $P.$q->id .'"><input type="radio" name="qP'.$P.$q->id. '" id="qP'. $q->id .'" value="0" checked="checked" />'.$default.'</label></td>';
579
580
0e30c987 581 for ($i=1;$i<=$numoptions;$i++) {
532f494d 582 $hiddentext = get_accesshide($options[$i-1]);
583 $id = "qP" . $q->id . "_$i";
584 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"qP$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 585 }
d1290cec 586 echo "</tr>";
0e30c987 587
532f494d 588 echo "<tr class=\"$rowclass rblock\">";
589 echo "<th scope=\"row\" class=\"optioncell\">";
590 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
7c517929 591 echo "<span class=\"foundthat\">$strifoundthat</span> &nbsp; ";
532f494d 592 echo "<span class=\"option\">$q->text</span></th>\n";
6438c34f
RW
593
594 $default = get_accesshide($strdefault);
595 echo '<td class="whitecell"><label for="q'. $q->id .'"><input type="radio" name="q'.$q->id. '" id="q'. $q->id .'" value="0" checked="checked" />'.$default.'</label></td>';
91421f3e 596
0e30c987 597 for ($i=1;$i<=$numoptions;$i++) {
532f494d 598 $hiddentext = get_accesshide($options[$i-1]);
599 $id = "q" . $q->id . "_$i";
600 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 601 }
91421f3e 602
6438c34f
RW
603 $checklist["qP$q->id"] = 0;
604 $checklist["q$q->id"] = 0;
0e30c987 605 }
d1290cec 606 echo "</tr>\n";
0e30c987 607 }
d1290cec 608 echo "</table>";
0e30c987 609}
610
611
e5cc530b 612/**
613 * @global object
614 * @global int
615 * @param object $question
616 */
0e30c987 617function survey_print_single($question) {
8b1b0382 618 global $DB, $qnum, $OUTPUT;
0e30c987 619
f1c1d2f1 620 $rowclass = survey_question_rowclass(0);
0e30c987 621
622 $qnum++;
623
caf261b0 624 echo "<br />\n";
795b56b3 625 echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n";
f1c1d2f1 626 echo "<tr class=\"$rowclass\">";
532f494d 627 echo "<th scope=\"row\" class=\"optioncell\"><label for=\"q$question->id\"><b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
628 echo "<span class=\"questioncell\">$question->text</span></label></th>\n";
629 echo "<td class=\"questioncell smalltext\">\n";
0e30c987 630
631
632 if ($question->type == 0) { // Plain text field
532f494d 633 echo "<textarea rows=\"3\" cols=\"30\" name=\"q$question->id\" id=\"q$question->id\">$question->options</textarea>";
0e30c987 634
635 } else if ($question->type > 0) { // Choose one of a number
98899fc0 636 $strchoose = get_string("choose");
532f494d 637 echo "<select name=\"q$question->id\" id=\"q$question->id\">";
e0b7b090 638 echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>";
0e30c987 639 $options = explode( ",", $question->options);
640 foreach ($options as $key => $val) {
641 $key++;
d1290cec 642 echo "<option value=\"$key\">$val</option>";
0e30c987 643 }
d1290cec 644 echo "</select>";
0e30c987 645
646 } else if ($question->type < 0) { // Choose several of a number
647 $options = explode( ",", $question->options);
8b1b0382 648 echo $OUTPUT->notification("This question type not supported yet");
0e30c987 649 }
650
532f494d 651 echo "</td></tr></table>";
0e30c987 652
653}
654
e5cc530b 655/**
656 *
657 * @param int $qnum
658 * @return string
659 */
f1c1d2f1 660function survey_question_rowclass($qnum) {
0e30c987 661
662 if ($qnum) {
f1c1d2f1 663 return $qnum % 2 ? 'r0' : 'r1';
0e30c987 664 } else {
f1c1d2f1 665 return 'r0';
0e30c987 666 }
667}
668
e5cc530b 669/**
670 * @global object
671 * @global int
672 * @global int
673 * @param string $url
674 */
607809b3 675function survey_print_graph($url) {
676 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
677
689096bc
PS
678 echo "<img class='resultgraph' height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\"".
679 " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"".get_string("surveygraph", "survey")."\" />";
607809b3 680}
681
e5cc530b 682/**
683 * @return array
684 */
f3221af9 685function survey_get_view_actions() {
686 return array('download','view all','view form','view graph','view report');
687}
688
e5cc530b 689/**
690 * @return array
691 */
f3221af9 692function survey_get_post_actions() {
693 return array('submit');
694}
695
0b5a80a1 696
697/**
698 * Implementation of the function for printing the form elements that control
699 * whether the course reset functionality affects the survey.
e5dd8e3b 700 *
e5cc530b 701 * @param object $mform form passed by reference
0b5a80a1 702 */
703function survey_reset_course_form_definition(&$mform) {
704 $mform->addElement('header', 'surveyheader', get_string('modulenameplural', 'survey'));
705 $mform->addElement('checkbox', 'reset_survey_answers', get_string('deleteallanswers','survey'));
706 $mform->addElement('checkbox', 'reset_survey_analysis', get_string('deleteanalysis','survey'));
707 $mform->disabledIf('reset_survey_analysis', 'reset_survey_answers', 'checked');
708}
709
710/**
711 * Course reset form defaults.
e5cc530b 712 * @return array
0b5a80a1 713 */
714function survey_reset_course_form_defaults($course) {
715 return array('reset_survey_answers'=>1, 'reset_survey_analysis'=>1);
716}
717
718/**
72d2982e 719 * Actual implementation of the reset course functionality, delete all the
0b5a80a1 720 * survey responses for course $data->courseid.
e5cc530b 721 *
722 * @global object
0b5a80a1 723 * @param $data the data submitted from the reset course.
724 * @return array status array
725 */
726function survey_reset_userdata($data) {
deb3a60d 727 global $DB;
0b5a80a1 728
729 $componentstr = get_string('modulenameplural', 'survey');
730 $status = array();
731
732 $surveyssql = "SELECT ch.id
deb3a60d 733 FROM {survey} ch
734 WHERE ch.course=?";
735 $params = array($data->courseid);
0b5a80a1 736
737 if (!empty($data->reset_survey_answers)) {
deb3a60d 738 $DB->delete_records_select('survey_answers', "survey IN ($surveyssql)", $params);
739 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
0b5a80a1 740 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
741 }
742
743 if (!empty($data->reset_survey_analysis)) {
deb3a60d 744 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
0b5a80a1 745 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
746 }
747
748 // no date shifting
749 return $status;
750}
751
f432bebf 752/**
753 * Returns all other caps used in module
e5cc530b 754 *
755 * @return array
f432bebf 756 */
757function survey_get_extra_capabilities() {
758 return array('moodle/site:accessallgroups');
759}
760
42f103be 761/**
e5cc530b 762 * @uses FEATURE_GROUPS
763 * @uses FEATURE_GROUPINGS
764 * @uses FEATURE_GROUPMEMBERSONLY
765 * @uses FEATURE_MOD_INTRO
766 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
767 * @uses FEATURE_GRADE_HAS_GRADE
768 * @uses FEATURE_GRADE_OUTCOMES
42f103be 769 * @param string $feature FEATURE_xx constant for requested feature
e5cc530b 770 * @return mixed True if module supports feature, false if not, null if doesn't know
42f103be 771 */
772function survey_supports($feature) {
773 switch($feature) {
774 case FEATURE_GROUPS: return true;
775 case FEATURE_GROUPINGS: return true;
776 case FEATURE_GROUPMEMBERSONLY: return true;
dc5c2bd9 777 case FEATURE_MOD_INTRO: return true;
42f103be 778 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
1c79d7db
SM
779 case FEATURE_GRADE_HAS_GRADE: return false;
780 case FEATURE_GRADE_OUTCOMES: return false;
253063a2 781 case FEATURE_BACKUP_MOODLE2: return true;
3e4c2435 782 case FEATURE_SHOW_DESCRIPTION: return true;
42f103be 783
784 default: return null;
785 }
17da2e6f 786}
a5cb6242 787
a5cb6242 788/**
789 * This function extends the settings navigation block for the site.
790 *
791 * It is safe to rely on PAGE here as we will only ever be within the module
792 * context when this is called
793 *
794 * @param navigation_node $settings
0b29477b
SH
795 * @param navigation_node $surveynode
796 */
797function survey_extend_settings_navigation($settings, $surveynode) {
798 global $PAGE;
a5cb6242 799
800 if (has_capability('mod/survey:readresponses', $PAGE->cm->context)) {
3406acde 801 $responsesnode = $surveynode->add(get_string("responsereports", "survey"));
a5cb6242 802
a6855934 803 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'summary'));
3406acde 804 $responsesnode->add(get_string("summary", "survey"), $url);
a5cb6242 805
a6855934 806 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'scales'));
3406acde 807 $responsesnode->add(get_string("scales", "survey"), $url);
a5cb6242 808
a6855934 809 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'questions'));
3406acde 810 $responsesnode->add(get_string("question", "survey"), $url);
a5cb6242 811
a6855934 812 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'students'));
3406acde 813 $responsesnode->add(get_string('participants'), $url);
a5cb6242 814
815 if (has_capability('mod/survey:download', $PAGE->cm->context)) {
a6855934 816 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'download'));
0b29477b 817 $surveynode->add(get_string('downloadresults', 'survey'), $url);
a5cb6242 818 }
819 }
253063a2 820}
b1627a92
DC
821
822/**
823 * Return a list of page types
824 * @param string $pagetype current page type
825 * @param stdClass $parentcontext Block's parent context
826 * @param stdClass $currentcontext Current context of block
827 */
b38e2e28 828function survey_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
829 $module_pagetype = array('mod-survey-*'=>get_string('page-mod-survey-x', 'survey'));
830 return $module_pagetype;
831}