MDL-8100, defaultgrade not imported.\rQuestion obect initialisation and value import...
[moodle-linuxchix.git] / question / format.php
blob86854954a6ac37148885e723190f870789da5a91
2 <?php // $Id$
4 ////////////////////////////////////////////////////////////////////
5 /// format.php - Default format class for file imports/exports. //
6 /// //
7 /// Doesn't do everything on it's own -- it needs to be extended. //
8 ////////////////////////////////////////////////////////////////////
10 // Included by import.php and export.php
12 class qformat_default {
14 // var $displayerrors = true;
15 var $category = NULL;
16 var $course = NULL;
17 var $filename = '';
18 var $matchgrades = 'error';
19 var $catfromfile = 0;
20 var $questionids = array();
22 // functions to indicate import/export functionality
23 // override to return true if implemented
25 function provide_import() {
26 return false;
29 function provide_export() {
30 return false;
33 // Accessor methods
35 /**
36 * set the category
37 * @param object category the category object
39 function setCategory( $category ) {
40 $this->category = $category;
43 /**
44 * set the course class variable
45 * @param course object Moodle course variable
47 function setCourse( $course ) {
48 $this->course = $course;
51 /**
52 * set the filename
53 * @param string filename name of file to import/export
55 function setFilename( $filename ) {
56 $this->filename = $filename;
59 /**
60 * set matchgrades
61 * @param string matchgrades error or nearest for grades
63 function setMatchgrades( $matchgrades ) {
64 $this->matchgrades = $matchgrades;
67 /**
68 * set catfromfile
69 * @param bool catfromfile allow categories embedded in import file
71 function setCatfromfile( $catfromfile ) {
72 $this->catfromfile = $catfromfile;
75 /// Importing functions
77 /**
78 * Perform any required pre-processing
80 function importpreprocess() {
81 return true;
84 /**
85 * Process the file
86 * This method should not normally be overidden
88 function importprocess() {
89 if (! $lines = $this->readdata($this->filename)) {
90 notify( get_string('cannotread','quiz') );
91 return false;
94 if (! $questions = $this->readquestions($lines)) { // Extract all the questions
95 notify( get_string('noquestionsinfile','quiz') );
96 return false;
99 notify( get_string('importingquestions','quiz',count($questions)) );
101 // get list of valid answer grades
102 $grades = get_grade_options();
103 $gradeoptionsfull = $grades->gradeoptionsfull;
105 $count = 0;
107 foreach ($questions as $question) { // Process and store each question
109 // check for category modifiers
110 if ($question->qtype=='category') {
111 if ($this->catfromfile) {
112 // find/create category object
113 $catpath = $question->category;
114 $newcategory = create_category_path( $catpath, '/', $this->course->id );
115 if (!empty($newcategory)) {
116 $this->category = $newcategory;
119 continue;
122 $count++;
124 echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
126 // check for answer grades validity (must match fixed list of grades)
127 if (!empty($question->fraction) and (is_array($question->fraction))) {
128 $fractions = $question->fraction;
129 $answersvalid = true; // in case they are!
130 foreach ($fractions as $key => $fraction) {
131 $newfraction = match_grade_options($gradeoptionsfull, $fraction, $this->matchgrades);
132 if ($newfraction===false) {
133 $answersvalid = false;
135 else {
136 $fractions[$key] = $newfraction;
139 if (!$answersvalid) {
140 notify( get_string('matcherror','quiz') );
141 continue;
143 else {
144 $question->fraction = $fractions;
148 $question->category = $this->category->id;
149 $question->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
151 if (!$question->id = insert_record("question", $question)) {
152 error( get_string('cannotinsert','quiz') );
155 $this->questionids[] = $question->id;
157 // Now to save all the answers and type-specific options
159 global $QTYPES;
160 $result = $QTYPES[$question->qtype]
161 ->save_question_options($question);
163 if (!empty($result->error)) {
164 notify($result->error);
165 return false;
168 if (!empty($result->notice)) {
169 notify($result->notice);
170 return true;
173 // Give the question a unique version stamp determined by question_hash()
174 set_field('question', 'version', question_hash($question), 'id', $question->id);
176 return true;
180 function readdata($filename) {
181 /// Returns complete file with an array, one item per line
183 if (is_readable($filename)) {
184 $filearray = file($filename);
186 /// Check for Macintosh OS line returns (ie file on one line), and fix
187 if (ereg("\r", $filearray[0]) AND !ereg("\n", $filearray[0])) {
188 return explode("\r", $filearray[0]);
189 } else {
190 return $filearray;
193 return false;
196 function readquestions($lines) {
197 /// Parses an array of lines into an array of questions,
198 /// where each item is a question object as defined by
199 /// readquestion(). Questions are defined as anything
200 /// between blank lines.
202 $questions = array();
203 $currentquestion = array();
205 foreach ($lines as $line) {
206 $line = trim($line);
207 if (empty($line)) {
208 if (!empty($currentquestion)) {
209 if ($question = $this->readquestion($currentquestion)) {
210 $questions[] = $question;
212 $currentquestion = array();
214 } else {
215 $currentquestion[] = $line;
219 if (!empty($currentquestion)) { // There may be a final question
220 if ($question = $this->readquestion($currentquestion)) {
221 $questions[] = $question;
225 return $questions;
229 function defaultquestion() {
230 // returns an "empty" question
231 // Somewhere to specify question parameters that are not handled
232 // by import but are required db fields.
233 // This should not be overridden.
234 global $CFG;
236 $question = new stdClass();
237 $question->shuffleanswers = $CFG->quiz_shuffleanswers;
238 $question->defaultgrade = 1;
239 $question->image = "";
240 $question->usecase = 0;
241 $question->multiplier = array();
242 $question->generalfeedback = '';
243 $question->correctfeedback = '';
244 $question->partiallycorrectfeedback = '';
245 $question->incorrectfeedback = '';
247 return $question;
250 function readquestion($lines) {
251 /// Given an array of lines known to define a question in
252 /// this format, this function converts it into a question
253 /// object suitable for processing and insertion into Moodle.
255 $formatnotimplemented = get_string( 'formatnotimplemented','quiz' );
256 echo "<p>$formatnotimplemented</p>";
258 return NULL;
262 function importpostprocess() {
263 /// Does any post-processing that may be desired
264 /// Argument is a simple array of question ids that
265 /// have just been added.
267 return true;
270 function importimagefile( $path, $base64 ) {
271 /// imports an image file encoded in base64 format
272 /// This should not be overridden.
273 global $CFG;
275 // all this to get the destination directory
276 // and filename!
277 $fullpath = "{$CFG->dataroot}/{$this->course->id}/$path";
278 $path_parts = pathinfo( $fullpath );
279 $destination = $path_parts['dirname'];
280 $file = clean_filename( $path_parts['basename'] );
282 // detect and fix any filename collision - get unique filename
283 $newfiles = resolve_filename_collisions( $destination, array($file) );
284 $newfile = $newfiles[0];
286 // convert and save file contents
287 if (!$content = base64_decode( $base64 )) {
288 return false;
290 $newfullpath = "$destination/$newfile";
291 if (!$fh = fopen( $newfullpath, 'w' )) {
292 return false;
294 if (!fwrite( $fh, $content )) {
295 return false;
297 fclose( $fh );
299 // return the (possibly) new filename
300 return $newfile;
303 //=================
304 // Export functions
305 //=================
307 function export_file_extension() {
308 /// return the files extension appropriate for this type
309 /// override if you don't want .txt
311 return ".txt";
314 function exportpreprocess() {
315 /// Does any pre-processing that may be desired
317 return true;
320 function presave_process( $content ) {
321 /// enables any processing to be done on the content
322 /// just prior to the file being saved
323 /// default is to do nothing
325 return $content;
328 function exportprocess() {
329 /// Exports a given category. There's probably little need to change this
331 global $CFG;
333 // create a directory for the exports (if not already existing)
334 if (! $export_dir = make_upload_directory($this->question_get_export_dir())) {
335 error( get_string('cannotcreatepath','quiz',$export_dir) );
337 $path = $CFG->dataroot.'/'.$this->question_get_export_dir();
339 // get the questions (from database) in this category
340 // only get q's with no parents (no cloze subquestions specifically)
341 $questions = get_questions_category( $this->category, true );
343 notify( get_string('exportingquestions','quiz') );
344 if (!count($questions)) {
345 notify( get_string('noquestions','quiz') );
346 return false;
348 $count = 0;
350 // results are first written into string (and then to a file)
351 // so create/initialize the string here
352 $expout = "";
354 // iterate through questions
355 foreach($questions as $question) {
356 // do not export hidden questions
357 if (!empty($question->hidden)) {
358 continue;
361 // do not export random questions
362 if ($question->qtype==RANDOM) {
363 continue;
366 // export the question displaying message
367 $count++;
368 echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
369 $expout .= $this->writequestion( $question ) . "\n";
372 // final pre-process on exported data
373 $expout = $this->presave_process( $expout );
375 // write file
376 $filepath = $path."/".$this->filename . $this->export_file_extension();
377 if (!$fh=fopen($filepath,"w")) {
378 error( get_string('cannotopen','quiz',$filepath) );
380 if (!fwrite($fh, $expout)) {
381 error( get_string('cannotwrite','quiz',$filepath) );
383 fclose($fh);
385 return true;
388 function exportpostprocess() {
389 /// Does any post-processing that may be desired
391 return true;
394 function writequestion($question) {
395 /// Turns a question object into textual output in the given format
396 /// must be overidden
398 // if not overidden, then this is an error.
399 $formatnotimplemented = get_string( 'formatnotimplemented','quiz' );
400 echo "<p>$formatnotimplemented</p>";
402 return NULL;
405 function question_get_export_dir() {
406 $dirname = get_string("exportfilename","quiz");
407 $path = $this->course->id.'/backupdata/'.$dirname; // backupdata is protected directory
408 return $path;