MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[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
77 $survey->questions = $template->questions;
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
101 $survey->id = $survey->instance;
102 $survey->questions = $template->questions;
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
111 * and any data that depends on it.
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);
182
183 foreach ($questionorder as $key=>$val) {
184 $question = $questions[$val];
185 $questiontext = get_string($question->shorttext, "survey");
186
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 }
02f78a26 194 echo $OUTPUT->table($table);
b6b3deb4 195
196 } else {
197
839f2456 198 survey_print_graph("id=$mod->id&amp;sid=$user->id&amp;type=student.png");
b6b3deb4 199 }
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;
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 }
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
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
deb3a60d 354 return $DB->get_record_sql("SELECT notes
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}
369 SET notes=?
370 WHERE survey=?
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
deb3a60d 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
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
deb3a60d 410 return $DB->get_record_sql("SELECT sa.*
411 FROM {survey_answers} sa
412 WHERE sa.survey = ?
413 AND sa.question = ?
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) {
02f78a26 473 $table->data[] = array($OUTPUT->user_picture(moodle_user_picture::make($a, $courseid)),
474 $OUTPUT->link("report.php?action=student&student=$a->id&id=$cmid", fullname($a)),
a30f997e 475 userdate($a->time));
b416a1c3 476 }
7c8c335f 477
02f78a26 478 echo $OUTPUT->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");
532f494d 527 $strdefault = get_string('default');
528 $strresponses = get_string('responses', 'survey');
529
8d849394 530 echo $OUTPUT->heading($question->text, 3, 'questiontext');
532f494d 531 echo "\n<table width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\">";
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>";
0e30c987 544 while (list ($key, $val) = each ($options)) {
532f494d 545 echo "<th scope=\"col\" class=\"hresponse\">$val</th>\n";
546 }
547 echo "<th>&nbsp;</th></tr>\n";
548
549 if ($oneanswer) {
550 echo "<tr><th scope=\"col\" colspan=\"6\">$question->intro</th></tr>\n";
551 } else {
552 echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n";
0e30c987 553 }
0e30c987 554
44e1b7d7 555 $subquestions = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi));
532f494d 556
0e30c987 557 foreach ($subquestions as $q) {
558 $qnum++;
f1c1d2f1 559 $rowclass = survey_question_rowclass($qnum);
f762c448 560 if ($q->text) {
561 $q->text = get_string($q->text, "survey");
562 }
563
532f494d 564 echo "<tr class=\"$rowclass rblock\">";
0e30c987 565 if ($oneanswer) {
3a2e0fe9 566
532f494d 567 echo "<th scope=\"row\" class=\"optioncell\">";
568 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
569 echo $q->text ."</th>\n";
570 for ($i=1;$i<=$numoptions;$i++) {
571 $hiddentext = get_accesshide($options[$i-1]);
572 $id = "q$P" . $q->id . "_$i";
573 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 574 }
532f494d 575 $default = get_accesshide($strdefault, 'label', '', "for=\"q$P$q->id\"");
576 echo "<td class=\"whitecell\"><input type=\"radio\" name=\"q$P$q->id\" id=\"q$P" . $q->id . "_D\" value=\"0\" checked=\"checked\" />$default</td>";
0e30c987 577 $checklist["q$P$q->id"] = $numoptions;
578
3a2e0fe9 579 } else {
f40aee6f 580 // yu : fix for MDL-7501, possibly need to use user flag as this is quite ugly.
532f494d 581 echo "<th scope=\"row\" class=\"optioncell\">";
582 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
5d1a5ef4 583 $qnum++;
532f494d 584 echo "<span class=\"preferthat smalltext\">$stripreferthat</span> &nbsp; ";
585 echo "<span class=\"option\">$q->text</span></th>\n";
0e30c987 586 for ($i=1;$i<=$numoptions;$i++) {
532f494d 587 $hiddentext = get_accesshide($options[$i-1]);
588 $id = "qP" . $q->id . "_$i";
589 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"qP$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 590 }
532f494d 591 $default = get_accesshide($strdefault, 'label', '', "for=\"qP$q->id\"");
592 echo "<td><input type=\"radio\" name=\"qP$q->id\" id=\"qP$q->id\" value=\"0\" checked=\"checked\" />$default</td>";
d1290cec 593 echo "</tr>";
0e30c987 594
532f494d 595 echo "<tr class=\"$rowclass rblock\">";
596 echo "<th scope=\"row\" class=\"optioncell\">";
597 echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
598 echo "<span class=\"foundthat smalltext\">$strifoundthat</span> &nbsp; ";
599 echo "<span class=\"option\">$q->text</span></th>\n";
0e30c987 600 for ($i=1;$i<=$numoptions;$i++) {
532f494d 601 $hiddentext = get_accesshide($options[$i-1]);
602 $id = "q" . $q->id . "_$i";
603 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
0e30c987 604 }
532f494d 605 $default = get_accesshide($strdefault, 'label', '', "for=\"q$q->id\"");
606 echo "<td class=\"buttoncell\"><input type=\"radio\" name=\"q$q->id\" id=\"q$q->id\" value=\"0\" checked=\"checked\" />$default</td>";
0e30c987 607 $checklist["qP$q->id"] = $numoptions;
5d1a5ef4 608 $checklist["q$q->id"] = $numoptions;
0e30c987 609 }
d1290cec 610 echo "</tr>\n";
0e30c987 611 }
d1290cec 612 echo "</table>";
0e30c987 613}
614
615
e5cc530b 616/**
617 * @global object
618 * @global int
619 * @param object $question
620 */
0e30c987 621function survey_print_single($question) {
8b1b0382 622 global $DB, $qnum, $OUTPUT;
0e30c987 623
f1c1d2f1 624 $rowclass = survey_question_rowclass(0);
0e30c987 625
626 $qnum++;
627
caf261b0 628 echo "<br />\n";
795b56b3 629 echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n";
f1c1d2f1 630 echo "<tr class=\"$rowclass\">";
532f494d 631 echo "<th scope=\"row\" class=\"optioncell\"><label for=\"q$question->id\"><b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
632 echo "<span class=\"questioncell\">$question->text</span></label></th>\n";
633 echo "<td class=\"questioncell smalltext\">\n";
0e30c987 634
635
636 if ($question->type == 0) { // Plain text field
532f494d 637 echo "<textarea rows=\"3\" cols=\"30\" name=\"q$question->id\" id=\"q$question->id\">$question->options</textarea>";
0e30c987 638
639 } else if ($question->type > 0) { // Choose one of a number
98899fc0 640 $strchoose = get_string("choose");
532f494d 641 echo "<select name=\"q$question->id\" id=\"q$question->id\">";
e0b7b090 642 echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>";
0e30c987 643 $options = explode( ",", $question->options);
644 foreach ($options as $key => $val) {
645 $key++;
d1290cec 646 echo "<option value=\"$key\">$val</option>";
0e30c987 647 }
d1290cec 648 echo "</select>";
0e30c987 649
650 } else if ($question->type < 0) { // Choose several of a number
651 $options = explode( ",", $question->options);
8b1b0382 652 echo $OUTPUT->notification("This question type not supported yet");
0e30c987 653 }
654
532f494d 655 echo "</td></tr></table>";
0e30c987 656
657}
658
e5cc530b 659/**
660 *
661 * @param int $qnum
662 * @return string
663 */
f1c1d2f1 664function survey_question_rowclass($qnum) {
0e30c987 665
666 if ($qnum) {
f1c1d2f1 667 return $qnum % 2 ? 'r0' : 'r1';
0e30c987 668 } else {
f1c1d2f1 669 return 'r0';
0e30c987 670 }
671}
672
e5cc530b 673/**
674 * @global object
675 * @global int
676 * @global int
677 * @param string $url
678 */
607809b3 679function survey_print_graph($url) {
680 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
681
682 if (empty($CFG->gdversion)) {
683 echo "(".get_string("gdneed").")";
684
685 } else {
795b56b3 686 echo "<img class='resultgraph' height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\"".
7150b8ae 687 " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"".get_string("surveygraph", "survey")."\" />";
607809b3 688 }
689}
690
e5cc530b 691/**
692 * @return array
693 */
f3221af9 694function survey_get_view_actions() {
695 return array('download','view all','view form','view graph','view report');
696}
697
e5cc530b 698/**
699 * @return array
700 */
f3221af9 701function survey_get_post_actions() {
702 return array('submit');
703}
704
0b5a80a1 705
706/**
707 * Implementation of the function for printing the form elements that control
708 * whether the course reset functionality affects the survey.
e5cc530b 709 *
710 * @param object $mform form passed by reference
0b5a80a1 711 */
712function survey_reset_course_form_definition(&$mform) {
713 $mform->addElement('header', 'surveyheader', get_string('modulenameplural', 'survey'));
714 $mform->addElement('checkbox', 'reset_survey_answers', get_string('deleteallanswers','survey'));
715 $mform->addElement('checkbox', 'reset_survey_analysis', get_string('deleteanalysis','survey'));
716 $mform->disabledIf('reset_survey_analysis', 'reset_survey_answers', 'checked');
717}
718
719/**
720 * Course reset form defaults.
e5cc530b 721 * @return array
0b5a80a1 722 */
723function survey_reset_course_form_defaults($course) {
724 return array('reset_survey_answers'=>1, 'reset_survey_analysis'=>1);
725}
726
727/**
728 * Actual implementation of the rest coures functionality, delete all the
729 * survey responses for course $data->courseid.
e5cc530b 730 *
731 * @global object
0b5a80a1 732 * @param $data the data submitted from the reset course.
733 * @return array status array
734 */
735function survey_reset_userdata($data) {
deb3a60d 736 global $DB;
0b5a80a1 737
738 $componentstr = get_string('modulenameplural', 'survey');
739 $status = array();
740
741 $surveyssql = "SELECT ch.id
deb3a60d 742 FROM {survey} ch
743 WHERE ch.course=?";
744 $params = array($data->courseid);
0b5a80a1 745
746 if (!empty($data->reset_survey_answers)) {
deb3a60d 747 $DB->delete_records_select('survey_answers', "survey IN ($surveyssql)", $params);
748 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
0b5a80a1 749 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
750 }
751
752 if (!empty($data->reset_survey_analysis)) {
deb3a60d 753 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
0b5a80a1 754 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
755 }
756
757 // no date shifting
758 return $status;
759}
760
f432bebf 761/**
762 * Returns all other caps used in module
e5cc530b 763 *
764 * @return array
f432bebf 765 */
766function survey_get_extra_capabilities() {
767 return array('moodle/site:accessallgroups');
768}
769
42f103be 770/**
e5cc530b 771 * @uses FEATURE_GROUPS
772 * @uses FEATURE_GROUPINGS
773 * @uses FEATURE_GROUPMEMBERSONLY
774 * @uses FEATURE_MOD_INTRO
775 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
776 * @uses FEATURE_GRADE_HAS_GRADE
777 * @uses FEATURE_GRADE_OUTCOMES
42f103be 778 * @param string $feature FEATURE_xx constant for requested feature
e5cc530b 779 * @return mixed True if module supports feature, false if not, null if doesn't know
42f103be 780 */
781function survey_supports($feature) {
782 switch($feature) {
783 case FEATURE_GROUPS: return true;
784 case FEATURE_GROUPINGS: return true;
785 case FEATURE_GROUPMEMBERSONLY: return true;
dc5c2bd9 786 case FEATURE_MOD_INTRO: return true;
42f103be 787 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
788 case FEATURE_GRADE_HAS_GRADE: return true;
789 case FEATURE_GRADE_OUTCOMES: return true;
790
791 default: return null;
792 }
17da2e6f 793}
a5cb6242 794
795/**
796 * This fucntion extends the global navigaiton for the site.
797 * It is important to note that you should not rely on PAGE objects within this
798 * body of code as there is no guarantee that during an AJAX request they are
799 * available
800 *
801 * @param navigation_node $navigation The quiz node within the global navigation
802 * @param stdClass $course The course object returned from the DB
803 * @param stdClass $module The module object returned from the DB
804 * @param stdClass $cm The course module isntance returned from the DB
805 */
806function survey_extend_navigation($navigation, $course, $module, $cm) {
807 /**
808 * This is currently just a stub so that it can be easily expanded upon.
809 * When expanding just remove this comment and the line below and then add
810 * you content.
811 */
812 $navigation->nodetype = navigation_node::NODETYPE_LEAF;
813}
814
815/**
816 * This function extends the settings navigation block for the site.
817 *
818 * It is safe to rely on PAGE here as we will only ever be within the module
819 * context when this is called
820 *
821 * @param navigation_node $settings
822 * @param stdClass $module
823 */
824function survey_extend_settings_navigation($settings, $module) {
825 global $PAGE, $CFG, $DB, $USER, $OUTPUT;
826
827 $survey = $DB->get_record('quiz', array('id'=>$PAGE->cm->instance));
828 $surveynavkey = $settings->add(get_string('surveyadministration', 'survey'));
829 $surveynav = $settings->get($surveynavkey);
830 $surveynav->forceopen = true;
831
832 $strreport = get_string("report", "survey");
833 $strsurvey = get_string("modulename", "survey");
834 $strsurveys = get_string("modulenameplural", "survey");
835 $strsummary = get_string("summary", "survey");
836 $strscales = get_string("scales", "survey");
837 $strquestion = get_string("question", "survey");
838 $strquestions = get_string("questions", "survey");
839 $strdownload = get_string("download", "survey");
840 $strallscales = get_string("allscales", "survey");
841 $strallquestions = get_string("allquestions", "survey");
842 $strselectedquestions = get_string("selectedquestions", "survey");
843 $strseemoredetail = get_string("seemoredetail", "survey");
844 $strnotes = get_string("notes", "survey");
845
846 if (has_capability('mod/survey:readresponses', $PAGE->cm->context)) {
847 $key = $surveynav->add(get_string("responsereports", "survey"));
848
849 $url = new moodle_url($CFG->wwwroot.'/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'summary'));
850 $surveynav->get($key)->add(get_string("summary", "survey"), $url);
851
852 $url = new moodle_url($CFG->wwwroot.'/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'scales'));
853 $surveynav->get($key)->add(get_string("scales", "survey"), $url);
854
855 $url = new moodle_url($CFG->wwwroot.'/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'questions'));
856 $surveynav->get($key)->add(get_string("question", "survey"), $url);
857
858 $url = new moodle_url($CFG->wwwroot.'/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'students'));
859 $surveynav->get($key)->add(get_string('participants'), $url);
860
861 if (has_capability('mod/survey:download', $PAGE->cm->context)) {
862 $url = new moodle_url($CFG->wwwroot.'/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'download'));
863 $surveynav->add(get_string('downloadresults', 'survey'), $url);
864 }
865 }
866
867 if (has_capability('moodle/course:manageactivities', $PAGE->cm->context)) {
868 $url = new moodle_url($CFG->wwwroot.'/course/mod.php', array('update' => $PAGE->cm->id, 'return' => true, 'sesskey' => sesskey()));
869 $surveynav->add(get_string('updatethis', '', get_string('modulename', 'quiz')), $url);
870 }
871
872 if (count($surveynav->children)<1) {
873 $settings->remove_child($surveynavkey);
874 }
875}