weekly release 5.0dev
[moodle.git] / mod / quiz / editrandom.php
blobb0412d16e03d18d6322790a76a091f48607aeaad
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.
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/>.
17 /**
18 * Page for editing random questions.
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
26 use mod_quiz\quiz_settings;
27 use mod_quiz\question\bank\random_question_view;
29 require_once(__DIR__ . '/../../config.php');
30 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
31 require_once($CFG->dirroot . '/mod/quiz/lib.php');
33 $slotid = required_param('slotid', PARAM_INT);
34 $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
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();
43 require_login($course, false, $cm);
45 if ($returnurl) {
46 $returnurl = new moodle_url($returnurl);
47 } else {
48 $returnurl = new moodle_url('/mod/quiz/edit.php', ['cmid' => $cm->id]);
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');
56 $setreference = $DB->get_record('question_set_references',
57 ['itemid' => $slot->id, 'component' => 'mod_quiz', 'questionarea' => 'slot']);
58 $filterconditions = json_decode($setreference->filtercondition, true);
60 $params = $filterconditions;
61 $params['cmid'] = $cm->id;
62 $extraparams['view'] = random_question_view::class;
64 // Build required parameters.
65 [$contexts, $thispageurl, $cm, $pagevars, $extraparams] = build_required_parameters_for_custom_view($params, $extraparams);
67 $thiscontext = $quizobj->get_context();
68 $contexts = new core_question\local\bank\question_edit_contexts($thiscontext);
70 // Create the editing form.
71 $mform = new mod_quiz\form\randomquestion_form(new moodle_url('/mod/quiz/editrandom.php'), ['contexts' => $contexts]);
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;
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;
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;
98 $mform->set_data($toform);
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;
109 $setreference->questionscontextid = $contextid;
111 // Set the filter conditions.
112 $filtercondition = new stdClass();
113 $filtercondition->questioncategoryid = $newcatid;
114 $filtercondition->includingsubcategories = $fromform->includesubcategories;
116 if (isset($fromform->fromtags)) {
117 $tags = [];
118 foreach ($fromform->fromtags as $tagstring) {
119 list($tagid, $tagname) = explode(',', $tagstring);
120 $tags[] = "{$tagid},{$tagname}";
122 if (!empty($tags)) {
123 $filtercondition->tags = $tags;
127 $setreference->filtercondition = json_encode($filtercondition);
128 $DB->update_record('question_set_references', $setreference);
130 redirect($returnurl);
133 $PAGE->set_title('Random question');
134 $PAGE->set_heading($COURSE->fullname);
135 $PAGE->navbar->add('Random question');
137 // Custom View.
138 $questionbank = new random_question_view($contexts, $thispageurl, $course, $cm, $params, $extraparams);
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');
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();