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 401 and 402] [Versions 401 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   * Page for editing random questions.
  19   *
  20   * @package    mod_quiz
  21   * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
  22   * @author     2021 Safat Shahin <safatshahin@catalyst-au.net>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once(__DIR__ . '/../../config.php');
  27  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  28  
  29  $slotid = required_param('slotid', PARAM_INT);
  30  $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
  31  
  32  // Get the quiz slot.
  33  $slot = $DB->get_record('quiz_slots', ['id' => $slotid]);
  34  if (!$slot) {
  35      new moodle_exception('invalidrandomslot', 'mod_quiz');
  36  }
  37  
  38  if (!$quiz = $DB->get_record('quiz', ['id' => $slot->quizid])) {
  39      new moodle_exception('invalidquizid', 'quiz');
  40  }
  41  
  42  $cm = get_coursemodule_from_instance('quiz', $slot->quizid, $quiz->course);
  43  
  44  require_login($cm->course, false, $cm);
  45  
  46  if ($returnurl) {
  47      $returnurl = new moodle_url($returnurl);
  48  } else {
  49      $returnurl = new moodle_url('/mod/quiz/edit.php', ['cmid' => $cm->id]);
  50  }
  51  
  52  $url = new moodle_url('/mod/quiz/editrandom.php', ['slotid' => $slotid]);
  53  $PAGE->set_url($url);
  54  $PAGE->set_pagelayout('admin');
  55  $PAGE->add_body_class('limitedwidth');
  56  
  57  $setreference = $DB->get_record('question_set_references',
  58      ['itemid' => $slot->id, 'component' => 'mod_quiz', 'questionarea' => 'slot']);
  59  $filterconditions = json_decode($setreference->filtercondition);
  60  
  61  // Validate the question category.
  62  if (!$category = $DB->get_record('question_categories', ['id' => $filterconditions->questioncategoryid])) {
  63      new moodle_exception('categorydoesnotexist', 'question', $returnurl);
  64  }
  65  
  66  // Check permissions.
  67  $catcontext = context::instance_by_id($category->contextid);
  68  require_capability('moodle/question:useall', $catcontext);
  69  
  70  $thiscontext = context_module::instance($cm->id);
  71  $contexts = new core_question\local\bank\question_edit_contexts($thiscontext);
  72  
  73  // Create the editing form.
  74  $mform = new mod_quiz\form\randomquestion_form(new moodle_url('/mod/quiz/editrandom.php'), ['contexts' => $contexts]);
  75  
  76  // Set the form data.
  77  $toform = new stdClass();
  78  $toform->category = "{$category->id},{$category->contextid}";
  79  $toform->includesubcategories = $filterconditions->includingsubcategories;
  80  $toform->fromtags = array();
  81  if (isset($filterconditions->tags)) {
  82      $currentslottags = $filterconditions->tags;
  83      foreach ($currentslottags as $slottag) {
  84          $toform->fromtags[] = $slottag;
  85      }
  86  }
  87  
  88  $toform->returnurl = $returnurl;
  89  $toform->slotid = $slot->id;
  90  if ($cm !== null) {
  91      $toform->cmid = $cm->id;
  92      $toform->courseid = $cm->course;
  93  } else {
  94      $toform->courseid = $COURSE->id;
  95  }
  96  
  97  $mform->set_data($toform);
  98  
  99  if ($mform->is_cancelled()) {
 100      redirect($returnurl);
 101  } else if ($fromform = $mform->get_data()) {
 102      list($newcatid, $newcontextid) = explode(',', $fromform->category);
 103      if ($newcatid != $category->id) {
 104          $contextid = $newcontextid;
 105      } else {
 106          $contextid = $category->contextid;
 107      }
 108      $setreference->questionscontextid = $contextid;
 109  
 110      // Set the filter conditions.
 111      $filtercondition = new stdClass();
 112      $filtercondition->questioncategoryid = $newcatid;
 113      $filtercondition->includingsubcategories = $fromform->includesubcategories;
 114  
 115      if (isset($fromform->fromtags)) {
 116          $tags = [];
 117          foreach ($fromform->fromtags as $tagstring) {
 118              list($tagid, $tagname) = explode(',', $tagstring);
 119              $tags[] = "{$tagid},{$tagname}";
 120          }
 121          if (!empty($tags)) {
 122              $filtercondition->tags = $tags;
 123          }
 124      }
 125  
 126      $setreference->filtercondition = json_encode($filtercondition);
 127      $DB->update_record('question_set_references', $setreference);
 128  
 129      redirect($returnurl);
 130  }
 131  
 132  $PAGE->set_title('Random question');
 133  $PAGE->set_heading($COURSE->fullname);
 134  $PAGE->navbar->add('Random question');
 135  
 136  // Display a heading, question editing form.
 137  echo $OUTPUT->header();
 138  $heading = get_string('randomediting', 'mod_quiz');
 139  echo $OUTPUT->heading_with_help($heading, 'randomquestion', 'mod_quiz');
 140  
 141  $mform->display();
 142  
 143  echo $OUTPUT->footer();