Merged fix for bug MDL-5866.
[moodle.git] / question / preview.php
CommitLineData
516cf3eb 1<?php // $Id$
2/**
3* This page displays a preview of a question
4*
e586cfb4 5* The preview uses the option settings from the activity within which the question
6* is previewed or the default settings if no activity is specified. The question session
516cf3eb 7* information is stored in the session as an array of subsequent states rather
8* than in the database.
e586cfb4 9*
10* TODO: make this work with activities other than quiz
11*
516cf3eb 12* @version $Id$
13* @author Alex Smith as part of the Serving Mathematics project
14* {@link http://maths.york.ac.uk/serving_maths}
15* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
e586cfb4 16* @package question
516cf3eb 17*/
18
e586cfb4 19 require_once("../config.php");
20 require_once($CFG->libdir.'/questionlib.php');
21 require_once($CFG->dirroot.'/mod/quiz/locallib.php'); // We really want to get rid of this
516cf3eb 22
23 $id = required_param('id', PARAM_INT); // question id
24 // if no quiz id is specified then a dummy quiz with default options is used
25 $quizid = optional_param('quizid', 0, PARAM_INT);
26 // Test if we are continuing an attempt at a question
27 $continue = optional_param('continue', 0, PARAM_BOOL);
28 // Check for any of the submit buttons
29 $fillcorrect = optional_param('fillcorrect', 0, PARAM_BOOL);
30 $markall = optional_param('markall', 0, PARAM_BOOL);
31 $finishattempt = optional_param('finishattempt', 0, PARAM_BOOL);
32 $back = optional_param('back', 0, PARAM_BOOL);
33 $startagain = optional_param('startagain', 0, PARAM_BOOL);
34 // We are always continuing an attempt if a submit button was pressed with the
35 // exception of the start again button
36 if ($fillcorrect || $markall || $finishattempt || $back) {
37 $continue = true;
38 } else if ($startagain) {
39 $continue = false;
40 }
41
42 require_login();
43
44 if (!isteacherinanycourse()) {
45 error('This page is for teachers only');
46 }
47
48 if (!$continue) {
49 // Start a new attempt; delete the old session
50 unset($SESSION->quizpreview);
51 // Redirect to ourselves but with continue=1; prevents refreshing the page
52 // from restarting an attempt (needed so that random questions don't change)
53 $quizid = $quizid ? '&amp;quizid=' . $quizid : '';
e586cfb4 54 redirect($CFG->wwwroot . '/question/preview.php?id=' . $id . $quizid .
516cf3eb 55 '&amp;continue=1');
56 }
57
58 if (empty($quizid)) {
219e92f2 59 $quiz = new cmoptions;
60 $quiz->id = 0;
6af1e0b2 61 $quiz->review = $CFG->quiz_review;
516cf3eb 62
63 } else if (!$quiz = get_record('quiz', 'id', $quizid)) {
64 error("Quiz id $quizid does not exist");
65 }
66
67 // Load the question information
4f48fb42 68 if (!$questions = get_records('question', 'id', $id)) {
516cf3eb 69 error('Could not load question');
70 }
71 if ($maxgrade = get_field('quiz_question_instances', 'grade', 'quiz', $quiz->id, 'question', $id)) {
72 $questions[$id]->maxgrade = $maxgrade;
73 } else {
74 $questions[$id]->maxgrade = $questions[$id]->defaultgrade;
75 }
76
77 $quiz->id = 0; // just for safety
78 $quiz->questions = $id;
79
dc1f00de 80 if (!$category = get_record("question_categories", "id", $questions[$id]->category)) {
516cf3eb 81 error("This question doesn't belong to a valid category!");
82 }
83
84 if (!isteacher($category->course) and !$category->publish) {
85 error("You can't preview these questions!");
86 }
87 $quiz->course = $category->course;
88
89 // Load the question type specific information
4f48fb42 90 if (!get_question_options($questions)) {
516cf3eb 91 error(get_string('newattemptfail', 'quiz'));
92 }
93
94 // Create a dummy quiz attempt
219e92f2 95 // TODO: find out what of the following we really need. What is $attempt
96 // really used for?
97 $timenow = time();
98 $attempt->quiz = $quiz->id;
99 $attempt->userid = $USER->id;
100 $attempt->attempt = 0;
101 $attempt->sumgrades = 0;
102 $attempt->timestart = $timenow;
103 $attempt->timefinish = 0;
104 $attempt->timemodified = $timenow;
b518c69e 105 $attempt->uniqueid = 0;
516cf3eb 106 $attempt->id = 0;
107
108 // Restore the history of question sessions from the moodle session or create
109 // new sessions. Make $states a reference to the states array in the moodle
110 // session.
111 if (isset($SESSION->quizpreview->states) and $SESSION->quizpreview->questionid
112 == $id) {
113 // Reload the question session history from the moodle session
114 $states =& $SESSION->quizpreview->states;
115 $historylength = count($states) - 1;
116 if ($back && $historylength > 0) {
117 // Go back one step in the history
118 unset($states[$historylength]);
119 $historylength--;
120 }
121 } else {
122 // Record the question id in the moodle session
123 $SESSION->quizpreview->questionid = $id;
124 // Create an empty session for the question
125 if (!$newstates =
4f48fb42 126 get_question_states($questions, $quiz, $attempt)) {
516cf3eb 127 error(get_string('newattemptfail', 'quiz'));
128 }
129 $SESSION->quizpreview->states = array($newstates);
130 $states =& $SESSION->quizpreview->states;
131 $historylength = 0;
132 }
133
134 if (!$fillcorrect && !$back && ($form = data_submitted())) {
135 $form = (array)$form;
136 $submitted = true;
137
138 // Create a new item in the history of question states (don't simplify!)
139 $states[$historylength + 1] = array();
140 $states[$historylength + 1][$id] = clone($states[$historylength][$id]);
141 $historylength++;
142 $curstate =& $states[$historylength][$id];
b518c69e 143 $curstate->changed = false;
516cf3eb 144
145 // Process the responses
146 unset($form['id']);
147 unset($form['quizid']);
148 unset($form['continue']);
149 unset($form['markall']);
150 unset($form['finishattempt']);
151 unset($form['back']);
152 unset($form['startagain']);
153
b518c69e 154 $event = $finishattempt ? QUESTION_EVENTCLOSE : QUESTION_EVENTSUBMIT;
4f48fb42 155 if ($actions = question_extract_responses($questions, $form, $event)) {
516cf3eb 156 $actions[$id]->timestamp = 0; // We do not care about timelimits here
b518c69e 157 question_process_responses($questions[$id], $curstate, $actions[$id], $quiz, $attempt);
158 if (!$curstate->changed) {
516cf3eb 159 // Update the current state rather than creating a new one
160 $historylength--;
161 unset($states[$historylength]);
162 $states = array_values($states);
163 $curstate =& $states[$historylength][$id];
164 }
165 }
166 } else {
167 $submitted = false;
168 $curstate =& $states[$historylength][$id];
169 }
170
66125eda 171 // TODO: should not use quiz-specific function here
172 $options = quiz_get_renderoptions($quiz->review, $curstate);
516cf3eb 173
174 // Fill in the correct responses (unless the question is in readonly mode)
175 if ($fillcorrect && !$options->readonly) {
f02c6f01 176 $curstate->responses = $QTYPES[$questions[$id]->qtype]
516cf3eb 177 ->get_correct_responses($questions[$id], $curstate);
178 }
179
3a5a6f59 180 $strpreview = get_string('preview', 'quiz').' '.s($questions[$id]->name);
516cf3eb 181 print_header($strpreview);
182 print_heading($strpreview);
183
3a5a6f59 184 if (!empty($quizid)) {
185 echo '<p align="center">'.get_string('modulename', 'quiz') . ': ';
516cf3eb 186 p($quiz->name);
3a5a6f59 187 echo "</p>\n";
516cf3eb 188 }
516cf3eb 189 $number = 1;
190 echo "<form method=\"post\" action=\"preview.php\" autocomplete=\"off\">\n";
191 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />\n";
192 echo "<input type=\"hidden\" name=\"quizid\" value=\"$quizid\" />\n";
193 echo "<input type=\"hidden\" name=\"continue\" value=\"1\" />\n";
194
4f48fb42 195 print_question($questions[$id], $curstate, $number, $quiz, $options);
516cf3eb 196
197 echo '<br />';
198 echo '<center>';
199
200 // Print the mark and finish attempt buttons
201 echo '<input name="markall" type="submit" value="' . get_string('markall',
202 'quiz') . "\" />\n";
203 echo '<input name="finishattempt" type="submit" value="' .
204 get_string('finishattempt', 'quiz') . "\" />\n";
205 echo '<br />';
206 echo '<br />';
207 // Print the fill correct button (unless the question is in readonly mode)
208 if (!$options->readonly) {
209 echo '<input name="fillcorrect" type="submit" value="' .
210 get_string('fillcorrect', 'quiz') . "\" />\n";
211 }
212 // Print the navigation buttons
213 if ($historylength > 0) {
214 echo '<input name="back" type="submit" value="' . get_string('previous',
215 'quiz') . "\" />\n";
216 }
217 // Print the start again button
218 echo '<input name="startagain" type="submit" value="' .
219 get_string('startagain', 'quiz') . "\" />\n";
220 // Print the close window button
221 echo '<input type="button" onclick="window.close()" value="' .
222 get_string('closepreview', 'quiz') . "\" />";
223 echo '</center>';
224 echo '</form>';
225 print_footer();
226?>