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