MDL-20636 Fix @package names and PHPdoc layout.
[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
bfebaf64
MD
28if (!defined('MOODLE_INTERNAL')) {
29 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
30}
31
f7c1dfaf
TH
32require_once($CFG->libdir . '/formslib.php');
33
515ed4c3 34
f7c1dfaf
TH
35/**
36 * Form to import questions into the question bank.
37 *
38 * @copyright 2007 Jamie Pratt me@jamiep.org
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
515ed4c3 41class question_import_form extends moodleform {
42
43 function definition() {
44 global $COURSE;
f7c1dfaf 45 $mform = $this->_form;
515ed4c3 46
f7c1dfaf
TH
47 $defaultcategory = $this->_customdata['defaultcategory'];
48 $contexts = $this->_customdata['contexts'];
49
50 // Choice of import format, with help icons.
51 $mform->addElement('header', 'fileformat', get_string('fileformat', 'question'));
45b68eef 52
45b68eef 53 $fileformatnames = get_import_export_formats('import');
f7c1dfaf 54 $radioarray = array();
a387e8ac 55 $i = 0 ;
f94902db 56 foreach ($fileformatnames as $shortname => $fileformatname) {
a387e8ac 57 $currentgrp1 = array();
f7c1dfaf
TH
58 $currentgrp1[] = $mform->createElement('radio', 'format', '', $fileformatname, $shortname);
59 $mform->addGroup($currentgrp1, "formathelp[$i]", '', array('<br />'), false);
60 $mform->addHelpButton("formathelp[$i]", $shortname, 'qformat_' . $shortname);
a387e8ac 61 $i++ ;
45b68eef 62 }
f7c1dfaf
TH
63 $mform->addRule("formathelp[0]", null, 'required', null, 'client');
64
65 // Import options.
515ed4c3 66 $mform->addElement('header','general', get_string('general', 'form'));
67
5fcefc97 68 $mform->addElement('questioncategory', 'category', get_string('importcategory', 'question'), compact('contexts'));
515ed4c3 69 $mform->setDefault('category', $defaultcategory);
5fcefc97 70 $mform->addHelpButton('category', 'importcategory', 'question');
515ed4c3 71
72 $categorygroup = array();
f7c1dfaf
TH
73 $categorygroup[] = $mform->createElement('checkbox', 'catfromfile', '', get_string('getcategoryfromfile', 'question'));
74 $categorygroup[] = $mform->createElement('checkbox', 'contextfromfile', '', get_string('getcontextfromfile', 'question'));
515ed4c3 75 $mform->addGroup($categorygroup, 'categorygroup', '', '', false);
76 $mform->disabledIf('categorygroup', 'catfromfile', 'notchecked');
77 $mform->setDefault('catfromfile', 1);
78 $mform->setDefault('contextfromfile', 1);
79
515ed4c3 80 $matchgrades = array();
f7c1dfaf
TH
81 $matchgrades['error'] = get_string('matchgradeserror', 'question');
82 $matchgrades['nearest'] = get_string('matchgradesnearest', 'question');
5fcefc97
DM
83 $mform->addElement('select', 'matchgrades', get_string('matchgrades', 'question'), $matchgrades);
84 $mform->addHelpButton('matchgrades', 'matchgrades', 'question');
515ed4c3 85 $mform->setDefault('matchgrades', 'error');
86
5fcefc97 87 $mform->addElement('selectyesno', 'stoponerror', get_string('stoponerror', 'question'));
515ed4c3 88 $mform->setDefault('stoponerror', 1);
5fcefc97 89 $mform->addHelpButton('stoponerror', 'stoponerror', 'question');
15b83581 90
f7c1dfaf
TH
91 // The file to import
92 $mform->addElement('header', 'importfileupload', get_string('importquestions', 'question'));
93
94 $mform->addElement('filepicker', 'newfile', get_string('import'));
95 $mform->addRule('newfile', null, 'required', null, 'client');
96
97 // Submit button.
98 $mform->addElement('submit', 'submitbutton', get_string('import'));
99
100 // Set a template for the format select elements
101 $renderer = $mform->defaultRenderer();
79e3dd8e 102 $template = "{help} {element}\n";
103 $renderer->setGroupElementTemplate($template, 'format');
515ed4c3 104 }
d1b7e03d 105
f7c1dfaf
TH
106 /**
107 * Checks that a file has been uploaded, and that it is of a plausible type.
108 * @param array $data the submitted data.
109 * @param array $errors the errors so far.
110 * @return array the updated errors.
111 */
112 protected function validate_uploaded_file($data, $errors) {
113 if (empty($data['newfile'])) {
114 $errors['newfile'] = get_string('required');
d1b7e03d
TH
115 return $errors;
116 }
f7c1dfaf
TH
117
118 $files = $this->get_draft_files('newfile');
119 if (count($files) < 1) {
120 $errors['newfile'] = get_string('required');
121 return $errors;
d1b7e03d 122 }
f7c1dfaf
TH
123
124 $formatfile = 'format/' . $data['format'] . '/format.php';
125 if (!is_readable($formatfile)) {
126 throw new moodle_exception('formatnotfound', 'quiz', '', $data['format']);
127 }
128
129 require_once($formatfile);
130
131 $classname = 'qformat_' . $data['format'];
132 $qformat = new $classname();
133
134 $file = reset($files);
135 if ($file->get_mimetype() != $qformat->mime_type()) {
136 $a = new stdClass();
137 $a->actualtype = $file->get_mimetype();
138 $a->expectedtype = $qformat->mime_type();
139 $errors['newfile'] = get_string('importwrongfiletype', 'question', $a);
d1b7e03d 140 }
f7c1dfaf 141
d1b7e03d
TH
142 return $errors;
143 }
aeb15530 144
f7c1dfaf
TH
145 public function validation($data, $files) {
146 $errors = parent::validation($data, $files);
147 $errors = $this->validate_uploaded_file($data, $errors);
148 return $errors;
149 }
150}