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 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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