Moving import/export to new question area.
[moodle.git] / question / format.php
CommitLineData
aca318e1 1<?php // $Id$
2
3////////////////////////////////////////////////////////////////////
4/// format.php - Default format class for file imports/exports. //
5/// //
6/// Doesn't do everything on it's own -- it needs to be extended. //
7////////////////////////////////////////////////////////////////////
8
9// Included by import.php and export.php
10
11class quiz_default_format {
12
13 var $displayerrors = true;
14 var $category = NULL;
15 var $course = NULL;
16 var $questionids = array();
17
18// functions to indicate import/export functionality
19// override to return true if implemented
20
21 function provide_import() {
22 return false;
23 }
24
25 function provide_export() {
26 return false;
27 }
28
29/// Importing functions
30
31 function importpreprocess($category, $course=NULL ) {
32 /// Does any pre-processing that may be desired
33
34 $this->category = $category; // Important
35 $this->course = $course;
36
37 return true;
38 }
39
40 function importprocess($filename) {
41 /// Processes a given file. There's probably little need to change this
42
43 if (! $lines = $this->readdata($filename)) {
44 notify("File could not be read, or was empty");
45 return false;
46 }
47
48 if (! $questions = $this->readquestions($lines)) { // Extract all the questions
49 notify("There are no questions in this file!");
50 return false;
51 }
52
53 notify("Importing ".count($questions)." questions");
54
55 $count = 0;
56
57 foreach ($questions as $question) { // Process and store each question
58 $count++;
59
60 echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
61
62 $question->category = $this->category->id;
63 $question->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
64 $question->version = 1; // Original version of this question
65
66 if (!$question->id = insert_record("quiz_questions", $question)) {
67 error("Could not insert new question!");
68 }
69
70 $this->questionids[] = $question->id;
71
72 // Now to save all the answers and type-specific options
73
74 global $QTYPES;
75 $result = $QTYPES[$question->qtype]
76 ->save_question_options($question);
77
78 if (!empty($result->error)) {
79 notify($result->error);
80 return false;
81 }
82
83 if (!empty($result->notice)) {
84 notify($result->notice);
85 return true;
86 }
87 }
88 return true;
89 }
90
91
92 function readdata($filename) {
93 /// Returns complete file with an array, one item per line
94
95 if (is_readable($filename)) {
96 $filearray = file($filename);
97
98 /// Check for Macintosh OS line returns (ie file on one line), and fix
99 if (ereg("\r", $filearray[0]) AND !ereg("\n", $filearray[0])) {
100 return explode("\r", $filearray[0]);
101 } else {
102 return $filearray;
103 }
104 }
105 return false;
106 }
107
108 function readquestions($lines) {
109 /// Parses an array of lines into an array of questions,
110 /// where each item is a question object as defined by
111 /// readquestion(). Questions are defined as anything
112 /// between blank lines.
113
114 $questions = array();
115 $currentquestion = array();
116
117 foreach ($lines as $line) {
118 $line = trim($line);
119 if (empty($line)) {
120 if (!empty($currentquestion)) {
121 if ($question = $this->readquestion($currentquestion)) {
122 $questions[] = $question;
123 }
124 $currentquestion = array();
125 }
126 } else {
127 $currentquestion[] = $line;
128 }
129 }
130
131 if (!empty($currentquestion)) { // There may be a final question
132 if ($question = $this->readquestion($currentquestion)) {
133 $questions[] = $question;
134 }
135 }
136
137 return $questions;
138 }
139
140
141 function defaultquestion() {
142 // returns an "empty" question
143 // Somewhere to specify question parameters that are not handled
144 // by import but are required db fields.
145 // This should not be overridden.
146 $question = new stdClass();
147 $question->shuffleanswers = 0;
148 $question->defaultgrade = 1;
149 $question->image = "";
150 $question->usecase = 0;
151 $question->multiplier = array();
152
153 return $question;
154 }
155
156 function readquestion($lines) {
157 /// Given an array of lines known to define a question in
158 /// this format, this function converts it into a question
159 /// object suitable for processing and insertion into Moodle.
160
161 echo "<p>This quiz format has not yet been completed!</p>";
162
163 return NULL;
164 }
165
166
167 function importpostprocess() {
168 /// Does any post-processing that may be desired
169 /// Argument is a simple array of question ids that
170 /// have just been added.
171
172 return true;
173 }
174
175// Export functions
176
177
178 function export_file_extension() {
179 /// return the files extension appropriate for this type
180 /// override if you don't want .txt
181
182 return ".txt";
183 }
184
185 function exportpreprocess($category, $course) {
186 /// Does any pre-processing that may be desired
187
188 $this->category = $category; // Important
189 $this->course = $course; // As is this!
190
191 return true;
192 }
193
194 function presave_process( $content ) {
195 /// enables any processing to be done on the content
196 /// just prior to the file being saved
197 /// default is to do nothing
198
199 return $content;
200 }
201
202 function exportprocess($filename) {
203 /// Exports a given category. There's probably little need to change this
204
205 global $CFG;
206
207 // create a directory for the exports (if not already existing)
208 $dirname = get_string("exportfilename","quiz");
209 $courseid = $this->course->id;
210 $path = $CFG->dataroot.'/'.$courseid.'/'.$dirname;
211 if (!is_dir($path)) {
212 if (!mkdir($path, $CFG->directorypermissions)) {
213 error("Cannot create path: $path");
214 }
215 }
216
217 // get the questions (from database) in this category
218 // only get q's with no parents (no cloze subquestions specifically)
219 $questions = get_questions_category( $this->category, true );
220
221 notify("Exporting questions.");
222 if (!count($questions)) {
223 return true;
224 }
225 $count = 0;
226
227 // results are first written into string (and then to a file)
228 // so create/initialize the string here
229 $expout = "";
230
231 // iterate through questions
232 foreach($questions as $question) {
233 $count++;
234 $qtype = $question->qtype;
235 // ignore random questiond
236 if ($qtype!=RANDOM) {
237 echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
238 $expout .= $this->writequestion( $question ) . "\n";
239 }
240 }
241
242 // final pre-process on exported data
243 $expout = $this->presave_process( $expout );
244
245 // write file
246 $filepath = $path."/".$filename . $this->export_file_extension();
247 if (!$fh=fopen($filepath,"w")) {
248 error("Cannot open for writing: $filepath");
249 }
250 if (!fwrite($fh, $expout)) {
251 error("Cannot write exported questions to $filepath");
252 }
253 fclose($fh);
254
255 return true;
256 }
257
258 function exportpostprocess() {
259 /// Does any post-processing that may be desired
260
261 return true;
262 }
263
264 function writequestion($question) {
265 /// Turns a question object into textual output in the given format
266 /// must be overidden
267
268 echo "<p>This quiz format has not yet been completed!</p>";
269
270 return NULL;
271 }
272
273}
274
275?>