Moving import/export to new question area.
[moodle-linuxchix.git] / question / format.php
blob94d48d2865d6a5ba33eed85e35d58a33cae1b4c4
1 <?php // $Id$
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 ////////////////////////////////////////////////////////////////////
9 // Included by import.php and export.php
11 class quiz_default_format {
13 var $displayerrors = true;
14 var $category = NULL;
15 var $course = NULL;
16 var $questionids = array();
18 // functions to indicate import/export functionality
19 // override to return true if implemented
21 function provide_import() {
22 return false;
25 function provide_export() {
26 return false;
29 /// Importing functions
31 function importpreprocess($category, $course=NULL ) {
32 /// Does any pre-processing that may be desired
34 $this->category = $category; // Important
35 $this->course = $course;
37 return true;
40 function importprocess($filename) {
41 /// Processes a given file. There's probably little need to change this
43 if (! $lines = $this->readdata($filename)) {
44 notify("File could not be read, or was empty");
45 return false;
48 if (! $questions = $this->readquestions($lines)) { // Extract all the questions
49 notify("There are no questions in this file!");
50 return false;
53 notify("Importing ".count($questions)." questions");
55 $count = 0;
57 foreach ($questions as $question) { // Process and store each question
58 $count++;
60 echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
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
66 if (!$question->id = insert_record("quiz_questions", $question)) {
67 error("Could not insert new question!");
70 $this->questionids[] = $question->id;
72 // Now to save all the answers and type-specific options
74 global $QTYPES;
75 $result = $QTYPES[$question->qtype]
76 ->save_question_options($question);
78 if (!empty($result->error)) {
79 notify($result->error);
80 return false;
83 if (!empty($result->notice)) {
84 notify($result->notice);
85 return true;
88 return true;
92 function readdata($filename) {
93 /// Returns complete file with an array, one item per line
95 if (is_readable($filename)) {
96 $filearray = file($filename);
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;
105 return false;
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.
114 $questions = array();
115 $currentquestion = array();
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;
124 $currentquestion = array();
126 } else {
127 $currentquestion[] = $line;
131 if (!empty($currentquestion)) { // There may be a final question
132 if ($question = $this->readquestion($currentquestion)) {
133 $questions[] = $question;
137 return $questions;
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();
153 return $question;
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.
161 echo "<p>This quiz format has not yet been completed!</p>";
163 return NULL;
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.
172 return true;
175 // Export functions
178 function export_file_extension() {
179 /// return the files extension appropriate for this type
180 /// override if you don't want .txt
182 return ".txt";
185 function exportpreprocess($category, $course) {
186 /// Does any pre-processing that may be desired
188 $this->category = $category; // Important
189 $this->course = $course; // As is this!
191 return true;
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
199 return $content;
202 function exportprocess($filename) {
203 /// Exports a given category. There's probably little need to change this
205 global $CFG;
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");
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 );
221 notify("Exporting questions.");
222 if (!count($questions)) {
223 return true;
225 $count = 0;
227 // results are first written into string (and then to a file)
228 // so create/initialize the string here
229 $expout = "";
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";
242 // final pre-process on exported data
243 $expout = $this->presave_process( $expout );
245 // write file
246 $filepath = $path."/".$filename . $this->export_file_extension();
247 if (!$fh=fopen($filepath,"w")) {
248 error("Cannot open for writing: $filepath");
250 if (!fwrite($fh, $expout)) {
251 error("Cannot write exported questions to $filepath");
253 fclose($fh);
255 return true;
258 function exportpostprocess() {
259 /// Does any post-processing that may be desired
261 return true;
264 function writequestion($question) {
265 /// Turns a question object into textual output in the given format
266 /// must be overidden
268 echo "<p>This quiz format has not yet been completed!</p>";
270 return NULL;