merging MOODLE_19_QUESTIONS with HEAD
[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
b72ff476 17 list($thispageurl, $courseid, $cmid, $cm, $module, $pagevars) = question_edit_setup(false, false);
7d43178b 18
cbc87100 19 // get parameters
20 $params = new stdClass;
21 $params->choosefile = optional_param('choosefile','',PARAM_PATH);
08892d5b 22 $catfromfile = optional_param('catfromfile', 0, PARAM_BOOL );
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);
7d43178b 26 $params->category = optional_param( 'category', 0, PARAM_INT );
aca318e1 27
cbc87100 28 // get display strings
29 $txt = new stdClass();
30 $txt->category = get_string('category','quiz');
31 $txt->choosefile = get_string('choosefile','quiz');
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
7d43178b 59 // not sure where $pagevars['cat'] comes from, but it doesn't respect
60 // the user's choice on the form - so this bodge
61 if (empty($params->category)) {
62 $params->category = $pagevars['cat'];
63 }
aca318e1 64
7d43178b 65 if (!$category = get_record("question_categories", "id", $params->category)) {
aca318e1 66 // if no valid category was given, use the default category
7bc26c8f 67 print_error('nocategory','quiz');
aca318e1 68 }
69
97828462 70 // check category is valid (against THIS courseid, before we change it)
7d43178b 71 $validcats = question_category_options( $cmid, false, true );
72 if (!array_key_exists( $params->category, $validcats )) {
97828462 73 print_error( 'invalidcategory', 'quiz' );
aca318e1 74 }
75
7d43178b 76 $localcourseid = $cmid;
97828462 77 $courseid = $category->course;
78
d187f660 79 if (!$course = get_record("course", "id", $courseid)) {
aca318e1 80 error("Invalid course!");
81 }
82
83 require_login($course->id, false);
84
d187f660 85 $context = get_context_instance(CONTEXT_COURSE, $course->id);
86 require_capability('moodle/question:import', $context);
aca318e1 87
88 // ensure the files area exists for this course
89 make_upload_directory( "$course->id" );
90
aca318e1 91
cbc87100 92 //==========
93 // PAGE HEADER
94 //==========
aca318e1 95
b72ff476 96 if ($cm!==null) {
97 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id))
98 ? update_module_button($cm->id, $course->id, get_string('modulename', $cm->modname))
aca318e1 99 : "";
3b27b0fe 100 $navlinks = array();
101 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$course->id", 'type' => 'activity');
102 $navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?cmid={$cm->id}", 'type' => 'title');
103 $navlinks[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title');
104 $navigation = build_navigation($navlinks);
b72ff476 105 print_header_simple($txt->importquestions, '', $navigation, "", "", true, $strupdatemodule);
106
aca318e1 107 $currenttab = 'edit';
108 $mode = 'import';
b72ff476 109 ${$cm->modname} = $module;
110 include($CFG->dirroot."/mod/$cm->modname/tabs.php");
aca318e1 111 } else {
b72ff476 112 // Print basic page layout.
3b27b0fe 113 $navlinks = array();
114 $navlinks[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title');
115 $navigation = build_navigation($navlinks);
b72ff476 116
117 print_header_simple($txt->importquestions, '', $navigation);
567709c4 118 // print tabs
119 $currenttab = 'import';
120 include('tabs.php');
aca318e1 121 }
122
123
cbc87100 124 // file upload form sumitted
125 if (!empty($format) and confirm_sesskey() ) {
aca318e1 126
cbc87100 127 // file checks out ok
128 $fileisgood = false;
aca318e1 129
cbc87100 130 // work out if this is an uploaded file
131 // or one from the filesarea.
132 if (!empty($params->choosefile)) {
133 $importfile = "{$CFG->dataroot}/{$course->id}/{$params->choosefile}";
134 if (file_exists($importfile)) {
135 $fileisgood = true;
136 }
137 else {
138 notify($txt->uploadproblem);
139 }
b72ff476 140 } else {
cbc87100 141 // must be upload file
142 if (empty($_FILES['newfile'])) {
143 notify( $txt->uploadproblem );
144 }
145 else if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) {
146 notify( $txt->uploadproblem );
147 }
148 else {
149 $importfile = $_FILES['newfile']['tmp_name'];
150 $fileisgood = true;
151 }
aca318e1 152 }
aca318e1 153
cbc87100 154 // process if we are happy file is ok
155 if ($fileisgood) {
aca318e1 156
157 if (! is_readable("format/$format/format.php")) {
1e3d6fd8 158 error( get_string('formatnotfound','quiz', $format) );
aca318e1 159 }
160
161 require("format.php"); // Parent class
162 require("format/$format/format.php");
163
f5565b69 164 $classname = "qformat_$format";
165 $qformat = new $classname();
aca318e1 166
08892d5b 167 // load data into class
168 $qformat->setCategory( $category );
169 $qformat->setCourse( $course );
170 $qformat->setFilename( $importfile );
171 $qformat->setMatchgrades( $params->matchgrades );
172 $qformat->setCatfromfile( $catfromfile );
f3701561 173 $qformat->setStoponerror( $params->stoponerror );
08892d5b 174
f3701561 175 // Do anything before that we need to
176 if (! $qformat->importpreprocess()) {
b72ff476 177 error( $txt->importerror, $thispageurl->out(false, array('category'=>$category->id)));
aca318e1 178 }
179
f3701561 180 // Process the uploaded file
181 if (! $qformat->importprocess() ) {
b72ff476 182 error( $txt->importerror, $thispageurl->out(false, array('category'=>$category->id)));
aca318e1 183 }
184
f3701561 185 // In case anything needs to be done after
186 if (! $qformat->importpostprocess()) {
b72ff476 187 error( $txt->importerror, $thispageurl->out(false, array('category'=>$category->id)));
aca318e1 188 }
189
190 echo "<hr />";
b72ff476 191 print_continue("edit.php?".$thispageurl->get_query_string());
aca318e1 192 print_footer($course);
193 exit;
194 }
195 }
196
197 /// Print upload form
198
199 // get list of available import formats
200 $fileformatnames = get_import_export_formats( 'import' );
201
ced7916c 202 print_heading_with_help($txt->importquestions, "import", "quiz");
aca318e1 203
204 /// Get all the existing categories now
b030a630 205 $catmenu = question_category_options($course->id, false, true);
cbc87100 206
207 //==========
208 // DISPLAY
209 //==========
210
211 ?>
212
d2ce367f 213 <form id="form" enctype="multipart/form-data" method="post" action="import.php">
d187f660 214 <fieldset class="invisiblefieldset" style="display: block;">
09275894 215 <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
a783cec2 216 <input type="hidden" name="courseid" value="<?php echo $localcourseid ?>" />
b72ff476 217 <?php echo $thispageurl->hidden_params_out(array(), 3); ?>
09275894 218 <?php print_simple_box_start("center"); ?>
219 <table cellpadding="5">
220 <tr>
221 <td align="right"><?php echo $txt->category; ?>:</td>
222 <td><?php choose_from_menu($catmenu, "category", $category->id, ""); ?>
223 <?php echo $txt->fromfile; ?>
224 <input name="catfromfile" type="checkbox" />
225 <?php helpbutton('importcategory', $txt->importcategory, 'quiz'); ?></td>
226 </tr>
227
228 <tr>
229 <td align="right"><?php echo $txt->fileformat; ?>:</td>
f3701561 230 <td><?php choose_from_menu($fileformatnames, 'format', 'gift', '');
231 helpbutton("import", $txt->importquestions, 'quiz'); ?></td>
09275894 232 </tr>
233 <tr>
234 <td align="right"><?php echo $txt->matchgrades; ?></td>
235 <td><?php choose_from_menu($matchgrades,'matchgrades',$txt->matchgradeserror,'' );
236 helpbutton('matchgrades', $txt->matchgrades, 'quiz'); ?></td>
237 </tr>
f3701561 238 <tr>
239 <td align="right"><?php echo $txt->stoponerror; ?></td>
240 <td><input name="stoponerror" type="checkbox" checked="checked" />
241 <?php helpbutton('stoponerror', $txt->stoponerror, 'quiz'); ?></td>
242 </tr>
09275894 243 </table>
244 <?php
245 print_simple_box_end();
246
247 print_simple_box_start('center'); ?>
248 <?php echo $txt->importfileupload; ?>
249 <table cellpadding="5">
250 <tr>
251 <td align="right"><?php echo $txt->upload; ?>:</td>
252 <td><?php upload_print_form_fragment(1,array('newfile'),null,false,null,$course->maxbytes,0,false); ?></td>
253 </tr>
254
255 <tr>
256 <td>&nbsp;</td>
257 <td><input type="submit" name="save" value="<?php echo $txt->uploadthisfile; ?>" /></td>
258 </tr>
259 </table>
260 <?php
261 print_simple_box_end();
262
263 print_simple_box_start('center'); ?>
264 <?php echo $txt->importfilearea; ?>
265 <table cellpadding="5">
266 <tr>
267 <td align="right"><?php echo $txt->file; ?>:</td>
268 <td><input type="text" name="choosefile" size="50" /></td>
269 </tr>
270
271 <tr>
272 <td>&nbsp;</td>
273 <td><?php button_to_popup_window ("/files/index.php?id={$course->id}&amp;choose=form.choosefile",
274 "coursefiles", $txt->choosefile, 500, 750, $txt->choosefile); ?>
275 <input type="submit" name="save" value="<?php echo $txt->importfromthisfile; ?>" /></td>
276 </tr>
277 </table>
278 <?php
279 print_simple_box_end(); ?>
280 </fieldset>
cbc87100 281 </form>
282
283 <?php
aca318e1 284 print_footer($course);
285
286?>