$CFG->tabselectedtofront is out. Part of MDL-8746
[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
d02eeded 44 // this might break things in the future
516cf3eb 45 if (!isteacherinanycourse()) {
46 error('This page is for teachers only');
47 }
48
49 if (!$continue) {
50 // Start a new attempt; delete the old session
51 unset($SESSION->quizpreview);
52 // Redirect to ourselves but with continue=1; prevents refreshing the page
53 // from restarting an attempt (needed so that random questions don't change)
54 $quizid = $quizid ? '&amp;quizid=' . $quizid : '';
e586cfb4 55 redirect($CFG->wwwroot . '/question/preview.php?id=' . $id . $quizid .
516cf3eb 56 '&amp;continue=1');
57 }
58
59 if (empty($quizid)) {
219e92f2 60 $quiz = new cmoptions;
61 $quiz->id = 0;
6af1e0b2 62 $quiz->review = $CFG->quiz_review;
516cf3eb 63
64 } else if (!$quiz = get_record('quiz', 'id', $quizid)) {
65 error("Quiz id $quizid does not exist");
66 }
67
68 // Load the question information
4f48fb42 69 if (!$questions = get_records('question', 'id', $id)) {
516cf3eb 70 error('Could not load question');
71 }
72 if ($maxgrade = get_field('quiz_question_instances', 'grade', 'quiz', $quiz->id, 'question', $id)) {
73 $questions[$id]->maxgrade = $maxgrade;
74 } else {
75 $questions[$id]->maxgrade = $questions[$id]->defaultgrade;
76 }
77
78 $quiz->id = 0; // just for safety
79 $quiz->questions = $id;
80
dc1f00de 81 if (!$category = get_record("question_categories", "id", $questions[$id]->category)) {
516cf3eb 82 error("This question doesn't belong to a valid category!");
83 }
84
d02eeded 85 if (!has_capability('moodle/question:manage', get_context_instance(CONTEXT_COURSE, $category->course)) and !$category->publish) {
516cf3eb 86 error("You can't preview these questions!");
87 }
88 $quiz->course = $category->course;
89
90 // Load the question type specific information
4f48fb42 91 if (!get_question_options($questions)) {
516cf3eb 92 error(get_string('newattemptfail', 'quiz'));
93 }
94
95 // Create a dummy quiz attempt
219e92f2 96 // TODO: find out what of the following we really need. What is $attempt
97 // really used for?
98 $timenow = time();
99 $attempt->quiz = $quiz->id;
100 $attempt->userid = $USER->id;
101 $attempt->attempt = 0;
102 $attempt->sumgrades = 0;
103 $attempt->timestart = $timenow;
104 $attempt->timefinish = 0;
105 $attempt->timemodified = $timenow;
b518c69e 106 $attempt->uniqueid = 0;
516cf3eb 107 $attempt->id = 0;
108
109 // Restore the history of question sessions from the moodle session or create
110 // new sessions. Make $states a reference to the states array in the moodle
111 // session.
112 if (isset($SESSION->quizpreview->states) and $SESSION->quizpreview->questionid
113 == $id) {
114 // Reload the question session history from the moodle session
115 $states =& $SESSION->quizpreview->states;
116 $historylength = count($states) - 1;
117 if ($back && $historylength > 0) {
118 // Go back one step in the history
119 unset($states[$historylength]);
120 $historylength--;
121 }
122 } else {
123 // Record the question id in the moodle session
124 $SESSION->quizpreview->questionid = $id;
125 // Create an empty session for the question
126 if (!$newstates =
4f48fb42 127 get_question_states($questions, $quiz, $attempt)) {
516cf3eb 128 error(get_string('newattemptfail', 'quiz'));
129 }
130 $SESSION->quizpreview->states = array($newstates);
131 $states =& $SESSION->quizpreview->states;
132 $historylength = 0;
133 }
134
135 if (!$fillcorrect && !$back && ($form = data_submitted())) {
136 $form = (array)$form;
137 $submitted = true;
138
139 // Create a new item in the history of question states (don't simplify!)
140 $states[$historylength + 1] = array();
141 $states[$historylength + 1][$id] = clone($states[$historylength][$id]);
142 $historylength++;
143 $curstate =& $states[$historylength][$id];
b518c69e 144 $curstate->changed = false;
516cf3eb 145
146 // Process the responses
147 unset($form['id']);
148 unset($form['quizid']);
149 unset($form['continue']);
150 unset($form['markall']);
151 unset($form['finishattempt']);
152 unset($form['back']);
153 unset($form['startagain']);
154
b518c69e 155 $event = $finishattempt ? QUESTION_EVENTCLOSE : QUESTION_EVENTSUBMIT;
4f48fb42 156 if ($actions = question_extract_responses($questions, $form, $event)) {
516cf3eb 157 $actions[$id]->timestamp = 0; // We do not care about timelimits here
b518c69e 158 question_process_responses($questions[$id], $curstate, $actions[$id], $quiz, $attempt);
159 if (!$curstate->changed) {
516cf3eb 160 // Update the current state rather than creating a new one
161 $historylength--;
162 unset($states[$historylength]);
163 $states = array_values($states);
164 $curstate =& $states[$historylength][$id];
165 }
166 }
167 } else {
168 $submitted = false;
169 $curstate =& $states[$historylength][$id];
170 }
171
66125eda 172 // TODO: should not use quiz-specific function here
173 $options = quiz_get_renderoptions($quiz->review, $curstate);
516cf3eb 174
175 // Fill in the correct responses (unless the question is in readonly mode)
176 if ($fillcorrect && !$options->readonly) {
f02c6f01 177 $curstate->responses = $QTYPES[$questions[$id]->qtype]
516cf3eb 178 ->get_correct_responses($questions[$id], $curstate);
179 }
180
70ed7667 181 $strpreview = get_string('preview', 'quiz').' '.format_string($questions[$id]->name);
516cf3eb 182 print_header($strpreview);
183 print_heading($strpreview);
184
3a5a6f59 185 if (!empty($quizid)) {
2139364f 186 echo '<p class="quemodname">'.get_string('modulename', 'quiz') . ': ';
516cf3eb 187 p($quiz->name);
3a5a6f59 188 echo "</p>\n";
516cf3eb 189 }
516cf3eb 190 $number = 1;
2e7bdb51 191 echo "<form method=\"post\" action=\"preview.php\">\n";
4f48fb42 192 print_question($questions[$id], $curstate, $number, $quiz, $options);
516cf3eb 193 echo '<br />';
2697726a 194
195
2e7bdb51 196
2139364f 197 echo '<div class="controls">';
2697726a 198 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />\n";
199 echo "<input type=\"hidden\" name=\"quizid\" value=\"$quizid\" />\n";
200 echo "<input type=\"hidden\" name=\"continue\" value=\"1\" />\n";
516cf3eb 201
202 // Print the mark and finish attempt buttons
203 echo '<input name="markall" type="submit" value="' . get_string('markall',
204 'quiz') . "\" />\n";
205 echo '<input name="finishattempt" type="submit" value="' .
206 get_string('finishattempt', 'quiz') . "\" />\n";
207 echo '<br />';
208 echo '<br />';
209 // Print the fill correct button (unless the question is in readonly mode)
210 if (!$options->readonly) {
211 echo '<input name="fillcorrect" type="submit" value="' .
212 get_string('fillcorrect', 'quiz') . "\" />\n";
213 }
214 // Print the navigation buttons
215 if ($historylength > 0) {
216 echo '<input name="back" type="submit" value="' . get_string('previous',
217 'quiz') . "\" />\n";
218 }
219 // Print the start again button
220 echo '<input name="startagain" type="submit" value="' .
221 get_string('startagain', 'quiz') . "\" />\n";
222 // Print the close window button
223 echo '<input type="button" onclick="window.close()" value="' .
224 get_string('closepreview', 'quiz') . "\" />";
2139364f 225 echo '</div>';
516cf3eb 226 echo '</form>';
227 print_footer();
2697726a 228?>