fixed alternative salt index range - now allowed 1...20
[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");
aca318e1 13 require_once($CFG->libdir.'/questionlib.php');
14
15 $categoryid = optional_param('category',0, PARAM_INT);
16 $courseid = required_param('courseid',PARAM_INT);
5a4bcbc3 17 $format = optional_param('format','', PARAM_FILE );
18 $exportfilename = optional_param('exportfilename','',PARAM_FILE );
aca318e1 19
20 if (! $course = get_record("course", "id", $courseid)) {
21 error("Course does not exist!");
22 }
23
24 if (!$categoryid) { // need to get category from modform
25 $showcatmenu = true; // will ensure that user can choose category
dc454ef4 26 if (isset($SESSION->questioncat)) {
27 $categoryid = $SESSION->questioncat;
aca318e1 28 }
29 }
30
dc1f00de 31 if (! $category = get_record("question_categories", "id", $categoryid)) {
062f1125 32 $category = get_default_question_category($courseid);
aca318e1 33 }
34
35 if (! $categorycourse = get_record("course", "id", $category->course)) {
1e3d6fd8 36 error( get_string('nocategory','quiz') );
aca318e1 37 }
38
39 require_login($course->id, false);
40
41 if (!isteacher($course->id)) {
1e3d6fd8 42 error( get_string('onlyteachersexport','quiz') );
aca318e1 43 }
44
45 // ensure the files area exists for this course
46 make_upload_directory( "$course->id" );
47
48 $strexportquestions = get_string("exportquestions", "quiz");
49 $strquestions = get_string("questions", "quiz");
50
51 $strquizzes = get_string('modulenameplural', 'quiz');
52 $streditingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz");
53
54 $dirname = get_string("exportfilename","quiz");
55
56 /// Header:
57
58 if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
59 $strupdatemodule = isteacheredit($course->id)
60 ? update_module_button($SESSION->modform->cmid, $course->id, get_string('modulename', 'quiz'))
61 : "";
62 print_header_simple($strexportquestions, '',
70c01adb 63 "<a href=\"$CFG->wwwroot/mod/quiz/index.php?id=$course->id\">".get_string('modulenameplural', 'quiz').'</a>'.
64 " -> <a href=\"$CFG->wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).'</a>'.
aca318e1 65 ' -> '.$strexportquestions,
66 "", "", true, $strupdatemodule);
67 $currenttab = 'edit';
68 $mode = 'export';
70c01adb 69 include($CFG->dirroot.'/mod/quiz/tabs.php');
aca318e1 70 } else {
2662cf46 71 print_header_simple($strexportquestions, '', $strexportquestions);
567709c4 72 // print tabs
73 $currenttab = 'export';
74 include('tabs.php');
aca318e1 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
f5565b69 90 $classname = "qformat_$format";
91 $qformat = new $classname();
aca318e1 92
f5565b69 93 if (! $qformat->exportpreprocess($category, $course)) { // Do anything before that we need to
1e3d6fd8 94 error( get_string('exporterror','quiz'),
95 "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 96 }
97
f5565b69 98 if (! $qformat->exportprocess($exportfilename)) { // Process the export data
1e3d6fd8 99 error( get_string('exporterror','quiz'),
100 "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 101 }
102
f5565b69 103 if (! $qformat->exportpostprocess()) { // In case anything needs to be done after
1e3d6fd8 104 error( get_string('exporterror','quiz'),
105 "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 106 }
107 echo "<hr />";
108
109 // link to download the finished file
f5565b69 110 $file_ext = $qformat->export_file_extension();
aca318e1 111 $download_str = get_string( 'download', 'quiz' );
49678184 112 $quizexportdir = get_string( 'exportfilename', 'quiz' );
aca318e1 113 $downloadextra_str = get_string( 'downloadextra','quiz' );
114 if ($CFG->slasharguments) {
49678184 115 $efile = "{$CFG->wwwroot}/file.php/$course->id/$quizexportdir/$exportfilename".$file_ext;
aca318e1 116 }
117 else {
49678184 118 $efile = "{$CFG->wwwroot}/file.php?file=/$course->id/$quizexportdir/$exportfilename".$file_ext;
aca318e1 119 }
120 $efile .= "?forcedownload=1";
121 echo "</p><center><a href=\"$efile\">$download_str</a></center></p>";
122 echo "</p><center><font size=\"-1\">$downloadextra_str</font></center></p>";
123
5d77ace2 124 print_continue("edit.php?courseid=$course->id");
aca318e1 125 print_footer($course);
126 exit;
127 }
128
129 /// Print upload form
130
131 // get valid formats to generate dropdown list
132 $fileformatnames = get_import_export_formats( "export" );
133
134 // get filename
135 if (empty($exportfilename)) {
136 $exportfilename = default_export_filename($course, $category);
137 }
138
139 /// Get all the existing categories now
dc1f00de 140 if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
aca318e1 141 error("Could not find any question categories!");
142 }
143 $categories = add_indented_names($categories);
144 foreach ($categories as $key => $cat) {
145 if ($catcourse = get_record("course", "id", $cat->course)) {
146 if ($cat->publish && $cat->course != $course->id) {
147 $cat->indentedname .= " ($catcourse->shortname)";
148 }
149 $catmenu[$cat->id] = $cat->indentedname;
150 }
151 }
152
153 print_heading_with_help($strexportquestions, "export", "quiz");
154
155 print_simple_box_start("center");
156 echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"export.php\">\n";
157 echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />\n";
158 echo "<table cellpadding=\"5\">\n";
159
160 echo "<tr><td align=\"right\">\n";
161 print_string("category", "quiz");
162 echo ":</td><td>";
163 if (!$showcatmenu) { // category already specified
dc1f00de 164 echo question_category_coursename($category);
aca318e1 165 echo " <input type=\"hidden\" name=\"category\" value=\"$category->id\" />";
166 } else { // no category specified, let user choose
167 choose_from_menu($catmenu, "category", $category->id, "");
168 }
169 //echo str_replace('&nbsp;', '', $category->name) . " ($categorycourse->shortname)";
170 echo "</td></tr>\n";
171
172 echo "<tr><td align=\"right\">";
173 print_string("fileformat", "quiz");
174 echo ":</td><td>";
175 choose_from_menu($fileformatnames, "format", "gift", "");
176 helpbutton("export", $strexportquestions, "quiz");
177 echo "</td></tr>\n";
178
179 echo "<tr><td align=\"right\">";
180 print_string("exportname", "quiz" );
181 echo ":</td><td>";
182 echo "<input type=\"text\" size=\"40\" name=\"exportfilename\" value=\"$exportfilename\" />";
183 echo "</td></tr>\n";
184
185 echo "<tr><td align=\"center\" colspan=\"2\">";
186 echo " <input type=\"hidden\" name=\"courseid\" value=\"$course->id\" />";
187 echo " <input type=\"submit\" name=\"save\" value=\"".get_string("exportquestions","quiz")."\" />";
188 echo "</td></tr>\n";
189
190 echo "</table>\n";
191 echo "</form>\n";
192 print_simple_box_end();
193
194 print_footer($course);
195
196?>