See Release Notes
Long Term Support Release
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 /** 18 * Defines the import questions form. 19 * 20 * @package qbank_importquestions 21 * @copyright 2007 Jamie Pratt me@jamiep.org 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace qbank_importquestions\form; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 use moodle_exception; 30 use moodleform; 31 use stdClass; 32 33 require_once($CFG->libdir . '/formslib.php'); 34 35 /** 36 * Form to import questions into the question bank. 37 * 38 * @copyright 2007 Jamie Pratt me@jamiep.org 39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 */ 41 class question_import_form extends moodleform { 42 43 /** 44 * Build the form definition. 45 * 46 * This adds all the form fields that the manage categories feature needs. 47 * @throws \coding_exception 48 */ 49 protected function definition() { 50 global $OUTPUT; 51 52 $mform = $this->_form; 53 54 $defaultcategory = $this->_customdata['defaultcategory']; 55 $contexts = $this->_customdata['contexts']; 56 57 // Choice of import format, with help icons. 58 $mform->addElement('header', 'fileformat', get_string('fileformat', 'question')); 59 60 $fileformatnames = get_import_export_formats('import'); 61 $radioarray = []; 62 $separators = []; 63 foreach ($fileformatnames as $shortname => $fileformatname) { 64 $radioarray[] = $mform->createElement('radio', 'format', '', $fileformatname, $shortname); 65 66 $separator = ''; 67 if (get_string_manager()->string_exists('pluginname_help', 'qformat_' . $shortname)) { 68 $separator .= $OUTPUT->help_icon('pluginname', 'qformat_' . $shortname); 69 } 70 $separator .= '<div class="w-100"></div>'; 71 $separators[] = $separator; 72 } 73 74 $radioarray[] = $mform->createElement('static', 'makelasthelpiconshowup', ''); 75 $mform->addGroup($radioarray, "formatchoices", '', $separators, false); 76 $mform->addRule("formatchoices", null, 'required', null, 'client'); 77 78 // Import options. 79 $mform->addElement('header', 'general', get_string('general', 'form')); 80 81 $mform->addElement('questioncategory', 'category', get_string('importcategory', 'question'), compact('contexts')); 82 $mform->setDefault('category', $defaultcategory); 83 $mform->addHelpButton('category', 'importcategory', 'question'); 84 85 $categorygroup = []; 86 $categorygroup[] = $mform->createElement('checkbox', 'catfromfile', '', get_string('getcategoryfromfile', 'question')); 87 $categorygroup[] = $mform->createElement('checkbox', 'contextfromfile', '', get_string('getcontextfromfile', 'question')); 88 $mform->addGroup($categorygroup, 'categorygroup', '', '', false); 89 $mform->disabledIf('categorygroup', 'catfromfile', 'notchecked'); 90 $mform->setDefault('catfromfile', 1); 91 $mform->setDefault('contextfromfile', 1); 92 93 $matchgrades = []; 94 $matchgrades['error'] = get_string('matchgradeserror', 'question'); 95 $matchgrades['nearest'] = get_string('matchgradesnearest', 'question'); 96 $mform->addElement('select', 'matchgrades', get_string('matchgrades', 'question'), $matchgrades); 97 $mform->addHelpButton('matchgrades', 'matchgrades', 'question'); 98 $mform->setDefault('matchgrades', 'error'); 99 100 $mform->addElement('selectyesno', 'stoponerror', get_string('stoponerror', 'question')); 101 $mform->setDefault('stoponerror', 1); 102 $mform->addHelpButton('stoponerror', 'stoponerror', 'question'); 103 104 // The file to import. 105 $mform->addElement('header', 'importfileupload', get_string('importquestions', 'question')); 106 107 $mform->addElement('filepicker', 'newfile', get_string('import')); 108 $mform->addRule('newfile', null, 'required', null, 'client'); 109 110 // Submit button. 111 $mform->addElement('submit', 'submitbutton', get_string('import')); 112 113 // Set a template for the format select elements. 114 $renderer = $mform->defaultRenderer(); 115 $template = "{help} {element}\n"; 116 $renderer->setGroupElementTemplate($template, 'format'); 117 } 118 119 /** 120 * Checks that a file has been uploaded, and that it is of a plausible type. 121 * @param array $data the submitted data. 122 * @param array $errors the errors so far. 123 * @return array the updated errors. 124 * @throws moodle_exception 125 */ 126 protected function validate_uploaded_file($data, $errors) { 127 global $CFG; 128 129 if (empty($data['newfile'])) { 130 $errors['newfile'] = get_string('required'); 131 return $errors; 132 } 133 134 $files = $this->get_draft_files('newfile'); 135 if (!is_array($files) || count($files) < 1) { 136 $errors['newfile'] = get_string('required'); 137 return $errors; 138 } 139 140 if (empty($data['format'])) { 141 $errors['format'] = get_string('required'); 142 return $errors; 143 } 144 145 $formatfile = $CFG->dirroot . '/question/format/' . $data['format'] . '/format.php'; 146 if (!is_readable($formatfile)) { 147 throw new moodle_exception('formatnotfound', 'question', '', $data['format']); 148 } 149 150 require_once($formatfile); 151 152 $classname = 'qformat_' . $data['format']; 153 $qformat = new $classname(); 154 155 $file = reset($files); 156 if (!$qformat->can_import_file($file)) { 157 $a = new stdClass(); 158 $a->actualtype = $file->get_mimetype(); 159 $a->expectedtype = $qformat->mime_type(); 160 $errors['newfile'] = get_string('importwrongfiletype', 'question', $a); 161 return $errors; 162 } 163 164 $fileerrors = $qformat->validate_file($file); 165 if ($fileerrors) { 166 $errors['newfile'] = $fileerrors; 167 } 168 169 return $errors; 170 } 171 172 /** 173 * Validation. 174 * 175 * @param array $data 176 * @param array $files 177 * @return array the errors that were found 178 * @throws \dml_exception|\coding_exception|moodle_exception 179 */ 180 public function validation($data, $files) { 181 $errors = parent::validation($data, $files); 182 $errors = $this->validate_uploaded_file($data, $errors); 183 return $errors; 184 } 185 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body