Automatic installer.php lang files by installer_builder (20061110)
[moodle.git] / question / question.php
blob2ea69d08fbe7806d3908ea40bedbc4de4528d330
1 <?php
2 /**
3 * Page for editing questions
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)
10 * TODO: currently this still treats the quiz as special
11 * TODO: question versioning is not currently enabled
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 *//** */
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');
25 $id = optional_param('id', 0, PARAM_INT); // question id
27 $qtype = optional_param('qtype', '', PARAM_FILE);
28 $category = optional_param('category', 0, PARAM_INT);
29 $inpopup = optional_param('inpopup', 0, PARAM_BOOL);
31 $CFG->pagepath = 'question/type/'.$qtype;
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';
40 if ($id) {
41 if (! $question = get_record("question", "id", $id)) {
42 error("This question doesn't exist");
44 if (!empty($category)) {
45 $question->category = $category;
47 if (! $category = get_record("question_categories", "id", $question->category)) {
48 error("This question doesn't belong to a valid category!");
50 if (! $course = get_record("course", "id", $category->course)) {
51 error("This question category doesn't belong to a valid course!");
54 $qtype = $question->qtype;
55 if (!isset($QTYPES[$qtype])) {
56 $qtype = 'missingtype';
59 } else if ($category) { // only for creating new questions
60 if (! $category = get_record("question_categories", "id", $category)) {
61 error("This wasn't a valid category!");
63 if (! $course = get_record("course", "id", $category->course)) {
64 error("This category doesn't belong to a valid course!");
67 $question->category = $category->id;
68 $question->qtype = $qtype;
70 } else {
71 error("Must specify question id or category");
74 if (!isset($SESSION->returnurl)) {
75 $SESSION->returnurl = 'edit.php?courseid='.$course->id;
78 // TODO: generalise this so it works for any activity
79 $contextquiz = isset($SESSION->modform->instance) ? $SESSION->modform->instance : 0;
81 if (isset($_REQUEST['cancel'])) {
82 redirect($SESSION->returnurl);
85 if (empty($qtype)) {
86 error("No question type was specified!");
87 } else if (!isset($QTYPES[$qtype])) {
88 error("Could not find question type: '$qtype'");
91 require_login($course->id, false);
92 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
93 require_capability('moodle/question:manage', $coursecontext);
95 // TODO: remove restriction to quiz
96 $streditingquestion = get_string('editingquestion', 'quiz');
97 if (isset($SESSION->modform->instance)) {
98 $strediting = '<a href="'.$SESSION->returnurl.'">'.get_string('editingquiz', 'quiz').'</a> -> '.
99 $streditingquestion;
100 } else {
101 $strediting = '<a href="edit.php?courseid='.$course->id.'">'.
102 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
105 print_header_simple($streditingquestion, '', $strediting);
107 if ($form = data_submitted() and confirm_sesskey()) {
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
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);
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;
133 if (isset($form->makecopy)) { // explicitly requested copies should be unhidden
134 $question->hidden = 0;
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
144 $hasresponses = record_exists('question_states', 'question', $form->id) or
145 record_exists('question_states', 'originalquestion', $form->id);
146 $replaceinall = ($quizlist == $replaceinquiz); // question is being replaced in all quizzes
147 $replaceold = !$hasresponses && $replaceinall;
150 $oldquestionid = false;
151 if (!$replaceold) { // create a new question
152 $oldquestionid = $question->id;
153 if (!$makecopy) {
154 if (!set_field("question", 'hidden', 1, 'id', $question->id)) {
155 error("Could not hide question!");
158 unset($question->id);
160 unset($makecopy, $hasresponses, $replaceinall, $replaceold);
161 $question = $QTYPES[$qtype]->save_question($question, $form, $course);
162 if(!isset($question->id)) {
163 error("Failed to save the question!");
166 if(!empty($oldquestionid)) {
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();
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!");
181 /// now update the question references in the quizzes
182 if (!empty($replaceinquiz) and $quizzes = get_records_list("quiz", "id", implode(',', $replaceinquiz))) {
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!");
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!");
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]);
203 // change question in attempts
204 if ($attempts = get_records_list('quiz_attempts', 'quiz', implode(',', $replaceinquiz))) {
205 foreach ($attempts as $attempt) {
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!");
215 // set originalquestion in states
216 set_field('question_states', 'originalquestion', $oldquestionid, 'attempt', $attempt->uniqueid, 'question', $question->id, 'originalquestion', '0');
218 // replace question id in states
219 set_field('question_states', 'question', $question->id, 'attempt', $attempt->uniqueid, 'question', $oldquestionid);
221 // replace question id in sessions
222 set_field('question_sessions', 'questionid', $question->id, 'attemptid', $attempt->uniqueid, 'questionid', $oldquestionid);
226 // Now do anything question-type specific that is required to replace the question
227 // For example questions that use the question_answers table to hold part of their question will
228 // have to recode the answer ids in the states
229 $QTYPES[$question->qtype]->change_states_question($oldquestionid, $question, $attempts);
233 } else {
234 // use the old code which simply overwrites old versions
235 // it is also used for creating new questions
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
241 $question = $QTYPES[$qtype]->save_question($question, $form, $course);
242 $replaceinquiz = 'all';
245 if (empty($question->errors) && $QTYPES[$qtype]->finished_edit_wizard($form)) {
246 // DISABLED AUTOMATIC REGRADING
247 // Automagically regrade all attempts (and states) in the affected quizzes
248 //if (!empty($replaceinquiz)) {
249 // $QTYPES[$question->qtype]->get_question_options($question);
250 // quiz_regrade_question_in_quizzes($question, $replaceinquiz);
253 $strsaved = get_string('changessaved');
254 if ($inpopup) {
255 notify($strsaved, '');
256 close_window(3);
257 } else {
258 redirect($SESSION->returnurl, $strsaved);
263 // prepare the grades selector drop-down used by many question types
264 $creategrades = get_grade_options();
265 $gradeoptions = $creategrades->gradeoptions;
266 $gradeoptionsfull = $creategrades->gradeoptionsfull;
268 // Initialise defaults if necessary.
269 if (empty($question->id)) {
270 $question->id = "";
272 if (empty($question->name)) {
273 $question->name = "";
275 if (empty($question->questiontext)) {
276 $question->questiontext = "";
278 if (empty($question->image)) {
279 $question->image = "";
281 if (!isset($question->penalty)) {
282 $question->penalty = 0.1;
284 if (!isset($question->defaultgrade)) {
285 $question->defaultgrade = 1;
287 if (empty($question->generalfeedback)) {
288 $question->generalfeedback = "";
291 // Set up some richtext editing if necessary
292 if ($usehtmleditor = can_use_richtext_editor()) {
293 $defaultformat = FORMAT_HTML;
294 } else {
295 $defaultformat = FORMAT_MOODLE;
298 if (isset($question->errors)) {
299 $err = $question->errors;
302 // Print the question editing form
303 echo '<br />';
304 print_simple_box_start('center');
305 require_once('type/'.$qtype.'/editquestion.php');
306 print_simple_box_end();
308 if ($usehtmleditor) {
309 use_html_editor('questiontext');
312 print_footer($course);