Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
/mod/quiz/ -> edit.php (source)

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   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.
   8  //
   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.
  13  //
  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/>.
  16  
  17  
  18  /**
  19   * Page to edit quizzes
  20   *
  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
  27   *
  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
  37   *
  38   * @package    mod_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
  41   */
  42  
  43  use mod_quiz\quiz_settings;
  44  
  45  require_once(__DIR__ . '/../../config.php');
  46  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  47  require_once($CFG->dirroot . '/question/editlib.php');
  48  
  49  $mdlscrollto = optional_param('mdlscrollto', '', PARAM_INT);
  50  
  51  list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) =
  52          question_edit_setup('editq', '/mod/quiz/edit.php', true);
  53  
  54  $PAGE->set_url($thispageurl);
  55  $PAGE->set_secondary_active_tab("mod_quiz_edit");
  56  
  57  // You need mod/quiz:manage in addition to question capabilities to access this page.
  58  require_capability('mod/quiz:manage', $contexts->lowest());
  59  
  60  // Get the course object and related bits.
  61  $course = get_course($quiz->course);
  62  $quizobj = new quiz_settings($quiz, $cm, $course);
  63  $structure = $quizobj->get_structure();
  64  $gradecalculator = $quizobj->get_grade_calculator();
  65  
  66  $defaultcategoryobj = question_make_default_categories($contexts->all());
  67  $defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid;
  68  
  69  $quizhasattempts = quiz_has_attempts($quiz->id);
  70  
  71  // Process commands ============================================================.
  72  
  73  // Get the list of question ids had their check-boxes ticked.
  74  $selectedslots = [];
  75  $params = (array) data_submitted();
  76  foreach ($params as $key => $value) {
  77      if (preg_match('!^s([0-9]+)$!', $key, $matches)) {
  78          $selectedslots[] = $matches[1];
  79      }
  80  }
  81  
  82  $afteractionurl = new moodle_url($thispageurl);
  83  
  84  if (optional_param('repaginate', false, PARAM_BOOL) && confirm_sesskey()) {
  85      // Re-paginate the quiz.
  86      $structure->check_can_be_edited();
  87      $questionsperpage = optional_param('questionsperpage', $quiz->questionsperpage, PARAM_INT);
  88      quiz_repaginate_questions($quiz->id, $questionsperpage );
  89      quiz_delete_previews($quiz);
  90      redirect($afteractionurl);
  91  }
  92  
  93  if ($mdlscrollto) {
  94      $afteractionurl->param('mdlscrollto', $mdlscrollto);
  95  }
  96  
  97  if (($addquestion = optional_param('addquestion', 0, PARAM_INT)) && confirm_sesskey()) {
  98      // Add a single question to the current quiz.
  99      $structure->check_can_be_edited();
 100      quiz_require_question_use($addquestion);
 101      $addonpage = optional_param('addonpage', 0, PARAM_INT);
 102      quiz_add_quiz_question($addquestion, $quiz, $addonpage);
 103      quiz_delete_previews($quiz);
 104      $gradecalculator->recompute_quiz_sumgrades();
 105      $thispageurl->param('lastchanged', $addquestion);
 106      redirect($afteractionurl);
 107  }
 108  
 109  if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
 110      $structure->check_can_be_edited();
 111      $addonpage = optional_param('addonpage', 0, PARAM_INT);
 112      // Add selected questions to the current quiz.
 113      $rawdata = (array) data_submitted();
 114      foreach ($rawdata as $key => $value) { // Parse input for question ids.
 115          if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
 116              $key = $matches[1];
 117              quiz_require_question_use($key);
 118              quiz_add_quiz_question($key, $quiz, $addonpage);
 119          }
 120      }
 121      quiz_delete_previews($quiz);
 122      $gradecalculator->recompute_quiz_sumgrades();
 123      redirect($afteractionurl);
 124  }
 125  
 126  if ($addsectionatpage = optional_param('addsectionatpage', false, PARAM_INT)) {
 127      // Add a section to the quiz.
 128      $structure->check_can_be_edited();
 129      $structure->add_section_heading($addsectionatpage);
 130      quiz_delete_previews($quiz);
 131      redirect($afteractionurl);
 132  }
 133  
 134  if ((optional_param('addrandom', false, PARAM_BOOL)) && confirm_sesskey()) {
 135      // Add random questions to the quiz.
 136      $structure->check_can_be_edited();
 137      $recurse = optional_param('recurse', 0, PARAM_BOOL);
 138      $addonpage = optional_param('addonpage', 0, PARAM_INT);
 139      $categoryid = required_param('categoryid', PARAM_INT);
 140      $randomcount = required_param('randomcount', PARAM_INT);
 141      quiz_add_random_questions($quiz, $addonpage, $categoryid, $randomcount, $recurse);
 142  
 143      quiz_delete_previews($quiz);
 144      $gradecalculator->recompute_quiz_sumgrades();
 145      redirect($afteractionurl);
 146  }
 147  
 148  if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
 149  
 150      // If rescaling is required save the new maximum.
 151      $maxgrade = unformat_float(optional_param('maxgrade', '', PARAM_RAW_TRIMMED), true);
 152      if (is_float($maxgrade) && $maxgrade >= 0) {
 153          $gradecalculator->update_quiz_maximum_grade($maxgrade);
 154      }
 155  
 156      redirect($afteractionurl);
 157  }
 158  
 159  // Log this visit.
 160  $event = \mod_quiz\event\edit_page_viewed::create([
 161      'courseid' => $course->id,
 162      'context' => $contexts->lowest(),
 163      'other' => [
 164          'quizid' => $quiz->id
 165      ]
 166  ]);
 167  $event->trigger();
 168  
 169  // Get the question bank view.
 170  $questionbank = new mod_quiz\question\bank\custom_view($contexts, $thispageurl, $course, $cm, $quiz);
 171  $questionbank->set_quiz_has_attempts($quizhasattempts);
 172  
 173  // End of process commands =====================================================.
 174  
 175  $PAGE->set_pagelayout('incourse');
 176  $PAGE->set_pagetype('mod-quiz-edit');
 177  
 178  $output = $PAGE->get_renderer('mod_quiz', 'edit');
 179  
 180  $PAGE->set_title(get_string('editingquizx', 'quiz', format_string($quiz->name)));
 181  $PAGE->set_heading($course->fullname);
 182  $PAGE->activityheader->disable();
 183  $node = $PAGE->settingsnav->find('mod_quiz_edit', navigation_node::TYPE_SETTING);
 184  if ($node) {
 185      $node->make_active();
 186  }
 187  echo $OUTPUT->header();
 188  
 189  // Initialise the JavaScript.
 190  $quizeditconfig = new stdClass();
 191  $quizeditconfig->url = $thispageurl->out(true, ['qbanktool' => '0']);
 192  $quizeditconfig->dialoglisteners = [];
 193  $numberoflisteners = $DB->get_field_sql("
 194      SELECT COALESCE(MAX(page), 1)
 195        FROM {quiz_slots}
 196       WHERE quizid = ?", [$quiz->id]);
 197  
 198  for ($pageiter = 1; $pageiter <= $numberoflisteners; $pageiter++) {
 199      $quizeditconfig->dialoglisteners[] = 'addrandomdialoglaunch_' . $pageiter;
 200  }
 201  
 202  $PAGE->requires->data_for_js('quiz_edit_config', $quizeditconfig);
 203  $PAGE->requires->js_call_amd('core_question/question_engine');
 204  
 205  // Questions wrapper start.
 206  echo html_writer::start_tag('div', ['class' => 'mod-quiz-edit-content']);
 207  
 208  echo $output->edit_page($quizobj, $structure, $contexts, $thispageurl, $pagevars);
 209  
 210  // Questions wrapper end.
 211  echo html_writer::end_tag('div');
 212  
 213  echo $OUTPUT->footer();