quiz: MDL-19145 Safe Exam Browser integration.
[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() {
fef8f84e 219 global $USER, $DB, $OUTPUT;
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
fef8f84e 225 echo $OUTPUT->notification( get_string('parsingquestions','quiz') );
271e6dec 226
08892d5b 227 if (! $lines = $this->readdata($this->filename)) {
fef8f84e 228 echo $OUTPUT->notification( get_string('cannotread','quiz') );
aca318e1 229 return false;
230 }
231
232 if (! $questions = $this->readquestions($lines)) { // Extract all the questions
fef8f84e 233 echo $OUTPUT->notification( get_string('noquestionsinfile','quiz') );
aca318e1 234 return false;
235 }
236
f3701561 237 // STAGE 2: Write data to database
fef8f84e 238 echo $OUTPUT->notification( 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)) {
fef8f84e 242 echo $OUTPUT->notification( get_string('importparseerror','quiz') );
10b4a508 243 return true;
f3701561 244 }
245
76f0a334 246 // get list of valid answer grades
247 $grades = get_grade_options();
248 $gradeoptionsfull = $grades->gradeoptionsfull;
249
c1828e0b 250 // check answer grades are valid
251 // (now need to do this here because of 'stop on error': MDL-10689)
252 $gradeerrors = 0;
253 $goodquestions = array();
254 foreach ($questions as $question) {
255 if (!empty($question->fraction) and (is_array($question->fraction))) {
256 $fractions = $question->fraction;
257 $answersvalid = true; // in case they are!
258 foreach ($fractions as $key => $fraction) {
259 $newfraction = match_grade_options($gradeoptionsfull, $fraction, $this->matchgrades);
260 if ($newfraction===false) {
261 $answersvalid = false;
262 }
263 else {
264 $fractions[$key] = $newfraction;
265 }
266 }
267 if (!$answersvalid) {
fef8f84e 268 echo $OUTPUT->notification(get_string('matcherror', 'quiz'));
c1828e0b 269 ++$gradeerrors;
270 continue;
271 }
272 else {
273 $question->fraction = $fractions;
274 }
275 }
276 $goodquestions[] = $question;
277 }
278 $questions = $goodquestions;
279
280 // check for errors before we continue
281 if ($this->stoponerror and ($gradeerrors>0)) {
282 return false;
283 }
284
285 // count number of questions processed
aca318e1 286 $count = 0;
287
288 foreach ($questions as $question) { // Process and store each question
08892d5b 289
271e6dec 290 // reset the php timeout
67c12527 291 @set_time_limit();
292
08892d5b 293 // check for category modifiers
294 if ($question->qtype=='category') {
295 if ($this->catfromfile) {
296 // find/create category object
40e71443 297 $catpath = $question->category;
271e6dec 298 $newcategory = $this->create_category_path( $catpath, '/');
08892d5b 299 if (!empty($newcategory)) {
300 $this->category = $newcategory;
301 }
302 }
271e6dec 303 continue;
08892d5b 304 }
305
aca318e1 306 $count++;
307
5b0dc681 308 echo "<hr /><p><b>$count</b>. ".$this->format_question_text($question)."</p>";
aca318e1 309
310 $question->category = $this->category->id;
311 $question->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
aca318e1 312
271e6dec 313 $question->createdby = $USER->id;
314 $question->timecreated = time();
315
bb4b6010 316 $question->id = $DB->insert_record("question", $question);
aca318e1 317
318 $this->questionids[] = $question->id;
319
320 // Now to save all the answers and type-specific options
321
322 global $QTYPES;
323 $result = $QTYPES[$question->qtype]
324 ->save_question_options($question);
325
326 if (!empty($result->error)) {
fef8f84e 327 echo $OUTPUT->notification($result->error);
aca318e1 328 return false;
329 }
330
331 if (!empty($result->notice)) {
fef8f84e 332 echo $OUTPUT->notification($result->notice);
aca318e1 333 return true;
334 }
cbe20043 335
336 // Give the question a unique version stamp determined by question_hash()
e5d7d1dc 337 $DB->set_field('question', 'version', question_hash($question), array('id'=>$question->id));
aca318e1 338 }
339 return true;
340 }
ce2df288 341 /**
342 * Count all non-category questions in the questions array.
f34488b2 343 *
ce2df288 344 * @param array questions An array of question objects.
345 * @return int The count.
f34488b2 346 *
ce2df288 347 */
348 function count_questions($questions) {
349 $count = 0;
350 if (!is_array($questions)) {
351 return $count;
352 }
353 foreach ($questions as $question) {
354 if (!is_object($question) || !isset($question->qtype) || ($question->qtype == 'category')) {
355 continue;
356 }
357 $count++;
358 }
359 return $count;
360 }
361
271e6dec 362 /**
363 * find and/or create the category described by a delimited list
364 * e.g. $course$/tom/dick/harry or tom/dick/harry
365 *
366 * removes any context string no matter whether $getcontext is set
367 * but if $getcontext is set then ignore the context and use selected category context.
368 *
369 * @param string catpath delimited category path
370 * @param string delimiter path delimiting character
371 * @param int courseid course to search for categories
372 * @return mixed category object or null if fails
373 */
374 function create_category_path($catpath, $delimiter='/') {
f34488b2 375 global $DB;
271e6dec 376 $catpath = clean_param($catpath, PARAM_PATH);
377 $catnames = explode($delimiter, $catpath);
378 $parent = 0;
379 $category = null;
5ca9e32d 380
381 // check for context id in path, it might not be there in pre 1.9 exports
382 $matchcount = preg_match('/^\$([a-z]+)\$$/', $catnames[0], $matches);
383 if ($matchcount==1) {
271e6dec 384 $contextid = $this->translator->string_to_context($matches[1]);
385 array_shift($catnames);
386 } else {
387 $contextid = FALSE;
388 }
389 if ($this->contextfromfile && ($contextid !== FALSE)){
390 $context = get_context_instance_by_id($contextid);
391 require_capability('moodle/question:add', $context);
392 } else {
393 $context = get_context_instance_by_id($this->category->contextid);
394 }
395 foreach ($catnames as $catname) {
f34488b2 396 if ($category = $DB->get_record( 'question_categories', array('name' => $catname, 'contextid' => $context->id, 'parent' => $parent))) {
271e6dec 397 $parent = $category->id;
398 } else {
399 require_capability('moodle/question:managecategory', $context);
400 // create the new category
401 $category = new object;
402 $category->contextid = $context->id;
403 $category->name = $catname;
404 $category->info = '';
405 $category->parent = $parent;
406 $category->sortorder = 999;
407 $category->stamp = make_unique_id_code();
bf8e93d7 408 $id = $DB->insert_record('question_categories', $category);
271e6dec 409 $category->id = $id;
410 $parent = $id;
411 }
412 }
413 return $category;
414 }
3f5633df 415
f3701561 416 /**
417 * Return complete file within an array, one item per line
418 * @param string filename name of file
419 * @return mixed contents array or false on failure
420 */
aca318e1 421 function readdata($filename) {
aca318e1 422 if (is_readable($filename)) {
423 $filearray = file($filename);
424
425 /// Check for Macintosh OS line returns (ie file on one line), and fix
6dbcacee 426 if (preg_match("~\r~", $filearray[0]) AND !preg_match("~\n~", $filearray[0])) {
aca318e1 427 return explode("\r", $filearray[0]);
428 } else {
429 return $filearray;
430 }
431 }
432 return false;
433 }
434
f3701561 435 /**
271e6dec 436 * Parses an array of lines into an array of questions,
437 * where each item is a question object as defined by
438 * readquestion(). Questions are defined as anything
f3701561 439 * between blank lines.
440 *
441 * If your format does not use blank lines as a delimiter
442 * then you will need to override this method. Even then
443 * try to use readquestion for each question
444 * @param array lines array of lines from readdata
445 * @return array array of question objects
446 */
aca318e1 447 function readquestions($lines) {
271e6dec 448
aca318e1 449 $questions = array();
450 $currentquestion = array();
451
452 foreach ($lines as $line) {
453 $line = trim($line);
454 if (empty($line)) {
455 if (!empty($currentquestion)) {
456 if ($question = $this->readquestion($currentquestion)) {
457 $questions[] = $question;
458 }
459 $currentquestion = array();
460 }
461 } else {
462 $currentquestion[] = $line;
463 }
464 }
465
466 if (!empty($currentquestion)) { // There may be a final question
467 if ($question = $this->readquestion($currentquestion)) {
468 $questions[] = $question;
469 }
470 }
471
472 return $questions;
473 }
474
475
f3701561 476 /**
477 * return an "empty" question
478 * Somewhere to specify question parameters that are not handled
479 * by import but are required db fields.
480 * This should not be overridden.
481 * @return object default question
271e6dec 482 */
aca318e1 483 function defaultquestion() {
b0679efa 484 global $CFG;
0cd46577 485 static $defaultshuffleanswers = null;
486 if (is_null($defaultshuffleanswers)) {
487 $defaultshuffleanswers = get_config('quiz', 'shuffleanswers');
488 }
271e6dec 489
aca318e1 490 $question = new stdClass();
0cd46577 491 $question->shuffleanswers = $defaultshuffleanswers;
aca318e1 492 $question->defaultgrade = 1;
493 $question->image = "";
494 $question->usecase = 0;
495 $question->multiplier = array();
172f6d95 496 $question->generalfeedback = '';
08892d5b 497 $question->correctfeedback = '';
498 $question->partiallycorrectfeedback = '';
499 $question->incorrectfeedback = '';
5931ea94 500 $question->answernumbering = 'abc';
46013523 501 $question->penalty = 0.1;
3f5633df 502 $question->length = 1;
aca318e1 503
5fd8f999 504 // this option in case the questiontypes class wants
505 // to know where the data came from
506 $question->export_process = true;
093414d2 507 $question->import_process = true;
5fd8f999 508
aca318e1 509 return $question;
510 }
511
f3701561 512 /**
271e6dec 513 * Given the data known to define a question in
514 * this format, this function converts it into a question
f3701561 515 * object suitable for processing and insertion into Moodle.
516 *
517 * If your format does not use blank lines to delimit questions
518 * (e.g. an XML format) you must override 'readquestions' too
519 * @param $lines mixed data that represents question
520 * @return object question object
521 */
aca318e1 522 function readquestion($lines) {
aca318e1 523
1e3d6fd8 524 $formatnotimplemented = get_string( 'formatnotimplemented','quiz' );
525 echo "<p>$formatnotimplemented</p>";
aca318e1 526
527 return NULL;
528 }
529
f3701561 530 /**
531 * Override if any post-processing is required
532 * @return boolean success
533 */
aca318e1 534 function importpostprocess() {
aca318e1 535 return true;
536 }
537
f3701561 538 /**
539 * Import an image file encoded in base64 format
540 * @param string path path (in course data) to store picture
541 * @param string base64 encoded picture
542 * @return string filename (nb. collisions are handled)
543 */
d08e16b2 544 function importimagefile( $path, $base64 ) {
d08e16b2 545 global $CFG;
546
547 // all this to get the destination directory
548 // and filename!
549 $fullpath = "{$CFG->dataroot}/{$this->course->id}/$path";
550 $path_parts = pathinfo( $fullpath );
551 $destination = $path_parts['dirname'];
552 $file = clean_filename( $path_parts['basename'] );
553
71735794 554 // check if path exists
555 check_dir_exists($destination, true, true );
556
d08e16b2 557 // detect and fix any filename collision - get unique filename
271e6dec 558 $newfiles = resolve_filename_collisions( $destination, array($file) );
d08e16b2 559 $newfile = $newfiles[0];
560
561 // convert and save file contents
562 if (!$content = base64_decode( $base64 )) {
46013523 563 return '';
d08e16b2 564 }
565 $newfullpath = "$destination/$newfile";
566 if (!$fh = fopen( $newfullpath, 'w' )) {
46013523 567 return '';
d08e16b2 568 }
569 if (!fwrite( $fh, $content )) {
46013523 570 return '';
d08e16b2 571 }
572 fclose( $fh );
573
574 // return the (possibly) new filename
6dbcacee 575 $newfile = preg_replace("~{$CFG->dataroot}/{$this->course->id}/~", '',$newfullpath);
d08e16b2 576 return $newfile;
577 }
578
3f5633df 579
f3701561 580/*******************
581 * EXPORT FUNCTIONS
582 *******************/
aca318e1 583
271e6dec 584 /**
a41e3287 585 * Provide export functionality for plugin questiontypes
586 * Do not override
587 * @param name questiontype name
271e6dec 588 * @param question object data to export
a41e3287 589 * @param extra mixed any addition format specific data needed
590 * @return string the data to append to export or false if error (or unhandled)
591 */
592 function try_exporting_using_qtypes( $name, $question, $extra=null ) {
593 global $QTYPES;
594
595 // work out the name of format in use
596 $formatname = substr( get_class( $this ), strlen( 'qformat_' ));
597 $methodname = "export_to_$formatname";
598
599 if (array_key_exists( $name, $QTYPES )) {
600 $qtype = $QTYPES[ $name ];
601 if (method_exists( $qtype, $methodname )) {
602 if ($data = $qtype->$methodname( $question, $this, $extra )) {
603 return $data;
604 }
605 }
606 }
607 return false;
608 }
609
f3701561 610 /**
611 * Return the files extension appropriate for this type
612 * override if you don't want .txt
613 * @return string file extension
614 */
aca318e1 615 function export_file_extension() {
aca318e1 616 return ".txt";
617 }
618
f3701561 619 /**
620 * Do any pre-processing that may be required
621 * @param boolean success
622 */
08892d5b 623 function exportpreprocess() {
aca318e1 624 return true;
625 }
626
f3701561 627 /**
628 * Enable any processing to be done on the content
629 * just prior to the file being saved
630 * default is to do nothing
631 * @param string output text
632 * @param string processed output text
633 */
aca318e1 634 function presave_process( $content ) {
aca318e1 635 return $content;
636 }
637
f3701561 638 /**
639 * Do the export
640 * For most types this should not need to be overrided
641 * @return boolean success
642 */
08892d5b 643 function exportprocess() {
fef8f84e 644 global $CFG, $OUTPUT;
aca318e1 645
646 // create a directory for the exports (if not already existing)
1367cb8d 647 if (! $export_dir = make_upload_directory($this->question_get_export_dir())) {
5a2a5331 648 print_error('cannotcreatepath', 'quiz', $export_dir);
aca318e1 649 }
1367cb8d 650 $path = $CFG->dataroot.'/'.$this->question_get_export_dir();
aca318e1 651
652 // get the questions (from database) in this category
653 // only get q's with no parents (no cloze subquestions specifically)
2c44a3d3 654 if ($this->category){
655 $questions = get_questions_category( $this->category, true );
656 } else {
657 $questions = $this->questions;
658 }
aca318e1 659
fef8f84e 660 echo $OUTPUT->notification( get_string('exportingquestions','quiz') );
aca318e1 661 $count = 0;
662
663 // results are first written into string (and then to a file)
664 // so create/initialize the string here
665 $expout = "";
271e6dec 666
f1abd39f 667 // track which category questions are in
668 // if it changes we will record the category change in the output
669 // file if selected. 0 means that it will get printed before the 1st question
670 $trackcategory = 0;
aca318e1 671
672 // iterate through questions
673 foreach($questions as $question) {
271e6dec 674
a9b16aff 675 // do not export hidden questions
676 if (!empty($question->hidden)) {
677 continue;
678 }
679
680 // do not export random questions
681 if ($question->qtype==RANDOM) {
682 continue;
683 }
271e6dec 684
f1abd39f 685 // check if we need to record category change
686 if ($this->cattofile) {
687 if ($question->category != $trackcategory) {
271e6dec 688 $trackcategory = $question->category;
689 $categoryname = $this->get_category_path($trackcategory, '/', $this->contexttofile);
690
f1abd39f 691 // create 'dummy' question for category export
692 $dummyquestion = new object;
693 $dummyquestion->qtype = 'category';
694 $dummyquestion->category = $categoryname;
695 $dummyquestion->name = "switch category to $categoryname";
696 $dummyquestion->id = 0;
697 $dummyquestion->questiontextformat = '';
698 $expout .= $this->writequestion( $dummyquestion ) . "\n";
271e6dec 699 }
700 }
f1abd39f 701
702 // export the question displaying message
703 $count++;
5b0dc681 704 echo "<hr /><p><b>$count</b>. ".$this->format_question_text($question)."</p>";
0647e82b 705 if (question_has_capability_on($question, 'view', $question->category)){
706 $expout .= $this->writequestion( $question ) . "\n";
707 }
a9b16aff 708 }
aca318e1 709
2c6d2c88 710 // continue path for following error checks
711 $course = $this->course;
2c44a3d3 712 $continuepath = "$CFG->wwwroot/question/export.php?courseid=$course->id";
2c6d2c88 713
714 // did we actually process anything
715 if ($count==0) {
2c44a3d3 716 print_error( 'noquestions','quiz',$continuepath );
2c6d2c88 717 }
718
aca318e1 719 // final pre-process on exported data
720 $expout = $this->presave_process( $expout );
271e6dec 721
aca318e1 722 // write file
08892d5b 723 $filepath = $path."/".$this->filename . $this->export_file_extension();
aca318e1 724 if (!$fh=fopen($filepath,"w")) {
2c6d2c88 725 print_error( 'cannotopen','quiz',$continuepath,$filepath );
aca318e1 726 }
f1abd39f 727 if (!fwrite($fh, $expout, strlen($expout) )) {
2c6d2c88 728 print_error( 'cannotwrite','quiz',$continuepath,$filepath );
aca318e1 729 }
730 fclose($fh);
aca318e1 731 return true;
732 }
3f5633df 733
271e6dec 734 /**
735 * get the category as a path (e.g., tom/dick/harry)
736 * @param int id the id of the most nested catgory
737 * @param string delimiter the delimiter you want
738 * @return string the path
739 */
740 function get_category_path($id, $delimiter='/', $includecontext = true) {
f34488b2 741 global $DB;
271e6dec 742 $path = '';
f34488b2 743 if (!$firstcategory = $DB->get_record('question_categories',array('id' =>$id))) {
1e7386c9 744 print_error('cannotfindcategory', 'error', '', $id);
271e6dec 745 }
746 $category = $firstcategory;
747 $contextstring = $this->translator->context_to_string($category->contextid);
748 do {
749 $name = $category->name;
750 $id = $category->parent;
751 if (!empty($path)) {
752 $path = "{$name}{$delimiter}{$path}";
753 }
754 else {
755 $path = $name;
756 }
f34488b2 757 } while ($category = $DB->get_record( 'question_categories',array('id' =>$id )));
271e6dec 758
759 if ($includecontext){
760 $path = '$'.$contextstring.'$'."{$delimiter}{$path}";
761 }
762 return $path;
763 }
aca318e1 764
f3701561 765 /**
766 * Do an post-processing that may be required
767 * @return boolean success
768 */
aca318e1 769 function exportpostprocess() {
aca318e1 770 return true;
771 }
772
f3701561 773 /**
774 * convert a single question object into text output in the given
775 * format.
776 * This must be overriden
777 * @param object question question object
778 * @return mixed question export text or null if not implemented
779 */
aca318e1 780 function writequestion($question) {
1e3d6fd8 781 // if not overidden, then this is an error.
782 $formatnotimplemented = get_string( 'formatnotimplemented','quiz' );
783 echo "<p>$formatnotimplemented</p>";
aca318e1 784 return NULL;
785 }
786
f3701561 787 /**
271e6dec 788 * get directory into which export is going
f3701561 789 * @return string file path
790 */
1367cb8d 791 function question_get_export_dir() {
1b8b535d 792 global $USER;
793 if ($this->canaccessbackupdata) {
794 $dirname = get_string("exportfilename","quiz");
795 $path = $this->course->id.'/backupdata/'.$dirname; // backupdata is protected directory
796 } else {
797 $path = 'temp/questionexport/' . $USER->id;
798 }
1367cb8d 799 return $path;
800 }
801
f3701561 802 /**
803 * where question specifies a moodle (text) format this
804 * performs the conversion.
805 */
5b0dc681 806 function format_question_text($question) {
807 $formatoptions = new stdClass;
808 $formatoptions->noclean = true;
809 $formatoptions->para = false;
810 if (empty($question->questiontextformat)) {
811 $format = FORMAT_MOODLE;
812 } else {
813 $format = $question->questiontextformat;
814 }
294ce987 815 return format_text($question->questiontext, $format, $formatoptions);
5b0dc681 816 }
271e6dec 817
818
aca318e1 819}
820
821?>