MDL-47547 quiz editing: minor fixes following MDL-47547
[moodle.git] / mod / quiz / addrandomform.php
CommitLineData
94dbfb3a 1<?php
ba643847
TH
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 * Defines the Moodle forum used to add random questions to the quiz.
19 *
01773a6d
SH
20 * @package mod_quiz
21 * @copyright 2008 Olli Savolainen
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
ba643847
TH
23 */
24
25
a17b297d
TH
26defined('MOODLE_INTERNAL') || die();
27
94dbfb3a
TH
28require_once($CFG->libdir.'/formslib.php');
29
ba643847
TH
30
31/**
32 * The add random questions form.
33 *
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
36 */
94dbfb3a
TH
37class quiz_add_random_form extends moodleform {
38
c7df5006 39 protected function definition() {
94dbfb3a
TH
40 global $CFG, $DB;
41 $mform =& $this->_form;
4d1fcfdf 42 $mform->setDisableShortforms();
94dbfb3a 43
e1a2d0d9 44 $contexts = $this->_customdata['contexts'];
76cf77e4 45 $usablecontexts = $contexts->having_cap('moodle/question:useall');
94dbfb3a 46
9e83f3d1 47 // Random from existing category section.
55ca80ed
TH
48 $mform->addElement('header', 'categoryheader',
49 get_string('randomfromexistingcategory', 'quiz'));
94dbfb3a
TH
50
51 $mform->addElement('questioncategory', 'category', get_string('category'),
76cf77e4 52 array('contexts' => $usablecontexts, 'top' => false));
e1a2d0d9 53 $mform->setDefault('category', $this->_customdata['cat']);
94dbfb3a
TH
54
55 $mform->addElement('checkbox', 'includesubcategories', '', get_string('recurse', 'quiz'));
56
e1a2d0d9
CC
57 $mform->addElement('select', 'numbertoadd', get_string('randomnumber', 'quiz'),
58 $this->get_number_of_questions_to_add_choices());
59
94dbfb3a
TH
60 $mform->addElement('submit', 'existingcategory', get_string('addrandomquestion', 'quiz'));
61
9e83f3d1 62 // Random from a new category section.
55ca80ed
TH
63 $mform->addElement('header', 'categoryheader',
64 get_string('randomquestionusinganewcategory', 'quiz'));
94dbfb3a
TH
65
66 $mform->addElement('text', 'name', get_string('name'), 'maxlength="254" size="50"');
071e68f9 67 $mform->setType('name', PARAM_TEXT);
94dbfb3a
TH
68
69 $mform->addElement('questioncategory', 'parent', get_string('parentcategory', 'question'),
76cf77e4 70 array('contexts' => $usablecontexts, 'top' => true));
94dbfb3a
TH
71 $mform->addHelpButton('parent', 'parentcategory', 'question');
72
55ca80ed
TH
73 $mform->addElement('submit', 'newcategory',
74 get_string('createcategoryandaddrandomquestion', 'quiz'));
94dbfb3a 75
e1a2d0d9 76 // Cancel button.
94dbfb3a
TH
77 $mform->addElement('cancel');
78 $mform->closeHeaderBefore('cancel');
79
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);
86 }
87
c7df5006 88 public function validation($fromform, $files) {
94dbfb3a
TH
89 $errors = parent::validation($fromform, $files);
90
91 if (!empty($fromform['newcategory']) && trim($fromform['name']) == '') {
42663bb7 92 $errors['name'] = get_string('categorynamecantbeblank', 'question');
94dbfb3a
TH
93 }
94
95 return $errors;
96 }
94dbfb3a 97
e1a2d0d9
CC
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)
101 */
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;
107 }
108 for ($i = 20; $i <= min(100, $maxrand); $i += 10) {
109 $randomcount[$i] = $i;
110 }
111 return $randomcount;
112 }
113}