MDL-44182 mod_survey: unified @package use
[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/**
da9bddbe 19 * @package mod_survey
e5cc530b 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
5def24cf 261 echo $OUTPUT->heading(get_string('newsurveyresponses', 'survey').':', 3);
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
5def24cf 506 echo $OUTPUT->heading($question->text, 3);
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
57c2052c 535 echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n";
0e30c987 536
44e1b7d7 537 $subquestions = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi));
532f494d 538
0e30c987 539 foreach ($subquestions as $q) {
540 $qnum++;
7c517929
DM
541 if ($oneanswer) {
542 $rowclass = survey_question_rowclass($qnum);
543 } else {
544 $rowclass = survey_question_rowclass(round($qnum / 2));
545 }
f762c448 546 if ($q->text) {
547 $q->text = get_string($q->text, "survey");
5196df58
AD
548 }
549
532f494d 550 echo "<tr class=\"$rowclass rblock\">";
0e30c987 551 if ($oneanswer) {
532f494d 552 echo "<th scope=\"row\" class=\"optioncell\">";
553 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
554 echo $q->text ."</th>\n";
6438c34f
RW
555
556 $default = get_accesshide($strdefault);
557 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>";
558
532f494d 559 for ($i=1;$i<=$numoptions;$i++) {
560 $hiddentext = get_accesshide($options[$i-1]);
561 $id = "q$P" . $q->id . "_$i";
562 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
91421f3e
PS
563 }
564 $checklist["q$P$q->id"] = 0;
e5dd8e3b 565
91421f3e 566 } else {
532f494d 567 echo "<th scope=\"row\" class=\"optioncell\">";
568 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
5d1a5ef4 569 $qnum++;
7c517929 570 echo "<span class=\"preferthat\">$stripreferthat</span> &nbsp; ";
532f494d 571 echo "<span class=\"option\">$q->text</span></th>\n";
6438c34f
RW
572
573 $default = get_accesshide($strdefault);
57c2052c 574 echo '<td class="whitecell"><label for="qP'.$q->id.'"><input type="radio" name="qP'.$q->id.'" id="qP'.$q->id.'" value="0" checked="checked" />'.$default.'</label></td>';
6438c34f
RW
575
576
0e30c987 577 for ($i=1;$i<=$numoptions;$i++) {
532f494d 578 $hiddentext = get_accesshide($options[$i-1]);
579 $id = "qP" . $q->id . "_$i";
580 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"qP$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 581 }
d1290cec 582 echo "</tr>";
0e30c987 583
532f494d 584 echo "<tr class=\"$rowclass rblock\">";
585 echo "<th scope=\"row\" class=\"optioncell\">";
586 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
7c517929 587 echo "<span class=\"foundthat\">$strifoundthat</span> &nbsp; ";
532f494d 588 echo "<span class=\"option\">$q->text</span></th>\n";
6438c34f
RW
589
590 $default = get_accesshide($strdefault);
591 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 592
0e30c987 593 for ($i=1;$i<=$numoptions;$i++) {
532f494d 594 $hiddentext = get_accesshide($options[$i-1]);
595 $id = "q" . $q->id . "_$i";
596 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 597 }
91421f3e 598
6438c34f
RW
599 $checklist["qP$q->id"] = 0;
600 $checklist["q$q->id"] = 0;
0e30c987 601 }
d1290cec 602 echo "</tr>\n";
0e30c987 603 }
d1290cec 604 echo "</table>";
0e30c987 605}
606
607
e5cc530b 608/**
609 * @global object
610 * @global int
611 * @param object $question
612 */
0e30c987 613function survey_print_single($question) {
8b1b0382 614 global $DB, $qnum, $OUTPUT;
0e30c987 615
f1c1d2f1 616 $rowclass = survey_question_rowclass(0);
0e30c987 617
618 $qnum++;
619
caf261b0 620 echo "<br />\n";
795b56b3 621 echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n";
f1c1d2f1 622 echo "<tr class=\"$rowclass\">";
532f494d 623 echo "<th scope=\"row\" class=\"optioncell\"><label for=\"q$question->id\"><b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
624 echo "<span class=\"questioncell\">$question->text</span></label></th>\n";
625 echo "<td class=\"questioncell smalltext\">\n";
0e30c987 626
627
628 if ($question->type == 0) { // Plain text field
532f494d 629 echo "<textarea rows=\"3\" cols=\"30\" name=\"q$question->id\" id=\"q$question->id\">$question->options</textarea>";
0e30c987 630
631 } else if ($question->type > 0) { // Choose one of a number
98899fc0 632 $strchoose = get_string("choose");
532f494d 633 echo "<select name=\"q$question->id\" id=\"q$question->id\">";
e0b7b090 634 echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>";
0e30c987 635 $options = explode( ",", $question->options);
636 foreach ($options as $key => $val) {
637 $key++;
d1290cec 638 echo "<option value=\"$key\">$val</option>";
0e30c987 639 }
d1290cec 640 echo "</select>";
0e30c987 641
642 } else if ($question->type < 0) { // Choose several of a number
643 $options = explode( ",", $question->options);
8b1b0382 644 echo $OUTPUT->notification("This question type not supported yet");
0e30c987 645 }
646
532f494d 647 echo "</td></tr></table>";
0e30c987 648
649}
650
e5cc530b 651/**
652 *
653 * @param int $qnum
654 * @return string
655 */
f1c1d2f1 656function survey_question_rowclass($qnum) {
0e30c987 657
658 if ($qnum) {
f1c1d2f1 659 return $qnum % 2 ? 'r0' : 'r1';
0e30c987 660 } else {
f1c1d2f1 661 return 'r0';
0e30c987 662 }
663}
664
e5cc530b 665/**
666 * @global object
667 * @global int
668 * @global int
669 * @param string $url
670 */
607809b3 671function survey_print_graph($url) {
672 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
673
689096bc
PS
674 echo "<img class='resultgraph' height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\"".
675 " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"".get_string("surveygraph", "survey")."\" />";
607809b3 676}
677
e5cc530b 678/**
679 * @return array
680 */
f3221af9 681function survey_get_view_actions() {
682 return array('download','view all','view form','view graph','view report');
683}
684
e5cc530b 685/**
686 * @return array
687 */
f3221af9 688function survey_get_post_actions() {
689 return array('submit');
690}
691
0b5a80a1 692
693/**
694 * Implementation of the function for printing the form elements that control
695 * whether the course reset functionality affects the survey.
e5dd8e3b 696 *
e5cc530b 697 * @param object $mform form passed by reference
0b5a80a1 698 */
699function survey_reset_course_form_definition(&$mform) {
700 $mform->addElement('header', 'surveyheader', get_string('modulenameplural', 'survey'));
701 $mform->addElement('checkbox', 'reset_survey_answers', get_string('deleteallanswers','survey'));
702 $mform->addElement('checkbox', 'reset_survey_analysis', get_string('deleteanalysis','survey'));
703 $mform->disabledIf('reset_survey_analysis', 'reset_survey_answers', 'checked');
704}
705
706/**
707 * Course reset form defaults.
e5cc530b 708 * @return array
0b5a80a1 709 */
710function survey_reset_course_form_defaults($course) {
711 return array('reset_survey_answers'=>1, 'reset_survey_analysis'=>1);
712}
713
714/**
72d2982e 715 * Actual implementation of the reset course functionality, delete all the
0b5a80a1 716 * survey responses for course $data->courseid.
e5cc530b 717 *
718 * @global object
0b5a80a1 719 * @param $data the data submitted from the reset course.
720 * @return array status array
721 */
722function survey_reset_userdata($data) {
deb3a60d 723 global $DB;
0b5a80a1 724
725 $componentstr = get_string('modulenameplural', 'survey');
726 $status = array();
727
728 $surveyssql = "SELECT ch.id
deb3a60d 729 FROM {survey} ch
730 WHERE ch.course=?";
731 $params = array($data->courseid);
0b5a80a1 732
733 if (!empty($data->reset_survey_answers)) {
deb3a60d 734 $DB->delete_records_select('survey_answers', "survey IN ($surveyssql)", $params);
735 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
0b5a80a1 736 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
737 }
738
739 if (!empty($data->reset_survey_analysis)) {
deb3a60d 740 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
0b5a80a1 741 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
742 }
743
744 // no date shifting
745 return $status;
746}
747
f432bebf 748/**
749 * Returns all other caps used in module
e5cc530b 750 *
751 * @return array
f432bebf 752 */
753function survey_get_extra_capabilities() {
754 return array('moodle/site:accessallgroups');
755}
756
42f103be 757/**
e5cc530b 758 * @uses FEATURE_GROUPS
759 * @uses FEATURE_GROUPINGS
760 * @uses FEATURE_GROUPMEMBERSONLY
761 * @uses FEATURE_MOD_INTRO
762 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
763 * @uses FEATURE_GRADE_HAS_GRADE
764 * @uses FEATURE_GRADE_OUTCOMES
42f103be 765 * @param string $feature FEATURE_xx constant for requested feature
e5cc530b 766 * @return mixed True if module supports feature, false if not, null if doesn't know
42f103be 767 */
768function survey_supports($feature) {
769 switch($feature) {
770 case FEATURE_GROUPS: return true;
771 case FEATURE_GROUPINGS: return true;
772 case FEATURE_GROUPMEMBERSONLY: return true;
dc5c2bd9 773 case FEATURE_MOD_INTRO: return true;
42f103be 774 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
1c79d7db
SM
775 case FEATURE_GRADE_HAS_GRADE: return false;
776 case FEATURE_GRADE_OUTCOMES: return false;
253063a2 777 case FEATURE_BACKUP_MOODLE2: return true;
3e4c2435 778 case FEATURE_SHOW_DESCRIPTION: return true;
42f103be 779
780 default: return null;
781 }
17da2e6f 782}
a5cb6242 783
a5cb6242 784/**
785 * This function extends the settings navigation block for the site.
786 *
787 * It is safe to rely on PAGE here as we will only ever be within the module
788 * context when this is called
789 *
790 * @param navigation_node $settings
0b29477b
SH
791 * @param navigation_node $surveynode
792 */
793function survey_extend_settings_navigation($settings, $surveynode) {
794 global $PAGE;
a5cb6242 795
796 if (has_capability('mod/survey:readresponses', $PAGE->cm->context)) {
3406acde 797 $responsesnode = $surveynode->add(get_string("responsereports", "survey"));
a5cb6242 798
a6855934 799 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'summary'));
3406acde 800 $responsesnode->add(get_string("summary", "survey"), $url);
a5cb6242 801
a6855934 802 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'scales'));
3406acde 803 $responsesnode->add(get_string("scales", "survey"), $url);
a5cb6242 804
a6855934 805 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'questions'));
3406acde 806 $responsesnode->add(get_string("question", "survey"), $url);
a5cb6242 807
a6855934 808 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'students'));
3406acde 809 $responsesnode->add(get_string('participants'), $url);
a5cb6242 810
811 if (has_capability('mod/survey:download', $PAGE->cm->context)) {
a6855934 812 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'download'));
0b29477b 813 $surveynode->add(get_string('downloadresults', 'survey'), $url);
a5cb6242 814 }
815 }
253063a2 816}
b1627a92
DC
817
818/**
819 * Return a list of page types
820 * @param string $pagetype current page type
821 * @param stdClass $parentcontext Block's parent context
822 * @param stdClass $currentcontext Current context of block
823 */
b38e2e28 824function survey_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
825 $module_pagetype = array('mod-survey-*'=>get_string('page-mod-survey-x', 'survey'));
826 return $module_pagetype;
827}