"MDL-13766, fix event register bug"
[moodle.git] / question / format.php
CommitLineData
271e6dec 1<?php // $Id$
4323d029 2/**
3 * Base class for question import and export formats.
4 *
5 * @author Martin Dougiamas, Howard Miller, and many others.
6 * {@link http://moodle.org}
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package questionbank
9 * @subpackage importexport
4323d029 10 */
f5565b69 11class qformat_default {
aca318e1 12
f1abd39f 13 var $displayerrors = true;
aca318e1 14 var $category = NULL;
2c44a3d3 15 var $questions = array();
aca318e1 16 var $course = NULL;
08892d5b 17 var $filename = '';
73b7b195 18 var $realfilename = '';
08892d5b 19 var $matchgrades = 'error';
20 var $catfromfile = 0;
271e6dec 21 var $contextfromfile = 0;
f1abd39f 22 var $cattofile = 0;
271e6dec 23 var $contexttofile = 0;
aca318e1 24 var $questionids = array();
f3701561 25 var $importerrors = 0;
26 var $stoponerror = true;
271e6dec 27 var $translator = null;
1b8b535d 28 var $canaccessbackupdata = true;
271e6dec 29
aca318e1 30
31// functions to indicate import/export functionality
32// override to return true if implemented
33
34 function provide_import() {
35 return false;
36 }
37
38 function provide_export() {
39 return false;
40 }
41
08892d5b 42// Accessor methods
aca318e1 43
08892d5b 44 /**
45 * set the category
46 * @param object category the category object
47 */
48 function setCategory( $category ) {
2c44a3d3 49 if (count($this->questions)){
50 debugging('You shouldn\'t call setCategory after setQuestions');
51 }
08892d5b 52 $this->category = $category;
53 }
aca318e1 54
2c44a3d3 55 /**
56 * Set the specific questions to export. Should not include questions with
57 * parents (sub questions of cloze question type).
58 * Only used for question export.
59 * @param array of question objects
60 */
61 function setQuestions( $questions ) {
62 if ($this->category !== null){
63 debugging('You shouldn\'t call setQuestions after setCategory');
64 }
65 $this->questions = $questions;
66 }
67
08892d5b 68 /**
69 * set the course class variable
70 * @param course object Moodle course variable
71 */
72 function setCourse( $course ) {
aca318e1 73 $this->course = $course;
08892d5b 74 }
271e6dec 75 /**
76 * set an array of contexts.
77 * @param array $contexts Moodle course variable
78 */
79 function setContexts($contexts) {
80 $this->contexts = $contexts;
81 $this->translator = new context_to_string_translator($this->contexts);
82 }
aca318e1 83
08892d5b 84 /**
85 * set the filename
86 * @param string filename name of file to import/export
87 */
88 function setFilename( $filename ) {
89 $this->filename = $filename;
90 }
73b7b195 91
92 /**
93 * set the "real" filename
94 * (this is what the user typed, regardless of wha happened next)
95 * @param string realfilename name of file as typed by user
96 */
97 function setRealfilename( $realfilename ) {
98 $this->realfilename = $realfilename;
99 }
08892d5b 100
101 /**
102 * set matchgrades
103 * @param string matchgrades error or nearest for grades
104 */
105 function setMatchgrades( $matchgrades ) {
106 $this->matchgrades = $matchgrades;
aca318e1 107 }
108
76f0a334 109 /**
08892d5b 110 * set catfromfile
111 * @param bool catfromfile allow categories embedded in import file
76f0a334 112 */
08892d5b 113 function setCatfromfile( $catfromfile ) {
114 $this->catfromfile = $catfromfile;
115 }
271e6dec 116
117 /**
118 * set contextfromfile
119 * @param bool $contextfromfile allow contexts embedded in import file
120 */
121 function setContextfromfile($contextfromfile) {
122 $this->contextfromfile = $contextfromfile;
123 }
124
f1abd39f 125 /**
126 * set cattofile
127 * @param bool cattofile exports categories within export file
128 */
129 function setCattofile( $cattofile ) {
130 $this->cattofile = $cattofile;
271e6dec 131 }
132 /**
133 * set contexttofile
134 * @param bool cattofile exports categories within export file
135 */
136 function setContexttofile($contexttofile) {
137 $this->contexttofile = $contexttofile;
138 }
aca318e1 139
f3701561 140 /**
141 * set stoponerror
142 * @param bool stoponerror stops database write if any errors reported
143 */
144 function setStoponerror( $stoponerror ) {
145 $this->stoponerror = $stoponerror;
146 }
147
1b8b535d 148 /**
149 * @param boolean $canaccess Whether the current use can access the backup data folder. Determines
150 * where export files are saved.
151 */
152 function set_can_access_backupdata($canaccess) {
153 $this->canaccessbackupdata = $canaccess;
154 }
155
f3701561 156/***********************
157 * IMPORTING FUNCTIONS
158 ***********************/
159
160 /**
161 * Handle parsing error
162 */
163 function error( $message, $text='', $questionname='' ) {
cdeabc06 164 $importerrorquestion = get_string('importerrorquestion','quiz');
165
f3701561 166 echo "<div class=\"importerror\">\n";
cdeabc06 167 echo "<strong>$importerrorquestion $questionname</strong>";
f3701561 168 if (!empty($text)) {
169 $text = s($text);
170 echo "<blockquote>$text</blockquote>\n";
171 }
172 echo "<strong>$message</strong>\n";
173 echo "</div>";
174
175 $this->importerrors++;
176 }
08892d5b 177
271e6dec 178 /**
a41e3287 179 * Import for questiontype plugins
180 * Do not override.
181 * @param data mixed The segment of data containing the question
182 * @param question object processed (so far) by standard import code if appropriate
183 * @param extra mixed any additional format specific data that may be passed by the format
184 * @return object question object suitable for save_options() or false if cannot handle
185 */
186 function try_importing_using_qtypes( $data, $question=null, $extra=null ) {
187 global $QTYPES;
188
189 // work out what format we are using
190 $formatname = substr( get_class( $this ), strlen('qformat_'));
191 $methodname = "import_from_$formatname";
192
193 // loop through installed questiontypes checking for
194 // function to handle this question
195 foreach ($QTYPES as $qtype) {
196 if (method_exists( $qtype, $methodname)) {
197 if ($question = $qtype->$methodname( $data, $question, $this, $extra )) {
198 return $question;
199 }
200 }
271e6dec 201 }
202 return false;
a41e3287 203 }
204
08892d5b 205 /**
206 * Perform any required pre-processing
f3701561 207 * @return boolean success
08892d5b 208 */
209 function importpreprocess() {
210 return true;
211 }
212
213 /**
214 * Process the file
215 * This method should not normally be overidden
f3701561 216 * @return boolean success
08892d5b 217 */
218 function importprocess() {
f34488b2 219 global $USER, $DB;
f3701561 220
67c12527 221 // reset the timer in case file upload was slow
222 @set_time_limit();
223
f3701561 224 // STAGE 1: Parse the file
225 notify( get_string('parsingquestions','quiz') );
271e6dec 226
08892d5b 227 if (! $lines = $this->readdata($this->filename)) {
1e3d6fd8 228 notify( get_string('cannotread','quiz') );
aca318e1 229 return false;
230 }
231
232 if (! $questions = $this->readquestions($lines)) { // Extract all the questions
1e3d6fd8 233 notify( get_string('noquestionsinfile','quiz') );
aca318e1 234 return false;
235 }
236
f3701561 237 // STAGE 2: Write data to database
ce2df288 238 notify( get_string('importingquestions','quiz',$this->count_questions($questions)) );
aca318e1 239
f3701561 240 // check for errors before we continue
241 if ($this->stoponerror and ($this->importerrors>0)) {
242 return false;
243 }
244
76f0a334 245 // get list of valid answer grades
246 $grades = get_grade_options();
247 $gradeoptionsfull = $grades->gradeoptionsfull;
248
c1828e0b 249 // check answer grades are valid
250 // (now need to do this here because of 'stop on error': MDL-10689)
251 $gradeerrors = 0;
252 $goodquestions = array();
253 foreach ($questions as $question) {
254 if (!empty($question->fraction) and (is_array($question->fraction))) {
255 $fractions = $question->fraction;
256 $answersvalid = true; // in case they are!
257 foreach ($fractions as $key => $fraction) {
258 $newfraction = match_grade_options($gradeoptionsfull, $fraction, $this->matchgrades);
259 if ($newfraction===false) {
260 $answersvalid = false;
261 }
262 else {
263 $fractions[$key] = $newfraction;
264 }
265 }
266 if (!$answersvalid) {
267 notify(get_string('matcherror', 'quiz'));
268 ++$gradeerrors;
269 continue;
270 }
271 else {
272 $question->fraction = $fractions;
273 }
274 }
275 $goodquestions[] = $question;
276 }
277 $questions = $goodquestions;
278
279 // check for errors before we continue
280 if ($this->stoponerror and ($gradeerrors>0)) {
281 return false;
282 }
283
284 // count number of questions processed
aca318e1 285 $count = 0;
286
287 foreach ($questions as $question) { // Process and store each question
08892d5b 288
271e6dec 289 // reset the php timeout
67c12527 290 @set_time_limit();
291
08892d5b 292 // check for category modifiers
293 if ($question->qtype=='category') {
294 if ($this->catfromfile) {
295 // find/create category object
40e71443 296 $catpath = $question->category;
271e6dec 297 $newcategory = $this->create_category_path( $catpath, '/');
08892d5b 298 if (!empty($newcategory)) {
299 $this->category = $newcategory;
300 }
301 }
271e6dec 302 continue;
08892d5b 303 }
304
aca318e1 305 $count++;
306
5b0dc681 307 echo "<hr /><p><b>$count</b>. ".$this->format_question_text($question)."</p>";
aca318e1 308
309 $question->category = $this->category->id;
310 $question->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
aca318e1 311
271e6dec 312 $question->createdby = $USER->id;
313 $question->timecreated = time();
314
f34488b2 315 if (!$question->id = $DB->insert_record("question", $question)) {
5a2a5331 316 print_error('cannotinsert','quiz');
aca318e1 317 }
318
319 $this->questionids[] = $question->id;
320
321 // Now to save all the answers and type-specific options
322
323 global $QTYPES;
324 $result = $QTYPES[$question->qtype]
325 ->save_question_options($question);
326
327 if (!empty($result->error)) {
328 notify($result->error);
329 return false;
330 }
331
332 if (!empty($result->notice)) {
333 notify($result->notice);
334 return true;
335 }
cbe20043 336
337 // Give the question a unique version stamp determined by question_hash()
e5d7d1dc 338 $DB->set_field('question', 'version', question_hash($question), array('id'=>$question->id));
aca318e1 339 }
340 return true;
341 }
ce2df288 342 /**
343 * Count all non-category questions in the questions array.
f34488b2 344 *
ce2df288 345 * @param array questions An array of question objects.
346 * @return int The count.
f34488b2 347 *
ce2df288 348 */
349 function count_questions($questions) {
350 $count = 0;
351 if (!is_array($questions)) {
352 return $count;
353 }
354 foreach ($questions as $question) {
355 if (!is_object($question) || !isset($question->qtype) || ($question->qtype == 'category')) {
356 continue;
357 }
358 $count++;
359 }
360 return $count;
361 }
362
271e6dec 363 /**
364 * find and/or create the category described by a delimited list
365 * e.g. $course$/tom/dick/harry or tom/dick/harry
366 *
367 * removes any context string no matter whether $getcontext is set
368 * but if $getcontext is set then ignore the context and use selected category context.
369 *
370 * @param string catpath delimited category path
371 * @param string delimiter path delimiting character
372 * @param int courseid course to search for categories
373 * @return mixed category object or null if fails
374 */
375 function create_category_path($catpath, $delimiter='/') {
f34488b2 376 global $DB;
271e6dec 377 $catpath = clean_param($catpath, PARAM_PATH);
378 $catnames = explode($delimiter, $catpath);
379 $parent = 0;
380 $category = null;
5ca9e32d 381
382 // check for context id in path, it might not be there in pre 1.9 exports
383 $matchcount = preg_match('/^\$([a-z]+)\$$/', $catnames[0], $matches);
384 if ($matchcount==1) {
271e6dec 385 $contextid = $this->translator->string_to_context($matches[1]);
386 array_shift($catnames);
387 } else {
388 $contextid = FALSE;
389 }
390 if ($this->contextfromfile && ($contextid !== FALSE)){
391 $context = get_context_instance_by_id($contextid);
392 require_capability('moodle/question:add', $context);
393 } else {
394 $context = get_context_instance_by_id($this->category->contextid);
395 }
396 foreach ($catnames as $catname) {
f34488b2 397 if ($category = $DB->get_record( 'question_categories', array('name' => $catname, 'contextid' => $context->id, 'parent' => $parent))) {
271e6dec 398 $parent = $category->id;
399 } else {
400 require_capability('moodle/question:managecategory', $context);
401 // create the new category
402 $category = new object;
403 $category->contextid = $context->id;
404 $category->name = $catname;
405 $category->info = '';
406 $category->parent = $parent;
407 $category->sortorder = 999;
408 $category->stamp = make_unique_id_code();
f34488b2 409 if (!($id = $DB->insert_record('question_categories', $category))) {
d9f30321 410 print_error("cannotcreatecategory");
271e6dec 411 }
412 $category->id = $id;
413 $parent = $id;
414 }
415 }
416 return $category;
417 }
3f5633df 418
f3701561 419 /**
420 * Return complete file within an array, one item per line
421 * @param string filename name of file
422 * @return mixed contents array or false on failure
423 */
aca318e1 424 function readdata($filename) {
aca318e1 425 if (is_readable($filename)) {
426 $filearray = file($filename);
427
428 /// Check for Macintosh OS line returns (ie file on one line), and fix
429 if (ereg("\r", $filearray[0]) AND !ereg("\n", $filearray[0])) {
430 return explode("\r", $filearray[0]);
431 } else {
432 return $filearray;
433 }
434 }
435 return false;
436 }
437
f3701561 438 /**
271e6dec 439 * Parses an array of lines into an array of questions,
440 * where each item is a question object as defined by
441 * readquestion(). Questions are defined as anything
f3701561 442 * between blank lines.
443 *
444 * If your format does not use blank lines as a delimiter
445 * then you will need to override this method. Even then
446 * try to use readquestion for each question
447 * @param array lines array of lines from readdata
448 * @return array array of question objects
449 */
aca318e1 450 function readquestions($lines) {
271e6dec 451
aca318e1 452 $questions = array();
453 $currentquestion = array();
454
455 foreach ($lines as $line) {
456 $line = trim($line);
457 if (empty($line)) {
458 if (!empty($currentquestion)) {
459 if ($question = $this->readquestion($currentquestion)) {
460 $questions[] = $question;
461 }
462 $currentquestion = array();
463 }
464 } else {
465 $currentquestion[] = $line;
466 }
467 }
468
469 if (!empty($currentquestion)) { // There may be a final question
470 if ($question = $this->readquestion($currentquestion)) {
471 $questions[] = $question;
472 }
473 }
474
475 return $questions;
476 }
477
478
f3701561 479 /**
480 * return an "empty" question
481 * Somewhere to specify question parameters that are not handled
482 * by import but are required db fields.
483 * This should not be overridden.
484 * @return object default question
271e6dec 485 */
aca318e1 486 function defaultquestion() {
b0679efa 487 global $CFG;
271e6dec 488
aca318e1 489 $question = new stdClass();
271e6dec 490 $question->shuffleanswers = $CFG->quiz_shuffleanswers;
aca318e1 491 $question->defaultgrade = 1;
492 $question->image = "";
493 $question->usecase = 0;
494 $question->multiplier = array();
172f6d95 495 $question->generalfeedback = '';
08892d5b 496 $question->correctfeedback = '';
497 $question->partiallycorrectfeedback = '';
498 $question->incorrectfeedback = '';
5931ea94 499 $question->answernumbering = 'abc';
46013523 500 $question->penalty = 0.1;
3f5633df 501 $question->length = 1;
aca318e1 502
5fd8f999 503 // this option in case the questiontypes class wants
504 // to know where the data came from
505 $question->export_process = true;
093414d2 506 $question->import_process = true;
5fd8f999 507
aca318e1 508 return $question;
509 }
510
f3701561 511 /**
271e6dec 512 * Given the data known to define a question in
513 * this format, this function converts it into a question
f3701561 514 * object suitable for processing and insertion into Moodle.
515 *
516 * If your format does not use blank lines to delimit questions
517 * (e.g. an XML format) you must override 'readquestions' too
518 * @param $lines mixed data that represents question
519 * @return object question object
520 */
aca318e1 521 function readquestion($lines) {
aca318e1 522
1e3d6fd8 523 $formatnotimplemented = get_string( 'formatnotimplemented','quiz' );
524 echo "<p>$formatnotimplemented</p>";
aca318e1 525
526 return NULL;
527 }
528
f3701561 529 /**
530 * Override if any post-processing is required
531 * @return boolean success
532 */
aca318e1 533 function importpostprocess() {
aca318e1 534 return true;
535 }
536
f3701561 537 /**
538 * Import an image file encoded in base64 format
539 * @param string path path (in course data) to store picture
540 * @param string base64 encoded picture
541 * @return string filename (nb. collisions are handled)
542 */
d08e16b2 543 function importimagefile( $path, $base64 ) {
d08e16b2 544 global $CFG;
545
546 // all this to get the destination directory
547 // and filename!
548 $fullpath = "{$CFG->dataroot}/{$this->course->id}/$path";
549 $path_parts = pathinfo( $fullpath );
550 $destination = $path_parts['dirname'];
551 $file = clean_filename( $path_parts['basename'] );
552
71735794 553 // check if path exists
554 check_dir_exists($destination, true, true );
555
d08e16b2 556 // detect and fix any filename collision - get unique filename
271e6dec 557 $newfiles = resolve_filename_collisions( $destination, array($file) );
d08e16b2 558 $newfile = $newfiles[0];
559
560 // convert and save file contents
561 if (!$content = base64_decode( $base64 )) {
46013523 562 return '';
d08e16b2 563 }
564 $newfullpath = "$destination/$newfile";
565 if (!$fh = fopen( $newfullpath, 'w' )) {
46013523 566 return '';
d08e16b2 567 }
568 if (!fwrite( $fh, $content )) {
46013523 569 return '';
d08e16b2 570 }
571 fclose( $fh );
572
573 // return the (possibly) new filename
71735794 574 $newfile = ereg_replace("{$CFG->dataroot}/{$this->course->id}/", '',$newfullpath);
d08e16b2 575 return $newfile;
576 }
577
3f5633df 578
f3701561 579/*******************
580 * EXPORT FUNCTIONS
581 *******************/
aca318e1 582
271e6dec 583 /**
a41e3287 584 * Provide export functionality for plugin questiontypes
585 * Do not override
586 * @param name questiontype name
271e6dec 587 * @param question object data to export
a41e3287 588 * @param extra mixed any addition format specific data needed
589 * @return string the data to append to export or false if error (or unhandled)
590 */
591 function try_exporting_using_qtypes( $name, $question, $extra=null ) {
592 global $QTYPES;
593
594 // work out the name of format in use
595 $formatname = substr( get_class( $this ), strlen( 'qformat_' ));
596 $methodname = "export_to_$formatname";
597
598 if (array_key_exists( $name, $QTYPES )) {
599 $qtype = $QTYPES[ $name ];
600 if (method_exists( $qtype, $methodname )) {
601 if ($data = $qtype->$methodname( $question, $this, $extra )) {
602 return $data;
603 }
604 }
605 }
606 return false;
607 }
608
f3701561 609 /**
610 * Return the files extension appropriate for this type
611 * override if you don't want .txt
612 * @return string file extension
613 */
aca318e1 614 function export_file_extension() {
aca318e1 615 return ".txt";
616 }
617
f3701561 618 /**
619 * Do any pre-processing that may be required
620 * @param boolean success
621 */
08892d5b 622 function exportpreprocess() {
aca318e1 623 return true;
624 }
625
f3701561 626 /**
627 * Enable any processing to be done on the content
628 * just prior to the file being saved
629 * default is to do nothing
630 * @param string output text
631 * @param string processed output text
632 */
aca318e1 633 function presave_process( $content ) {
aca318e1 634 return $content;
635 }
636
f3701561 637 /**
638 * Do the export
639 * For most types this should not need to be overrided
640 * @return boolean success
641 */
08892d5b 642 function exportprocess() {
aca318e1 643 global $CFG;
644
645 // create a directory for the exports (if not already existing)
1367cb8d 646 if (! $export_dir = make_upload_directory($this->question_get_export_dir())) {
5a2a5331 647 print_error('cannotcreatepath', 'quiz', $export_dir);
aca318e1 648 }
1367cb8d 649 $path = $CFG->dataroot.'/'.$this->question_get_export_dir();
aca318e1 650
651 // get the questions (from database) in this category
652 // only get q's with no parents (no cloze subquestions specifically)
2c44a3d3 653 if ($this->category){
654 $questions = get_questions_category( $this->category, true );
655 } else {
656 $questions = $this->questions;
657 }
aca318e1 658
1e3d6fd8 659 notify( get_string('exportingquestions','quiz') );
aca318e1 660 $count = 0;
661
662 // results are first written into string (and then to a file)
663 // so create/initialize the string here
664 $expout = "";
271e6dec 665
f1abd39f 666 // track which category questions are in
667 // if it changes we will record the category change in the output
668 // file if selected. 0 means that it will get printed before the 1st question
669 $trackcategory = 0;
aca318e1 670
671 // iterate through questions
672 foreach($questions as $question) {
271e6dec 673
a9b16aff 674 // do not export hidden questions
675 if (!empty($question->hidden)) {
676 continue;
677 }
678
679 // do not export random questions
680 if ($question->qtype==RANDOM) {
681 continue;
682 }
271e6dec 683
f1abd39f 684 // check if we need to record category change
685 if ($this->cattofile) {
686 if ($question->category != $trackcategory) {
271e6dec 687 $trackcategory = $question->category;
688 $categoryname = $this->get_category_path($trackcategory, '/', $this->contexttofile);
689
f1abd39f 690 // create 'dummy' question for category export
691 $dummyquestion = new object;
692 $dummyquestion->qtype = 'category';
693 $dummyquestion->category = $categoryname;
694 $dummyquestion->name = "switch category to $categoryname";
695 $dummyquestion->id = 0;
696 $dummyquestion->questiontextformat = '';
697 $expout .= $this->writequestion( $dummyquestion ) . "\n";
271e6dec 698 }
699 }
f1abd39f 700
701 // export the question displaying message
702 $count++;
5b0dc681 703 echo "<hr /><p><b>$count</b>. ".$this->format_question_text($question)."</p>";
0647e82b 704 if (question_has_capability_on($question, 'view', $question->category)){
705 $expout .= $this->writequestion( $question ) . "\n";
706 }
a9b16aff 707 }
aca318e1 708
2c6d2c88 709 // continue path for following error checks
710 $course = $this->course;
2c44a3d3 711 $continuepath = "$CFG->wwwroot/question/export.php?courseid=$course->id";
2c6d2c88 712
713 // did we actually process anything
714 if ($count==0) {
2c44a3d3 715 print_error( 'noquestions','quiz',$continuepath );
2c6d2c88 716 }
717
aca318e1 718 // final pre-process on exported data
719 $expout = $this->presave_process( $expout );
271e6dec 720
aca318e1 721 // write file
08892d5b 722 $filepath = $path."/".$this->filename . $this->export_file_extension();
aca318e1 723 if (!$fh=fopen($filepath,"w")) {
2c6d2c88 724 print_error( 'cannotopen','quiz',$continuepath,$filepath );
aca318e1 725 }
f1abd39f 726 if (!fwrite($fh, $expout, strlen($expout) )) {
2c6d2c88 727 print_error( 'cannotwrite','quiz',$continuepath,$filepath );
aca318e1 728 }
729 fclose($fh);
aca318e1 730 return true;
731 }
3f5633df 732
271e6dec 733 /**
734 * get the category as a path (e.g., tom/dick/harry)
735 * @param int id the id of the most nested catgory
736 * @param string delimiter the delimiter you want
737 * @return string the path
738 */
739 function get_category_path($id, $delimiter='/', $includecontext = true) {
f34488b2 740 global $DB;
271e6dec 741 $path = '';
f34488b2 742 if (!$firstcategory = $DB->get_record('question_categories',array('id' =>$id))) {
1e7386c9 743 print_error('cannotfindcategory', 'error', '', $id);
271e6dec 744 }
745 $category = $firstcategory;
746 $contextstring = $this->translator->context_to_string($category->contextid);
747 do {
748 $name = $category->name;
749 $id = $category->parent;
750 if (!empty($path)) {
751 $path = "{$name}{$delimiter}{$path}";
752 }
753 else {
754 $path = $name;
755 }
f34488b2 756 } while ($category = $DB->get_record( 'question_categories',array('id' =>$id )));
271e6dec 757
758 if ($includecontext){
759 $path = '$'.$contextstring.'$'."{$delimiter}{$path}";
760 }
761 return $path;
762 }
aca318e1 763
f3701561 764 /**
765 * Do an post-processing that may be required
766 * @return boolean success
767 */
aca318e1 768 function exportpostprocess() {
aca318e1 769 return true;
770 }
771
f3701561 772 /**
773 * convert a single question object into text output in the given
774 * format.
775 * This must be overriden
776 * @param object question question object
777 * @return mixed question export text or null if not implemented
778 */
aca318e1 779 function writequestion($question) {
1e3d6fd8 780 // if not overidden, then this is an error.
781 $formatnotimplemented = get_string( 'formatnotimplemented','quiz' );
782 echo "<p>$formatnotimplemented</p>";
aca318e1 783 return NULL;
784 }
785
f3701561 786 /**
271e6dec 787 * get directory into which export is going
f3701561 788 * @return string file path
789 */
1367cb8d 790 function question_get_export_dir() {
1b8b535d 791 global $USER;
792 if ($this->canaccessbackupdata) {
793 $dirname = get_string("exportfilename","quiz");
794 $path = $this->course->id.'/backupdata/'.$dirname; // backupdata is protected directory
795 } else {
796 $path = 'temp/questionexport/' . $USER->id;
797 }
1367cb8d 798 return $path;
799 }
800
f3701561 801 /**
802 * where question specifies a moodle (text) format this
803 * performs the conversion.
804 */
5b0dc681 805 function format_question_text($question) {
806 $formatoptions = new stdClass;
807 $formatoptions->noclean = true;
808 $formatoptions->para = false;
809 if (empty($question->questiontextformat)) {
810 $format = FORMAT_MOODLE;
811 } else {
812 $format = $question->questiontextformat;
813 }
294ce987 814 return format_text($question->questiontext, $format, $formatoptions);
5b0dc681 815 }
271e6dec 816
817
aca318e1 818}
819
820?>