Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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 essay question type.
  19   *
  20   * @package    qtype_essay
  21   * @copyright  2013 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 essay question type.
  31   *
  32   * @copyright  2013 The Open University
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class qtype_essay_test_helper extends question_test_helper {
  36      public function get_test_questions() {
  37          return array('editor', 'editorfilepicker', 'plain', 'monospaced', 'responsetemplate', 'noinline');
  38      }
  39  
  40      /**
  41       * Helper method to reduce duplication.
  42       * @return qtype_essay_question
  43       */
  44      protected function initialise_essay_question() {
  45          question_bank::load_question_definition_classes('essay');
  46          $q = new qtype_essay_question();
  47          test_question_maker::initialise_a_question($q);
  48          $q->name = 'Essay question (HTML editor)';
  49          $q->questiontext = 'Please write a story about a frog.';
  50          $q->generalfeedback = 'I hope your story had a beginning, a middle and an end.';
  51          $q->responseformat = 'editor';
  52          $q->responserequired = 1;
  53          $q->responsefieldlines = 10;
  54          $q->attachments = 0;
  55          $q->attachmentsrequired = 0;
  56          $q->maxbytes = 0;
  57          $q->filetypeslist = null;
  58          $q->graderinfo = '';
  59          $q->graderinfoformat = FORMAT_HTML;
  60          $q->qtype = question_bank::get_qtype('essay');
  61  
  62          return $q;
  63      }
  64  
  65      /**
  66       * Makes an essay question using the HTML editor as input.
  67       * @return qtype_essay_question
  68       */
  69      public function make_essay_question_editor() {
  70          return $this->initialise_essay_question();
  71      }
  72  
  73      /**
  74       * Make the data what would be received from the editing form for an essay
  75       * question using the HTML editor allowing embedded files as input, and up
  76       * to three attachments.
  77       *
  78       * @return stdClass the data that would be returned by $form->get_gata();
  79       */
  80      public function get_essay_question_form_data_editor() {
  81          $fromform = new stdClass();
  82  
  83          $fromform->name = 'Essay question (HTML editor)';
  84          $fromform->questiontext = array('text' => 'Please write a story about a frog.', 'format' => FORMAT_HTML);
  85          $fromform->defaultmark = 1.0;
  86          $fromform->generalfeedback = array('text' => 'I hope your story had a beginning, a middle and an end.', 'format' => FORMAT_HTML);
  87          $fromform->responseformat = 'editor';
  88          $fromform->responserequired = 1;
  89          $fromform->responsefieldlines = 10;
  90          $fromform->attachments = 0;
  91          $fromform->attachmentsrequired = 0;
  92          $fromform->maxbytes = 0;
  93          $fromform->filetypeslist = '';
  94          $fromform->graderinfo = array('text' => '', 'format' => FORMAT_HTML);
  95          $fromform->responsetemplate = array('text' => '', 'format' => FORMAT_HTML);
  96  
  97          return $fromform;
  98      }
  99  
 100      /**
 101       * Makes an essay question using the HTML editor allowing embedded files as
 102       * input, and up to three attachments.
 103       * @return qtype_essay_question
 104       */
 105      public function make_essay_question_editorfilepicker() {
 106          $q = $this->initialise_essay_question();
 107          $q->responseformat = 'editorfilepicker';
 108          $q->attachments = 3;
 109          return $q;
 110      }
 111  
 112      /**
 113       * Makes an essay question using the HTML editor allowing embedded files as
 114       * input, and up to two attachments, two needed.
 115       * @return qtype_essay_question
 116       */
 117      public function make_essay_question_editorfilepickertworequired() {
 118          $q = $this->initialise_essay_question();
 119          $q->responseformat = 'editorfilepicker';
 120          $q->attachments = 2;
 121          $q->attachmentsrequired = 2;
 122          return $q;
 123      }
 124  
 125      /**
 126       * Make the data what would be received from the editing form for an essay
 127       * question using the HTML editor allowing embedded files as input, and up
 128       * to three attachments.
 129       *
 130       * @return stdClass the data that would be returned by $form->get_gata();
 131       */
 132      public function get_essay_question_form_data_editorfilepicker() {
 133          $fromform = new stdClass();
 134  
 135          $fromform->name = 'Essay question with filepicker and attachments';
 136          $fromform->questiontext = array('text' => 'Please write a story about a frog.', 'format' => FORMAT_HTML);
 137          $fromform->defaultmark = 1.0;
 138          $fromform->generalfeedback = array('text' => 'I hope your story had a beginning, a middle and an end.', 'format' => FORMAT_HTML);
 139          $fromform->responseformat = 'editorfilepicker';
 140          $fromform->responserequired = 1;
 141          $fromform->responsefieldlines = 10;
 142          $fromform->attachments = 3;
 143          $fromform->attachmentsrequired = 0;
 144          $fromform->maxbytes = 0;
 145          $fromform->filetypeslist = '';
 146          $fromform->graderinfo = array('text' => '', 'format' => FORMAT_HTML);
 147          $fromform->responsetemplate = array('text' => '', 'format' => FORMAT_HTML);
 148  
 149          return $fromform;
 150      }
 151  
 152      /**
 153       * Makes an essay question using plain text input.
 154       * @return qtype_essay_question
 155       */
 156      public function make_essay_question_plain() {
 157          $q = $this->initialise_essay_question();
 158          $q->responseformat = 'plain';
 159          return $q;
 160      }
 161  
 162      /**
 163       * Make the data what would be received from the editing form for an essay
 164       * question using the HTML editor allowing embedded files as input, and up
 165       * to three attachments.
 166       *
 167       * @return stdClass the data that would be returned by $form->get_gata();
 168       */
 169      public function get_essay_question_form_data_plain() {
 170          $fromform = new stdClass();
 171  
 172          $fromform->name = 'Essay question with filepicker and attachments';
 173          $fromform->questiontext = array('text' => 'Please write a story about a frog.', 'format' => FORMAT_HTML);
 174          $fromform->defaultmark = 1.0;
 175          $fromform->generalfeedback = array('text' => 'I hope your story had a beginning, a middle and an end.', 'format' => FORMAT_HTML);
 176          $fromform->responseformat = 'plain';
 177          $fromform->responserequired = 1;
 178          $fromform->responsefieldlines = 10;
 179          $fromform->attachments = 0;
 180          $fromform->attachmentsrequired = 0;
 181          $fromform->maxbytes = 0;
 182          $fromform->filetypeslist = '';
 183          $fromform->graderinfo = array('text' => '', 'format' => FORMAT_HTML);
 184          $fromform->responsetemplate = array('text' => '', 'format' => FORMAT_HTML);
 185  
 186          return $fromform;
 187      }
 188  
 189      /**
 190       * Makes an essay question using monospaced input.
 191       * @return qtype_essay_question
 192       */
 193      public function make_essay_question_monospaced() {
 194          $q = $this->initialise_essay_question();
 195          $q->responseformat = 'monospaced';
 196          return $q;
 197      }
 198  
 199      public function make_essay_question_responsetemplate() {
 200          $q = $this->initialise_essay_question();
 201          $q->responsetemplate = 'Once upon a time';
 202          $q->responsetemplateformat = FORMAT_HTML;
 203          return $q;
 204      }
 205  
 206      /**
 207       * Makes an essay question without an online text editor.
 208       * @return qtype_essay_question
 209       */
 210      public function make_essay_question_noinline() {
 211          $q = $this->initialise_essay_question();
 212          $q->responseformat = 'noinline';
 213          $q->attachments = 3;
 214          $q->attachmentsrequired = 1;
 215          $q->filetypeslist = '';
 216          $q->maxbytes = 0;
 217          return $q;
 218      }
 219  
 220      /**
 221       * Creates an empty draft area for attachments.
 222       * @return int The draft area's itemid.
 223       */
 224      protected function make_attachment_draft_area() {
 225          $draftid = 0;
 226          $contextid = 0;
 227  
 228          $component = 'question';
 229          $filearea = 'response_attachments';
 230  
 231          // Create an empty file area.
 232          file_prepare_draft_area($draftid, $contextid, $component, $filearea, null);
 233          return $draftid;
 234      }
 235  
 236      /**
 237       * Creates an attachment in the provided attachment draft area.
 238       * @param int $draftid The itemid for the draft area in which the file should be created.
 239       * @param string $name The filename for the file to be created.
 240       * @param string $contents The contents of the file to be created.
 241       */
 242      protected function make_attachment($draftid, $name, $contents) {
 243          global $USER;
 244  
 245          $fs = get_file_storage();
 246          $usercontext = context_user::instance($USER->id);
 247  
 248          // Create the file in the provided draft area.
 249          $fileinfo = array(
 250              'contextid' => $usercontext->id,
 251              'component' => 'user',
 252              'filearea'  => 'draft',
 253              'itemid'    => $draftid,
 254              'filepath'  => '/',
 255              'filename'  => $name,
 256          );
 257          $fs->create_file_from_string($fileinfo, $contents);
 258      }
 259  
 260      /**
 261       * Generates a draft file area that contains the provided number of attachments. You should ensure
 262       * that a user is logged in with setUser before you run this function.
 263       *
 264       * @param int $attachments The number of attachments to generate.
 265       * @return int The itemid of the generated draft file area.
 266       */
 267      public function make_attachments($attachments) {
 268          $draftid = $this->make_attachment_draft_area();
 269  
 270          // Create the relevant amount of dummy attachments in the given draft area.
 271          for ($i = 0; $i < $attachments; ++$i) {
 272              $this->make_attachment($draftid, $i, $i);
 273          }
 274  
 275          return $draftid;
 276      }
 277  
 278      /**
 279       * Generates a question_file_saver that contains the provided number of attachments. You should ensure
 280       * that a user is logged in with setUser before you run this function.
 281       *
 282       * @param int $:attachments The number of attachments to generate.
 283       * @return question_file_saver a question_file_saver that contains the given amount of dummy files, for use in testing.
 284       */
 285      public function make_attachments_saver($attachments) {
 286          return new question_file_saver($this->make_attachments($attachments), 'question', 'response_attachments');
 287      }
 288  
 289  
 290  }