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