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