MDL-32240 quiz editing: check permissions questions are added.
[moodle.git] / mod / quiz / addrandomform.php
blob0abecb3e534e19437f3ef509adcc3a061d71cd6c
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
21 * @subpackage quiz
22 * @copyright 2008 Olli Savolainen
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir.'/formslib.php');
32 /**
33 * The add random questions form.
35 * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class quiz_add_random_form extends moodleform {
40 protected function definition() {
41 global $CFG, $DB;
42 $mform =& $this->_form;
44 $contexts = $this->_customdata;
45 $usablecontexts = $contexts->having_cap('moodle/question:useall');
47 //--------------------------------------------------------------------------------
48 $mform->addElement('header', 'categoryheader',
49 get_string('randomfromexistingcategory', 'quiz'));
51 $mform->addElement('questioncategory', 'category', get_string('category'),
52 array('contexts' => $usablecontexts, 'top' => false));
54 $mform->addElement('checkbox', 'includesubcategories', '', get_string('recurse', 'quiz'));
56 $mform->addElement('submit', 'existingcategory', get_string('addrandomquestion', 'quiz'));
58 //--------------------------------------------------------------------------------
59 $mform->addElement('header', 'categoryheader',
60 get_string('randomquestionusinganewcategory', 'quiz'));
62 $mform->addElement('text', 'name', get_string('name'), 'maxlength="254" size="50"');
63 $mform->setType('name', PARAM_MULTILANG);
65 $mform->addElement('questioncategory', 'parent', get_string('parentcategory', 'question'),
66 array('contexts' => $usablecontexts, 'top' => true));
67 $mform->addHelpButton('parent', 'parentcategory', 'question');
69 $mform->addElement('submit', 'newcategory',
70 get_string('createcategoryandaddrandomquestion', 'quiz'));
72 //--------------------------------------------------------------------------------
73 $mform->addElement('cancel');
74 $mform->closeHeaderBefore('cancel');
76 $mform->addElement('hidden', 'addonpage', 0, 'id="rform_qpage"');
77 $mform->setType('addonpage', PARAM_SEQUENCE);
78 $mform->addElement('hidden', 'cmid', 0);
79 $mform->setType('cmid', PARAM_INT);
80 $mform->addElement('hidden', 'returnurl', 0);
81 $mform->setType('returnurl', PARAM_LOCALURL);
84 public function validation($fromform, $files) {
85 $errors = parent::validation($fromform, $files);
87 if (!empty($fromform['newcategory']) && trim($fromform['name']) == '') {
88 $errors['name'] = get_string('categorynamecantbeblank', 'question');
91 return $errors;