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