Merge branch 'MDL-51305_master_too_many_gradeitem_fetches' of https://github.com...
[moodle.git] / mod / quiz / addrandomform.php
blobf66832e4b28eab48d64da855baf0b5a21adc5977
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 $CFG, $DB;
41 $mform =& $this->_form;
42 $mform->setDisableShortforms();
44 $contexts = $this->_customdata['contexts'];
45 $usablecontexts = $contexts->having_cap('moodle/question:useall');
47 // Random from existing category section.
48 $mform->addElement('header', 'categoryheader',
49 get_string('randomfromexistingcategory', 'quiz'));
51 $mform->addElement('questioncategory', 'category', get_string('category'),
52 array('contexts' => $usablecontexts, 'top' => false));
53 $mform->setDefault('category', $this->_customdata['cat']);
55 $mform->addElement('checkbox', 'includesubcategories', '', get_string('recurse', 'quiz'));
57 $mform->addElement('select', 'numbertoadd', get_string('randomnumber', 'quiz'),
58 $this->get_number_of_questions_to_add_choices());
60 $mform->addElement('submit', 'existingcategory', get_string('addrandomquestion', 'quiz'));
62 // Random from a new category section.
63 $mform->addElement('header', 'categoryheader',
64 get_string('randomquestionusinganewcategory', 'quiz'));
66 $mform->addElement('text', 'name', get_string('name'), 'maxlength="254" size="50"');
67 $mform->setType('name', PARAM_TEXT);
69 $mform->addElement('questioncategory', 'parent', get_string('parentcategory', 'question'),
70 array('contexts' => $usablecontexts, 'top' => true));
71 $mform->addHelpButton('parent', 'parentcategory', 'question');
73 $mform->addElement('submit', 'newcategory',
74 get_string('createcategoryandaddrandomquestion', 'quiz'));
76 // Cancel button.
77 $mform->addElement('cancel');
78 $mform->closeHeaderBefore('cancel');
80 $mform->addElement('hidden', 'addonpage', 0, 'id="rform_qpage"');
81 $mform->setType('addonpage', PARAM_SEQUENCE);
82 $mform->addElement('hidden', 'cmid', 0);
83 $mform->setType('cmid', PARAM_INT);
84 $mform->addElement('hidden', 'returnurl', 0);
85 $mform->setType('returnurl', PARAM_LOCALURL);
88 public function validation($fromform, $files) {
89 $errors = parent::validation($fromform, $files);
91 if (!empty($fromform['newcategory']) && trim($fromform['name']) == '') {
92 $errors['name'] = get_string('categorynamecantbeblank', 'question');
95 return $errors;
98 /**
99 * Return an arbitrary array for the dropdown menu
100 * @return array of integers array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
102 private function get_number_of_questions_to_add_choices() {
103 $maxrand = 100;
104 $randomcount = array();
105 for ($i = 1; $i <= min(10, $maxrand); $i++) {
106 $randomcount[$i] = $i;
108 for ($i = 20; $i <= min(100, $maxrand); $i += 10) {
109 $randomcount[$i] = $i;
111 return $randomcount;