Fixes to the PaintWeb cron task.
[moodle/mihaisucan.git] / mod / survey / view.php
blob230ee0061e3aa21c0f2fb5fb050736638de50cc3
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 $strsurvey = get_string("modulename", "survey");
38 $navigation = build_navigation('', $cm);
39 print_header_simple(format_string($survey->name), "", $navigation, "", "", true,
40 update_module_button($cm->id, $course->id, $strsurvey), navmenu($course, $cm));
42 /// Check to see if groups are being used in this survey
43 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
44 $currentgroup = groups_get_activity_group($cm);
45 } else {
46 $currentgroup = 0;
48 $groupingid = $cm->groupingid;
50 if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
51 $currentgroup = 0;
54 if (has_capability('mod/survey:readresponses', $context)) {
55 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
56 echo "<div class=\"reportlink\"><a href=\"report.php?id=$cm->id\">".
57 get_string("viewsurveyresponses", "survey", $numusers)."</a></div>";
58 } else if (!$cm->visible) {
59 notice(get_string("activityiscurrentlyhidden"));
62 if (isguest()) {
63 notify(get_string("guestsnotallowed", "survey"));
67 // Check the survey hasn't already been filled out.
69 if (survey_already_done($survey->id, $USER->id)) {
71 add_to_log($course->id, "survey", "view graph", "view.php?id=$cm->id", $survey->id, $cm->id);
72 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
74 if ($showscales) {
75 print_heading(get_string("surveycompleted", "survey"));
76 print_heading(get_string("peoplecompleted", "survey", $numusers));
77 echo '<div class="resultgraph">';
78 survey_print_graph("id=$cm->id&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
79 echo '</div>';
81 } else {
83 print_box(format_text($survey->intro), 'generalbox', 'intro');
84 print_spacer(30);
86 $questions = get_records_list("survey_questions", "id", $survey->questions);
87 $questionorder = explode(",", $survey->questions);
88 foreach ($questionorder as $key => $val) {
89 $question = $questions[$val];
90 if ($question->type == 0 or $question->type == 1) {
91 if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
92 $table = NULL;
93 $table->head = array(get_string($question->text, "survey"));
94 $table->align = array ("left");
95 $table->data[] = array(s($answer->answer1));//no html here, just plain text
96 print_table($table);
97 print_spacer(30);
103 print_footer($course);
104 exit;
107 // Start the survey form
108 add_to_log($course->id, "survey", "view form", "view.php?id=$cm->id", $survey->id, $cm->id);
110 echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
111 echo '<div>';
112 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
114 print_simple_box(format_text($survey->intro), 'center', '70%', '', 5, 'generalbox', 'intro');
116 // Get all the major questions and their proper order
117 if (! $questions = get_records_list("survey_questions", "id", $survey->questions)) {
118 error("Couldn't find any questions in this survey!!");
120 $questionorder = explode( ",", $survey->questions);
122 // Cycle through all the questions in order and print them
124 $qnum = 0;
125 foreach ($questionorder as $key => $val) {
126 $question = $questions["$val"];
127 $question->id = $val;
129 if ($question->type >= 0) {
131 if ($question->text) {
132 $question->text = get_string($question->text, "survey");
135 if ($question->shorttext) {
136 $question->shorttext = get_string($question->shorttext, "survey");
139 if ($question->intro) {
140 $question->intro = get_string($question->intro, "survey");
143 if ($question->options) {
144 $question->options = get_string($question->options, "survey");
147 if ($question->multi) {
148 survey_print_multi($question);
149 } else {
150 survey_print_single($question);
155 if (isguest()) {
156 echo '</div>';
157 echo "</form>";
158 print_footer($course);
159 exit;
164 <br />
165 <script type="text/javascript">
166 <!--
167 function checkform() {
169 var error=false;
171 with (document.getElementById('surveyform')) {
172 <?php
173 if (!empty($checklist)) {
174 foreach ($checklist as $question => $default) {
175 echo " if (".$question."[".$default."].checked) error=true;\n";
181 if (error) {
182 alert("<?php print_string("questionsnotanswered", "survey") ?>");
183 } else {
184 document.getElementById('surveyform').submit();
188 <?php echo "document.write('<input type=\"button\" value=\"".get_string("clicktocontinuecheck", "survey")."\" onClick=\"checkform()\" />');"; ?>
190 // END -->
191 </script>
193 <noscript>
194 <!-- Without Javascript, no checking is done -->
195 <div>
196 <input type="submit" value="<?php get_string("clicktocontinue", "survey") ?>" />
197 </div>
198 </noscript>
200 <?php
201 echo '</div>';
202 echo "</form>";
204 print_footer($course);