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   * Question type class for the essay question type.
  19   *
  20   * @package    qtype
  21   * @subpackage essay
  22   * @copyright  2005 Mark Nielsen
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->libdir . '/questionlib.php');
  30  
  31  
  32  /**
  33   * The essay question type.
  34   *
  35   * @copyright  2005 Mark Nielsen
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class qtype_essay extends question_type {
  39      public function is_manual_graded() {
  40          return true;
  41      }
  42  
  43      public function response_file_areas() {
  44          return array('attachments', 'answer');
  45      }
  46  
  47      public function get_question_options($question) {
  48          global $DB;
  49          $question->options = $DB->get_record('qtype_essay_options',
  50                  array('questionid' => $question->id), '*', MUST_EXIST);
  51          parent::get_question_options($question);
  52      }
  53  
  54      public function save_question_options($formdata) {
  55          global $DB;
  56          $context = $formdata->context;
  57  
  58          $options = $DB->get_record('qtype_essay_options', array('questionid' => $formdata->id));
  59          if (!$options) {
  60              $options = new stdClass();
  61              $options->questionid = $formdata->id;
  62              $options->id = $DB->insert_record('qtype_essay_options', $options);
  63          }
  64  
  65          $options->responseformat = $formdata->responseformat;
  66          $options->responserequired = $formdata->responserequired;
  67          $options->responsefieldlines = $formdata->responsefieldlines;
  68          $options->attachments = $formdata->attachments;
  69          $options->attachmentsrequired = $formdata->attachmentsrequired;
  70          if (!isset($formdata->filetypeslist)) {
  71              $options->filetypeslist = null;
  72          } else {
  73              $options->filetypeslist = $formdata->filetypeslist;
  74          }
  75          $options->maxbytes = $formdata->maxbytes ?? 0;
  76          $options->graderinfo = $this->import_or_save_files($formdata->graderinfo,
  77                  $context, 'qtype_essay', 'graderinfo', $formdata->id);
  78          $options->graderinfoformat = $formdata->graderinfo['format'];
  79          $options->responsetemplate = $formdata->responsetemplate['text'];
  80          $options->responsetemplateformat = $formdata->responsetemplate['format'];
  81          $DB->update_record('qtype_essay_options', $options);
  82      }
  83  
  84      protected function initialise_question_instance(question_definition $question, $questiondata) {
  85          parent::initialise_question_instance($question, $questiondata);
  86          $question->responseformat = $questiondata->options->responseformat;
  87          $question->responserequired = $questiondata->options->responserequired;
  88          $question->responsefieldlines = $questiondata->options->responsefieldlines;
  89          $question->attachments = $questiondata->options->attachments;
  90          $question->attachmentsrequired = $questiondata->options->attachmentsrequired;
  91          $question->graderinfo = $questiondata->options->graderinfo;
  92          $question->graderinfoformat = $questiondata->options->graderinfoformat;
  93          $question->responsetemplate = $questiondata->options->responsetemplate;
  94          $question->responsetemplateformat = $questiondata->options->responsetemplateformat;
  95          $filetypesutil = new \core_form\filetypes_util();
  96          $question->filetypeslist = $filetypesutil->normalize_file_types($questiondata->options->filetypeslist);
  97          $question->maxbytes = $questiondata->options->maxbytes;
  98      }
  99  
 100      public function delete_question($questionid, $contextid) {
 101          global $DB;
 102  
 103          $DB->delete_records('qtype_essay_options', array('questionid' => $questionid));
 104          parent::delete_question($questionid, $contextid);
 105      }
 106  
 107      /**
 108       * @return array the different response formats that the question type supports.
 109       * internal name => human-readable name.
 110       */
 111      public function response_formats() {
 112          return array(
 113              'editor' => get_string('formateditor', 'qtype_essay'),
 114              'editorfilepicker' => get_string('formateditorfilepicker', 'qtype_essay'),
 115              'plain' => get_string('formatplain', 'qtype_essay'),
 116              'monospaced' => get_string('formatmonospaced', 'qtype_essay'),
 117              'noinline' => get_string('formatnoinline', 'qtype_essay'),
 118          );
 119      }
 120  
 121      /**
 122       * @return array the choices that should be offerd when asking if a response is required
 123       */
 124      public function response_required_options() {
 125          return array(
 126              1 => get_string('responseisrequired', 'qtype_essay'),
 127              0 => get_string('responsenotrequired', 'qtype_essay'),
 128          );
 129      }
 130  
 131      /**
 132       * @return array the choices that should be offered for the input box size.
 133       */
 134      public function response_sizes() {
 135          $choices = array();
 136          for ($lines = 5; $lines <= 40; $lines += 5) {
 137              $choices[$lines] = get_string('nlines', 'qtype_essay', $lines);
 138          }
 139          return $choices;
 140      }
 141  
 142      /**
 143       * @return array the choices that should be offered for the number of attachments.
 144       */
 145      public function attachment_options() {
 146          return array(
 147              0 => get_string('no'),
 148              1 => '1',
 149              2 => '2',
 150              3 => '3',
 151              -1 => get_string('unlimited'),
 152          );
 153      }
 154  
 155      /**
 156       * @return array the choices that should be offered for the number of required attachments.
 157       */
 158      public function attachments_required_options() {
 159          return array(
 160              0 => get_string('attachmentsoptional', 'qtype_essay'),
 161              1 => '1',
 162              2 => '2',
 163              3 => '3'
 164          );
 165      }
 166  
 167      /**
 168       * Return array of the choices that should be offered for the maximum file sizes.
 169       * @return array|lang_string[]|string[]
 170       */
 171      public function max_file_size_options() {
 172          global $CFG, $COURSE;
 173          return get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
 174      }
 175  
 176      public function move_files($questionid, $oldcontextid, $newcontextid) {
 177          parent::move_files($questionid, $oldcontextid, $newcontextid);
 178          $fs = get_file_storage();
 179          $fs->move_area_files_to_new_context($oldcontextid,
 180                  $newcontextid, 'qtype_essay', 'graderinfo', $questionid);
 181      }
 182  
 183      protected function delete_files($questionid, $contextid) {
 184          parent::delete_files($questionid, $contextid);
 185          $fs = get_file_storage();
 186          $fs->delete_area_files($contextid, 'qtype_essay', 'graderinfo', $questionid);
 187      }
 188  }