Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

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