MDL-20601 fixed fatal SQL upgrade error, missing {} around table names
[moodle.git] / question / preview.php
CommitLineData
516cf3eb 1<?php // $Id$
2/**
4323d029 3 * This page displays a preview of a question
4 *
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
7 * information is stored in the session as an array of subsequent states rather
8 * than in the database.
9 *
10 * TODO: make this work with activities other than quiz
11 *
12 * @author Alex Smith as part of the Serving Mathematics project
13 * {@link http://maths.york.ac.uk/serving_maths}
14 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
15 * @package questionbank
16 */
516cf3eb 17
e586cfb4 18 require_once("../config.php");
19 require_once($CFG->libdir.'/questionlib.php');
20 require_once($CFG->dirroot.'/mod/quiz/locallib.php'); // We really want to get rid of this
516cf3eb 21
22 $id = required_param('id', PARAM_INT); // question id
23 // if no quiz id is specified then a dummy quiz with default options is used
24 $quizid = optional_param('quizid', 0, PARAM_INT);
e7e62d45 25 // if no quiz id is specified then tell us the course
d0fdb92c 26
27 $pageurl = new moodle_url($CFG->wwwroot . '/question/preview.php', array('id' => $id, 'continue' => 1));
e7e62d45 28 if (empty($quizid)) {
29 $courseid = required_param('courseid', PARAM_INT);
d0fdb92c 30 $pageurl->param('courseid', $courseid);
31 } else {
32 $pageurl->param('quizid', $quizid);
e7e62d45 33 }
d0fdb92c 34 $PAGE->set_url($pageurl);
35 $PAGE->set_generaltype('popup');
e7e62d45 36
516cf3eb 37 // Test if we are continuing an attempt at a question
38 $continue = optional_param('continue', 0, PARAM_BOOL);
39 // Check for any of the submit buttons
40 $fillcorrect = optional_param('fillcorrect', 0, PARAM_BOOL);
41 $markall = optional_param('markall', 0, PARAM_BOOL);
42 $finishattempt = optional_param('finishattempt', 0, PARAM_BOOL);
43 $back = optional_param('back', 0, PARAM_BOOL);
44 $startagain = optional_param('startagain', 0, PARAM_BOOL);
45 // We are always continuing an attempt if a submit button was pressed with the
46 // exception of the start again button
47 if ($fillcorrect || $markall || $finishattempt || $back) {
48 $continue = true;
49 } else if ($startagain) {
50 $continue = false;
51 }
52
3a4a9233 53 $url = new moodle_url($CFG->wwwroot . '/question/preview.php');
54 $url->param('id', $id);
55 if ($quizid) {
56 $url->param('quizid', $quizid);
57 } else {
58 $url->param('courseid', $courseid);
59 }
60 $url->param('continue', 1);
516cf3eb 61 if (!$continue) {
62 // Start a new attempt; delete the old session
63 unset($SESSION->quizpreview);
64 // Redirect to ourselves but with continue=1; prevents refreshing the page
65 // from restarting an attempt (needed so that random questions don't change)
e42f153c 66 redirect($url);
516cf3eb 67 }
271e6dec 68 // Load the question information
f34488b2 69 if (!$questions = $DB->get_records('question', array('id' => $id))) {
ac93d63d 70 print_error('cannotloadquestion', 'question');
271e6dec 71 }
516cf3eb 72 if (empty($quizid)) {
219e92f2 73 $quiz = new cmoptions;
74 $quiz->id = 0;
0cd46577 75 $quiz->review = get_config('quiz', 'review');
e7e62d45 76 require_login($courseid, false);
77 $quiz->course = $courseid;
71990c7c 78 $quiz->decimalpoints = get_config('quiz', 'decimalpoints');
62e76c67 79 $context = get_context_instance(CONTEXT_COURSE, $courseid);
f34488b2 80 } else if (!$quiz = $DB->get_record('quiz', array('id' => $quizid))) {
ac93d63d 81 print_error('invalidquizid', 'quiz', '', $quizid);
271e6dec 82 } else {
62e76c67 83 $cm = get_coursemodule_from_instance('quiz', $quizid, $quiz->course);
84 require_login($quiz->course, false, $cm);
85 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
516cf3eb 86 }
87
f34488b2 88 if ($maxgrade = $DB->get_field('quiz_question_instances', 'grade', array('quiz' => $quiz->id, 'question' => $id))) {
516cf3eb 89 $questions[$id]->maxgrade = $maxgrade;
90 } else {
91 $questions[$id]->maxgrade = $questions[$id]->defaultgrade;
92 }
93
94 $quiz->id = 0; // just for safety
95 $quiz->questions = $id;
96
f34488b2 97 if (!$category = $DB->get_record("question_categories", array("id" => $questions[$id]->category))) {
ac93d63d 98 print_error('invalidquestionid', 'quiz');
516cf3eb 99 }
100
271e6dec 101 if (!question_has_capability_on($questions[$id], 'use', $questions[$id]->category)){
ac93d63d 102 print_error('cannotpreview', 'question');
516cf3eb 103 }
271e6dec 104 if (isset($COURSE)){
105 $quiz->course = $COURSE->id;
106 }
516cf3eb 107
108 // Load the question type specific information
4f48fb42 109 if (!get_question_options($questions)) {
5a2a5331 110 print_error('newattemptfail', 'quiz');
516cf3eb 111 }
112
113 // Create a dummy quiz attempt
219e92f2 114 // TODO: find out what of the following we really need. What is $attempt
115 // really used for?
116 $timenow = time();
117 $attempt->quiz = $quiz->id;
118 $attempt->userid = $USER->id;
119 $attempt->attempt = 0;
120 $attempt->sumgrades = 0;
121 $attempt->timestart = $timenow;
122 $attempt->timefinish = 0;
123 $attempt->timemodified = $timenow;
b518c69e 124 $attempt->uniqueid = 0;
516cf3eb 125 $attempt->id = 0;
71990c7c 126 $attempt->layout = $id;
516cf3eb 127
128 // Restore the history of question sessions from the moodle session or create
129 // new sessions. Make $states a reference to the states array in the moodle
130 // session.
3a4a9233 131 if (isset($SESSION->quizpreview->states) and $SESSION->quizpreview->questionid == $id) {
516cf3eb 132 // Reload the question session history from the moodle session
133 $states =& $SESSION->quizpreview->states;
134 $historylength = count($states) - 1;
135 if ($back && $historylength > 0) {
136 // Go back one step in the history
137 unset($states[$historylength]);
138 $historylength--;
139 }
140 } else {
141 // Record the question id in the moodle session
142 $SESSION->quizpreview->questionid = $id;
143 // Create an empty session for the question
81d833ad 144 if (!$newstates = get_question_states($questions, $quiz, $attempt)) {
5a2a5331 145 print_error('newattemptfail', 'quiz');
516cf3eb 146 }
e056398a 147 $newstates[$id]->questionsessionid = 0;
516cf3eb 148 $SESSION->quizpreview->states = array($newstates);
149 $states =& $SESSION->quizpreview->states;
150 $historylength = 0;
151 }
152
153 if (!$fillcorrect && !$back && ($form = data_submitted())) {
154 $form = (array)$form;
155 $submitted = true;
156
157 // Create a new item in the history of question states (don't simplify!)
158 $states[$historylength + 1] = array();
159 $states[$historylength + 1][$id] = clone($states[$historylength][$id]);
160 $historylength++;
161 $curstate =& $states[$historylength][$id];
b518c69e 162 $curstate->changed = false;
516cf3eb 163
164 // Process the responses
165 unset($form['id']);
166 unset($form['quizid']);
167 unset($form['continue']);
168 unset($form['markall']);
169 unset($form['finishattempt']);
170 unset($form['back']);
171 unset($form['startagain']);
172
84857f8c 173 if ($finishattempt) {
174 $event = QUESTION_EVENTCLOSE;
175 } else if ($markall) {
176 $event = QUESTION_EVENTSUBMIT;
177 } else {
178 $event = QUESTION_EVENTSAVE;
179 }
4f48fb42 180 if ($actions = question_extract_responses($questions, $form, $event)) {
516cf3eb 181 $actions[$id]->timestamp = 0; // We do not care about timelimits here
373f0afd 182 if (!question_process_responses($questions[$id], $curstate, $actions[$id], $quiz, $attempt)) {
183 unset($SESSION->quizpreview);
184 print_error('errorprocessingresponses', 'question', $url->out());
185 }
b518c69e 186 if (!$curstate->changed) {
516cf3eb 187 // Update the current state rather than creating a new one
188 $historylength--;
189 unset($states[$historylength]);
190 $states = array_values($states);
191 $curstate =& $states[$historylength][$id];
192 }
193 }
194 } else {
195 $submitted = false;
196 $curstate =& $states[$historylength][$id];
197 }
198
66125eda 199 // TODO: should not use quiz-specific function here
62e76c67 200 $options = quiz_get_renderoptions($quiz, $attempt, $context, $curstate);
aafdb447 201 $options->noeditlink = true;
516cf3eb 202
203 // Fill in the correct responses (unless the question is in readonly mode)
204 if ($fillcorrect && !$options->readonly) {
f34488b2 205 $curstate->responses = $QTYPES[$questions[$id]->qtype]->get_correct_responses($questions[$id], $curstate);
516cf3eb 206 }
207
70ed7667 208 $strpreview = get_string('preview', 'quiz').' '.format_string($questions[$id]->name);
7d87171b 209 $questionlist = array($id);
d412354c 210 get_html_head_contributions($questionlist, $questions, $states[$historylength]);
211 $PAGE->set_title($strpreview);
212 echo $OUTPUT->header();
723d610c 213 echo $OUTPUT->heading($strpreview);
516cf3eb 214
3a5a6f59 215 if (!empty($quizid)) {
2139364f 216 echo '<p class="quemodname">'.get_string('modulename', 'quiz') . ': ';
29e959a5 217 p(format_string($quiz->name));
3a5a6f59 218 echo "</p>\n";
516cf3eb 219 }
516cf3eb 220 $number = 1;
3a4a9233 221 echo '<form method="post" action="'.$url->out(true).'" enctype="multipart/form-data" id="responseform">', "\n";
4f48fb42 222 print_question($questions[$id], $curstate, $number, $quiz, $options);
271e6dec 223
2139364f 224 echo '<div class="controls">';
3a4a9233 225 echo $url->hidden_params_out();
516cf3eb 226
227 // Print the mark and finish attempt buttons
228 echo '<input name="markall" type="submit" value="' . get_string('markall',
229 'quiz') . "\" />\n";
230 echo '<input name="finishattempt" type="submit" value="' .
231 get_string('finishattempt', 'quiz') . "\" />\n";
232 echo '<br />';
233 echo '<br />';
234 // Print the fill correct button (unless the question is in readonly mode)
235 if (!$options->readonly) {
236 echo '<input name="fillcorrect" type="submit" value="' .
237 get_string('fillcorrect', 'quiz') . "\" />\n";
238 }
239 // Print the navigation buttons
240 if ($historylength > 0) {
241 echo '<input name="back" type="submit" value="' . get_string('previous',
242 'quiz') . "\" />\n";
243 }
244 // Print the start again button
245 echo '<input name="startagain" type="submit" value="' .
246 get_string('startagain', 'quiz') . "\" />\n";
247 // Print the close window button
248 echo '<input type="button" onclick="window.close()" value="' .
249 get_string('closepreview', 'quiz') . "\" />";
2139364f 250 echo '</div>';
516cf3eb 251 echo '</form>';
9b59580b 252 echo $OUTPUT->footer();
29e959a5 253?>