calendar/lib: calendar_set_filters() use pre-fetched context and course recs
[moodle-pu.git] / mod / survey / view.php
blob3bb956496ab8e0a6aa3ff24b5e2fa8e8f7596c77
1 <?php // $Id$
3 require_once("../../config.php");
4 require_once("lib.php");
6 $id = required_param('id', PARAM_INT); // Course Module ID
8 if (! $cm = get_coursemodule_from_id('survey', $id)) {
9 error("Course Module ID was incorrect");
12 if (! $course = get_record("course", "id", $cm->course)) {
13 error("Course is misconfigured");
16 require_login($course->id, false, $cm);
18 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
20 require_capability('mod/survey:participate', $context);
22 if (! $survey = get_record("survey", "id", $cm->instance)) {
23 error("Survey ID was incorrect");
25 $trimmedintro = trim($survey->intro);
26 if (empty($trimmedintro)) {
27 $tempo = get_field("survey", "intro", "id", $survey->template);
28 $survey->intro = get_string($tempo, "survey");
31 if (! $template = get_record("survey", "id", $survey->template)) {
32 error("Template ID was incorrect");
35 $showscales = ($template->name != 'ciqname');
37 $strsurveys = get_string("modulenameplural", "survey");
38 $strsurvey = get_string("modulename", "survey");
40 $navlinks = array();
41 $navlinks[] = array('name' => $strsurveys, 'link' => "index.php?id=$course->id", 'type' => 'activity');;
42 $navlinks[] = array('name' => format_string($survey->name), 'link' => '', 'type' => 'activityinistance');
43 $navigation = build_navigation($navlinks);
45 print_header_simple(format_string($survey->name), "", $navigation, "", "", true,
46 update_module_button($cm->id, $course->id, $strsurvey), navmenu($course, $cm));
48 /// Check to see if groups are being used in this survey
49 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
50 $currentgroup = groups_get_activity_group($cm);
51 } else {
52 $currentgroup = 0;
54 $groupingid = $cm->groupingid;
56 if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
57 $currentgroup = 0;
60 if (has_capability('mod/survey:readresponses', $context)) {
61 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
62 echo "<div class=\"reportlink\"><a href=\"report.php?id=$cm->id\">".
63 get_string("viewsurveyresponses", "survey", $numusers)."</a></div>";
64 } else if (!$cm->visible) {
65 notice(get_string("activityiscurrentlyhidden"));
68 if (isguest()) {
69 notify(get_string("guestsnotallowed", "survey"));
73 // Check the survey hasn't already been filled out.
75 if (survey_already_done($survey->id, $USER->id)) {
77 add_to_log($course->id, "survey", "view graph", "view.php?id=$cm->id", $survey->id, $cm->id);
78 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
80 if ($showscales) {
81 print_heading(get_string("surveycompleted", "survey"));
82 print_heading(get_string("peoplecompleted", "survey", $numusers));
83 echo '<div class="resultgraph">';
84 survey_print_graph("id=$cm->id&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
85 echo '</div>';
87 } else {
89 print_box(format_text($survey->intro), 'generalbox', 'intro');
90 print_spacer(30);
92 $questions = get_records_list("survey_questions", "id", $survey->questions);
93 $questionorder = explode(",", $survey->questions);
94 foreach ($questionorder as $key => $val) {
95 $question = $questions[$val];
96 if ($question->type == 0 or $question->type == 1) {
97 if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
98 $table = NULL;
99 $table->head = array(get_string($question->text, "survey"));
100 $table->align = array ("left");
101 $table->data[] = array(s($answer->answer1));//no html here, just plain text
102 print_table($table);
103 print_spacer(30);
109 print_footer($course);
110 exit;
113 // Start the survey form
114 add_to_log($course->id, "survey", "view form", "view.php?id=$cm->id", $survey->id, $cm->id);
116 echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
117 echo '<div>';
118 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
120 print_simple_box(format_text($survey->intro), 'center', '70%', '', 5, 'generalbox', 'intro');
122 // Get all the major questions and their proper order
123 if (! $questions = get_records_list("survey_questions", "id", $survey->questions)) {
124 error("Couldn't find any questions in this survey!!");
126 $questionorder = explode( ",", $survey->questions);
128 // Cycle through all the questions in order and print them
130 $qnum = 0;
131 foreach ($questionorder as $key => $val) {
132 $question = $questions["$val"];
133 $question->id = $val;
135 if ($question->type >= 0) {
137 if ($question->text) {
138 $question->text = get_string($question->text, "survey");
141 if ($question->shorttext) {
142 $question->shorttext = get_string($question->shorttext, "survey");
145 if ($question->intro) {
146 $question->intro = get_string($question->intro, "survey");
149 if ($question->options) {
150 $question->options = get_string($question->options, "survey");
153 if ($question->multi) {
154 survey_print_multi($question);
155 } else {
156 survey_print_single($question);
161 if (isguest()) {
162 echo '</div>';
163 echo "</form>";
164 print_footer($course);
165 exit;
170 <br />
171 <script type="text/javascript">
172 <!--
173 function checkform() {
175 var error=false;
177 with (document.getElementById('surveyform')) {
178 <?php
179 if (!empty($checklist)) {
180 foreach ($checklist as $question => $default) {
181 echo " if (".$question."[".$default."].checked) error=true;\n";
187 if (error) {
188 alert("<?php print_string("questionsnotanswered", "survey") ?>");
189 } else {
190 document.getElementById('surveyform').submit();
194 <?php echo "document.write('<input type=\"button\" value=\"".get_string("clicktocontinuecheck", "survey")."\" onClick=\"checkform()\" />');"; ?>
196 // END -->
197 </script>
199 <noscript>
200 <!-- Without Javascript, no checking is done -->
201 <div>
202 <input type="submit" value="<?php get_string("clicktocontinue", "survey") ?>" />
203 </div>
204 </noscript>
206 <?php
207 echo '</div>';
208 echo "</form>";
210 print_footer($course);