printing user picture for comments
[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;
516cf3eb 61
62 } else if (!$quiz = get_record('quiz', 'id', $quizid)) {
63 error("Quiz id $quizid does not exist");
64 }
65
66 // Load the question information
4f48fb42 67 if (!$questions = get_records('question', 'id', $id)) {
516cf3eb 68 error('Could not load question');
69 }
70 if ($maxgrade = get_field('quiz_question_instances', 'grade', 'quiz', $quiz->id, 'question', $id)) {
71 $questions[$id]->maxgrade = $maxgrade;
72 } else {
73 $questions[$id]->maxgrade = $questions[$id]->defaultgrade;
74 }
75
76 $quiz->id = 0; // just for safety
77 $quiz->questions = $id;
78
79 if (!$category = get_record("quiz_categories", "id", $questions[$id]->category)) {
80 error("This question doesn't belong to a valid category!");
81 }
82
83 if (!isteacher($category->course) and !$category->publish) {
84 error("You can't preview these questions!");
85 }
86 $quiz->course = $category->course;
87
88 // Load the question type specific information
4f48fb42 89 if (!get_question_options($questions)) {
516cf3eb 90 error(get_string('newattemptfail', 'quiz'));
91 }
92
93 // Create a dummy quiz attempt
219e92f2 94 // TODO: find out what of the following we really need. What is $attempt
95 // really used for?
96 $timenow = time();
97 $attempt->quiz = $quiz->id;
98 $attempt->userid = $USER->id;
99 $attempt->attempt = 0;
100 $attempt->sumgrades = 0;
101 $attempt->timestart = $timenow;
102 $attempt->timefinish = 0;
103 $attempt->timemodified = $timenow;
4f48fb42 104 $attempt->uniqueid = question_new_attempt_uniqueid();
516cf3eb 105 $attempt->id = 0;
106
107 // Restore the history of question sessions from the moodle session or create
108 // new sessions. Make $states a reference to the states array in the moodle
109 // session.
110 if (isset($SESSION->quizpreview->states) and $SESSION->quizpreview->questionid
111 == $id) {
112 // Reload the question session history from the moodle session
113 $states =& $SESSION->quizpreview->states;
114 $historylength = count($states) - 1;
115 if ($back && $historylength > 0) {
116 // Go back one step in the history
117 unset($states[$historylength]);
118 $historylength--;
119 }
120 } else {
121 // Record the question id in the moodle session
122 $SESSION->quizpreview->questionid = $id;
123 // Create an empty session for the question
124 if (!$newstates =
4f48fb42 125 get_question_states($questions, $quiz, $attempt)) {
516cf3eb 126 error(get_string('newattemptfail', 'quiz'));
127 }
128 $SESSION->quizpreview->states = array($newstates);
129 $states =& $SESSION->quizpreview->states;
130 $historylength = 0;
131 }
132
133 if (!$fillcorrect && !$back && ($form = data_submitted())) {
134 $form = (array)$form;
135 $submitted = true;
136
137 // Create a new item in the history of question states (don't simplify!)
138 $states[$historylength + 1] = array();
139 $states[$historylength + 1][$id] = clone($states[$historylength][$id]);
140 $historylength++;
141 $curstate =& $states[$historylength][$id];
142
143 // Process the responses
144 unset($form['id']);
145 unset($form['quizid']);
146 unset($form['continue']);
147 unset($form['markall']);
148 unset($form['finishattempt']);
149 unset($form['back']);
150 unset($form['startagain']);
151
4f48fb42 152 $event = $finishattempt ? QUESTION_EVENTCLOSE : ($markall ? QUESTION_EVENTGRADE : QUESTION_EVENTSAVE);
153 if ($actions = question_extract_responses($questions, $form, $event)) {
516cf3eb 154 $actions[$id]->timestamp = 0; // We do not care about timelimits here
4f48fb42 155 question_process_responses($questions[$id], $states[$historylength][$id], $actions[$id], $quiz, $attempt);
156 if (QUESTION_EVENTGRADE != $curstate->event && QUESTION_EVENTCLOSE != $curstate->event) {
516cf3eb 157 // Update the current state rather than creating a new one
158 $historylength--;
159 unset($states[$historylength]);
160 $states = array_values($states);
161 $curstate =& $states[$historylength][$id];
162 }
163 }
164 } else {
165 $submitted = false;
166 $curstate =& $states[$historylength][$id];
167 }
168
169 $options = quiz_get_renderoptions($quiz, $curstate);
170
171 // Fill in the correct responses (unless the question is in readonly mode)
172 if ($fillcorrect && !$options->readonly) {
f02c6f01 173 $curstate->responses = $QTYPES[$questions[$id]->qtype]
516cf3eb 174 ->get_correct_responses($questions[$id], $curstate);
175 }
176
177 $strpreview = get_string('previewquestion', 'quiz');
178 print_header($strpreview);
179 print_heading($strpreview);
180
181 echo '<p align="center">' . get_string('modulename', 'quiz') . ': ';
182 if (empty($quizid)) {
183 echo '[' . get_string('default', 'quiz') . ']';
184 } else {
185 p($quiz->name);
186 }
187 echo '<br />'.get_string('question', 'quiz').': ';
188 p($questions[$id]->name);
189 echo "</p>\n";
190 $number = 1;
191 echo "<form method=\"post\" action=\"preview.php\" autocomplete=\"off\">\n";
192 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />\n";
193 echo "<input type=\"hidden\" name=\"quizid\" value=\"$quizid\" />\n";
194 echo "<input type=\"hidden\" name=\"continue\" value=\"1\" />\n";
195
4f48fb42 196 print_question($questions[$id], $curstate, $number, $quiz, $options);
516cf3eb 197
198 echo '<br />';
199 echo '<center>';
200
201 // Print the mark and finish attempt buttons
202 echo '<input name="markall" type="submit" value="' . get_string('markall',
203 'quiz') . "\" />\n";
204 echo '<input name="finishattempt" type="submit" value="' .
205 get_string('finishattempt', 'quiz') . "\" />\n";
206 echo '<br />';
207 echo '<br />';
208 // Print the fill correct button (unless the question is in readonly mode)
209 if (!$options->readonly) {
210 echo '<input name="fillcorrect" type="submit" value="' .
211 get_string('fillcorrect', 'quiz') . "\" />\n";
212 }
213 // Print the navigation buttons
214 if ($historylength > 0) {
215 echo '<input name="back" type="submit" value="' . get_string('previous',
216 'quiz') . "\" />\n";
217 }
218 // Print the start again button
219 echo '<input name="startagain" type="submit" value="' .
220 get_string('startagain', 'quiz') . "\" />\n";
221 // Print the close window button
222 echo '<input type="button" onclick="window.close()" value="' .
223 get_string('closepreview', 'quiz') . "\" />";
224 echo '</center>';
225 echo '</form>';
226 print_footer();
227?>