[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Unit tests for the Moodle XML format. 19 * 20 * @package qformat_xml 21 * @copyright 2010 The Open University 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once($CFG->libdir . '/questionlib.php'); 30 require_once($CFG->dirroot . '/question/format/xml/format.php'); 31 require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); 32 33 34 /** 35 * Unit tests for the matching question definition class. 36 * 37 * @copyright 2009 The Open University 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class qformat_xml_test extends question_testcase { 41 public function assert_same_xml($expectedxml, $xml) { 42 $this->assertEquals(str_replace("\r\n", "\n", $expectedxml), 43 str_replace("\r\n", "\n", $xml)); 44 } 45 46 public function make_test_question() { 47 global $USER; 48 $q = new stdClass(); 49 $q->id = 0; 50 $q->contextid = 0; 51 $q->category = 0; 52 $q->parent = 0; 53 $q->questiontextformat = FORMAT_HTML; 54 $q->generalfeedbackformat = FORMAT_HTML; 55 $q->defaultmark = 1; 56 $q->penalty = 0.3333333; 57 $q->length = 1; 58 $q->stamp = make_unique_id_code(); 59 $q->version = make_unique_id_code(); 60 $q->hidden = 0; 61 $q->timecreated = time(); 62 $q->timemodified = time(); 63 $q->createdby = $USER->id; 64 $q->modifiedby = $USER->id; 65 return $q; 66 } 67 68 /** 69 * The data the XML import format sends to save_question is not exactly 70 * the same as the data returned from the editing form, so this method 71 * makes necessary changes to the return value of 72 * test_question_maker::get_question_form_data so that the tests can work. 73 * @param object $expectedq as returned by get_question_form_data. 74 * @return object one more likely to match the return value of import_...(). 75 */ 76 public function remove_irrelevant_form_data_fields($expectedq) { 77 return $this->itemid_to_files($expectedq); 78 } 79 80 /** 81 * Becuase XML import uses a files array instead of an itemid integer to 82 * handle saving files with a question, we need to covert the output of 83 * test_question_maker::get_question_form_data to match. This method recursively 84 * replaces all array elements with key itemid with an array entry with 85 * key files and value an empty array. 86 * 87 * @param mixed $var any data structure. 88 * @return mixed an equivalent structure with the relacements made. 89 */ 90 protected function itemid_to_files($var) { 91 if (is_object($var)) { 92 $newvar = new stdClass(); 93 foreach (get_object_vars($var) as $field => $value) { 94 $newvar->$field = $this->itemid_to_files($value); 95 } 96 97 } else if (is_array($var)) { 98 $newvar = array(); 99 foreach ($var as $index => $value) { 100 if ($index === 'itemid') { 101 $newvar['files'] = array(); 102 } else { 103 $newvar[$index] = $this->itemid_to_files($value); 104 } 105 } 106 107 } else { 108 $newvar = $var; 109 } 110 111 return $newvar; 112 } 113 114 public function test_write_hint_basic() { 115 $q = $this->make_test_question(); 116 $q->name = 'Short answer question'; 117 $q->questiontext = 'Name an amphibian: __________'; 118 $q->generalfeedback = 'Generalfeedback: frog or toad would have been OK.'; 119 if (!isset($q->options)) { 120 $q->options = new stdClass(); 121 } 122 $q->options->usecase = false; 123 $q->options->answers = array( 124 13 => new question_answer(13, 'frog', 1.0, 'Frog is a very good answer.', FORMAT_HTML), 125 14 => new question_answer(14, 'toad', 0.8, 'Toad is an OK good answer.', FORMAT_HTML), 126 15 => new question_answer(15, '*', 0.0, 'That is a bad answer.', FORMAT_HTML), 127 ); 128 $q->qtype = 'shortanswer'; 129 $q->hints = array( 130 new question_hint(0, 'This is the first hint.', FORMAT_MOODLE), 131 ); 132 133 $exporter = new qformat_xml(); 134 $xml = $exporter->writequestion($q); 135 136 $this->assertRegExp('|<hint format=\"moodle_auto_format\">\s*<text>\s*' . 137 'This is the first hint\.\s*</text>\s*</hint>|', $xml); 138 $this->assertNotRegExp('|<shownumcorrect/>|', $xml); 139 $this->assertNotRegExp('|<clearwrong/>|', $xml); 140 $this->assertNotRegExp('|<options>|', $xml); 141 } 142 143 public function test_write_hint_with_parts() { 144 $q = $this->make_test_question(); 145 $q->name = 'Matching question'; 146 $q->questiontext = 'Classify the animals.'; 147 $q->generalfeedback = 'Frogs and toads are amphibians, the others are mammals.'; 148 $q->qtype = 'match'; 149 150 if (!isset($q->options)) { 151 $q->options = new stdClass(); 152 } 153 $q->options->shuffleanswers = 1; 154 $q->options->correctfeedback = ''; 155 $q->options->correctfeedbackformat = FORMAT_HTML; 156 $q->options->partiallycorrectfeedback = ''; 157 $q->options->partiallycorrectfeedbackformat = FORMAT_HTML; 158 $q->options->incorrectfeedback = ''; 159 $q->options->incorrectfeedbackformat = FORMAT_HTML; 160 161 $q->options->subquestions = array(); 162 $q->hints = array( 163 new question_hint_with_parts(0, 'This is the first hint.', FORMAT_HTML, false, true), 164 new question_hint_with_parts(0, 'This is the second hint.', FORMAT_HTML, true, false), 165 ); 166 167 $exporter = new qformat_xml(); 168 $xml = $exporter->writequestion($q); 169 170 $this->assertRegExp( 171 '|<hint format=\"html\">\s*<text>\s*This is the first hint\.\s*</text>|', $xml); 172 $this->assertRegExp( 173 '|<hint format=\"html\">\s*<text>\s*This is the second hint\.\s*</text>|', $xml); 174 list($ignored, $hint1, $hint2) = explode('<hint', $xml); 175 $this->assertNotRegExp('|<shownumcorrect/>|', $hint1); 176 $this->assertRegExp('|<clearwrong/>|', $hint1); 177 $this->assertRegExp('|<shownumcorrect/>|', $hint2); 178 $this->assertNotRegExp('|<clearwrong/>|', $hint2); 179 $this->assertNotRegExp('|<options>|', $xml); 180 } 181 182 public function test_import_hints_no_parts() { 183 $xml = <<<END 184 <question> 185 <hint> 186 <text>This is the first hint</text> 187 <clearwrong/> 188 </hint> 189 <hint> 190 <text>This is the second hint</text> 191 <shownumcorrect/> 192 </hint> 193 </question> 194 END; 195 196 $questionxml = xmlize($xml); 197 $qo = new stdClass(); 198 199 $importer = new qformat_xml(); 200 $importer->import_hints($qo, $questionxml['question'], false, false, 'html'); 201 202 $this->assertEquals(array( 203 array('text' => 'This is the first hint', 204 'format' => FORMAT_HTML), 205 array('text' => 'This is the second hint', 206 'format' => FORMAT_HTML), 207 ), $qo->hint); 208 $this->assertFalse(isset($qo->hintclearwrong)); 209 $this->assertFalse(isset($qo->hintshownumcorrect)); 210 } 211 212 public function test_import_hints_with_parts() { 213 $xml = <<<END 214 <question> 215 <hint> 216 <text>This is the first hint</text> 217 <clearwrong/> 218 </hint> 219 <hint> 220 <text>This is the second hint</text> 221 <shownumcorrect/> 222 </hint> 223 </question> 224 END; 225 226 $questionxml = xmlize($xml); 227 $qo = new stdClass(); 228 229 $importer = new qformat_xml(); 230 $importer->import_hints($qo, $questionxml['question'], true, true, 'html'); 231 232 $this->assertEquals(array( 233 array('text' => 'This is the first hint', 234 'format' => FORMAT_HTML), 235 array('text' => 'This is the second hint', 236 'format' => FORMAT_HTML), 237 ), $qo->hint); 238 $this->assertEquals(array(1, 0), $qo->hintclearwrong); 239 $this->assertEquals(array(0, 1), $qo->hintshownumcorrect); 240 } 241 242 public function test_import_no_hints_no_error() { 243 $xml = <<<END 244 <question> 245 </question> 246 END; 247 248 $questionxml = xmlize($xml); 249 $qo = new stdClass(); 250 251 $importer = new qformat_xml(); 252 $importer->import_hints($qo, $questionxml['question'], 'html'); 253 254 $this->assertFalse(isset($qo->hint)); 255 } 256 257 public function test_import_description() { 258 $xml = ' <question type="description"> 259 <name> 260 <text>A description</text> 261 </name> 262 <questiontext format="html"> 263 <text>The question text.</text> 264 </questiontext> 265 <generalfeedback> 266 <text>Here is some general feedback.</text> 267 </generalfeedback> 268 <defaultgrade>0</defaultgrade> 269 <penalty>0</penalty> 270 <hidden>0</hidden> 271 </question>'; 272 $xmldata = xmlize($xml); 273 274 $importer = new qformat_xml(); 275 $q = $importer->import_description($xmldata['question']); 276 277 $expectedq = new stdClass(); 278 $expectedq->qtype = 'description'; 279 $expectedq->name = 'A description'; 280 $expectedq->questiontext = 'The question text.'; 281 $expectedq->questiontextformat = FORMAT_HTML; 282 $expectedq->generalfeedback = 'Here is some general feedback.'; 283 $expectedq->defaultmark = 0; 284 $expectedq->length = 0; 285 $expectedq->penalty = 0; 286 287 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 288 } 289 290 public function test_export_description() { 291 $qdata = new stdClass(); 292 $qdata->id = 123; 293 $qdata->contextid = 0; 294 $qdata->qtype = 'description'; 295 $qdata->name = 'A description'; 296 $qdata->questiontext = 'The question text.'; 297 $qdata->questiontextformat = FORMAT_HTML; 298 $qdata->generalfeedback = 'Here is some general feedback.'; 299 $qdata->generalfeedbackformat = FORMAT_HTML; 300 $qdata->defaultmark = 0; 301 $qdata->length = 0; 302 $qdata->penalty = 0; 303 $qdata->hidden = 0; 304 305 $exporter = new qformat_xml(); 306 $xml = $exporter->writequestion($qdata); 307 308 $expectedxml = '<!-- question: 123 --> 309 <question type="description"> 310 <name> 311 <text>A description</text> 312 </name> 313 <questiontext format="html"> 314 <text>The question text.</text> 315 </questiontext> 316 <generalfeedback format="html"> 317 <text>Here is some general feedback.</text> 318 </generalfeedback> 319 <defaultgrade>0</defaultgrade> 320 <penalty>0</penalty> 321 <hidden>0</hidden> 322 </question> 323 '; 324 325 $this->assert_same_xml($expectedxml, $xml); 326 } 327 328 public function test_import_essay_20() { 329 $xml = ' <question type="essay"> 330 <name> 331 <text>An essay</text> 332 </name> 333 <questiontext format="moodle_auto_format"> 334 <text>Write something.</text> 335 </questiontext> 336 <generalfeedback> 337 <text>I hope you wrote something interesting.</text> 338 </generalfeedback> 339 <defaultgrade>1</defaultgrade> 340 <penalty>0</penalty> 341 <hidden>0</hidden> 342 </question>'; 343 $xmldata = xmlize($xml); 344 345 $importer = new qformat_xml(); 346 $q = $importer->import_essay($xmldata['question']); 347 348 $expectedq = new stdClass(); 349 $expectedq->qtype = 'essay'; 350 $expectedq->name = 'An essay'; 351 $expectedq->questiontext = 'Write something.'; 352 $expectedq->questiontextformat = FORMAT_MOODLE; 353 $expectedq->generalfeedback = 'I hope you wrote something interesting.'; 354 $expectedq->defaultmark = 1; 355 $expectedq->length = 1; 356 $expectedq->penalty = 0; 357 $expectedq->responseformat = 'editor'; 358 $expectedq->responserequired = 1; 359 $expectedq->responsefieldlines = 15; 360 $expectedq->attachments = 0; 361 $expectedq->attachmentsrequired = 0; 362 $expectedq->graderinfo['text'] = ''; 363 $expectedq->graderinfo['format'] = FORMAT_MOODLE; 364 $expectedq->responsetemplate['text'] = ''; 365 $expectedq->responsetemplate['format'] = FORMAT_MOODLE; 366 367 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 368 } 369 370 public function test_import_essay_21() { 371 $xml = ' <question type="essay"> 372 <name> 373 <text>An essay</text> 374 </name> 375 <questiontext format="moodle_auto_format"> 376 <text>Write something.</text> 377 </questiontext> 378 <generalfeedback> 379 <text>I hope you wrote something interesting.</text> 380 </generalfeedback> 381 <defaultgrade>1</defaultgrade> 382 <penalty>0</penalty> 383 <hidden>0</hidden> 384 <responseformat>monospaced</responseformat> 385 <responserequired>0</responserequired> 386 <responsefieldlines>42</responsefieldlines> 387 <attachments>-1</attachments> 388 <attachmentsrequired>1</attachmentsrequired> 389 <graderinfo format="html"> 390 <text><![CDATA[<p>Grade <b>generously</b>!</p>]]></text> 391 </graderinfo> 392 <responsetemplate format="html"> 393 <text><![CDATA[<p>Here is something <b>really</b> interesting.</p>]]></text> 394 </responsetemplate> 395 </question>'; 396 $xmldata = xmlize($xml); 397 398 $importer = new qformat_xml(); 399 $q = $importer->import_essay($xmldata['question']); 400 401 $expectedq = new stdClass(); 402 $expectedq->qtype = 'essay'; 403 $expectedq->name = 'An essay'; 404 $expectedq->questiontext = 'Write something.'; 405 $expectedq->questiontextformat = FORMAT_MOODLE; 406 $expectedq->generalfeedback = 'I hope you wrote something interesting.'; 407 $expectedq->defaultmark = 1; 408 $expectedq->length = 1; 409 $expectedq->penalty = 0; 410 $expectedq->responseformat = 'monospaced'; 411 $expectedq->responserequired = 0; 412 $expectedq->responsefieldlines = 42; 413 $expectedq->attachments = -1; 414 $expectedq->attachmentsrequired = 1; 415 $expectedq->graderinfo['text'] = '<p>Grade <b>generously</b>!</p>'; 416 $expectedq->graderinfo['format'] = FORMAT_HTML; 417 $expectedq->responsetemplate['text'] = '<p>Here is something <b>really</b> interesting.</p>'; 418 $expectedq->responsetemplate['format'] = FORMAT_HTML; 419 420 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 421 } 422 423 public function test_export_essay() { 424 $qdata = new stdClass(); 425 $qdata->id = 123; 426 $qdata->contextid = 0; 427 $qdata->qtype = 'essay'; 428 $qdata->name = 'An essay'; 429 $qdata->questiontext = 'Write something.'; 430 $qdata->questiontextformat = FORMAT_MOODLE; 431 $qdata->generalfeedback = 'I hope you wrote something interesting.'; 432 $qdata->generalfeedbackformat = FORMAT_MOODLE; 433 $qdata->defaultmark = 1; 434 $qdata->length = 1; 435 $qdata->penalty = 0; 436 $qdata->hidden = 0; 437 $qdata->options = new stdClass(); 438 $qdata->options->id = 456; 439 $qdata->options->questionid = 123; 440 $qdata->options->responseformat = 'monospaced'; 441 $qdata->options->responserequired = 0; 442 $qdata->options->responsefieldlines = 42; 443 $qdata->options->attachments = -1; 444 $qdata->options->attachmentsrequired = 1; 445 $qdata->options->graderinfo = '<p>Grade <b>generously</b>!</p>'; 446 $qdata->options->graderinfoformat = FORMAT_HTML; 447 $qdata->options->responsetemplate = '<p>Here is something <b>really</b> interesting.</p>'; 448 $qdata->options->responsetemplateformat = FORMAT_HTML; 449 $exporter = new qformat_xml(); 450 $xml = $exporter->writequestion($qdata); 451 452 $expectedxml = '<!-- question: 123 --> 453 <question type="essay"> 454 <name> 455 <text>An essay</text> 456 </name> 457 <questiontext format="moodle_auto_format"> 458 <text>Write something.</text> 459 </questiontext> 460 <generalfeedback format="moodle_auto_format"> 461 <text>I hope you wrote something interesting.</text> 462 </generalfeedback> 463 <defaultgrade>1</defaultgrade> 464 <penalty>0</penalty> 465 <hidden>0</hidden> 466 <responseformat>monospaced</responseformat> 467 <responserequired>0</responserequired> 468 <responsefieldlines>42</responsefieldlines> 469 <attachments>-1</attachments> 470 <attachmentsrequired>1</attachmentsrequired> 471 <graderinfo format="html"> 472 <text><![CDATA[<p>Grade <b>generously</b>!</p>]]></text> 473 </graderinfo> 474 <responsetemplate format="html"> 475 <text><![CDATA[<p>Here is something <b>really</b> interesting.</p>]]></text> 476 </responsetemplate> 477 </question> 478 '; 479 480 $this->assert_same_xml($expectedxml, $xml); 481 } 482 483 public function test_import_match_19() { 484 $xml = ' <question type="matching"> 485 <name> 486 <text>Matching question</text> 487 </name> 488 <questiontext format="html"> 489 <text>Match the upper and lower case letters.</text> 490 </questiontext> 491 <generalfeedback> 492 <text>The answer is A -> a, B -> b and C -> c.</text> 493 </generalfeedback> 494 <defaultgrade>1</defaultgrade> 495 <penalty>0.3333333</penalty> 496 <hidden>0</hidden> 497 <shuffleanswers>false</shuffleanswers> 498 <correctfeedback> 499 <text>Well done.</text> 500 </correctfeedback> 501 <partiallycorrectfeedback> 502 <text>Not entirely.</text> 503 </partiallycorrectfeedback> 504 <incorrectfeedback> 505 <text>Completely wrong!</text> 506 </incorrectfeedback> 507 <subquestion> 508 <text>A</text> 509 <answer> 510 <text>a</text> 511 </answer> 512 </subquestion> 513 <subquestion> 514 <text>B</text> 515 <answer> 516 <text>b</text> 517 </answer> 518 </subquestion> 519 <subquestion> 520 <text>C</text> 521 <answer> 522 <text>c</text> 523 </answer> 524 </subquestion> 525 <subquestion> 526 <text></text> 527 <answer> 528 <text>d</text> 529 </answer> 530 </subquestion> 531 <hint> 532 <text>Hint 1</text> 533 <shownumcorrect /> 534 </hint> 535 <hint> 536 <text></text> 537 <shownumcorrect /> 538 <clearwrong /> 539 </hint> 540 </question>'; 541 $xmldata = xmlize($xml); 542 543 $importer = new qformat_xml(); 544 $q = $importer->import_match($xmldata['question']); 545 546 $expectedq = new stdClass(); 547 $expectedq->qtype = 'match'; 548 $expectedq->name = 'Matching question'; 549 $expectedq->questiontext = 'Match the upper and lower case letters.'; 550 $expectedq->questiontextformat = FORMAT_HTML; 551 $expectedq->correctfeedback = array('text' => 'Well done.', 552 'format' => FORMAT_HTML); 553 $expectedq->partiallycorrectfeedback = array('text' => 'Not entirely.', 554 'format' => FORMAT_HTML); 555 $expectedq->shownumcorrect = false; 556 $expectedq->incorrectfeedback = array('text' => 'Completely wrong!', 557 'format' => FORMAT_HTML); 558 $expectedq->generalfeedback = 'The answer is A -> a, B -> b and C -> c.'; 559 $expectedq->generalfeedbackformat = FORMAT_HTML; 560 $expectedq->defaultmark = 1; 561 $expectedq->length = 1; 562 $expectedq->penalty = 0.3333333; 563 $expectedq->shuffleanswers = 0; 564 $expectedq->subquestions = array( 565 array('text' => 'A', 'format' => FORMAT_HTML), 566 array('text' => 'B', 'format' => FORMAT_HTML), 567 array('text' => 'C', 'format' => FORMAT_HTML), 568 array('text' => '', 'format' => FORMAT_HTML)); 569 $expectedq->subanswers = array('a', 'b', 'c', 'd'); 570 $expectedq->hint = array( 571 array('text' => 'Hint 1', 'format' => FORMAT_HTML), 572 array('text' => '', 'format' => FORMAT_HTML), 573 ); 574 $expectedq->hintshownumcorrect = array(true, true); 575 $expectedq->hintclearwrong = array(false, true); 576 577 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 578 } 579 580 public function test_export_match() { 581 $qdata = new stdClass(); 582 $qdata->id = 123; 583 $qdata->contextid = 0; 584 $qdata->qtype = 'match'; 585 $qdata->name = 'Matching question'; 586 $qdata->questiontext = 'Match the upper and lower case letters.'; 587 $qdata->questiontextformat = FORMAT_HTML; 588 $qdata->generalfeedback = 'The answer is A -> a, B -> b and C -> c.'; 589 $qdata->generalfeedbackformat = FORMAT_HTML; 590 $qdata->defaultmark = 1; 591 $qdata->length = 1; 592 $qdata->penalty = 0.3333333; 593 $qdata->hidden = 0; 594 595 $qdata->options = new stdClass(); 596 $qdata->options->shuffleanswers = 1; 597 $qdata->options->correctfeedback = 'Well done.'; 598 $qdata->options->correctfeedbackformat = FORMAT_HTML; 599 $qdata->options->partiallycorrectfeedback = 'Not entirely.'; 600 $qdata->options->partiallycorrectfeedbackformat = FORMAT_HTML; 601 $qdata->options->shownumcorrect = false; 602 $qdata->options->incorrectfeedback = 'Completely wrong!'; 603 $qdata->options->incorrectfeedbackformat = FORMAT_HTML; 604 605 $subq1 = new stdClass(); 606 $subq1->id = -4; 607 $subq1->questiontext = 'A'; 608 $subq1->questiontextformat = FORMAT_HTML; 609 $subq1->answertext = 'a'; 610 611 $subq2 = new stdClass(); 612 $subq2->id = -3; 613 $subq2->questiontext = 'B'; 614 $subq2->questiontextformat = FORMAT_HTML; 615 $subq2->answertext = 'b'; 616 617 $subq3 = new stdClass(); 618 $subq3->id = -2; 619 $subq3->questiontext = 'C'; 620 $subq3->questiontextformat = FORMAT_HTML; 621 $subq3->answertext = 'c'; 622 623 $subq4 = new stdClass(); 624 $subq4->id = -1; 625 $subq4->questiontext = ''; 626 $subq4->questiontextformat = FORMAT_HTML; 627 $subq4->answertext = 'd'; 628 629 $qdata->options->subquestions = array( 630 $subq1, $subq2, $subq3, $subq4); 631 632 $qdata->hints = array( 633 new question_hint_with_parts(0, 'Hint 1', FORMAT_HTML, true, false), 634 new question_hint_with_parts(0, '', FORMAT_HTML, true, true), 635 ); 636 637 $exporter = new qformat_xml(); 638 $xml = $exporter->writequestion($qdata); 639 640 $expectedxml = '<!-- question: 123 --> 641 <question type="matching"> 642 <name> 643 <text>Matching question</text> 644 </name> 645 <questiontext format="html"> 646 <text>Match the upper and lower case letters.</text> 647 </questiontext> 648 <generalfeedback format="html"> 649 <text><![CDATA[The answer is A -> a, B -> b and C -> c.]]></text> 650 </generalfeedback> 651 <defaultgrade>1</defaultgrade> 652 <penalty>0.3333333</penalty> 653 <hidden>0</hidden> 654 <shuffleanswers>true</shuffleanswers> 655 <correctfeedback format="html"> 656 <text>Well done.</text> 657 </correctfeedback> 658 <partiallycorrectfeedback format="html"> 659 <text>Not entirely.</text> 660 </partiallycorrectfeedback> 661 <incorrectfeedback format="html"> 662 <text>Completely wrong!</text> 663 </incorrectfeedback> 664 <subquestion format="html"> 665 <text>A</text> 666 <answer> 667 <text>a</text> 668 </answer> 669 </subquestion> 670 <subquestion format="html"> 671 <text>B</text> 672 <answer> 673 <text>b</text> 674 </answer> 675 </subquestion> 676 <subquestion format="html"> 677 <text>C</text> 678 <answer> 679 <text>c</text> 680 </answer> 681 </subquestion> 682 <subquestion format="html"> 683 <text></text> 684 <answer> 685 <text>d</text> 686 </answer> 687 </subquestion> 688 <hint format="html"> 689 <text>Hint 1</text> 690 <shownumcorrect/> 691 </hint> 692 <hint format="html"> 693 <text></text> 694 <shownumcorrect/> 695 <clearwrong/> 696 </hint> 697 </question> 698 '; 699 700 $this->assert_same_xml($expectedxml, $xml); 701 } 702 703 public function test_import_multichoice_19() { 704 $xml = ' <question type="multichoice"> 705 <name> 706 <text>Multiple choice question</text> 707 </name> 708 <questiontext format="html"> 709 <text>Which are the even numbers?</text> 710 </questiontext> 711 <generalfeedback> 712 <text>The even numbers are 2 and 4.</text> 713 </generalfeedback> 714 <defaultgrade>2</defaultgrade> 715 <penalty>0.3333333</penalty> 716 <hidden>0</hidden> 717 <single>false</single> 718 <shuffleanswers>false</shuffleanswers> 719 <answernumbering>abc</answernumbering> 720 <correctfeedback> 721 <text><![CDATA[<p>Your answer is correct.</p>]]></text> 722 </correctfeedback> 723 <partiallycorrectfeedback> 724 <text><![CDATA[<p>Your answer is partially correct.</p>]]></text> 725 </partiallycorrectfeedback> 726 <incorrectfeedback> 727 <text><![CDATA[<p>Your answer is incorrect.</p>]]></text> 728 </incorrectfeedback> 729 <shownumcorrect/> 730 <answer fraction="0"> 731 <text>1</text> 732 <feedback> 733 <text></text> 734 </feedback> 735 </answer> 736 <answer fraction="100"> 737 <text>2</text> 738 <feedback> 739 <text></text> 740 </feedback> 741 </answer> 742 <answer fraction="0"> 743 <text>3</text> 744 <feedback> 745 <text></text> 746 </feedback> 747 </answer> 748 <answer fraction="100"> 749 <text>4</text> 750 <feedback> 751 <text></text> 752 </feedback> 753 </answer> 754 <hint> 755 <text>Hint 1.</text> 756 </hint> 757 <hint> 758 <text>Hint 2.</text> 759 </hint> 760 </question>'; 761 $xmldata = xmlize($xml); 762 763 $importer = new qformat_xml(); 764 $q = $importer->import_multichoice($xmldata['question']); 765 766 $expectedq = new stdClass(); 767 $expectedq->qtype = 'multichoice'; 768 $expectedq->name = 'Multiple choice question'; 769 $expectedq->questiontext = 'Which are the even numbers?'; 770 $expectedq->questiontextformat = FORMAT_HTML; 771 $expectedq->correctfeedback = array( 772 'text' => '<p>Your answer is correct.</p>', 773 'format' => FORMAT_HTML); 774 $expectedq->shownumcorrect = false; 775 $expectedq->partiallycorrectfeedback = array( 776 'text' => '<p>Your answer is partially correct.</p>', 777 'format' => FORMAT_HTML); 778 $expectedq->shownumcorrect = true; 779 $expectedq->incorrectfeedback = array( 780 'text' => '<p>Your answer is incorrect.</p>', 781 'format' => FORMAT_HTML); 782 $expectedq->generalfeedback = 'The even numbers are 2 and 4.'; 783 $expectedq->defaultmark = 2; 784 $expectedq->length = 1; 785 $expectedq->penalty = 0.3333333; 786 $expectedq->shuffleanswers = 0; 787 $expectedq->single = false; 788 789 $expectedq->answer = array( 790 array('text' => '1', 'format' => FORMAT_HTML), 791 array('text' => '2', 'format' => FORMAT_HTML), 792 array('text' => '3', 'format' => FORMAT_HTML), 793 array('text' => '4', 'format' => FORMAT_HTML)); 794 $expectedq->fraction = array(0, 1, 0, 1); 795 $expectedq->feedback = array( 796 array('text' => '', 'format' => FORMAT_HTML), 797 array('text' => '', 'format' => FORMAT_HTML), 798 array('text' => '', 'format' => FORMAT_HTML), 799 array('text' => '', 'format' => FORMAT_HTML)); 800 801 $expectedq->hint = array( 802 array('text' => 'Hint 1.', 'format' => FORMAT_HTML), 803 array('text' => 'Hint 2.', 'format' => FORMAT_HTML), 804 ); 805 $expectedq->hintshownumcorrect = array(false, false); 806 $expectedq->hintclearwrong = array(false, false); 807 808 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 809 } 810 811 public function test_export_multichoice() { 812 $qdata = new stdClass(); 813 $qdata->id = 123; 814 $qdata->contextid = 0; 815 $qdata->qtype = 'multichoice'; 816 $qdata->name = 'Multiple choice question'; 817 $qdata->questiontext = 'Which are the even numbers?'; 818 $qdata->questiontextformat = FORMAT_HTML; 819 $qdata->generalfeedback = 'The even numbers are 2 and 4.'; 820 $qdata->generalfeedbackformat = FORMAT_HTML; 821 $qdata->defaultmark = 2; 822 $qdata->length = 1; 823 $qdata->penalty = 0.3333333; 824 $qdata->hidden = 0; 825 826 $qdata->options = new stdClass(); 827 $qdata->options->single = 0; 828 $qdata->options->shuffleanswers = 0; 829 $qdata->options->answernumbering = 'abc'; 830 $qdata->options->correctfeedback = '<p>Your answer is correct.</p>'; 831 $qdata->options->correctfeedbackformat = FORMAT_HTML; 832 $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>'; 833 $qdata->options->partiallycorrectfeedbackformat = FORMAT_HTML; 834 $qdata->options->shownumcorrect = 1; 835 $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>'; 836 $qdata->options->incorrectfeedbackformat = FORMAT_HTML; 837 838 $qdata->options->answers = array( 839 13 => new question_answer(13, '1', 0, '', FORMAT_HTML), 840 14 => new question_answer(14, '2', 1, '', FORMAT_HTML), 841 15 => new question_answer(15, '3', 0, '', FORMAT_HTML), 842 16 => new question_answer(16, '4', 1, '', FORMAT_HTML), 843 ); 844 845 $qdata->hints = array( 846 new question_hint_with_parts(0, 'Hint 1.', FORMAT_HTML, false, false), 847 new question_hint_with_parts(0, 'Hint 2.', FORMAT_HTML, false, false), 848 ); 849 850 $exporter = new qformat_xml(); 851 $xml = $exporter->writequestion($qdata); 852 853 $expectedxml = '<!-- question: 123 --> 854 <question type="multichoice"> 855 <name> 856 <text>Multiple choice question</text> 857 </name> 858 <questiontext format="html"> 859 <text>Which are the even numbers?</text> 860 </questiontext> 861 <generalfeedback format="html"> 862 <text>The even numbers are 2 and 4.</text> 863 </generalfeedback> 864 <defaultgrade>2</defaultgrade> 865 <penalty>0.3333333</penalty> 866 <hidden>0</hidden> 867 <single>false</single> 868 <shuffleanswers>false</shuffleanswers> 869 <answernumbering>abc</answernumbering> 870 <correctfeedback format="html"> 871 <text><![CDATA[<p>Your answer is correct.</p>]]></text> 872 </correctfeedback> 873 <partiallycorrectfeedback format="html"> 874 <text><![CDATA[<p>Your answer is partially correct.</p>]]></text> 875 </partiallycorrectfeedback> 876 <incorrectfeedback format="html"> 877 <text><![CDATA[<p>Your answer is incorrect.</p>]]></text> 878 </incorrectfeedback> 879 <shownumcorrect/> 880 <answer fraction="0" format="plain_text"> 881 <text>1</text> 882 <feedback format="html"> 883 <text></text> 884 </feedback> 885 </answer> 886 <answer fraction="100" format="plain_text"> 887 <text>2</text> 888 <feedback format="html"> 889 <text></text> 890 </feedback> 891 </answer> 892 <answer fraction="0" format="plain_text"> 893 <text>3</text> 894 <feedback format="html"> 895 <text></text> 896 </feedback> 897 </answer> 898 <answer fraction="100" format="plain_text"> 899 <text>4</text> 900 <feedback format="html"> 901 <text></text> 902 </feedback> 903 </answer> 904 <hint format="html"> 905 <text>Hint 1.</text> 906 </hint> 907 <hint format="html"> 908 <text>Hint 2.</text> 909 </hint> 910 </question> 911 '; 912 913 $this->assert_same_xml($expectedxml, $xml); 914 } 915 916 public function test_import_numerical_19() { 917 $xml = ' <question type="numerical"> 918 <name> 919 <text>Numerical question</text> 920 </name> 921 <questiontext format="html"> 922 <text>What is the answer?</text> 923 </questiontext> 924 <generalfeedback> 925 <text>General feedback: Think Hitch-hikers guide to the Galaxy.</text> 926 </generalfeedback> 927 <defaultgrade>1</defaultgrade> 928 <penalty>0.1</penalty> 929 <hidden>0</hidden> 930 <answer fraction="100"> 931 <text>42</text> 932 <feedback> 933 <text>Well done!</text> 934 </feedback> 935 <tolerance>0.001</tolerance> 936 </answer> 937 <answer fraction="0"> 938 <text>13</text> 939 <feedback> 940 <text>What were you thinking?!</text> 941 </feedback> 942 <tolerance>1</tolerance> 943 </answer> 944 <answer fraction="0"> 945 <text>*</text> 946 <feedback> 947 <text>Completely wrong.</text> 948 </feedback> 949 <tolerance></tolerance> 950 </answer> 951 </question>'; 952 $xmldata = xmlize($xml); 953 954 $importer = new qformat_xml(); 955 $q = $importer->import_numerical($xmldata['question']); 956 957 $expectedq = new stdClass(); 958 $expectedq->qtype = 'numerical'; 959 $expectedq->name = 'Numerical question'; 960 $expectedq->questiontext = 'What is the answer?'; 961 $expectedq->questiontextformat = FORMAT_HTML; 962 $expectedq->generalfeedback = 'General feedback: Think Hitch-hikers guide to the Galaxy.'; 963 $expectedq->generalfeedbackformat = FORMAT_HTML; 964 $expectedq->defaultmark = 1; 965 $expectedq->length = 1; 966 $expectedq->penalty = 0.1; 967 968 $expectedq->answer = array('42', '13', '*'); 969 $expectedq->fraction = array(1, 0, 0); 970 $expectedq->feedback = array( 971 array('text' => 'Well done!', 972 'format' => FORMAT_HTML), 973 array('text' => 'What were you thinking?!', 974 'format' => FORMAT_HTML), 975 array('text' => 'Completely wrong.', 976 'format' => FORMAT_HTML)); 977 $expectedq->tolerance = array(0.001, 1, 0); 978 979 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 980 } 981 982 public function test_export_numerical() { 983 question_bank::load_question_definition_classes('numerical'); 984 985 $qdata = new stdClass(); 986 $qdata->id = 123; 987 $qdata->contextid = 0; 988 $qdata->qtype = 'numerical'; 989 $qdata->name = 'Numerical question'; 990 $qdata->questiontext = 'What is the answer?'; 991 $qdata->questiontextformat = FORMAT_HTML; 992 $qdata->generalfeedback = 'General feedback: Think Hitch-hikers guide to the Galaxy.'; 993 $qdata->generalfeedbackformat = FORMAT_HTML; 994 $qdata->defaultmark = 1; 995 $qdata->length = 1; 996 $qdata->penalty = 0.1; 997 $qdata->hidden = 0; 998 999 $qdata->options = new stdClass(); 1000 $qdata->options->answers = array( 1001 13 => new qtype_numerical_answer(13, '42', 1, 'Well done!', 1002 FORMAT_HTML, 0.001), 1003 14 => new qtype_numerical_answer(14, '13', 0, 'What were you thinking?!', 1004 FORMAT_HTML, 1), 1005 15 => new qtype_numerical_answer(15, '*', 0, 'Completely wrong.', 1006 FORMAT_HTML, ''), 1007 ); 1008 1009 $qdata->options->units = array(); 1010 1011 $exporter = new qformat_xml(); 1012 $xml = $exporter->writequestion($qdata); 1013 1014 $expectedxml = '<!-- question: 123 --> 1015 <question type="numerical"> 1016 <name> 1017 <text>Numerical question</text> 1018 </name> 1019 <questiontext format="html"> 1020 <text>What is the answer?</text> 1021 </questiontext> 1022 <generalfeedback format="html"> 1023 <text>General feedback: Think Hitch-hikers guide to the Galaxy.</text> 1024 </generalfeedback> 1025 <defaultgrade>1</defaultgrade> 1026 <penalty>0.1</penalty> 1027 <hidden>0</hidden> 1028 <answer fraction="100" format="plain_text"> 1029 <text>42</text> 1030 <feedback format="html"> 1031 <text>Well done!</text> 1032 </feedback> 1033 <tolerance>0.001</tolerance> 1034 </answer> 1035 <answer fraction="0" format="plain_text"> 1036 <text>13</text> 1037 <feedback format="html"> 1038 <text>What were you thinking?!</text> 1039 </feedback> 1040 <tolerance>1</tolerance> 1041 </answer> 1042 <answer fraction="0" format="plain_text"> 1043 <text>*</text> 1044 <feedback format="html"> 1045 <text>Completely wrong.</text> 1046 </feedback> 1047 <tolerance>0</tolerance> 1048 </answer> 1049 </question> 1050 '; 1051 1052 $this->assert_same_xml($expectedxml, $xml); 1053 } 1054 1055 public function test_import_shortanswer_19() { 1056 $xml = ' <question type="shortanswer"> 1057 <name> 1058 <text>Short answer question</text> 1059 </name> 1060 <questiontext format="html"> 1061 <text>Fill in the gap in this sequence: Alpha, ________, Gamma.</text> 1062 </questiontext> 1063 <generalfeedback> 1064 <text>The answer is Beta.</text> 1065 </generalfeedback> 1066 <defaultgrade>1</defaultgrade> 1067 <penalty>0.3333333</penalty> 1068 <hidden>0</hidden> 1069 <usecase>0</usecase> 1070 <answer fraction="100" format="plain_text"> 1071 <text>Beta</text> 1072 <feedback> 1073 <text>Well done!</text> 1074 </feedback> 1075 </answer> 1076 <answer fraction="0" format="plain_text"> 1077 <text>*</text> 1078 <feedback> 1079 <text>Doh!</text> 1080 </feedback> 1081 </answer> 1082 <hint> 1083 <text>Hint 1</text> 1084 </hint> 1085 <hint> 1086 <text>Hint 2</text> 1087 </hint> 1088 </question>'; 1089 $xmldata = xmlize($xml); 1090 1091 $importer = new qformat_xml(); 1092 $q = $importer->import_shortanswer($xmldata['question']); 1093 1094 $expectedq = new stdClass(); 1095 $expectedq->qtype = 'shortanswer'; 1096 $expectedq->name = 'Short answer question'; 1097 $expectedq->questiontext = 'Fill in the gap in this sequence: Alpha, ________, Gamma.'; 1098 $expectedq->questiontextformat = FORMAT_HTML; 1099 $expectedq->generalfeedback = 'The answer is Beta.'; 1100 $expectedq->usecase = false; 1101 $expectedq->defaultmark = 1; 1102 $expectedq->length = 1; 1103 $expectedq->penalty = 0.3333333; 1104 1105 $expectedq->answer = array('Beta', '*'); 1106 $expectedq->fraction = array(1, 0); 1107 $expectedq->feedback = array( 1108 array('text' => 'Well done!', 'format' => FORMAT_HTML), 1109 array('text' => 'Doh!', 'format' => FORMAT_HTML)); 1110 1111 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 1112 } 1113 1114 public function test_export_shortanswer() { 1115 $qdata = new stdClass(); 1116 $qdata->id = 123; 1117 $qdata->contextid = 0; 1118 $qdata->qtype = 'shortanswer'; 1119 $qdata->name = 'Short answer question'; 1120 $qdata->questiontext = 'Fill in the gap in this sequence: Alpha, ________, Gamma.'; 1121 $qdata->questiontextformat = FORMAT_HTML; 1122 $qdata->generalfeedback = 'The answer is Beta.'; 1123 $qdata->generalfeedbackformat = FORMAT_HTML; 1124 $qdata->defaultmark = 1; 1125 $qdata->length = 1; 1126 $qdata->penalty = 0.3333333; 1127 $qdata->hidden = 0; 1128 1129 $qdata->options = new stdClass(); 1130 $qdata->options->usecase = 0; 1131 1132 $qdata->options->answers = array( 1133 13 => new question_answer(13, 'Beta', 1, 'Well done!', FORMAT_HTML), 1134 14 => new question_answer(14, '*', 0, 'Doh!', FORMAT_HTML), 1135 ); 1136 1137 $qdata->hints = array( 1138 new question_hint(0, 'Hint 1', FORMAT_HTML), 1139 new question_hint(0, 'Hint 2', FORMAT_HTML), 1140 ); 1141 1142 $exporter = new qformat_xml(); 1143 $xml = $exporter->writequestion($qdata); 1144 1145 $expectedxml = '<!-- question: 123 --> 1146 <question type="shortanswer"> 1147 <name> 1148 <text>Short answer question</text> 1149 </name> 1150 <questiontext format="html"> 1151 <text>Fill in the gap in this sequence: Alpha, ________, Gamma.</text> 1152 </questiontext> 1153 <generalfeedback format="html"> 1154 <text>The answer is Beta.</text> 1155 </generalfeedback> 1156 <defaultgrade>1</defaultgrade> 1157 <penalty>0.3333333</penalty> 1158 <hidden>0</hidden> 1159 <usecase>0</usecase> 1160 <answer fraction="100" format="plain_text"> 1161 <text>Beta</text> 1162 <feedback format="html"> 1163 <text>Well done!</text> 1164 </feedback> 1165 </answer> 1166 <answer fraction="0" format="plain_text"> 1167 <text>*</text> 1168 <feedback format="html"> 1169 <text>Doh!</text> 1170 </feedback> 1171 </answer> 1172 <hint format="html"> 1173 <text>Hint 1</text> 1174 </hint> 1175 <hint format="html"> 1176 <text>Hint 2</text> 1177 </hint> 1178 </question> 1179 '; 1180 1181 $this->assert_same_xml($expectedxml, $xml); 1182 } 1183 1184 public function test_import_truefalse_19() { 1185 $xml = ' <question type="truefalse"> 1186 <name> 1187 <text>True false question</text> 1188 </name> 1189 <questiontext format="html"> 1190 <text>The answer is true.</text> 1191 </questiontext> 1192 <generalfeedback> 1193 <text>General feedback: You should have chosen true.</text> 1194 </generalfeedback> 1195 <defaultgrade>1</defaultgrade> 1196 <penalty>1</penalty> 1197 <hidden>0</hidden> 1198 <answer fraction="100"> 1199 <text>true</text> 1200 <feedback> 1201 <text>Well done!</text> 1202 </feedback> 1203 </answer> 1204 <answer fraction="0"> 1205 <text>false</text> 1206 <feedback> 1207 <text>Doh!</text> 1208 </feedback> 1209 </answer> 1210 </question>'; 1211 $xmldata = xmlize($xml); 1212 1213 $importer = new qformat_xml(); 1214 $q = $importer->import_truefalse($xmldata['question']); 1215 1216 $expectedq = new stdClass(); 1217 $expectedq->qtype = 'truefalse'; 1218 $expectedq->name = 'True false question'; 1219 $expectedq->questiontext = 'The answer is true.'; 1220 $expectedq->questiontextformat = FORMAT_HTML; 1221 $expectedq->generalfeedback = 'General feedback: You should have chosen true.'; 1222 $expectedq->defaultmark = 1; 1223 $expectedq->length = 1; 1224 $expectedq->penalty = 1; 1225 1226 $expectedq->feedbacktrue = array('text' => 'Well done!', 1227 'format' => FORMAT_HTML); 1228 $expectedq->feedbackfalse = array('text' => 'Doh!', 1229 'format' => FORMAT_HTML); 1230 $expectedq->correctanswer = true; 1231 1232 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 1233 } 1234 1235 public function test_export_truefalse() { 1236 $qdata = new stdClass(); 1237 $qdata->id = 12; 1238 $qdata->contextid = 0; 1239 $qdata->qtype = 'truefalse'; 1240 $qdata->name = 'True false question'; 1241 $qdata->questiontext = 'The answer is true.'; 1242 $qdata->questiontextformat = FORMAT_HTML; 1243 $qdata->generalfeedback = 'General feedback: You should have chosen true.'; 1244 $qdata->generalfeedbackformat = FORMAT_HTML; 1245 $qdata->defaultmark = 1; 1246 $qdata->length = 1; 1247 $qdata->penalty = 1; 1248 $qdata->hidden = 0; 1249 1250 $qdata->options = new stdClass(); 1251 $qdata->options->answers = array( 1252 1 => new question_answer(1, 'True', 1, 'Well done!', FORMAT_HTML), 1253 2 => new question_answer(2, 'False', 0, 'Doh!', FORMAT_HTML), 1254 ); 1255 $qdata->options->trueanswer = 1; 1256 $qdata->options->falseanswer = 2; 1257 1258 $exporter = new qformat_xml(); 1259 $xml = $exporter->writequestion($qdata); 1260 1261 $expectedxml = '<!-- question: 12 --> 1262 <question type="truefalse"> 1263 <name> 1264 <text>True false question</text> 1265 </name> 1266 <questiontext format="html"> 1267 <text>The answer is true.</text> 1268 </questiontext> 1269 <generalfeedback format="html"> 1270 <text>General feedback: You should have chosen true.</text> 1271 </generalfeedback> 1272 <defaultgrade>1</defaultgrade> 1273 <penalty>1</penalty> 1274 <hidden>0</hidden> 1275 <answer fraction="100" format="plain_text"> 1276 <text>true</text> 1277 <feedback format="html"> 1278 <text>Well done!</text> 1279 </feedback> 1280 </answer> 1281 <answer fraction="0" format="plain_text"> 1282 <text>false</text> 1283 <feedback format="html"> 1284 <text>Doh!</text> 1285 </feedback> 1286 </answer> 1287 </question> 1288 '; 1289 1290 $this->assert_same_xml($expectedxml, $xml); 1291 } 1292 1293 public function test_import_multianswer() { 1294 $xml = ' <question type="cloze"> 1295 <name> 1296 <text>Simple multianswer</text> 1297 </name> 1298 <questiontext format="html"> 1299 <text><![CDATA[Complete this opening line of verse: "The {1:SHORTANSWER:Dog#Wrong, silly!~=Owl#Well done!~*#Wrong answer} and the {1:MULTICHOICE:Bow-wow#You seem to have a dog obsessions!~Wiggly worm#Now you are just being ridiculous!~=Pussy-cat#Well done!} went to sea".]]></text> 1300 </questiontext> 1301 <generalfeedback format="html"> 1302 <text><![CDATA[General feedback: It\'s from "The Owl and the Pussy-cat" by Lear: "The owl and the pussycat went to sea".]]></text> 1303 </generalfeedback> 1304 <penalty>0.5</penalty> 1305 <hidden>0</hidden> 1306 <hint format="html"> 1307 <text>Hint 1</text> 1308 </hint> 1309 <hint format="html"> 1310 <text>Hint 2</text> 1311 </hint> 1312 </question> 1313 '; 1314 $xmldata = xmlize($xml); 1315 1316 $importer = new qformat_xml(); 1317 $q = $importer->import_multianswer($xmldata['question']); 1318 1319 // Annoyingly, import works in a weird way (it duplicates code, rather 1320 // than just calling save_question) so we cannot use 1321 // test_question_maker::get_question_form_data('multianswer', 'twosubq'). 1322 $expectedqa = new stdClass(); 1323 $expectedqa->name = 'Simple multianswer'; 1324 $expectedqa->qtype = 'multianswer'; 1325 $expectedqa->questiontext = 'Complete this opening line of verse: "The {#1} and the {#2} went to sea".'; 1326 $expectedqa->generalfeedback = 1327 'General feedback: It\'s from "The Owl and the Pussy-cat" by Lear: "The owl and the pussycat went to sea".'; 1328 $expectedqa->defaultmark = 2; 1329 $expectedqa->penalty = 0.5; 1330 1331 $expectedqa->hint = array( 1332 array('text' => 'Hint 1', 'format' => FORMAT_HTML), 1333 array('text' => 'Hint 2', 'format' => FORMAT_HTML), 1334 ); 1335 1336 $sa = new stdClass(); 1337 1338 $sa->questiontext = array('text' => '{1:SHORTANSWER:Dog#Wrong, silly!~=Owl#Well done!~*#Wrong answer}', 1339 'format' => FORMAT_HTML, 'itemid' => null); 1340 $sa->generalfeedback = array('text' => '', 'format' => FORMAT_HTML, 'itemid' => null); 1341 $sa->defaultmark = 1.0; 1342 $sa->qtype = 'shortanswer'; 1343 $sa->usecase = 0; 1344 1345 $sa->answer = array('Dog', 'Owl', '*'); 1346 $sa->fraction = array(0, 1, 0); 1347 $sa->feedback = array( 1348 array('text' => 'Wrong, silly!', 'format' => FORMAT_HTML, 'itemid' => null), 1349 array('text' => 'Well done!', 'format' => FORMAT_HTML, 'itemid' => null), 1350 array('text' => 'Wrong answer', 'format' => FORMAT_HTML, 'itemid' => null), 1351 ); 1352 1353 $mc = new stdClass(); 1354 1355 $mc->generalfeedback = ''; 1356 $mc->questiontext = array('text' => '{1:MULTICHOICE:Bow-wow#You seem to have a dog obsessions!~' . 1357 'Wiggly worm#Now you are just being ridiculous!~=Pussy-cat#Well done!}', 1358 'format' => FORMAT_HTML, 'itemid' => null); 1359 $mc->generalfeedback = array('text' => '', 'format' => FORMAT_HTML, 'itemid' => null); 1360 $mc->defaultmark = 1.0; 1361 $mc->qtype = 'multichoice'; 1362 1363 $mc->layout = 0; 1364 $mc->single = 1; 1365 $mc->shuffleanswers = 1; 1366 $mc->correctfeedback = array('text' => '', 'format' => FORMAT_HTML, 'itemid' => null); 1367 $mc->partiallycorrectfeedback = array('text' => '', 'format' => FORMAT_HTML, 'itemid' => null); 1368 $mc->incorrectfeedback = array('text' => '', 'format' => FORMAT_HTML, 'itemid' => null); 1369 $mc->answernumbering = 0; 1370 1371 $mc->answer = array( 1372 array('text' => 'Bow-wow', 'format' => FORMAT_HTML, 'itemid' => null), 1373 array('text' => 'Wiggly worm', 'format' => FORMAT_HTML, 'itemid' => null), 1374 array('text' => 'Pussy-cat', 'format' => FORMAT_HTML, 'itemid' => null), 1375 ); 1376 $mc->fraction = array(0, 0, 1); 1377 $mc->feedback = array( 1378 array('text' => 'You seem to have a dog obsessions!', 'format' => FORMAT_HTML, 'itemid' => null), 1379 array('text' => 'Now you are just being ridiculous!', 'format' => FORMAT_HTML, 'itemid' => null), 1380 array('text' => 'Well done!', 'format' => FORMAT_HTML, 'itemid' => null), 1381 ); 1382 1383 $expectedqa->options = new stdClass(); 1384 $expectedqa->options->questions = array( 1385 1 => $sa, 1386 2 => $mc, 1387 ); 1388 1389 $this->assertEquals($expectedqa->hint, $q->hint); 1390 $this->assertEquals($expectedqa->options->questions[1], $q->options->questions[1]); 1391 $this->assertEquals($expectedqa->options->questions[2], $q->options->questions[2]); 1392 $this->assert(new question_check_specified_fields_expectation($expectedqa), $q); 1393 } 1394 1395 public function test_export_multianswer() { 1396 $qdata = test_question_maker::get_question_data('multianswer', 'twosubq'); 1397 1398 $exporter = new qformat_xml(); 1399 $xml = $exporter->writequestion($qdata); 1400 1401 $expectedxml = '<!-- question: 0 --> 1402 <question type="cloze"> 1403 <name> 1404 <text>Simple multianswer</text> 1405 </name> 1406 <questiontext format="html"> 1407 <text><![CDATA[Complete this opening line of verse: "The {1:SHORTANSWER:Dog#Wrong, silly!~=Owl#Well done!~*#Wrong answer} and the {1:MULTICHOICE:Bow-wow#You seem to have a dog obsessions!~Wiggly worm#Now you are just being ridiculous!~=Pussy-cat#Well done!} went to sea".]]></text> 1408 </questiontext> 1409 <generalfeedback format="html"> 1410 <text><![CDATA[General feedback: It\'s from "The Owl and the Pussy-cat" by Lear: "The owl and the pussycat went to sea]]></text> 1411 </generalfeedback> 1412 <penalty>0.3333333</penalty> 1413 <hidden>0</hidden> 1414 <hint format="html"> 1415 <text>Hint 1</text> 1416 </hint> 1417 <hint format="html"> 1418 <text>Hint 2</text> 1419 </hint> 1420 </question> 1421 '; 1422 1423 $this->assert_same_xml($expectedxml, $xml); 1424 } 1425 1426 public function test_export_multianswer_withdollars() { 1427 $qdata = test_question_maker::get_question_data('multianswer', 'dollarsigns'); 1428 1429 $exporter = new qformat_xml(); 1430 $xml = $exporter->writequestion($qdata); 1431 1432 $expectedxml = '<!-- question: 0 --> 1433 <question type="cloze"> 1434 <name> 1435 <text>Multianswer with $s</text> 1436 </name> 1437 <questiontext format="html"> 1438 <text>Which is the right order? {1:MULTICHOICE:=y,y,$3~$3,y,y}</text> 1439 </questiontext> 1440 <generalfeedback format="html"> 1441 <text></text> 1442 </generalfeedback> 1443 <penalty>0.3333333</penalty> 1444 <hidden>0</hidden> 1445 </question> 1446 '; 1447 1448 $this->assert_same_xml($expectedxml, $xml); 1449 } 1450 1451 public function test_import_files_as_draft() { 1452 $this->resetAfterTest(); 1453 $this->setAdminUser(); 1454 1455 $xml = <<<END 1456 <questiontext format="html"> 1457 <text><![CDATA[<p><a href="@@PLUGINFILE@@/moodle.txt">This text file</a> contains the word 'Moodle'.</p>]]></text> 1458 <file name="moodle.txt" encoding="base64">TW9vZGxl</file> 1459 </questiontext> 1460 END; 1461 1462 $textxml = xmlize($xml); 1463 $qo = new stdClass(); 1464 1465 $importer = new qformat_xml(); 1466 $draftitemid = $importer->import_files_as_draft($textxml['questiontext']['#']['file']); 1467 $files = file_get_drafarea_files($draftitemid); 1468 1469 $this->assertEquals(1, count($files->list)); 1470 1471 $file = $files->list[0]; 1472 $this->assertEquals('moodle.txt', $file->filename); 1473 $this->assertEquals('/', $file->filepath); 1474 $this->assertEquals(6, $file->size); 1475 } 1476 1477 public function test_import_truefalse_wih_files() { 1478 $this->resetAfterTest(); 1479 $this->setAdminUser(); 1480 1481 $xml = '<question type="truefalse"> 1482 <name> 1483 <text>truefalse</text> 1484 </name> 1485 <questiontext format="html"> 1486 <text><![CDATA[<p><a href="@@PLUGINFILE@@/myfolder/moodle.txt">This text file</a> contains the word Moodle.</p>]]></text> 1487 <file name="moodle.txt" path="/myfolder/" encoding="base64">TW9vZGxl</file> 1488 </questiontext> 1489 <generalfeedback format="html"> 1490 <text><![CDATA[<p>For further information, see the documentation about Moodle.</p>]]></text> 1491 </generalfeedback> 1492 <defaultgrade>1.0000000</defaultgrade> 1493 <penalty>1.0000000</penalty> 1494 <hidden>0</hidden> 1495 <answer fraction="100" format="moodle_auto_format"> 1496 <text>true</text> 1497 <feedback format="html"> 1498 <text></text> 1499 </feedback> 1500 </answer> 1501 <answer fraction="0" format="moodle_auto_format"> 1502 <text>false</text> 1503 <feedback format="html"> 1504 <text></text> 1505 </feedback> 1506 </answer> 1507 </question>'; 1508 $xmldata = xmlize($xml); 1509 1510 $importer = new qformat_xml(); 1511 $q = $importer->import_truefalse($xmldata['question']); 1512 1513 $draftitemid = $q->questiontextitemid; 1514 $files = file_get_drafarea_files($draftitemid, '/myfolder/'); 1515 1516 $this->assertEquals(1, count($files->list)); 1517 1518 $file = $files->list[0]; 1519 $this->assertEquals('moodle.txt', $file->filename); 1520 $this->assertEquals('/myfolder/', $file->filepath); 1521 $this->assertEquals(6, $file->size); 1522 } 1523 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |