moodle/mod/survey/view.php

206 lines
7.5 KiB
PHP
Raw Normal View History

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file is responsible for displaying the survey
*
* @package mod-survey
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
2001-11-22 06:23:56 +00:00
require_once("../../config.php");
require_once("lib.php");
2001-11-22 06:23:56 +00:00
$id = required_param('id', PARAM_INT); // Course Module ID
2001-11-22 06:23:56 +00:00
if (! $cm = get_coursemodule_from_id('survey', $id)) {
2008-06-15 09:14:55 +00:00
print_error('invalidcoursemodule');
2001-11-22 06:23:56 +00:00
}
2008-06-08 14:43:39 +00:00
if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
2008-06-15 09:14:55 +00:00
print_error('coursemisconf');
2001-11-22 06:23:56 +00:00
}
$PAGE->set_url('/mod/survey/view.php', array('id'=>$id));
require_login($course->id, false, $cm);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/survey:participate', $context);
2001-11-22 06:23:56 +00:00
2008-06-08 14:43:39 +00:00
if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
2008-06-15 09:14:55 +00:00
print_error('invalidsurveyid', 'survey');
2001-11-22 06:23:56 +00:00
}
$trimmedintro = trim($survey->intro);
if (empty($trimmedintro)) {
2008-06-08 14:43:39 +00:00
$tempo = $DB->get_field("survey", "intro", array("id"=>$survey->template));
$survey->intro = get_string($tempo, "survey");
}
2008-06-08 14:43:39 +00:00
if (! $template = $DB->get_record("survey", array("id"=>$survey->template))) {
2008-06-15 09:14:55 +00:00
print_error('invalidtmptid', 'survey');
}
$showscales = ($template->name != 'ciqname');
2002-08-12 17:54:13 +00:00
$strsurvey = get_string("modulename", "survey");
$PAGE->set_title(format_string($survey->name));
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
2001-11-22 06:23:56 +00:00
2004-02-20 16:03:05 +00:00
/// Check to see if groups are being used in this survey
if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
$currentgroup = groups_get_activity_group($cm);
2004-02-20 16:03:05 +00:00
} else {
$currentgroup = 0;
}
$groupingid = $cm->groupingid;
if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
2006-08-09 13:39:44 +00:00
$currentgroup = 0;
2004-02-20 16:03:05 +00:00
}
if (has_capability('mod/survey:readresponses', $context)) {
$numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
echo "<div class=\"reportlink\"><a href=\"report.php?id=$cm->id\">".
get_string("viewsurveyresponses", "survey", $numusers)."</a></div>";
} else if (!$cm->visible) {
notice(get_string("activityiscurrentlyhidden"));
2001-11-22 06:23:56 +00:00
}
if (!is_enrolled($context)) {
echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
2003-05-06 03:06:06 +00:00
}
2001-11-22 06:23:56 +00:00
// Check the survey hasn't already been filled out.
if (survey_already_done($survey->id, $USER->id)) {
2004-01-31 15:22:04 +00:00
add_to_log($course->id, "survey", "view graph", "view.php?id=$cm->id", $survey->id, $cm->id);
$numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
if ($showscales) {
echo $OUTPUT->heading(get_string("surveycompleted", "survey"));
echo $OUTPUT->heading(get_string("peoplecompleted", "survey", $numusers));
2007-01-08 05:47:12 +00:00
echo '<div class="resultgraph">';
survey_print_graph("id=$cm->id&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
2007-01-08 05:47:12 +00:00
echo '</div>';
} else {
2004-02-16 17:14:07 +00:00
echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox', 'intro');
echo $OUTPUT->spacer(array('height'=>30, 'width'=>1, 'br'=>true)); // should be done with CSS instead
2004-02-16 17:14:07 +00:00
$questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
$questionorder = explode(",", $survey->questions);
foreach ($questionorder as $key => $val) {
$question = $questions[$val];
if ($question->type == 0 or $question->type == 1) {
if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
$table = new html_table();
$table->head = array(get_string($question->text, "survey"));
$table->align = array ("left");
$table->data[] = array(s($answer->answer1));//no html here, just plain text
echo html_writer::table($table);
echo $OUTPUT->spacer(clone($spacer)) . '<br />';
}
}
}
}
echo $OUTPUT->footer();
2001-11-22 06:23:56 +00:00
exit;
}
// Start the survey form
2004-01-31 15:22:04 +00:00
add_to_log($course->id, "survey", "view form", "view.php?id=$cm->id", $survey->id, $cm->id);
2001-11-22 06:23:56 +00:00
echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
2007-01-08 05:47:12 +00:00
echo '<div>';
echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
2009-11-20 08:33:33 +00:00
echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
2001-11-22 06:23:56 +00:00
echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox boxaligncenter bowidthnormal', 'intro');
echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
2001-11-22 06:23:56 +00:00
// Get all the major questions and their proper order
if (! $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions))) {
2008-06-15 09:14:55 +00:00
print_error('cannotfindquestion', 'survey');
2001-11-22 06:23:56 +00:00
}
$questionorder = explode( ",", $survey->questions);
// Cycle through all the questions in order and print them
$qnum = 0;
foreach ($questionorder as $key => $val) {
$question = $questions["$val"];
$question->id = $val;
if ($question->type >= 0) {
if ($question->text) {
$question->text = get_string($question->text, "survey");
}
if ($question->shorttext) {
$question->shorttext = get_string($question->shorttext, "survey");
}
if ($question->intro) {
$question->intro = get_string($question->intro, "survey");
}
if ($question->options) {
$question->options = get_string($question->options, "survey");
}
if ($question->multi) {
survey_print_multi($question);
} else {
survey_print_single($question);
}
2001-11-22 06:23:56 +00:00
}
}
if (!is_enrolled($context)) {
echo '</div>';
2003-05-06 03:06:06 +00:00
echo "</form>";
echo $OUTPUT->footer();
2003-05-06 03:06:06 +00:00
exit;
}
$checkarray = Array('questions'=>Array());
if (!empty($checklist)) {
foreach ($checklist as $question => $default) {
$checkarray['questions'][] = Array('question'=>$question, 'default'=>$default);
}
2001-11-22 06:23:56 +00:00
}
$PAGE->requires->js('/mod/survey/survey.js');
$PAGE->requires->data_for_js('surveycheck', $checkarray);
$PAGE->requires->string_for_js('questionsnotanswered', 'survey');
$PAGE->requires->js_function_call('survey_attach_onsubmit');
2001-11-22 06:23:56 +00:00
echo '<br />';
echo '<input type="submit" value="'.get_string("clicktocontinue", "survey").'" />';
echo '</div>';
echo "</form>";
2001-11-22 06:23:56 +00:00
echo $OUTPUT->footer();
2001-11-22 06:23:56 +00:00