3 ////////////////////////////////////////////////////////////////////
4 /// format.php - Default format class for file imports/exports. //
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;
16 var $questionids = array();
18 // functions to indicate import/export functionality
19 // override to return true if implemented
21 function provide_import() {
25 function provide_export() {
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;
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");
48 if (! $questions = $this->readquestions($lines)) { // Extract all the questions
49 notify("There are no questions in this file!");
53 notify("Importing ".count($questions)." questions");
57 foreach ($questions as $question) { // Process and store each question
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
75 $result = $QTYPES[$question->qtype
]
76 ->save_question_options($question);
78 if (!empty($result->error
)) {
79 notify($result->error
);
83 if (!empty($result->notice
)) {
84 notify($result->notice
);
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]);
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) {
120 if (!empty($currentquestion)) {
121 if ($question = $this->readquestion($currentquestion)) {
122 $questions[] = $question;
124 $currentquestion = array();
127 $currentquestion[] = $line;
131 if (!empty($currentquestion)) { // There may be a final question
132 if ($question = $this->readquestion($currentquestion)) {
133 $questions[] = $question;
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();
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>";
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.
178 function export_file_extension() {
179 /// return the files extension appropriate for this type
180 /// override if you don't want .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!
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
202 function exportprocess($filename) {
203 /// Exports a given category. There's probably little need to change this
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)) {
227 // results are first written into string (and then to a file)
228 // so create/initialize the string here
231 // iterate through questions
232 foreach($questions as $question) {
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 );
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");
258 function exportpostprocess() {
259 /// Does any post-processing that may be desired
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>";