MDL-13114 tool_uploadcourse: Fixed typo in CLI help
[moodle.git] / mod / lesson / format.php
CommitLineData
472e5662 1<?php
0a4abb73
SH
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
5491947a 18/**
86342d63 19 * format.php - Default format class for file imports/exports. Doesn't do
5491947a 20 * everything on it's own -- it needs to be extended.
21 *
0a4abb73
SH
22 * Included by import.ph
23 *
cc3dbaaa
PS
24 * @package mod
25 * @subpackage lesson
26 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0a4abb73
SH
28 **/
29
1e7f8ea2
PS
30defined('MOODLE_INTERNAL') || die();
31
0a4abb73
SH
32/**
33 * Given some question info and some data about the the answers
34 * this function parses, organises and saves the question
35 *
36 * This is only used when IMPORTING questions and is only called
37 * from format.php
38 * Lifted from mod/quiz/lib.php -
39 * 1. all reference to oldanswers removed
40 * 2. all reference to quiz_multichoice table removed
e231a3ff
TH
41 * 3. In shortanswer questions usecase is store in the qoption field
42 * 4. In numeric questions store the range as two answers
43 * 5. truefalse options are ignored
44 * 6. For multichoice questions with more than one answer the qoption field is true
0a4abb73
SH
45 *
46 * @param opject $question Contains question data like question, type and answers.
47 * @return object Returns $result->error or $result->notice.
5491947a 48 **/
0a4abb73
SH
49function lesson_save_question_options($question, $lesson) {
50 global $DB;
51
52 // These lines are required to ensure that all page types have
53 // been loaded for the following switch
54 if (!($lesson instanceof lesson)) {
55 $lesson = new lesson($lesson);
56 }
57 $manager = lesson_page_type_manager::get($lesson);
2f67a9b3 58
0a4abb73 59 $timenow = time();
39790bd8 60 $result = new stdClass();
0a4abb73
SH
61 switch ($question->qtype) {
62 case LESSON_PAGE_SHORTANSWER:
63
64 $answers = array();
65 $maxfraction = -1;
66
67 // Insert all the new answers
68 foreach ($question->answer as $key => $dataanswer) {
69 if ($dataanswer != "") {
70 $answer = new stdClass;
71 $answer->lessonid = $question->lessonid;
72 $answer->pageid = $question->id;
73 if ($question->fraction[$key] >=0.5) {
74 $answer->jumpto = LESSON_NEXTPAGE;
75 }
76 $answer->timecreated = $timenow;
77 $answer->grade = $question->fraction[$key] * 100;
78 $answer->answer = $dataanswer;
912ea4bc
RT
79 $answer->response = $question->feedback[$key]['text'];
80 $answer->responseformat = $question->feedback[$key]['format'];
0a4abb73
SH
81 $answer->id = $DB->insert_record("lesson_answers", $answer);
82 $answers[] = $answer->id;
83 if ($question->fraction[$key] > $maxfraction) {
84 $maxfraction = $question->fraction[$key];
85 }
86 }
87 }
88
89
90 /// Perform sanity checks on fractional grades
91 if ($maxfraction != 1) {
92 $maxfraction = $maxfraction * 100;
93 $result->notice = get_string("fractionsnomax", "quiz", $maxfraction);
94 return $result;
95 }
96 break;
97
e231a3ff 98 case LESSON_PAGE_NUMERICAL: // Note similarities to shortanswer.
0a4abb73
SH
99
100 $answers = array();
101 $maxfraction = -1;
102
103
104 // for each answer store the pair of min and max values even if they are the same
105 foreach ($question->answer as $key => $dataanswer) {
106 if ($dataanswer != "") {
107 $answer = new stdClass;
108 $answer->lessonid = $question->lessonid;
109 $answer->pageid = $question->id;
110 $answer->jumpto = LESSON_NEXTPAGE;
111 $answer->timecreated = $timenow;
112 $answer->grade = $question->fraction[$key] * 100;
113 $min = $question->answer[$key] - $question->tolerance[$key];
114 $max = $question->answer[$key] + $question->tolerance[$key];
115 $answer->answer = $min.":".$max;
116 // $answer->answer = $question->min[$key].":".$question->max[$key]; original line for min/max
912ea4bc
RT
117 $answer->response = $question->feedback[$key]['text'];
118 $answer->responseformat = $question->feedback[$key]['format'];
0a4abb73
SH
119 $answer->id = $DB->insert_record("lesson_answers", $answer);
120
121 $answers[] = $answer->id;
122 if ($question->fraction[$key] > $maxfraction) {
123 $maxfraction = $question->fraction[$key];
124 }
125 }
126 }
127
128 /// Perform sanity checks on fractional grades
129 if ($maxfraction != 1) {
130 $maxfraction = $maxfraction * 100;
131 $result->notice = get_string("fractionsnomax", "quiz", $maxfraction);
132 return $result;
133 }
134 break;
135
136
137 case LESSON_PAGE_TRUEFALSE:
138
139 // the truth
92701024 140 $answer = new stdClass();
0a4abb73
SH
141 $answer->lessonid = $question->lessonid;
142 $answer->pageid = $question->id;
143 $answer->timecreated = $timenow;
144 $answer->answer = get_string("true", "quiz");
912ea4bc 145 $answer->grade = $question->correctanswer * 100;
0a4abb73
SH
146 if ($answer->grade > 50 ) {
147 $answer->jumpto = LESSON_NEXTPAGE;
148 }
149 if (isset($question->feedbacktrue)) {
912ea4bc
RT
150 $answer->response = $question->feedbacktrue['text'];
151 $answer->responseformat = $question->feedbacktrue['format'];
0a4abb73 152 }
64a3ce8c 153 $DB->insert_record("lesson_answers", $answer);
0a4abb73
SH
154
155 // the lie
156 $answer = new stdClass;
157 $answer->lessonid = $question->lessonid;
158 $answer->pageid = $question->id;
159 $answer->timecreated = $timenow;
160 $answer->answer = get_string("false", "quiz");
912ea4bc 161 $answer->grade = (1 - (int)$question->correctanswer) * 100;
0a4abb73
SH
162 if ($answer->grade > 50 ) {
163 $answer->jumpto = LESSON_NEXTPAGE;
164 }
165 if (isset($question->feedbackfalse)) {
912ea4bc
RT
166 $answer->response = $question->feedbackfalse['text'];
167 $answer->responseformat = $question->feedbackfalse['format'];
0a4abb73 168 }
64a3ce8c 169 $DB->insert_record("lesson_answers", $answer);
0a4abb73
SH
170
171 break;
172
173 case LESSON_PAGE_MULTICHOICE:
174
175 $totalfraction = 0;
176 $maxfraction = -1;
177
178 $answers = array();
179
180 // Insert all the new answers
181 foreach ($question->answer as $key => $dataanswer) {
182 if ($dataanswer != "") {
183 $answer = new stdClass;
184 $answer->lessonid = $question->lessonid;
185 $answer->pageid = $question->id;
186 $answer->timecreated = $timenow;
187 $answer->grade = $question->fraction[$key] * 100;
188 // changed some defaults
189 /* Original Code
190 if ($answer->grade > 50 ) {
191 $answer->jumpto = LESSON_NEXTPAGE;
192 }
193 Replaced with: */
194 if ($answer->grade > 50 ) {
195 $answer->jumpto = LESSON_NEXTPAGE;
196 $answer->score = 1;
197 }
198 // end Replace
912ea4bc
RT
199 $answer->answer = $dataanswer['text'];
200 $answer->answerformat = $dataanswer['format'];
201 $answer->response = $question->feedback[$key]['text'];
202 $answer->responseformat = $question->feedback[$key]['format'];
0a4abb73
SH
203 $answer->id = $DB->insert_record("lesson_answers", $answer);
204 // for Sanity checks
205 if ($question->fraction[$key] > 0) {
206 $totalfraction += $question->fraction[$key];
207 }
208 if ($question->fraction[$key] > $maxfraction) {
209 $maxfraction = $question->fraction[$key];
210 }
211 }
212 }
213
214 /// Perform sanity checks on fractional grades
215 if ($question->single) {
216 if ($maxfraction != 1) {
217 $maxfraction = $maxfraction * 100;
218 $result->notice = get_string("fractionsnomax", "quiz", $maxfraction);
219 return $result;
220 }
221 } else {
222 $totalfraction = round($totalfraction,2);
223 if ($totalfraction != 1) {
224 $totalfraction = $totalfraction * 100;
225 $result->notice = get_string("fractionsaddwrong", "quiz", $totalfraction);
226 return $result;
227 }
228 }
229 break;
230
231 case LESSON_PAGE_MATCHING:
232
233 $subquestions = array();
234
235 $defaultanswer = new stdClass;
236 $defaultanswer->lessonid = $question->lessonid;
237 $defaultanswer->pageid = $question->id;
238 $defaultanswer->timecreated = $timenow;
239 $defaultanswer->grade = 0;
240
241 // The first answer should always be the correct answer
242 $correctanswer = clone($defaultanswer);
243 $correctanswer->answer = get_string('thatsthecorrectanswer', 'lesson');
b09ac20b 244 $correctanswer->jumpto = LESSON_NEXTPAGE;
0a4abb73
SH
245 $DB->insert_record("lesson_answers", $correctanswer);
246
247 // The second answer should always be the wrong answer
248 $wronganswer = clone($defaultanswer);
249 $wronganswer->answer = get_string('thatsthewronganswer', 'lesson');
250 $DB->insert_record("lesson_answers", $wronganswer);
251
252 $i = 0;
253 // Insert all the new question+answer pairs
254 foreach ($question->subquestions as $key => $questiontext) {
255 $answertext = $question->subanswers[$key];
256 if (!empty($questiontext) and !empty($answertext)) {
257 $answer = clone($defaultanswer);
912ea4bc
RT
258 $answer->answer = $questiontext['text'];
259 $answer->answerformat = $questiontext['format'];
0a4abb73
SH
260 $answer->response = $answertext;
261 if ($i == 0) {
262 // first answer contains the correct answer jump
263 $answer->jumpto = LESSON_NEXTPAGE;
264 }
64a3ce8c 265 $subquestions[] = $DB->insert_record("lesson_answers", $answer);
0a4abb73
SH
266 $i++;
267 }
268 }
269
270 if (count($subquestions) < 3) {
271 $result->notice = get_string("notenoughsubquestions", "quiz");
272 return $result;
273 }
274 break;
275 default:
276 $result->error = "Unsupported question type ($question->qtype)!";
277 return $result;
278 }
279 return true;
280}
394c97c8 281
394c97c8 282
90455bb3 283class qformat_default {
394c97c8 284
285 var $displayerrors = true;
ecea65ca 286 var $category = null;
394c97c8 287 var $questionids = array();
e231a3ff
TH
288 var $qtypeconvert = array('numerical' => LESSON_PAGE_NUMERICAL,
289 'multichoice' => LESSON_PAGE_MULTICHOICE,
290 'truefalse' => LESSON_PAGE_TRUEFALSE,
291 'shortanswer' => LESSON_PAGE_SHORTANSWER,
292 'match' => LESSON_PAGE_MATCHING
90455bb3 293 );
394c97c8 294
0a4abb73
SH
295 // Importing functions
296 function provide_import() {
297 return false;
298 }
394c97c8 299
300 function importpreprocess() {
0a4abb73 301 // Does any pre-processing that may be desired
394c97c8 302 return true;
303 }
304
305 function importprocess($filename, $lesson, $pageid) {
d68ccdba 306 global $DB, $OUTPUT;
86342d63 307
394c97c8 308 /// Processes a given file. There's probably little need to change this
309 $timenow = time();
310
311 if (! $lines = $this->readdata($filename)) {
d68ccdba 312 echo $OUTPUT->notification("File could not be read, or was empty");
394c97c8 313 return false;
314 }
315
316 if (! $questions = $this->readquestions($lines)) { // Extract all the questions
d68ccdba 317 echo $OUTPUT->notification("There are no questions in this file!");
394c97c8 318 return false;
319 }
86342d63 320
912ea4bc
RT
321 //Avoid category as question type
322 echo $OUTPUT->notification(get_string('importcount', 'lesson',
323 $this->count_questions($questions)), 'notifysuccess');
394c97c8 324
325 $count = 0;
eebc821c
RW
326 $addquestionontop = false;
327 if ($pageid == 0) {
328 $addquestionontop = true;
329 $updatelessonpage = $DB->get_record('lesson_pages', array('lessonid' => $lesson->id, 'prevpageid' => 0));
330 } else {
331 $updatelessonpage = $DB->get_record('lesson_pages', array('id' => $pageid));
332 }
394c97c8 333
0a4abb73
SH
334 $unsupportedquestions = 0;
335
394c97c8 336 foreach ($questions as $question) { // Process and store each question
337 switch ($question->qtype) {
912ea4bc
RT
338 //TODO: Bad way to bypass category in data... Quickfix for MDL-27964
339 case 'category':
340 break;
394c97c8 341 // the good ones
e231a3ff
TH
342 case 'shortanswer' :
343 case 'numerical' :
344 case 'truefalse' :
345 case 'multichoice' :
346 case 'match' :
394c97c8 347 $count++;
348
912ea4bc
RT
349 //Show nice formated question in one line.
350 echo "<hr><p><b>$count</b>. ".$this->format_question_text($question)."</p>";
351
f7ffb898 352 $newpage = new stdClass;
394c97c8 353 $newpage->lessonid = $lesson->id;
90455bb3 354 $newpage->qtype = $this->qtypeconvert[$question->qtype];
394c97c8 355 switch ($question->qtype) {
e231a3ff 356 case 'shortanswer' :
90455bb3 357 if (isset($question->usecase)) {
358 $newpage->qoption = $question->usecase;
359 }
394c97c8 360 break;
e231a3ff 361 case 'multichoice' :
394c97c8 362 if (isset($question->single)) {
363 $newpage->qoption = !$question->single;
364 }
365 break;
366 }
367 $newpage->timecreated = $timenow;
368 if ($question->name != $question->questiontext) {
369 $newpage->title = $question->name;
370 } else {
371 $newpage->title = "Page $count";
372 }
373 $newpage->contents = $question->questiontext;
1c3b1f7a
EL
374 $newpage->contentsformat = isset($question->questionformat) ? $question->questionformat : FORMAT_HTML;
375
394c97c8 376 // set up page links
377 if ($pageid) {
378 // the new page follows on from this page
646fc290 379 if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
771dc7b2 380 print_error('invalidpageid', 'lesson');
394c97c8 381 }
382 $newpage->prevpageid = $pageid;
383 $newpage->nextpageid = $page->nextpageid;
384 // insert the page and reset $pageid
6d40f12e 385 $newpageid = $DB->insert_record("lesson_pages", $newpage);
394c97c8 386 // update the linked list
6d40f12e 387 $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
394c97c8 388 } else {
389 // new page is the first page
390 // get the existing (first) page (if any)
646fc290 391 $params = array ("lessonid" => $lesson->id, "prevpageid" => 0);
392 if (!$page = $DB->get_record_select("lesson_pages", "lessonid = :lessonid AND prevpageid = :prevpageid", $params)) {
394c97c8 393 // there are no existing pages
394 $newpage->prevpageid = 0; // this is a first page
395 $newpage->nextpageid = 0; // this is the only page
646fc290 396 $newpageid = $DB->insert_record("lesson_pages", $newpage);
394c97c8 397 } else {
398 // there are existing pages put this at the start
399 $newpage->prevpageid = 0; // this is a first page
400 $newpage->nextpageid = $page->id;
646fc290 401 $newpageid = $DB->insert_record("lesson_pages", $newpage);
394c97c8 402 // update the linked list
6d40f12e 403 $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->id));
394c97c8 404 }
405 }
eebc821c 406
394c97c8 407 // reset $pageid and put the page ID in $question, used in save_question_option()
408 $pageid = $newpageid;
409 $question->id = $newpageid;
86342d63 410
394c97c8 411 $this->questionids[] = $question->id;
412
413 // Now to save all the answers and type-specific options
414
415 $question->lessonid = $lesson->id; // needed for foreign key
90455bb3 416 $question->qtype = $this->qtypeconvert[$question->qtype];
0a4abb73 417 $result = lesson_save_question_options($question, $lesson);
394c97c8 418
419 if (!empty($result->error)) {
d68ccdba 420 echo $OUTPUT->notification($result->error);
394c97c8 421 return false;
422 }
423
424 if (!empty($result->notice)) {
d68ccdba 425 echo $OUTPUT->notification($result->notice);
394c97c8 426 return true;
427 }
428 break;
429 // the Bad ones
430 default :
0a4abb73
SH
431 $unsupportedquestions++;
432 break;
394c97c8 433 }
eebc821c
RW
434 }
435 // update the prev links
436 if ($addquestionontop) {
437 $DB->set_field("lesson_pages", "prevpageid", $pageid, array("id" => $updatelessonpage->id));
438 } else {
439 $DB->set_field("lesson_pages", "prevpageid", $pageid, array("id" => $updatelessonpage->nextpageid));
394c97c8 440 }
0a4abb73
SH
441 if ($unsupportedquestions) {
442 echo $OUTPUT->notification(get_string('unknownqtypesnotimported', 'lesson', $unsupportedquestions));
443 }
394c97c8 444 return true;
445 }
446
912ea4bc
RT
447 /**
448 * Count all non-category questions in the questions array.
449 *
450 * @param array questions An array of question objects.
451 * @return int The count.
452 *
453 */
454 protected function count_questions($questions) {
455 $count = 0;
456 if (!is_array($questions)) {
457 return $count;
458 }
459 foreach ($questions as $question) {
460 if (!is_object($question) || !isset($question->qtype) ||
461 ($question->qtype == 'category')) {
462 continue;
463 }
464 $count++;
465 }
466 return $count;
467 }
394c97c8 468
469 function readdata($filename) {
470 /// Returns complete file with an array, one item per line
471
472 if (is_readable($filename)) {
473 $filearray = file($filename);
474
475 /// Check for Macintosh OS line returns (ie file on one line), and fix
6dbcacee 476 if (preg_match("/\r/", $filearray[0]) AND !preg_match("/\n/", $filearray[0])) {
394c97c8 477 return explode("\r", $filearray[0]);
478 } else {
479 return $filearray;
480 }
481 }
482 return false;
483 }
484
ba15c346 485 protected function readquestions($lines) {
86342d63
PS
486 /// Parses an array of lines into an array of questions,
487 /// where each item is a question object as defined by
488 /// readquestion(). Questions are defined as anything
394c97c8 489 /// between blank lines.
86342d63 490
394c97c8 491 $questions = array();
492 $currentquestion = array();
493
494 foreach ($lines as $line) {
495 $line = trim($line);
496 if (empty($line)) {
497 if (!empty($currentquestion)) {
498 if ($question = $this->readquestion($currentquestion)) {
499 $questions[] = $question;
500 }
501 $currentquestion = array();
502 }
503 } else {
504 $currentquestion[] = $line;
505 }
506 }
507
508 if (!empty($currentquestion)) { // There may be a final question
509 if ($question = $this->readquestion($currentquestion)) {
510 $questions[] = $question;
511 }
512 }
513
514 return $questions;
515 }
516
517
e8eb2b00 518 protected function readquestion($lines) {
86342d63
PS
519 /// Given an array of lines known to define a question in
520 /// this format, this function converts it into a question
394c97c8 521 /// object suitable for processing and insertion into Moodle.
522
523 echo "<p>This flash question format has not yet been completed!</p>";
524
ecea65ca 525 return null;
394c97c8 526 }
527
cacb8fa0
TH
528 /**
529 * Construct a reasonable default question name, based on the start of the question text.
530 * @param string $questiontext the question text.
531 * @param string $default default question name to use if the constructed one comes out blank.
532 * @return string a reasonable question name.
533 */
534 public function create_default_question_name($questiontext, $default) {
535 $name = $this->clean_question_name(shorten_text($questiontext, 80));
536 if ($name) {
537 return $name;
538 } else {
539 return $default;
540 }
541 }
542
543 /**
544 * Ensure that a question name does not contain anything nasty, and will fit in the DB field.
545 * @param string $name the raw question name.
546 * @return string a safe question name.
547 */
548 public function clean_question_name($name) {
549 $name = clean_param($name, PARAM_TEXT); // Matches what the question editing form does.
550 $name = trim($name);
551 $trimlength = 251;
552 while (textlib::strlen($name) > 255 && $trimlength > 0) {
553 $name = shorten_text($name, $trimlength);
554 $trimlength -= 10;
555 }
556 return $name;
557 }
558
90455bb3 559 function defaultquestion() {
560 // returns an "empty" question
561 // Somewhere to specify question parameters that are not handled
562 // by import but are required db fields.
86342d63 563 // This should not be overridden.
508fe4d8 564 global $CFG;
565
90455bb3 566 $question = new stdClass();
0a4abb73 567 $question->shuffleanswers = get_config('quiz', 'shuffleanswers');
912ea4bc 568 $question->defaultmark = 1;
508fe4d8 569 $question->image = "";
570 $question->usecase = 0;
571 $question->multiplier = array();
572 $question->generalfeedback = '';
573 $question->correctfeedback = '';
574 $question->partiallycorrectfeedback = '';
575 $question->incorrectfeedback = '';
576 $question->answernumbering = 'abc';
577 $question->penalty = 0.1;
578 $question->length = 1;
90455bb3 579 $question->qoption = 0;
580 $question->layout = 1;
86342d63 581
912ea4bc
RT
582 // this option in case the questiontypes class wants
583 // to know where the data came from
584 $question->export_process = true;
585 $question->import_process = true;
586
90455bb3 587 return $question;
588 }
394c97c8 589
590 function importpostprocess() {
0a4abb73
SH
591 /// Does any post-processing that may be desired
592 /// Argument is a simple array of question ids that
593 /// have just been added.
394c97c8 594 return true;
595 }
596
912ea4bc
RT
597 /**
598 * Convert the question text to plain text, so it can safely be displayed
599 * during import to let the user see roughly what is going on.
600 */
601 protected function format_question_text($question) {
602 $formatoptions = new stdClass();
603 $formatoptions->noclean = true;
604 return html_to_text(format_text($question->questiontext,
605 $question->questiontextformat, $formatoptions), 0, false);
606 }
bba5e716
TH
607
608 /**
609 * Since the lesson module tries to re-use the question bank import classes in
610 * a crazy way, this is necessary to stop things breaking.
611 */
612 protected function add_blank_combined_feedback($question) {
613 return $question;
614 }
394c97c8 615}
616
86342d63 617
bba5e716
TH
618/**
619 * Since the lesson module tries to re-use the question bank import classes in
620 * a crazy way, this is necessary to stop things breaking. This should be exactly
621 * the same as the class defined in question/format.php.
622 */
623class qformat_based_on_xml extends qformat_default {
624 /**
625 * A lot of imported files contain unwanted entities.
626 * This method tries to clean up all known problems.
627 * @param string str string to correct
628 * @return string the corrected string
629 */
630 public function cleaninput($str) {
631
632 $html_code_list = array(
633 "&#039;" => "'",
634 "&#8217;" => "'",
635 "&#8220;" => "\"",
636 "&#8221;" => "\"",
637 "&#8211;" => "-",
638 "&#8212;" => "-",
639 );
640 $str = strtr($str, $html_code_list);
641 // Use textlib entities_to_utf8 function to convert only numerical entities.
642 $str = textlib::entities_to_utf8($str, false);
643 return $str;
644 }
645
646 /**
647 * Return the array moodle is expecting
648 * for an HTML text. No processing is done on $text.
649 * qformat classes that want to process $text
650 * for instance to import external images files
651 * and recode urls in $text must overwrite this method.
652 * @param array $text some HTML text string
653 * @return array with keys text, format and files.
654 */
655 public function text_field($text) {
656 return array(
657 'text' => trim($text),
658 'format' => FORMAT_HTML,
659 'files' => array(),
660 );
661 }
662
663 /**
664 * Return the value of a node, given a path to the node
665 * if it doesn't exist return the default value.
666 * @param array xml data to read
667 * @param array path path to node expressed as array
668 * @param mixed default
669 * @param bool istext process as text
670 * @param string error if set value must exist, return false and issue message if not
671 * @return mixed value
672 */
673 public function getpath($xml, $path, $default, $istext=false, $error='') {
674 foreach ($path as $index) {
675 if (!isset($xml[$index])) {
676 if (!empty($error)) {
677 $this->error($error);
678 return false;
679 } else {
680 return $default;
681 }
682 }
683
684 $xml = $xml[$index];
685 }
686
687 if ($istext) {
688 if (!is_string($xml)) {
689 $this->error(get_string('invalidxml', 'qformat_xml'));
690 }
691 $xml = trim($xml);
692 }
693
694 return $xml;
695 }
696}