MDL-20636 Question import, support for files in hints.
[moodle.git] / question / import_form.php
CommitLineData
aeb15530 1<?php
515ed4c3 2
f7c1dfaf
TH
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Defines the import questions form.
20 *
21 * @package moodlecore
22 * @subpackage questionbank
23 * @copyright 2007 Jamie Pratt me@jamiep.org
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
a17b297d 28defined('MOODLE_INTERNAL') || die();
bfebaf64 29
f7c1dfaf
TH
30require_once($CFG->libdir . '/formslib.php');
31
515ed4c3 32
f7c1dfaf
TH
33/**
34 * Form to import questions into the question bank.
35 *
36 * @copyright 2007 Jamie Pratt me@jamiep.org
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
515ed4c3 39class question_import_form extends moodleform {
40
41 function definition() {
42 global $COURSE;
f7c1dfaf 43 $mform = $this->_form;
515ed4c3 44
f7c1dfaf
TH
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'));
45b68eef 50
45b68eef 51 $fileformatnames = get_import_export_formats('import');
f7c1dfaf 52 $radioarray = array();
a387e8ac 53 $i = 0 ;
f94902db 54 foreach ($fileformatnames as $shortname => $fileformatname) {
a387e8ac 55 $currentgrp1 = array();
f7c1dfaf
TH
56 $currentgrp1[] = $mform->createElement('radio', 'format', '', $fileformatname, $shortname);
57 $mform->addGroup($currentgrp1, "formathelp[$i]", '', array('<br />'), false);
58 $mform->addHelpButton("formathelp[$i]", $shortname, 'qformat_' . $shortname);
a387e8ac 59 $i++ ;
45b68eef 60 }
f7c1dfaf
TH
61 $mform->addRule("formathelp[0]", null, 'required', null, 'client');
62
63 // Import options.
515ed4c3 64 $mform->addElement('header','general', get_string('general', 'form'));
65
5fcefc97 66 $mform->addElement('questioncategory', 'category', get_string('importcategory', 'question'), compact('contexts'));
515ed4c3 67 $mform->setDefault('category', $defaultcategory);
5fcefc97 68 $mform->addHelpButton('category', 'importcategory', 'question');
515ed4c3 69
70 $categorygroup = array();
f7c1dfaf
TH
71 $categorygroup[] = $mform->createElement('checkbox', 'catfromfile', '', get_string('getcategoryfromfile', 'question'));
72 $categorygroup[] = $mform->createElement('checkbox', 'contextfromfile', '', get_string('getcontextfromfile', 'question'));
515ed4c3 73 $mform->addGroup($categorygroup, 'categorygroup', '', '', false);
74 $mform->disabledIf('categorygroup', 'catfromfile', 'notchecked');
75 $mform->setDefault('catfromfile', 1);
76 $mform->setDefault('contextfromfile', 1);
77
515ed4c3 78 $matchgrades = array();
f7c1dfaf
TH
79 $matchgrades['error'] = get_string('matchgradeserror', 'question');
80 $matchgrades['nearest'] = get_string('matchgradesnearest', 'question');
5fcefc97
DM
81 $mform->addElement('select', 'matchgrades', get_string('matchgrades', 'question'), $matchgrades);
82 $mform->addHelpButton('matchgrades', 'matchgrades', 'question');
515ed4c3 83 $mform->setDefault('matchgrades', 'error');
84
5fcefc97 85 $mform->addElement('selectyesno', 'stoponerror', get_string('stoponerror', 'question'));
515ed4c3 86 $mform->setDefault('stoponerror', 1);
5fcefc97 87 $mform->addHelpButton('stoponerror', 'stoponerror', 'question');
15b83581 88
f7c1dfaf
TH
89 // The file to import
90 $mform->addElement('header', 'importfileupload', get_string('importquestions', 'question'));
91
92 $mform->addElement('filepicker', 'newfile', get_string('import'));
93 $mform->addRule('newfile', null, 'required', null, 'client');
94
95 // Submit button.
96 $mform->addElement('submit', 'submitbutton', get_string('import'));
97
98 // Set a template for the format select elements
99 $renderer = $mform->defaultRenderer();
79e3dd8e 100 $template = "{help} {element}\n";
101 $renderer->setGroupElementTemplate($template, 'format');
515ed4c3 102 }
d1b7e03d 103
f7c1dfaf
TH
104 /**
105 * Checks that a file has been uploaded, and that it is of a plausible type.
106 * @param array $data the submitted data.
107 * @param array $errors the errors so far.
108 * @return array the updated errors.
109 */
110 protected function validate_uploaded_file($data, $errors) {
111 if (empty($data['newfile'])) {
112 $errors['newfile'] = get_string('required');
d1b7e03d
TH
113 return $errors;
114 }
f7c1dfaf
TH
115
116 $files = $this->get_draft_files('newfile');
117 if (count($files) < 1) {
118 $errors['newfile'] = get_string('required');
119 return $errors;
d1b7e03d 120 }
f7c1dfaf
TH
121
122 $formatfile = 'format/' . $data['format'] . '/format.php';
123 if (!is_readable($formatfile)) {
5e8a85aa 124 throw new moodle_exception('formatnotfound', 'question', '', $data['format']);
f7c1dfaf
TH
125 }
126
127 require_once($formatfile);
128
129 $classname = 'qformat_' . $data['format'];
130 $qformat = new $classname();
131
132 $file = reset($files);
133 if ($file->get_mimetype() != $qformat->mime_type()) {
134 $a = new stdClass();
135 $a->actualtype = $file->get_mimetype();
136 $a->expectedtype = $qformat->mime_type();
137 $errors['newfile'] = get_string('importwrongfiletype', 'question', $a);
d1b7e03d 138 }
f7c1dfaf 139
d1b7e03d
TH
140 return $errors;
141 }
aeb15530 142
f7c1dfaf
TH
143 public function validation($data, $files) {
144 $errors = parent::validation($data, $files);
145 $errors = $this->validate_uploaded_file($data, $errors);
146 return $errors;
147 }
148}