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