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.

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