$CFG->tabselectedtofront is out. Part of MDL-8746
[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
516cf3eb 99
516cf3eb 100 if ($form = data_submitted() and confirm_sesskey()) {
101
102 if (isset($form->versioning) && isset($question->id) and false) { // disable versioning until it is fixed.
103 // use new code that handles whether to overwrite or copy a question
104 // and keeps track of the versions in the quiz_question_version table
105
106 // $replaceinquiz is an array with the ids of all quizzes in which
107 // the teacher has chosen to replace the old version
108 $replaceinquiz = array();
109 foreach($form as $key => $val) {
110 if ($tmp = quiz_parse_fieldname($key, 'q')) {
111 if ($tmp['mode'] == 'replace') {
112 $replaceinquiz[$tmp['id']] = $tmp['id'];
113 unset($form->$key);
114 }
115 }
116 }
117
118 // $quizlist is an array with the ids of quizzes which use this question
119 $quizlist = array();
120 if ($instances = get_records('quiz_question_instances', 'question', $question->id)) {
121 foreach($instances as $instance) {
122 $quizlist[$instance->quiz] = $instance->quiz;
123 }
124 }
125
126 if (isset($form->makecopy)) { // explicitly requested copies should be unhidden
127 $question->hidden = 0;
128 }
129
130 // Logic to determine whether old version should be overwritten
131 $makecopy = isset($form->makecopy) || (!$form->id); unset($form->makecopy);
132 if ($makecopy) {
133 $replaceold = false;
134 } else {
135 // this should be improved to exclude teacher preview responses and empty responses
136 // the current code leaves many unneeded questions in the database
4f48fb42 137 $hasresponses = record_exists('question_states', 'question', $form->id) or
138 record_exists('question_states', 'originalquestion', $form->id);
516cf3eb 139 $replaceinall = ($quizlist == $replaceinquiz); // question is being replaced in all quizzes
140 $replaceold = !$hasresponses && $replaceinall;
141 }
142
b01a2fcd 143 $oldquestionid = false;
516cf3eb 144 if (!$replaceold) { // create a new question
145 $oldquestionid = $question->id;
146 if (!$makecopy) {
4f48fb42 147 if (!set_field("question", 'hidden', 1, 'id', $question->id)) {
516cf3eb 148 error("Could not hide question!");
149 }
150 }
151 unset($question->id);
152 }
153 unset($makecopy, $hasresponses, $replaceinall, $replaceold);
f02c6f01 154 $question = $QTYPES[$qtype]->save_question($question, $form, $course);
516cf3eb 155 if(!isset($question->id)) {
156 error("Failed to save the question!");
157 }
158
b01a2fcd 159 if(!empty($oldquestionid)) {
516cf3eb 160 // create version entries for different quizzes
161 $version = new object();
162 $version->oldquestion = $oldquestionid;
163 $version->newquestion = $question->id;
164 $version->userid = $USER->id;
165 $version->timestamp = time();
166
167 foreach($replaceinquiz as $qid) {
168 $version->quiz = $qid;
169 if(!insert_record("quiz_question_versions", $version)) {
170 error("Could not store version information of question $oldquestionid in quiz $qid!");
171 }
172 }
173
174 /// now update the question references in the quizzes
175 if (!empty($replaceinquiz) and $quizzes = get_records_list("quiz", "id", implode(',', $replaceinquiz))) {
176
177 foreach($quizzes as $quiz) {
178 $questionlist = ",$quiz->questions,"; // a little hack with the commas here. not nice but effective
179 $questionlist = str_replace(",$oldquestionid,", ",$question->id,", $questionlist);
180 $questionlist = substr($questionlist, 1, -1); // and get rid of the surrounding commas again
181 if (!set_field("quiz", 'questions', $questionlist, 'id', $quiz->id)) {
182 error("Could not update questionlist in quiz $quiz->id!");
183 }
184
185 // the quiz_question_instances table needs to be updated too (aah, the joys of duplication :)
186 if (!set_field('quiz_question_instances', 'question', $question->id, 'quiz', $quiz->id, 'question', $oldquestionid)) {
187 error("Could not update question instance!");
188 }
189 if (isset($SESSION->modform) && (int)$SESSION->modform->instance === (int)$quiz->id) {
190 $SESSION->modform->questions = $questionlist;
191 $SESSION->modform->grades[$question->id] = $SESSION->modform->grades[$oldquestionid];
192 unset($SESSION->modform->grades[$oldquestionid]);
193 }
194 }
195
196 // change question in attempts
197 if ($attempts = get_records_list('quiz_attempts', 'quiz', implode(',', $replaceinquiz))) {
198 foreach ($attempts as $attempt) {
199
200 // replace question id in $attempt->layout
201 $questionlist = ",$attempt->layout,"; // a little hack with the commas here. not nice but effective
202 $questionlist = str_replace(",$oldquestionid,", ",$question->id,", $questionlist);
203 $questionlist = substr($questionlist, 1, -1); // and get rid of the surrounding commas again
204 if (!set_field('quiz_attempts', 'layout', $questionlist, 'id', $attempt->id)) {
205 error("Could not update layout in attempt $attempt->id!");
206 }
207
208 // set originalquestion in states
4f48fb42 209 set_field('question_states', 'originalquestion', $oldquestionid, 'attempt', $attempt->uniqueid, 'question', $question->id, 'originalquestion', '0');
516cf3eb 210
211 // replace question id in states
4f48fb42 212 set_field('question_states', 'question', $question->id, 'attempt', $attempt->uniqueid, 'question', $oldquestionid);
516cf3eb 213
214 // replace question id in sessions
215 set_field('question_sessions', 'questionid', $question->id, 'attemptid', $attempt->uniqueid, 'questionid', $oldquestionid);
216
217 }
218
219 // Now do anything question-type specific that is required to replace the question
dc1f00de 220 // For example questions that use the question_answers table to hold part of their question will
516cf3eb 221 // have to recode the answer ids in the states
f02c6f01 222 $QTYPES[$question->qtype]->change_states_question($oldquestionid, $question, $attempts);
516cf3eb 223 }
224 }
225 }
226 } else {
227 // use the old code which simply overwrites old versions
228 // it is also used for creating new questions
76de2cdf 229
230 if (isset($form->makecopy)) {
231 $question->hidden = 0; // explicitly requested copies should be unhidden
232 $question->id = 0; // This will prompt save_question to create a new question
233 }
f02c6f01 234 $question = $QTYPES[$qtype]->save_question($question, $form, $course);
516cf3eb 235 $replaceinquiz = 'all';
236 }
237
f02c6f01 238 if (empty($question->errors) && $QTYPES[$qtype]->finished_edit_wizard($form)) {
516cf3eb 239 // DISABLED AUTOMATIC REGRADING
240 // Automagically regrade all attempts (and states) in the affected quizzes
241 //if (!empty($replaceinquiz)) {
f02c6f01 242 // $QTYPES[$question->qtype]->get_question_options($question);
516cf3eb 243 // quiz_regrade_question_in_quizzes($question, $replaceinquiz);
244 //}
ee6c9355 245
246 $strsaved = get_string('changessaved');
247 if ($inpopup) {
248 notify($strsaved, '');
249 close_window(3);
250 } else {
79b9651e 251 echo '</div>';
87391656 252 redirect($SESSION->returnurl);
ee6c9355 253 }
516cf3eb 254 }
255 }
8c2550c8 256 // TODO: remove restriction to quiz
257 $streditingquestion = get_string('editingquestion', 'quiz');
258 if (isset($SESSION->modform->instance)) {
259 $strediting = '<a href="'.$SESSION->returnurl.'">'.get_string('editingquiz', 'quiz').'</a> -> '.
260 $streditingquestion;
261 } else {
262 $strediting = '<a href="edit.php?courseid='.$course->id.'">'.
263 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
264 }
265
266 print_header_simple($streditingquestion, '', $strediting);
516cf3eb 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?>