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);
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');
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" );
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
))
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);
99 $
{$cm->modname
} = $module;
100 include($CFG->dirroot
."/mod/$cm->modname/tabs.php");
102 // Print basic page layout.
104 $navlinks[] = array('name' => $txt->importquestions
, 'link' => '', 'type' => 'title');
105 $navigation = build_navigation($navlinks);
107 print_header_simple($txt->importquestions
, '', $navigation);
109 $currenttab = 'import';
114 // file upload form sumitted
115 if (!empty($format) and confirm_sesskey() ) {
117 // file checks out ok
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)) {
128 notify($txt->uploadproblem
);
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
);
139 $importfile = $_FILES['newfile']['tmp_name'];
144 // process if we are happy file is ok
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
)));
181 print_continue("edit.php?".$thispageurl->get_query_string());
182 print_footer($course);
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);
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">
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
>
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
>
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
>
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
>
234 print_simple_box_end();
236 print_simple_box_start('center'); ?
>
237 <?php
echo $txt->importfileupload
; ?
>
238 <table cellpadding
="5">
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
>
246 <td
><input type
="submit" name
="save" value
="<?php echo $txt->uploadthisfile; ?>" /></td
>
250 print_simple_box_end();
252 print_simple_box_start('center'); ?
>
253 <?php
echo $txt->importfilearea
; ?
>
254 <table cellpadding
="5">
256 <td align
="right"><?php
echo $txt->file
; ?
>:</td
>
257 <td
><input type
="text" name
="choosefile" size
="50" /></td
>
262 <td
><?php
button_to_popup_window ("/files/index.php?id={$course->id}&choose=form.choosefile",
263 "coursefiles", $txt->choosefile
, 500, 750, $txt->choosefile
); ?
>
264 <input type
="submit" name
="save" value
="<?php echo $txt->importfromthisfile; ?>" /></td
>
268 print_simple_box_end(); ?
>
273 print_footer($course);