Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [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  use mod_quiz\question\bank\random_question_view;
  28  
  29  require_once(__DIR__ . '/../../config.php');
  30  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  31  require_once($CFG->dirroot . '/mod/quiz/lib.php');
  32  
  33  $slotid = required_param('slotid', PARAM_INT);
  34  $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
  35  
  36  // Get the quiz slot.
  37  $slot = $DB->get_record('quiz_slots', ['id' => $slotid], '*', MUST_EXIST);
  38  $quizobj = quiz_settings::create($slot->quizid);
  39  $quiz = $quizobj->get_quiz();
  40  $cm = $quizobj->get_cm();
  41  $course = $quizobj->get_course();
  42  
  43  require_login($course, false, $cm);
  44  
  45  if ($returnurl) {
  46      $returnurl = new moodle_url($returnurl);
  47  } else {
  48      $returnurl = new moodle_url('/mod/quiz/edit.php', ['cmid' => $cm->id]);
  49  }
  50  
  51  $url = new moodle_url('/mod/quiz/editrandom.php', ['slotid' => $slotid]);
  52  $PAGE->set_url($url);
  53  $PAGE->set_pagelayout('admin');
  54  $PAGE->add_body_class('limitedwidth');
  55  
  56  $setreference = $DB->get_record('question_set_references',
  57      ['itemid' => $slot->id, 'component' => 'mod_quiz', 'questionarea' => 'slot']);
  58  $filterconditions = json_decode($setreference->filtercondition, true);
  59  
  60  $params = $filterconditions;
  61  $params['cmid'] = $cm->id;
  62  $extraparams['view'] = random_question_view::class;
  63  
  64  // Build required parameters.
  65  [$contexts, $thispageurl, $cm, $pagevars, $extraparams] = build_required_parameters_for_custom_view($params, $extraparams);
  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 = $filterconditions['filter']['category']['values'][0];
  76  $includesubcategories = false;
  77  if (!empty($filterconditions['filter']['category']['filteroptions']['includesubcategories'])) {
  78      $includesubcategories = true;
  79  }
  80  $toform->includesubcategories = $includesubcategories;
  81  $toform->fromtags = [];
  82  if (isset($filterconditions['tags'])) {
  83      $currentslottags = $filterconditions['tags'];
  84      foreach ($currentslottags as $slottag) {
  85          $toform->fromtags[] = $slottag;
  86      }
  87  }
  88  
  89  $toform->returnurl = $returnurl;
  90  $toform->slotid = $slot->id;
  91  if ($cm !== null) {
  92      $toform->cmid = $cm->id;
  93      $toform->courseid = $cm->course;
  94  } else {
  95      $toform->courseid = $COURSE->id;
  96  }
  97  
  98  $mform->set_data($toform);
  99  
 100  if ($mform->is_cancelled()) {
 101      redirect($returnurl);
 102  } else if ($fromform = $mform->get_data()) {
 103      list($newcatid, $newcontextid) = explode(',', $fromform->category);
 104      if ($newcatid != $category->id) {
 105          $contextid = $newcontextid;
 106      } else {
 107          $contextid = $category->contextid;
 108      }
 109      $setreference->questionscontextid = $contextid;
 110  
 111      // Set the filter conditions.
 112      $filtercondition = new stdClass();
 113      $filtercondition->questioncategoryid = $newcatid;
 114      $filtercondition->includingsubcategories = $fromform->includesubcategories;
 115  
 116      if (isset($fromform->fromtags)) {
 117          $tags = [];
 118          foreach ($fromform->fromtags as $tagstring) {
 119              list($tagid, $tagname) = explode(',', $tagstring);
 120              $tags[] = "{$tagid},{$tagname}";
 121          }
 122          if (!empty($tags)) {
 123              $filtercondition->tags = $tags;
 124          }
 125      }
 126  
 127      $setreference->filtercondition = json_encode($filtercondition);
 128      $DB->update_record('question_set_references', $setreference);
 129  
 130      redirect($returnurl);
 131  }
 132  
 133  $PAGE->set_title('Random question');
 134  $PAGE->set_heading($COURSE->fullname);
 135  $PAGE->navbar->add('Random question');
 136  
 137  // Custom View.
 138  $questionbank = new random_question_view($contexts, $thispageurl, $course, $cm, $params, $extraparams);
 139  
 140  // Output.
 141  $renderer = $PAGE->get_renderer('mod_quiz', 'edit');
 142  $data = new \stdClass();
 143  $data->questionbank = $renderer->question_bank_contents($questionbank, $params);
 144  $data->cmid = $cm->id;
 145  $data->slotid = $slot->id;
 146  $data->returnurl = $returnurl;
 147  $updateform = $OUTPUT->render_from_template('mod_quiz/update_filter_condition_form', $data);
 148  $PAGE->requires->js_call_amd('mod_quiz/update_random_question_filter_condition', 'init');
 149  
 150  // Display a heading, question editing form.
 151  echo $OUTPUT->header();
 152  $heading = get_string('randomediting', 'mod_quiz');
 153  echo $OUTPUT->heading_with_help($heading, 'randomquestion', 'mod_quiz');
 154  echo $updateform;
 155  echo $OUTPUT->footer();