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