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