Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/question/format/gift/simpletest/testgiftformat.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.php');
00031 require_once($CFG->dirroot . '/question/format/gift/format.php');
00032 
00033 
00040 class qformat_gift_test extends UnitTestCase {
00041     public function assert_same_gift($expectedtext, $text) {
00042         $this->assertEqual(str_replace("\r\n", "\n", $expectedtext),
00043                 str_replace("\r\n", "\n", $text));
00044     }
00045 
00046     public function test_import_essay() {
00047         $gift = '
00048 // essay
00049 ::Q8:: How are you? {}';
00050         $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
00051 
00052         $importer = new qformat_gift();
00053         $q = $importer->readquestion($lines);
00054 
00055         $expectedq = (object) array(
00056             'name' => 'Q8',
00057             'questiontext' => 'How are you?',
00058             'questiontextformat' => FORMAT_MOODLE,
00059             'generalfeedback' => '',
00060             'generalfeedbackformat' => FORMAT_MOODLE,
00061             'qtype' => 'essay',
00062             'defaultmark' => 1,
00063             'penalty' => 0.3333333,
00064             'length' => 1,
00065             'responseformat' => 'editor',
00066             'responsefieldlines' => 15,
00067             'attachments' => 0,
00068             'graderinfo' => array(
00069                 'text' => '',
00070                 'format' => FORMAT_HTML,
00071                 'files' => array()),
00072         );
00073 
00074         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00075     }
00076 
00077     public function test_export_essay() {
00078         $qdata = (object) array(
00079             'id' => 666 ,
00080             'name' => 'Q8',
00081             'questiontext' => 'How are you?',
00082             'questiontextformat' => FORMAT_MOODLE,
00083             'generalfeedback' => '',
00084             'generalfeedbackformat' => FORMAT_MOODLE,
00085             'defaultmark' => 1,
00086             'penalty' => 0.3333333,
00087             'length' => 1,
00088             'qtype' => 'essay',
00089             'options' => (object) array(
00090                 'responseformat' => 'editor',
00091                 'responsefieldlines' => 15,
00092                 'attachments' => 0,
00093                 'graderinfo' => '',
00094                 'graderinfoformat' => FORMAT_HTML,
00095             ),
00096         );
00097 
00098         $exporter = new qformat_gift();
00099         $gift = $exporter->writequestion($qdata);
00100 
00101         $expectedgift = "// question: 666  name: Q8
00102 ::Q8::How are you?{}
00103 
00104 ";
00105 
00106         $this->assert_same_gift($expectedgift, $gift);
00107     }
00108 
00109     public function test_import_match() {
00110         $gift = '
00111 // question: 2  name: Moodle activities
00112 ::Moodle activities::[html]Match the <b>activity</b> to the description.{
00113     =[html]An activity supporting asynchronous discussions. -> Forum
00114     =[moodle]A teacher asks a question and specifies a choice of multiple responses. -> Choice
00115     =[plain]A bank of record entries which participants can add to. -> Database
00116     =[markdown]A collection of web pages that anyone can add to or edit. -> Wiki
00117     = -> Chat
00118 }';
00119         $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
00120 
00121         $importer = new qformat_gift();
00122         $q = $importer->readquestion($lines);
00123 
00124         $expectedq = (object) array(
00125             'name' => 'Moodle activities',
00126             'questiontext' => 'Match the <b>activity</b> to the description.',
00127             'questiontextformat' => FORMAT_HTML,
00128             'generalfeedback' => '',
00129             'generalfeedbackformat' => FORMAT_HTML,
00130             'qtype' => 'match',
00131             'defaultmark' => 1,
00132             'penalty' => 0.3333333,
00133             'length' => 1,
00134             'shuffleanswers' => '1',
00135             'correctfeedback' => array(
00136                 'text' => '',
00137                 'format' => FORMAT_HTML,
00138                 'files' => array(),
00139             ),
00140             'partiallycorrectfeedback' => array(
00141                 'text' => '',
00142                 'format' => FORMAT_HTML,
00143                 'files' => array(),
00144             ),
00145             'incorrectfeedback' => array(
00146                 'text' => '',
00147                 'format' => FORMAT_HTML,
00148                 'files' => array(),
00149             ),
00150             'subquestions' => array(
00151                 0 => array(
00152                     'text' => 'An activity supporting asynchronous discussions.',
00153                     'format' => FORMAT_HTML,
00154                     'files' => array(),
00155                 ),
00156                 1 => array(
00157                     'text' => 'A teacher asks a question and specifies a choice of multiple responses.',
00158                     'format' => FORMAT_MOODLE,
00159                     'files' => array(),
00160                 ),
00161                 2 => array(
00162                     'text' => 'A bank of record entries which participants can add to.',
00163                     'format' => FORMAT_PLAIN,
00164                     'files' => array(),
00165                 ),
00166                 3 => array(
00167                     'text' => 'A collection of web pages that anyone can add to or edit.',
00168                     'format' => FORMAT_MARKDOWN,
00169                     'files' => array(),
00170                 ),
00171                 4 => array(
00172                     'text' => '',
00173                     'format' => FORMAT_HTML,
00174                     'files' => array(),
00175                 ),
00176             ),
00177             'subanswers' => array(
00178                 0 => 'Forum',
00179                 1 => 'Choice',
00180                 2 => 'Database',
00181                 3 => 'Wiki',
00182                 4 => 'Chat',
00183             ),
00184         );
00185 
00186         // Repeated test for better failure messages.
00187         $this->assertEqual($expectedq->subquestions, $q->subquestions);
00188         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00189     }
00190 
00191     public function test_export_match() {
00192         $qdata = (object) array(
00193             'id' => 666 ,
00194             'name' => 'Moodle activities',
00195             'questiontext' => 'Match the <b>activity</b> to the description.',
00196             'questiontextformat' => FORMAT_HTML,
00197             'generalfeedback' => '',
00198             'generalfeedbackformat' => FORMAT_HTML,
00199             'defaultmark' => 1,
00200             'penalty' => 0.3333333,
00201             'length' => 1,
00202             'qtype' => 'match',
00203             'options' => (object) array(
00204                 'id' => 123,
00205                 'question' => 666,
00206                 'shuffleanswers' => 1,
00207                 'subquestions' => array(
00208                     42 => (object) array(
00209                         'id' => 1234,
00210                         'code' => 12341234,
00211                         'question' => 666,
00212                         'questiontext' => '<div class="frog">An activity supporting asynchronous discussions.</div>',
00213                         'questiontextformat' => FORMAT_HTML,
00214                         'answertext' => 'Forum',
00215                     ),
00216                     43 => (object) array(
00217                         'id' => 1234,
00218                         'code' => 12341234,
00219                         'question' => 666,
00220                         'questiontext' => 'A teacher asks a question and specifies a choice of multiple responses.',
00221                         'questiontextformat' => FORMAT_MOODLE,
00222                         'answertext' => 'Choice',
00223                     ),
00224                     44 => (object) array(
00225                         'id' => 1234,
00226                         'code' => 12341234,
00227                         'question' => 666,
00228                         'questiontext' => 'A bank of record entries which participants can add to.',
00229                         'questiontextformat' => FORMAT_PLAIN,
00230                         'answertext' => 'Database',
00231                     ),
00232                     45 => (object) array(
00233                         'id' => 1234,
00234                         'code' => 12341234,
00235                         'question' => 666,
00236                         'questiontext' => 'A collection of web pages that anyone can add to or edit.',
00237                         'questiontextformat' => FORMAT_MARKDOWN,
00238                         'answertext' => 'Wiki',
00239                     ),
00240                     46 => (object) array(
00241                         'id' => 1234,
00242                         'code' => 12341234,
00243                         'question' => 666,
00244                         'questiontext' => '',
00245                         'questiontextformat' => FORMAT_MARKDOWN,
00246                         'answertext' => 'Chat',
00247                     ),
00248                 ),
00249             ),
00250         );
00251 
00252         $exporter = new qformat_gift();
00253         $gift = $exporter->writequestion($qdata);
00254 
00255         $expectedgift = "// question: 666  name: Moodle activities
00256 ::Moodle activities::[html]Match the <b>activity</b> to the description.{
00257 \t=<div class\\=\"frog\">An activity supporting asynchronous discussions.</div> -> Forum
00258 \t=[moodle]A teacher asks a question and specifies a choice of multiple responses. -> Choice
00259 \t=[plain]A bank of record entries which participants can add to. -> Database
00260 \t=[markdown]A collection of web pages that anyone can add to or edit. -> Wiki
00261 \t= -> Chat
00262 }
00263 
00264 ";
00265 
00266         $this->assert_same_gift($expectedgift, $gift);
00267     }
00268 
00269     public function test_import_multichoice() {
00270         $gift = "
00271 // multiple choice with specified feedback for right and wrong answers
00272 ::Q2:: What's between orange and green in the spectrum?
00273 {
00274     =yellow # right; good!
00275     ~red # [html]wrong, it's yellow
00276     ~[plain]blue # wrong, it's yellow
00277 }";
00278         $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
00279 
00280         $importer = new qformat_gift();
00281         $q = $importer->readquestion($lines);
00282 
00283         $expectedq = (object) array(
00284             'name' => 'Q2',
00285             'questiontext' => "What's between orange and green in the spectrum?",
00286             'questiontextformat' => FORMAT_MOODLE,
00287             'generalfeedback' => '',
00288             'generalfeedbackformat' => FORMAT_MOODLE,
00289             'qtype' => 'multichoice',
00290             'defaultmark' => 1,
00291             'penalty' => 0.3333333,
00292             'length' => 1,
00293             'single' => 1,
00294             'shuffleanswers' => '1',
00295             'answernumbering' => 'abc',
00296             'correctfeedback' => array(
00297                 'text' => '',
00298                 'format' => FORMAT_MOODLE,
00299                 'files' => array(),
00300             ),
00301             'partiallycorrectfeedback' => array(
00302                 'text' => '',
00303                 'format' => FORMAT_MOODLE,
00304                 'files' => array(),
00305             ),
00306             'incorrectfeedback' => array(
00307                 'text' => '',
00308                 'format' => FORMAT_MOODLE,
00309                 'files' => array(),
00310             ),
00311             'answer' => array(
00312                 0 => array(
00313                     'text' => 'yellow',
00314                     'format' => FORMAT_MOODLE,
00315                     'files' => array(),
00316                 ),
00317                 1 => array(
00318                     'text' => 'red',
00319                     'format' => FORMAT_MOODLE,
00320                     'files' => array(),
00321                 ),
00322                 2 => array(
00323                     'text' => 'blue',
00324                     'format' => FORMAT_PLAIN,
00325                     'files' => array(),
00326                 ),
00327             ),
00328             'fraction' => array(1, 0, 0),
00329             'feedback' => array(
00330                 0 => array(
00331                     'text' => 'right; good!',
00332                     'format' => FORMAT_MOODLE,
00333                     'files' => array(),
00334                 ),
00335                 1 => array(
00336                     'text' => "wrong, it's yellow",
00337                     'format' => FORMAT_HTML,
00338                     'files' => array(),
00339                 ),
00340                 2 => array(
00341                     'text' => "wrong, it's yellow",
00342                     'format' => FORMAT_MOODLE,
00343                     'files' => array(),
00344                 ),
00345             ),
00346         );
00347 
00348         // Repeated test for better failure messages.
00349         $this->assertEqual($expectedq->answer, $q->answer);
00350         $this->assertEqual($expectedq->feedback, $q->feedback);
00351         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00352     }
00353 
00354     public function test_import_multichoice_multi() {
00355         $gift = "
00356 // multiple choice, multiple response with specified feedback for right and wrong answers
00357 ::colours:: What's between orange and green in the spectrum?
00358 {
00359     ~%50%yellow # right; good!
00360     ~%-100%red # [html]wrong
00361     ~%50%off-beige # right; good!
00362     ~%-100%[plain]blue # wrong
00363 }";
00364         $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
00365 
00366         $importer = new qformat_gift();
00367         $q = $importer->readquestion($lines);
00368 
00369         $expectedq = (object) array(
00370             'name' => 'colours',
00371             'questiontext' => "What's between orange and green in the spectrum?",
00372             'questiontextformat' => FORMAT_MOODLE,
00373             'generalfeedback' => '',
00374             'generalfeedbackformat' => FORMAT_MOODLE,
00375             'qtype' => 'multichoice',
00376             'defaultmark' => 1,
00377             'penalty' => 0.3333333,
00378             'length' => 1,
00379             'single' => 0,
00380             'shuffleanswers' => '1',
00381             'answernumbering' => 'abc',
00382             'correctfeedback' => array(
00383                 'text' => '',
00384                 'format' => FORMAT_MOODLE,
00385                 'files' => array(),
00386             ),
00387             'partiallycorrectfeedback' => array(
00388                 'text' => '',
00389                 'format' => FORMAT_MOODLE,
00390                 'files' => array(),
00391             ),
00392             'incorrectfeedback' => array(
00393                 'text' => '',
00394                 'format' => FORMAT_MOODLE,
00395                 'files' => array(),
00396             ),
00397             'answer' => array(
00398                 0 => array(
00399                     'text' => 'yellow',
00400                     'format' => FORMAT_MOODLE,
00401                     'files' => array(),
00402                 ),
00403                 1 => array(
00404                     'text' => 'red',
00405                     'format' => FORMAT_MOODLE,
00406                     'files' => array(),
00407                 ),
00408                 2 => array(
00409                     'text' => 'off-beige',
00410                     'format' => FORMAT_MOODLE,
00411                     'files' => array(),
00412                 ),
00413                 3 => array(
00414                     'text' => 'blue',
00415                     'format' => FORMAT_PLAIN,
00416                     'files' => array(),
00417                 ),
00418             ),
00419             'fraction' => array(0.5, -1, 0.5, -1),
00420             'feedback' => array(
00421                 0 => array(
00422                     'text' => 'right; good!',
00423                     'format' => FORMAT_MOODLE,
00424                     'files' => array(),
00425                 ),
00426                 1 => array(
00427                     'text' => "wrong",
00428                     'format' => FORMAT_HTML,
00429                     'files' => array(),
00430                 ),
00431                 2 => array(
00432                     'text' => "right; good!",
00433                     'format' => FORMAT_MOODLE,
00434                     'files' => array(),
00435                 ),
00436                 3 => array(
00437                     'text' => "wrong",
00438                     'format' => FORMAT_MOODLE,
00439                     'files' => array(),
00440                 ),
00441             ),
00442         );
00443 
00444         // Repeated test for better failure messages.
00445         $this->assertEqual($expectedq->answer, $q->answer);
00446         $this->assertEqual($expectedq->feedback, $q->feedback);
00447         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00448     }
00449 
00450     public function test_export_multichoice() {
00451         $qdata = (object) array(
00452             'id' => 666 ,
00453             'name' => 'Q8',
00454             'questiontext' => "What's between orange and green in the spectrum?",
00455             'questiontextformat' => FORMAT_MOODLE,
00456             'generalfeedback' => '',
00457             'generalfeedbackformat' => FORMAT_MOODLE,
00458             'defaultmark' => 1,
00459             'penalty' => 0.3333333,
00460             'length' => 1,
00461             'qtype' => 'multichoice',
00462             'options' => (object) array(
00463                 'single' => 1,
00464                 'shuffleanswers' => '1',
00465                 'answernumbering' => 'abc',
00466                 'correctfeedback' => '',
00467                 'correctfeedbackformat' => FORMAT_MOODLE,
00468                 'partiallycorrectfeedback' => '',
00469                 'partiallycorrectfeedbackformat' => FORMAT_MOODLE,
00470                 'incorrectfeedback' => '',
00471                 'incorrectfeedbackformat' => FORMAT_MOODLE,
00472                 'answers' => array(
00473                     123 => (object) array(
00474                         'id' => 123,
00475                         'answer' => 'yellow',
00476                         'answerformat' => FORMAT_MOODLE,
00477                         'fraction' => 1,
00478                         'feedback' => 'right; good!',
00479                         'feedbackformat' => FORMAT_MOODLE,
00480                     ),
00481                     124 => (object) array(
00482                         'id' => 124,
00483                         'answer' => 'red',
00484                         'answerformat' => FORMAT_MOODLE,
00485                         'fraction' => 0,
00486                         'feedback' => "wrong, it's yellow",
00487                         'feedbackformat' => FORMAT_HTML,
00488                     ),
00489                     125 => (object) array(
00490                         'id' => 125,
00491                         'answer' => 'blue',
00492                         'answerformat' => FORMAT_PLAIN,
00493                         'fraction' => 0,
00494                         'feedback' => "wrong, it's yellow",
00495                         'feedbackformat' => FORMAT_MOODLE,
00496                     ),
00497                 ),
00498             ),
00499         );
00500 
00501         $exporter = new qformat_gift();
00502         $gift = $exporter->writequestion($qdata);
00503 
00504         $expectedgift = "// question: 666  name: Q8
00505 ::Q8::What's between orange and green in the spectrum?{
00506 \t=yellow#right; good!
00507 \t~red#[html]wrong, it's yellow
00508 \t~[plain]blue#wrong, it's yellow
00509 }
00510 
00511 ";
00512 
00513         $this->assert_same_gift($expectedgift, $gift);
00514     }
00515 
00516     public function test_import_numerical() {
00517         $gift = "
00518 // math range question
00519 ::Q5:: What is a number from 1 to 5? {#3:2~#Completely wrong}";
00520         $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
00521 
00522         $importer = new qformat_gift();
00523         $q = $importer->readquestion($lines);
00524 
00525         $expectedq = (object) array(
00526             'name' => 'Q5',
00527             'questiontext' => "What is a number from 1 to 5?",
00528             'questiontextformat' => FORMAT_MOODLE,
00529             'generalfeedback' => '',
00530             'generalfeedbackformat' => FORMAT_MOODLE,
00531             'qtype' => 'numerical',
00532             'defaultmark' => 1,
00533             'penalty' => 0.3333333,
00534             'length' => 1,
00535             'answer' => array(
00536                 '3',
00537                 '*',
00538             ),
00539             'fraction' => array(1, 0),
00540             'feedback' => array(
00541                 0 => array(
00542                     'text' => '',
00543                     'format' => FORMAT_MOODLE,
00544                     'files' => array(),
00545                 ),
00546                 1 => array(
00547                     'text' => "Completely wrong",
00548                     'format' => FORMAT_MOODLE,
00549                     'files' => array(),
00550                 ),
00551             ),
00552             'tolerance' => array(2, 0),
00553         );
00554 
00555         // Repeated test for better failure messages.
00556         $this->assertEqual($expectedq->answer, $q->answer);
00557         $this->assertEqual($expectedq->fraction, $q->fraction);
00558         $this->assertEqual($expectedq->feedback, $q->feedback);
00559         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00560     }
00561 
00562     public function test_export_numerical() {
00563         $qdata = (object) array(
00564             'id' => 666 ,
00565             'name' => 'Q5',
00566             'questiontext' => "What is a number from 1 to 5?",
00567             'questiontextformat' => FORMAT_MOODLE,
00568             'generalfeedback' => '',
00569             'generalfeedbackformat' => FORMAT_MOODLE,
00570             'defaultmark' => 1,
00571             'penalty' => 1,
00572             'length' => 1,
00573             'qtype' => 'numerical',
00574             'options' => (object) array(
00575                 'id' => 123,
00576                 'question' => 666,
00577                 'showunits' => 0,
00578                 'unitsleft' => 0,
00579                 'showunits' => 2,
00580                 'unitgradingtype' => 0,
00581                 'unitpenalty' => 0,
00582                 'answers' => array(
00583                     1 => (object) array(
00584                         'id' => 123,
00585                         'answer' => '3',
00586                         'answerformat' => 0,
00587                         'fraction' => 1,
00588                         'tolerance' => 2,
00589                         'feedback' => '',
00590                         'feedbackformat' => FORMAT_MOODLE,
00591                     ),
00592                     2 => (object) array(
00593                         'id' => 124,
00594                         'answer' => '*',
00595                         'answerformat' => 0,
00596                         'fraction' => 0,
00597                         'tolerance' => 0,
00598                         'feedback' => "Completely wrong",
00599                         'feedbackformat' => FORMAT_MOODLE,
00600                     ),
00601                 ),
00602             ),
00603         );
00604 
00605         $exporter = new qformat_gift();
00606         $gift = $exporter->writequestion($qdata);
00607 
00608         $expectedgift = "// question: 666  name: Q5
00609 ::Q5::What is a number from 1 to 5?{#
00610 \t=%100%3:2#
00611 \t~#Completely wrong
00612 }
00613 
00614 ";
00615 
00616         $this->assert_same_gift($expectedgift, $gift);
00617     }
00618 
00619     public function test_import_shortanswer() {
00620         $gift = "
00621 // question: 666  name: Shortanswer
00622 ::Shortanswer::Which is the best animal?{
00623     =Frog#Good!
00624     =%50%Cat#What is it with Moodlers and cats?
00625     =%0%*#Completely wrong
00626 }";
00627         $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
00628 
00629         $importer = new qformat_gift();
00630         $q = $importer->readquestion($lines);
00631 
00632         $expectedq = (object) array(
00633             'name' => 'Shortanswer',
00634             'questiontext' => "Which is the best animal?",
00635             'questiontextformat' => FORMAT_MOODLE,
00636             'generalfeedback' => '',
00637             'generalfeedbackformat' => FORMAT_MOODLE,
00638             'qtype' => 'shortanswer',
00639             'defaultmark' => 1,
00640             'penalty' => 0.3333333,
00641             'length' => 1,
00642             'answer' => array(
00643                 'Frog',
00644                 'Cat',
00645                 '*',
00646             ),
00647             'fraction' => array(1, 0.5, 0),
00648             'feedback' => array(
00649                 0 => array(
00650                     'text' => 'Good!',
00651                     'format' => FORMAT_MOODLE,
00652                     'files' => array(),
00653                 ),
00654                 1 => array(
00655                     'text' => "What is it with Moodlers and cats?",
00656                     'format' => FORMAT_MOODLE,
00657                     'files' => array(),
00658                 ),
00659                 2 => array(
00660                     'text' => "Completely wrong",
00661                     'format' => FORMAT_MOODLE,
00662                     'files' => array(),
00663                 ),
00664             ),
00665         );
00666 
00667         // Repeated test for better failure messages.
00668         $this->assertEqual($expectedq->answer, $q->answer);
00669         $this->assertEqual($expectedq->fraction, $q->fraction);
00670         $this->assertEqual($expectedq->feedback, $q->feedback);
00671         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00672     }
00673 
00674     public function test_export_shortanswer() {
00675         $qdata = (object) array(
00676             'id' => 666 ,
00677             'name' => 'Shortanswer',
00678             'questiontext' => "Which is the best animal?",
00679             'questiontextformat' => FORMAT_MOODLE,
00680             'generalfeedback' => '',
00681             'generalfeedbackformat' => FORMAT_MOODLE,
00682             'defaultmark' => 1,
00683             'penalty' => 1,
00684             'length' => 1,
00685             'qtype' => 'shortanswer',
00686             'options' => (object) array(
00687                 'id' => 123,
00688                 'question' => 666,
00689                 'usecase' => 1,
00690                 'answers' => array(
00691                     1 => (object) array(
00692                         'id' => 1,
00693                         'answer' => 'Frog',
00694                         'answerformat' => 0,
00695                         'fraction' => 1,
00696                         'feedback' => 'Good!',
00697                         'feedbackformat' => FORMAT_MOODLE,
00698                     ),
00699                     2 => (object) array(
00700                         'id' => 2,
00701                         'answer' => 'Cat',
00702                         'answerformat' => 0,
00703                         'fraction' => 0.5,
00704                         'feedback' => "What is it with Moodlers and cats?",
00705                         'feedbackformat' => FORMAT_MOODLE,
00706                     ),
00707                     3 => (object) array(
00708                         'id' => 3,
00709                         'answer' => '*',
00710                         'answerformat' => 0,
00711                         'fraction' => 0,
00712                         'feedback' => "Completely wrong",
00713                         'feedbackformat' => FORMAT_MOODLE,
00714                     ),
00715                 ),
00716             ),
00717         );
00718 
00719         $exporter = new qformat_gift();
00720         $gift = $exporter->writequestion($qdata);
00721 
00722         $expectedgift = "// question: 666  name: Shortanswer
00723 ::Shortanswer::Which is the best animal?{
00724 \t=%100%Frog#Good!
00725 \t=%50%Cat#What is it with Moodlers and cats?
00726 \t=%0%*#Completely wrong
00727 }
00728 
00729 ";
00730 
00731         $this->assert_same_gift($expectedgift, $gift);
00732     }
00733 
00734     public function test_import_truefalse() {
00735         $gift = "
00736 // true/false
00737 ::Q1:: 42 is the Absolute Answer to everything.{
00738 FALSE#42 is the Ultimate Answer.#You gave the right answer.}";
00739         $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
00740 
00741         $importer = new qformat_gift();
00742         $q = $importer->readquestion($lines);
00743 
00744         $expectedq = (object) array(
00745             'name' => 'Q1',
00746             'questiontext' => "42 is the Absolute Answer to everything.",
00747             'questiontextformat' => FORMAT_MOODLE,
00748             'generalfeedback' => '',
00749             'generalfeedbackformat' => FORMAT_MOODLE,
00750             'qtype' => 'truefalse',
00751             'defaultmark' => 1,
00752             'penalty' => 1,
00753             'length' => 1,
00754             'correctanswer' => 0,
00755             'feedbacktrue' => array(
00756                 'text' => '42 is the Ultimate Answer.',
00757                 'format' => FORMAT_MOODLE,
00758                 'files' => array(),
00759             ),
00760             'feedbackfalse' => array(
00761                 'text' => 'You gave the right answer.',
00762                 'format' => FORMAT_MOODLE,
00763                 'files' => array(),
00764             ),
00765         );
00766 
00767         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00768     }
00769 
00770     public function test_import_truefalse_true_answer1() {
00771         $gift = "// name 0-11
00772 ::2-08 TSL::TSL is blablabla.{T}";
00773         $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
00774 
00775         $importer = new qformat_gift();
00776         $q = $importer->readquestion($lines);
00777 
00778         $expectedq = (object) array(
00779             'name' => '2-08 TSL',
00780             'questiontext' => "TSL is blablabla.",
00781             'questiontextformat' => FORMAT_MOODLE,
00782             'generalfeedback' => '',
00783             'generalfeedbackformat' => FORMAT_MOODLE,
00784             'qtype' => 'truefalse',
00785             'defaultmark' => 1,
00786             'penalty' => 1,
00787             'length' => 1,
00788             'correctanswer' => 1,
00789             'feedbacktrue' => array(
00790                 'text' => '',
00791                 'format' => FORMAT_MOODLE,
00792                 'files' => array(),
00793             ),
00794             'feedbackfalse' => array(
00795                 'text' => '',
00796                 'format' => FORMAT_MOODLE,
00797                 'files' => array(),
00798             ),
00799         );
00800 
00801         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00802     }
00803 
00804     public function test_import_truefalse_true_answer2() {
00805         $gift = "// name 0-11
00806 ::2-08 TSL::TSL is blablabla.{TRUE}";
00807         $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
00808 
00809         $importer = new qformat_gift();
00810         $q = $importer->readquestion($lines);
00811 
00812         $expectedq = (object) array(
00813             'name' => '2-08 TSL',
00814             'questiontext' => "TSL is blablabla.",
00815             'questiontextformat' => FORMAT_MOODLE,
00816             'generalfeedback' => '',
00817             'generalfeedbackformat' => FORMAT_MOODLE,
00818             'qtype' => 'truefalse',
00819             'defaultmark' => 1,
00820             'penalty' => 1,
00821             'length' => 1,
00822             'correctanswer' => 1,
00823             'feedbacktrue' => array(
00824                 'text' => '',
00825                 'format' => FORMAT_MOODLE,
00826                 'files' => array(),
00827             ),
00828             'feedbackfalse' => array(
00829                 'text' => '',
00830                 'format' => FORMAT_MOODLE,
00831                 'files' => array(),
00832             ),
00833         );
00834 
00835         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
00836     }
00837 
00838     public function test_export_truefalse() {
00839         $qdata = (object) array(
00840             'id' => 666 ,
00841             'name' => 'Q1',
00842             'questiontext' => "42 is the Absolute Answer to everything.",
00843             'questiontextformat' => FORMAT_MOODLE,
00844             'generalfeedback' => '',
00845             'generalfeedbackformat' => FORMAT_MOODLE,
00846             'defaultmark' => 1,
00847             'penalty' => 1,
00848             'length' => 1,
00849             'qtype' => 'truefalse',
00850             'options' => (object) array(
00851                 'id' => 123,
00852                 'question' => 666,
00853                 'trueanswer' => 1,
00854                 'falseanswer' => 2,
00855                 'answers' => array(
00856                     1 => (object) array(
00857                         'id' => 123,
00858                         'answer' => 'True',
00859                         'answerformat' => 0,
00860                         'fraction' => 1,
00861                         'feedback' => 'You gave the right answer.',
00862                         'feedbackformat' => FORMAT_MOODLE,
00863                     ),
00864                     2 => (object) array(
00865                         'id' => 124,
00866                         'answer' => 'False',
00867                         'answerformat' => 0,
00868                         'fraction' => 0,
00869                         'feedback' => "42 is the Ultimate Answer.",
00870                         'feedbackformat' => FORMAT_HTML,
00871                     ),
00872                 ),
00873             ),
00874         );
00875 
00876         $exporter = new qformat_gift();
00877         $gift = $exporter->writequestion($qdata);
00878 
00879         $expectedgift = "// question: 666  name: Q1
00880 ::Q1::42 is the Absolute Answer to everything.{TRUE#[html]42 is the Ultimate Answer.#You gave the right answer.}
00881 
00882 ";
00883 
00884         $this->assert_same_gift($expectedgift, $gift);
00885     }
00886 }
 All Data Structures Namespaces Files Functions Variables Enumerations