$CFG->tabselectedtofront is out. Part of MDL-8746
[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");
3ed998f3 13 require_once("editlib.php" );
14 require_once($CFG->libdir . '/uploadlib.php');
15 require_once($CFG->libdir . '/questionlib.php');
aca318e1 16
cbc87100 17 // get parameters
18 $params = new stdClass;
19 $params->choosefile = optional_param('choosefile','',PARAM_PATH);
aca318e1 20 $categoryid = optional_param('category', 0, PARAM_INT);
08892d5b 21 $catfromfile = optional_param('catfromfile', 0, PARAM_BOOL );
aca318e1 22 $courseid = optional_param('course', 0, PARAM_INT);
5a4bcbc3 23 $format = optional_param('format','',PARAM_FILE);
76f0a334 24 $params->matchgrades = optional_param('matchgrades','',PARAM_ALPHA);
f3701561 25 $params->stoponerror = optional_param('stoponerror', 0, PARAM_BOOL);
aca318e1 26
cbc87100 27 // get display strings
28 $txt = new stdClass();
29 $txt->category = get_string('category','quiz');
30 $txt->choosefile = get_string('choosefile','quiz');
ced7916c 31 $txt->editingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz");
cbc87100 32 $txt->file = get_string('file');
33 $txt->fileformat = get_string('fileformat','quiz');
08892d5b 34 $txt->fromfile = get_string('fromfile','quiz');
35 $txt->importcategory = get_string('importcategory','quiz');
ced7916c 36 $txt->importerror = get_string('importerror','quiz');
cbc87100 37 $txt->importfilearea = get_string('importfilearea','quiz');
38 $txt->importfileupload = get_string('importfileupload','quiz');
296cb5b5 39 $txt->importfromthisfile = get_string('importfromthisfile','quiz');
ced7916c 40 $txt->importquestions = get_string("importquestions", "quiz");
76f0a334 41 $txt->matchgrades = get_string('matchgrades','quiz');
42 $txt->matchgradeserror = get_string('matchgradeserror','quiz');
43 $txt->matchgradesnearest = get_string('matchgradesnearest','quiz');
ced7916c 44 $txt->modulename = get_string('modulename','quiz');
45 $txt->modulenameplural = get_string('modulenameplural','quiz');
46 $txt->nocategory = get_string('nocategory','quiz');
47 $txt->onlyteachersimport = get_string('onlyteachersimport','quiz');
48 $txt->questions = get_string("questions", "quiz");
49 $txt->quizzes = get_string('modulenameplural', 'quiz');
f3701561 50 $txt->stoponerror = get_string('stoponerror', 'quiz');
cbc87100 51 $txt->upload = get_string('upload');
52 $txt->uploadproblem = get_string('uploadproblem');
53 $txt->uploadthisfile = get_string('uploadthisfile');
54
76f0a334 55 // matching options
56 $matchgrades = array();
57 $matchgrades['error'] = $txt->matchgradeserror;
58 $matchgrades['nearest'] = $txt->matchgradesnearest;
59
0c1e3a14 60 if ($categoryid) { // update category in session variable
61 $SESSION->questioncat = $categoryid;
62 } else { // try to get category from modform
aca318e1 63 $showcatmenu = true; // will ensure that user can choose category
dc454ef4 64 if (isset($SESSION->questioncat)) {
65 $categoryid = $SESSION->questioncat;
aca318e1 66 }
67 }
68
dc1f00de 69 if (! $category = get_record("question_categories", "id", $categoryid)) {
aca318e1 70 // if no valid category was given, use the default category
71 if ($courseid) {
062f1125 72 $category = get_default_question_category($courseid);
aca318e1 73 } else {
ced7916c 74 error( $txt->nocategory );
aca318e1 75 }
76 }
77
78 if (!$courseid) { // need to get the course from the chosen category
79 $courseid = $category->course;
80 }
81
82 if (! $course = get_record("course", "id", $courseid)) {
83 error("Invalid course!");
84 }
85
86 require_login($course->id, false);
87
17d6a25e 88 require_capability('moodle/question:import', get_context_instance(CONTEXT_COURSE, $course->id));
aca318e1 89
90 // ensure the files area exists for this course
91 make_upload_directory( "$course->id" );
92
aca318e1 93
cbc87100 94 //==========
95 // PAGE HEADER
96 //==========
aca318e1 97
98 if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
17d6a25e 99 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id))
ced7916c 100 ? update_module_button($SESSION->modform->cmid, $course->id, $txt->modulename)
aca318e1 101 : "";
ced7916c 102 print_header_simple($txt->importquestions, '',
103 "<a href=\"$CFG->wwwroot/mod/quiz/index.php?id=$course->id\">".$txt->modulenameplural.'</a>'.
70c01adb 104 " -> <a href=\"$CFG->wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).'</a>'.
ced7916c 105 ' -> '.$txt->importquestions,
aca318e1 106 "", "", true, $strupdatemodule);
107 $currenttab = 'edit';
108 $mode = 'import';
70c01adb 109 include($CFG->dirroot.'/mod/quiz/tabs.php');
aca318e1 110 } else {
ced7916c 111 print_header_simple($txt->importquestions, '', $txt->importquestions);
567709c4 112 // print tabs
113 $currenttab = 'import';
114 include('tabs.php');
aca318e1 115 }
116
117
cbc87100 118 // file upload form sumitted
119 if (!empty($format) and confirm_sesskey() ) {
aca318e1 120
cbc87100 121 // file checks out ok
122 $fileisgood = false;
aca318e1 123
cbc87100 124 // work out if this is an uploaded file
125 // or one from the filesarea.
126 if (!empty($params->choosefile)) {
127 $importfile = "{$CFG->dataroot}/{$course->id}/{$params->choosefile}";
128 if (file_exists($importfile)) {
129 $fileisgood = true;
130 }
131 else {
132 notify($txt->uploadproblem);
133 }
134 }
135 else {
136 // must be upload file
137 if (empty($_FILES['newfile'])) {
138 notify( $txt->uploadproblem );
139 }
140 else if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) {
141 notify( $txt->uploadproblem );
142 }
143 else {
144 $importfile = $_FILES['newfile']['tmp_name'];
145 $fileisgood = true;
146 }
aca318e1 147 }
aca318e1 148
cbc87100 149 // process if we are happy file is ok
150 if ($fileisgood) {
aca318e1 151
152 if (! is_readable("format/$format/format.php")) {
1e3d6fd8 153 error( get_string('formatnotfound','quiz', $format) );
aca318e1 154 }
155
156 require("format.php"); // Parent class
157 require("format/$format/format.php");
158
f5565b69 159 $classname = "qformat_$format";
160 $qformat = new $classname();
aca318e1 161
08892d5b 162 // load data into class
163 $qformat->setCategory( $category );
164 $qformat->setCourse( $course );
165 $qformat->setFilename( $importfile );
166 $qformat->setMatchgrades( $params->matchgrades );
167 $qformat->setCatfromfile( $catfromfile );
f3701561 168 $qformat->setStoponerror( $params->stoponerror );
08892d5b 169
f3701561 170 // Do anything before that we need to
171 if (! $qformat->importpreprocess()) {
ced7916c 172 error( $txt->importerror ,
1e3d6fd8 173 "$CFG->wwwroot/question/import.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 174 }
175
f3701561 176 // Process the uploaded file
177 if (! $qformat->importprocess() ) {
ced7916c 178 error( $txt->importerror ,
1e3d6fd8 179 "$CFG->wwwroot/question/import.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 180 }
181
f3701561 182 // In case anything needs to be done after
183 if (! $qformat->importpostprocess()) {
ced7916c 184 error( $txt->importerror ,
1e3d6fd8 185 "$CFG->wwwroot/question/import.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 186 }
187
188 echo "<hr />";
c4c06b67 189 print_continue("edit.php?courseid=$course->id");
aca318e1 190 print_footer($course);
191 exit;
192 }
193 }
194
195 /// Print upload form
196
197 // get list of available import formats
198 $fileformatnames = get_import_export_formats( 'import' );
199
ced7916c 200 print_heading_with_help($txt->importquestions, "import", "quiz");
aca318e1 201
202 /// Get all the existing categories now
b030a630 203 $catmenu = question_category_options($course->id, false, true);
cbc87100 204
205 //==========
206 // DISPLAY
207 //==========
208
209 ?>
210
d2ce367f 211 <form id="form" enctype="multipart/form-data" method="post" action="import.php">
09275894 212 <fieldset class="invisiblefieldset">
213 <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
214 <?php print_simple_box_start("center"); ?>
215 <table cellpadding="5">
216 <tr>
217 <td align="right"><?php echo $txt->category; ?>:</td>
218 <td><?php choose_from_menu($catmenu, "category", $category->id, ""); ?>
219 <?php echo $txt->fromfile; ?>
220 <input name="catfromfile" type="checkbox" />
221 <?php helpbutton('importcategory', $txt->importcategory, 'quiz'); ?></td>
222 </tr>
223
224 <tr>
225 <td align="right"><?php echo $txt->fileformat; ?>:</td>
f3701561 226 <td><?php choose_from_menu($fileformatnames, 'format', 'gift', '');
227 helpbutton("import", $txt->importquestions, 'quiz'); ?></td>
09275894 228 </tr>
229 <tr>
230 <td align="right"><?php echo $txt->matchgrades; ?></td>
231 <td><?php choose_from_menu($matchgrades,'matchgrades',$txt->matchgradeserror,'' );
232 helpbutton('matchgrades', $txt->matchgrades, 'quiz'); ?></td>
233 </tr>
f3701561 234 <tr>
235 <td align="right"><?php echo $txt->stoponerror; ?></td>
236 <td><input name="stoponerror" type="checkbox" checked="checked" />
237 <?php helpbutton('stoponerror', $txt->stoponerror, 'quiz'); ?></td>
238 </tr>
09275894 239 </table>
240 <?php
241 print_simple_box_end();
242
243 print_simple_box_start('center'); ?>
244 <?php echo $txt->importfileupload; ?>
245 <table cellpadding="5">
246 <tr>
247 <td align="right"><?php echo $txt->upload; ?>:</td>
248 <td><?php upload_print_form_fragment(1,array('newfile'),null,false,null,$course->maxbytes,0,false); ?></td>
249 </tr>
250
251 <tr>
252 <td>&nbsp;</td>
253 <td><input type="submit" name="save" value="<?php echo $txt->uploadthisfile; ?>" /></td>
254 </tr>
255 </table>
256 <?php
257 print_simple_box_end();
258
259 print_simple_box_start('center'); ?>
260 <?php echo $txt->importfilearea; ?>
261 <table cellpadding="5">
262 <tr>
263 <td align="right"><?php echo $txt->file; ?>:</td>
264 <td><input type="text" name="choosefile" size="50" /></td>
265 </tr>
266
267 <tr>
268 <td>&nbsp;</td>
269 <td><?php button_to_popup_window ("/files/index.php?id={$course->id}&amp;choose=form.choosefile",
270 "coursefiles", $txt->choosefile, 500, 750, $txt->choosefile); ?>
271 <input type="submit" name="save" value="<?php echo $txt->importfromthisfile; ?>" /></td>
272 </tr>
273 </table>
274 <?php
275 print_simple_box_end(); ?>
276 </fieldset>
cbc87100 277 </form>
278
279 <?php
aca318e1 280 print_footer($course);
281
282?>