Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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   * Fallback page of /mod/quiz/edit.php add random question dialog,
  19   * for users who do not use javascript.
  20   *
  21   * @package   mod_quiz
  22   * @copyright 2008 Olli Savolainen
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  require_once(__DIR__ . '/../../config.php');
  28  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  29  require_once($CFG->dirroot . '/mod/quiz/addrandomform.php');
  30  require_once($CFG->dirroot . '/question/editlib.php');
  31  
  32  use qbank_managecategories\question_category_object;
  33  
  34  list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) =
  35          question_edit_setup('editq', '/mod/quiz/addrandom.php', true);
  36  
  37  // These params are only passed from page request to request while we stay on
  38  // this page otherwise they would go in question_edit_setup.
  39  $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
  40  $addonpage = optional_param('addonpage', 0, PARAM_INT);
  41  $category = optional_param('category', 0, PARAM_INT);
  42  $scrollpos = optional_param('scrollpos', 0, PARAM_INT);
  43  
  44  // Get the course object and related bits.
  45  if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
  46      throw new \moodle_exception('invalidcourseid');
  47  }
  48  // You need mod/quiz:manage in addition to question capabilities to access this page.
  49  // You also need the moodle/question:useall capability somewhere.
  50  require_capability('mod/quiz:manage', $contexts->lowest());
  51  if (!$contexts->having_cap('moodle/question:useall')) {
  52      throw new \moodle_exception('nopermissions', '', '', 'use');
  53  }
  54  
  55  $PAGE->set_url($thispageurl);
  56  
  57  if ($returnurl) {
  58      $returnurl = new moodle_url($returnurl);
  59  } else {
  60      $returnurl = new moodle_url('/mod/quiz/edit.php', array('cmid' => $cmid));
  61  }
  62  if ($scrollpos) {
  63      $returnurl->param('scrollpos', $scrollpos);
  64  }
  65  
  66  $defaultcategoryobj = question_make_default_categories($contexts->all());
  67  $defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid;
  68  
  69  $qcobject = new question_category_object(
  70      $pagevars['cpage'],
  71      $thispageurl,
  72      $contexts->having_one_edit_tab_cap('categories'),
  73      $defaultcategoryobj->id,
  74      $defaultcategory,
  75      null,
  76      $contexts->having_cap('moodle/question:add'));
  77  
  78  $mform = new quiz_add_random_form(new moodle_url('/mod/quiz/addrandom.php'),
  79                  array('contexts' => $contexts, 'cat' => $pagevars['cat']));
  80  
  81  if ($mform->is_cancelled()) {
  82      redirect($returnurl);
  83  }
  84  
  85  if ($data = $mform->get_data()) {
  86      if (!empty($data->existingcategory)) {
  87          list($categoryid) = explode(',', $data->category);
  88          $includesubcategories = !empty($data->includesubcategories);
  89          if (!$includesubcategories) {
  90              // If the chosen category is a top category.
  91              $includesubcategories = $DB->record_exists('question_categories', ['id' => $categoryid, 'parent' => 0]);
  92          }
  93          $returnurl->param('cat', $data->category);
  94  
  95      } else if (!empty($data->newcategory)) {
  96          list($parentid, $contextid) = explode(',', $data->parent);
  97          $categoryid = $qcobject->add_category($data->parent, $data->name, '', true);
  98          $includesubcategories = 0;
  99  
 100          $returnurl->param('cat', $categoryid . ',' . $contextid);
 101      } else {
 102          throw new coding_exception(
 103                  'It seems a form was submitted without any button being pressed???');
 104      }
 105  
 106      if (empty($data->fromtags)) {
 107          $data->fromtags = [];
 108      }
 109  
 110      $tagids = array_map(function($tagstrings) {
 111          return (int)explode(',', $tagstrings)[0];
 112      }, $data->fromtags);
 113  
 114      quiz_add_random_questions($quiz, $addonpage, $categoryid, $data->numbertoadd, $includesubcategories, $tagids);
 115      quiz_delete_previews($quiz);
 116      quiz_update_sumgrades($quiz);
 117      redirect($returnurl);
 118  }
 119  
 120  $mform->set_data(array(
 121      'addonpage' => $addonpage,
 122      'returnurl' => $returnurl,
 123      'cmid' => $cm->id,
 124      'category' => $category,
 125  ));
 126  
 127  // Setup $PAGE.
 128  $streditingquiz = get_string('editinga', 'moodle', get_string('modulename', 'quiz'));
 129  $PAGE->navbar->add($streditingquiz);
 130  $PAGE->set_title($streditingquiz);
 131  $PAGE->set_heading($course->fullname);
 132  echo $OUTPUT->header();
 133  
 134  if (!$quizname = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance))) {
 135              throw new \moodle_exception('invalidcoursemodule');
 136  }
 137  
 138  echo $OUTPUT->heading(get_string('addrandomquestiontoquiz', 'quiz', $quizname), 2);
 139  $mform->display();
 140  echo $OUTPUT->footer();
 141