Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace qformat_blackboard_six;
  18  
  19  use qformat_blackboard_six;
  20  use qformat_blackboard_six_file;
  21  use question_bank;
  22  use question_check_specified_fields_expectation;
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  global $CFG;
  27  require_once($CFG->libdir . '/questionlib.php');
  28  require_once($CFG->dirroot . '/question/format.php');
  29  require_once($CFG->dirroot . '/question/format/blackboard_six/format.php');
  30  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  31  
  32  /**
  33   * Unit tests for the blackboard v6+ question import format.
  34   *
  35   * @package    qformat_blackboard_six
  36   * @copyright  2012 Jean-Michel Vedrine
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class blackboardsixformatqti_test extends \question_testcase {
  40  
  41      public function make_test_xml() {
  42          $xmlfile = new qformat_blackboard_six_file();
  43          $xmlfile->filetype = 1;
  44          $xmlfile->text = file_get_contents(__DIR__ . '/fixtures/sample_blackboard_qti.dat');
  45          return array(0 => $xmlfile);
  46      }
  47      public function test_import_match() {
  48          $xml = $this->make_test_xml();
  49  
  50          $importer = new qformat_blackboard_six();
  51          $questions = $importer->readquestions($xml);
  52          $q = $questions[4];
  53  
  54          // If qtype_ddmatch is installed, the formatter produces ddmatch
  55          // qtypes, not match ones.
  56          $ddmatchisinstalled = question_bank::is_qtype_installed('ddmatch');
  57  
  58          $expectedq = new \stdClass();
  59          $expectedq->qtype = $ddmatchisinstalled ? 'ddmatch' : 'match';
  60          $expectedq->name = 'Classify the animals.';
  61          $expectedq->questiontext = 'Classify the animals.';
  62          $expectedq->questiontextformat = FORMAT_HTML;
  63          $expectedq->correctfeedback = array('text' => '',
  64                  'format' => FORMAT_HTML, 'files' => array());
  65          $expectedq->partiallycorrectfeedback = array('text' => '',
  66                  'format' => FORMAT_HTML, 'files' => array());
  67          $expectedq->incorrectfeedback = array('text' => '',
  68                  'format' => FORMAT_HTML, 'files' => array());
  69          $expectedq->generalfeedback = '';
  70          $expectedq->generalfeedbackformat = FORMAT_HTML;
  71          $expectedq->defaultmark = 1;
  72          $expectedq->length = 1;
  73          $expectedq->penalty = 0.3333333;
  74          $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
  75          $expectedq->subquestions = array(
  76              array('text' => '', 'format' => FORMAT_HTML),
  77              array('text' => 'cat', 'format' => FORMAT_HTML),
  78              array('text' => 'frog', 'format' => FORMAT_HTML),
  79              array('text' => 'newt', 'format' => FORMAT_HTML));
  80          if ($ddmatchisinstalled) {
  81              $expectedq->subanswers = array(
  82                  array('text' => 'insect', 'format' => FORMAT_HTML),
  83                  array('text' => 'mammal', 'format' => FORMAT_HTML),
  84                  array('text' => 'amphibian', 'format' => FORMAT_HTML),
  85                  array('text' => 'amphibian', 'format' => FORMAT_HTML),
  86              );
  87          } else {
  88              $expectedq->subanswers = array('insect', 'mammal', 'amphibian', 'amphibian');
  89          }
  90  
  91          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  92      }
  93  
  94      public function test_import_multichoice_single() {
  95          $xml = $this->make_test_xml();
  96  
  97          $importer = new qformat_blackboard_six();
  98          $questions = $importer->readquestions($xml);
  99          $q = $questions[2];
 100  
 101          $expectedq = new \stdClass();
 102          $expectedq->qtype = 'multichoice';
 103          $expectedq->single = 1;
 104          $expectedq->name = 'What\'s between orange and green in the spectrum?';
 105          $expectedq->questiontext = '<span style="font-size:12pt">What\'s between orange and green in the spectrum?</span>';
 106          $expectedq->questiontextformat = FORMAT_HTML;
 107          $expectedq->correctfeedback = array('text' => '',
 108                  'format' => FORMAT_HTML, 'files' => array());
 109          $expectedq->partiallycorrectfeedback = array('text' => '',
 110                  'format' => FORMAT_HTML, 'files' => array());
 111          $expectedq->incorrectfeedback = array('text' => '',
 112                  'format' => FORMAT_HTML, 'files' => array());
 113          $expectedq->generalfeedback = '';
 114          $expectedq->generalfeedbackformat = FORMAT_HTML;
 115          $expectedq->defaultmark = 1;
 116          $expectedq->length = 1;
 117          $expectedq->penalty = 0.3333333;
 118          $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
 119          $expectedq->answer = array(
 120                  0 => array(
 121                      'text' => '<span style="font-size:12pt">red</span>',
 122                      'format' => FORMAT_HTML,
 123                  ),
 124                  1 => array(
 125                      'text' => '<span style="font-size:12pt">yellow</span>',
 126                      'format' => FORMAT_HTML,
 127                  ),
 128                  2 => array(
 129                      'text' => '<span style="font-size:12pt">blue</span>',
 130                      'format' => FORMAT_HTML,
 131                  )
 132              );
 133          $expectedq->fraction = array(0, 1, 0);
 134          $expectedq->feedback = array(
 135                  0 => array(
 136                      'text' => 'Red is not between orange and green in the spectrum but yellow is.',
 137                      'format' => FORMAT_HTML,
 138                  ),
 139                  1 => array(
 140                      'text' => 'You gave the right answer.',
 141                      'format' => FORMAT_HTML,
 142                  ),
 143                  2 => array(
 144                      'text' => 'Blue is not between orange and green in the spectrum but yellow is.',
 145                      'format' => FORMAT_HTML,
 146                  )
 147              );
 148  
 149          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 150      }
 151  
 152      public function test_import_multichoice_multi() {
 153  
 154          $xml = $this->make_test_xml();
 155  
 156          $importer = new qformat_blackboard_six();
 157          $questions = $importer->readquestions($xml);
 158          $q = $questions[3];
 159  
 160          $expectedq = new \stdClass();
 161          $expectedq->qtype = 'multichoice';
 162          $expectedq->single = 0;
 163          $expectedq->name = 'What\'s between orange and green in the spectrum?';
 164          $expectedq->questiontext = '<i>What\'s between orange and green in the spectrum?</i>';
 165          $expectedq->questiontextformat = FORMAT_HTML;
 166          $expectedq->correctfeedback = array(
 167                  'text' => '',
 168                  'format' => FORMAT_HTML,
 169                  'files' => array(),
 170              );
 171          $expectedq->partiallycorrectfeedback = array(
 172                  'text' => '',
 173                  'format' => FORMAT_HTML,
 174                  'files' => array(),
 175              );
 176          $expectedq->incorrectfeedback = array(
 177                  'text' => '',
 178                  'format' => FORMAT_HTML,
 179                  'files' => array(),
 180              );
 181          $expectedq->generalfeedback = '';
 182          $expectedq->generalfeedbackformat = FORMAT_HTML;
 183          $expectedq->defaultmark = 1;
 184          $expectedq->length = 1;
 185          $expectedq->penalty = 0.3333333;
 186          $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
 187          $expectedq->answer = array(
 188                  0 => array(
 189                      'text' => '<span style="font-size:12pt">yellow</span>',
 190                      'format' => FORMAT_HTML,
 191                  ),
 192                  1 => array(
 193                      'text' => '<span style="font-size:12pt">red</span>',
 194                      'format' => FORMAT_HTML,
 195                  ),
 196                  2 => array(
 197                      'text' => '<span style="font-size:12pt">off-beige</span>',
 198                      'format' => FORMAT_HTML,
 199                  ),
 200                  3 => array(
 201                      'text' => '<span style="font-size:12pt">blue</span>',
 202                      'format' => FORMAT_HTML,
 203                  )
 204              );
 205          $expectedq->fraction = array(0.5, 0, 0.5, 0);
 206          $expectedq->feedback = array(
 207                  0 => array(
 208                      'text' => '',
 209                      'format' => FORMAT_HTML,
 210                  ),
 211                  1 => array(
 212                      'text' => '',
 213                      'format' => FORMAT_HTML,
 214                  ),
 215                  2 => array(
 216                      'text' => '',
 217                      'format' => FORMAT_HTML,
 218                  ),
 219                  3 => array(
 220                      'text' => '',
 221                      'format' => FORMAT_HTML,
 222                  )
 223              );
 224  
 225          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 226      }
 227  
 228      public function test_import_truefalse() {
 229  
 230          $xml = $this->make_test_xml();
 231  
 232          $importer = new qformat_blackboard_six();
 233          $questions = $importer->readquestions($xml);
 234          $q = $questions[1];
 235  
 236          $expectedq = new \stdClass();
 237          $expectedq->qtype = 'truefalse';
 238          $expectedq->name = '42 is the Absolute Answer to everything.';
 239          $expectedq->questiontext = '<span style="font-size:12pt">42 is the Absolute Answer to everything.</span>';
 240          $expectedq->questiontextformat = FORMAT_HTML;
 241          $expectedq->generalfeedback = '';
 242          $expectedq->generalfeedbackformat = FORMAT_HTML;
 243          $expectedq->defaultmark = 1;
 244          $expectedq->length = 1;
 245          $expectedq->correctanswer = 0;
 246          $expectedq->feedbacktrue = array(
 247                  'text' => '42 is the <b>Ultimate</b> Answer.',
 248                  'format' => FORMAT_HTML,
 249              );
 250          $expectedq->feedbackfalse = array(
 251                  'text' => 'You gave the right answer.',
 252                  'format' => FORMAT_HTML,
 253              );
 254  
 255          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 256      }
 257  
 258      public function test_import_fill_in_the_blank() {
 259  
 260          $xml = $this->make_test_xml();
 261  
 262          $importer = new qformat_blackboard_six();
 263          $questions = $importer->readquestions($xml);
 264          $q = $questions[5];
 265  
 266          $expectedq = new \stdClass();
 267          $expectedq->qtype = 'shortanswer';
 268          $expectedq->name = 'Name an amphibian: __________.';
 269          $expectedq->questiontext = '<span style="font-size:12pt">Name an amphibian: __________.</span>';
 270          $expectedq->questiontextformat = FORMAT_HTML;
 271          $expectedq->generalfeedback = '';
 272          $expectedq->generalfeedbackformat = FORMAT_HTML;
 273          $expectedq->defaultmark = 1;
 274          $expectedq->length = 1;
 275          $expectedq->usecase = 0;
 276          $expectedq->answer = array('frog', '*');
 277          $expectedq->fraction = array(1, 0);
 278          $expectedq->feedback = array(
 279                  0 => array(
 280                      'text' => 'A frog is an amphibian.',
 281                      'format' => FORMAT_HTML,
 282                  ),
 283                  1 => array(
 284                      'text' => 'A frog is an amphibian.',
 285                      'format' => FORMAT_HTML,
 286                  )
 287              );
 288  
 289          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 290      }
 291  
 292      public function test_import_essay() {
 293  
 294          $xml = $this->make_test_xml();
 295  
 296          $importer = new qformat_blackboard_six();
 297          $questions = $importer->readquestions($xml);
 298          $q = $questions[6];
 299  
 300          $expectedq = new \stdClass();
 301          $expectedq->qtype = 'essay';
 302          $expectedq->name = 'How are you?';
 303          $expectedq->questiontext = 'How are you?';
 304          $expectedq->questiontextformat = FORMAT_HTML;
 305          $expectedq->generalfeedback = '';
 306          $expectedq->generalfeedbackformat = FORMAT_HTML;
 307          $expectedq->defaultmark = 1;
 308          $expectedq->length = 1;
 309          $expectedq->responseformat = 'editor';
 310          $expectedq->responsefieldlines = 15;
 311          $expectedq->attachments = 0;
 312          $expectedq->graderinfo = array(
 313                  'text' => 'Blackboard answer for essay questions will be imported as informations for graders.',
 314                  'format' => FORMAT_HTML,
 315              );
 316  
 317          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 318      }
 319  
 320      public function test_import_category() {
 321  
 322          $xml = $this->make_test_xml();
 323  
 324          $importer = new qformat_blackboard_six();
 325          $questions = $importer->readquestions($xml);
 326          $q = $questions[0];
 327  
 328          $expectedq = new \stdClass();
 329          $expectedq->qtype = 'category';
 330          $expectedq->category = 'sample_blackboard_six';
 331  
 332          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 333      }
 334  }