Moving quiz-independent question scripts to their new location. In a following commit...
[moodle.git] / question / question.php
CommitLineData
516cf3eb 1<?php // $Id$
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* - delete question from quiz (delete, sesskey)
9* - delete question (in two steps)
10* - if question is in use: display this conflict (allow to hide the question?)
11* - else: confirm deletion and delete from database (sesskey, id, delete, confirm)
12* - cancel (cancel)
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 quiz
19*/
20
21 require_once("../../config.php");
22 require_once("locallib.php");
23 require_once($CFG->libdir.'/filelib.php');
24
25 $id = optional_param('id'); // question id
26
27 $qtype = optional_param('qtype');
28 $category = optional_param('category');
29
30 // a qtype > 99 means a remote question
31 if ($qtype > 99) {
32 $typeid = $qtype - 100;
33 $qtype = RQP;
34 }
35
36 $contextquiz = optional_param('contextquiz'); // the quiz from which this question is being edited
37
38 if (isset($_REQUEST['cancel'])) {
39 redirect("edit.php");
40 }
41
42 if(!empty($id) && isset($_REQUEST['hide']) && confirm_sesskey()) {
43 if(!set_field('quiz_questions', 'hidden', $_REQUEST['hide'], 'id', $id)) {
44 error("Faild to hide the question.");
45 }
46 redirect('edit.php');
47 }
48
49 if ($id) {
50 if (! $question = get_record("quiz_questions", "id", $id)) {
51 error("This question doesn't exist");
52 }
53 if (!empty($category)) {
54 $question->category = $category;
55 }
56 if (! $category = get_record("quiz_categories", "id", $question->category)) {
57 error("This question doesn't belong to a valid category!");
58 }
59 if (! $course = get_record("course", "id", $category->course)) {
60 error("This question category doesn't belong to a valid course!");
61 }
62
63 $qtype = $question->qtype;
64
65
66 } else if ($category) { // only for creating new questions
67 if (! $category = get_record("quiz_categories", "id", $category)) {
68 error("This wasn't a valid category!");
69 }
70 if (! $course = get_record("course", "id", $category->course)) {
71 error("This category doesn't belong to a valid course!");
72 }
73
74 $question->category = $category->id;
75 $question->qtype = $qtype;
76
77 } else {
78 error("Must specify question id or category");
79 }
80
81 if (empty($qtype)) {
82 error("No question type was specified!");
83 } else if (!isset($QUIZ_QTYPES[$qtype])) {
84 error("Could not find question type: '$qtype'");
85 }
86
87 require_login($course->id, false);
88
89 if (!isteacheredit($course->id)) {
90 error("You can't modify these questions!");
91 }
92
93 $strquizzes = get_string('modulenameplural', 'quiz');
94 $streditingquestion = get_string('editingquestion', 'quiz');
95 if (isset($SESSION->modform->instance)) {
96 $strediting = '<a href="edit.php">'.get_string('editingquiz', 'quiz').'</a> -> '.
97 $streditingquestion;
98 } else {
99 $strediting = '<a href="edit.php?courseid='.$course->id.'">'.
100 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
101 }
102
103 print_header_simple("$streditingquestion", "", $strediting);
104
105 if (isset($_REQUEST['delete'])) {
106 if (isset($confirm) and confirm_sesskey()) {
107 if ($confirm == md5($delete)) {
108 if (record_exists('quiz_question_instances', 'question', $question->id) or
109 record_exists('quiz_states', 'originalquestion', $question->id)) {
110 if (!set_field('quiz_questions', 'hidden', 1, 'id', $delete)) {
111 error('Was not able to hide question');
112 }
113 } else {
114 if (!delete_records("quiz_questions", "id", $question->id)) {
115 error("An error occurred trying to delete question (id $question->id)");
116 }
117 if (!delete_records("quiz_questions", "parent", $question->id)) {
118 error("An error occurred trying to delete question (id $question->id)");
119 }
120 }
121 redirect("edit.php");
122 } else {
123 error("Confirmation string was incorrect");
124 }
125
126 } else {
127 if ($quiznames = quizzes_question_used($id)) {
128 $a->questionname = $question->name;
129 $a->quiznames = implode(', ', $quiznames);
130 notify(get_string('questioninuse', 'quiz', $a));
131 }
132
133 notice_yesno(get_string("deletequestioncheck", "quiz", $question->name),
134 "question.php?sesskey=$USER->sesskey&amp;id=$question->id&amp;delete=$delete&amp;confirm=".md5($delete), "edit.php");
135 }
136 print_footer($course);
137 exit;
138 }
139
140 if ($form = data_submitted() and confirm_sesskey()) {
141
142 if (isset($form->versioning) && isset($question->id) and false) { // disable versioning until it is fixed.
143 // use new code that handles whether to overwrite or copy a question
144 // and keeps track of the versions in the quiz_question_version table
145
146 // $replaceinquiz is an array with the ids of all quizzes in which
147 // the teacher has chosen to replace the old version
148 $replaceinquiz = array();
149 foreach($form as $key => $val) {
150 if ($tmp = quiz_parse_fieldname($key, 'q')) {
151 if ($tmp['mode'] == 'replace') {
152 $replaceinquiz[$tmp['id']] = $tmp['id'];
153 unset($form->$key);
154 }
155 }
156 }
157
158 // $quizlist is an array with the ids of quizzes which use this question
159 $quizlist = array();
160 if ($instances = get_records('quiz_question_instances', 'question', $question->id)) {
161 foreach($instances as $instance) {
162 $quizlist[$instance->quiz] = $instance->quiz;
163 }
164 }
165
166 if (isset($form->makecopy)) { // explicitly requested copies should be unhidden
167 $question->hidden = 0;
168 }
169
170 // Logic to determine whether old version should be overwritten
171 $makecopy = isset($form->makecopy) || (!$form->id); unset($form->makecopy);
172 if ($makecopy) {
173 $replaceold = false;
174 } else {
175 // this should be improved to exclude teacher preview responses and empty responses
176 // the current code leaves many unneeded questions in the database
177 $hasresponses = record_exists('quiz_states', 'question', $form->id) or
178 record_exists('quiz_states', 'originalquestion', $form->id);
179 $replaceinall = ($quizlist == $replaceinquiz); // question is being replaced in all quizzes
180 $replaceold = !$hasresponses && $replaceinall;
181 }
182
183 if (!$replaceold) { // create a new question
184 $oldquestionid = $question->id;
185 if (!$makecopy) {
186 if (!set_field("quiz_questions", 'hidden', 1, 'id', $question->id)) {
187 error("Could not hide question!");
188 }
189 }
190 unset($question->id);
191 }
192 unset($makecopy, $hasresponses, $replaceinall, $replaceold);
193 $question = $QUIZ_QTYPES[$qtype]->save_question($question, $form, $course);
194 if(!isset($question->id)) {
195 error("Failed to save the question!");
196 }
197
198 if(isset($oldquestionid)) {
199 // create version entries for different quizzes
200 $version = new object();
201 $version->oldquestion = $oldquestionid;
202 $version->newquestion = $question->id;
203 $version->userid = $USER->id;
204 $version->timestamp = time();
205
206 foreach($replaceinquiz as $qid) {
207 $version->quiz = $qid;
208 if(!insert_record("quiz_question_versions", $version)) {
209 error("Could not store version information of question $oldquestionid in quiz $qid!");
210 }
211 }
212
213 /// now update the question references in the quizzes
214 if (!empty($replaceinquiz) and $quizzes = get_records_list("quiz", "id", implode(',', $replaceinquiz))) {
215
216 foreach($quizzes as $quiz) {
217 $questionlist = ",$quiz->questions,"; // a little hack with the commas here. not nice but effective
218 $questionlist = str_replace(",$oldquestionid,", ",$question->id,", $questionlist);
219 $questionlist = substr($questionlist, 1, -1); // and get rid of the surrounding commas again
220 if (!set_field("quiz", 'questions', $questionlist, 'id', $quiz->id)) {
221 error("Could not update questionlist in quiz $quiz->id!");
222 }
223
224 // the quiz_question_instances table needs to be updated too (aah, the joys of duplication :)
225 if (!set_field('quiz_question_instances', 'question', $question->id, 'quiz', $quiz->id, 'question', $oldquestionid)) {
226 error("Could not update question instance!");
227 }
228 if (isset($SESSION->modform) && (int)$SESSION->modform->instance === (int)$quiz->id) {
229 $SESSION->modform->questions = $questionlist;
230 $SESSION->modform->grades[$question->id] = $SESSION->modform->grades[$oldquestionid];
231 unset($SESSION->modform->grades[$oldquestionid]);
232 }
233 }
234
235 // change question in attempts
236 if ($attempts = get_records_list('quiz_attempts', 'quiz', implode(',', $replaceinquiz))) {
237 foreach ($attempts as $attempt) {
238
239 // replace question id in $attempt->layout
240 $questionlist = ",$attempt->layout,"; // a little hack with the commas here. not nice but effective
241 $questionlist = str_replace(",$oldquestionid,", ",$question->id,", $questionlist);
242 $questionlist = substr($questionlist, 1, -1); // and get rid of the surrounding commas again
243 if (!set_field('quiz_attempts', 'layout', $questionlist, 'id', $attempt->id)) {
244 error("Could not update layout in attempt $attempt->id!");
245 }
246
247 // set originalquestion in states
248 set_field('quiz_states', 'originalquestion', $oldquestionid, 'attempt', $attempt->uniqueid, 'question', $question->id, 'originalquestion', '0');
249
250 // replace question id in states
251 set_field('quiz_states', 'question', $question->id, 'attempt', $attempt->uniqueid, 'question', $oldquestionid);
252
253 // replace question id in sessions
254 set_field('question_sessions', 'questionid', $question->id, 'attemptid', $attempt->uniqueid, 'questionid', $oldquestionid);
255
256 }
257
258 // Now do anything question-type specific that is required to replace the question
259 // For example questions that use the quiz_answers table to hold part of their question will
260 // have to recode the answer ids in the states
261 $QUIZ_QTYPES[$question->qtype]->change_states_question($oldquestionid, $question, $attempts);
262 }
263 }
264 }
265 } else {
266 // use the old code which simply overwrites old versions
267 // it is also used for creating new questions
268 $question = $QUIZ_QTYPES[$qtype]->save_question($question, $form, $course);
269 $replaceinquiz = 'all';
270 }
271
272 if (empty($question->errors) && $QUIZ_QTYPES[$qtype]->finished_edit_wizard($form)) {
273 // DISABLED AUTOMATIC REGRADING
274 // Automagically regrade all attempts (and states) in the affected quizzes
275 //if (!empty($replaceinquiz)) {
276 // $QUIZ_QTYPES[$question->qtype]->get_question_options($question);
277 // quiz_regrade_question_in_quizzes($question, $replaceinquiz);
278 //}
279 redirect("edit.php");
280 }
281 }
282
283 $grades = array(1,0.9,0.8,0.75,0.70,0.66666,0.60,0.50,0.40,0.33333,0.30,0.25,0.20,0.16666,0.142857,0.125,0.11111,0.10,0.05,0);
284 foreach ($grades as $grade) {
285 $percentage = 100 * $grade;
286 $neggrade = -$grade;
287 $gradeoptions["$grade"] = "$percentage %";
288 $gradeoptionsfull["$grade"] = "$percentage %";
289 $gradeoptionsfull["$neggrade"] = -$percentage." %";
290 }
291 $gradeoptionsfull["0"] = $gradeoptions["0"] = get_string("none");
292
293 arsort($gradeoptions, SORT_NUMERIC);
294 arsort($gradeoptionsfull, SORT_NUMERIC);
295
296 if (!$categories = quiz_get_category_menu($course->id, false)) {
297 error("No categories!");
298 }
299
300
301 make_upload_directory("$course->id"); // Just in case
302 $coursefiles = get_directory_list("$CFG->dataroot/$course->id", $CFG->moddata);
303 foreach ($coursefiles as $filename) {
304 if (mimeinfo("icon", $filename) == "image.gif") {
305 $images["$filename"] = $filename;
306 }
307 }
308
309 // Print the question editing form
310
311 if (empty($question->id)) {
312 $question->id = "";
313 }
314 if (empty($question->name)) {
315 $question->name = "";
316 }
317 if (empty($question->questiontext)) {
318 $question->questiontext = "";
319 }
320 if (empty($question->image)) {
321 $question->image = "";
322 }
323 if (!isset($question->penalty)) {
324 $question->penalty = 0.1;
325 }
326 if (!isset($question->defaultgrade)) {
327 $question->defaultgrade = 1;
328 }
329
330 // Set up some Richtext editing if necessary
331 if ($usehtmleditor = can_use_richtext_editor()) {
332 $defaultformat = FORMAT_HTML;
333 } else {
334 $defaultformat = FORMAT_MOODLE;
335 }
336
337 if (isset($question->errors)) {
338 $err = $question->errors;
339 }
340
341 echo '<br />';
342 print_simple_box_start('center');
343 require_once('questiontypes/'.$QUIZ_QTYPES[$qtype]->name().'/editquestion.php');
344 print_simple_box_end();
345
346 if ($usehtmleditor) {
347 use_html_editor('questiontext');
348 }
349
350 print_footer($course);
351
352?>