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 311 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  /**
  18   * Test helpers for the drag-and-drop onto image question type.
  19   *
  20   * @package    qtype_ddimageortext
  21   * @copyright  2010 The Open University
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  
  29  /**
  30   * Test helper class for the drag-and-drop onto image question type.
  31   *
  32   * @copyright  2010 The Open University
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class qtype_ddimageortext_test_helper extends question_test_helper {
  36      public function get_test_questions() {
  37          return array('fox', 'maths', 'xsection', 'mixedlang');
  38      }
  39  
  40      /**
  41       * @return qtype_ddimageortext_question
  42       */
  43      public function make_ddimageortext_question_fox() {
  44          question_bank::load_question_definition_classes('ddimageortext');
  45          $dd = new qtype_ddimageortext_question();
  46  
  47          test_question_maker::initialise_a_question($dd);
  48  
  49          $dd->name = 'Drag-and-drop onto image question';
  50          $dd->questiontext = 'The quick brown fox jumped over the lazy dog.';
  51          $dd->generalfeedback = 'This sentence uses each letter of the alphabet.';
  52          $dd->qtype = question_bank::get_qtype('ddimageortext');
  53  
  54          $dd->shufflechoices = true;
  55  
  56          test_question_maker::set_standard_combined_feedback_fields($dd);
  57  
  58          $dd->choices = $this->make_choice_structure(array(
  59                      new qtype_ddimageortext_drag_item('quick', 1, 1),
  60                      new qtype_ddimageortext_drag_item('fox', 2, 1),
  61                      new qtype_ddimageortext_drag_item('lazy', 3, 2),
  62                      new qtype_ddimageortext_drag_item('dog', 4, 2)
  63  
  64          ));
  65  
  66          $dd->places = $this->make_place_structure(array(
  67                              new qtype_ddimageortext_drop_zone('', 1, 1),
  68                              new qtype_ddimageortext_drop_zone('', 2, 1),
  69                              new qtype_ddimageortext_drop_zone('', 3, 2),
  70                              new qtype_ddimageortext_drop_zone('', 4, 2)
  71          ));
  72          $dd->rightchoices = array(1 => 1, 2 => 2, 3 => 1, 4 => 4);
  73  
  74          return $dd;
  75      }
  76  
  77      protected function make_choice_structure($choices) {
  78          $choicestructure = array();
  79          foreach ($choices as $choice) {
  80              if (!isset($choicestructure[$choice->group])) {
  81                  $choicestructure[$choice->group][1] = $choice;
  82              } else {
  83                  $choicestructure[$choice->group][$choice->no] = $choice;
  84              }
  85          }
  86          return $choicestructure;
  87      }
  88  
  89      protected function make_place_structure($places) {
  90          $placestructure = array();
  91          foreach ($places as $place) {
  92              $placestructure[$place->no] = $place;
  93          }
  94          return $placestructure;
  95      }
  96  
  97      /**
  98       * Make a mathematical ddimageortext question.
  99       *
 100       * @return qtype_ddimageortext_question
 101       */
 102      public function make_ddimageortext_question_maths() {
 103          question_bank::load_question_definition_classes('ddimageortext');
 104          $dd = new qtype_ddimageortext_question();
 105  
 106          test_question_maker::initialise_a_question($dd);
 107  
 108          $dd->name = 'Drag-and-drop onto image question';
 109          $dd->questiontext = 'Fill in the operators to make this equation work: ' .
 110                  '7 [[1]] 11 [[2]] 13 [[1]] 17 [[2]] 19 = 3';
 111          $dd->generalfeedback = 'This sentence uses each letter of the alphabet.';
 112          $dd->qtype = question_bank::get_qtype('ddimageortext');
 113  
 114          $dd->shufflechoices = true;
 115  
 116          test_question_maker::set_standard_combined_feedback_fields($dd);
 117  
 118          $dd->choices = $this->make_choice_structure(array(
 119                  new qtype_ddimageortext_drag_item('+', 1, 1),
 120                  new qtype_ddimageortext_drag_item('-', 2, 1)
 121          ));
 122  
 123          $dd->places = $this->make_place_structure(array(
 124                              new qtype_ddimageortext_drop_zone('', 1, 1),
 125                              new qtype_ddimageortext_drop_zone('', 2, 1),
 126                              new qtype_ddimageortext_drop_zone('', 3, 1),
 127                              new qtype_ddimageortext_drop_zone('', 4, 1)
 128          ));
 129          $dd->rightchoices = array(1 => 1, 2 => 2, 3 => 1, 4 => 2);
 130  
 131          return $dd;
 132      }
 133  
 134      /**
 135       * @return stdClass date to create a ddimageortext question.
 136       */
 137      public function get_ddimageortext_question_form_data_xsection() {
 138          global $CFG, $USER;
 139          $fromform = new stdClass();
 140  
 141          $bgdraftitemid = 0;
 142          file_prepare_draft_area($bgdraftitemid, null, null, null, null);
 143          $fs = get_file_storage();
 144          $filerecord = new stdClass();
 145          $filerecord->contextid = context_user::instance($USER->id)->id;
 146          $filerecord->component = 'user';
 147          $filerecord->filearea = 'draft';
 148          $filerecord->itemid = $bgdraftitemid;
 149          $filerecord->filepath = '/';
 150          $filerecord->filename = 'oceanfloorbase.jpg';
 151          $fs->create_file_from_pathname($filerecord, $CFG->dirroot .
 152                  '/question/type/ddimageortext/tests/fixtures/oceanfloorbase.jpg');
 153  
 154          $fromform->name = 'Geography cross-section';
 155          $fromform->questiontext = array(
 156              'text' => '<p>Identify the features in this cross-section by dragging the labels into the boxes.</p>
 157                         <p><em>Use the mouse to drag the boxed words into the empty boxes. '.
 158                        'Alternatively, use the tab key to select an empty box, '.
 159                        'then use the space key to cycle through the options.</em></p>',
 160              'format' => FORMAT_HTML,
 161          );
 162          $fromform->defaultmark = 1;
 163          $fromform->generalfeedback = array(
 164              'text' => '<p>More information about the major features of the Earth\'s surface '.
 165                        'can be found in Block 3, Section 6.2.</p>',
 166              'format' => FORMAT_HTML,
 167          );
 168          $fromform->bgimage = $bgdraftitemid;
 169          $fromform->shuffleanswers = 0;
 170          $fromform->drags = array(
 171              array('dragitemtype' => 'word', 'draggroup' => '1', 'infinite' => '0'),
 172              array('dragitemtype' => 'word', 'draggroup' => '1', 'infinite' => '0'),
 173              array('dragitemtype' => 'word', 'draggroup' => '1', 'infinite' => '0'),
 174              array('dragitemtype' => 'word', 'draggroup' => '1', 'infinite' => '0'),
 175              array('dragitemtype' => 'word', 'draggroup' => '1', 'infinite' => '0'),
 176              array('dragitemtype' => 'word', 'draggroup' => '1', 'infinite' => '0'),
 177              array('dragitemtype' => 'word', 'draggroup' => '1', 'infinite' => '0'),
 178              array('dragitemtype' => 'word', 'draggroup' => '1', 'infinite' => '0'),
 179          );
 180          $fromform->dragitem = array(0, 0, 0, 0, 0, 0, 0, 0);
 181          $fromform->draglabel =
 182          array(
 183              'island<br/>arc',
 184              'mid-ocean<br/>ridge',
 185              'abyssal<br/>plain',
 186              'continental<br/>rise',
 187              'ocean<br/>trench',
 188              'continental<br/>slope',
 189              'mountain<br/>belt',
 190              'continental<br/>shelf',
 191          );
 192          $fromform->drops = array(
 193              array('xleft' => '53', 'ytop' => '17', 'choice' => '7', 'droplabel' => ''),
 194              array('xleft' => '172', 'ytop' => '2', 'choice' => '8', 'droplabel' => ''),
 195              array('xleft' => '363', 'ytop' => '31', 'choice' => '5', 'droplabel' => ''),
 196              array('xleft' => '440', 'ytop' => '13', 'choice' => '3', 'droplabel' => ''),
 197              array('xleft' => '115', 'ytop' => '74', 'choice' => '6', 'droplabel' => ''),
 198              array('xleft' => '210', 'ytop' => '94', 'choice' => '4', 'droplabel' => ''),
 199              array('xleft' => '310', 'ytop' => '87', 'choice' => '1', 'droplabel' => ''),
 200              array('xleft' => '479', 'ytop' => '84', 'choice' => '2', 'droplabel' => ''),
 201          );
 202  
 203          test_question_maker::set_standard_combined_feedback_form_data($fromform);
 204  
 205          $fromform->penalty = '0.3333333';
 206          $fromform->hint = array(
 207              array(
 208                  'text' => '<p>Incorrect placements will be removed.</p>',
 209                  'format' => FORMAT_HTML,
 210              ),
 211              array(
 212                  'text' => '<ul>
 213                             <li>The abyssal plain is a flat almost featureless expanse of ocean '.
 214                             'floor 4km to 6km below sea-level.</li>
 215                             <li>The continental rise is the gently sloping part of the ocean floor beyond the continental slope.</li>
 216                             <li>The continental shelf is the gently sloping ocean floor just offshore from the land.</li>
 217                             <li>The continental slope is the relatively steep part of the ocean floor '.
 218                             'beyond the continental shelf.</li>
 219                             <li>A mid-ocean ridge is a broad submarine ridge several kilometres high.</li>
 220                             <li>A mountain belt is a long range of mountains.</li>
 221                             <li>An island arc is a chain of volcanic islands.</li>
 222                             <li>An oceanic trench is a deep trough in the ocean floor.</li>
 223                             </ul>',
 224                  'format' => FORMAT_HTML,
 225              ),
 226              array(
 227                  'text' => '<p>Incorrect placements will be removed.</p>',
 228                  'format' => FORMAT_HTML,
 229              ),
 230              array(
 231                  'text' => '<ul>
 232                             <li>The abyssal plain is a flat almost featureless expanse of ocean '.
 233                             'floor 4km to 6km below sea-level.</li>
 234                             <li>The continental rise is the gently sloping part of the ocean floor beyond the continental slope.</li>
 235                             <li>The continental shelf is the gently sloping ocean floor just offshore from the land.</li>
 236                             <li>The continental slope is the relatively steep part of the ocean floor '.
 237                             'beyond the continental shelf.</li>
 238                             <li>A mid-ocean ridge is a broad submarine ridge several kilometres high.</li>
 239                             <li>A mountain belt is a long range of mountains.</li>
 240                             <li>An island arc is a chain of volcanic islands.</li>
 241                             <li>An oceanic trench is a deep trough in the ocean floor.</li>
 242                             </ul>',
 243                  'format' => FORMAT_HTML,
 244              ),
 245          );
 246          $fromform->hintclearwrong = array(1, 0, 1, 0);
 247          $fromform->hintshownumcorrect = array(1, 1, 1, 1);
 248  
 249          $fromform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
 250  
 251          return $fromform;
 252      }
 253  
 254      /**
 255       * Make a test question where the drag items are a different language than the main question text.
 256       *
 257       * @return qtype_ddimageortext_question
 258       */
 259      public function make_ddimageortext_question_mixedlang() {
 260          question_bank::load_question_definition_classes('ddimageortext');
 261          $dd = new qtype_ddimageortext_question();
 262  
 263          test_question_maker::initialise_a_question($dd);
 264  
 265          $dd->name = 'Question about French in English.';
 266          $dd->questiontext = '<p>Complete the blanks in this sentence.</p>' .
 267                  '<p lang="fr">J\'ai perdu [[1]] plume de [[2]] tante - l\'avez-vous vue?</p>';
 268          $dd->generalfeedback = 'This sentence uses each letter of the alphabet.';
 269          $dd->qtype = question_bank::get_qtype('ddimageortext');
 270  
 271          $dd->shufflechoices = true;
 272  
 273          test_question_maker::set_standard_combined_feedback_fields($dd);
 274  
 275          $dd->choices = $this->make_choice_structure(array(
 276                  new qtype_ddimageortext_drag_item('<span lang="fr">la</span>', 1, 1),
 277                  new qtype_ddimageortext_drag_item('<span lang="fr">ma</span>', 2, 1),
 278          ));
 279  
 280          $dd->places = $this->make_place_structure(array(
 281                  new qtype_ddimageortext_drop_zone('', 1, 1),
 282                  new qtype_ddimageortext_drop_zone('', 2, 1)
 283          ));
 284          $dd->rightchoices = array(1 => 1, 2 => 2);
 285  
 286          return $dd;
 287      }
 288  }