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