[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/essay/tests/ -> helper.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1