Fixes for MDL-7861.
[moodle.git] / question / question.php
CommitLineData
16750bcd 1<?php
2/**
3 * Page for editing questions
4 *
5 * This page shows the question editing form or processes the following actions:
6 * - create new question (category, qtype)
7 * - edit question (id, contextquiz (optional))
8 * - cancel (cancel)
9 *
10 * TODO: currently this still treats the quiz as special
11 * TODO: question versioning is not currently enabled
12 *
13 * @version $Id$
14 * @author Martin Dougiamas and many others. This has recently been extensively
15 * rewritten by members of the Serving Mathematics project
16 * {@link http://maths.york.ac.uk/serving_maths}
17 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
18 * @package question
19 *//** */
87391656 20
16750bcd 21 require_once(dirname(__FILE__) . '/../config.php');
22 require_once('editlib.php'); // NOTE - is this correct? This is just about editing screens?
23 require_once($CFG->libdir . '/filelib.php');
516cf3eb 24
16750bcd 25 $id = optional_param('id', 0, PARAM_INT); // question id
516cf3eb 26
18cbc968 27 $qtype = optional_param('qtype', '', PARAM_FILE);
28 $category = optional_param('category', 0, PARAM_INT);
ee6c9355 29 $inpopup = optional_param('inpopup', 0, PARAM_BOOL);
87391656 30
180f4106 31 $CFG->pagepath = 'question/type/'.$qtype;
87391656 32
516cf3eb 33
18cbc968 34 // rqp questions set the type to rqp_nn where nn is the rqp_type id
35 if (substr($qtype, 0, 4) == 'rqp_') {
36 $typeid = (int) substr($qtype, 4);
37 $qtype = 'rqp';
516cf3eb 38 }
39
516cf3eb 40 if ($id) {
4f48fb42 41 if (! $question = get_record("question", "id", $id)) {
516cf3eb 42 error("This question doesn't exist");
43 }
44 if (!empty($category)) {
45 $question->category = $category;
46 }
dc1f00de 47 if (! $category = get_record("question_categories", "id", $question->category)) {
516cf3eb 48 error("This question doesn't belong to a valid category!");
49 }
50 if (! $course = get_record("course", "id", $category->course)) {
51 error("This question category doesn't belong to a valid course!");
52 }
53
54 $qtype = $question->qtype;
4eda4eec 55 if (!isset($QTYPES[$qtype])) {
56 $qtype = 'missingtype';
57 }
516cf3eb 58
516cf3eb 59 } else if ($category) { // only for creating new questions
dc1f00de 60 if (! $category = get_record("question_categories", "id", $category)) {
516cf3eb 61 error("This wasn't a valid category!");
62 }
63 if (! $course = get_record("course", "id", $category->course)) {
64 error("This category doesn't belong to a valid course!");
65 }
66
67 $question->category = $category->id;
68 $question->qtype = $qtype;
69
70 } else {
71 error("Must specify question id or category");
72 }
73
2662cf46 74 if (!isset($SESSION->returnurl)) {
75 $SESSION->returnurl = 'edit.php?courseid='.$course->id;
76 }
77
05054049 78 // TODO: generalise this so it works for any activity
2662cf46 79 $contextquiz = isset($SESSION->modform->instance) ? $SESSION->modform->instance : 0;
05054049 80
81 if (isset($_REQUEST['cancel'])) {
2662cf46 82 redirect($SESSION->returnurl);
05054049 83 }
84
516cf3eb 85 if (empty($qtype)) {
86 error("No question type was specified!");
f02c6f01 87 } else if (!isset($QTYPES[$qtype])) {
516cf3eb 88 error("Could not find question type: '$qtype'");
89 }
90
36703ed7 91 if (!file_exists("type/$qtype/editquestion.php")) {
92 redirect(str_ireplace('question.php', 'question2.php', me()));
93 }
94
516cf3eb 95 require_login($course->id, false);
1680ebe4 96 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
97 require_capability('moodle/question:manage', $coursecontext);
516cf3eb 98
e586cfb4 99 // TODO: remove restriction to quiz
516cf3eb 100 $streditingquestion = get_string('editingquestion', 'quiz');
101 if (isset($SESSION->modform->instance)) {
2662cf46 102 $strediting = '<a href="'.$SESSION->returnurl.'">'.get_string('editingquiz', 'quiz').'</a> -> '.
516cf3eb 103 $streditingquestion;
104 } else {
105 $strediting = '<a href="edit.php?courseid='.$course->id.'">'.
106 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
107 }
108
1680ebe4 109 print_header_simple($streditingquestion, '', $strediting);
516cf3eb 110
516cf3eb 111 if ($form = data_submitted() and confirm_sesskey()) {
112
113 if (isset($form->versioning) && isset($question->id) and false) { // disable versioning until it is fixed.
114 // use new code that handles whether to overwrite or copy a question
115 // and keeps track of the versions in the quiz_question_version table
116
117 // $replaceinquiz is an array with the ids of all quizzes in which
118 // the teacher has chosen to replace the old version
119 $replaceinquiz = array();
120 foreach($form as $key => $val) {
121 if ($tmp = quiz_parse_fieldname($key, 'q')) {
122 if ($tmp['mode'] == 'replace') {
123 $replaceinquiz[$tmp['id']] = $tmp['id'];
124 unset($form->$key);
125 }
126 }
127 }
128
129 // $quizlist is an array with the ids of quizzes which use this question
130 $quizlist = array();
131 if ($instances = get_records('quiz_question_instances', 'question', $question->id)) {
132 foreach($instances as $instance) {
133 $quizlist[$instance->quiz] = $instance->quiz;
134 }
135 }
136
137 if (isset($form->makecopy)) { // explicitly requested copies should be unhidden
138 $question->hidden = 0;
139 }
140
141 // Logic to determine whether old version should be overwritten
142 $makecopy = isset($form->makecopy) || (!$form->id); unset($form->makecopy);
143 if ($makecopy) {
144 $replaceold = false;
145 } else {
146 // this should be improved to exclude teacher preview responses and empty responses
147 // the current code leaves many unneeded questions in the database
4f48fb42 148 $hasresponses = record_exists('question_states', 'question', $form->id) or
149 record_exists('question_states', 'originalquestion', $form->id);
516cf3eb 150 $replaceinall = ($quizlist == $replaceinquiz); // question is being replaced in all quizzes
151 $replaceold = !$hasresponses && $replaceinall;
152 }
153
b01a2fcd 154 $oldquestionid = false;
516cf3eb 155 if (!$replaceold) { // create a new question
156 $oldquestionid = $question->id;
157 if (!$makecopy) {
4f48fb42 158 if (!set_field("question", 'hidden', 1, 'id', $question->id)) {
516cf3eb 159 error("Could not hide question!");
160 }
161 }
162 unset($question->id);
163 }
164 unset($makecopy, $hasresponses, $replaceinall, $replaceold);
f02c6f01 165 $question = $QTYPES[$qtype]->save_question($question, $form, $course);
516cf3eb 166 if(!isset($question->id)) {
167 error("Failed to save the question!");
168 }
169
b01a2fcd 170 if(!empty($oldquestionid)) {
516cf3eb 171 // create version entries for different quizzes
172 $version = new object();
173 $version->oldquestion = $oldquestionid;
174 $version->newquestion = $question->id;
175 $version->userid = $USER->id;
176 $version->timestamp = time();
177
178 foreach($replaceinquiz as $qid) {
179 $version->quiz = $qid;
180 if(!insert_record("quiz_question_versions", $version)) {
181 error("Could not store version information of question $oldquestionid in quiz $qid!");
182 }
183 }
184
185 /// now update the question references in the quizzes
186 if (!empty($replaceinquiz) and $quizzes = get_records_list("quiz", "id", implode(',', $replaceinquiz))) {
187
188 foreach($quizzes as $quiz) {
189 $questionlist = ",$quiz->questions,"; // a little hack with the commas here. not nice but effective
190 $questionlist = str_replace(",$oldquestionid,", ",$question->id,", $questionlist);
191 $questionlist = substr($questionlist, 1, -1); // and get rid of the surrounding commas again
192 if (!set_field("quiz", 'questions', $questionlist, 'id', $quiz->id)) {
193 error("Could not update questionlist in quiz $quiz->id!");
194 }
195
196 // the quiz_question_instances table needs to be updated too (aah, the joys of duplication :)
197 if (!set_field('quiz_question_instances', 'question', $question->id, 'quiz', $quiz->id, 'question', $oldquestionid)) {
198 error("Could not update question instance!");
199 }
200 if (isset($SESSION->modform) && (int)$SESSION->modform->instance === (int)$quiz->id) {
201 $SESSION->modform->questions = $questionlist;
202 $SESSION->modform->grades[$question->id] = $SESSION->modform->grades[$oldquestionid];
203 unset($SESSION->modform->grades[$oldquestionid]);
204 }
205 }
206
207 // change question in attempts
208 if ($attempts = get_records_list('quiz_attempts', 'quiz', implode(',', $replaceinquiz))) {
209 foreach ($attempts as $attempt) {
210
211 // replace question id in $attempt->layout
212 $questionlist = ",$attempt->layout,"; // a little hack with the commas here. not nice but effective
213 $questionlist = str_replace(",$oldquestionid,", ",$question->id,", $questionlist);
214 $questionlist = substr($questionlist, 1, -1); // and get rid of the surrounding commas again
215 if (!set_field('quiz_attempts', 'layout', $questionlist, 'id', $attempt->id)) {
216 error("Could not update layout in attempt $attempt->id!");
217 }
218
219 // set originalquestion in states
4f48fb42 220 set_field('question_states', 'originalquestion', $oldquestionid, 'attempt', $attempt->uniqueid, 'question', $question->id, 'originalquestion', '0');
516cf3eb 221
222 // replace question id in states
4f48fb42 223 set_field('question_states', 'question', $question->id, 'attempt', $attempt->uniqueid, 'question', $oldquestionid);
516cf3eb 224
225 // replace question id in sessions
226 set_field('question_sessions', 'questionid', $question->id, 'attemptid', $attempt->uniqueid, 'questionid', $oldquestionid);
227
228 }
229
230 // Now do anything question-type specific that is required to replace the question
dc1f00de 231 // For example questions that use the question_answers table to hold part of their question will
516cf3eb 232 // have to recode the answer ids in the states
f02c6f01 233 $QTYPES[$question->qtype]->change_states_question($oldquestionid, $question, $attempts);
516cf3eb 234 }
235 }
236 }
237 } else {
238 // use the old code which simply overwrites old versions
239 // it is also used for creating new questions
76de2cdf 240
241 if (isset($form->makecopy)) {
242 $question->hidden = 0; // explicitly requested copies should be unhidden
243 $question->id = 0; // This will prompt save_question to create a new question
244 }
f02c6f01 245 $question = $QTYPES[$qtype]->save_question($question, $form, $course);
516cf3eb 246 $replaceinquiz = 'all';
247 }
248
f02c6f01 249 if (empty($question->errors) && $QTYPES[$qtype]->finished_edit_wizard($form)) {
516cf3eb 250 // DISABLED AUTOMATIC REGRADING
251 // Automagically regrade all attempts (and states) in the affected quizzes
252 //if (!empty($replaceinquiz)) {
f02c6f01 253 // $QTYPES[$question->qtype]->get_question_options($question);
516cf3eb 254 // quiz_regrade_question_in_quizzes($question, $replaceinquiz);
255 //}
ee6c9355 256
257 $strsaved = get_string('changessaved');
258 if ($inpopup) {
259 notify($strsaved, '');
260 close_window(3);
261 } else {
79b9651e 262 echo '</div>';
87391656 263 redirect($SESSION->returnurl);
ee6c9355 264 }
516cf3eb 265 }
266 }
267
e586cfb4 268 // prepare the grades selector drop-down used by many question types
b4db945e 269 $creategrades = get_grade_options();
270 $gradeoptions = $creategrades->gradeoptions;
271 $gradeoptionsfull = $creategrades->gradeoptionsfull;
516cf3eb 272
cc080913 273 // Initialise defaults if necessary.
516cf3eb 274 if (empty($question->id)) {
275 $question->id = "";
276 }
277 if (empty($question->name)) {
278 $question->name = "";
279 }
280 if (empty($question->questiontext)) {
281 $question->questiontext = "";
282 }
283 if (empty($question->image)) {
284 $question->image = "";
285 }
286 if (!isset($question->penalty)) {
287 $question->penalty = 0.1;
288 }
289 if (!isset($question->defaultgrade)) {
290 $question->defaultgrade = 1;
291 }
a4514d91 292 if (empty($question->generalfeedback)) {
293 $question->generalfeedback = "";
1b8a7434 294 }
516cf3eb 295
cc080913 296 // Set up some richtext editing if necessary
516cf3eb 297 if ($usehtmleditor = can_use_richtext_editor()) {
298 $defaultformat = FORMAT_HTML;
299 } else {
300 $defaultformat = FORMAT_MOODLE;
301 }
302
303 if (isset($question->errors)) {
304 $err = $question->errors;
305 }
306
cc080913 307 // Print the question editing form
516cf3eb 308 echo '<br />';
309 print_simple_box_start('center');
4eda4eec 310 require_once('type/'.$qtype.'/editquestion.php');
516cf3eb 311 print_simple_box_end();
312
313 if ($usehtmleditor) {
314 use_html_editor('questiontext');
315 }
316
317 print_footer($course);
318
319?>