MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / quiz / edit.php
blob094b6d2aef8d174120ba42bb4cb5abd8f7fab81a
1 <?php // $Id$
2 /**
3 * Page to edit quizzes
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 * The lecturer can add questions from the right hand list to the quiz or remove them
12 * The script also processes a number of actions:
13 * Actions affecting a quiz:
14 * up and down Changes the order of questions and page breaks
15 * addquestion Adds a single question to the quiz
16 * add Adds several selected questions to the quiz
17 * addrandom Adds a certain number of random questions to the quiz
18 * repaginate Re-paginates the quiz
19 * delete Removes a question from the quiz
20 * savechanges Saves the order and grades for questions in the quiz
22 * @version $Id$
23 * @author Martin Dougiamas and many others. This has recently been extensively
24 * rewritten by Gustav Delius and other members of the Serving Mathematics project
25 * {@link http://maths.york.ac.uk/serving_maths}
26 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
27 * @package quiz
29 require_once("../../config.php");
30 require_once($CFG->dirroot.'/mod/quiz/editlib.php');
32 /**
33 * Callback function called from question_list() function (which is called from showbank())
34 * Displays action icon as first action for each question.
36 function module_specific_actions($pageurl, $questionid, $cmid, $canuse){
37 global $CFG;
38 if ($canuse){
39 $straddtoquiz = get_string("addtoquiz", "quiz");
40 $out = "<a title=\"$straddtoquiz\" href=\"edit.php?".$pageurl->get_query_string()."&amp;addquestion=$questionid&amp;sesskey=".sesskey()."\"><img
41 src=\"$CFG->pixpath/t/moveleft.gif\" alt=\"$straddtoquiz\" /></a>&nbsp;";
42 return $out;
43 } else {
44 return '';
47 /**
48 * Callback function called from question_list() function (which is called from showbank())
49 * Displays button in form with checkboxes for each question.
51 function module_specific_buttons($cmid){
52 global $THEME;
53 $straddtoquiz = get_string("addtoquiz", "quiz");
54 $out = "<input type=\"submit\" name=\"add\" value=\"{$THEME->larrow} $straddtoquiz\" />\n";
55 return $out;
59 /**
60 * Callback function called from question_list() function (which is called from showbank())
62 function module_specific_controls($totalnumber, $recurse, $category, $cmid){
63 $catcontext = get_context_instance_by_id($category->contextid);
64 if (has_capability('moodle/question:useall', $catcontext)){
65 for ($i = 1;$i <= min(10, $totalnumber); $i++) {
66 $randomcount[$i] = $i;
68 for ($i = 20;$i <= min(100, $totalnumber); $i += 10) {
69 $randomcount[$i] = $i;
71 $out = '<br />';
72 $out .= get_string('addrandom', 'quiz', choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
73 $out .= '<input type="hidden" name="recurse" value="'.$recurse.'" />';
74 $out .= "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
75 $out .= ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
76 $out .= helpbutton('random', get_string('random', 'quiz'), 'quiz', true, false, '', true);
77 } else {
78 $out = '';
80 return $out;
83 list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) = question_edit_setup('editq', true);
85 //these params are only passed from page request to request while we stay on this page
86 //otherwise they would go in question_edit_setup
87 $quiz_showbreaks = optional_param('showbreaks', -1, PARAM_BOOL);
88 $quiz_reordertool = optional_param('reordertool', 0, PARAM_BOOL);
89 if ($quiz_showbreaks > -1) {
90 $thispageurl->param('showbreaks', $quiz_showbreaks);
91 } else {
92 $quiz_showbreaks = ($CFG->quiz_questionsperpage < 2) ? 0 : 1;
94 if ($quiz_reordertool != 0) {
95 $thispageurl->param('reordertool', $quiz_reordertool);
98 $strquizzes = get_string('modulenameplural', 'quiz');
99 $strquiz = get_string('modulename', 'quiz');
100 $streditingquestions = get_string('editquestions', "quiz");
101 $streditingquiz = get_string('editinga', 'moodle', $strquiz);
106 // Get the course object and related bits.
107 if (! $course = get_record("course", "id", $quiz->course)) {
108 error("This course doesn't exist");
112 // Log this visit.
113 add_to_log($cm->course, 'quiz', 'editquestions',
114 "view.php?id=$cm->id", "$quiz->id", $cm->id);
116 //you need mod/quiz:manage in addition to question capabilities to access this page.
117 require_capability('mod/quiz:manage', $contexts->lowest());
119 if (isset($quiz->instance)
120 && empty($quiz->grades)){ // Construct an array to hold all the grades.
121 $quiz->grades = quiz_get_all_question_grades($quiz);
125 /// Now, check for commands on this page and modify variables as necessary
127 if (isset($_REQUEST['up']) and confirm_sesskey()) { /// Move the given question up a slot
128 $up = optional_param('up', 0, PARAM_INT);
129 $questions = explode(",", $quiz->questions);
130 if ($up > 0 and isset($questions[$up])) {
131 $prevkey = ($questions[$up-1] == 0) ? $up-2 : $up-1;
132 $swap = $questions[$prevkey];
133 $questions[$prevkey] = $questions[$up];
134 $questions[$up] = $swap;
135 $quiz->questions = implode(",", $questions);
136 // Always have a page break at the end
137 $quiz->questions = $quiz->questions . ',0';
138 // Avoid duplicate page breaks
139 $quiz->questions = str_replace(',0,0', ',0', $quiz->questions);
140 if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->instance)) {
141 error('Could not save question list');
146 if (isset($_REQUEST['down']) and confirm_sesskey()) { /// Move the given question down a slot
147 $down = optional_param('down', 0, PARAM_INT);
148 $questions = explode(",", $quiz->questions);
149 if ($down < count($questions)) {
150 $nextkey = ($questions[$down+1] == 0) ? $down+2 : $down+1;
151 $swap = $questions[$nextkey];
152 $questions[$nextkey] = $questions[$down];
153 $questions[$down] = $swap;
154 $quiz->questions = implode(",", $questions);
155 // Avoid duplicate page breaks
156 $quiz->questions = str_replace(',0,0', ',0', $quiz->questions);
157 if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->instance)) {
158 error('Could not save question list');
163 if (isset($_REQUEST['addquestion']) and confirm_sesskey()) { /// Add a single question to the current quiz
164 quiz_add_quiz_question($_REQUEST['addquestion'], $quiz);
167 if (isset($_REQUEST['add']) and confirm_sesskey()) { /// Add selected questions to the current quiz
168 foreach ($_POST as $key => $value) { // Parse input for question ids
169 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
170 $key = $matches[1];
171 quiz_add_quiz_question($key, $quiz);
176 if (isset($_REQUEST['addrandom']) and confirm_sesskey()) { /// Add random questions to the quiz
177 $recurse = optional_param('recurse', 0, PARAM_BOOL);
178 $categoryid = required_param('categoryid', PARAM_INT);
179 $randomcount = required_param('randomcount', PARAM_INT);
180 // load category
181 if (! $category = get_record('question_categories', 'id', $categoryid)) {
182 error('Category ID is incorrect');
184 $catcontext = get_context_instance_by_id($category->contextid);
185 require_capability('moodle/question:useall', $catcontext);
186 $category->name = addslashes($category->name);
187 // Find existing random questions in this category that are not used by any quiz.
188 if ($existingquestions = get_records_sql(
189 "SELECT * FROM " . $CFG->prefix . "question q
190 WHERE qtype = '" . RANDOM . "'
191 AND category = $category->id
192 AND " . sql_compare_text('questiontext') . " = '$recurse'
193 AND NOT EXISTS (SELECT * FROM " . $CFG->prefix . "quiz_question_instances WHERE question = q.id)
194 ORDER BY id")) {
195 // Take as many of these as needed.
196 while (($existingquestion = array_shift($existingquestions)) and $randomcount > 0) {
197 quiz_add_quiz_question($existingquestion->id, $quiz);
198 $randomcount--;
202 // If more are needed, create them.
203 if ($randomcount > 0) {
204 $form->questiontext = $recurse; // we use the questiontext field to store the info
205 // on whether to include questions in subcategories
206 $form->questiontextformat = 0;
207 $form->image = '';
208 $form->defaultgrade = 1;
209 $form->hidden = 1;
210 for ($i = 0; $i < $randomcount; $i++) {
211 $form->category = "$category->id,$category->contextid";
212 $form->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
213 $question = new stdClass;
214 $question->qtype = RANDOM;
215 $question = $QTYPES[RANDOM]->save_question($question, $form, $course);
216 if(!isset($question->id)) {
217 error('Could not insert new random question!');
219 quiz_add_quiz_question($question->id, $quiz);
224 if (isset($_REQUEST['repaginate']) and confirm_sesskey()) { /// Re-paginate the quiz
225 if (isset($_REQUEST['questionsperpage'])) {
226 $quiz->questionsperpage = required_param('questionsperpage', PARAM_INT);
227 if (!set_field('quiz', 'questionsperpage', $quiz->questionsperpage, 'id', $quiz->id)) {
228 error('Could not save number of questions per page');
231 $quiz->questions = quiz_repaginate($quiz->questions, $quiz->questionsperpage);
232 if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->id)) {
233 error('Could not save layout');
236 if (isset($_REQUEST['delete']) and confirm_sesskey()) { /// Remove a question from the quiz
237 quiz_delete_quiz_question($_REQUEST['delete'], $quiz);
240 if (isset($_REQUEST['savechanges']) and confirm_sesskey()) {
241 /// We need to save the new ordering (if given) and the new grades
242 $oldquestions = explode(",", $quiz->questions); // the questions in the old order
243 $questions = array(); // for questions in the new order
244 $rawgrades = $_POST;
245 unset($quiz->grades);
246 foreach ($rawgrades as $key => $value) { // Parse input for question -> grades
247 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
248 $key = $matches[1];
249 $quiz->grades[$key] = $value;
250 quiz_update_question_instance($quiz->grades[$key], $key, $quiz->instance);
251 } elseif (preg_match('!^q([0-9]+)$!', $key, $matches)) { // Parse input for ordering info
252 $key = $matches[1];
253 $questions[$value] = $oldquestions[$key];
257 // If ordering info was given, reorder the questions
258 if ($questions) {
259 ksort($questions);
260 $quiz->questions = implode(",", $questions);
261 // Always have a page break at the end
262 $quiz->questions = $quiz->questions . ',0';
263 // Avoid duplicate page breaks
264 while (strpos($quiz->questions, ',0,0')) {
265 $quiz->questions = str_replace(',0,0', ',0', $quiz->questions);
267 if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->instance)) {
268 error('Could not save question list');
272 // If rescaling is required save the new maximum
273 if (isset($_REQUEST['maxgrade'])) {
274 if (!quiz_set_grade(optional_param('maxgrade', 0), $quiz)) {
275 error('Could not set a new maximum grade for the quiz');
280 /// Delete any teacher preview attempts if the quiz has been modified
281 if (isset($_REQUEST['savechanges']) 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'])) {
282 delete_records('quiz_attempts', 'preview', '1', 'quiz', $quiz->id);
285 question_showbank_actions($thispageurl, $cm);
287 /// all commands have been dealt with, now print the page
289 // Print basic page layout.
291 if (isset($quiz->instance) and record_exists_select('quiz_attempts', "quiz = '$quiz->instance' AND preview = '0'")){
292 // one column layout with table of questions used in this quiz
293 $strupdatemodule = has_capability('moodle/course:manageactivities', $contexts->lowest())
294 ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
295 : "";
296 $navlinks = array();
297 $navlinks[] = array('name' => $strquizzes, 'link' => "index.php?id=$course->id", 'type' => 'activity');
298 $navlinks[] = array('name' => format_string($quiz->name), 'link' => "view.php?q=$quiz->instance", 'type' => 'activityinstance');
299 $navlinks[] = array('name' => $streditingquiz, 'link' => '', 'type' => 'title');
300 $navigation = build_navigation($navlinks);
302 print_header_simple($streditingquiz, '', $navigation, "", "",
303 true, $strupdatemodule);
305 $currenttab = 'edit';
306 $mode = 'editq';
308 include('tabs.php');
310 print_box_start();
312 $a->attemptnum = count_records('quiz_attempts', 'quiz', $quiz->id, 'preview', 0);
313 $a->studentnum = count_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = '0'", 'COUNT(DISTINCT userid)');
314 $a->studentstring = $course->students;
316 echo "<div class=\"attemptsnotice\">\n";
317 echo "<a href=\"report.php?mode=overview&amp;id=$cm->id\">".get_string('numattempts', 'quiz', $a)."</a><br />".get_string("attemptsexist","quiz");
318 echo "</div><br />\n";
320 $sumgrades = quiz_print_question_list($quiz, $thispageurl, false, $quiz_showbreaks, $quiz_reordertool);
321 if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $quiz->instance)) {
322 error('Failed to set sumgrades');
325 print_box_end();
326 print_footer($course);
327 exit;
330 // two column layout with quiz info in left column
331 $strupdatemodule = has_capability('moodle/course:manageactivities', $contexts->lowest())
332 ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
333 : "";
334 $navlinks = array();
335 $navlinks[] = array('name' => $strquizzes, 'link' => "index.php?id=$course->id", 'type' => 'activity');
336 $navlinks[] = array('name' => format_string($quiz->name), 'link' => "view.php?q=$quiz->instance", 'type' => 'activityinstance');
337 $navlinks[] = array('name' => $streditingquiz, 'link' => '', 'type' => 'title');
338 $navigation = build_navigation($navlinks);
340 print_header_simple($streditingquiz, '', $navigation, "", "", true, $strupdatemodule);
342 $currenttab = 'edit';
343 $mode = 'editq';
345 include('tabs.php');
347 echo '<table border="0" style="width:100%" cellpadding="2" cellspacing="0">';
348 echo '<tr><td style="width:50%" valign="top">';
349 print_box_start('generalbox quizquestions');
350 print_heading(get_string('questionsinthisquiz', 'quiz'), '', 2);
352 $sumgrades = quiz_print_question_list($quiz, $thispageurl, true, $quiz_showbreaks, $quiz_reordertool);
353 if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $quiz->instance)) {
354 error('Failed to set sumgrades');
357 print_box_end();
359 echo '</td><td style="width:50%" valign="top">';
361 question_showbank('editq', $contexts, $thispageurl, $cm, $pagevars['qpage'], $pagevars['qperpage'], $pagevars['qsortorder'], $pagevars['qsortorderdecoded'],
362 $pagevars['cat'], $pagevars['recurse'], $pagevars['showhidden'], $pagevars['showquestiontext']);
364 echo '</td></tr>';
365 echo '</table>';
367 print_footer($course);