Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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 markers question type.
  19   *
  20   * @package   qtype_ddmarker
  21   * @copyright 2012 The Open University
  22   * @author    Jamie Pratt <me@jamiep.org>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  /**
  31   * Test helper class for the drag-and-drop markers question type.
  32   *
  33   * @copyright  2010 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class qtype_ddmarker_test_helper extends question_test_helper {
  37      public function get_test_questions() {
  38          return array('fox', 'maths', 'mkmap', 'zerodrag');
  39      }
  40  
  41      /**
  42       * @return qtype_ddmarker_question
  43       */
  44      public function make_ddmarker_question_fox() {
  45          question_bank::load_question_definition_classes('ddmarker');
  46          $dd = new qtype_ddmarker_question();
  47  
  48          test_question_maker::initialise_a_question($dd);
  49  
  50          $dd->name = 'Drag-and-drop markers question';
  51          $dd->questiontext = 'The quick brown fox jumped over the lazy dog.';
  52          $dd->generalfeedback = 'This sentence uses each letter of the alphabet.';
  53          $dd->qtype = question_bank::get_qtype('ddmarker');
  54  
  55          $dd->shufflechoices = true;
  56  
  57          test_question_maker::set_standard_combined_feedback_fields($dd);
  58  
  59          $dd->choices = $this->make_choice_structure(array(
  60                      new qtype_ddmarker_drag_item('quick', 1, 0, 1),
  61                      new qtype_ddmarker_drag_item('fox', 2, 0, 1),
  62                      new qtype_ddmarker_drag_item('lazy', 3, 0, 1)
  63  
  64          ));
  65  
  66          $dd->places = $this->make_place_structure(array(
  67                              new qtype_ddmarker_drop_zone(1, 'circle', '50,50;50'),
  68                              new qtype_ddmarker_drop_zone(2, 'rectangle', '100,0;100,100'),
  69                              new qtype_ddmarker_drop_zone(3, 'polygon', '0,100;200,100;200,200;0,200')
  70          ));
  71          $dd->rightchoices = array(1 => 1, 2 => 2, 3 => 3);
  72  
  73          return $dd;
  74      }
  75  
  76      protected function make_choice_structure($choices) {
  77          $choicestructure = array();
  78          foreach ($choices as $choice) {
  79              $group = $choice->choice_group();
  80              if (!isset($choicestructure[$group])) {
  81                  $choicestructure[$group] = array();
  82              }
  83              $choicestructure[$group][$choice->no] = $choice;
  84          }
  85          return $choicestructure;
  86      }
  87  
  88      protected function make_place_structure($places) {
  89          $placestructure = array();
  90          foreach ($places as $place) {
  91              $placestructure[$place->no] = $place;
  92          }
  93          return $placestructure;
  94      }
  95  
  96      /**
  97       * @return qtype_ddmarker_question
  98       */
  99      public function make_ddmarker_question_maths() {
 100          question_bank::load_question_definition_classes('ddmarker');
 101          $dd = new qtype_ddmarker_question();
 102  
 103          test_question_maker::initialise_a_question($dd);
 104  
 105          $dd->name = 'Drag-and-drop markers question';
 106          $dd->questiontext = 'Fill in the operators to make this equation work: ';
 107          $dd->generalfeedback = 'Hmmmm...';
 108          $dd->qtype = question_bank::get_qtype('ddmarker');
 109  
 110          $dd->shufflechoices = true;
 111  
 112          test_question_maker::set_standard_combined_feedback_fields($dd);
 113  
 114          $dd->choices = $this->make_choice_structure(array(
 115                      new qtype_ddmarker_drag_item('+', 1, 1, 0),
 116                      new qtype_ddmarker_drag_item('-', 2, 1, 0),
 117                      new qtype_ddmarker_drag_item('*', 3, 1, 0),
 118                      new qtype_ddmarker_drag_item('/', 4, 1, 0)
 119  
 120          ));
 121  
 122          $dd->places = $this->make_place_structure(array(
 123                      new qtype_ddmarker_drop_zone(1, 'circle', '50,50;50'),
 124                      new qtype_ddmarker_drop_zone(2, 'rectangle', '100,0;100,100'),
 125                      new qtype_ddmarker_drop_zone(3, 'polygon', '0,100;100,100;100,200;0,200')
 126          ));
 127          $dd->rightchoices = array(1 => 1, 2 => 1, 3 => 1);
 128  
 129          return $dd;
 130      }
 131  
 132      /**
 133       * @return stdClass date to create a ddmarkers question.
 134       */
 135      public function get_ddmarker_question_form_data_mkmap() {
 136          global $CFG, $USER;
 137          $fromform = new stdClass();
 138  
 139          $bgdraftitemid = 0;
 140          file_prepare_draft_area($bgdraftitemid, null, null, null, null);
 141          $fs = get_file_storage();
 142          $filerecord = new stdClass();
 143          $filerecord->contextid = context_user::instance($USER->id)->id;
 144          $filerecord->component = 'user';
 145          $filerecord->filearea = 'draft';
 146          $filerecord->itemid = $bgdraftitemid;
 147          $filerecord->filepath = '/';
 148          $filerecord->filename = 'mkmap.png';
 149          $fs->create_file_from_pathname($filerecord, $CFG->dirroot .
 150                  '/question/type/ddmarker/tests/fixtures/mkmap.png');
 151  
 152          $fromform->name = 'Milton Keynes landmarks';
 153          $fromform->questiontext = array(
 154              'text' => 'Please place the markers on the map of Milton Keynes and be aware that '.
 155                        'there is more than one railway station.',
 156              'format' => FORMAT_HTML,
 157          );
 158          $fromform->defaultmark = 1;
 159          $fromform->generalfeedback = array(
 160              'text' => 'The Open University is at the junction of Brickhill Street and Groveway. '.
 161                        'There are three railway stations, Wolverton, Milton Keynes Central and Bletchley.',
 162              'format' => FORMAT_HTML,
 163          );
 164          $fromform->bgimage = $bgdraftitemid;
 165          $fromform->shuffleanswers = 0;
 166  
 167          $fromform->drags = array(
 168              array('label' => 'OU', 'noofdrags' => 1),
 169              array('label' => 'Railway station', 'noofdrags' => 3),
 170          );
 171  
 172          $fromform->drops = array(
 173              array('shape' => 'circle', 'coords' => '322,213;10', 'choice' => 1),
 174              array('shape' => 'circle', 'coords' => '144,84;10', 'choice' => 2),
 175              array('shape' => 'circle', 'coords' => '195,180;10', 'choice' => 2),
 176              array('shape' => 'circle', 'coords' => '267,302;10', 'choice' => 2),
 177          );
 178  
 179          test_question_maker::set_standard_combined_feedback_form_data($fromform);
 180  
 181          $fromform->penalty = '0.3333333';
 182          $fromform->hint = array(
 183              array(
 184                  'text' => 'You are trying to place four markers on the map.',
 185                  'format' => FORMAT_HTML,
 186              ),
 187              array(
 188                  'text' => 'You are trying to mark three railway stations.',
 189                  'format' => FORMAT_HTML,
 190              ),
 191          );
 192          $fromform->hintshownumcorrect = array(1, 1);
 193          $fromform->hintclearwrong = array(0, 1);
 194          $fromform->hintoptions = array(0, 1);
 195  
 196          return $fromform;
 197      }
 198  
 199      /**
 200       * Return the test data needed by the question generator (the data that
 201       * would come from saving the editing form).
 202       * @return stdClass date to create a ddmarkers question where one of the drag items has text '0'.
 203       */
 204      public function get_ddmarker_question_form_data_zerodrag() {
 205          global $CFG, $USER;
 206          $fromform = new stdClass();
 207  
 208          $bgdraftitemid = 0;
 209          file_prepare_draft_area($bgdraftitemid, null, null, null, null);
 210          $fs = get_file_storage();
 211          $filerecord = new stdClass();
 212          $filerecord->contextid = context_user::instance($USER->id)->id;
 213          $filerecord->component = 'user';
 214          $filerecord->filearea = 'draft';
 215          $filerecord->itemid = $bgdraftitemid;
 216          $filerecord->filepath = '/';
 217          $filerecord->filename = 'mkmap.png';
 218          $fs->create_file_from_pathname($filerecord, $CFG->dirroot .
 219                  '/question/type/ddmarker/tests/fixtures/mkmap.png');
 220  
 221          $fromform->name = 'Drag digits';
 222          $fromform->questiontext = array(
 223              'text' => 'Put 0 in the left of the image, and 1 in the right.',
 224              'format' => FORMAT_HTML,
 225          );
 226          $fromform->defaultmark = 2;
 227          $fromform->generalfeedback = array(
 228              'text' => '',
 229              'format' => FORMAT_HTML,
 230          );
 231          $fromform->bgimage = $bgdraftitemid;
 232          $fromform->shuffleanswers = 0;
 233  
 234          $fromform->drags = array(
 235              array('label' => '0', 'noofdrags' => 1),
 236              array('label' => '1', 'noofdrags' => 1),
 237          );
 238  
 239          $fromform->drops = array(
 240              array('shape' => 'Rectangle', 'coords' => '0,0;272,389', 'choice' => 1),
 241              array('shape' => 'Rectangle', 'coords' => '272,0;272,389', 'choice' => 2),
 242          );
 243  
 244          test_question_maker::set_standard_combined_feedback_form_data($fromform);
 245  
 246          $fromform->penalty = '0.3333333';
 247          $fromform->hint = array(
 248              array(
 249                  'text' => 'Hint 1.',
 250                  'format' => FORMAT_HTML,
 251              ),
 252              array(
 253                  'text' => 'Hint 2.',
 254                  'format' => FORMAT_HTML,
 255              ),
 256          );
 257          $fromform->hintshownumcorrect = array(1, 1);
 258          $fromform->hintclearwrong = array(0, 1);
 259          $fromform->hintoptions = array(0, 1);
 260  
 261          return $fromform;
 262      }
 263  }