MDL-11278 Implementation is complete, but grade_categories records in DB are not...
[moodle-pu.git] / mod / quiz / edit.php
blobc798a99b90b483d5dc2b916287e0964ceb4ea94c
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
188 $random = RANDOM;
189 if ($existingquestions = get_records_select('question', "qtype = '$random' AND category = '$category->id'")) {
190 // now remove the ones that are already used in this quiz
191 if ($questionids = explode(',', $quiz->questions)) {
192 foreach ($questionids as $questionid) {
193 unset($existingquestions[$questionid]);
196 // now take as many of these as needed
197 $i = 0;
198 while (($existingquestion = array_pop($existingquestions)) and ($i < $randomcount)) {
199 if ($existingquestion->questiontext == $recurse) {
200 // this question has the right recurse property, so use it
201 quiz_add_quiz_question($existingquestion->id, $quiz);
202 $i++;
205 $randomcreate = $randomcount - $i; // the number of additional random questions needed.
206 } else {
207 $randomcreate = $randomcount;
210 if ($randomcreate > 0) {
212 $form->name = get_string('random', 'quiz') .' ('. $category->name .')';
213 $form->category = "$category->id,$category->contextid";
214 $form->questiontext = $recurse; // we use the questiontext field to store the info
215 // on whether to include questions in subcategories
216 $form->questiontextformat = 0;
217 $form->image = '';
218 $form->defaultgrade = 1;
219 $form->hidden = 1;
220 for ($i=0; $i<$randomcreate; $i++) {
221 $form->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
222 $question = new stdClass;
223 $question->qtype = RANDOM;
224 $question = $QTYPES[RANDOM]->save_question($question, $form, $course);
225 if(!isset($question->id)) {
226 error('Could not insert new random question!');
228 quiz_add_quiz_question($question->id, $quiz);
233 if (isset($_REQUEST['repaginate']) and confirm_sesskey()) { /// Re-paginate the quiz
234 if (isset($_REQUEST['questionsperpage'])) {
235 $quiz->questionsperpage = required_param('questionsperpage', PARAM_INT);
236 if (!set_field('quiz', 'questionsperpage', $quiz->questionsperpage, 'id', $quiz->id)) {
237 error('Could not save number of questions per page');
240 $quiz->questions = quiz_repaginate($quiz->questions, $quiz->questionsperpage);
241 if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->id)) {
242 error('Could not save layout');
245 if (isset($_REQUEST['delete']) and confirm_sesskey()) { /// Remove a question from the quiz
246 quiz_delete_quiz_question($_REQUEST['delete'], $quiz);
249 if (isset($_REQUEST['savechanges']) and confirm_sesskey()) {
250 /// We need to save the new ordering (if given) and the new grades
251 $oldquestions = explode(",", $quiz->questions); // the questions in the old order
252 $questions = array(); // for questions in the new order
253 $rawgrades = $_POST;
254 unset($quiz->grades);
255 foreach ($rawgrades as $key => $value) { // Parse input for question -> grades
256 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
257 $key = $matches[1];
258 $quiz->grades[$key] = $value;
259 quiz_update_question_instance($quiz->grades[$key], $key, $quiz->instance);
260 } elseif (preg_match('!^q([0-9]+)$!', $key, $matches)) { // Parse input for ordering info
261 $key = $matches[1];
262 $questions[$value] = $oldquestions[$key];
266 // If ordering info was given, reorder the questions
267 if ($questions) {
268 ksort($questions);
269 $quiz->questions = implode(",", $questions);
270 // Always have a page break at the end
271 $quiz->questions = $quiz->questions . ',0';
272 // Avoid duplicate page breaks
273 while (strpos($quiz->questions, ',0,0')) {
274 $quiz->questions = str_replace(',0,0', ',0', $quiz->questions);
276 if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->instance)) {
277 error('Could not save question list');
281 // If rescaling is required save the new maximum
282 if (isset($_REQUEST['maxgrade'])) {
283 if (!quiz_set_grade(optional_param('maxgrade', 0), $quiz)) {
284 error('Could not set a new maximum grade for the quiz');
289 /// Delete any teacher preview attempts if the quiz has been modified
290 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'])) {
291 delete_records('quiz_attempts', 'preview', '1', 'quiz', $quiz->id);
294 question_showbank_actions($thispageurl, $cm);
296 /// all commands have been dealt with, now print the page
298 // Print basic page layout.
300 if (isset($quiz->instance) and record_exists_select('quiz_attempts', "quiz = '$quiz->instance' AND preview = '0'")){
301 // one column layout with table of questions used in this quiz
302 $strupdatemodule = has_capability('moodle/course:manageactivities', $contexts->lowest())
303 ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
304 : "";
305 $navlinks = array();
306 $navlinks[] = array('name' => $strquizzes, 'link' => "index.php?id=$course->id", 'type' => 'activity');
307 $navlinks[] = array('name' => format_string($quiz->name), 'link' => "view.php?q=$quiz->instance", 'type' => 'activityinstance');
308 $navlinks[] = array('name' => $streditingquiz, 'link' => '', 'type' => 'title');
309 $navigation = build_navigation($navlinks);
311 print_header_simple($streditingquiz, '', $navigation, "", "",
312 true, $strupdatemodule);
314 $currenttab = 'edit';
315 $mode = 'editq';
317 include('tabs.php');
319 print_box_start();
321 $a->attemptnum = count_records('quiz_attempts', 'quiz', $quiz->id, 'preview', 0);
322 $a->studentnum = count_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = '0'", 'COUNT(DISTINCT userid)');
323 $a->studentstring = $course->students;
325 echo "<div class=\"attemptsnotice\">\n";
326 echo "<a href=\"report.php?mode=overview&amp;id=$cm->id\">".get_string('numattempts', 'quiz', $a)."</a><br />".get_string("attemptsexist","quiz");
327 echo "</div><br />\n";
329 $sumgrades = quiz_print_question_list($quiz, $thispageurl, false, $quiz_showbreaks, $quiz_reordertool);
330 if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $quiz->instance)) {
331 error('Failed to set sumgrades');
334 print_box_end();
335 print_footer($course);
336 exit;
339 // two column layout with quiz info in left column
340 $strupdatemodule = has_capability('moodle/course:manageactivities', $contexts->lowest())
341 ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
342 : "";
343 $navlinks = array();
344 $navlinks[] = array('name' => $strquizzes, 'link' => "index.php?id=$course->id", 'type' => 'activity');
345 $navlinks[] = array('name' => format_string($quiz->name), 'link' => "view.php?q=$quiz->instance", 'type' => 'activityinstance');
346 $navlinks[] = array('name' => $streditingquiz, 'link' => '', 'type' => 'title');
347 $navigation = build_navigation($navlinks);
349 print_header_simple($streditingquiz, '', $navigation, "", "", true, $strupdatemodule);
351 $currenttab = 'edit';
352 $mode = 'editq';
354 include('tabs.php');
356 echo '<table border="0" style="width:100%" cellpadding="2" cellspacing="0">';
357 echo '<tr><td style="width:50%" valign="top">';
358 print_box_start('generalbox quizquestions');
359 print_heading(get_string('questionsinthisquiz', 'quiz'), '', 2);
361 $sumgrades = quiz_print_question_list($quiz, $thispageurl, true, $quiz_showbreaks, $quiz_reordertool);
362 if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $quiz->instance)) {
363 error('Failed to set sumgrades');
366 print_box_end();
368 echo '</td><td style="width:50%" valign="top">';
370 question_showbank('editq', $contexts, $thispageurl, $cm, $pagevars['qpage'], $pagevars['qperpage'], $pagevars['qsortorder'], $pagevars['qsortorderdecoded'],
371 $pagevars['cat'], $pagevars['recurse'], $pagevars['showhidden'], $pagevars['showquestiontext']);
373 echo '</td></tr>';
374 echo '</table>';
376 print_footer($course);