Fixed logic
[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
26 if (empty($quizid)) {
27 $courseid = required_param('courseid', PARAM_INT);
28 }
29
516cf3eb 30 // Test if we are continuing an attempt at a question
31 $continue = optional_param('continue', 0, PARAM_BOOL);
32 // Check for any of the submit buttons
33 $fillcorrect = optional_param('fillcorrect', 0, PARAM_BOOL);
34 $markall = optional_param('markall', 0, PARAM_BOOL);
35 $finishattempt = optional_param('finishattempt', 0, PARAM_BOOL);
36 $back = optional_param('back', 0, PARAM_BOOL);
37 $startagain = optional_param('startagain', 0, PARAM_BOOL);
38 // We are always continuing an attempt if a submit button was pressed with the
39 // exception of the start again button
40 if ($fillcorrect || $markall || $finishattempt || $back) {
41 $continue = true;
42 } else if ($startagain) {
43 $continue = false;
44 }
45
516cf3eb 46
516cf3eb 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)
7d87171b 53 $url = $CFG->wwwroot . '/question/preview.php?id=' . $id;
54 if ($quizid) {
55 $url .= '&amp;quizid=' . $quizid;
e7e62d45 56 } else {
57 $url .= '&amp;courseid=' . $courseid;
7d87171b 58 }
59 $url .= '&amp;continue=1';
60 redirect($url);
516cf3eb 61 }
271e6dec 62 // Load the question information
63 if (!$questions = get_records('question', 'id', $id)) {
64 error('Could not load question');
65 }
516cf3eb 66 if (empty($quizid)) {
219e92f2 67 $quiz = new cmoptions;
68 $quiz->id = 0;
6af1e0b2 69 $quiz->review = $CFG->quiz_review;
e7e62d45 70 require_login($courseid, false);
71 $quiz->course = $courseid;
516cf3eb 72 } else if (!$quiz = get_record('quiz', 'id', $quizid)) {
73 error("Quiz id $quizid does not exist");
271e6dec 74 } else {
75 require_login($quiz->course, false, get_coursemodule_from_instance('quiz', $quizid, $quiz->course));
516cf3eb 76 }
77
271e6dec 78
79
516cf3eb 80 if ($maxgrade = get_field('quiz_question_instances', 'grade', 'quiz', $quiz->id, 'question', $id)) {
81 $questions[$id]->maxgrade = $maxgrade;
82 } else {
83 $questions[$id]->maxgrade = $questions[$id]->defaultgrade;
84 }
85
86 $quiz->id = 0; // just for safety
87 $quiz->questions = $id;
88
dc1f00de 89 if (!$category = get_record("question_categories", "id", $questions[$id]->category)) {
516cf3eb 90 error("This question doesn't belong to a valid category!");
91 }
92
271e6dec 93 if (!question_has_capability_on($questions[$id], 'use', $questions[$id]->category)){
516cf3eb 94 error("You can't preview these questions!");
95 }
271e6dec 96 if (isset($COURSE)){
97 $quiz->course = $COURSE->id;
98 }
516cf3eb 99
100 // Load the question type specific information
4f48fb42 101 if (!get_question_options($questions)) {
516cf3eb 102 error(get_string('newattemptfail', 'quiz'));
103 }
104
105 // Create a dummy quiz attempt
219e92f2 106 // TODO: find out what of the following we really need. What is $attempt
107 // really used for?
108 $timenow = time();
109 $attempt->quiz = $quiz->id;
110 $attempt->userid = $USER->id;
111 $attempt->attempt = 0;
112 $attempt->sumgrades = 0;
113 $attempt->timestart = $timenow;
114 $attempt->timefinish = 0;
115 $attempt->timemodified = $timenow;
b518c69e 116 $attempt->uniqueid = 0;
516cf3eb 117 $attempt->id = 0;
118
119 // Restore the history of question sessions from the moodle session or create
120 // new sessions. Make $states a reference to the states array in the moodle
121 // session.
122 if (isset($SESSION->quizpreview->states) and $SESSION->quizpreview->questionid
123 == $id) {
124 // Reload the question session history from the moodle session
125 $states =& $SESSION->quizpreview->states;
126 $historylength = count($states) - 1;
127 if ($back && $historylength > 0) {
128 // Go back one step in the history
129 unset($states[$historylength]);
130 $historylength--;
131 }
132 } else {
133 // Record the question id in the moodle session
134 $SESSION->quizpreview->questionid = $id;
135 // Create an empty session for the question
136 if (!$newstates =
4f48fb42 137 get_question_states($questions, $quiz, $attempt)) {
516cf3eb 138 error(get_string('newattemptfail', 'quiz'));
139 }
140 $SESSION->quizpreview->states = array($newstates);
141 $states =& $SESSION->quizpreview->states;
142 $historylength = 0;
143 }
144
145 if (!$fillcorrect && !$back && ($form = data_submitted())) {
146 $form = (array)$form;
147 $submitted = true;
148
149 // Create a new item in the history of question states (don't simplify!)
150 $states[$historylength + 1] = array();
151 $states[$historylength + 1][$id] = clone($states[$historylength][$id]);
152 $historylength++;
153 $curstate =& $states[$historylength][$id];
b518c69e 154 $curstate->changed = false;
516cf3eb 155
156 // Process the responses
157 unset($form['id']);
158 unset($form['quizid']);
159 unset($form['continue']);
160 unset($form['markall']);
161 unset($form['finishattempt']);
162 unset($form['back']);
163 unset($form['startagain']);
164
b518c69e 165 $event = $finishattempt ? QUESTION_EVENTCLOSE : QUESTION_EVENTSUBMIT;
4f48fb42 166 if ($actions = question_extract_responses($questions, $form, $event)) {
516cf3eb 167 $actions[$id]->timestamp = 0; // We do not care about timelimits here
b518c69e 168 question_process_responses($questions[$id], $curstate, $actions[$id], $quiz, $attempt);
169 if (!$curstate->changed) {
516cf3eb 170 // Update the current state rather than creating a new one
171 $historylength--;
172 unset($states[$historylength]);
173 $states = array_values($states);
174 $curstate =& $states[$historylength][$id];
175 }
176 }
177 } else {
178 $submitted = false;
179 $curstate =& $states[$historylength][$id];
180 }
181
66125eda 182 // TODO: should not use quiz-specific function here
183 $options = quiz_get_renderoptions($quiz->review, $curstate);
516cf3eb 184
185 // Fill in the correct responses (unless the question is in readonly mode)
186 if ($fillcorrect && !$options->readonly) {
f02c6f01 187 $curstate->responses = $QTYPES[$questions[$id]->qtype]
516cf3eb 188 ->get_correct_responses($questions[$id], $curstate);
189 }
190
70ed7667 191 $strpreview = get_string('preview', 'quiz').' '.format_string($questions[$id]->name);
7d87171b 192 $questionlist = array($id);
193 $headtags = get_html_head_contributions($questionlist, $questions, $states[$historylength]);
194 print_header($strpreview, '', '', '', $headtags);
516cf3eb 195 print_heading($strpreview);
196
3a5a6f59 197 if (!empty($quizid)) {
2139364f 198 echo '<p class="quemodname">'.get_string('modulename', 'quiz') . ': ';
29e959a5 199 p(format_string($quiz->name));
3a5a6f59 200 echo "</p>\n";
516cf3eb 201 }
516cf3eb 202 $number = 1;
46a29ccf 203 echo '<form method="post" action="preview.php" enctype="multipart/form-data" id="responseform">', "\n";
4f48fb42 204 print_question($questions[$id], $curstate, $number, $quiz, $options);
516cf3eb 205 echo '<br />';
2697726a 206
207
271e6dec 208
2139364f 209 echo '<div class="controls">';
2697726a 210 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />\n";
211 echo "<input type=\"hidden\" name=\"quizid\" value=\"$quizid\" />\n";
e7e62d45 212 echo "<input type=\"hidden\" name=\"courseid\" value=\"$courseid\" />\n";
2697726a 213 echo "<input type=\"hidden\" name=\"continue\" value=\"1\" />\n";
516cf3eb 214
215 // Print the mark and finish attempt buttons
216 echo '<input name="markall" type="submit" value="' . get_string('markall',
217 'quiz') . "\" />\n";
218 echo '<input name="finishattempt" type="submit" value="' .
219 get_string('finishattempt', 'quiz') . "\" />\n";
220 echo '<br />';
221 echo '<br />';
222 // Print the fill correct button (unless the question is in readonly mode)
223 if (!$options->readonly) {
224 echo '<input name="fillcorrect" type="submit" value="' .
225 get_string('fillcorrect', 'quiz') . "\" />\n";
226 }
227 // Print the navigation buttons
228 if ($historylength > 0) {
229 echo '<input name="back" type="submit" value="' . get_string('previous',
230 'quiz') . "\" />\n";
231 }
232 // Print the start again button
233 echo '<input name="startagain" type="submit" value="' .
234 get_string('startagain', 'quiz') . "\" />\n";
235 // Print the close window button
236 echo '<input type="button" onclick="window.close()" value="' .
237 get_string('closepreview', 'quiz') . "\" />";
2139364f 238 echo '</div>';
516cf3eb 239 echo '</form>';
240 print_footer();
29e959a5 241?>