forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.php
292 lines (251 loc) · 11.3 KB
/
import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php // $Id$
/**
* Import quiz questions into the given category
*
* @version $Id$
* @author Martin Dougiamas, Howard Miller, and many others.
* {@link http://moodle.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package quiz
*/
require_once("../config.php");
require_once( "editlib.php" );
require_once($CFG->dirroot.'/lib/uploadlib.php');
// get parameters
$params = new stdClass;
$params->choosefile = optional_param('choosefile','',PARAM_PATH);
$categoryid = optional_param('category', 0, PARAM_INT);
$catfromfile = optional_param('catfromfile', 0, PARAM_BOOL );
$courseid = optional_param('course', 0, PARAM_INT);
$format = optional_param('format','',PARAM_FILE);
$params->matchgrades = optional_param('matchgrades','',PARAM_ALPHA);
// get display strings
$txt = new stdClass();
$txt->category = get_string('category','quiz');
$txt->choosefile = get_string('choosefile','quiz');
$txt->editingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz");
$txt->file = get_string('file');
$txt->fileformat = get_string('fileformat','quiz');
$txt->fromfile = get_string('fromfile','quiz');
$txt->importcategory = get_string('importcategory','quiz');
$txt->importerror = get_string('importerror','quiz');
$txt->importfilearea = get_string('importfilearea','quiz');
$txt->importfileupload = get_string('importfileupload','quiz');
$txt->importfromthisfile = get_string('importfromthisfile','quiz');
$txt->importquestions = get_string("importquestions", "quiz");
$txt->matchgrades = get_string('matchgrades','quiz');
$txt->matchgradeserror = get_string('matchgradeserror','quiz');
$txt->matchgradesnearest = get_string('matchgradesnearest','quiz');
$txt->modulename = get_string('modulename','quiz');
$txt->modulenameplural = get_string('modulenameplural','quiz');
$txt->nocategory = get_string('nocategory','quiz');
$txt->onlyteachersimport = get_string('onlyteachersimport','quiz');
$txt->questions = get_string("questions", "quiz");
$txt->quizzes = get_string('modulenameplural', 'quiz');
$txt->upload = get_string('upload');
$txt->uploadproblem = get_string('uploadproblem');
$txt->uploadthisfile = get_string('uploadthisfile');
// matching options
$matchgrades = array();
$matchgrades['error'] = $txt->matchgradeserror;
$matchgrades['nearest'] = $txt->matchgradesnearest;
if ($categoryid) { // update category in session variable
$SESSION->questioncat = $categoryid;
} else { // try to get category from modform
$showcatmenu = true; // will ensure that user can choose category
if (isset($SESSION->questioncat)) {
$categoryid = $SESSION->questioncat;
}
}
if (! $category = get_record("question_categories", "id", $categoryid)) {
// if no valid category was given, use the default category
if ($courseid) {
$category = get_default_question_category($courseid);
} else {
error( $txt->nocategory );
}
}
if (!$courseid) { // need to get the course from the chosen category
$courseid = $category->course;
}
if (! $course = get_record("course", "id", $courseid)) {
error("Invalid course!");
}
require_login($course->id, false);
require_capability('moodle/question:import', get_context_instance(CONTEXT_COURSE, $course->id));
// ensure the files area exists for this course
make_upload_directory( "$course->id" );
//==========
// PAGE HEADER
//==========
if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
$strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id))
? update_module_button($SESSION->modform->cmid, $course->id, $txt->modulename)
: "";
print_header_simple($txt->importquestions, '',
"<a href=\"$CFG->wwwroot/mod/quiz/index.php?id=$course->id\">".$txt->modulenameplural.'</a>'.
" -> <a href=\"$CFG->wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).'</a>'.
' -> '.$txt->importquestions,
"", "", true, $strupdatemodule);
$currenttab = 'edit';
$mode = 'import';
include($CFG->dirroot.'/mod/quiz/tabs.php');
} else {
print_header_simple($txt->importquestions, '', $txt->importquestions);
// print tabs
$currenttab = 'import';
include('tabs.php');
}
// file upload form sumitted
if (!empty($format) and confirm_sesskey() ) {
// file checks out ok
$fileisgood = false;
// work out if this is an uploaded file
// or one from the filesarea.
if (!empty($params->choosefile)) {
$importfile = "{$CFG->dataroot}/{$course->id}/{$params->choosefile}";
if (file_exists($importfile)) {
$fileisgood = true;
}
else {
notify($txt->uploadproblem);
}
}
else {
// must be upload file
if (empty($_FILES['newfile'])) {
notify( $txt->uploadproblem );
}
else if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) {
notify( $txt->uploadproblem );
}
else {
$importfile = $_FILES['newfile']['tmp_name'];
$fileisgood = true;
}
}
// process if we are happy file is ok
if ($fileisgood) {
if (! is_readable("format/$format/format.php")) {
error( get_string('formatnotfound','quiz', $format) );
}
require("format.php"); // Parent class
require("format/$format/format.php");
$classname = "qformat_$format";
$qformat = new $classname();
// load data into class
$qformat->setCategory( $category );
$qformat->setCourse( $course );
$qformat->setFilename( $importfile );
$qformat->setMatchgrades( $params->matchgrades );
$qformat->setCatfromfile( $catfromfile );
if (! $qformat->importpreprocess()) { // Do anything before that we need to
error( $txt->importerror ,
"$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id");
}
if (! $qformat->importprocess() ) { // Process the uploaded file
error( $txt->importerror ,
"$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id");
}
if (! $qformat->importpostprocess()) { // In case anything needs to be done after
error( $txt->importerror ,
"$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id");
}
echo "<hr />";
print_continue("edit.php?courseid=$course->id");
print_footer($course);
exit;
}
}
/// Print upload form
// get list of available import formats
$fileformatnames = get_import_export_formats( 'import' );
print_heading_with_help($txt->importquestions, "import", "quiz");
/// Get all the existing categories now
if (has_capability('moodle/question:manage', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // the admin can import into all categories
if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
error("Could not find any question categories!"); // Something is really wrong
}
} else { // select only the categories to which the teacher has write access
$cats = get_records('question_categories');
$categories = array();
foreach ($cats as $cat) {
if (has_capability('moodle/question:managecategory', get_context_instance(CONTEXT_COURSE, $cat->course))) {
$categories[] = $cat;
}
}
if (empty($categories)) {
error("Could not find any question categories!");
}
}
$categories = add_indented_names($categories);
foreach ($categories as $key => $cat) {
if ($catcourse = get_record("course", "id", $cat->course)) {
if ($cat->publish && $cat->course != $course->id) {
$cat->indentedname .= " ($catcourse->shortname)";
}
$catmenu[$cat->id] = $cat->indentedname;
}
}
//==========
// DISPLAY
//==========
?>
<form name="form" enctype="multipart/form-data" method="post" action="import.php">
<input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
<?php print_simple_box_start("center"); ?>
<table cellpadding="5">
<tr>
<td align="right"><?php echo $txt->category; ?>:</td>
<td><?php choose_from_menu($catmenu, "category", $category->id, ""); ?>
<?php echo $txt->fromfile; ?>
<input name="catfromfile" type="checkbox" />
<?php helpbutton('importcategory', $txt->importcategory, 'quiz'); ?></td>
</tr>
<tr>
<td align="right"><?php echo $txt->fileformat; ?>:</td>
<td><?php choose_from_menu($fileformatnames, "format", "gift", "");
helpbutton("import", $txt->importquestions, "quiz"); ?></td>
</tr>
<tr>
<td align="right"><?php echo $txt->matchgrades; ?></td>
<td><?php choose_from_menu($matchgrades,'matchgrades',$txt->matchgradeserror,'' );
helpbutton('matchgrades', $txt->matchgrades, 'quiz'); ?></td>
</tr>
</table>
<?php
print_simple_box_end();
print_simple_box_start('center'); ?>
<?php echo $txt->importfileupload; ?>
<table cellpadding="5">
<tr>
<td align="right"><?php echo $txt->upload; ?>:</td>
<td><?php upload_print_form_fragment(1,array('newfile'),null,false,null,$course->maxbytes,0,false); ?></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="save" value="<?php echo $txt->uploadthisfile; ?>" /></td>
</tr>
</table>
<?php
print_simple_box_end();
print_simple_box_start('center'); ?>
<?php echo $txt->importfilearea; ?>
<table cellpadding="5">
<tr>
<td align="right"><?php echo $txt->file; ?>:</td>
<td><input type="text" name="choosefile" size="50" /></td>
</tr>
<tr>
<td> </td>
<td><?php button_to_popup_window ("/files/index.php?id={$course->id}&choose=form.choosefile",
"coursefiles", $txt->choosefile, 500, 750, $txt->choosefile); ?>
<input type="submit" name="save" value="<?php echo $txt->importfromthisfile; ?>" /></td>
</tr>
</table>
<?php
print_simple_box_end(); ?>
</form>
<?php
print_footer($course);
?>