Accessibility: validation, missing 'alt' attribute, near $strshowallweeks (OU-Bugz...
[moodle.git] / question / import.php
CommitLineData
aca318e1 1<?php // $Id$
2/**
3* Import quiz questions into the given category
4*
5* @version $Id$
6* @author Martin Dougiamas, Howard Miller, and many others.
7* {@link http://moodle.org}
8* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9* @package quiz
10*/
11
12 require_once("../config.php");
aca318e1 13 require_once($CFG->libdir.'/questionlib.php');
cbc87100 14 require_once($CFG->dirroot.'/lib/uploadlib.php');
aca318e1 15
cbc87100 16 // get parameters
17 $params = new stdClass;
18 $params->choosefile = optional_param('choosefile','',PARAM_PATH);
aca318e1 19 $categoryid = optional_param('category', 0, PARAM_INT);
20 $courseid = optional_param('course', 0, PARAM_INT);
5a4bcbc3 21 $format = optional_param('format','',PARAM_FILE);
aca318e1 22
cbc87100 23 // get display strings
24 $txt = new stdClass();
25 $txt->category = get_string('category','quiz');
26 $txt->choosefile = get_string('choosefile','quiz');
27 $txt->file = get_string('file');
28 $txt->fileformat = get_string('fileformat','quiz');
29 $txt->importfilearea = get_string('importfilearea','quiz');
30 $txt->importfileupload = get_string('importfileupload','quiz');
31 $txt->upload = get_string('upload');
32 $txt->uploadproblem = get_string('uploadproblem');
33 $txt->uploadthisfile = get_string('uploadthisfile');
34
aca318e1 35 if (!$categoryid) { // try to get category from modform
36 $showcatmenu = true; // will ensure that user can choose category
37 if (isset($SESSION->modform)) {
38 $categoryid = $SESSION->modform->category;
39 }
40 }
41
dc1f00de 42 if (! $category = get_record("question_categories", "id", $categoryid)) {
aca318e1 43 // if no valid category was given, use the default category
44 if ($courseid) {
062f1125 45 $category = get_default_question_category($courseid);
aca318e1 46 } else {
1e3d6fd8 47 error( get_string('nocategory','quiz') );
aca318e1 48 }
49 }
50
51 if (!$courseid) { // need to get the course from the chosen category
52 $courseid = $category->course;
53 }
54
55 if (! $course = get_record("course", "id", $courseid)) {
56 error("Invalid course!");
57 }
58
59 require_login($course->id, false);
60
61 if (!isteacheredit($course->id)) {
1e3d6fd8 62 error( get_string('onlyteachersimport','quiz') );
aca318e1 63 }
64
65 // ensure the files area exists for this course
66 make_upload_directory( "$course->id" );
67
68 $strimportquestions = get_string("importquestions", "quiz");
69 $strquestions = get_string("questions", "quiz");
70
71 $strquizzes = get_string('modulenameplural', 'quiz');
72 $streditingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz");
73
cbc87100 74 //==========
75 // PAGE HEADER
76 //==========
aca318e1 77
78 if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
79 $strupdatemodule = isteacheredit($course->id)
80 ? update_module_button($SESSION->modform->cmid, $course->id, get_string('modulename', 'quiz'))
81 : "";
82 print_header_simple($strimportquestions, '',
70c01adb 83 "<a href=\"$CFG->wwwroot/mod/quiz/index.php?id=$course->id\">".get_string('modulenameplural', 'quiz').'</a>'.
84 " -> <a href=\"$CFG->wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).'</a>'.
aca318e1 85 ' -> '.$strimportquestions,
86 "", "", true, $strupdatemodule);
87 $currenttab = 'edit';
88 $mode = 'import';
70c01adb 89 include($CFG->dirroot.'/mod/quiz/tabs.php');
aca318e1 90 } else {
2662cf46 91 print_header_simple($strimportquestions, '', $strimportquestions);
567709c4 92 // print tabs
93 $currenttab = 'import';
94 include('tabs.php');
aca318e1 95 }
96
97
cbc87100 98 // file upload form sumitted
99 if (!empty($format) and confirm_sesskey() ) {
aca318e1 100
cbc87100 101 // file checks out ok
102 $fileisgood = false;
aca318e1 103
cbc87100 104 // work out if this is an uploaded file
105 // or one from the filesarea.
106 if (!empty($params->choosefile)) {
107 $importfile = "{$CFG->dataroot}/{$course->id}/{$params->choosefile}";
108 if (file_exists($importfile)) {
109 $fileisgood = true;
110 }
111 else {
112 notify($txt->uploadproblem);
113 }
114 }
115 else {
116 // must be upload file
117 if (empty($_FILES['newfile'])) {
118 notify( $txt->uploadproblem );
119 }
120 else if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) {
121 notify( $txt->uploadproblem );
122 }
123 else {
124 $importfile = $_FILES['newfile']['tmp_name'];
125 $fileisgood = true;
126 }
aca318e1 127 }
aca318e1 128
cbc87100 129 // process if we are happy file is ok
130 if ($fileisgood) {
aca318e1 131
132 if (! is_readable("format/$format/format.php")) {
1e3d6fd8 133 error( get_string('formatnotfound','quiz', $format) );
aca318e1 134 }
135
136 require("format.php"); // Parent class
137 require("format/$format/format.php");
138
f5565b69 139 $classname = "qformat_$format";
140 $qformat = new $classname();
aca318e1 141
f5565b69 142 if (! $qformat->importpreprocess($category,$course)) { // Do anything before that we need to
1e3d6fd8 143 error( get_string('importerror','quiz'),
144 "$CFG->wwwroot/question/import.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 145 }
146
cbc87100 147 if (! $qformat->importprocess($importfile) ) { // Process the uploaded file
1e3d6fd8 148 error( get_string('importerror','quiz'),
149 "$CFG->wwwroot/question/import.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 150 }
151
f5565b69 152 if (! $qformat->importpostprocess()) { // In case anything needs to be done after
1e3d6fd8 153 error( get_string('importerror','quiz'),
154 "$CFG->wwwroot/question/import.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 155 }
156
157 echo "<hr />";
c4c06b67 158 print_continue("edit.php?courseid=$course->id");
aca318e1 159 print_footer($course);
160 exit;
161 }
162 }
163
164 /// Print upload form
165
166 // get list of available import formats
167 $fileformatnames = get_import_export_formats( 'import' );
168
169 print_heading_with_help($strimportquestions, "import", "quiz");
170
171 /// Get all the existing categories now
172 if (isadmin()) { // the admin can import into all categories
dc1f00de 173 if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
aca318e1 174 error("Could not find any question categories!"); // Something is really wrong
175 }
176 } else { // select only the categories to which the teacher has write access
177 $sql = "SELECT c.*
dc1f00de 178 FROM {$CFG->prefix}question_categories AS c,
aca318e1 179 {$CFG->prefix}user_teachers AS t
180 WHERE t.userid = '$USER->id'
181 AND t.course = c.course
182 AND (c.course = '$course->id'
183 OR (c.publish = '1' AND t.editall = '1'))
184 ORDER BY c.parent ASC, c.sortorder ASC, c.name ASC";
185 if (!$categories = get_records_sql($sql)) {
186 error("Could not find any question categories!");
187 }
188 }
189 $categories = add_indented_names($categories);
190 foreach ($categories as $key => $cat) {
191 if ($catcourse = get_record("course", "id", $cat->course)) {
192 if ($cat->publish && $cat->course != $course->id) {
193 $cat->indentedname .= " ($catcourse->shortname)";
194 }
195 $catmenu[$cat->id] = $cat->indentedname;
196 }
197 }
cbc87100 198
199 //==========
200 // DISPLAY
201 //==========
202
203 ?>
204
205 <form name="form" enctype="multipart/form-data" method="post" action="import.php">
206 <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
207 <?php print_simple_box_start("center"); ?>
208 <table cellpadding="5">
209 <tr>
210 <td align="right"><?php echo $txt->category; ?>:</td>
211 <td><?php choose_from_menu($catmenu, "category", $category->id, ""); ?></td>
212 </tr>
213
214 <tr>
215 <td align="right"><?php echo $txt->fileformat; ?>:</td>
216 <td><?php choose_from_menu($fileformatnames, "format", "gift", "");
217 helpbutton("import", $strimportquestions, "quiz"); ?></td>
218 </tr>
219 </table>
220 <?php
221 print_simple_box_end();
222
223 print_simple_box_start('center'); ?>
224 <?php echo $txt->importfileupload; ?>
225 <table cellpadding="5">
226 <tr>
227 <td align="right"><?php echo $txt->upload; ?>:</td>
228 <td><?php upload_print_form_fragment(1,array('newfile'),null,false,null,$course->maxbytes,0,false); ?></td>
229 </tr>
230
231 <tr>
232 <td>&nbsp;</td>
233 <td><input type="submit" name="save" value="<?php echo $txt->uploadthisfile; ?>" /></td>
234 </tr>
235 </table>
236 <?php
237 print_simple_box_end();
238
239 print_simple_box_start('center'); ?>
240 <?php echo $txt->importfilearea; ?>
241 <table cellpadding="5">
242 <tr>
243 <td align="right"><?php echo $txt->file; ?>:</td>
244 <td><input type="text" name="choosefile" size="50" /></td>
245 </tr>
246
247 <tr>
248 <td>&nbsp;</td>
249 <td><?php button_to_popup_window ("/files/index.php?id={$course->id}&amp;choose=form.choosefile",
250 "coursefiles", $txt->choosefile, 500, 750, $txt->choosefile); ?>
251 <input type="submit" name="save" value="<?php echo $txt->uploadthisfile; ?>" /></td>
252 </tr>
253 </table>
254 <?php
255 print_simple_box_end(); ?>
256 </form>
257
258 <?php
aca318e1 259 print_footer($course);
260
261?>