|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once($CFG->libdir . '/formslib.php'); 00030 00031 00038 class question_import_form extends moodleform { 00039 00040 protected function definition() { 00041 global $COURSE; 00042 $mform = $this->_form; 00043 00044 $defaultcategory = $this->_customdata['defaultcategory']; 00045 $contexts = $this->_customdata['contexts']; 00046 00047 // Choice of import format, with help icons. 00048 $mform->addElement('header', 'fileformat', get_string('fileformat', 'question')); 00049 00050 $fileformatnames = get_import_export_formats('import'); 00051 $radioarray = array(); 00052 $i = 0 ; 00053 foreach ($fileformatnames as $shortname => $fileformatname) { 00054 $currentgrp1 = array(); 00055 $currentgrp1[] = $mform->createElement('radio', 'format', '', $fileformatname, $shortname); 00056 $mform->addGroup($currentgrp1, "formathelp[$i]", ' ', array('<br />'), false); 00057 00058 if (get_string_manager()->string_exists('pluginname_help', 'qformat_' . $shortname)) { 00059 $mform->addHelpButton("formathelp[$i]", 'pluginname', 'qformat_' . $shortname); 00060 } 00061 00062 $i++ ; 00063 } 00064 $mform->addRule("formathelp[0]", null, 'required', null, 'client'); 00065 00066 // Import options. 00067 $mform->addElement('header','general', get_string('general', 'form')); 00068 00069 $mform->addElement('questioncategory', 'category', get_string('importcategory', 'question'), compact('contexts')); 00070 $mform->setDefault('category', $defaultcategory); 00071 $mform->addHelpButton('category', 'importcategory', 'question'); 00072 00073 $categorygroup = array(); 00074 $categorygroup[] = $mform->createElement('checkbox', 'catfromfile', '', get_string('getcategoryfromfile', 'question')); 00075 $categorygroup[] = $mform->createElement('checkbox', 'contextfromfile', '', get_string('getcontextfromfile', 'question')); 00076 $mform->addGroup($categorygroup, 'categorygroup', '', '', false); 00077 $mform->disabledIf('categorygroup', 'catfromfile', 'notchecked'); 00078 $mform->setDefault('catfromfile', 1); 00079 $mform->setDefault('contextfromfile', 1); 00080 00081 $matchgrades = array(); 00082 $matchgrades['error'] = get_string('matchgradeserror', 'question'); 00083 $matchgrades['nearest'] = get_string('matchgradesnearest', 'question'); 00084 $mform->addElement('select', 'matchgrades', get_string('matchgrades', 'question'), $matchgrades); 00085 $mform->addHelpButton('matchgrades', 'matchgrades', 'question'); 00086 $mform->setDefault('matchgrades', 'error'); 00087 00088 $mform->addElement('selectyesno', 'stoponerror', get_string('stoponerror', 'question')); 00089 $mform->setDefault('stoponerror', 1); 00090 $mform->addHelpButton('stoponerror', 'stoponerror', 'question'); 00091 00092 // The file to import 00093 $mform->addElement('header', 'importfileupload', get_string('importquestions', 'question')); 00094 00095 $mform->addElement('filepicker', 'newfile', get_string('import')); 00096 $mform->addRule('newfile', null, 'required', null, 'client'); 00097 00098 // Submit button. 00099 $mform->addElement('submit', 'submitbutton', get_string('import')); 00100 00101 // Set a template for the format select elements 00102 $renderer = $mform->defaultRenderer(); 00103 $template = "{help} {element}\n"; 00104 $renderer->setGroupElementTemplate($template, 'format'); 00105 } 00106 00113 protected function validate_uploaded_file($data, $errors) { 00114 if (empty($data['newfile'])) { 00115 $errors['newfile'] = get_string('required'); 00116 return $errors; 00117 } 00118 00119 $files = $this->get_draft_files('newfile'); 00120 if (count($files) < 1) { 00121 $errors['newfile'] = get_string('required'); 00122 return $errors; 00123 } 00124 00125 $formatfile = 'format/' . $data['format'] . '/format.php'; 00126 if (!is_readable($formatfile)) { 00127 throw new moodle_exception('formatnotfound', 'question', '', $data['format']); 00128 } 00129 00130 require_once($formatfile); 00131 00132 $classname = 'qformat_' . $data['format']; 00133 $qformat = new $classname(); 00134 00135 $file = reset($files); 00136 if ($file->get_mimetype() != $qformat->mime_type()) { 00137 $a = new stdClass(); 00138 $a->actualtype = $file->get_mimetype(); 00139 $a->expectedtype = $qformat->mime_type(); 00140 $errors['newfile'] = get_string('importwrongfiletype', 'question', $a); 00141 } 00142 00143 return $errors; 00144 } 00145 00146 public function validation($data, $files) { 00147 $errors = parent::validation($data, $files); 00148 $errors = $this->validate_uploaded_file($data, $errors); 00149 return $errors; 00150 } 00151 }