adding some strings
[moodle-linuxchix.git] / question / import.php
blob476f524a3d01616c7b80da8d4bde55a9127cffd3
1 <?php // $Id$
2 /**
3 * Import quiz questions into the given category
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
12 require_once("../config.php");
13 require_once("editlib.php" );
14 require_once($CFG->libdir . '/uploadlib.php');
15 require_once($CFG->libdir . '/questionlib.php');
17 list($thispageurl, $courseid, $cmid, $cm, $module, $pagevars) = question_edit_setup(false, false);
19 // get parameters
20 $params = new stdClass;
21 $params->choosefile = optional_param('choosefile','',PARAM_PATH);
22 $catfromfile = optional_param('catfromfile', 0, PARAM_BOOL );
23 $format = optional_param('format','',PARAM_FILE);
24 $params->matchgrades = optional_param('matchgrades','',PARAM_ALPHA);
25 $params->stoponerror = optional_param('stoponerror', 0, PARAM_BOOL);
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');
33 $txt->fromfile = get_string('fromfile','quiz');
34 $txt->importcategory = get_string('importcategory','quiz');
35 $txt->importerror = get_string('importerror','quiz');
36 $txt->importfilearea = get_string('importfilearea','quiz');
37 $txt->importfileupload = get_string('importfileupload','quiz');
38 $txt->importfromthisfile = get_string('importfromthisfile','quiz');
39 $txt->importquestions = get_string("importquestions", "quiz");
40 $txt->matchgrades = get_string('matchgrades','quiz');
41 $txt->matchgradeserror = get_string('matchgradeserror','quiz');
42 $txt->matchgradesnearest = get_string('matchgradesnearest','quiz');
43 $txt->modulename = get_string('modulename','quiz');
44 $txt->modulenameplural = get_string('modulenameplural','quiz');
45 $txt->onlyteachersimport = get_string('onlyteachersimport','quiz');
46 $txt->questions = get_string("questions", "quiz");
47 $txt->quizzes = get_string('modulenameplural', 'quiz');
48 $txt->stoponerror = get_string('stoponerror', 'quiz');
49 $txt->upload = get_string('upload');
50 $txt->uploadproblem = get_string('uploadproblem');
51 $txt->uploadthisfile = get_string('uploadthisfile');
53 // matching options
54 $matchgrades = array();
55 $matchgrades['error'] = $txt->matchgradeserror;
56 $matchgrades['nearest'] = $txt->matchgradesnearest;
60 if (!$category = get_record("question_categories", "id", $pagevars['cat'])) {
61 // if no valid category was given, use the default category
62 print_error('nocategory','quiz');
65 if (!$courseid) { // need to get the course from the chosen category
66 $courseid = $category->course;
69 if (!$course = get_record("course", "id", $courseid)) {
70 error("Invalid course!");
73 require_login($course->id, false);
75 $context = get_context_instance(CONTEXT_COURSE, $course->id);
76 require_capability('moodle/question:import', $context);
78 // ensure the files area exists for this course
79 make_upload_directory( "$course->id" );
82 //==========
83 // PAGE HEADER
84 //==========
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))
89 : "";
90 $navlinks = array();
91 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$course->id", 'type' => 'activity');
92 $navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?cmid={$cm->id}", 'type' => 'title');
93 $navlinks[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title');
94 $navigation = build_navigation($navlinks);
95 print_header_simple($txt->importquestions, '', $navigation, "", "", true, $strupdatemodule);
97 $currenttab = 'edit';
98 $mode = 'import';
99 ${$cm->modname} = $module;
100 include($CFG->dirroot."/mod/$cm->modname/tabs.php");
101 } else {
102 // Print basic page layout.
103 $navlinks = array();
104 $navlinks[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title');
105 $navigation = build_navigation($navlinks);
107 print_header_simple($txt->importquestions, '', $navigation);
108 // print tabs
109 $currenttab = 'import';
110 include('tabs.php');
114 // file upload form sumitted
115 if (!empty($format) and confirm_sesskey() ) {
117 // file checks out ok
118 $fileisgood = false;
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;
127 else {
128 notify($txt->uploadproblem);
130 } else {
131 // must be upload file
132 if (empty($_FILES['newfile'])) {
133 notify( $txt->uploadproblem );
135 else if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) {
136 notify( $txt->uploadproblem );
138 else {
139 $importfile = $_FILES['newfile']['tmp_name'];
140 $fileisgood = true;
144 // process if we are happy file is ok
145 if ($fileisgood) {
147 if (! is_readable("format/$format/format.php")) {
148 error( get_string('formatnotfound','quiz', $format) );
151 require("format.php"); // Parent class
152 require("format/$format/format.php");
154 $classname = "qformat_$format";
155 $qformat = new $classname();
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 );
163 $qformat->setStoponerror( $params->stoponerror );
165 // Do anything before that we need to
166 if (! $qformat->importpreprocess()) {
167 error( $txt->importerror, $thispageurl->out(false, array('category'=>$category->id)));
170 // Process the uploaded file
171 if (! $qformat->importprocess() ) {
172 error( $txt->importerror, $thispageurl->out(false, array('category'=>$category->id)));
175 // In case anything needs to be done after
176 if (! $qformat->importpostprocess()) {
177 error( $txt->importerror, $thispageurl->out(false, array('category'=>$category->id)));
180 echo "<hr />";
181 print_continue("edit.php?".$thispageurl->get_query_string());
182 print_footer($course);
183 exit;
187 /// Print upload form
189 // get list of available import formats
190 $fileformatnames = get_import_export_formats( 'import' );
192 print_heading_with_help($txt->importquestions, "import", "quiz");
194 /// Get all the existing categories now
195 $catmenu = question_category_options($course->id, false, true);
197 //==========
198 // DISPLAY
199 //==========
203 <form id="form" enctype="multipart/form-data" method="post" action="import.php">
204 <fieldset class="invisiblefieldset" style="display: block;">
205 <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
206 <?php echo $thispageurl->hidden_params_out(array(), 3); ?>
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>
217 <tr>
218 <td align="right"><?php echo $txt->fileformat; ?>:</td>
219 <td><?php choose_from_menu($fileformatnames, 'format', 'gift', '');
220 helpbutton("import", $txt->importquestions, 'quiz'); ?></td>
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>
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>
232 </table>
233 <?php
234 print_simple_box_end();
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>
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();
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>
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>
270 </form>
272 <?php
273 print_footer($course);