mirrored from git://git.moodle.org/moodle.git
/
export.php
211 lines (177 loc) · 8.32 KB
/
export.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
<?php // $Id$
/**
* Export 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" );
// get parameters
$categoryid = optional_param('category',0, PARAM_INT);
$cattofile = optional_param('cattofile',0, PARAM_BOOL);
$courseid = required_param('courseid',PARAM_INT);
$exportfilename = optional_param('exportfilename','',PARAM_FILE );
$format = optional_param('format','', PARAM_FILE );
// get display strings
$txt = new object;
$txt->category = get_string('category','quiz');
$txt->download = get_string('download','quiz');
$txt->downloadextra = get_string('downloadextra','quiz');
$txt->exporterror = get_string('exporterror','quiz');
$txt->exportname = get_string('exportname','quiz');
$txt->exportquestions = get_string('exportquestions', 'quiz');
$txt->fileformat = get_string('fileformat','quiz');
$txt->exportcategory = get_string('exportcategory','quiz');
$txt->modulename = get_string('modulename','quiz');
$txt->modulenameplural = get_string('modulenameplural','quiz');
$txt->nocategory = get_string('nocategory','quiz');
$txt->tofile = get_string('tofile','quiz');
if (! $course = get_record("course", "id", $courseid)) {
error("Course does not exist!");
}
$showcatmenu = false;
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)) {
$category = get_default_question_category($courseid);
}
if (! $categorycourse = get_record("course", "id", $category->course)) {
error( $txt->nocategory );
}
require_login($course->id, false);
// check role capability
$context = get_context_instance(CONTEXT_COURSE, $course->id);
require_capability('moodle/question:export', $context);
// ensure the files area exists for this course
make_upload_directory( "$course->id" );
// check category is valid
if (!empty($categoryid)) {
$validcats = question_category_select_menu( $course->id, true, false, 0, false );
if (!array_key_exists( $categoryid, $validcats)) {
print_error( 'invalidcategory','quiz' );
}
}
/// Header
if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
$strupdatemodule = has_capability('moodle/course:manageactivities', $context)
? update_module_button($SESSION->modform->cmid, $course->id, $txt->modulename )
: "";
print_header_simple($txt->exportquestions, '',
"<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->exportquestions,
"", "", true, $strupdatemodule);
$currenttab = 'edit';
$mode = 'export';
include($CFG->dirroot.'/mod/quiz/tabs.php');
} else {
print_header_simple($txt->exportquestions, '', $txt->exportquestions);
// print tabs
$currenttab = 'export';
include('tabs.php');
}
if (!empty($format)) { /// Filename
if (!confirm_sesskey()) {
print_error( 'sesskey' );
}
if (! is_readable("format/$format/format.php")) {
error( "Format not known ($format)" ); }
// load parent class for import/export
require("format.php");
// and then the class for the selected format
require("format/$format/format.php");
$classname = "qformat_$format";
$qformat = new $classname();
$qformat->setCategory( $category );
$qformat->setCourse( $course );
$qformat->setFilename( $exportfilename );
$qformat->setCattofile( $cattofile );
if (! $qformat->exportpreprocess()) { // Do anything before that we need to
error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&category=$category->id");
}
if (! $qformat->exportprocess()) { // Process the export data
error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&category=$category->id");
}
if (! $qformat->exportpostprocess()) { // In case anything needs to be done after
error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&category=$category->id");
}
echo "<hr />";
// link to download the finished file
$file_ext = $qformat->export_file_extension();
if ($CFG->slasharguments) {
$efile = "{$CFG->wwwroot}/file.php/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."?forcedownload=1";
}
else {
$efile = "{$CFG->wwwroot}/file.php?file=/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."&forcedownload=1";
}
echo "</p><center><a href=\"$efile\">$txt->download</a></center></p>";
echo "</p><center><font size=\"-1\">$txt->downloadextra</font></center></p>";
print_continue("edit.php?courseid=$course->id");
print_footer($course);
exit;
}
/// Display upload form
// get valid formats to generate dropdown list
$fileformatnames = get_import_export_formats( 'export' );
// get filename
if (empty($exportfilename)) {
$exportfilename = default_export_filename($course, $category);
}
// DISPLAY MAIN PAGE
print_heading_with_help($txt->exportquestions, 'export', 'quiz');
print_simple_box_start('center');
?>
<form enctype="multipart/form-data" method="post" action="export.php">
<input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
<input type="hidden" name="courseid" value="<?php echo $course->id; ?>" />
<table cellpadding="5">
<tr>
<td align="right"><?php echo $txt->category; ?>:</td>
<td>
<?php
if (!$showcatmenu) { // category already specified
echo '<strong>'.question_category_coursename($category).'</strong> '; ?>
<input type="hidden" name="category" value="<?php echo $category->id ?>" />
<?php
} else { // no category specified, let user choose
question_category_select_menu($course->id, true, false, $category->id);
}
echo $txt->tofile; ?>
<input name="cattofile" type="checkbox" />
<?php helpbutton('exportcategory', $txt->exportcategory, 'quiz'); ?>
</td>
</tr>
<tr>
<td align="right"><?php echo $txt->fileformat; ?>:</td>
<td>
<?php choose_from_menu($fileformatnames, 'format', 'gift', '');
helpbutton('export', $txt->exportquestions, 'quiz'); ?>
</td>
</tr>
<tr>
<td align="right"><?php echo $txt->exportname; ?>:</td>
<td>
<input type="text" size="40" name="exportfilename" value="<?php echo $exportfilename; ?>" />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" name="save" value="<?php echo $txt->exportquestions; ?>" />
</td>
</tr>
</table>
</form>
<?php
print_simple_box_end();
print_footer($course);
?>