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