Moving import/export to new question area.
[moodle.git] / question / export.php
CommitLineData
aca318e1 1<?php // $Id$
2/**
3* Export quiz questions into the given category
4*
5* @version $Id$
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
9* @package quiz
10*/
11
12 require_once("../config.php");
13 // require_once("locallib.php"); // TODO: this should not need locallib.php
14 require_once($CFG->libdir.'/questionlib.php');
15
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 );
20
21 if (! $course = get_record("course", "id", $courseid)) {
22 error("Course does not exist!");
23 }
24
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;
29 }
30 }
31
32 if (! $category = get_record("quiz_categories", "id", $categoryid)) {
33 $category = quiz_get_default_category($courseid);
34 }
35
36 if (! $categorycourse = get_record("course", "id", $category->course)) {
37 error("This category doesn't belong to a valid course!");
38 }
39
40 require_login($course->id, false);
41
42 if (!isteacher($course->id)) {
43 error("Only the teacher can export quiz questions!");
44 }
45
46 // ensure the files area exists for this course
47 make_upload_directory( "$course->id" );
48
49 $strexportquestions = get_string("exportquestions", "quiz");
50 $strquestions = get_string("questions", "quiz");
51
52 $strquizzes = get_string('modulenameplural', 'quiz');
53 $streditingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz");
54
55 $dirname = get_string("exportfilename","quiz");
56
57 /// Header:
58
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'))
62 : "";
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);
68 $currenttab = 'edit';
69 $mode = 'export';
70 include('tabs.php');
71 } else {
72 print_header_simple($strexportquestions, '',
73 '<a href="edit.php">'.get_string('editquestions', 'quiz').'</a>'.
74 ' -> '.$strexportquestions);
75 }
76
77 if (!empty($format)) { /// Filename
78
79 if (!confirm_sesskey()) {
80 echo( 'Sesskey error' );
81 }
82
83 if (! is_readable("format/$format/format.php")) {
84 error('Format not known ('.clean_text($form->format).')');
85 }
86
87 require("format.php"); // Parent class
88 require("format/$format/format.php");
89
90 $classname = "quiz_format_$format";
91 $quiz_format = new $classname();
92
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");
96 }
97
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");
101 }
102
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");
106 }
107 echo "<hr />";
108
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;
115 }
116 else {
117 $efile = "{$CFG->wwwroot}/file.php?file=/$course->id/quiz/$exportfilename".$file_ext;
118 }
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>";
122
123 print_continue("edit.php");
124 print_footer($course);
125 exit;
126 }
127
128 /// Print upload form
129
130 // get valid formats to generate dropdown list
131 $fileformatnames = get_import_export_formats( "export" );
132
133 // get filename
134 if (empty($exportfilename)) {
135 $exportfilename = default_export_filename($course, $category);
136 }
137
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!");
141 }
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)";
147 }
148 $catmenu[$cat->id] = $cat->indentedname;
149 }
150 }
151
152 print_heading_with_help($strexportquestions, "export", "quiz");
153
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";
158
159 echo "<tr><td align=\"right\">\n";
160 print_string("category", "quiz");
161 echo ":</td><td>";
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, "");
167 }
168 //echo str_replace('&nbsp;', '', $category->name) . " ($categorycourse->shortname)";
169 echo "</td></tr>\n";
170
171 echo "<tr><td align=\"right\">";
172 print_string("fileformat", "quiz");
173 echo ":</td><td>";
174 choose_from_menu($fileformatnames, "format", "gift", "");
175 helpbutton("export", $strexportquestions, "quiz");
176 echo "</td></tr>\n";
177
178 echo "<tr><td align=\"right\">";
179 print_string("exportname", "quiz" );
180 echo ":</td><td>";
181 echo "<input type=\"text\" size=\"40\" name=\"exportfilename\" value=\"$exportfilename\" />";
182 echo "</td></tr>\n";
183
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")."\" />";
187 echo "</td></tr>\n";
188
189 echo "</table>\n";
190 echo "</form>\n";
191 print_simple_box_end();
192
193 print_footer($course);
194
195?>