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