2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Page to edit quizzes
21 * This page generally has two columns:
22 * The right column lists all available questions in a chosen category and
23 * allows them to be edited or more to be added. This column is only there if
24 * the quiz does not already have student attempts
25 * The left column lists all questions that have been added to the current quiz.
26 * The lecturer can add questions from the right hand list to the quiz or remove them
28 * The script also processes a number of actions:
29 * Actions affecting a quiz:
30 * up and down Changes the order of questions and page breaks
31 * addquestion Adds a single question to the quiz
32 * add Adds several selected questions to the quiz
33 * addrandom Adds a certain number of random questions to the quiz
34 * repaginate Re-paginates the quiz
35 * delete Removes a question from the quiz
36 * savechanges Saves the order and grades for questions in the quiz
39 * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 require_once('../../config.php');
45 require_once($CFG->dirroot . '/mod/quiz/editlib.php');
46 require_once($CFG->dirroot . '/mod/quiz/addrandomform.php');
47 require_once($CFG->dirroot . '/question/category_class.php');
51 * Callback function called from question_list() function
52 * (which is called from showbank())
53 * Displays button in form with checkboxes for each question.
55 function module_specific_buttons($cmid, $cmoptions) {
60 'value' => $OUTPUT->larrow() . ' ' . get_string('addtoquiz', 'quiz'),
62 if ($cmoptions->hasattempts) {
63 $params['disabled'] = 'disabled';
65 return html_writer::empty_tag('input', $params);
69 * Callback function called from question_list() function
70 * (which is called from showbank())
72 function module_specific_controls($totalnumber, $recurse, $category, $cmid, $cmoptions) {
75 $catcontext = context::instance_by_id($category->contextid);
76 if (has_capability('moodle/question:useall', $catcontext)) {
77 if ($cmoptions->hasattempts) {
78 $disabled = ' disabled="disabled"';
82 $randomusablequestions =
83 question_bank::get_qtype('random')->get_available_questions_from_category(
84 $category->id, $recurse);
85 $maxrand = count($randomusablequestions);
87 for ($i = 1; $i <= min(10, $maxrand); $i++) {
88 $randomcount[$i] = $i;
90 for ($i = 20; $i <= min(100, $maxrand); $i += 10) {
91 $randomcount[$i] = $i;
95 $disabled = ' disabled="disabled"';
98 $out = '<strong><label for="menurandomcount">'.get_string('addrandomfromcategory', 'quiz').
99 '</label></strong><br />';
100 $attributes = array();
101 $attributes['disabled'] = $disabled ? 'disabled' : null;
102 $select = html_writer::select($randomcount, 'randomcount', '1', null, $attributes);
103 $out .= get_string('addrandom', 'quiz', $select);
104 $out .= '<input type="hidden" name="recurse" value="'.$recurse.'" />';
105 $out .= '<input type="hidden" name="categoryid" value="' . $category->id . '" />';
106 $out .= ' <input type="submit" name="addrandom" value="'.
107 get_string('addtoquiz', 'quiz').'"' . $disabled . ' />';
108 $out .= $OUTPUT->help_icon('addarandomquestion', 'quiz');
113 // These params are only passed from page request to request while we stay on
114 // this page otherwise they would go in question_edit_setup.
115 $quiz_reordertool = optional_param('reordertool', -1, PARAM_BOOL);
116 $quiz_qbanktool = optional_param('qbanktool', -1, PARAM_BOOL);
117 $scrollpos = optional_param('scrollpos', '', PARAM_INT);
119 list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) =
120 question_edit_setup('editq', '/mod/quiz/edit.php', true);
122 $defaultcategoryobj = question_make_default_categories($contexts->all());
123 $defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid;
125 if ($quiz_qbanktool > -1) {
126 $thispageurl->param('qbanktool', $quiz_qbanktool);
127 set_user_preference('quiz_qbanktool_open', $quiz_qbanktool);
129 $quiz_qbanktool = get_user_preferences('quiz_qbanktool_open', 0);
132 if ($quiz_reordertool > -1) {
133 $thispageurl->param('reordertool', $quiz_reordertool);
134 set_user_preference('quiz_reordertab', $quiz_reordertool);
136 $quiz_reordertool = get_user_preferences('quiz_reordertab', 0);
139 $canaddrandom = $contexts->have_cap('moodle/question:useall');
140 $canaddquestion = (bool) $contexts->having_add_and_use();
142 $quizhasattempts = quiz_has_attempts($quiz->id);
144 $PAGE->set_url($thispageurl);
146 // Get the course object and related bits.
147 $course = $DB->get_record('course', array('id' => $quiz->course));
149 print_error('invalidcourseid', 'error');
152 $questionbank = new quiz_question_bank_view($contexts, $thispageurl, $course, $cm, $quiz);
153 $questionbank->set_quiz_has_attempts($quizhasattempts);
156 add_to_log($cm->course, 'quiz', 'editquestions',
157 "view.php?id=$cm->id", "$quiz->id", $cm->id);
159 // You need mod/quiz:manage in addition to question capabilities to access this page.
160 require_capability('mod/quiz:manage', $contexts->lowest());
162 // Process commands ============================================================
164 // Get the list of question ids had their check-boxes ticked.
165 $selectedslots = array();
166 $params = (array) data_submitted();
167 foreach ($params as $key => $value) {
168 if (preg_match('!^s([0-9]+)$!', $key, $matches)) {
169 $selectedslots[] = $matches[1];
173 $afteractionurl = new moodle_url($thispageurl);
175 $afteractionurl->param('scrollpos', $scrollpos);
177 if (($up = optional_param('up', false, PARAM_INT)) && confirm_sesskey()) {
178 quiz_move_question_up($quiz, $up);
179 quiz_delete_previews($quiz);
180 redirect($afteractionurl);
183 if (($down = optional_param('down', false, PARAM_INT)) && confirm_sesskey()) {
184 quiz_move_question_down($quiz, $down);
185 quiz_delete_previews($quiz);
186 redirect($afteractionurl);
189 if (optional_param('repaginate', false, PARAM_BOOL) && confirm_sesskey()) {
190 // Re-paginate the quiz.
191 $questionsperpage = optional_param('questionsperpage', $quiz->questionsperpage, PARAM_INT);
192 quiz_repaginate_questions($quiz->id, $questionsperpage );
193 quiz_delete_previews($quiz);
194 redirect($afteractionurl);
197 if (($addquestion = optional_param('addquestion', 0, PARAM_INT)) && confirm_sesskey()) {
198 // Add a single question to the current quiz.
199 quiz_require_question_use($addquestion);
200 $addonpage = optional_param('addonpage', 0, PARAM_INT);
201 quiz_add_quiz_question($addquestion, $quiz, $addonpage);
202 quiz_delete_previews($quiz);
203 quiz_update_sumgrades($quiz);
204 $thispageurl->param('lastchanged', $addquestion);
205 redirect($afteractionurl);
208 if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
209 // Add selected questions to the current quiz.
210 $rawdata = (array) data_submitted();
211 foreach ($rawdata as $key => $value) { // Parse input for question ids.
212 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
214 quiz_require_question_use($key);
215 quiz_add_quiz_question($key, $quiz);
218 quiz_delete_previews($quiz);
219 quiz_update_sumgrades($quiz);
220 redirect($afteractionurl);
223 if ((optional_param('addrandom', false, PARAM_BOOL)) && confirm_sesskey()) {
224 // Add random questions to the quiz.
225 $recurse = optional_param('recurse', 0, PARAM_BOOL);
226 $addonpage = optional_param('addonpage', 0, PARAM_INT);
227 $categoryid = required_param('categoryid', PARAM_INT);
228 $randomcount = required_param('randomcount', PARAM_INT);
229 quiz_add_random_questions($quiz, $addonpage, $categoryid, $randomcount, $recurse);
231 quiz_delete_previews($quiz);
232 quiz_update_sumgrades($quiz);
233 redirect($afteractionurl);
236 if (optional_param('addnewpagesafterselected', null, PARAM_CLEAN) &&
237 !empty($selectedslots) && confirm_sesskey()) {
238 foreach ($selectedslots as $slot) {
239 quiz_add_page_break_after_slot($quiz, $slot);
241 quiz_delete_previews($quiz);
242 redirect($afteractionurl);
245 $addpage = optional_param('addpage', false, PARAM_INT);
246 if ($addpage !== false && confirm_sesskey()) {
247 quiz_add_page_break_after_slot($quiz, $addpage);
248 quiz_delete_previews($quiz);
249 redirect($afteractionurl);
252 $deleteemptypage = optional_param('deleteemptypage', false, PARAM_INT);
253 if (($deleteemptypage !== false) && confirm_sesskey()) {
254 quiz_delete_empty_page($quiz, $deleteemptypage);
255 quiz_delete_previews($quiz);
256 redirect($afteractionurl);
259 $remove = optional_param('remove', false, PARAM_INT);
260 if ($remove && confirm_sesskey() && quiz_has_question_use($quiz, $remove)) {
261 // Remove a question from the quiz.
262 // We require the user to have the 'use' capability on the question,
263 // so that then can add it back if they remove the wrong one by mistake,
264 // but, if the question is missing, it can always be removed.
265 quiz_remove_slot($quiz, $remove);
266 quiz_delete_previews($quiz);
267 quiz_update_sumgrades($quiz);
268 redirect($afteractionurl);
271 if (optional_param('quizdeleteselected', false, PARAM_BOOL) &&
272 !empty($selectedslots) && confirm_sesskey()) {
273 // Work backwards, since removing a question renumbers following slots.
274 foreach (array_reverse($selectedslots) as $slot) {
275 if (quiz_has_question_use($quiz, $slot)) {
276 quiz_remove_slot($quiz, $slot);
279 quiz_delete_previews($quiz);
280 quiz_update_sumgrades($quiz);
281 redirect($afteractionurl);
284 if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
285 $deletepreviews = false;
286 $recomputesummarks = false;
288 $rawdata = (array) data_submitted();
289 $moveonpagequestions = array();
290 $moveselectedonpage = optional_param('moveselectedonpagetop', 0, PARAM_INT);
291 if (!$moveselectedonpage) {
292 $moveselectedonpage = optional_param('moveselectedonpagebottom', 0, PARAM_INT);
295 $newslotorder = array();
296 foreach ($rawdata as $key => $value) {
297 if (preg_match('!^g([0-9]+)$!', $key, $matches)) {
298 // Parse input for question -> grades.
299 $slotnumber = $matches[1];
300 $newgrade = unformat_float($value);
301 quiz_update_slot_maxmark($DB->get_record('quiz_slots',
302 array('quizid' => $quiz->id, 'slot' => $slotnumber), '*', MUST_EXIST), $newgrade);
303 $deletepreviews = true;
304 $recomputesummarks = true;
306 } else if (preg_match('!^o(pg)?([0-9]+)$!', $key, $matches)) {
307 // Parse input for ordering info.
308 $slotnumber = $matches[2];
309 // Make sure two questions don't overwrite each other. If we get a second
310 // question with the same position, shift the second one along to the next gap.
311 $value = clean_param($value, PARAM_INT);
312 while (array_key_exists($value, $newslotorder)) {
316 // This is a page-break entry.
317 $newslotorder[$value] = 0;
319 $newslotorder[$value] = $slotnumber;
321 $deletepreviews = true;
325 if ($moveselectedonpage) {
327 // Make up a $newslotorder, then let the next if statement do the work.
328 $oldslots = $DB->get_records('quiz_slots', array('quizid' => $quiz->id), 'slot');
330 $beforepage = array();
332 $afterpage = array();
333 foreach ($oldslots as $oldslot) {
334 if (in_array($oldslot->slot, $selectedslots)) {
335 $onpage[] = $oldslot;
336 } else if ($oldslot->page <= $moveselectedonpage) {
337 $beforepage[] = $oldslot;
339 $afterpage[] = $oldslot;
343 $newslotorder = array();
346 foreach ($beforepage as $slot) {
347 while ($currentpage < $slot->page) {
348 $newslotorder[$index] = 0;
352 $newslotorder[$index] = $slot->slot;
356 while ($currentpage < $moveselectedonpage) {
357 $newslotorder[$index] = 0;
361 foreach ($onpage as $slot) {
362 $newslotorder[$index] = $slot->slot;
366 foreach ($afterpage as $slot) {
367 while ($currentpage < $slot->page) {
368 $newslotorder[$index] = 0;
372 $newslotorder[$index] = $slot->slot;
377 // If ordering info was given, reorder the questions.
379 ksort($newslotorder);
382 $slotreorder = array();
383 $slotpages = array();
384 foreach ($newslotorder as $slotnumber) {
385 if ($slotnumber == 0) {
389 $slotreorder[$slotnumber] = $currentslot;
390 $slotpages[$currentslot] = $currentpage;
393 $trans = $DB->start_delegated_transaction();
394 update_field_with_unique_index('quiz_slots',
395 'slot', $slotreorder, array('quizid' => $quiz->id));
396 foreach ($slotpages as $slotnumber => $page) {
397 $DB->set_field('quiz_slots', 'page', $page, array('quizid' => $quiz->id, 'slot' => $slotnumber));
399 $trans->allow_commit();
400 $deletepreviews = true;
403 // If rescaling is required save the new maximum.
404 $maxgrade = unformat_float(optional_param('maxgrade', -1, PARAM_RAW));
405 if ($maxgrade >= 0) {
406 quiz_set_grade($maxgrade, $quiz);
409 if ($deletepreviews) {
410 quiz_delete_previews($quiz);
412 if ($recomputesummarks) {
413 quiz_update_sumgrades($quiz);
414 quiz_update_all_attempt_sumgrades($quiz);
415 quiz_update_all_final_grades($quiz);
416 quiz_update_grades($quiz, 0, true);
418 redirect($afteractionurl);
421 $questionbank->process_actions($thispageurl, $cm);
423 // End of process commands =====================================================
425 $PAGE->requires->skip_link_to('questionbank',
426 get_string('skipto', 'access', get_string('questionbank', 'question')));
427 $PAGE->requires->skip_link_to('quizcontentsblock',
428 get_string('skipto', 'access', get_string('questionsinthisquiz', 'quiz')));
429 $PAGE->set_title(get_string('editingquizx', 'quiz', format_string($quiz->name)));
430 $PAGE->set_heading($course->fullname);
431 $node = $PAGE->settingsnav->find('mod_quiz_edit', navigation_node::TYPE_SETTING);
433 $node->make_active();
435 echo $OUTPUT->header();
437 // Initialise the JavaScript.
438 $quizeditconfig = new stdClass();
439 $quizeditconfig->url = $thispageurl->out(true, array('qbanktool' => '0'));
440 $quizeditconfig->dialoglisteners = array();
441 $numberoflisteners = $DB->get_field_sql("
442 SELECT COALESCE(MAX(page), 1)
444 WHERE quizid = ?", array($quiz->id));
446 for ($pageiter = 1; $pageiter <= $numberoflisteners; $pageiter++) {
447 $quizeditconfig->dialoglisteners[] = 'addrandomdialoglaunch_' . $pageiter;
449 $PAGE->requires->data_for_js('quiz_edit_config', $quizeditconfig);
450 $PAGE->requires->js('/question/qengine.js');
452 'name' => 'mod_quiz_edit',
453 'fullpath' => '/mod/quiz/edit.js',
454 'requires' => array('yui2-dom', 'yui2-event', 'yui2-container'),
455 'strings' => array(),
458 $PAGE->requires->js_init_call('quiz_edit_init', null, false, $module);
460 // Print the tabs to switch mode.
461 if ($quiz_reordertool) {
462 $currenttab = 'reorder';
464 $currenttab = 'edit';
467 new tabobject('edit', new moodle_url($thispageurl,
468 array('reordertool' => 0)), get_string('editingquiz', 'quiz')),
469 new tabobject('reorder', new moodle_url($thispageurl,
470 array('reordertool' => 1)), get_string('orderingquiz', 'quiz')),
472 print_tabs($tabs, $currenttab);
474 if ($quiz_qbanktool) {
476 $quizcontentsclass = '';
478 $bankclass = 'collapsed ';
479 $quizcontentsclass = 'quizwhenbankcollapsed';
482 echo '<div class="questionbankwindow ' . $bankclass . 'block">';
483 echo '<div class="header"><div class="title"><h2>';
484 echo get_string('questionbankcontents', 'quiz') .
485 ' [<a href="' . $thispageurl->out(true, array('qbanktool' => '1')) .
486 '" id="showbankcmd">' . get_string('show').
487 '</a><a href="' . $thispageurl->out(true, array('qbanktool' => '0')) .
488 '" id="hidebankcmd">' . get_string('hide').
490 echo '</h2></div></div><div class="content">';
492 echo '<span id="questionbank"></span>';
493 echo '<div class="container">';
494 echo '<div id="module" class="module">';
495 echo '<div class="bd">';
496 $questionbank->display('editq',
498 $pagevars['qperpage'],
499 $pagevars['cat'], $pagevars['recurse'], $pagevars['showhidden'],
500 $pagevars['qbshowtext']);
507 echo '<div class="quizcontents ' . $quizcontentsclass . '" id="quizcontentsblock">';
508 if ($quiz->shufflequestions) {
509 $repaginatingdisabledhtml = 'disabled="disabled"';
510 $repaginatingdisabled = true;
512 $repaginatingdisabledhtml = '';
513 $repaginatingdisabled = false;
515 if ($quiz_reordertool) {
516 echo '<div class="repaginatecommand"><button id="repaginatecommand" ' .
517 $repaginatingdisabledhtml.'>'.
518 get_string('repaginatecommand', 'quiz').'...</button>';
522 if ($quiz_reordertool) {
523 echo $OUTPUT->heading_with_help(get_string('orderingquizx', 'quiz', format_string($quiz->name)),
524 'orderandpaging', 'quiz');
526 echo $OUTPUT->heading(get_string('editingquizx', 'quiz', format_string($quiz->name)), 2);
527 echo $OUTPUT->help_icon('editingquiz', 'quiz', get_string('basicideasofquiz', 'quiz'));
529 quiz_print_status_bar($quiz);
532 quiz_print_grading_form($quiz, $thispageurl, $tabindex);
534 $notifystrings = array();
535 if ($quizhasattempts) {
536 $reviewlink = quiz_attempt_summary_link_to_reports($quiz, $cm, $contexts->lowest());
537 $notifystrings[] = get_string('cannoteditafterattempts', 'quiz', $reviewlink);
539 if ($quiz->shufflequestions) {
540 $updateurl = new moodle_url("$CFG->wwwroot/course/mod.php",
541 array('return' => 'true', 'update' => $quiz->cmid, 'sesskey' => sesskey()));
542 $updatelink = '<a href="'.$updateurl->out().'">' . get_string('updatethis', '',
543 get_string('modulename', 'quiz')) . '</a>';
544 $notifystrings[] = get_string('shufflequestionsselected', 'quiz', $updatelink);
546 if (!empty($notifystrings)) {
547 echo $OUTPUT->box('<p>' . implode('</p><p>', $notifystrings) . '</p>', 'statusdisplay');
550 if ($quiz_reordertool) {
552 $perpage[0] = get_string('allinone', 'quiz');
553 for ($i = 1; $i <= 50; ++$i) {
556 $gostring = get_string('go');
557 echo '<div id="repaginatedialog"><div class="hd">';
558 echo get_string('repaginatecommand', 'quiz');
559 echo '</div><div class="bd">';
560 echo '<form action="edit.php" method="post">';
561 echo '<fieldset class="invisiblefieldset">';
562 echo html_writer::input_hidden_params($thispageurl);
563 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
564 // YUI does not submit the value of the submit button so we need to add the value.
565 echo '<input type="hidden" name="repaginate" value="'.$gostring.'" />';
566 $attributes = array();
567 $attributes['disabled'] = $repaginatingdisabledhtml ? 'disabled' : null;
568 $select = html_writer::select(
569 $perpage, 'questionsperpage', $quiz->questionsperpage, null, $attributes);
570 print_string('repaginate', 'quiz', $select);
571 echo '<div class="quizquestionlistcontrols">';
572 echo ' <input type="submit" name="repaginate" value="'. $gostring . '" ' .
573 $repaginatingdisabledhtml.' />';
574 echo '</div></fieldset></form></div></div>';
577 if ($quiz_reordertool) {
578 echo '<div class="reorder">';
580 echo '<div class="editq">';
583 quiz_print_question_list($quiz, $thispageurl, true, $quiz_reordertool, $quiz_qbanktool,
584 $quizhasattempts, $defaultcategoryobj, $canaddquestion, $canaddrandom);
587 // Close <div class="quizcontents">.
590 if (!$quiz_reordertool && $canaddrandom) {
591 $randomform = new quiz_add_random_form(new moodle_url('/mod/quiz/addrandom.php'), $contexts);
592 $randomform->set_data(array(
593 'category' => $pagevars['cat'],
594 'returnurl' => $thispageurl->out_as_local_url(false),
598 <div id="randomquestiondialog">
599 <div class="hd"><?php print_string('addrandomquestiontoquiz', 'quiz', $quiz->name); ?>
600 <span id="pagenumber"><!-- JavaScript will insert the page number here. -->
603 <div class="bd"><?php
604 $randomform->display();
609 echo $OUTPUT->footer();