[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 [email protected] 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 [email protected] 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 $COURSE; 42 $mform = $this->_form; 43 44 $defaultcategory = $this->_customdata['defaultcategory']; 45 $contexts = $this->_customdata['contexts']; 46 47 // Choice of import format, with help icons. 48 $mform->addElement('header', 'fileformat', get_string('fileformat', 'question')); 49 50 $fileformatnames = get_import_export_formats('import'); 51 $radioarray = array(); 52 $i = 0 ; 53 foreach ($fileformatnames as $shortname => $fileformatname) { 54 $currentgrp1 = array(); 55 $currentgrp1[] = $mform->createElement('radio', 'format', '', $fileformatname, $shortname); 56 $mform->addGroup($currentgrp1, "formathelp[{$i}]", '', array('<br />'), false); 57 58 if (get_string_manager()->string_exists('pluginname_help', 'qformat_' . $shortname)) { 59 $mform->addHelpButton("formathelp[{$i}]", 'pluginname', 'qformat_' . $shortname); 60 } 61 62 $i++ ; 63 } 64 $mform->addRule("formathelp[0]", null, 'required', null, 'client'); 65 66 // Import options. 67 $mform->addElement('header','general', get_string('general', 'form')); 68 69 $mform->addElement('questioncategory', 'category', get_string('importcategory', 'question'), compact('contexts')); 70 $mform->setDefault('category', $defaultcategory); 71 $mform->addHelpButton('category', 'importcategory', 'question'); 72 73 $categorygroup = array(); 74 $categorygroup[] = $mform->createElement('checkbox', 'catfromfile', '', get_string('getcategoryfromfile', 'question')); 75 $categorygroup[] = $mform->createElement('checkbox', 'contextfromfile', '', get_string('getcontextfromfile', 'question')); 76 $mform->addGroup($categorygroup, 'categorygroup', '', '', false); 77 $mform->disabledIf('categorygroup', 'catfromfile', 'notchecked'); 78 $mform->setDefault('catfromfile', 1); 79 $mform->setDefault('contextfromfile', 1); 80 81 $matchgrades = array(); 82 $matchgrades['error'] = get_string('matchgradeserror', 'question'); 83 $matchgrades['nearest'] = get_string('matchgradesnearest', 'question'); 84 $mform->addElement('select', 'matchgrades', get_string('matchgrades', 'question'), $matchgrades); 85 $mform->addHelpButton('matchgrades', 'matchgrades', 'question'); 86 $mform->setDefault('matchgrades', 'error'); 87 88 $mform->addElement('selectyesno', 'stoponerror', get_string('stoponerror', 'question')); 89 $mform->setDefault('stoponerror', 1); 90 $mform->addHelpButton('stoponerror', 'stoponerror', 'question'); 91 92 // The file to import 93 $mform->addElement('header', 'importfileupload', get_string('importquestions', 'question')); 94 95 $mform->addElement('filepicker', 'newfile', get_string('import')); 96 $mform->addRule('newfile', null, 'required', null, 'client'); 97 98 // Submit button. 99 $mform->addElement('submit', 'submitbutton', get_string('import')); 100 101 // Set a template for the format select elements 102 $renderer = $mform->defaultRenderer(); 103 $template = "{help} {element}\n"; 104 $renderer->setGroupElementTemplate($template, 'format'); 105 } 106 107 /** 108 * Checks that a file has been uploaded, and that it is of a plausible type. 109 * @param array $data the submitted data. 110 * @param array $errors the errors so far. 111 * @return array the updated errors. 112 */ 113 protected function validate_uploaded_file($data, $errors) { 114 if (empty($data['newfile'])) { 115 $errors['newfile'] = get_string('required'); 116 return $errors; 117 } 118 119 $files = $this->get_draft_files('newfile'); 120 if (count($files) < 1) { 121 $errors['newfile'] = get_string('required'); 122 return $errors; 123 } 124 125 if (empty($data['format'])) { 126 $errors['format'] = get_string('required'); 127 return $errors; 128 } 129 130 $formatfile = 'format/' . $data['format'] . '/format.php'; 131 if (!is_readable($formatfile)) { 132 throw new moodle_exception('formatnotfound', 'question', '', $data['format']); 133 } 134 135 require_once($formatfile); 136 137 $classname = 'qformat_' . $data['format']; 138 $qformat = new $classname(); 139 140 $file = reset($files); 141 if (!$qformat->can_import_file($file)) { 142 $a = new stdClass(); 143 $a->actualtype = $file->get_mimetype(); 144 $a->expectedtype = $qformat->mime_type(); 145 $errors['newfile'] = get_string('importwrongfiletype', 'question', $a); 146 } 147 148 return $errors; 149 } 150 151 public function validation($data, $files) { 152 $errors = parent::validation($data, $files); 153 $errors = $this->validate_uploaded_file($data, $errors); 154 return $errors; 155 } 156 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |