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