Updated questionlib to work with the new location of the question scripts
[moodle.git] / question / edit.php
CommitLineData
516cf3eb 1<?php // $Id$
2/**
3* Page to edit quizzes
4*
5* This page generally has two columns:
6* The right column lists all available questions in a chosen category and
7* allows them to be edited or more to be added. This column is only there if
8* the quiz does not already have student attempts
9* The left column lists all questions that have been added to the current quiz.
10* This column is only there if this page has been called in the context of a quiz.
11* The lecturer can add questions from the right hand list to the quiz or remove them
12*
13* The script also processes a number of actions:
14* Actions affecting a quiz:
15* up and down Changes the order of questions and page breaks
16* addquestion Adds a single question to the quiz
17* add Adds several selected questions to the quiz
18* addrandom Adds a certain number of random questions to the quiz
19* delete Removes a question from the quiz
20* savechanges Saves the order and grades for questions in the quiz
21* repaginate Re-paginates the quiz
22* Actions affecting the question pool:
23* move Moves a question to a different category
24* deleteselected Deletes the selected questions from the category
25* Other actions:
26* cat Chooses the category
27* displayoptions Sets display options
28*
29* @version $Id$
30* @author Martin Dougiamas and many others. This has recently been extensively
31* rewritten by Gustav Delius and other members of the Serving Mathematics project
32* {@link http://maths.york.ac.uk/serving_maths}
33* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
34* @package quiz
35*/
36 require_once("../../config.php");
37 require_once("editlib.php");
38
39 require_login();
40
41 $courseid = optional_param('courseid');
42 $quizid = optional_param('quizid');
43 $page = optional_param('page', -1);
44 $perpage = optional_param('perpage', 20);
45 $sortorder = optional_param('sortorder', 'qtype, name ASC');
46 if (preg_match("/[';]/", $sortorder)) {
47 error("Incorrect use of the parameter 'sortorder'");
48 }
49
50 $strquizzes = get_string('modulenameplural', 'quiz');
51 $strquiz = get_string('modulename', 'quiz');
52 $streditingquestions = get_string('editquestions', "quiz");
53 $streditingquiz = get_string("editinga", "moodle", $strquiz);
54
55 if ($modform = data_submitted() and !empty($modform->course)) { // data submitted
56
57 $SESSION->modform = $modform; // Save the form in the current session
58
59 } else if ($quizid) {
60 if (isset($SESSION->modform->id) and $SESSION->modform->id == $quizid) {
61 // modform for this quiz already exists, use it
62 $modform = $SESSION->modform;
63 } else {
64 // create new modform from database
65 if (! $modform = get_record('quiz', 'id', $quizid)) {
66 error("The required quiz doesn't exist");
67 }
68 $modform->instance = $modform->id;
69 $SESSION->modform = $modform; // Save the form in the current session
70
71 $cm = get_coursemodule_from_instance('quiz', $modform->instance);
72 $modform->cmid = $cm->id;
73 // We don't log all visits to this page but only those that recreate modform
74 add_to_log($cm->course, 'quiz', 'editquestions',
75 "view.php?id=$cm->id",
76 "$quizid", $cm->id);
77 }
78
79 } else if ($courseid) { // Page retrieve through "Edit Questions" link - no quiz selected
80 $modform->course = $courseid;
81 unset($modform->instance);
82 $SESSION->modform = $modform; // Save the form in the current session
83
84 add_to_log($courseid, 'quiz', 'editquestions', "index.php?id=$courseid");
85
86 } else if (!empty($sortorder)) {
87 // no quiz or course was specified so we need to use the stored modform
88 if (isset($SESSION->modform)) {
89 $modform = $SESSION->modform;
90 } else {
91 exit;
92 }
93 } else {
94 // we might get here after editing a question in
95 // a popup window. So close window automatically.
96?>
97<script type="text/javascript">
98<!--
99if (self.name == 'editquestion') {
100 self.close();
101}
102-->
103</script>
104<noscript>
105<?php notify(get_string('pleaseclose', 'quiz')); ?>
106</noscript>
107<?php
108 // no quiz or course was specified so we need to use the stored modform
109 if (isset($SESSION->modform)) {
110 $modform = $SESSION->modform;
111 } else {
112 exit;
113 }
114 }
115
116 if (! $course = get_record("course", "id", $modform->course)) {
117 error("This course doesn't exist");
118 }
119
120 require_login($course->id, false);
121
122 if (!isteacheredit($course->id)) {
123 error("You can't modify this course!");
124 }
125
126 if (isset($modform->instance)
127 && empty($modform->grades)) // Construct an array to hold all the grades.
128 {
129 $modform->grades = quiz_get_all_question_grades($modform);
130 }
131
132 if ($page > -1) {
133 $modform->page = $page;
134 } else {
135 $page = isset($modform->page) ? $modform->page : 0;
136 }
137
138/// Now, check for commands on this page and modify variables as necessary
139
140 if (isset($_REQUEST['up']) and confirm_sesskey()) { /// Move the given question up a slot
141 $questions = explode(",", $modform->questions);
142 if ($up > 0 and isset($questions[$up])) {
143 $prevkey = ($questions[$up-1] == 0) ? $up-2 : $up-1;
144 $swap = $questions[$prevkey];
145 $questions[$prevkey] = $questions[$up];
146 $questions[$up] = $swap;
147 $modform->questions = implode(",", $questions);
148 // Always have a page break at the end
149 $modform->questions = $modform->questions . ',0';
150 // Avoid duplicate page breaks
151 $modform->questions = str_replace(',0,0', ',0', $modform->questions);
152 if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) {
153 error('Could not save question list');
154 }
155 }
156 }
157
158 if (isset($_REQUEST['down']) and confirm_sesskey()) { /// Move the given question down a slot
159 $questions = explode(",", $modform->questions);
160 if ($down < count($questions)) {
161 $nextkey = ($questions[$down+1] == 0) ? $down+2 : $down+1;
162 $swap = $questions[$nextkey];
163 $questions[$nextkey] = $questions[$down];
164 $questions[$down] = $swap;
165 $modform->questions = implode(",", $questions);
166 // Avoid duplicate page breaks
167 $modform->questions = str_replace(',0,0', ',0', $modform->questions);
168 if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) {
169 error('Could not save question list');
170 }
171 }
172 }
173
174 if (isset($_REQUEST['addquestion']) and confirm_sesskey()) { /// Add a single question to the current quiz
175 quiz_add_quiz_question($_REQUEST['addquestion'], $modform);
176 }
177
178 if (isset($_REQUEST['add']) and confirm_sesskey()) { /// Add selected questions to the current quiz
179 foreach ($_POST as $key => $value) { // Parse input for question ids
180 if (substr($key, 0, 1) == "q") {
181 quiz_add_quiz_question(substr($key,1), $modform);
182 }
183 }
184 }
185
186 if (isset($_REQUEST['addrandom']) and confirm_sesskey()) { /// Add random questions to the quiz
187 $recurse = optional_param('recurse', 0, PARAM_BOOL);
188 $categoryid = required_param('categoryid', PARAM_INT);
189 $randomcount = required_param('randomcount', PARAM_INT);
190 // load category
191 if (! $category = get_record('quiz_categories', 'id', $categoryid)) {
192 error('Category ID is incorrect');
193 }
194 // find existing random questions in this category
195 $random = RANDOM;
196 if ($existingquestions = get_records_select('quiz_questions', "qtype = '$random' AND category = '$category->id'")) {
197 // now remove the ones that are already used in this quiz
198 if ($questionids = explode(',', $modform->questions)) {
199 foreach ($questionids as $questionid) {
200 unset($existingquestions[$questionid]);
201 }
202 }
203 // now take as many of these as needed
204 $i = 0;
205 while (($existingquestion = array_pop($existingquestions)) and ($i < $randomcount)) {
206 if ($existingquestion->questiontext == $recurse) {
207 // this question has the right recurse property, so use it
208 quiz_add_quiz_question($existingquestion->id, $modform);
209 $i++;
210 }
211 }
212 $randomcreate = $randomcount - $i; // the number of additional random questions needed.
213 } else {
214 $randomcreate = $randomcount;
215 }
216
217 if ($randomcreate > 0) {
218
219 $form->name = get_string('random', 'quiz') .' ('. $category->name .')';
220 $form->questiontext = $recurse; // we use the questiontext field to store the info
221 // on whether to include questions in subcategories
222 $form->questiontextformat = 0;
223 $form->image = '';
224 $form->defaultgrade = 1;
225 $form->hidden = 1;
226 for ($i=0; $i<$randomcreate; $i++) {
227 $form->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
228 $question = new stdClass;
229 $question->category = $category->id;
230 $question->qtype = RANDOM;
231 $question = $QUIZ_QTYPES[RANDOM]->save_question($question, $form, $course);
232 if(!isset($question->id)) {
233 error('Could not insert new random question!');
234 }
235 quiz_add_quiz_question($question->id, $modform);
236 }
237 }
238 }
239
240 if (isset($_REQUEST['repaginate']) and confirm_sesskey()) { /// Re-paginate the quiz
241 if (isset($_REQUEST['questionsperpage'])) {
242 $modform->questionsperpage = required_param('questionsperpage', 1, PARAM_INT);
243 if (!set_field('quiz', 'questionsperpage', $modform->questionsperpage, 'id', $modform->id)) {
244 error('Could not save number of questions per page');
245 }
246 }
247 $modform->questions = quiz_repaginate($modform->questions, $modform->questionsperpage);
248 if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->id)) {
249 error('Could not save layout');
250 }
251 }
252
253 if (isset($_REQUEST['move']) and confirm_sesskey()) { /// Move selected questions to new category
254 if (!$tocategory = get_record('quiz_categories', 'id', $_REQUEST['category'])) {
255 error('Invalid category');
256 }
257 if (!isteacheredit($tocategory->course)) {
258 error(get_string('categorynoedit', 'quiz', $tocategory->name), 'edit.php');
259 }
260 foreach ($_POST as $key => $value) { // Parse input for question ids
261 if (substr($key, 0, 1) == "q") {
262 $key = substr($key,1);
263 if (!set_field('quiz_questions', 'category', $tocategory->id, 'id', $key)) {
264 error('Could not update category field');
265 }
266 }
267 }
268 }
269
270 if (isset($_REQUEST['delete']) and confirm_sesskey()) { /// Remove a question from the quiz
271 quiz_delete_quiz_question($_REQUEST['delete'], $modform);
272 }
273
274 if (isset($_REQUEST['deleteselected'])) { // delete selected questions from the category
275
276 if (isset($confirm) and confirm_sesskey()) { // teacher has already confirmed the action
277 if ($confirm == md5($deleteselected)) {
278 if ($questionlist = explode(',', $deleteselected)) {
279 // for each question either hide it if it is in use or delete it
280 foreach ($questionlist as $questionid) {
281 if (record_exists('quiz_question_instances', 'question', $questionid) or
282 record_exists('quiz_states', 'originalquestion', $questionid)) {
283 if (!set_field('quiz_questions', 'hidden', 1, 'id', $questionid)) {
284 error('Was not able to hide question');
285 }
286 } else {
287 delete_records("quiz_questions", "id", $questionid);
288 }
289 }
290 }
291 redirect("edit.php");
292 } else {
293 error("Confirmation string was incorrect");
294 }
295
296 } else { // teacher still has to confirm
297 // make a list of all the questions that are selected
298 $rawquestions = $_POST;
299 $questionlist = ''; // comma separated list of ids of questions to be deleted
300 $questionnames = ''; // string with names of questions separated by <br /> with
301 // an asterix in front of those that are in use
302 $inuse = false; // set to true if at least one of the questions is in use
303 foreach ($rawquestions as $key => $value) { // Parse input for question ids
304 if (substr($key, 0, 1) == "q") {
305 $key = substr($key,1);
306 $questionlist .= $key.',';
307 if (record_exists('quiz_question_instances', 'question', $key) or
308 record_exists('quiz_states', 'originalquestion', $key)) {
309 $questionnames .= '* ';
310 $inuse = true;
311 }
312 $questionnames .= get_field('quiz_questions', 'name', 'id', $key).'<br />';
313 }
314 }
315 if (!$questionlist) { // no questions were selected
316 redirect('edit.php');
317 }
318 $questionlist = rtrim($questionlist, ',');
319
320 // Add an explanation about questions in use
321 if ($inuse) {
322 $questionnames .= get_string('questionsinuse', 'quiz');
323 }
324 print_header_simple($streditingquestions, '',
325 "$streditingquestions");
326 notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
327 "edit.php?sesskey=$USER->sesskey&amp;deleteselected=$questionlist&amp;confirm=".md5($questionlist), "edit.php");
328 print_footer($course);
329 exit;
330 }
331 }
332
333 if (isset($_REQUEST['savechanges']) and confirm_sesskey()) {
334 /// We need to save the new ordering (if given) and the new grades
335 $oldquestions = explode(",", $modform->questions); // the questions in the old order
336 $questions = array(); // for questions in the new order
337 $rawgrades = $_POST;
338 unset($modform->grades);
339 foreach ($rawgrades as $key => $value) { // Parse input for question -> grades
340 if (substr($key, 0, 1) == "q") {
341 $key = substr($key,1);
342 $modform->grades[$key] = $value;
343 quiz_update_question_instance($modform->grades[$key], $key, $modform->instance);
344 } elseif (substr($key, 0, 1) == "o") { // Parse input for ordering info
345 $key = substr($key,1);
346 $questions[$value] = $oldquestions[$key];
347 }
348 }
349
350 // If ordering info was given, reorder the questions
351 if ($questions) {
352 ksort($questions);
353 $modform->questions = implode(",", $questions);
354 // Always have a page break at the end
355 $modform->questions = $modform->questions . ',0';
356 // Avoid duplicate page breaks
357 while (strpos($modform->questions, ',0,0')) {
358 $modform->questions = str_replace(',0,0', ',0', $modform->questions);
359 }
360 if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) {
361 error('Could not save question list');
362 }
363 }
364
365 // If rescaling is required save the new maximum
366 if (isset($_REQUEST['maxgrade'])) {
367 $modform->grade = optional_param('maxgrade', 0);
368 if (!set_field('quiz', 'grade', $modform->grade, 'id', $modform->instance)) {
369 error('Could not set new maximal grade for quiz');
370 }
371 }
372 }
373
374 if (isset($_REQUEST['cat'])) { /// coming from category selection drop-down menu
375 $modform->category = $cat;
376 $page = 0;
377 $modform->page = 0;
378 }
379
380 if(isset($_REQUEST['recurse'])) {
381 $SESSION->quiz_recurse = optional_param('recurse', 0, PARAM_BOOL);
382 }
383 if(isset($_REQUEST['showbreaks'])) {
384 $SESSION->quiz_showbreaks = optional_param('showbreaks', 0, PARAM_BOOL);
385 $SESSION->quiz_reordertool = optional_param('reordertool', 0, PARAM_BOOL);
386 }
387 if(isset($_REQUEST['showhidden'])) {
388 $SESSION->quiz_showhidden = optional_param('showhidden', 0, PARAM_BOOL);
389 }
390
391/// Delete any teacher preview attempts if the quiz has been modified
392 if (isset($_REQUEST['setgrades']) or isset($_REQUEST['delete']) or isset($_REQUEST['repaginate']) or isset($_REQUEST['addrandom']) or isset($_REQUEST['addquestion']) or isset($_REQUEST['up']) or isset($_REQUEST['down']) or isset($_REQUEST['add'])) {
393 delete_records('quiz_attempts', 'preview', '1', 'quiz', $modform->id);
394 }
395
396/// all commands have been dealt with, now print the page
397
398 if (empty($modform->category) or !record_exists('quiz_categories', 'id', $modform->category)) {
399 $category = quiz_get_default_category($course->id);
400 $modform->category = $category->id;
401 }
402 if (!isset($SESSION->quiz_recurse)) {
403 $SESSION->quiz_recurse = 1;
404 }
405 if (!isset($SESSION->quiz_showhidden)) {
406 $SESSION->quiz_showhidden = false;
407 }
408 if (!isset($SESSION->quiz_showbreaks)) {
409 $SESSION->quiz_showbreaks = ($CFG->quiz_questionsperpage < 2) ? 0 : 1;
410 }
411 if (!isset($SESSION->quiz_reordertool)) {
412 $SESSION->quiz_reordertool = 0;
413 }
414
415 $SESSION->modform = $modform;
416
417 // Print basic page layout.
418
419 if (isset($modform->instance) and record_exists_sql("SELECT * FROM {$CFG->prefix}quiz_attempts WHERE quiz = '$modform->instance' AND preview = '0' LIMIT 1")){
420 // one column layout with table of questions used in this quiz
421 $strupdatemodule = isteacheredit($course->id)
422 ? update_module_button($modform->cmid, $course->id, get_string('modulename', 'quiz'))
423 : "";
424 print_header_simple($streditingquiz, '',
425 "<a href=\"index.php?id=$course->id\">$strquizzes</a>".
426 " -> <a href=\"view.php?q=$modform->instance\">".format_string($modform->name).'</a>'.
427 " -> $streditingquiz", "", "",
428 true, $strupdatemodule);
429
430 $currenttab = 'edit';
431 $mode = 'editq';
432 $quiz = &$modform;
433 include('tabs.php');
434
435 print_simple_box_start("center");
436
437 $attemptcount = count_records('quiz_attempts', 'quiz', $modform->instance, 'preview', 0);
438
439 $strviewallanswers = get_string("viewallanswers","quiz",$attemptcount);
440 $strattemptsexist = get_string("attemptsexist","quiz");
441 $usercount = count_records_select('quiz_attempts', "quiz = '$modform->id' AND preview = '0'", 'COUNT(DISTINCT userid)');
442 $strusers = $course->students;
443 if (! $cm = get_coursemodule_from_instance("quiz", $modform->instance, $course->id)) {
444 error("Course Module ID was incorrect");
445 }
446 echo "<center>\n";
447 echo "$strattemptsexist<br /><a href=\"report.php?mode=overview&amp;id=$cm->id\">$strviewallanswers ($usercount $strusers)</a>";
448 echo "<form target=\"_parent\" method=\"get\" action=\"$CFG->wwwroot/mod/quiz/edit.php\">\n";
449 echo " <input type=\"hidden\" name=\"courseid\" value=\"$course->id\" />\n";
450 echo " <input type=\"submit\" value=\"".get_string("editcatquestions", "quiz")."\" />\n";
451 echo "</form>";
452 echo "</center><br/ >\n";
453
454 $sumgrades = quiz_print_question_list($modform, false, $SESSION->quiz_showbreaks, $SESSION->quiz_reordertool);
455 if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $modform->instance)) {
456 error('Failed to set sumgrades');
457 }
458
459 print_simple_box_end();
460 print_footer($course);
461 exit;
462 }
463
464 if (!isset($modform->instance)) {
465 // one column layout for non-quiz-specific editing page
466 print_header_simple($streditingquestions, '',
467 "$streditingquestions");
468 print_heading_with_help(get_string('questions', 'quiz'), 'questionbank');
469 echo '<table align="center" border="0" cellpadding="2" cellspacing="0">';
470 echo '<tr><td valign="top">';
471
472 } else {
473 // two column layout with quiz info in left column
474 $strupdatemodule = isteacheredit($course->id)
475 ? update_module_button($modform->cmid, $course->id, get_string('modulename', 'quiz'))
476 : "";
477 print_header_simple($streditingquiz, '',
478 "<a href=\"index.php?id=$course->id\">$strquizzes</a>".
479 " -> <a href=\"view.php?q=$modform->instance\">".format_string($modform->name).'</a>'.
480 " -> $streditingquiz",
481 "", "", true, $strupdatemodule);
482
483 $currenttab = 'edit';
484 $mode = 'editq';
485 $quiz = &$modform;
486 include('tabs.php');
487
488 echo '<table border="0" width="100%" cellpadding="2" cellspacing="0">';
489 echo '<tr><td width="50%" valign="top">';
490 print_simple_box_start("center", "100%");
491
492 $sumgrades = quiz_print_question_list($modform, true, $SESSION->quiz_showbreaks, $SESSION->quiz_reordertool);
493 if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $modform->instance)) {
494 error('Failed to set sumgrades');
495 }
496
497 print_simple_box_end();
498
499 echo '</td><td valign="top" width="50%">';
500 }
501 // non-quiz-specific column
502 print_simple_box_start("center", "100%");
503 // starts with category selection form
504 quiz_print_category_form($course, $modform->category, $SESSION->quiz_recurse, $SESSION->quiz_showhidden);
505 print_simple_box_end();
506
507 print_spacer(5,1);
508 // continues with list of questions
509 print_simple_box_start("center", "100%");
510 quiz_print_cat_question_list($course, $modform->category,
511 isset($modform->instance) ? $modform->instance : 0, $SESSION->quiz_recurse, $page, $perpage, $SESSION->quiz_showhidden, $sortorder);
512 print_simple_box_end();
513
514 echo '</td></tr>';
515 echo '</table>';
516
517
518
519 print_footer($course);
520?>