Merge branch 'MDL-74441-400' of https://github.com/cameron1729/moodle into MOODLE_400...
[moodle.git] / mod / quiz / addrandomform.php
blobd16121fc0178bff3422aa28de0166ae707e6c0ff
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 * Defines the Moodle forum used to add random questions to the quiz.
20 * @package mod_quiz
21 * @copyright 2008 Olli Savolainen
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir.'/formslib.php');
31 /**
32 * The add random questions form.
34 * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class quiz_add_random_form extends moodleform {
39 protected function definition() {
40 global $OUTPUT, $PAGE, $CFG;
42 $mform = $this->_form;
43 $mform->setDisableShortforms();
45 $contexts = $this->_customdata['contexts'];
46 $usablecontexts = $contexts->having_cap('moodle/question:useall');
48 // Random from existing category section.
49 $mform->addElement('header', 'existingcategoryheader',
50 get_string('randomfromexistingcategory', 'quiz'));
52 $mform->addElement('questioncategory', 'category', get_string('category'),
53 array('contexts' => $usablecontexts, 'top' => true));
54 $mform->setDefault('category', $this->_customdata['cat']);
56 $mform->addElement('checkbox', 'includesubcategories', '', get_string('recurse', 'quiz'));
58 $tops = question_get_top_categories_for_contexts(array_column($contexts->all(), 'id'));
59 $mform->hideIf('includesubcategories', 'category', 'in', $tops);
61 if ($CFG->usetags) {
62 $tagstrings = array();
63 $tags = core_tag_tag::get_tags_by_area_in_contexts('core_question', 'question', $usablecontexts);
64 foreach ($tags as $tag) {
65 $tagstrings["{$tag->id},{$tag->name}"] = $tag->name;
67 $options = array(
68 'multiple' => true,
69 'noselectionstring' => get_string('anytags', 'quiz'),
71 $mform->addElement('autocomplete', 'fromtags', get_string('randomquestiontags', 'mod_quiz'), $tagstrings, $options);
72 $mform->addHelpButton('fromtags', 'randomquestiontags', 'mod_quiz');
75 // TODO: in the past, the drop-down used to only show sensible choices for
76 // number of questions to add. That is, if the currently selected filter
77 // only matched 9 questions (not already in the quiz), then the drop-down would
78 // only offer choices 1..9. This nice UI hint got lost when the UI became Ajax-y.
79 // We should add it back.
80 $mform->addElement('select', 'numbertoadd', get_string('randomnumber', 'quiz'),
81 $this->get_number_of_questions_to_add_choices());
83 $previewhtml = $OUTPUT->render_from_template('mod_quiz/random_question_form_preview', []);
84 $mform->addElement('html', $previewhtml);
86 $mform->addElement('submit', 'existingcategory', get_string('addrandomquestion', 'quiz'));
88 // If the manage categories plugins is enabled, add the elements to create a new category in the form.
89 if (\core\plugininfo\qbank::is_plugin_enabled(\qbank_managecategories\helper::PLUGINNAME)) {
90 // Random from a new category section.
91 $mform->addElement('header', 'newcategoryheader',
92 get_string('randomquestionusinganewcategory', 'quiz'));
94 $mform->addElement('text', 'name', get_string('name'), 'maxlength="254" size="50"');
95 $mform->setType('name', PARAM_TEXT);
97 $mform->addElement('questioncategory', 'parent', get_string('parentcategory', 'question'),
98 array('contexts' => $usablecontexts, 'top' => true));
99 $mform->addHelpButton('parent', 'parentcategory', 'question');
101 $mform->addElement('submit', 'newcategory',
102 get_string('createcategoryandaddrandomquestion', 'quiz'));
105 // Cancel button.
106 $mform->addElement('cancel');
107 $mform->closeHeaderBefore('cancel');
109 $mform->addElement('hidden', 'addonpage', 0, 'id="rform_qpage"');
110 $mform->setType('addonpage', PARAM_SEQUENCE);
111 $mform->addElement('hidden', 'cmid', 0);
112 $mform->setType('cmid', PARAM_INT);
113 $mform->addElement('hidden', 'returnurl', 0);
114 $mform->setType('returnurl', PARAM_LOCALURL);
116 // Add the javascript required to enhance this mform.
117 $PAGE->requires->js_call_amd('mod_quiz/add_random_form', 'init', [
118 $mform->getAttribute('id'),
119 $contexts->lowest()->id,
120 $tops,
121 $CFG->usetags
125 public function validation($fromform, $files) {
126 $errors = parent::validation($fromform, $files);
128 if (!empty($fromform['newcategory']) && trim($fromform['name']) == '') {
129 $errors['name'] = get_string('categorynamecantbeblank', 'question');
132 return $errors;
136 * Return an arbitrary array for the dropdown menu
138 * @param int $maxrand
139 * @return array of integers [1, 2, ..., 100] (or to the smaller of $maxrand and 100.)
141 private function get_number_of_questions_to_add_choices($maxrand = 100) {
142 $randomcount = array();
143 for ($i = 1; $i <= min(100, $maxrand); $i++) {
144 $randomcount[$i] = $i;
146 return $randomcount;