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() { |
f34488b2 |
200 | global $USER, $DB; |
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 | |
f34488b2 |
296 | if (!$question->id = $DB->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() |
e5d7d1dc |
319 | $DB->set_field('question', 'version', question_hash($question), array('id'=>$question->id)); |
aca318e1 |
320 | } |
321 | return true; |
322 | } |
ce2df288 |
323 | /** |
324 | * Count all non-category questions in the questions array. |
f34488b2 |
325 | * |
ce2df288 |
326 | * @param array questions An array of question objects. |
327 | * @return int The count. |
f34488b2 |
328 | * |
ce2df288 |
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='/') { |
f34488b2 |
357 | global $DB; |
271e6dec |
358 | $catpath = clean_param($catpath, PARAM_PATH); |
359 | $catnames = explode($delimiter, $catpath); |
360 | $parent = 0; |
361 | $category = null; |
5ca9e32d |
362 | |
363 | // check for context id in path, it might not be there in pre 1.9 exports |
364 | $matchcount = preg_match('/^\$([a-z]+)\$$/', $catnames[0], $matches); |
365 | if ($matchcount==1) { |
271e6dec |
366 | $contextid = $this->translator->string_to_context($matches[1]); |
367 | array_shift($catnames); |
368 | } else { |
369 | $contextid = FALSE; |
370 | } |
371 | if ($this->contextfromfile && ($contextid !== FALSE)){ |
372 | $context = get_context_instance_by_id($contextid); |
373 | require_capability('moodle/question:add', $context); |
374 | } else { |
375 | $context = get_context_instance_by_id($this->category->contextid); |
376 | } |
377 | foreach ($catnames as $catname) { |
f34488b2 |
378 | if ($category = $DB->get_record( 'question_categories', array('name' => $catname, 'contextid' => $context->id, 'parent' => $parent))) { |
271e6dec |
379 | $parent = $category->id; |
380 | } else { |
381 | require_capability('moodle/question:managecategory', $context); |
382 | // create the new category |
383 | $category = new object; |
384 | $category->contextid = $context->id; |
385 | $category->name = $catname; |
386 | $category->info = ''; |
387 | $category->parent = $parent; |
388 | $category->sortorder = 999; |
389 | $category->stamp = make_unique_id_code(); |
f34488b2 |
390 | if (!($id = $DB->insert_record('question_categories', $category))) { |
d9f30321 |
391 | print_error("cannotcreatecategory"); |
271e6dec |
392 | } |
393 | $category->id = $id; |
394 | $parent = $id; |
395 | } |
396 | } |
397 | return $category; |
398 | } |
3f5633df |
399 | |
f3701561 |
400 | /** |
401 | * Return complete file within an array, one item per line |
402 | * @param string filename name of file |
403 | * @return mixed contents array or false on failure |
404 | */ |
aca318e1 |
405 | function readdata($filename) { |
aca318e1 |
406 | if (is_readable($filename)) { |
407 | $filearray = file($filename); |
408 | |
409 | /// Check for Macintosh OS line returns (ie file on one line), and fix |
410 | if (ereg("\r", $filearray[0]) AND !ereg("\n", $filearray[0])) { |
411 | return explode("\r", $filearray[0]); |
412 | } else { |
413 | return $filearray; |
414 | } |
415 | } |
416 | return false; |
417 | } |
418 | |
f3701561 |
419 | /** |
271e6dec |
420 | * Parses an array of lines into an array of questions, |
421 | * where each item is a question object as defined by |
422 | * readquestion(). Questions are defined as anything |
f3701561 |
423 | * between blank lines. |
424 | * |
425 | * If your format does not use blank lines as a delimiter |
426 | * then you will need to override this method. Even then |
427 | * try to use readquestion for each question |
428 | * @param array lines array of lines from readdata |
429 | * @return array array of question objects |
430 | */ |
aca318e1 |
431 | function readquestions($lines) { |
271e6dec |
432 | |
aca318e1 |
433 | $questions = array(); |
434 | $currentquestion = array(); |
435 | |
436 | foreach ($lines as $line) { |
437 | $line = trim($line); |
438 | if (empty($line)) { |
439 | if (!empty($currentquestion)) { |
440 | if ($question = $this->readquestion($currentquestion)) { |
441 | $questions[] = $question; |
442 | } |
443 | $currentquestion = array(); |
444 | } |
445 | } else { |
446 | $currentquestion[] = $line; |
447 | } |
448 | } |
449 | |
450 | if (!empty($currentquestion)) { // There may be a final question |
451 | if ($question = $this->readquestion($currentquestion)) { |
452 | $questions[] = $question; |
453 | } |
454 | } |
455 | |
456 | return $questions; |
457 | } |
458 | |
459 | |
f3701561 |
460 | /** |
461 | * return an "empty" question |
462 | * Somewhere to specify question parameters that are not handled |
463 | * by import but are required db fields. |
464 | * This should not be overridden. |
465 | * @return object default question |
271e6dec |
466 | */ |
aca318e1 |
467 | function defaultquestion() { |
b0679efa |
468 | global $CFG; |
271e6dec |
469 | |
aca318e1 |
470 | $question = new stdClass(); |
271e6dec |
471 | $question->shuffleanswers = $CFG->quiz_shuffleanswers; |
aca318e1 |
472 | $question->defaultgrade = 1; |
473 | $question->image = ""; |
474 | $question->usecase = 0; |
475 | $question->multiplier = array(); |
172f6d95 |
476 | $question->generalfeedback = ''; |
08892d5b |
477 | $question->correctfeedback = ''; |
478 | $question->partiallycorrectfeedback = ''; |
479 | $question->incorrectfeedback = ''; |
5931ea94 |
480 | $question->answernumbering = 'abc'; |
46013523 |
481 | $question->penalty = 0.1; |
3f5633df |
482 | $question->length = 1; |
aca318e1 |
483 | |
5fd8f999 |
484 | // this option in case the questiontypes class wants |
485 | // to know where the data came from |
486 | $question->export_process = true; |
093414d2 |
487 | $question->import_process = true; |
5fd8f999 |
488 | |
aca318e1 |
489 | return $question; |
490 | } |
491 | |
f3701561 |
492 | /** |
271e6dec |
493 | * Given the data known to define a question in |
494 | * this format, this function converts it into a question |
f3701561 |
495 | * object suitable for processing and insertion into Moodle. |
496 | * |
497 | * If your format does not use blank lines to delimit questions |
498 | * (e.g. an XML format) you must override 'readquestions' too |
499 | * @param $lines mixed data that represents question |
500 | * @return object question object |
501 | */ |
aca318e1 |
502 | function readquestion($lines) { |
aca318e1 |
503 | |
1e3d6fd8 |
504 | $formatnotimplemented = get_string( 'formatnotimplemented','quiz' ); |
505 | echo "<p>$formatnotimplemented</p>"; |
aca318e1 |
506 | |
507 | return NULL; |
508 | } |
509 | |
f3701561 |
510 | /** |
511 | * Override if any post-processing is required |
512 | * @return boolean success |
513 | */ |
aca318e1 |
514 | function importpostprocess() { |
aca318e1 |
515 | return true; |
516 | } |
517 | |
f3701561 |
518 | /** |
519 | * Import an image file encoded in base64 format |
520 | * @param string path path (in course data) to store picture |
521 | * @param string base64 encoded picture |
522 | * @return string filename (nb. collisions are handled) |
523 | */ |
d08e16b2 |
524 | function importimagefile( $path, $base64 ) { |
d08e16b2 |
525 | global $CFG; |
526 | |
527 | // all this to get the destination directory |
528 | // and filename! |
529 | $fullpath = "{$CFG->dataroot}/{$this->course->id}/$path"; |
530 | $path_parts = pathinfo( $fullpath ); |
531 | $destination = $path_parts['dirname']; |
532 | $file = clean_filename( $path_parts['basename'] ); |
533 | |
71735794 |
534 | // check if path exists |
535 | check_dir_exists($destination, true, true ); |
536 | |
d08e16b2 |
537 | // detect and fix any filename collision - get unique filename |
271e6dec |
538 | $newfiles = resolve_filename_collisions( $destination, array($file) ); |
d08e16b2 |
539 | $newfile = $newfiles[0]; |
540 | |
541 | // convert and save file contents |
542 | if (!$content = base64_decode( $base64 )) { |
46013523 |
543 | return ''; |
d08e16b2 |
544 | } |
545 | $newfullpath = "$destination/$newfile"; |
546 | if (!$fh = fopen( $newfullpath, 'w' )) { |
46013523 |
547 | return ''; |
d08e16b2 |
548 | } |
549 | if (!fwrite( $fh, $content )) { |
46013523 |
550 | return ''; |
d08e16b2 |
551 | } |
552 | fclose( $fh ); |
553 | |
554 | // return the (possibly) new filename |
71735794 |
555 | $newfile = ereg_replace("{$CFG->dataroot}/{$this->course->id}/", '',$newfullpath); |
d08e16b2 |
556 | return $newfile; |
557 | } |
558 | |
3f5633df |
559 | |
f3701561 |
560 | /******************* |
561 | * EXPORT FUNCTIONS |
562 | *******************/ |
aca318e1 |
563 | |
271e6dec |
564 | /** |
a41e3287 |
565 | * Provide export functionality for plugin questiontypes |
566 | * Do not override |
567 | * @param name questiontype name |
271e6dec |
568 | * @param question object data to export |
a41e3287 |
569 | * @param extra mixed any addition format specific data needed |
570 | * @return string the data to append to export or false if error (or unhandled) |
571 | */ |
572 | function try_exporting_using_qtypes( $name, $question, $extra=null ) { |
573 | global $QTYPES; |
574 | |
575 | // work out the name of format in use |
576 | $formatname = substr( get_class( $this ), strlen( 'qformat_' )); |
577 | $methodname = "export_to_$formatname"; |
578 | |
579 | if (array_key_exists( $name, $QTYPES )) { |
580 | $qtype = $QTYPES[ $name ]; |
581 | if (method_exists( $qtype, $methodname )) { |
582 | if ($data = $qtype->$methodname( $question, $this, $extra )) { |
583 | return $data; |
584 | } |
585 | } |
586 | } |
587 | return false; |
588 | } |
589 | |
f3701561 |
590 | /** |
591 | * Return the files extension appropriate for this type |
592 | * override if you don't want .txt |
593 | * @return string file extension |
594 | */ |
aca318e1 |
595 | function export_file_extension() { |
aca318e1 |
596 | return ".txt"; |
597 | } |
598 | |
f3701561 |
599 | /** |
600 | * Do any pre-processing that may be required |
601 | * @param boolean success |
602 | */ |
08892d5b |
603 | function exportpreprocess() { |
aca318e1 |
604 | return true; |
605 | } |
606 | |
f3701561 |
607 | /** |
608 | * Enable any processing to be done on the content |
609 | * just prior to the file being saved |
610 | * default is to do nothing |
611 | * @param string output text |
612 | * @param string processed output text |
613 | */ |
aca318e1 |
614 | function presave_process( $content ) { |
aca318e1 |
615 | return $content; |
616 | } |
617 | |
f3701561 |
618 | /** |
619 | * Do the export |
620 | * For most types this should not need to be overrided |
621 | * @return boolean success |
622 | */ |
08892d5b |
623 | function exportprocess() { |
aca318e1 |
624 | global $CFG; |
625 | |
626 | // create a directory for the exports (if not already existing) |
1367cb8d |
627 | if (! $export_dir = make_upload_directory($this->question_get_export_dir())) { |
5a2a5331 |
628 | print_error('cannotcreatepath', 'quiz', $export_dir); |
aca318e1 |
629 | } |
1367cb8d |
630 | $path = $CFG->dataroot.'/'.$this->question_get_export_dir(); |
aca318e1 |
631 | |
632 | // get the questions (from database) in this category |
633 | // only get q's with no parents (no cloze subquestions specifically) |
2c44a3d3 |
634 | if ($this->category){ |
635 | $questions = get_questions_category( $this->category, true ); |
636 | } else { |
637 | $questions = $this->questions; |
638 | } |
aca318e1 |
639 | |
1e3d6fd8 |
640 | notify( get_string('exportingquestions','quiz') ); |
aca318e1 |
641 | $count = 0; |
642 | |
643 | // results are first written into string (and then to a file) |
644 | // so create/initialize the string here |
645 | $expout = ""; |
271e6dec |
646 | |
f1abd39f |
647 | // track which category questions are in |
648 | // if it changes we will record the category change in the output |
649 | // file if selected. 0 means that it will get printed before the 1st question |
650 | $trackcategory = 0; |
aca318e1 |
651 | |
652 | // iterate through questions |
653 | foreach($questions as $question) { |
271e6dec |
654 | |
a9b16aff |
655 | // do not export hidden questions |
656 | if (!empty($question->hidden)) { |
657 | continue; |
658 | } |
659 | |
660 | // do not export random questions |
661 | if ($question->qtype==RANDOM) { |
662 | continue; |
663 | } |
271e6dec |
664 | |
f1abd39f |
665 | // check if we need to record category change |
666 | if ($this->cattofile) { |
667 | if ($question->category != $trackcategory) { |
271e6dec |
668 | $trackcategory = $question->category; |
669 | $categoryname = $this->get_category_path($trackcategory, '/', $this->contexttofile); |
670 | |
f1abd39f |
671 | // create 'dummy' question for category export |
672 | $dummyquestion = new object; |
673 | $dummyquestion->qtype = 'category'; |
674 | $dummyquestion->category = $categoryname; |
675 | $dummyquestion->name = "switch category to $categoryname"; |
676 | $dummyquestion->id = 0; |
677 | $dummyquestion->questiontextformat = ''; |
678 | $expout .= $this->writequestion( $dummyquestion ) . "\n"; |
271e6dec |
679 | } |
680 | } |
f1abd39f |
681 | |
682 | // export the question displaying message |
683 | $count++; |
5b0dc681 |
684 | echo "<hr /><p><b>$count</b>. ".$this->format_question_text($question)."</p>"; |
0647e82b |
685 | if (question_has_capability_on($question, 'view', $question->category)){ |
686 | $expout .= $this->writequestion( $question ) . "\n"; |
687 | } |
a9b16aff |
688 | } |
aca318e1 |
689 | |
2c6d2c88 |
690 | // continue path for following error checks |
691 | $course = $this->course; |
2c44a3d3 |
692 | $continuepath = "$CFG->wwwroot/question/export.php?courseid=$course->id"; |
2c6d2c88 |
693 | |
694 | // did we actually process anything |
695 | if ($count==0) { |
2c44a3d3 |
696 | print_error( 'noquestions','quiz',$continuepath ); |
2c6d2c88 |
697 | } |
698 | |
aca318e1 |
699 | // final pre-process on exported data |
700 | $expout = $this->presave_process( $expout ); |
271e6dec |
701 | |
aca318e1 |
702 | // write file |
08892d5b |
703 | $filepath = $path."/".$this->filename . $this->export_file_extension(); |
aca318e1 |
704 | if (!$fh=fopen($filepath,"w")) { |
2c6d2c88 |
705 | print_error( 'cannotopen','quiz',$continuepath,$filepath ); |
aca318e1 |
706 | } |
f1abd39f |
707 | if (!fwrite($fh, $expout, strlen($expout) )) { |
2c6d2c88 |
708 | print_error( 'cannotwrite','quiz',$continuepath,$filepath ); |
aca318e1 |
709 | } |
710 | fclose($fh); |
aca318e1 |
711 | return true; |
712 | } |
3f5633df |
713 | |
271e6dec |
714 | /** |
715 | * get the category as a path (e.g., tom/dick/harry) |
716 | * @param int id the id of the most nested catgory |
717 | * @param string delimiter the delimiter you want |
718 | * @return string the path |
719 | */ |
720 | function get_category_path($id, $delimiter='/', $includecontext = true) { |
f34488b2 |
721 | global $DB; |
271e6dec |
722 | $path = ''; |
f34488b2 |
723 | if (!$firstcategory = $DB->get_record('question_categories',array('id' =>$id))) { |
1e7386c9 |
724 | print_error('cannotfindcategory', 'error', '', $id); |
271e6dec |
725 | } |
726 | $category = $firstcategory; |
727 | $contextstring = $this->translator->context_to_string($category->contextid); |
728 | do { |
729 | $name = $category->name; |
730 | $id = $category->parent; |
731 | if (!empty($path)) { |
732 | $path = "{$name}{$delimiter}{$path}"; |
733 | } |
734 | else { |
735 | $path = $name; |
736 | } |
f34488b2 |
737 | } while ($category = $DB->get_record( 'question_categories',array('id' =>$id ))); |
271e6dec |
738 | |
739 | if ($includecontext){ |
740 | $path = '$'.$contextstring.'$'."{$delimiter}{$path}"; |
741 | } |
742 | return $path; |
743 | } |
aca318e1 |
744 | |
f3701561 |
745 | /** |
746 | * Do an post-processing that may be required |
747 | * @return boolean success |
748 | */ |
aca318e1 |
749 | function exportpostprocess() { |
aca318e1 |
750 | return true; |
751 | } |
752 | |
f3701561 |
753 | /** |
754 | * convert a single question object into text output in the given |
755 | * format. |
756 | * This must be overriden |
757 | * @param object question question object |
758 | * @return mixed question export text or null if not implemented |
759 | */ |
aca318e1 |
760 | function writequestion($question) { |
1e3d6fd8 |
761 | // if not overidden, then this is an error. |
762 | $formatnotimplemented = get_string( 'formatnotimplemented','quiz' ); |
763 | echo "<p>$formatnotimplemented</p>"; |
aca318e1 |
764 | return NULL; |
765 | } |
766 | |
f3701561 |
767 | /** |
271e6dec |
768 | * get directory into which export is going |
f3701561 |
769 | * @return string file path |
770 | */ |
1367cb8d |
771 | function question_get_export_dir() { |
772 | $dirname = get_string("exportfilename","quiz"); |
773 | $path = $this->course->id.'/backupdata/'.$dirname; // backupdata is protected directory |
774 | return $path; |
775 | } |
776 | |
f3701561 |
777 | /** |
778 | * where question specifies a moodle (text) format this |
779 | * performs the conversion. |
780 | */ |
5b0dc681 |
781 | function format_question_text($question) { |
782 | $formatoptions = new stdClass; |
783 | $formatoptions->noclean = true; |
784 | $formatoptions->para = false; |
785 | if (empty($question->questiontextformat)) { |
786 | $format = FORMAT_MOODLE; |
787 | } else { |
788 | $format = $question->questiontextformat; |
789 | } |
294ce987 |
790 | return format_text($question->questiontext, $format, $formatoptions); |
5b0dc681 |
791 | } |
271e6dec |
792 | |
793 | |
aca318e1 |
794 | } |
795 | |
796 | ?> |