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 $oneanswer = ($question->type
== 1 ||
$question->type
== 2) ?
true : false;
514 echo "<tr class=\"smalltext\"><th scope=\"row\">$strresponses</th>";
515 echo "<th scope=\"col\" class=\"hresponse\">". get_string('notyetanswered', 'survey'). "</th>";
516 while (list ($key, $val) = each ($options)) {
517 echo "<th scope=\"col\" class=\"hresponse\">$val</th>\n";
522 echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n";
524 echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n";
527 $subquestions = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi
));
529 foreach ($subquestions as $q) {
531 $rowclass = survey_question_rowclass($qnum);
533 $q->text
= get_string($q->text
, "survey");
536 $oneanswer = ($q->type
== 1 ||
$q->type
== 2) ?
true : false;
543 echo "<tr class=\"$rowclass rblock\">";
545 echo "<th scope=\"row\" class=\"optioncell\">";
546 echo "<b class=\"qnumtopcell\">$qnum</b> ";
547 echo $q->text
."</th>\n";
549 $default = get_accesshide($strdefault);
550 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>";
552 for ($i=1;$i<=$numoptions;$i++
) {
553 $hiddentext = get_accesshide($options[$i-1]);
554 $id = "q$P" . $q->id
. "_$i";
555 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
557 $checklist["q$P$q->id"] = 0;
560 // yu : fix for MDL-7501, possibly need to use user flag as this is quite ugly.
561 echo "<th scope=\"row\" class=\"optioncell\">";
562 echo "<b class=\"qnumtopcell\">$qnum</b> ";
564 echo "<span class=\"preferthat smalltext\">$stripreferthat</span> ";
565 echo "<span class=\"option\">$q->text</span></th>\n";
567 $default = get_accesshide($strdefault);
568 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>';
571 for ($i=1;$i<=$numoptions;$i++
) {
572 $hiddentext = get_accesshide($options[$i-1]);
573 $id = "qP" . $q->id
. "_$i";
574 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"qP$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
578 echo "<tr class=\"$rowclass rblock\">";
579 echo "<th scope=\"row\" class=\"optioncell\">";
580 echo "<b class=\"qnumtopcell\">$qnum</b> ";
581 echo "<span class=\"foundthat smalltext\">$strifoundthat</span> ";
582 echo "<span class=\"option\">$q->text</span></th>\n";
584 $default = get_accesshide($strdefault);
585 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>';
587 for ($i=1;$i<=$numoptions;$i++
) {
588 $hiddentext = get_accesshide($options[$i-1]);
589 $id = "q" . $q->id
. "_$i";
590 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
593 $checklist["qP$q->id"] = 0;
594 $checklist["q$q->id"] = 0;
605 * @param object $question
607 function survey_print_single($question) {
608 global $DB, $qnum, $OUTPUT;
610 $rowclass = survey_question_rowclass(0);
615 echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n";
616 echo "<tr class=\"$rowclass\">";
617 echo "<th scope=\"row\" class=\"optioncell\"><label for=\"q$question->id\"><b class=\"qnumtopcell\">$qnum</b> ";
618 echo "<span class=\"questioncell\">$question->text</span></label></th>\n";
619 echo "<td class=\"questioncell smalltext\">\n";
622 if ($question->type
== 0) { // Plain text field
623 echo "<textarea rows=\"3\" cols=\"30\" name=\"q$question->id\" id=\"q$question->id\">$question->options</textarea>";
625 } else if ($question->type
> 0) { // Choose one of a number
626 $strchoose = get_string("choose");
627 echo "<select name=\"q$question->id\" id=\"q$question->id\">";
628 echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>";
629 $options = explode( ",", $question->options
);
630 foreach ($options as $key => $val) {
632 echo "<option value=\"$key\">$val</option>";
636 } else if ($question->type
< 0) { // Choose several of a number
637 $options = explode( ",", $question->options
);
638 echo $OUTPUT->notification("This question type not supported yet");
641 echo "</td></tr></table>";
650 function survey_question_rowclass($qnum) {
653 return $qnum %
2 ?
'r0' : 'r1';
665 function survey_print_graph($url) {
666 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
668 echo "<img class='resultgraph' height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\"".
669 " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"".get_string("surveygraph", "survey")."\" />";
675 function survey_get_view_actions() {
676 return array('download','view all','view form','view graph','view report');
682 function survey_get_post_actions() {
683 return array('submit');
688 * Implementation of the function for printing the form elements that control
689 * whether the course reset functionality affects the survey.
691 * @param object $mform form passed by reference
693 function survey_reset_course_form_definition(&$mform) {
694 $mform->addElement('header', 'surveyheader', get_string('modulenameplural', 'survey'));
695 $mform->addElement('checkbox', 'reset_survey_answers', get_string('deleteallanswers','survey'));
696 $mform->addElement('checkbox', 'reset_survey_analysis', get_string('deleteanalysis','survey'));
697 $mform->disabledIf('reset_survey_analysis', 'reset_survey_answers', 'checked');
701 * Course reset form defaults.
704 function survey_reset_course_form_defaults($course) {
705 return array('reset_survey_answers'=>1, 'reset_survey_analysis'=>1);
709 * Actual implementation of the reset course functionality, delete all the
710 * survey responses for course $data->courseid.
713 * @param $data the data submitted from the reset course.
714 * @return array status array
716 function survey_reset_userdata($data) {
719 $componentstr = get_string('modulenameplural', 'survey');
722 $surveyssql = "SELECT ch.id
725 $params = array($data->courseid
);
727 if (!empty($data->reset_survey_answers
)) {
728 $DB->delete_records_select('survey_answers', "survey IN ($surveyssql)", $params);
729 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
730 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
733 if (!empty($data->reset_survey_analysis
)) {
734 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
735 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
743 * Returns all other caps used in module
747 function survey_get_extra_capabilities() {
748 return array('moodle/site:accessallgroups');
752 * @uses FEATURE_GROUPS
753 * @uses FEATURE_GROUPINGS
754 * @uses FEATURE_GROUPMEMBERSONLY
755 * @uses FEATURE_MOD_INTRO
756 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
757 * @uses FEATURE_GRADE_HAS_GRADE
758 * @uses FEATURE_GRADE_OUTCOMES
759 * @param string $feature FEATURE_xx constant for requested feature
760 * @return mixed True if module supports feature, false if not, null if doesn't know
762 function survey_supports($feature) {
764 case FEATURE_GROUPS
: return true;
765 case FEATURE_GROUPINGS
: return true;
766 case FEATURE_GROUPMEMBERSONLY
: return true;
767 case FEATURE_MOD_INTRO
: return true;
768 case FEATURE_COMPLETION_TRACKS_VIEWS
: return true;
769 case FEATURE_GRADE_HAS_GRADE
: return false;
770 case FEATURE_GRADE_OUTCOMES
: return false;
771 case FEATURE_BACKUP_MOODLE2
: return true;
772 case FEATURE_SHOW_DESCRIPTION
: return true;
774 default: return null;
779 * This function extends the settings navigation block for the site.
781 * It is safe to rely on PAGE here as we will only ever be within the module
782 * context when this is called
784 * @param navigation_node $settings
785 * @param navigation_node $surveynode
787 function survey_extend_settings_navigation($settings, $surveynode) {
790 if (has_capability('mod/survey:readresponses', $PAGE->cm
->context
)) {
791 $responsesnode = $surveynode->add(get_string("responsereports", "survey"));
793 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm
->id
, 'action'=>'summary'));
794 $responsesnode->add(get_string("summary", "survey"), $url);
796 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm
->id
, 'action'=>'scales'));
797 $responsesnode->add(get_string("scales", "survey"), $url);
799 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm
->id
, 'action'=>'questions'));
800 $responsesnode->add(get_string("question", "survey"), $url);
802 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm
->id
, 'action'=>'students'));
803 $responsesnode->add(get_string('participants'), $url);
805 if (has_capability('mod/survey:download', $PAGE->cm
->context
)) {
806 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm
->id
, 'action'=>'download'));
807 $surveynode->add(get_string('downloadresults', 'survey'), $url);
813 * Return a list of page types
814 * @param string $pagetype current page type
815 * @param stdClass $parentcontext Block's parent context
816 * @param stdClass $currentcontext Current context of block
818 function survey_page_type_list($pagetype, $parentcontext, $currentcontext) {
819 $module_pagetype = array('mod-survey-*'=>get_string('page-mod-survey-x', 'survey'));
820 return $module_pagetype;