Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/mod/quiz/ -> edit.php (source)

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