survey MDL-23324 survey form generation was assuming that subquestions would always...
[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
deb3a60d 155 $result = new object();
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
deb3a60d 235 if (!$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))) {
dd97c328 242 return false;
54634efc 243 }
244
dd97c328 245 $surveys = array();
246
deb3a60d 247 foreach ($rs as $survey) {
dd97c328 248 $cm = $modinfo->instances['survey'][$survey->survey];
249 $survey->name = $cm->name;
250 $survey->cmid = $cm->id;
251 $surveys[] = $survey;
e5dd8e3b 252 }
deb3a60d 253 $rs->close();
dd97c328 254
255 if (!$surveys) {
256 return false;
257 }
258
11e0676a 259 echo $OUTPUT->heading(get_string('newsurveyresponses', 'survey').':');
dd97c328 260 foreach ($surveys as $survey) {
d5edb961 261 $url = $CFG->wwwroot.'/mod/survey/view.php?id='.$survey->cmid;
dd97c328 262 print_recent_activity_note($survey->time, $survey, $survey->name, $url, false, $viewfullnames);
54634efc 263 }
e5dd8e3b 264
dd97c328 265 return true;
54634efc 266}
04eba58f 267
e5cc530b 268/**
269 * Returns the users with data in one survey
270 * (users with records in survey_analysis and survey_answers, students)
271 *
272 * @global object
273 * @param int $surveyid
274 * @return array
275 */
05855091 276function survey_get_participants($surveyid) {
deb3a60d 277 global $DB;
05855091 278
279 //Get students from survey_analysis
deb3a60d 280 $st_analysis = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
281 FROM {user} u, {survey_analysis} a
282 WHERE a.survey = ? AND
283 u.id = a.userid", array($surveyid));
05855091 284 //Get students from survey_answers
deb3a60d 285 $st_answers = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
286 FROM {user} u, {survey_answers} a
287 WHERE a.survey = ? AND
288 u.id = a.userid", array($surveyid));
05855091 289
290 //Add st_answers to st_analysis
291 if ($st_answers) {
292 foreach ($st_answers as $st_answer) {
293 $st_analysis[$st_answer->id] = $st_answer;
294 }
295 }
296 //Return st_analysis array (it contains an array of unique users)
297 return ($st_analysis);
298}
66ea15f3 299
e323955d 300// SQL FUNCTIONS ////////////////////////////////////////////////////////
66ea15f3 301
e5cc530b 302/**
303 * @global object
304 * @param sting $log
305 * @return array
306 */
e323955d 307function survey_log_info($log) {
deb3a60d 308 global $DB;
309 return $DB->get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture
310 FROM {survey} s, {user} u
311 WHERE s.id = ? AND u.id = ?", array($log->info, $log->userid));
e323955d 312}
f9903ed0 313
e5cc530b 314/**
315 * @global object
316 * @param int $surveyid
317 * @param int $groupid
318 * @param int $groupingid
319 * @return array
320 */
07aa2057 321function survey_get_responses($surveyid, $groupid, $groupingid) {
deb3a60d 322 global $DB;
323
e5dd8e3b 324 $params = array('surveyid'=>$surveyid, 'groupid'=>$groupid, 'groupingid'=>$groupingid);
a9ccbf60 325
326 if ($groupid) {
deb3a60d 327 $groupsjoin = "JOIN {groups_members} gm ON u.id = gm.userid AND gm.groupid = :groupid ";
1d684195 328
07aa2057 329 } else if ($groupingid) {
deb3a60d 330 $groupsjoin = "JOIN {groups_members} gm ON u.id = gm.userid
331 JOIN {groupings_groups} gg ON gm.groupid = gg.groupid AND gg.groupingid = :groupingid ";
a9ccbf60 332 } else {
07aa2057 333 $groupsjoin = "";
a9ccbf60 334 }
335
9b8b9b73
AD
336 $userfields = user_picture::fields('u');
337 return $DB->get_records_sql("SELECT $userfields, MAX(a.time) as time
deb3a60d 338 FROM {survey_answers} a
339 JOIN {user} u ON a.userid = u.id
340 $groupsjoin
341 WHERE a.survey = :surveyid
8e1e3c14 342 GROUP BY $userfields
deb3a60d 343 ORDER BY time ASC", $params);
e323955d 344}
345
e5cc530b 346/**
347 * @global object
348 * @param int $survey
349 * @param int $user
350 * @return array
351 */
e323955d 352function survey_get_analysis($survey, $user) {
deb3a60d 353 global $DB;
e323955d 354
e5dd8e3b 355 return $DB->get_record_sql("SELECT notes
deb3a60d 356 FROM {survey_analysis}
357 WHERE survey=? AND userid=?", array($survey, $user));
e323955d 358}
359
e5cc530b 360/**
361 * @global object
362 * @param int $survey
363 * @param int $user
364 * @param string $notes
365 */
e323955d 366function survey_update_analysis($survey, $user, $notes) {
deb3a60d 367 global $DB;
e323955d 368
deb3a60d 369 return $DB->execute("UPDATE {survey_analysis}
e5dd8e3b
PS
370 SET notes=?
371 WHERE survey=?
deb3a60d 372 AND userid=?", array($notes, $survey, $user));
e323955d 373}
374
e5cc530b 375/**
376 * @global object
377 * @param int $surveyid
378 * @param int $groupid
379 * @param string $sort
380 * @return array
381 */
a9ccbf60 382function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") {
deb3a60d 383 global $DB;
384
62ab566e 385 $params = array('surveyid'=>$surveyid, 'questionid'=>$questionid);
e323955d 386
a9ccbf60 387 if ($groupid) {
62ab566e
EL
388 $groupfrom = ', {groups_members} gm';
389 $groupsql = 'AND gm.groupid = :groupid AND u.id = gm.userid';
390 $params['groupid'] = $groupid;
a9ccbf60 391 } else {
62ab566e
EL
392 $groupfrom = '';
393 $groupsql = '';
a9ccbf60 394 }
395
d04d8e96
AD
396 $userfields = user_picture::fields('u');
397 return $DB->get_records_sql("SELECT sa.*, $userfields
62ab566e 398 FROM {survey_answers} sa, {user} u $groupfrom
e5dd8e3b
PS
399 WHERE sa.survey = :surveyid
400 AND sa.question = :questionid
deb3a60d 401 AND u.id = sa.userid $groupsql
402 ORDER BY $sort", $params);
7c8c335f 403}
404
e5cc530b 405/**
406 * @global object
407 * @param int $surveyid
408 * @param int $questionid
409 * @param int $userid
410 * @return array
411 */
7c8c335f 412function survey_get_user_answer($surveyid, $questionid, $userid) {
deb3a60d 413 global $DB;
7c8c335f 414
e5dd8e3b 415 return $DB->get_record_sql("SELECT sa.*
deb3a60d 416 FROM {survey_answers} sa
e5dd8e3b
PS
417 WHERE sa.survey = ?
418 AND sa.question = ?
deb3a60d 419 AND sa.userid = ?", array($surveyid, $questionid, $userid));
e323955d 420}
421
422// MODULE FUNCTIONS ////////////////////////////////////////////////////////
e5cc530b 423/**
424 * @global object
425 * @param int $survey
426 * @param int $user
427 * @param string $notes
428 * @return bool|int
429 */
ebc3bd2b 430function survey_add_analysis($survey, $user, $notes) {
deb3a60d 431 global $DB;
ebc3bd2b 432
deb3a60d 433 $record = new object();
ebc3bd2b 434 $record->survey = $survey;
435 $record->userid = $user;
436 $record->notes = $notes;
437
deb3a60d 438 return $DB->insert_record("survey_analysis", $record, false);
ebc3bd2b 439}
e5cc530b 440/**
441 * @global object
442 * @param int $survey
443 * @param int $user
444 * @return bool
445 */
e323955d 446function survey_already_done($survey, $user) {
deb3a60d 447 global $DB;
448
449 return $DB->record_exists("survey_answers", array("survey"=>$survey, "userid"=>$user));
551b0b98 450}
e5cc530b 451/**
452 * @param int $surveyid
453 * @param int $groupid
454 * @param int $groupingid
455 * @return int
456 */
07aa2057 457function survey_count_responses($surveyid, $groupid, $groupingid) {
458 if ($responses = survey_get_responses($surveyid, $groupid, $groupingid)) {
551b0b98 459 return count($responses);
460 } else {
461 return 0;
462 }
f9903ed0 463}
464
e5cc530b 465/**
466 * @param int $cmid
467 * @param array $results
468 * @param int $courseid
469 */
7c8c335f 470function survey_print_all_responses($cmid, $results, $courseid) {
02f78a26 471 global $OUTPUT;
472 $table = new html_table();
a30f997e 473 $table->head = array ("", get_string("name"), get_string("time"));
474 $table->align = array ("", "left", "left");
475 $table->size = array (35, "", "" );
b416a1c3 476
477 foreach ($results as $a) {
812dbaf7 478 $table->data[] = array($OUTPUT->user_picture($a, array('courseid'=>$courseid)),
9bf16314 479 html_writer::link("report.php?action=student&student=$a->id&id=$cmid", fullname($a)),
a30f997e 480 userdate($a->time));
b416a1c3 481 }
7c8c335f 482
16be8974 483 echo html_writer::table($table);
b416a1c3 484}
485
e5cc530b 486/**
487 * @global object
488 * @param int $templateid
489 * @return string
490 */
551b0b98 491function survey_get_template_name($templateid) {
fd0e6640 492 global $DB;
f9903ed0 493
494 if ($templateid) {
deb3a60d 495 if ($ss = $DB->get_record("surveys", array("id"=>$templateid))) {
e323955d 496 return $ss->name;
f9903ed0 497 }
498 } else {
499 return "";
500 }
501}
502
0d22e7cc 503
e5cc530b 504/**
505 * @param string $name
506 * @param array $numwords
507 * @return string
508 */
0e30c987 509function survey_shorten_name ($name, $numwords) {
510 $words = explode(" ", $name);
511 for ($i=0; $i < $numwords; $i++) {
512 $output .= $words[$i]." ";
513 }
514 return $output;
515}
f9903ed0 516
e5cc530b 517/**
518 * @todo Check this function
519 *
520 * @global object
521 * @global object
522 * @global int
523 * @global void This is never defined
524 * @global object This is defined twice?
525 * @param object $question
526 */
0e30c987 527function survey_print_multi($question) {
8d849394 528 global $USER, $DB, $qnum, $checklist, $DB, $OUTPUT;
0e30c987 529
98899fc0 530 $stripreferthat = get_string("ipreferthat", "survey");
531 $strifoundthat = get_string("ifoundthat", "survey");
6438c34f 532 $strdefault = get_string('notyetanswered', 'survey');
532f494d 533 $strresponses = get_string('responses', 'survey');
534
8d849394 535 echo $OUTPUT->heading($question->text, 3, 'questiontext');
90723839 536 echo "\n<table width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" class=\"surveytable\">";
0e30c987 537
538 $options = explode( ",", $question->options);
539 $numoptions = count($options);
540
541 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
0e30c987 542
532f494d 543 echo "<tr class=\"smalltext\"><th scope=\"row\">$strresponses</th>";
6438c34f 544 echo "<th scope=\"col\" class=\"hresponse\">". get_string('notyetanswered', 'survey'). "</th>";
0e30c987 545 while (list ($key, $val) = each ($options)) {
532f494d 546 echo "<th scope=\"col\" class=\"hresponse\">$val</th>\n";
547 }
6438c34f 548 echo "</tr>\n";
532f494d 549
550 if ($oneanswer) {
6438c34f 551 echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n";
532f494d 552 } else {
e5dd8e3b 553 echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n";
0e30c987 554 }
0e30c987 555
44e1b7d7 556 $subquestions = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi));
532f494d 557
0e30c987 558 foreach ($subquestions as $q) {
559 $qnum++;
f1c1d2f1 560 $rowclass = survey_question_rowclass($qnum);
f762c448 561 if ($q->text) {
562 $q->text = get_string($q->text, "survey");
563 }
564
5196df58
AD
565 $oneanswer = ($q->type == 1 || $q->type == 2) ? true : false;
566 if ($q->type == 2) {
567 $P = "P";
568 } else {
569 $P = "";
570 }
571
532f494d 572 echo "<tr class=\"$rowclass rblock\">";
0e30c987 573 if ($oneanswer) {
532f494d 574 echo "<th scope=\"row\" class=\"optioncell\">";
575 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
576 echo $q->text ."</th>\n";
6438c34f
RW
577
578 $default = get_accesshide($strdefault);
579 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>";
580
532f494d 581 for ($i=1;$i<=$numoptions;$i++) {
582 $hiddentext = get_accesshide($options[$i-1]);
583 $id = "q$P" . $q->id . "_$i";
584 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
6438c34f
RW
585 }
586 $checklist["q$P$q->id"] = 0;
e5dd8e3b 587
6438c34f 588 } else {
f40aee6f 589 // yu : fix for MDL-7501, possibly need to use user flag as this is quite ugly.
532f494d 590 echo "<th scope=\"row\" class=\"optioncell\">";
591 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
5d1a5ef4 592 $qnum++;
532f494d 593 echo "<span class=\"preferthat smalltext\">$stripreferthat</span> &nbsp; ";
594 echo "<span class=\"option\">$q->text</span></th>\n";
6438c34f
RW
595
596 $default = get_accesshide($strdefault);
597 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>';
598
599
0e30c987 600 for ($i=1;$i<=$numoptions;$i++) {
532f494d 601 $hiddentext = get_accesshide($options[$i-1]);
602 $id = "qP" . $q->id . "_$i";
603 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"qP$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 604 }
d1290cec 605 echo "</tr>";
0e30c987 606
532f494d 607 echo "<tr class=\"$rowclass rblock\">";
608 echo "<th scope=\"row\" class=\"optioncell\">";
609 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
610 echo "<span class=\"foundthat smalltext\">$strifoundthat</span> &nbsp; ";
611 echo "<span class=\"option\">$q->text</span></th>\n";
6438c34f
RW
612
613 $default = get_accesshide($strdefault);
614 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>';
615
0e30c987 616 for ($i=1;$i<=$numoptions;$i++) {
532f494d 617 $hiddentext = get_accesshide($options[$i-1]);
618 $id = "q" . $q->id . "_$i";
619 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 620 }
6438c34f
RW
621
622 $checklist["qP$q->id"] = 0;
623 $checklist["q$q->id"] = 0;
0e30c987 624 }
d1290cec 625 echo "</tr>\n";
0e30c987 626 }
d1290cec 627 echo "</table>";
0e30c987 628}
629
630
e5cc530b 631/**
632 * @global object
633 * @global int
634 * @param object $question
635 */
0e30c987 636function survey_print_single($question) {
8b1b0382 637 global $DB, $qnum, $OUTPUT;
0e30c987 638
f1c1d2f1 639 $rowclass = survey_question_rowclass(0);
0e30c987 640
641 $qnum++;
642
caf261b0 643 echo "<br />\n";
795b56b3 644 echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n";
f1c1d2f1 645 echo "<tr class=\"$rowclass\">";
532f494d 646 echo "<th scope=\"row\" class=\"optioncell\"><label for=\"q$question->id\"><b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
647 echo "<span class=\"questioncell\">$question->text</span></label></th>\n";
648 echo "<td class=\"questioncell smalltext\">\n";
0e30c987 649
650
651 if ($question->type == 0) { // Plain text field
532f494d 652 echo "<textarea rows=\"3\" cols=\"30\" name=\"q$question->id\" id=\"q$question->id\">$question->options</textarea>";
0e30c987 653
654 } else if ($question->type > 0) { // Choose one of a number
98899fc0 655 $strchoose = get_string("choose");
532f494d 656 echo "<select name=\"q$question->id\" id=\"q$question->id\">";
e0b7b090 657 echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>";
0e30c987 658 $options = explode( ",", $question->options);
659 foreach ($options as $key => $val) {
660 $key++;
d1290cec 661 echo "<option value=\"$key\">$val</option>";
0e30c987 662 }
d1290cec 663 echo "</select>";
0e30c987 664
665 } else if ($question->type < 0) { // Choose several of a number
666 $options = explode( ",", $question->options);
8b1b0382 667 echo $OUTPUT->notification("This question type not supported yet");
0e30c987 668 }
669
532f494d 670 echo "</td></tr></table>";
0e30c987 671
672}
673
e5cc530b 674/**
675 *
676 * @param int $qnum
677 * @return string
678 */
f1c1d2f1 679function survey_question_rowclass($qnum) {
0e30c987 680
681 if ($qnum) {
f1c1d2f1 682 return $qnum % 2 ? 'r0' : 'r1';
0e30c987 683 } else {
f1c1d2f1 684 return 'r0';
0e30c987 685 }
686}
687
e5cc530b 688/**
689 * @global object
690 * @global int
691 * @global int
692 * @param string $url
693 */
607809b3 694function survey_print_graph($url) {
695 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
696
697 if (empty($CFG->gdversion)) {
698 echo "(".get_string("gdneed").")";
699
700 } else {
795b56b3 701 echo "<img class='resultgraph' height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\"".
7150b8ae 702 " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"".get_string("surveygraph", "survey")."\" />";
607809b3 703 }
704}
705
e5cc530b 706/**
707 * @return array
708 */
f3221af9 709function survey_get_view_actions() {
710 return array('download','view all','view form','view graph','view report');
711}
712
e5cc530b 713/**
714 * @return array
715 */
f3221af9 716function survey_get_post_actions() {
717 return array('submit');
718}
719
0b5a80a1 720
721/**
722 * Implementation of the function for printing the form elements that control
723 * whether the course reset functionality affects the survey.
e5dd8e3b 724 *
e5cc530b 725 * @param object $mform form passed by reference
0b5a80a1 726 */
727function survey_reset_course_form_definition(&$mform) {
728 $mform->addElement('header', 'surveyheader', get_string('modulenameplural', 'survey'));
729 $mform->addElement('checkbox', 'reset_survey_answers', get_string('deleteallanswers','survey'));
730 $mform->addElement('checkbox', 'reset_survey_analysis', get_string('deleteanalysis','survey'));
731 $mform->disabledIf('reset_survey_analysis', 'reset_survey_answers', 'checked');
732}
733
734/**
735 * Course reset form defaults.
e5cc530b 736 * @return array
0b5a80a1 737 */
738function survey_reset_course_form_defaults($course) {
739 return array('reset_survey_answers'=>1, 'reset_survey_analysis'=>1);
740}
741
742/**
743 * Actual implementation of the rest coures functionality, delete all the
744 * survey responses for course $data->courseid.
e5cc530b 745 *
746 * @global object
0b5a80a1 747 * @param $data the data submitted from the reset course.
748 * @return array status array
749 */
750function survey_reset_userdata($data) {
deb3a60d 751 global $DB;
0b5a80a1 752
753 $componentstr = get_string('modulenameplural', 'survey');
754 $status = array();
755
756 $surveyssql = "SELECT ch.id
deb3a60d 757 FROM {survey} ch
758 WHERE ch.course=?";
759 $params = array($data->courseid);
0b5a80a1 760
761 if (!empty($data->reset_survey_answers)) {
deb3a60d 762 $DB->delete_records_select('survey_answers', "survey IN ($surveyssql)", $params);
763 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
0b5a80a1 764 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
765 }
766
767 if (!empty($data->reset_survey_analysis)) {
deb3a60d 768 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
0b5a80a1 769 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
770 }
771
772 // no date shifting
773 return $status;
774}
775
f432bebf 776/**
777 * Returns all other caps used in module
e5cc530b 778 *
779 * @return array
f432bebf 780 */
781function survey_get_extra_capabilities() {
782 return array('moodle/site:accessallgroups');
783}
784
42f103be 785/**
e5cc530b 786 * @uses FEATURE_GROUPS
787 * @uses FEATURE_GROUPINGS
788 * @uses FEATURE_GROUPMEMBERSONLY
789 * @uses FEATURE_MOD_INTRO
790 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
791 * @uses FEATURE_GRADE_HAS_GRADE
792 * @uses FEATURE_GRADE_OUTCOMES
42f103be 793 * @param string $feature FEATURE_xx constant for requested feature
e5cc530b 794 * @return mixed True if module supports feature, false if not, null if doesn't know
42f103be 795 */
796function survey_supports($feature) {
797 switch($feature) {
798 case FEATURE_GROUPS: return true;
799 case FEATURE_GROUPINGS: return true;
800 case FEATURE_GROUPMEMBERSONLY: return true;
dc5c2bd9 801 case FEATURE_MOD_INTRO: return true;
42f103be 802 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
803 case FEATURE_GRADE_HAS_GRADE: return true;
804 case FEATURE_GRADE_OUTCOMES: return true;
253063a2 805 case FEATURE_BACKUP_MOODLE2: return true;
42f103be 806
807 default: return null;
808 }
17da2e6f 809}
a5cb6242 810
811/**
792881f0 812 * This fucntion extends the global navigation for the site.
a5cb6242 813 * It is important to note that you should not rely on PAGE objects within this
814 * body of code as there is no guarantee that during an AJAX request they are
815 * available
816 *
817 * @param navigation_node $navigation The quiz node within the global navigation
818 * @param stdClass $course The course object returned from the DB
819 * @param stdClass $module The module object returned from the DB
792881f0 820 * @param stdClass $cm The course module instance returned from the DB
a5cb6242 821 */
822function survey_extend_navigation($navigation, $course, $module, $cm) {
823 /**
824 * This is currently just a stub so that it can be easily expanded upon.
825 * When expanding just remove this comment and the line below and then add
826 * you content.
827 */
828 $navigation->nodetype = navigation_node::NODETYPE_LEAF;
829}
830
831/**
832 * This function extends the settings navigation block for the site.
833 *
834 * It is safe to rely on PAGE here as we will only ever be within the module
835 * context when this is called
836 *
837 * @param navigation_node $settings
0b29477b
SH
838 * @param navigation_node $surveynode
839 */
840function survey_extend_settings_navigation($settings, $surveynode) {
841 global $PAGE;
a5cb6242 842
843 if (has_capability('mod/survey:readresponses', $PAGE->cm->context)) {
3406acde 844 $responsesnode = $surveynode->add(get_string("responsereports", "survey"));
a5cb6242 845
a6855934 846 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'summary'));
3406acde 847 $responsesnode->add(get_string("summary", "survey"), $url);
a5cb6242 848
a6855934 849 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'scales'));
3406acde 850 $responsesnode->add(get_string("scales", "survey"), $url);
a5cb6242 851
a6855934 852 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'questions'));
3406acde 853 $responsesnode->add(get_string("question", "survey"), $url);
a5cb6242 854
a6855934 855 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'students'));
3406acde 856 $responsesnode->add(get_string('participants'), $url);
a5cb6242 857
858 if (has_capability('mod/survey:download', $PAGE->cm->context)) {
a6855934 859 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'download'));
0b29477b 860 $surveynode->add(get_string('downloadresults', 'survey'), $url);
a5cb6242 861 }
862 }
253063a2 863}