Merged branch 'MDL-27692_master' of git://github.com/MaryE/moodle.git
[moodle.git] / question / import_form.php
CommitLineData
aeb15530 1<?php
f7c1dfaf
TH
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
a17b297d 27defined('MOODLE_INTERNAL') || die();
bfebaf64 28
f7c1dfaf
TH
29require_once($CFG->libdir . '/formslib.php');
30
515ed4c3 31
f7c1dfaf
TH
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 */
515ed4c3 38class question_import_form extends moodleform {
39
c7df5006 40 protected function definition() {
515ed4c3 41 global $COURSE;
f7c1dfaf 42 $mform = $this->_form;
515ed4c3 43
f7c1dfaf
TH
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'));
45b68eef 49
45b68eef 50 $fileformatnames = get_import_export_formats('import');
f7c1dfaf 51 $radioarray = array();
a387e8ac 52 $i = 0 ;
f94902db 53 foreach ($fileformatnames as $shortname => $fileformatname) {
a387e8ac 54 $currentgrp1 = array();
f7c1dfaf
TH
55 $currentgrp1[] = $mform->createElement('radio', 'format', '', $fileformatname, $shortname);
56 $mform->addGroup($currentgrp1, "formathelp[$i]", '', array('<br />'), false);
57 $mform->addHelpButton("formathelp[$i]", $shortname, 'qformat_' . $shortname);
a387e8ac 58 $i++ ;
45b68eef 59 }
f7c1dfaf
TH
60 $mform->addRule("formathelp[0]", null, 'required', null, 'client');
61
62 // Import options.
515ed4c3 63 $mform->addElement('header','general', get_string('general', 'form'));
64
5fcefc97 65 $mform->addElement('questioncategory', 'category', get_string('importcategory', 'question'), compact('contexts'));
515ed4c3 66 $mform->setDefault('category', $defaultcategory);
5fcefc97 67 $mform->addHelpButton('category', 'importcategory', 'question');
515ed4c3 68
69 $categorygroup = array();
f7c1dfaf
TH
70 $categorygroup[] = $mform->createElement('checkbox', 'catfromfile', '', get_string('getcategoryfromfile', 'question'));
71 $categorygroup[] = $mform->createElement('checkbox', 'contextfromfile', '', get_string('getcontextfromfile', 'question'));
515ed4c3 72 $mform->addGroup($categorygroup, 'categorygroup', '', '', false);
73 $mform->disabledIf('categorygroup', 'catfromfile', 'notchecked');
74 $mform->setDefault('catfromfile', 1);
75 $mform->setDefault('contextfromfile', 1);
76
515ed4c3 77 $matchgrades = array();
f7c1dfaf
TH
78 $matchgrades['error'] = get_string('matchgradeserror', 'question');
79 $matchgrades['nearest'] = get_string('matchgradesnearest', 'question');
5fcefc97
DM
80 $mform->addElement('select', 'matchgrades', get_string('matchgrades', 'question'), $matchgrades);
81 $mform->addHelpButton('matchgrades', 'matchgrades', 'question');
515ed4c3 82 $mform->setDefault('matchgrades', 'error');
83
5fcefc97 84 $mform->addElement('selectyesno', 'stoponerror', get_string('stoponerror', 'question'));
515ed4c3 85 $mform->setDefault('stoponerror', 1);
5fcefc97 86 $mform->addHelpButton('stoponerror', 'stoponerror', 'question');
15b83581 87
f7c1dfaf
TH
88 // The file to import
89 $mform->addElement('header', 'importfileupload', get_string('importquestions', 'question'));
90
91 $mform->addElement('filepicker', 'newfile', get_string('import'));
92 $mform->addRule('newfile', null, 'required', null, 'client');
93
94 // Submit button.
95 $mform->addElement('submit', 'submitbutton', get_string('import'));
96
97 // Set a template for the format select elements
98 $renderer = $mform->defaultRenderer();
79e3dd8e 99 $template = "{help} {element}\n";
100 $renderer->setGroupElementTemplate($template, 'format');
515ed4c3 101 }
d1b7e03d 102
f7c1dfaf
TH
103 /**
104 * Checks that a file has been uploaded, and that it is of a plausible type.
105 * @param array $data the submitted data.
106 * @param array $errors the errors so far.
107 * @return array the updated errors.
108 */
109 protected function validate_uploaded_file($data, $errors) {
110 if (empty($data['newfile'])) {
111 $errors['newfile'] = get_string('required');
d1b7e03d
TH
112 return $errors;
113 }
f7c1dfaf
TH
114
115 $files = $this->get_draft_files('newfile');
116 if (count($files) < 1) {
117 $errors['newfile'] = get_string('required');
118 return $errors;
d1b7e03d 119 }
f7c1dfaf
TH
120
121 $formatfile = 'format/' . $data['format'] . '/format.php';
122 if (!is_readable($formatfile)) {
5e8a85aa 123 throw new moodle_exception('formatnotfound', 'question', '', $data['format']);
f7c1dfaf
TH
124 }
125
126 require_once($formatfile);
127
128 $classname = 'qformat_' . $data['format'];
129 $qformat = new $classname();
130
131 $file = reset($files);
132 if ($file->get_mimetype() != $qformat->mime_type()) {
133 $a = new stdClass();
134 $a->actualtype = $file->get_mimetype();
135 $a->expectedtype = $qformat->mime_type();
136 $errors['newfile'] = get_string('importwrongfiletype', 'question', $a);
d1b7e03d 137 }
f7c1dfaf 138
d1b7e03d
TH
139 return $errors;
140 }
aeb15530 141
f7c1dfaf
TH
142 public function validation($data, $files) {
143 $errors = parent::validation($data, $files);
144 $errors = $this->validate_uploaded_file($data, $errors);
145 return $errors;
146 }
147}