3 * Export quiz questions into the given category
6 * @author Martin Dougiamas, Howard Miller, and many others.
7 * {@link http://moodle.org}
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 require_once("../config.php");
13 // require_once("locallib.php"); // TODO: this should not need locallib.php
14 require_once($CFG->libdir
.'/questionlib.php');
16 $categoryid = optional_param('category',0, PARAM_INT
);
17 $courseid = required_param('courseid',PARAM_INT
);
18 $format = optional_param('format','', PARAM_CLEANFILE
);
19 $exportfilename = optional_param('exportfilename','',PARAM_CLEANFILE
);
21 if (! $course = get_record("course", "id", $courseid)) {
22 error("Course does not exist!");
25 if (!$categoryid) { // need to get category from modform
26 $showcatmenu = true; // will ensure that user can choose category
27 if (isset($SESSION->modform
)) {
28 $categoryid = $SESSION->modform
->category
;
32 if (! $category = get_record("quiz_categories", "id", $categoryid)) {
33 $category = quiz_get_default_category($courseid);
36 if (! $categorycourse = get_record("course", "id", $category->course
)) {
37 error("This category doesn't belong to a valid course!");
40 require_login($course->id
, false);
42 if (!isteacher($course->id
)) {
43 error("Only the teacher can export quiz questions!");
46 // ensure the files area exists for this course
47 make_upload_directory( "$course->id" );
49 $strexportquestions = get_string("exportquestions", "quiz");
50 $strquestions = get_string("questions", "quiz");
52 $strquizzes = get_string('modulenameplural', 'quiz');
53 $streditingquiz = get_string(isset($SESSION->modform
->instance
) ?
"editingquiz" : "editquestions", "quiz");
55 $dirname = get_string("exportfilename","quiz");
59 if (isset($SESSION->modform
->instance
) and $quiz = get_record('quiz', 'id', $SESSION->modform
->instance
)) {
60 $strupdatemodule = isteacheredit($course->id
)
61 ?
update_module_button($SESSION->modform
->cmid
, $course->id
, get_string('modulename', 'quiz'))
63 print_header_simple($strexportquestions, '',
64 "<a href=\"index.php?id=$course->id\">".get_string('modulenameplural', 'quiz').'</a>'.
65 " -> <a href=\"view.php?q=$quiz->id\">".format_string($quiz->name
).'</a>'.
66 ' -> '.$strexportquestions,
67 "", "", true, $strupdatemodule);
72 print_header_simple($strexportquestions, '',
73 '<a href="edit.php">'.get_string('editquestions', 'quiz').'</a>'.
74 ' -> '.$strexportquestions);
77 if (!empty($format)) { /// Filename
79 if (!confirm_sesskey()) {
80 echo( 'Sesskey error' );
83 if (! is_readable("format/$format/format.php")) {
84 error('Format not known ('.clean_text($form->format
).')');
87 require("format.php"); // Parent class
88 require("format/$format/format.php");
90 $classname = "quiz_format_$format";
91 $quiz_format = new $classname();
93 if (! $quiz_format->exportpreprocess($category, $course)) { // Do anything before that we need to
94 error("Error occurred during pre-processing!",
95 "$CFG->wwwroot/mod/quiz/export.php?category=$category->id");
98 if (! $quiz_format->exportprocess($exportfilename)) { // Process the export data
99 error("Error occurred during processing!",
100 "$CFG->wwwroot/mod/quiz/export.php?category=$category->id");
103 if (! $quiz_format->exportpostprocess()) { // In case anything needs to be done after
104 error("Error occurred during post-processing!",
105 "$CFG->wwwroot/mod/quiz/export.php?category=$category->id");
109 // link to download the finished file
110 $file_ext = $quiz_format->export_file_extension();
111 $download_str = get_string( 'download', 'quiz' );
112 $downloadextra_str = get_string( 'downloadextra','quiz' );
113 if ($CFG->slasharguments
) {
114 $efile = "{$CFG->wwwroot}/file.php/$course->id/quiz/$exportfilename".$file_ext;
117 $efile = "{$CFG->wwwroot}/file.php?file=/$course->id/quiz/$exportfilename".$file_ext;
119 $efile .= "?forcedownload=1";
120 echo "</p><center><a href=\"$efile\">$download_str</a></center></p>";
121 echo "</p><center><font size=\"-1\">$downloadextra_str</font></center></p>";
123 print_continue("edit.php");
124 print_footer($course);
128 /// Print upload form
130 // get valid formats to generate dropdown list
131 $fileformatnames = get_import_export_formats( "export" );
134 if (empty($exportfilename)) {
135 $exportfilename = default_export_filename($course, $category);
138 /// Get all the existing categories now
139 if (!$categories = get_records_select("quiz_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
140 error("Could not find any question categories!");
142 $categories = add_indented_names($categories);
143 foreach ($categories as $key => $cat) {
144 if ($catcourse = get_record("course", "id", $cat->course
)) {
145 if ($cat->publish
&& $cat->course
!= $course->id
) {
146 $cat->indentedname
.= " ($catcourse->shortname)";
148 $catmenu[$cat->id
] = $cat->indentedname
;
152 print_heading_with_help($strexportquestions, "export", "quiz");
154 print_simple_box_start("center");
155 echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"export.php\">\n";
156 echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />\n";
157 echo "<table cellpadding=\"5\">\n";
159 echo "<tr><td align=\"right\">\n";
160 print_string("category", "quiz");
162 if (!$showcatmenu) { // category already specified
163 echo quiz_get_category_coursename($category);
164 echo " <input type=\"hidden\" name=\"category\" value=\"$category->id\" />";
165 } else { // no category specified, let user choose
166 choose_from_menu($catmenu, "category", $category->id
, "");
168 //echo str_replace(' ', '', $category->name) . " ($categorycourse->shortname)";
171 echo "<tr><td align=\"right\">";
172 print_string("fileformat", "quiz");
174 choose_from_menu($fileformatnames, "format", "gift", "");
175 helpbutton("export", $strexportquestions, "quiz");
178 echo "<tr><td align=\"right\">";
179 print_string("exportname", "quiz" );
181 echo "<input type=\"text\" size=\"40\" name=\"exportfilename\" value=\"$exportfilename\" />";
184 echo "<tr><td align=\"center\" colspan=\"2\">";
185 echo " <input type=\"hidden\" name=\"courseid\" value=\"$course->id\" />";
186 echo " <input type=\"submit\" name=\"save\" value=\"".get_string("exportquestions","quiz")."\" />";
191 print_simple_box_end();
193 print_footer($course);