Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400]

   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_defaults_for_new_questions(stdClass $fromform): void {
  55          parent::save_defaults_for_new_questions($fromform);
  56          $this->set_default_value('responseformat', $fromform->responseformat);
  57          $this->set_default_value('responserequired', $fromform->responserequired);
  58          $this->set_default_value('responsefieldlines', $fromform->responsefieldlines);
  59          $this->set_default_value('attachments', $fromform->attachments);
  60          $this->set_default_value('attachmentsrequired', $fromform->attachmentsrequired);
  61          $this->set_default_value('maxbytes', $fromform->maxbytes);
  62      }
  63  
  64      public function save_question_options($formdata) {
  65          global $DB;
  66          $context = $formdata->context;
  67  
  68          $options = $DB->get_record('qtype_essay_options', array('questionid' => $formdata->id));
  69          if (!$options) {
  70              $options = new stdClass();
  71              $options->questionid = $formdata->id;
  72              $options->id = $DB->insert_record('qtype_essay_options', $options);
  73          }
  74  
  75          $options->responseformat = $formdata->responseformat;
  76          $options->responserequired = $formdata->responserequired;
  77          $options->responsefieldlines = $formdata->responsefieldlines;
  78          $options->minwordlimit = isset($formdata->minwordenabled) ? $formdata->minwordlimit : null;
  79          $options->maxwordlimit = isset($formdata->maxwordenabled) ? $formdata->maxwordlimit : null;
  80          $options->attachments = $formdata->attachments;
  81          if ((int)$formdata->attachments === 0 && $formdata->attachmentsrequired > 0) {
  82              // Adjust the value for the field 'attachmentsrequired' when the field 'attachments' is set to 'No'.
  83              $options->attachmentsrequired = 0;
  84          } else {
  85              $options->attachmentsrequired = $formdata->attachmentsrequired;
  86          }
  87          if (!isset($formdata->filetypeslist)) {
  88              $options->filetypeslist = null;
  89          } else {
  90              $options->filetypeslist = $formdata->filetypeslist;
  91          }
  92          $options->maxbytes = $formdata->maxbytes ?? 0;
  93          $options->graderinfo = $this->import_or_save_files($formdata->graderinfo,
  94                  $context, 'qtype_essay', 'graderinfo', $formdata->id);
  95          $options->graderinfoformat = $formdata->graderinfo['format'];
  96          $options->responsetemplate = $formdata->responsetemplate['text'];
  97          $options->responsetemplateformat = $formdata->responsetemplate['format'];
  98          $DB->update_record('qtype_essay_options', $options);
  99      }
 100  
 101      protected function initialise_question_instance(question_definition $question, $questiondata) {
 102          parent::initialise_question_instance($question, $questiondata);
 103          $question->responseformat = $questiondata->options->responseformat;
 104          $question->responserequired = $questiondata->options->responserequired;
 105          $question->responsefieldlines = $questiondata->options->responsefieldlines;
 106          $question->minwordlimit = $questiondata->options->minwordlimit;
 107          $question->maxwordlimit = $questiondata->options->maxwordlimit;
 108          $question->attachments = $questiondata->options->attachments;
 109          $question->attachmentsrequired = $questiondata->options->attachmentsrequired;
 110          $question->graderinfo = $questiondata->options->graderinfo;
 111          $question->graderinfoformat = $questiondata->options->graderinfoformat;
 112          $question->responsetemplate = $questiondata->options->responsetemplate;
 113          $question->responsetemplateformat = $questiondata->options->responsetemplateformat;
 114          $filetypesutil = new \core_form\filetypes_util();
 115          $question->filetypeslist = $filetypesutil->normalize_file_types($questiondata->options->filetypeslist);
 116          $question->maxbytes = $questiondata->options->maxbytes;
 117      }
 118  
 119      public function delete_question($questionid, $contextid) {
 120          global $DB;
 121  
 122          $DB->delete_records('qtype_essay_options', array('questionid' => $questionid));
 123          parent::delete_question($questionid, $contextid);
 124      }
 125  
 126      /**
 127       * @return array the different response formats that the question type supports.
 128       * internal name => human-readable name.
 129       */
 130      public function response_formats() {
 131          return array(
 132              'editor' => get_string('formateditor', 'qtype_essay'),
 133              'editorfilepicker' => get_string('formateditorfilepicker', 'qtype_essay'),
 134              'plain' => get_string('formatplain', 'qtype_essay'),
 135              'monospaced' => get_string('formatmonospaced', 'qtype_essay'),
 136              'noinline' => get_string('formatnoinline', 'qtype_essay'),
 137          );
 138      }
 139  
 140      /**
 141       * @return array the choices that should be offerd when asking if a response is required
 142       */
 143      public function response_required_options() {
 144          return array(
 145              1 => get_string('responseisrequired', 'qtype_essay'),
 146              0 => get_string('responsenotrequired', 'qtype_essay'),
 147          );
 148      }
 149  
 150      /**
 151       * @return array the choices that should be offered for the input box size.
 152       */
 153      public function response_sizes() {
 154          $choices = [
 155              2 => get_string('nlines', 'qtype_essay', 2),
 156              3 => get_string('nlines', 'qtype_essay', 3),
 157          ];
 158          for ($lines = 5; $lines <= 40; $lines += 5) {
 159              $choices[$lines] = get_string('nlines', 'qtype_essay', $lines);
 160          }
 161          return $choices;
 162      }
 163  
 164      /**
 165       * @return array the choices that should be offered for the number of attachments.
 166       */
 167      public function attachment_options() {
 168          return array(
 169              0 => get_string('no'),
 170              1 => '1',
 171              2 => '2',
 172              3 => '3',
 173              -1 => get_string('unlimited'),
 174          );
 175      }
 176  
 177      /**
 178       * @return array the choices that should be offered for the number of required attachments.
 179       */
 180      public function attachments_required_options() {
 181          return array(
 182              0 => get_string('attachmentsoptional', 'qtype_essay'),
 183              1 => '1',
 184              2 => '2',
 185              3 => '3'
 186          );
 187      }
 188  
 189      /**
 190       * Return array of the choices that should be offered for the maximum file sizes.
 191       * @return array|lang_string[]|string[]
 192       */
 193      public function max_file_size_options() {
 194          global $CFG, $COURSE;
 195          return get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
 196      }
 197  
 198      public function move_files($questionid, $oldcontextid, $newcontextid) {
 199          parent::move_files($questionid, $oldcontextid, $newcontextid);
 200          $fs = get_file_storage();
 201          $fs->move_area_files_to_new_context($oldcontextid,
 202                  $newcontextid, 'qtype_essay', 'graderinfo', $questionid);
 203      }
 204  
 205      protected function delete_files($questionid, $contextid) {
 206          parent::delete_files($questionid, $contextid);
 207          $fs = get_file_storage();
 208          $fs->delete_area_files($contextid, 'qtype_essay', 'graderinfo', $questionid);
 209      }
 210  }