Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/question/format/xml/simpletest/testxmlformat.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00029 require_once($CFG->libdir . '/questionlib.php');
00030 require_once($CFG->dirroot . '/question/format/xml/format.php');
00031 require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
00032 
00033 
00040 class qformat_xml_test extends UnitTestCase {
00041     public function assert_same_xml($expectedxml, $xml) {
00042         $this->assertEqual(str_replace("\r\n", "\n", $expectedxml),
00043                 str_replace("\r\n", "\n", $xml));
00044     }
00045 
00046     public function make_test_question() {
00047         global $USER;
00048         $q = new stdClass();
00049         $q->id = 0;
00050         $q->contextid = 0;
00051         $q->category = 0;
00052         $q->parent = 0;
00053         $q->questiontextformat = FORMAT_HTML;
00054         $q->generalfeedbackformat = FORMAT_HTML;
00055         $q->defaultmark = 1;
00056         $q->penalty = 0.3333333;
00057         $q->length = 1;
00058         $q->stamp = make_unique_id_code();
00059         $q->version = make_unique_id_code();
00060         $q->hidden = 0;
00061         $q->timecreated = time();
00062         $q->timemodified = time();
00063         $q->createdby = $USER->id;
00064         $q->modifiedby = $USER->id;
00065         return $q;
00066     }
00067 
00076     public function remove_irrelevant_form_data_fields($expectedq) {
00077         return $this->itemid_to_files($expectedq);
00078     }
00079 
00090     protected function itemid_to_files($var) {
00091         if (is_object($var)) {
00092             $newvar = new stdClass();
00093             foreach(get_object_vars($var) as $field => $value) {
00094                 $newvar->$field = $this->itemid_to_files($value);
00095             }
00096 
00097         } else if (is_array($var)) {
00098             $newvar = array();
00099             foreach ($var as $index => $value) {
00100                 if ($index === 'itemid') {
00101                     $newvar['files'] = array();
00102                 } else {
00103                     $newvar[$index] = $this->itemid_to_files($value);
00104                 }
00105             }
00106 
00107         } else {
00108             $newvar = $var;
00109         }
00110 
00111         return $newvar;
00112     }
00113 
00114     public function test_write_hint_basic() {
00115         $q = $this->make_test_question();
00116         $q->name = 'Short answer question';
00117         $q->questiontext = 'Name an amphibian: __________';
00118         $q->generalfeedback = 'Generalfeedback: frog or toad would have been OK.';
00119         $q->options->usecase = false;
00120         $q->options->answers = array(
00121             13 => new question_answer(13, 'frog', 1.0, 'Frog is a very good answer.', FORMAT_HTML),
00122             14 => new question_answer(14, 'toad', 0.8, 'Toad is an OK good answer.', FORMAT_HTML),
00123             15 => new question_answer(15, '*', 0.0, 'That is a bad answer.', FORMAT_HTML),
00124         );
00125         $q->qtype = 'shortanswer';
00126         $q->hints = array(
00127             new question_hint(0, 'This is the first hint.', FORMAT_MOODLE),
00128         );
00129 
00130         $exporter = new qformat_xml();
00131         $xml = $exporter->writequestion($q);
00132 
00133         $this->assertPattern('|<hint format=\"moodle_auto_format\">\s*<text>\s*' .
00134                 'This is the first hint\.\s*</text>\s*</hint>|', $xml);
00135         $this->assertNoPattern('|<shownumcorrect/>|', $xml);
00136         $this->assertNoPattern('|<clearwrong/>|', $xml);
00137         $this->assertNoPattern('|<options>|', $xml);
00138     }
00139 
00140     public function test_write_hint_with_parts() {
00141         $q = $this->make_test_question();
00142         $q->name = 'Matching question';
00143         $q->questiontext = 'Classify the animals.';
00144         $q->generalfeedback = 'Frogs and toads are amphibians, the others are mammals.';
00145         $q->qtype = 'match';
00146 
00147         $q->options->shuffleanswers = 1;
00148         $q->options->correctfeedback = '';
00149         $q->options->correctfeedbackformat = FORMAT_HTML;
00150         $q->options->partiallycorrectfeedback = '';
00151         $q->options->partiallycorrectfeedbackformat = FORMAT_HTML;
00152         $q->options->incorrectfeedback = '';
00153         $q->options->incorrectfeedbackformat = FORMAT_HTML;
00154 
00155         $q->options->subquestions = array();
00156         $q->hints = array(
00157             new question_hint_with_parts(0, 'This is the first hint.', FORMAT_HTML, false, true),
00158             new question_hint_with_parts(0, 'This is the second hint.', FORMAT_HTML, true, false),
00159         );
00160 
00161         $exporter = new qformat_xml();
00162         $xml = $exporter->writequestion($q);
00163 
00164         $this->assertPattern(
00165                 '|<hint format=\"html\">\s*<text>\s*This is the first hint\.\s*</text>|', $xml);
00166         $this->assertPattern(
00167                 '|<hint format=\"html\">\s*<text>\s*This is the second hint\.\s*</text>|', $xml);
00168         list($ignored, $hint1, $hint2) = explode('<hint', $xml);
00169         $this->assertNoPattern('|<shownumcorrect/>|', $hint1);
00170         $this->assertPattern('|<clearwrong/>|', $hint1);
00171         $this->assertPattern('|<shownumcorrect/>|', $hint2);
00172         $this->assertNoPattern('|<clearwrong/>|', $hint2);
00173         $this->assertNoPattern('|<options>|', $xml);
00174     }
00175 
00176     public function test_import_hints_no_parts() {
00177         $xml = <<<END
00178 <question>
00179     <hint>
00180         <text>This is the first hint</text>
00181         <clearwrong/>
00182     </hint>
00183     <hint>
00184         <text>This is the second hint</text>
00185         <shownumcorrect/>
00186     </hint>
00187 </question>
00188 END;
00189 
00190         $questionxml = xmlize($xml);
00191         $qo = new stdClass();
00192 
00193         $importer = new qformat_xml();
00194         $importer->import_hints($qo, $questionxml['question'], false, false, 'html');
00195 
00196         $this->assertEqual(array(
00197                 array('text' => 'This is the first hint',
00198                         'format' => FORMAT_HTML, 'files' => array()),
00199                 array('text' => 'This is the second hint',
00200                         'format' => FORMAT_HTML, 'files' => array()),
00201                 ), $qo->hint);
00202         $this->assertFalse(isset($qo->hintclearwrong));
00203         $this->assertFalse(isset($qo->hintshownumcorrect));
00204     }
00205 
00206     public function test_import_hints_with_parts() {
00207         $xml = <<<END
00208 <question>
00209     <hint>
00210         <text>This is the first hint</text>
00211         <clearwrong/>
00212     </hint>
00213     <hint>
00214         <text>This is the second hint</text>
00215         <shownumcorrect/>
00216     </hint>
00217 </question>
00218 END;
00219 
00220         $questionxml = xmlize($xml);
00221         $qo = new stdClass();
00222 
00223         $importer = new qformat_xml();
00224         $importer->import_hints($qo, $questionxml['question'], true, true, 'html');
00225 
00226         $this->assertEqual(array(
00227                 array('text' => 'This is the first hint',
00228                         'format' => FORMAT_HTML, 'files' => array()),
00229                 array('text' => 'This is the second hint',
00230                         'format' => FORMAT_HTML, 'files' => array()),
00231                 ), $qo->hint);
00232         $this->assertEqual(array(1, 0), $qo->hintclearwrong);
00233         $this->assertEqual(array(0, 1), $qo->hintshownumcorrect);
00234     }
00235 
00236     public function test_import_no_hints_no_error() {
00237         $xml = <<<END
00238 <question>
00239 </question>
00240 END;
00241 
00242         $questionxml = xmlize($xml);
00243         $qo = new stdClass();
00244 
00245         $importer = new qformat_xml();
00246         $importer->import_hints($qo, $questionxml['question'], 'html');
00247 
00248         $this->assertFalse(isset($qo->hint));
00249     }
00250 
00251     public function test_import_description() {
00252         $xml = '  <question type="description">
00253     <name>
00254       <text>A description</text>
00255     </name>
00256     <questiontext format="html">
00257       <text>The question text.</text>
00258     </questiontext>
00259     <generalfeedback>
00260       <text>Here is some general feedback.</text>
00261     </generalfeedback>
00262     <defaultgrade>0</defaultgrade>
00263     <penalty>0</penalty>
00264     <hidden>0</hidden>
00265   </question>';
00266         $xmldata = xmlize($xml);
00267 
00268         $importer = new qformat_xml();
00269         $q = $importer->import_description($xmldata['question']);
00270 
00271         $expectedq = new stdClass();
00272         $expectedq->qtype = 'description';
00273         $expectedq->name = 'A description';
00274         $expectedq->questiontext = 'The question text.';
00275         $expectedq->questiontextformat = FORMAT_HTML;
00276         $expectedq->generalfeedback = 'Here is some general feedback.';
00277         $expectedq->defaultmark = 0;
00278         $expectedq->length = 0;
00279         $expectedq->penalty = 0;
00280 
00281         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00282     }
00283 
00284     public function test_export_description() {
00285         $qdata = new stdClass();
00286         $qdata->id = 123;
00287         $qdata->contextid = 0;
00288         $qdata->qtype = 'description';
00289         $qdata->name = 'A description';
00290         $qdata->questiontext = 'The question text.';
00291         $qdata->questiontextformat = FORMAT_HTML;
00292         $qdata->generalfeedback = 'Here is some general feedback.';
00293         $qdata->generalfeedbackformat = FORMAT_HTML;
00294         $qdata->defaultmark = 0;
00295         $qdata->length = 0;
00296         $qdata->penalty = 0;
00297         $qdata->hidden = 0;
00298 
00299         $exporter = new qformat_xml();
00300         $xml = $exporter->writequestion($qdata);
00301 
00302         $expectedxml = '<!-- question: 123  -->
00303   <question type="description">
00304     <name>
00305       <text>A description</text>
00306     </name>
00307     <questiontext format="html">
00308       <text>The question text.</text>
00309     </questiontext>
00310     <generalfeedback format="html">
00311       <text>Here is some general feedback.</text>
00312     </generalfeedback>
00313     <defaultgrade>0</defaultgrade>
00314     <penalty>0</penalty>
00315     <hidden>0</hidden>
00316   </question>
00317 ';
00318 
00319         $this->assert_same_xml($expectedxml, $xml);
00320     }
00321 
00322     public function test_import_essay_20() {
00323         $xml = '  <question type="essay">
00324     <name>
00325       <text>An essay</text>
00326     </name>
00327     <questiontext format="moodle_auto_format">
00328       <text>Write something.</text>
00329     </questiontext>
00330     <generalfeedback>
00331       <text>I hope you wrote something interesting.</text>
00332     </generalfeedback>
00333     <defaultgrade>1</defaultgrade>
00334     <penalty>0</penalty>
00335     <hidden>0</hidden>
00336   </question>';
00337         $xmldata = xmlize($xml);
00338 
00339         $importer = new qformat_xml();
00340         $q = $importer->import_essay($xmldata['question']);
00341 
00342         $expectedq = new stdClass();
00343         $expectedq->qtype = 'essay';
00344         $expectedq->name = 'An essay';
00345         $expectedq->questiontext = 'Write something.';
00346         $expectedq->questiontextformat = FORMAT_MOODLE;
00347         $expectedq->generalfeedback = 'I hope you wrote something interesting.';
00348         $expectedq->defaultmark = 1;
00349         $expectedq->length = 1;
00350         $expectedq->penalty = 0;
00351         $expectedq->responseformat = 'editor';
00352         $expectedq->responsefieldlines = 15;
00353         $expectedq->attachments = 0;
00354         $expectedq->graderinfo['text'] = '';
00355         $expectedq->graderinfo['format'] = FORMAT_MOODLE;
00356         $expectedq->graderinfo['files'] = array();
00357 
00358         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00359     }
00360 
00361     public function test_import_essay_21() {
00362         $xml = '  <question type="essay">
00363     <name>
00364       <text>An essay</text>
00365     </name>
00366     <questiontext format="moodle_auto_format">
00367       <text>Write something.</text>
00368     </questiontext>
00369     <generalfeedback>
00370       <text>I hope you wrote something interesting.</text>
00371     </generalfeedback>
00372     <defaultgrade>1</defaultgrade>
00373     <penalty>0</penalty>
00374     <hidden>0</hidden>
00375     <responseformat>monospaced</responseformat>
00376     <responsefieldlines>42</responsefieldlines>
00377     <attachments>-1</attachments>
00378     <graderinfo format="html">
00379         <text><![CDATA[<p>Grade <b>generously</b>!</p>]]></text>
00380     </graderinfo>
00381   </question>';
00382         $xmldata = xmlize($xml);
00383 
00384         $importer = new qformat_xml();
00385         $q = $importer->import_essay($xmldata['question']);
00386 
00387         $expectedq = new stdClass();
00388         $expectedq->qtype = 'essay';
00389         $expectedq->name = 'An essay';
00390         $expectedq->questiontext = 'Write something.';
00391         $expectedq->questiontextformat = FORMAT_MOODLE;
00392         $expectedq->generalfeedback = 'I hope you wrote something interesting.';
00393         $expectedq->defaultmark = 1;
00394         $expectedq->length = 1;
00395         $expectedq->penalty = 0;
00396         $expectedq->responseformat = 'monospaced';
00397         $expectedq->responsefieldlines = 42;
00398         $expectedq->attachments = -1;
00399         $expectedq->graderinfo['text'] = '<p>Grade <b>generously</b>!</p>';
00400         $expectedq->graderinfo['format'] = FORMAT_HTML;
00401         $expectedq->graderinfo['files'] = array();
00402 
00403         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00404     }
00405 
00406     public function test_export_essay() {
00407         $qdata = new stdClass();
00408         $qdata->id = 123;
00409         $qdata->contextid = 0;
00410         $qdata->qtype = 'essay';
00411         $qdata->name = 'An essay';
00412         $qdata->questiontext = 'Write something.';
00413         $qdata->questiontextformat = FORMAT_MOODLE;
00414         $qdata->generalfeedback = 'I hope you wrote something interesting.';
00415         $qdata->generalfeedbackformat = FORMAT_MOODLE;
00416         $qdata->defaultmark = 1;
00417         $qdata->length = 1;
00418         $qdata->penalty = 0;
00419         $qdata->hidden = 0;
00420         $qdata->options->id = 456;
00421         $qdata->options->questionid = 123;
00422         $qdata->options->responseformat = 'monospaced';
00423         $qdata->options->responsefieldlines = 42;
00424         $qdata->options->attachments = -1;
00425         $qdata->options->graderinfo = '<p>Grade <b>generously</b>!</p>';
00426         $qdata->options->graderinfoformat = FORMAT_HTML;
00427 
00428         $exporter = new qformat_xml();
00429         $xml = $exporter->writequestion($qdata);
00430 
00431         $expectedxml = '<!-- question: 123  -->
00432   <question type="essay">
00433     <name>
00434       <text>An essay</text>
00435     </name>
00436     <questiontext format="moodle_auto_format">
00437       <text>Write something.</text>
00438     </questiontext>
00439     <generalfeedback format="moodle_auto_format">
00440       <text>I hope you wrote something interesting.</text>
00441     </generalfeedback>
00442     <defaultgrade>1</defaultgrade>
00443     <penalty>0</penalty>
00444     <hidden>0</hidden>
00445     <responseformat>monospaced</responseformat>
00446     <responsefieldlines>42</responsefieldlines>
00447     <attachments>-1</attachments>
00448     <graderinfo format="html">
00449       <text><![CDATA[<p>Grade <b>generously</b>!</p>]]></text>
00450     </graderinfo>
00451   </question>
00452 ';
00453 
00454         $this->assert_same_xml($expectedxml, $xml);
00455     }
00456 
00457     public function test_import_match_19() {
00458         $xml = '  <question type="matching">
00459     <name>
00460       <text>Matching question</text>
00461     </name>
00462     <questiontext format="html">
00463       <text>Match the upper and lower case letters.</text>
00464     </questiontext>
00465     <generalfeedback>
00466       <text>The answer is A -> a, B -> b and C -> c.</text>
00467     </generalfeedback>
00468     <defaultgrade>1</defaultgrade>
00469     <penalty>0.3333333</penalty>
00470     <hidden>0</hidden>
00471     <shuffleanswers>false</shuffleanswers>
00472     <correctfeedback>
00473       <text>Well done.</text>
00474     </correctfeedback>
00475     <partiallycorrectfeedback>
00476       <text>Not entirely.</text>
00477     </partiallycorrectfeedback>
00478     <incorrectfeedback>
00479       <text>Completely wrong!</text>
00480     </incorrectfeedback>
00481     <subquestion>
00482       <text>A</text>
00483       <answer>
00484         <text>a</text>
00485       </answer>
00486     </subquestion>
00487     <subquestion>
00488       <text>B</text>
00489       <answer>
00490         <text>b</text>
00491       </answer>
00492     </subquestion>
00493     <subquestion>
00494       <text>C</text>
00495       <answer>
00496         <text>c</text>
00497       </answer>
00498     </subquestion>
00499     <subquestion>
00500       <text></text>
00501       <answer>
00502         <text>d</text>
00503       </answer>
00504     </subquestion>
00505     <hint>
00506       <text>Hint 1</text>
00507       <shownumcorrect />
00508     </hint>
00509     <hint>
00510       <text></text>
00511       <shownumcorrect />
00512       <clearwrong />
00513     </hint>
00514   </question>';
00515         $xmldata = xmlize($xml);
00516 
00517         $importer = new qformat_xml();
00518         $q = $importer->import_match($xmldata['question']);
00519 
00520         $expectedq = new stdClass();
00521         $expectedq->qtype = 'match';
00522         $expectedq->name = 'Matching question';
00523         $expectedq->questiontext = 'Match the upper and lower case letters.';
00524         $expectedq->questiontextformat = FORMAT_HTML;
00525         $expectedq->correctfeedback = array('text' => 'Well done.',
00526                 'format' => FORMAT_HTML, 'files' => array());
00527         $expectedq->partiallycorrectfeedback = array('text' => 'Not entirely.',
00528                 'format' => FORMAT_HTML, 'files' => array());
00529         $expectedq->shownumcorrect = false;
00530         $expectedq->incorrectfeedback = array('text' => 'Completely wrong!',
00531                 'format' => FORMAT_HTML, 'files' => array());
00532         $expectedq->generalfeedback = 'The answer is A -> a, B -> b and C -> c.';
00533         $expectedq->generalfeedbackformat = FORMAT_HTML;
00534         $expectedq->defaultmark = 1;
00535         $expectedq->length = 1;
00536         $expectedq->penalty = 0.3333333;
00537         $expectedq->shuffleanswers = 0;
00538         $expectedq->subquestions = array(
00539             array('text' => 'A', 'format' => FORMAT_HTML, 'files' => array()),
00540             array('text' => 'B', 'format' => FORMAT_HTML, 'files' => array()),
00541             array('text' => 'C', 'format' => FORMAT_HTML, 'files' => array()),
00542             array('text' => '', 'format' => FORMAT_HTML, 'files' => array()));
00543         $expectedq->subanswers = array('a', 'b', 'c', 'd');
00544         $expectedq->hint = array(
00545             array('text' => 'Hint 1', 'format' => FORMAT_HTML, 'files' => array()),
00546             array('text' => '', 'format' => FORMAT_HTML, 'files' => array()),
00547         );
00548         $expectedq->hintshownumcorrect = array(true, true);
00549         $expectedq->hintclearwrong = array(false, true);
00550 
00551         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00552     }
00553 
00554     public function test_export_match() {
00555         $qdata = new stdClass();
00556         $qdata->id = 123;
00557         $qdata->contextid = 0;
00558         $qdata->qtype = 'match';
00559         $qdata->name = 'Matching question';
00560         $qdata->questiontext = 'Match the upper and lower case letters.';
00561         $qdata->questiontextformat = FORMAT_HTML;
00562         $qdata->generalfeedback = 'The answer is A -> a, B -> b and C -> c.';
00563         $qdata->generalfeedbackformat = FORMAT_HTML;
00564         $qdata->defaultmark = 1;
00565         $qdata->length = 1;
00566         $qdata->penalty = 0.3333333;
00567         $qdata->hidden = 0;
00568 
00569         $qdata->options = new stdClass();
00570         $qdata->options->shuffleanswers = 1;
00571         $qdata->options->correctfeedback = 'Well done.';
00572         $qdata->options->correctfeedbackformat = FORMAT_HTML;
00573         $qdata->options->partiallycorrectfeedback = 'Not entirely.';
00574         $qdata->options->partiallycorrectfeedbackformat = FORMAT_HTML;
00575         $qdata->options->shownumcorrect = false;
00576         $qdata->options->incorrectfeedback = 'Completely wrong!';
00577         $qdata->options->incorrectfeedbackformat = FORMAT_HTML;
00578 
00579         $subq1 = new stdClass();
00580         $subq1->id = -4;
00581         $subq1->questiontext = 'A';
00582         $subq1->questiontextformat = FORMAT_HTML;
00583         $subq1->answertext = 'a';
00584 
00585         $subq2 = new stdClass();
00586         $subq2->id = -3;
00587         $subq2->questiontext = 'B';
00588         $subq2->questiontextformat = FORMAT_HTML;
00589         $subq2->answertext = 'b';
00590 
00591         $subq3 = new stdClass();
00592         $subq3->id = -2;
00593         $subq3->questiontext = 'C';
00594         $subq3->questiontextformat = FORMAT_HTML;
00595         $subq3->answertext = 'c';
00596 
00597         $subq4 = new stdClass();
00598         $subq4->id = -1;
00599         $subq4->questiontext = '';
00600         $subq4->questiontextformat = FORMAT_HTML;
00601         $subq4->answertext = 'd';
00602 
00603         $qdata->options->subquestions = array(
00604                 $subq1, $subq2, $subq3, $subq4);
00605 
00606         $qdata->hints = array(
00607             new question_hint_with_parts(0, 'Hint 1', FORMAT_HTML, true, false),
00608             new question_hint_with_parts(0, '', FORMAT_HTML, true, true),
00609         );
00610 
00611         $exporter = new qformat_xml();
00612         $xml = $exporter->writequestion($qdata);
00613 
00614         $expectedxml = '<!-- question: 123  -->
00615   <question type="matching">
00616     <name>
00617       <text>Matching question</text>
00618     </name>
00619     <questiontext format="html">
00620       <text>Match the upper and lower case letters.</text>
00621     </questiontext>
00622     <generalfeedback format="html">
00623       <text><![CDATA[The answer is A -> a, B -> b and C -> c.]]></text>
00624     </generalfeedback>
00625     <defaultgrade>1</defaultgrade>
00626     <penalty>0.3333333</penalty>
00627     <hidden>0</hidden>
00628     <shuffleanswers>true</shuffleanswers>
00629     <correctfeedback format="html">
00630       <text>Well done.</text>
00631     </correctfeedback>
00632     <partiallycorrectfeedback format="html">
00633       <text>Not entirely.</text>
00634     </partiallycorrectfeedback>
00635     <incorrectfeedback format="html">
00636       <text>Completely wrong!</text>
00637     </incorrectfeedback>
00638     <subquestion format="html">
00639       <text>A</text>
00640       <answer>
00641         <text>a</text>
00642       </answer>
00643     </subquestion>
00644     <subquestion format="html">
00645       <text>B</text>
00646       <answer>
00647         <text>b</text>
00648       </answer>
00649     </subquestion>
00650     <subquestion format="html">
00651       <text>C</text>
00652       <answer>
00653         <text>c</text>
00654       </answer>
00655     </subquestion>
00656     <subquestion format="html">
00657       <text></text>
00658       <answer>
00659         <text>d</text>
00660       </answer>
00661     </subquestion>
00662     <hint format="html">
00663       <text>Hint 1</text>
00664       <shownumcorrect/>
00665     </hint>
00666     <hint format="html">
00667       <text></text>
00668       <shownumcorrect/>
00669       <clearwrong/>
00670     </hint>
00671   </question>
00672 ';
00673 
00674         $this->assert_same_xml($expectedxml, $xml);
00675     }
00676 
00677     public function test_import_multichoice_19() {
00678         $xml = '  <question type="multichoice">
00679     <name>
00680       <text>Multiple choice question</text>
00681     </name>
00682     <questiontext format="html">
00683       <text>Which are the even numbers?</text>
00684     </questiontext>
00685     <generalfeedback>
00686       <text>The even numbers are 2 and 4.</text>
00687     </generalfeedback>
00688     <defaultgrade>2</defaultgrade>
00689     <penalty>0.3333333</penalty>
00690     <hidden>0</hidden>
00691     <single>false</single>
00692     <shuffleanswers>false</shuffleanswers>
00693     <answernumbering>abc</answernumbering>
00694     <correctfeedback>
00695       <text><![CDATA[<p>Your answer is correct.</p>]]></text>
00696     </correctfeedback>
00697     <partiallycorrectfeedback>
00698       <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
00699     </partiallycorrectfeedback>
00700     <incorrectfeedback>
00701       <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
00702     </incorrectfeedback>
00703     <shownumcorrect/>
00704     <answer fraction="0">
00705       <text>1</text>
00706       <feedback>
00707         <text></text>
00708       </feedback>
00709     </answer>
00710     <answer fraction="100">
00711       <text>2</text>
00712       <feedback>
00713         <text></text>
00714       </feedback>
00715     </answer>
00716     <answer fraction="0">
00717       <text>3</text>
00718       <feedback>
00719         <text></text>
00720       </feedback>
00721     </answer>
00722     <answer fraction="100">
00723       <text>4</text>
00724       <feedback>
00725         <text></text>
00726       </feedback>
00727     </answer>
00728     <hint>
00729       <text>Hint 1.</text>
00730     </hint>
00731     <hint>
00732       <text>Hint 2.</text>
00733     </hint>
00734   </question>';
00735         $xmldata = xmlize($xml);
00736 
00737         $importer = new qformat_xml();
00738         $q = $importer->import_multichoice($xmldata['question']);
00739 
00740         $expectedq = new stdClass();
00741         $expectedq->qtype = 'multichoice';
00742         $expectedq->name = 'Multiple choice question';
00743         $expectedq->questiontext = 'Which are the even numbers?';
00744         $expectedq->questiontextformat = FORMAT_HTML;
00745         $expectedq->correctfeedback = array(
00746                 'text'   => '<p>Your answer is correct.</p>',
00747                 'format' => FORMAT_HTML,
00748                 'files'  => array());
00749         $expectedq->shownumcorrect = false;
00750         $expectedq->partiallycorrectfeedback = array(
00751                 'text'   => '<p>Your answer is partially correct.</p>',
00752                 'format' => FORMAT_HTML,
00753                 'files'  => array());
00754         $expectedq->shownumcorrect = true;
00755         $expectedq->incorrectfeedback = array(
00756                 'text'   => '<p>Your answer is incorrect.</p>',
00757                 'format' => FORMAT_HTML,
00758                 'files'  => array());
00759         $expectedq->generalfeedback = 'The even numbers are 2 and 4.';
00760         $expectedq->defaultmark = 2;
00761         $expectedq->length = 1;
00762         $expectedq->penalty = 0.3333333;
00763         $expectedq->shuffleanswers = 0;
00764         $expectedq->single = false;
00765 
00766         $expectedq->answer = array(
00767             array('text' => '1', 'format' => FORMAT_HTML, 'files' => array()),
00768             array('text' => '2', 'format' => FORMAT_HTML, 'files' => array()),
00769             array('text' => '3', 'format' => FORMAT_HTML, 'files' => array()),
00770             array('text' => '4', 'format' => FORMAT_HTML, 'files' => array()));
00771         $expectedq->fraction = array(0, 1, 0, 1);
00772         $expectedq->feedback = array(
00773             array('text' => '', 'format' => FORMAT_HTML, 'files' => array()),
00774             array('text' => '', 'format' => FORMAT_HTML, 'files' => array()),
00775             array('text' => '', 'format' => FORMAT_HTML, 'files' => array()),
00776             array('text' => '', 'format' => FORMAT_HTML, 'files' => array()));
00777 
00778         $expectedq->hint = array(
00779             array('text' => 'Hint 1.', 'format' => FORMAT_HTML, 'files' => array()),
00780             array('text' => 'Hint 2.', 'format' => FORMAT_HTML, 'files' => array()),
00781         );
00782         $expectedq->hintshownumcorrect = array(false, false);
00783         $expectedq->hintclearwrong = array(false, false);
00784 
00785         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00786     }
00787 
00788     public function test_export_multichoice() {
00789         $qdata = new stdClass();
00790         $qdata->id = 123;
00791         $qdata->contextid = 0;
00792         $qdata->qtype = 'multichoice';
00793         $qdata->name = 'Multiple choice question';
00794         $qdata->questiontext = 'Which are the even numbers?';
00795         $qdata->questiontextformat = FORMAT_HTML;
00796         $qdata->generalfeedback = 'The even numbers are 2 and 4.';
00797         $qdata->generalfeedbackformat = FORMAT_HTML;
00798         $qdata->defaultmark = 2;
00799         $qdata->length = 1;
00800         $qdata->penalty = 0.3333333;
00801         $qdata->hidden = 0;
00802 
00803         $qdata->options = new stdClass();
00804         $qdata->options->single = 0;
00805         $qdata->options->shuffleanswers = 0;
00806         $qdata->options->answernumbering = 'abc';
00807         $qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
00808         $qdata->options->correctfeedbackformat = FORMAT_HTML;
00809         $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
00810         $qdata->options->partiallycorrectfeedbackformat = FORMAT_HTML;
00811         $qdata->options->shownumcorrect = 1;
00812         $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
00813         $qdata->options->incorrectfeedbackformat = FORMAT_HTML;
00814 
00815         $qdata->options->answers = array(
00816             13 => new question_answer(13, '1', 0, '', FORMAT_HTML),
00817             14 => new question_answer(14, '2', 1, '', FORMAT_HTML),
00818             15 => new question_answer(15, '3', 0, '', FORMAT_HTML),
00819             16 => new question_answer(16, '4', 1, '', FORMAT_HTML),
00820         );
00821 
00822         $qdata->hints = array(
00823             new question_hint_with_parts(0, 'Hint 1.', FORMAT_HTML, false, false),
00824             new question_hint_with_parts(0, 'Hint 2.', FORMAT_HTML, false, false),
00825         );
00826 
00827         $exporter = new qformat_xml();
00828         $xml = $exporter->writequestion($qdata);
00829 
00830         $expectedxml = '<!-- question: 123  -->
00831   <question type="multichoice">
00832     <name>
00833       <text>Multiple choice question</text>
00834     </name>
00835     <questiontext format="html">
00836       <text>Which are the even numbers?</text>
00837     </questiontext>
00838     <generalfeedback format="html">
00839       <text>The even numbers are 2 and 4.</text>
00840     </generalfeedback>
00841     <defaultgrade>2</defaultgrade>
00842     <penalty>0.3333333</penalty>
00843     <hidden>0</hidden>
00844     <single>false</single>
00845     <shuffleanswers>false</shuffleanswers>
00846     <answernumbering>abc</answernumbering>
00847     <correctfeedback format="html">
00848       <text><![CDATA[<p>Your answer is correct.</p>]]></text>
00849     </correctfeedback>
00850     <partiallycorrectfeedback format="html">
00851       <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
00852     </partiallycorrectfeedback>
00853     <incorrectfeedback format="html">
00854       <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
00855     </incorrectfeedback>
00856     <shownumcorrect/>
00857     <answer fraction="0" format="plain_text">
00858       <text>1</text>
00859       <feedback format="html">
00860         <text></text>
00861       </feedback>
00862     </answer>
00863     <answer fraction="100" format="plain_text">
00864       <text>2</text>
00865       <feedback format="html">
00866         <text></text>
00867       </feedback>
00868     </answer>
00869     <answer fraction="0" format="plain_text">
00870       <text>3</text>
00871       <feedback format="html">
00872         <text></text>
00873       </feedback>
00874     </answer>
00875     <answer fraction="100" format="plain_text">
00876       <text>4</text>
00877       <feedback format="html">
00878         <text></text>
00879       </feedback>
00880     </answer>
00881     <hint format="html">
00882       <text>Hint 1.</text>
00883     </hint>
00884     <hint format="html">
00885       <text>Hint 2.</text>
00886     </hint>
00887   </question>
00888 ';
00889 
00890         $this->assert_same_xml($expectedxml, $xml);
00891     }
00892 
00893     public function test_import_numerical_19() {
00894         $xml = '  <question type="numerical">
00895     <name>
00896       <text>Numerical question</text>
00897     </name>
00898     <questiontext format="html">
00899       <text>What is the answer?</text>
00900     </questiontext>
00901     <generalfeedback>
00902       <text>General feedback: Think Hitch-hikers guide to the Galaxy.</text>
00903     </generalfeedback>
00904     <defaultgrade>1</defaultgrade>
00905     <penalty>0.1</penalty>
00906     <hidden>0</hidden>
00907     <answer fraction="100">
00908       <text>42</text>
00909       <feedback>
00910         <text>Well done!</text>
00911       </feedback>
00912       <tolerance>0.001</tolerance>
00913     </answer>
00914     <answer fraction="0">
00915       <text>13</text>
00916       <feedback>
00917         <text>What were you thinking?!</text>
00918       </feedback>
00919       <tolerance>1</tolerance>
00920     </answer>
00921     <answer fraction="0">
00922       <text>*</text>
00923       <feedback>
00924         <text>Completely wrong.</text>
00925       </feedback>
00926       <tolerance></tolerance>
00927     </answer>
00928   </question>';
00929         $xmldata = xmlize($xml);
00930 
00931         $importer = new qformat_xml();
00932         $q = $importer->import_numerical($xmldata['question']);
00933 
00934         $expectedq = new stdClass();
00935         $expectedq->qtype = 'numerical';
00936         $expectedq->name = 'Numerical question';
00937         $expectedq->questiontext = 'What is the answer?';
00938         $expectedq->questiontextformat = FORMAT_HTML;
00939         $expectedq->generalfeedback = 'General feedback: Think Hitch-hikers guide to the Galaxy.';
00940         $expectedq->generalfeedbackformat = FORMAT_HTML;
00941         $expectedq->defaultmark = 1;
00942         $expectedq->length = 1;
00943         $expectedq->penalty = 0.1;
00944 
00945         $expectedq->answer = array('42', '13', '*');
00946         $expectedq->fraction = array(1, 0, 0);
00947         $expectedq->feedback = array(
00948             array('text' => 'Well done!',
00949                     'format' => FORMAT_HTML, 'files' => array()),
00950             array('text' => 'What were you thinking?!',
00951                     'format' => FORMAT_HTML, 'files' => array()),
00952             array('text' => 'Completely wrong.',
00953                     'format' => FORMAT_HTML, 'files' => array()));
00954         $expectedq->tolerance = array(0.001, 1, 0);
00955 
00956         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00957     }
00958 
00959     public function test_export_numerical() {
00960         question_bank::load_question_definition_classes('numerical');
00961 
00962         $qdata = new stdClass();
00963         $qdata->id = 123;
00964         $qdata->contextid = 0;
00965         $qdata->qtype = 'numerical';
00966         $qdata->name = 'Numerical question';
00967         $qdata->questiontext = 'What is the answer?';
00968         $qdata->questiontextformat = FORMAT_HTML;
00969         $qdata->generalfeedback = 'General feedback: Think Hitch-hikers guide to the Galaxy.';
00970         $qdata->generalfeedbackformat = FORMAT_HTML;
00971         $qdata->defaultmark = 1;
00972         $qdata->length = 1;
00973         $qdata->penalty = 0.1;
00974         $qdata->hidden = 0;
00975 
00976         $qdata->options->answers = array(
00977             13 => new qtype_numerical_answer(13, '42', 1, 'Well done!',
00978                     FORMAT_HTML, 0.001),
00979             14 => new qtype_numerical_answer(14, '13', 0, 'What were you thinking?!',
00980                     FORMAT_HTML, 1),
00981             15 => new qtype_numerical_answer(15, '*', 0, 'Completely wrong.',
00982                     FORMAT_HTML, ''),
00983         );
00984 
00985         $qdata->options->units = array();
00986 
00987         $exporter = new qformat_xml();
00988         $xml = $exporter->writequestion($qdata);
00989 
00990         $expectedxml = '<!-- question: 123  -->
00991   <question type="numerical">
00992     <name>
00993       <text>Numerical question</text>
00994     </name>
00995     <questiontext format="html">
00996       <text>What is the answer?</text>
00997     </questiontext>
00998     <generalfeedback format="html">
00999       <text>General feedback: Think Hitch-hikers guide to the Galaxy.</text>
01000     </generalfeedback>
01001     <defaultgrade>1</defaultgrade>
01002     <penalty>0.1</penalty>
01003     <hidden>0</hidden>
01004     <answer fraction="100" format="plain_text">
01005       <text>42</text>
01006       <feedback format="html">
01007         <text>Well done!</text>
01008       </feedback>
01009       <tolerance>0.001</tolerance>
01010     </answer>
01011     <answer fraction="0" format="plain_text">
01012       <text>13</text>
01013       <feedback format="html">
01014         <text>What were you thinking?!</text>
01015       </feedback>
01016       <tolerance>1</tolerance>
01017     </answer>
01018     <answer fraction="0" format="plain_text">
01019       <text>*</text>
01020       <feedback format="html">
01021         <text>Completely wrong.</text>
01022       </feedback>
01023       <tolerance>0</tolerance>
01024     </answer>
01025   </question>
01026 ';
01027 
01028         $this->assert_same_xml($expectedxml, $xml);
01029     }
01030 
01031     public function test_import_shortanswer_19() {
01032         $xml = '  <question type="shortanswer">
01033     <name>
01034       <text>Short answer question</text>
01035     </name>
01036     <questiontext format="html">
01037       <text>Fill in the gap in this sequence: Alpha, ________, Gamma.</text>
01038     </questiontext>
01039     <generalfeedback>
01040       <text>The answer is Beta.</text>
01041     </generalfeedback>
01042     <defaultgrade>1</defaultgrade>
01043     <penalty>0.3333333</penalty>
01044     <hidden>0</hidden>
01045     <usecase>0</usecase>
01046     <answer fraction="100" format="plain_text">
01047       <text>Beta</text>
01048       <feedback>
01049         <text>Well done!</text>
01050       </feedback>
01051     </answer>
01052     <answer fraction="0" format="plain_text">
01053       <text>*</text>
01054       <feedback>
01055         <text>Doh!</text>
01056       </feedback>
01057     </answer>
01058     <hint>
01059       <text>Hint 1</text>
01060     </hint>
01061     <hint>
01062       <text>Hint 2</text>
01063     </hint>
01064   </question>';
01065         $xmldata = xmlize($xml);
01066 
01067         $importer = new qformat_xml();
01068         $q = $importer->import_shortanswer($xmldata['question']);
01069 
01070         $expectedq = new stdClass();
01071         $expectedq->qtype = 'shortanswer';
01072         $expectedq->name = 'Short answer question';
01073         $expectedq->questiontext = 'Fill in the gap in this sequence: Alpha, ________, Gamma.';
01074         $expectedq->questiontextformat = FORMAT_HTML;
01075         $expectedq->generalfeedback = 'The answer is Beta.';
01076         $expectedq->usecase = false;
01077         $expectedq->defaultmark = 1;
01078         $expectedq->length = 1;
01079         $expectedq->penalty = 0.3333333;
01080 
01081         $expectedq->answer = array('Beta', '*');
01082         $expectedq->fraction = array(1, 0);
01083         $expectedq->feedback = array(
01084             array('text' => 'Well done!', 'format' => FORMAT_HTML, 'files' => array()),
01085             array('text' => 'Doh!', 'format' => FORMAT_HTML, 'files' => array()));
01086 
01087         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
01088     }
01089 
01090     public function test_export_shortanswer() {
01091         $qdata = new stdClass();
01092         $qdata->id = 123;
01093         $qdata->contextid = 0;
01094         $qdata->qtype = 'shortanswer';
01095         $qdata->name = 'Short answer question';
01096         $qdata->questiontext = 'Fill in the gap in this sequence: Alpha, ________, Gamma.';
01097         $qdata->questiontextformat = FORMAT_HTML;
01098         $qdata->generalfeedback = 'The answer is Beta.';
01099         $qdata->generalfeedbackformat = FORMAT_HTML;
01100         $qdata->defaultmark = 1;
01101         $qdata->length = 1;
01102         $qdata->penalty = 0.3333333;
01103         $qdata->hidden = 0;
01104 
01105         $qdata->options->usecase = 0;
01106 
01107         $qdata->options->answers = array(
01108             13 => new question_answer(13, 'Beta', 1, 'Well done!', FORMAT_HTML),
01109             14 => new question_answer(14, '*', 0, 'Doh!', FORMAT_HTML),
01110         );
01111 
01112         $qdata->hints = array(
01113             new question_hint(0, 'Hint 1', FORMAT_HTML),
01114             new question_hint(0, 'Hint 2', FORMAT_HTML),
01115         );
01116 
01117         $exporter = new qformat_xml();
01118         $xml = $exporter->writequestion($qdata);
01119 
01120         $expectedxml = '<!-- question: 123  -->
01121   <question type="shortanswer">
01122     <name>
01123       <text>Short answer question</text>
01124     </name>
01125     <questiontext format="html">
01126       <text>Fill in the gap in this sequence: Alpha, ________, Gamma.</text>
01127     </questiontext>
01128     <generalfeedback format="html">
01129       <text>The answer is Beta.</text>
01130     </generalfeedback>
01131     <defaultgrade>1</defaultgrade>
01132     <penalty>0.3333333</penalty>
01133     <hidden>0</hidden>
01134     <usecase>0</usecase>
01135     <answer fraction="100" format="plain_text">
01136       <text>Beta</text>
01137       <feedback format="html">
01138         <text>Well done!</text>
01139       </feedback>
01140     </answer>
01141     <answer fraction="0" format="plain_text">
01142       <text>*</text>
01143       <feedback format="html">
01144         <text>Doh!</text>
01145       </feedback>
01146     </answer>
01147     <hint format="html">
01148       <text>Hint 1</text>
01149     </hint>
01150     <hint format="html">
01151       <text>Hint 2</text>
01152     </hint>
01153   </question>
01154 ';
01155 
01156         $this->assert_same_xml($expectedxml, $xml);
01157     }
01158 
01159     public function test_import_truefalse_19() {
01160         $xml = '  <question type="truefalse">
01161     <name>
01162       <text>True false question</text>
01163     </name>
01164     <questiontext format="html">
01165       <text>The answer is true.</text>
01166     </questiontext>
01167     <generalfeedback>
01168       <text>General feedback: You should have chosen true.</text>
01169     </generalfeedback>
01170     <defaultgrade>1</defaultgrade>
01171     <penalty>1</penalty>
01172     <hidden>0</hidden>
01173     <answer fraction="100">
01174       <text>true</text>
01175       <feedback>
01176         <text>Well done!</text>
01177       </feedback>
01178     </answer>
01179     <answer fraction="0">
01180       <text>false</text>
01181       <feedback>
01182         <text>Doh!</text>
01183       </feedback>
01184     </answer>
01185   </question>';
01186         $xmldata = xmlize($xml);
01187 
01188         $importer = new qformat_xml();
01189         $q = $importer->import_truefalse($xmldata['question']);
01190 
01191         $expectedq = new stdClass();
01192         $expectedq->qtype = 'truefalse';
01193         $expectedq->name = 'True false question';
01194         $expectedq->questiontext = 'The answer is true.';
01195         $expectedq->questiontextformat = FORMAT_HTML;
01196         $expectedq->generalfeedback = 'General feedback: You should have chosen true.';
01197         $expectedq->defaultmark = 1;
01198         $expectedq->length = 1;
01199         $expectedq->penalty = 1;
01200 
01201         $expectedq->feedbacktrue = array('text' => 'Well done!',
01202                 'format' => FORMAT_HTML, 'files' => array());
01203         $expectedq->feedbackfalse = array('text' => 'Doh!',
01204                 'format' => FORMAT_HTML, 'files' => array());
01205         $expectedq->correctanswer = true;
01206 
01207         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
01208     }
01209 
01210     public function test_export_truefalse() {
01211         $qdata = new stdClass();
01212         $qdata->id = 12;
01213         $qdata->contextid = 0;
01214         $qdata->qtype = 'truefalse';
01215         $qdata->name = 'True false question';
01216         $qdata->questiontext = 'The answer is true.';
01217         $qdata->questiontextformat = FORMAT_HTML;
01218         $qdata->generalfeedback = 'General feedback: You should have chosen true.';
01219         $qdata->generalfeedbackformat = FORMAT_HTML;
01220         $qdata->defaultmark = 1;
01221         $qdata->length = 1;
01222         $qdata->penalty = 1;
01223         $qdata->hidden = 0;
01224 
01225         $qdata->options->answers = array(
01226             1 => new question_answer(1, 'True', 1, 'Well done!', FORMAT_HTML),
01227             2 => new question_answer(2, 'False', 0, 'Doh!', FORMAT_HTML),
01228         );
01229         $qdata->options->trueanswer = 1;
01230         $qdata->options->falseanswer = 2;
01231 
01232         $exporter = new qformat_xml();
01233         $xml = $exporter->writequestion($qdata);
01234 
01235         $expectedxml = '<!-- question: 12  -->
01236   <question type="truefalse">
01237     <name>
01238       <text>True false question</text>
01239     </name>
01240     <questiontext format="html">
01241       <text>The answer is true.</text>
01242     </questiontext>
01243     <generalfeedback format="html">
01244       <text>General feedback: You should have chosen true.</text>
01245     </generalfeedback>
01246     <defaultgrade>1</defaultgrade>
01247     <penalty>1</penalty>
01248     <hidden>0</hidden>
01249     <answer fraction="100" format="plain_text">
01250       <text>true</text>
01251       <feedback format="html">
01252         <text>Well done!</text>
01253       </feedback>
01254     </answer>
01255     <answer fraction="0" format="plain_text">
01256       <text>false</text>
01257       <feedback format="html">
01258         <text>Doh!</text>
01259       </feedback>
01260     </answer>
01261   </question>
01262 ';
01263 
01264         $this->assert_same_xml($expectedxml, $xml);
01265     }
01266 
01267     public function test_import_multianswer() {
01268         $xml = '  <question type="cloze">
01269     <name>
01270       <text>Simple multianswer</text>
01271     </name>
01272     <questiontext format="html">
01273       <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>
01274     </questiontext>
01275     <generalfeedback format="html">
01276       <text><![CDATA[General feedback: It\'s from "The Owl and the Pussy-cat" by Lear: "The owl and the pussycat went to sea".]]></text>
01277     </generalfeedback>
01278     <penalty>0.5</penalty>
01279     <hidden>0</hidden>
01280     <hint format="html">
01281       <text>Hint 1</text>
01282     </hint>
01283     <hint format="html">
01284       <text>Hint 2</text>
01285     </hint>
01286   </question>
01287 ';
01288         $xmldata = xmlize($xml);
01289 
01290         $importer = new qformat_xml();
01291         $q = $importer->import_multianswer($xmldata['question']);
01292 
01293         // Annoyingly, import works in a weird way (it duplicates code, rather
01294         // than just calling save_question) so we cannot use
01295         // test_question_maker::get_question_form_data('multianswer', 'twosubq').
01296         $expectedqa = new stdClass();
01297         $expectedqa->name = 'Simple multianswer';
01298         $expectedqa->qtype = 'multianswer';
01299         $expectedqa->questiontext = 'Complete this opening line of verse: "The {#1} and the {#2} went to sea".';
01300         $expectedqa->generalfeedback = 'General feedback: It\'s from "The Owl and the Pussy-cat" by Lear: "The owl and the pussycat went to sea".';
01301         $expectedqa->defaultmark = 2;
01302         $expectedqa->penalty = 0.5;
01303 
01304         $expectedqa->hint = array(
01305             array('text' => 'Hint 1', 'format' => FORMAT_HTML, 'files' => array()),
01306             array('text' => 'Hint 2', 'format' => FORMAT_HTML, 'files' => array()),
01307         );
01308 
01309         $sa = new stdClass();
01310 
01311         $sa->questiontext = array('text' => '{1:SHORTANSWER:Dog#Wrong, silly!~=Owl#Well done!~*#Wrong answer}',
01312                 'format' => FORMAT_HTML, 'itemid' => null);
01313         $sa->generalfeedback = array('text' => '', 'format' => FORMAT_HTML, 'itemid' => null);
01314         $sa->defaultmark = 1.0;
01315         $sa->qtype = 'shortanswer';
01316         $sa->usecase = 0;
01317 
01318         $sa->answer = array('Dog', 'Owl', '*');
01319         $sa->fraction = array(0, 1, 0);
01320         $sa->feedback = array(
01321             array('text' => 'Wrong, silly!', 'format' => FORMAT_HTML, 'itemid' => null),
01322             array('text' => 'Well done!',    'format' => FORMAT_HTML, 'itemid' => null),
01323             array('text' => 'Wrong answer',  'format' => FORMAT_HTML, 'itemid' => null),
01324         );
01325 
01326         $mc = new stdClass();
01327 
01328         $mc->generalfeedback = '';
01329         $mc->questiontext = array('text' => '{1:MULTICHOICE:Bow-wow#You seem to have a dog obsessions!~' .
01330                 'Wiggly worm#Now you are just being ridiculous!~=Pussy-cat#Well done!}',
01331                 'format' => FORMAT_HTML, 'itemid' => null);
01332         $mc->generalfeedback = array('text' => '', 'format' => FORMAT_HTML, 'itemid' => null);
01333         $mc->defaultmark = 1.0;
01334         $mc->qtype = 'multichoice';
01335 
01336         $mc->layout = 0;
01337         $mc->single = 1;
01338         $mc->shuffleanswers = 1;
01339         $mc->correctfeedback =          array('text' => '', 'format' => FORMAT_HTML, 'itemid' => null);
01340         $mc->partiallycorrectfeedback = array('text' => '', 'format' => FORMAT_HTML, 'itemid' => null);
01341         $mc->incorrectfeedback =        array('text' => '', 'format' => FORMAT_HTML, 'itemid' => null);
01342         $mc->answernumbering = 0;
01343 
01344         $mc->answer = array(
01345             array('text' => 'Bow-wow',     'format' => FORMAT_HTML, 'itemid' => null),
01346             array('text' => 'Wiggly worm', 'format' => FORMAT_HTML, 'itemid' => null),
01347             array('text' => 'Pussy-cat',   'format' => FORMAT_HTML, 'itemid' => null),
01348         );
01349         $mc->fraction = array(0, 0, 1);
01350         $mc->feedback = array(
01351             array('text' => 'You seem to have a dog obsessions!', 'format' => FORMAT_HTML, 'itemid' => null),
01352             array('text' => 'Now you are just being ridiculous!', 'format' => FORMAT_HTML, 'itemid' => null),
01353             array('text' => 'Well done!',                         'format' => FORMAT_HTML, 'itemid' => null),
01354         );
01355 
01356         $expectedqa->options = new stdClass();
01357         $expectedqa->options->questions = array(
01358             1 => $sa,
01359             2 => $mc,
01360         );
01361 
01362         $this->assertEqual($expectedqa->hint, $q->hint);
01363         $this->assertEqual($expectedqa->options->questions[1], $q->options->questions[1]);
01364         $this->assertEqual($expectedqa->options->questions[2], $q->options->questions[2]);
01365         $this->assert(new CheckSpecifiedFieldsExpectation($expectedqa), $q);
01366     }
01367 
01368     public function test_export_multianswer() {
01369         $qdata = test_question_maker::get_question_data('multianswer', 'twosubq');
01370 
01371         $exporter = new qformat_xml();
01372         $xml = $exporter->writequestion($qdata);
01373 
01374         $expectedxml = '<!-- question: 0  -->
01375   <question type="cloze">
01376     <name>
01377       <text>Simple multianswer</text>
01378     </name>
01379     <questiontext format="html">
01380       <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>
01381     </questiontext>
01382     <generalfeedback format="html">
01383       <text><![CDATA[General feedback: It\'s from "The Owl and the Pussy-cat" by Lear: "The owl and the pussycat went to sea]]></text>
01384     </generalfeedback>
01385     <penalty>0.3333333</penalty>
01386     <hidden>0</hidden>
01387     <hint format="html">
01388       <text>Hint 1</text>
01389     </hint>
01390     <hint format="html">
01391       <text>Hint 2</text>
01392     </hint>
01393   </question>
01394 ';
01395 
01396         $this->assert_same_xml($expectedxml, $xml);
01397     }
01398 }
 All Data Structures Namespaces Files Functions Variables Enumerations