3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 * @global int $SURVEY_GHEIGHT
28 global $SURVEY_GHEIGHT;
29 $SURVEY_GHEIGHT = 500;
32 * @global int $SURVEY_GWIDTH
34 global $SURVEY_GWIDTH;
38 * @global array $SURVEY_QTYPE
41 $SURVEY_QTYPE = array (
42 "-3" => "Virtual Actual and Preferred",
43 "-2" => "Virtual Preferred",
44 "-1" => "Virtual Actual",
48 "3" => "Actual and Preferred",
52 define("SURVEY_COLLES_ACTUAL", "1");
53 define("SURVEY_COLLES_PREFERRED", "2");
54 define("SURVEY_COLLES_PREFERRED_ACTUAL", "3");
55 define("SURVEY_ATTLS", "4");
56 define("SURVEY_CIQ", "5");
59 // STANDARD FUNCTIONS ////////////////////////////////////////////////////////
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.
67 * @param object $survey
70 function survey_add_instance($survey) {
73 if (!$template = $DB->get_record("survey", array("id"=>$survey->template))) {
77 $survey->questions = $template->questions;
78 $survey->timecreated = time();
79 $survey->timemodified = $survey->timecreated;
81 return $DB->insert_record("survey", $survey);
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.
91 * @param object $survey
94 function survey_update_instance($survey) {
97 if (!$template = $DB->get_record("survey", array("id"=>$survey->template))) {
101 $survey->id = $survey->instance;
102 $survey->questions = $template->questions;
103 $survey->timemodified = time();
105 return $DB->update_record("survey", $survey);
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.
117 function survey_delete_instance($id) {
120 if (! $survey = $DB->get_record("survey", array("id"=>$id))) {
126 if (! $DB->delete_records("survey_analysis", array("survey"=>$survey->id))) {
130 if (! $DB->delete_records("survey_answers", array("survey"=>$survey->id))) {
134 if (! $DB->delete_records("survey", array("id"=>$survey->id))) {
143 * @param object $course
144 * @param object $user
146 * @param object $survey
149 function survey_user_outline($course, $user, $mod, $survey) {
152 if ($answers = $DB->get_records("survey_answers", array('survey'=>$survey->id, 'userid'=>$user->id))) {
153 $lastanswer = array_pop($answers);
155 $result = new stdClass();
156 $result->info = get_string("done", "survey");
157 $result->time = $lastanswer->time;
167 * @param object $course
168 * @param object $user
170 * @param object $survey
172 function survey_user_complete($course, $user, $mod, $survey) {
173 global $CFG, $DB, $OUTPUT;
175 if (survey_already_done($survey->id, $user->id)) {
176 if ($survey->template == SURVEY_CIQ) { // print out answers for critical incidents
177 $table = new html_table();
178 $table->align = array("left", "left");
180 $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
181 $questionorder = explode(",", $survey->questions);
183 foreach ($questionorder as $key=>$val) {
184 $question = $questions[$val];
185 $questiontext = get_string($question->shorttext, "survey");
187 if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
188 $answertext = "$answer->answer1";
190 $answertext = "No answer";
192 $table->data[] = array("<b>$questiontext</b>", $answertext);
194 echo html_writer::table($table);
198 survey_print_graph("id=$mod->id&sid=$user->id&type=student.png");
202 print_string("notdone", "survey");
209 * @param object $course
210 * @param mixed $viewfullnames
211 * @param int $timestamp
214 function survey_print_recent_activity($course, $viewfullnames, $timestart) {
215 global $CFG, $DB, $OUTPUT;
217 $modinfo = get_fast_modinfo($course);
219 foreach ($modinfo->cms as $cm) {
220 if ($cm->modname != 'survey') {
223 if (!$cm->uservisible) {
226 $ids[$cm->instance] = $cm->instance;
233 $slist = implode(',', $ids); // there should not be hundreds of glossaries in one course, right?
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));
243 $rs->close(); // Not going to iterate (but exit), close rs
249 foreach ($rs as $survey) {
250 $cm = $modinfo->instances['survey'][$survey->survey];
251 $survey->name = $cm->name;
252 $survey->cmid = $cm->id;
253 $surveys[] = $survey;
261 echo $OUTPUT->heading(get_string('newsurveyresponses', 'survey').':');
262 foreach ($surveys as $survey) {
263 $url = $CFG->wwwroot.'/mod/survey/view.php?id='.$survey->cmid;
264 print_recent_activity_note($survey->time, $survey, $survey->name, $url, false, $viewfullnames);
270 // SQL FUNCTIONS ////////////////////////////////////////////////////////
277 function survey_log_info($log) {
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));
286 * @param int $surveyid
287 * @param int $groupid
288 * @param int $groupingid
291 function survey_get_responses($surveyid, $groupid, $groupingid) {
294 $params = array('surveyid'=>$surveyid, 'groupid'=>$groupid, 'groupingid'=>$groupingid);
297 $groupsjoin = "JOIN {groups_members} gm ON u.id = gm.userid AND gm.groupid = :groupid ";
299 } else if ($groupingid) {
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 ";
306 $userfields = user_picture::fields('u');
307 return $DB->get_records_sql("SELECT $userfields, MAX(a.time) as time
308 FROM {survey_answers} a
309 JOIN {user} u ON a.userid = u.id
311 WHERE a.survey = :surveyid
313 ORDER BY time ASC", $params);
322 function survey_get_analysis($survey, $user) {
325 return $DB->get_record_sql("SELECT notes
326 FROM {survey_analysis}
327 WHERE survey=? AND userid=?", array($survey, $user));
334 * @param string $notes
336 function survey_update_analysis($survey, $user, $notes) {
339 return $DB->execute("UPDATE {survey_analysis}
342 AND userid=?", array($notes, $survey, $user));
347 * @param int $surveyid
348 * @param int $groupid
349 * @param string $sort
352 function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") {
355 $params = array('surveyid'=>$surveyid, 'questionid'=>$questionid);
358 $groupfrom = ', {groups_members} gm';
359 $groupsql = 'AND gm.groupid = :groupid AND u.id = gm.userid';
360 $params['groupid'] = $groupid;
366 $userfields = user_picture::fields('u');
367 return $DB->get_records_sql("SELECT sa.*, $userfields
368 FROM {survey_answers} sa, {user} u $groupfrom
369 WHERE sa.survey = :surveyid
370 AND sa.question = :questionid
371 AND u.id = sa.userid $groupsql
372 ORDER BY $sort", $params);
377 * @param int $surveyid
378 * @param int $questionid
382 function survey_get_user_answer($surveyid, $questionid, $userid) {
385 return $DB->get_record_sql("SELECT sa.*
386 FROM {survey_answers} sa
389 AND sa.userid = ?", array($surveyid, $questionid, $userid));
392 // MODULE FUNCTIONS ////////////////////////////////////////////////////////
397 * @param string $notes
400 function survey_add_analysis($survey, $user, $notes) {
403 $record = new stdClass();
404 $record->survey = $survey;
405 $record->userid = $user;
406 $record->notes = $notes;
408 return $DB->insert_record("survey_analysis", $record, false);
416 function survey_already_done($survey, $user) {
419 return $DB->record_exists("survey_answers", array("survey"=>$survey, "userid"=>$user));
422 * @param int $surveyid
423 * @param int $groupid
424 * @param int $groupingid
427 function survey_count_responses($surveyid, $groupid, $groupingid) {
428 if ($responses = survey_get_responses($surveyid, $groupid, $groupingid)) {
429 return count($responses);
437 * @param array $results
438 * @param int $courseid
440 function survey_print_all_responses($cmid, $results, $courseid) {
442 $table = new html_table();
443 $table->head = array ("", get_string("name"), get_string("time"));
444 $table->align = array ("", "left", "left");
445 $table->size = array (35, "", "" );
447 foreach ($results as $a) {
448 $table->data[] = array($OUTPUT->user_picture($a, array('courseid'=>$courseid)),
449 html_writer::link("report.php?action=student&student=$a->id&id=$cmid", fullname($a)),
453 echo html_writer::table($table);
458 * @param int $templateid
461 function survey_get_template_name($templateid) {
465 if ($ss = $DB->get_record("surveys", array("id"=>$templateid))) {
475 * @param string $name
476 * @param array $numwords
479 function survey_shorten_name ($name, $numwords) {
480 $words = explode(" ", $name);
482 for ($i=0; $i < $numwords; $i++) {
483 $output .= $words[$i]." ";
489 * @todo Check this function
494 * @global void This is never defined
495 * @global object This is defined twice?
496 * @param object $question
498 function survey_print_multi($question) {
499 global $USER, $DB, $qnum, $checklist, $DB, $OUTPUT; //TODO: this is sloppy globals abuse
501 $stripreferthat = get_string("ipreferthat", "survey");
502 $strifoundthat = get_string("ifoundthat", "survey");
503 $strdefault = get_string('notyetanswered', 'survey');
504 $strresponses = get_string('responses', 'survey');
506 echo $OUTPUT->heading($question->text, 3, 'questiontext');
507 echo "\n<table width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" class=\"surveytable\">";
509 $options = explode( ",", $question->options);
510 $numoptions = count($options);
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.
518 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
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) {
528 echo "<tr class=\"smalltext\"><th scope=\"row\">$strresponses</th>";
529 echo "<th scope=\"col\" class=\"hresponse\">". get_string('notyetanswered', 'survey'). "</th>";
530 while (list ($key, $val) = each ($options)) {
531 echo "<th scope=\"col\" class=\"hresponse\">$val</th>\n";
536 echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n";
538 echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n";
541 $subquestions = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi));
543 foreach ($subquestions as $q) {
546 $rowclass = survey_question_rowclass($qnum);
548 $rowclass = survey_question_rowclass(round($qnum / 2));
551 $q->text = get_string($q->text, "survey");
554 echo "<tr class=\"$rowclass rblock\">";
556 echo "<th scope=\"row\" class=\"optioncell\">";
557 echo "<b class=\"qnumtopcell\">$qnum</b> ";
558 echo $q->text ."</th>\n";
560 $default = get_accesshide($strdefault);
561 echo "<td class=\"whitecell\"><label for=\"q$P$q->id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"q$P" . $q->id . "_D\" value=\"0\" checked=\"checked\" />$default</label></td>";
563 for ($i=1;$i<=$numoptions;$i++) {
564 $hiddentext = get_accesshide($options[$i-1]);
565 $id = "q$P" . $q->id . "_$i";
566 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
568 $checklist["q$P$q->id"] = 0;
571 echo "<th scope=\"row\" class=\"optioncell\">";
572 echo "<b class=\"qnumtopcell\">$qnum</b> ";
574 echo "<span class=\"preferthat\">$stripreferthat</span> ";
575 echo "<span class=\"option\">$q->text</span></th>\n";
577 $default = get_accesshide($strdefault);
578 echo '<td class="whitecell"><label for="qP'. $P.$q->id .'"><input type="radio" name="qP'.$P.$q->id. '" id="qP'. $q->id .'" value="0" checked="checked" />'.$default.'</label></td>';
581 for ($i=1;$i<=$numoptions;$i++) {
582 $hiddentext = get_accesshide($options[$i-1]);
583 $id = "qP" . $q->id . "_$i";
584 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"qP$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
588 echo "<tr class=\"$rowclass rblock\">";
589 echo "<th scope=\"row\" class=\"optioncell\">";
590 echo "<b class=\"qnumtopcell\">$qnum</b> ";
591 echo "<span class=\"foundthat\">$strifoundthat</span> ";
592 echo "<span class=\"option\">$q->text</span></th>\n";
594 $default = get_accesshide($strdefault);
595 echo '<td class="whitecell"><label for="q'. $q->id .'"><input type="radio" name="q'.$q->id. '" id="q'. $q->id .'" value="0" checked="checked" />'.$default.'</label></td>';
597 for ($i=1;$i<=$numoptions;$i++) {
598 $hiddentext = get_accesshide($options[$i-1]);
599 $id = "q" . $q->id . "_$i";
600 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
603 $checklist["qP$q->id"] = 0;
604 $checklist["q$q->id"] = 0;
615 * @param object $question
617 function survey_print_single($question) {
618 global $DB, $qnum, $OUTPUT;
620 $rowclass = survey_question_rowclass(0);
625 echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n";
626 echo "<tr class=\"$rowclass\">";
627 echo "<th scope=\"row\" class=\"optioncell\"><label for=\"q$question->id\"><b class=\"qnumtopcell\">$qnum</b> ";
628 echo "<span class=\"questioncell\">$question->text</span></label></th>\n";
629 echo "<td class=\"questioncell smalltext\">\n";
632 if ($question->type == 0) { // Plain text field
633 echo "<textarea rows=\"3\" cols=\"30\" name=\"q$question->id\" id=\"q$question->id\">$question->options</textarea>";
635 } else if ($question->type > 0) { // Choose one of a number
636 $strchoose = get_string("choose");
637 echo "<select name=\"q$question->id\" id=\"q$question->id\">";
638 echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>";
639 $options = explode( ",", $question->options);
640 foreach ($options as $key => $val) {
642 echo "<option value=\"$key\">$val</option>";
646 } else if ($question->type < 0) { // Choose several of a number
647 $options = explode( ",", $question->options);
648 echo $OUTPUT->notification("This question type not supported yet");
651 echo "</td></tr></table>";
660 function survey_question_rowclass($qnum) {
663 return $qnum % 2 ? 'r0' : 'r1';
675 function survey_print_graph($url) {
676 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
678 echo "<img class='resultgraph' height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\"".
679 " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"".get_string("surveygraph", "survey")."\" />";
685 function survey_get_view_actions() {
686 return array('download','view all','view form','view graph','view report');
692 function survey_get_post_actions() {
693 return array('submit');
698 * Implementation of the function for printing the form elements that control
699 * whether the course reset functionality affects the survey.
701 * @param object $mform form passed by reference
703 function survey_reset_course_form_definition(&$mform) {
704 $mform->addElement('header', 'surveyheader', get_string('modulenameplural', 'survey'));
705 $mform->addElement('checkbox', 'reset_survey_answers', get_string('deleteallanswers','survey'));
706 $mform->addElement('checkbox', 'reset_survey_analysis', get_string('deleteanalysis','survey'));
707 $mform->disabledIf('reset_survey_analysis', 'reset_survey_answers', 'checked');
711 * Course reset form defaults.
714 function survey_reset_course_form_defaults($course) {
715 return array('reset_survey_answers'=>1, 'reset_survey_analysis'=>1);
719 * Actual implementation of the reset course functionality, delete all the
720 * survey responses for course $data->courseid.
723 * @param $data the data submitted from the reset course.
724 * @return array status array
726 function survey_reset_userdata($data) {
729 $componentstr = get_string('modulenameplural', 'survey');
732 $surveyssql = "SELECT ch.id
735 $params = array($data->courseid);
737 if (!empty($data->reset_survey_answers)) {
738 $DB->delete_records_select('survey_answers', "survey IN ($surveyssql)", $params);
739 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
740 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
743 if (!empty($data->reset_survey_analysis)) {
744 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
745 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
753 * Returns all other caps used in module
757 function survey_get_extra_capabilities() {
758 return array('moodle/site:accessallgroups');
762 * @uses FEATURE_GROUPS
763 * @uses FEATURE_GROUPINGS
764 * @uses FEATURE_GROUPMEMBERSONLY
765 * @uses FEATURE_MOD_INTRO
766 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
767 * @uses FEATURE_GRADE_HAS_GRADE
768 * @uses FEATURE_GRADE_OUTCOMES
769 * @param string $feature FEATURE_xx constant for requested feature
770 * @return mixed True if module supports feature, false if not, null if doesn't know
772 function survey_supports($feature) {
774 case FEATURE_GROUPS: return true;
775 case FEATURE_GROUPINGS: return true;
776 case FEATURE_GROUPMEMBERSONLY: return true;
777 case FEATURE_MOD_INTRO: return true;
778 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
779 case FEATURE_GRADE_HAS_GRADE: return false;
780 case FEATURE_GRADE_OUTCOMES: return false;
781 case FEATURE_BACKUP_MOODLE2: return true;
782 case FEATURE_SHOW_DESCRIPTION: return true;
784 default: return null;
789 * This function extends the settings navigation block for the site.
791 * It is safe to rely on PAGE here as we will only ever be within the module
792 * context when this is called
794 * @param navigation_node $settings
795 * @param navigation_node $surveynode
797 function survey_extend_settings_navigation($settings, $surveynode) {
800 if (has_capability('mod/survey:readresponses', $PAGE->cm->context)) {
801 $responsesnode = $surveynode->add(get_string("responsereports", "survey"));
803 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'summary'));
804 $responsesnode->add(get_string("summary", "survey"), $url);
806 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'scales'));
807 $responsesnode->add(get_string("scales", "survey"), $url);
809 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'questions'));
810 $responsesnode->add(get_string("question", "survey"), $url);
812 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'students'));
813 $responsesnode->add(get_string('participants'), $url);
815 if (has_capability('mod/survey:download', $PAGE->cm->context)) {
816 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'download'));
817 $surveynode->add(get_string('downloadresults', 'survey'), $url);
823 * Return a list of page types
824 * @param string $pagetype current page type
825 * @param stdClass $parentcontext Block's parent context
826 * @param stdClass $currentcontext Current context of block
828 function survey_page_type_list($pagetype, $parentcontext, $currentcontext) {
829 $module_pagetype = array('mod-survey-*'=>get_string('page-mod-survey-x', 'survey'));
830 return $module_pagetype;