New param type: PARAM_NUMBER, for floating point numbers.
[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 *//** */
20
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);
180f4106 30
31 $CFG->pagepath = 'question/type/'.$qtype;
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
91 require_login($course->id, false);
1680ebe4 92 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
93 require_capability('moodle/question:manage', $coursecontext);
516cf3eb 94
e586cfb4 95 // TODO: remove restriction to quiz
516cf3eb 96 $streditingquestion = get_string('editingquestion', 'quiz');
97 if (isset($SESSION->modform->instance)) {
2662cf46 98 $strediting = '<a href="'.$SESSION->returnurl.'">'.get_string('editingquiz', 'quiz').'</a> -> '.
516cf3eb 99 $streditingquestion;
100 } else {
101 $strediting = '<a href="edit.php?courseid='.$course->id.'">'.
102 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
103 }
104
1680ebe4 105 print_header_simple($streditingquestion, '', $strediting);
516cf3eb 106
516cf3eb 107 if ($form = data_submitted() and confirm_sesskey()) {
108
109 if (isset($form->versioning) && isset($question->id) and false) { // disable versioning until it is fixed.
110 // use new code that handles whether to overwrite or copy a question
111 // and keeps track of the versions in the quiz_question_version table
112
113 // $replaceinquiz is an array with the ids of all quizzes in which
114 // the teacher has chosen to replace the old version
115 $replaceinquiz = array();
116 foreach($form as $key => $val) {
117 if ($tmp = quiz_parse_fieldname($key, 'q')) {
118 if ($tmp['mode'] == 'replace') {
119 $replaceinquiz[$tmp['id']] = $tmp['id'];
120 unset($form->$key);
121 }
122 }
123 }
124
125 // $quizlist is an array with the ids of quizzes which use this question
126 $quizlist = array();
127 if ($instances = get_records('quiz_question_instances', 'question', $question->id)) {
128 foreach($instances as $instance) {
129 $quizlist[$instance->quiz] = $instance->quiz;
130 }
131 }
132
133 if (isset($form->makecopy)) { // explicitly requested copies should be unhidden
134 $question->hidden = 0;
135 }
136
137 // Logic to determine whether old version should be overwritten
138 $makecopy = isset($form->makecopy) || (!$form->id); unset($form->makecopy);
139 if ($makecopy) {
140 $replaceold = false;
141 } else {
142 // this should be improved to exclude teacher preview responses and empty responses
143 // the current code leaves many unneeded questions in the database
4f48fb42 144 $hasresponses = record_exists('question_states', 'question', $form->id) or
145 record_exists('question_states', 'originalquestion', $form->id);
516cf3eb 146 $replaceinall = ($quizlist == $replaceinquiz); // question is being replaced in all quizzes
147 $replaceold = !$hasresponses && $replaceinall;
148 }
149
b01a2fcd 150 $oldquestionid = false;
516cf3eb 151 if (!$replaceold) { // create a new question
152 $oldquestionid = $question->id;
153 if (!$makecopy) {
4f48fb42 154 if (!set_field("question", 'hidden', 1, 'id', $question->id)) {
516cf3eb 155 error("Could not hide question!");
156 }
157 }
158 unset($question->id);
159 }
160 unset($makecopy, $hasresponses, $replaceinall, $replaceold);
f02c6f01 161 $question = $QTYPES[$qtype]->save_question($question, $form, $course);
516cf3eb 162 if(!isset($question->id)) {
163 error("Failed to save the question!");
164 }
165
b01a2fcd 166 if(!empty($oldquestionid)) {
516cf3eb 167 // create version entries for different quizzes
168 $version = new object();
169 $version->oldquestion = $oldquestionid;
170 $version->newquestion = $question->id;
171 $version->userid = $USER->id;
172 $version->timestamp = time();
173
174 foreach($replaceinquiz as $qid) {
175 $version->quiz = $qid;
176 if(!insert_record("quiz_question_versions", $version)) {
177 error("Could not store version information of question $oldquestionid in quiz $qid!");
178 }
179 }
180
181 /// now update the question references in the quizzes
182 if (!empty($replaceinquiz) and $quizzes = get_records_list("quiz", "id", implode(',', $replaceinquiz))) {
183
184 foreach($quizzes as $quiz) {
185 $questionlist = ",$quiz->questions,"; // a little hack with the commas here. not nice but effective
186 $questionlist = str_replace(",$oldquestionid,", ",$question->id,", $questionlist);
187 $questionlist = substr($questionlist, 1, -1); // and get rid of the surrounding commas again
188 if (!set_field("quiz", 'questions', $questionlist, 'id', $quiz->id)) {
189 error("Could not update questionlist in quiz $quiz->id!");
190 }
191
192 // the quiz_question_instances table needs to be updated too (aah, the joys of duplication :)
193 if (!set_field('quiz_question_instances', 'question', $question->id, 'quiz', $quiz->id, 'question', $oldquestionid)) {
194 error("Could not update question instance!");
195 }
196 if (isset($SESSION->modform) && (int)$SESSION->modform->instance === (int)$quiz->id) {
197 $SESSION->modform->questions = $questionlist;
198 $SESSION->modform->grades[$question->id] = $SESSION->modform->grades[$oldquestionid];
199 unset($SESSION->modform->grades[$oldquestionid]);
200 }
201 }
202
203 // change question in attempts
204 if ($attempts = get_records_list('quiz_attempts', 'quiz', implode(',', $replaceinquiz))) {
205 foreach ($attempts as $attempt) {
206
207 // replace question id in $attempt->layout
208 $questionlist = ",$attempt->layout,"; // a little hack with the commas here. not nice but effective
209 $questionlist = str_replace(",$oldquestionid,", ",$question->id,", $questionlist);
210 $questionlist = substr($questionlist, 1, -1); // and get rid of the surrounding commas again
211 if (!set_field('quiz_attempts', 'layout', $questionlist, 'id', $attempt->id)) {
212 error("Could not update layout in attempt $attempt->id!");
213 }
214
215 // set originalquestion in states
4f48fb42 216 set_field('question_states', 'originalquestion', $oldquestionid, 'attempt', $attempt->uniqueid, 'question', $question->id, 'originalquestion', '0');
516cf3eb 217
218 // replace question id in states
4f48fb42 219 set_field('question_states', 'question', $question->id, 'attempt', $attempt->uniqueid, 'question', $oldquestionid);
516cf3eb 220
221 // replace question id in sessions
222 set_field('question_sessions', 'questionid', $question->id, 'attemptid', $attempt->uniqueid, 'questionid', $oldquestionid);
223
224 }
225
226 // Now do anything question-type specific that is required to replace the question
dc1f00de 227 // For example questions that use the question_answers table to hold part of their question will
516cf3eb 228 // have to recode the answer ids in the states
f02c6f01 229 $QTYPES[$question->qtype]->change_states_question($oldquestionid, $question, $attempts);
516cf3eb 230 }
231 }
232 }
233 } else {
234 // use the old code which simply overwrites old versions
235 // it is also used for creating new questions
76de2cdf 236
237 if (isset($form->makecopy)) {
238 $question->hidden = 0; // explicitly requested copies should be unhidden
239 $question->id = 0; // This will prompt save_question to create a new question
240 }
f02c6f01 241 $question = $QTYPES[$qtype]->save_question($question, $form, $course);
516cf3eb 242 $replaceinquiz = 'all';
243 }
244
f02c6f01 245 if (empty($question->errors) && $QTYPES[$qtype]->finished_edit_wizard($form)) {
516cf3eb 246 // DISABLED AUTOMATIC REGRADING
247 // Automagically regrade all attempts (and states) in the affected quizzes
248 //if (!empty($replaceinquiz)) {
f02c6f01 249 // $QTYPES[$question->qtype]->get_question_options($question);
516cf3eb 250 // quiz_regrade_question_in_quizzes($question, $replaceinquiz);
251 //}
ee6c9355 252
253 $strsaved = get_string('changessaved');
254 if ($inpopup) {
255 notify($strsaved, '');
256 close_window(3);
257 } else {
79b9651e 258 echo '</div>';
ee6c9355 259 redirect($SESSION->returnurl, $strsaved);
260 }
516cf3eb 261 }
262 }
263
e586cfb4 264 // prepare the grades selector drop-down used by many question types
b4db945e 265 $creategrades = get_grade_options();
266 $gradeoptions = $creategrades->gradeoptions;
267 $gradeoptionsfull = $creategrades->gradeoptionsfull;
516cf3eb 268
cc080913 269 // Initialise defaults if necessary.
516cf3eb 270 if (empty($question->id)) {
271 $question->id = "";
272 }
273 if (empty($question->name)) {
274 $question->name = "";
275 }
276 if (empty($question->questiontext)) {
277 $question->questiontext = "";
278 }
279 if (empty($question->image)) {
280 $question->image = "";
281 }
282 if (!isset($question->penalty)) {
283 $question->penalty = 0.1;
284 }
285 if (!isset($question->defaultgrade)) {
286 $question->defaultgrade = 1;
287 }
a4514d91 288 if (empty($question->generalfeedback)) {
289 $question->generalfeedback = "";
1b8a7434 290 }
516cf3eb 291
cc080913 292 // Set up some richtext editing if necessary
516cf3eb 293 if ($usehtmleditor = can_use_richtext_editor()) {
294 $defaultformat = FORMAT_HTML;
295 } else {
296 $defaultformat = FORMAT_MOODLE;
297 }
298
299 if (isset($question->errors)) {
300 $err = $question->errors;
301 }
302
cc080913 303 // Print the question editing form
516cf3eb 304 echo '<br />';
305 print_simple_box_start('center');
4eda4eec 306 require_once('type/'.$qtype.'/editquestion.php');
516cf3eb 307 print_simple_box_end();
308
309 if ($usehtmleditor) {
310 use_html_editor('questiontext');
311 }
312
313 print_footer($course);
314
315?>