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 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   * Defines the editing form for the essay question type.
  19   *
  20   * @package    qtype
  21   * @subpackage essay
  22   * @copyright  2007 Jamie Pratt me@jamiep.org
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  /**
  31   * Essay question type editing form.
  32   *
  33   * @copyright  2007 Jamie Pratt me@jamiep.org
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class qtype_essay_edit_form extends question_edit_form {
  37  
  38      protected function definition_inner($mform) {
  39          $qtype = question_bank::get_qtype('essay');
  40  
  41          $mform->addElement('header', 'responseoptions', get_string('responseoptions', 'qtype_essay'));
  42          $mform->setExpanded('responseoptions');
  43  
  44          $mform->addElement('select', 'responseformat',
  45                  get_string('responseformat', 'qtype_essay'), $qtype->response_formats());
  46          $mform->setDefault('responseformat', $this->get_default_value('responseformat', 'editor'));
  47  
  48          $mform->addElement('select', 'responserequired',
  49                  get_string('responserequired', 'qtype_essay'), $qtype->response_required_options());
  50          $mform->setDefault('responserequired', $this->get_default_value('responserequired', 1));
  51          $mform->hideIf('responserequired', 'responseformat', 'eq', 'noinline');
  52  
  53          $mform->addElement('select', 'responsefieldlines',
  54                  get_string('responsefieldlines', 'qtype_essay'), $qtype->response_sizes());
  55          $mform->setDefault('responsefieldlines', $this->get_default_value('responsefieldlines', 10));
  56          $mform->hideIf('responsefieldlines', 'responseformat', 'eq', 'noinline');
  57  
  58          // Create a text box that can be enabled/disabled for max/min word limits options.
  59          $wordlimitoptions = ['size' => '6', 'maxlength' => '6'];
  60          $mingrp[] = $mform->createElement('text', 'minwordlimit', '', $wordlimitoptions);
  61          $mform->setType('minwordlimit', PARAM_INT);
  62          $mingrp[] = $mform->createElement('checkbox', 'minwordenabled', '', get_string('enable'));
  63          $mform->setDefault('minwordenabled', 0);
  64          $mform->addGroup($mingrp, 'mingroup', get_string('minwordlimit', 'qtype_essay'), ' ', false);
  65          $mform->addHelpButton('mingroup', 'minwordlimit', 'qtype_essay');
  66          $mform->disabledIf('minwordlimit', 'minwordenabled', 'notchecked');
  67          $mform->hideIf('mingroup', 'responserequired', 'eq', '0');
  68          $mform->hideIf('mingroup', 'responseformat', 'eq', 'noinline');
  69  
  70          $maxgrp[] = $mform->createElement('text', 'maxwordlimit', '', $wordlimitoptions);
  71          $mform->setType('maxwordlimit', PARAM_INT);
  72          $maxgrp[] = $mform->createElement('checkbox', 'maxwordenabled', '', get_string('enable'));
  73          $mform->setDefault('maxwordenabled', 0);
  74          $mform->addGroup($maxgrp, 'maxgroup', get_string('maxwordlimit', 'qtype_essay'), ' ', false);
  75          $mform->addHelpButton('maxgroup', 'maxwordlimit', 'qtype_essay');
  76          $mform->disabledIf('maxwordlimit', 'maxwordenabled', 'notchecked');
  77          $mform->hideIf('maxgroup', 'responserequired', 'eq', '0');
  78          $mform->hideIf('maxgroup', 'responseformat', 'eq', 'noinline');
  79  
  80          $mform->addElement('select', 'attachments',
  81                  get_string('allowattachments', 'qtype_essay'), $qtype->attachment_options());
  82          $mform->setDefault('attachments', $this->get_default_value('attachments', 0));
  83  
  84          $mform->addElement('select', 'attachmentsrequired',
  85                  get_string('attachmentsrequired', 'qtype_essay'), $qtype->attachments_required_options());
  86          $mform->setDefault('attachmentsrequired', $this->get_default_value('attachmentsrequired', 0));
  87          $mform->addHelpButton('attachmentsrequired', 'attachmentsrequired', 'qtype_essay');
  88          $mform->hideIf('attachmentsrequired', 'attachments', 'eq', 0);
  89  
  90          $mform->addElement('filetypes', 'filetypeslist', get_string('acceptedfiletypes', 'qtype_essay'));
  91          $mform->addHelpButton('filetypeslist', 'acceptedfiletypes', 'qtype_essay');
  92          $mform->hideIf('filetypeslist', 'attachments', 'eq', 0);
  93  
  94          $mform->addElement('select', 'maxbytes', get_string('maxbytes', 'qtype_essay'), $qtype->max_file_size_options());
  95          $mform->setDefault('maxbytes', $this->get_default_value('maxbytes', 0));
  96          $mform->hideIf('maxbytes', 'attachments', 'eq', 0);
  97  
  98          $mform->addElement('header', 'responsetemplateheader', get_string('responsetemplateheader', 'qtype_essay'));
  99          $mform->addElement('editor', 'responsetemplate', get_string('responsetemplate', 'qtype_essay'),
 100                  array('rows' => 10),  array_merge($this->editoroptions, array('maxfiles' => 0)));
 101          $mform->addHelpButton('responsetemplate', 'responsetemplate', 'qtype_essay');
 102  
 103          $mform->addElement('header', 'graderinfoheader', get_string('graderinfoheader', 'qtype_essay'));
 104          $mform->setExpanded('graderinfoheader');
 105          $mform->addElement('editor', 'graderinfo', get_string('graderinfo', 'qtype_essay'),
 106                  array('rows' => 10), $this->editoroptions);
 107      }
 108  
 109      protected function data_preprocessing($question) {
 110          $question = parent::data_preprocessing($question);
 111  
 112          if (empty($question->options)) {
 113              return $question;
 114          }
 115  
 116          $question->responseformat = $question->options->responseformat;
 117          $question->responserequired = $question->options->responserequired;
 118          $question->responsefieldlines = $question->options->responsefieldlines;
 119          $question->minwordenabled = $question->options->minwordlimit ? 1 : 0;
 120          $question->minwordlimit = $question->options->minwordlimit;
 121          $question->maxwordenabled = $question->options->maxwordlimit ? 1 : 0;
 122          $question->maxwordlimit = $question->options->maxwordlimit;
 123          $question->attachments = $question->options->attachments;
 124          $question->attachmentsrequired = $question->options->attachmentsrequired;
 125          $question->filetypeslist = $question->options->filetypeslist;
 126          $question->maxbytes = $question->options->maxbytes;
 127  
 128          $draftid = file_get_submitted_draft_itemid('graderinfo');
 129          $question->graderinfo = array();
 130          $question->graderinfo['text'] = file_prepare_draft_area(
 131              $draftid,           // Draftid
 132              $this->context->id, // context
 133              'qtype_essay',      // component
 134              'graderinfo',       // filarea
 135              !empty($question->id) ? (int) $question->id : null, // itemid
 136              $this->fileoptions, // options
 137              $question->options->graderinfo // text.
 138          );
 139          $question->graderinfo['format'] = $question->options->graderinfoformat;
 140          $question->graderinfo['itemid'] = $draftid;
 141  
 142          $question->responsetemplate = array(
 143              'text' => $question->options->responsetemplate,
 144              'format' => $question->options->responsetemplateformat,
 145          );
 146  
 147          return $question;
 148      }
 149  
 150      public function validation($fromform, $files) {
 151          $errors = parent::validation($fromform, $files);
 152  
 153          // Don't allow both 'no inline response' and 'no attachments' to be selected,
 154          // as these options would result in there being no input requested from the user.
 155          if ($fromform['responseformat'] == 'noinline' && !$fromform['attachments']) {
 156              $errors['attachments'] = get_string('mustattach', 'qtype_essay');
 157          }
 158  
 159          // If 'no inline response' is set, force the teacher to require attachments;
 160          // otherwise there will be nothing to grade.
 161          if ($fromform['responseformat'] == 'noinline' && !$fromform['attachmentsrequired']) {
 162              $errors['attachmentsrequired'] = get_string('mustrequire', 'qtype_essay');
 163          }
 164  
 165          // Don't allow the teacher to require more attachments than they allow; as this would
 166          // create a condition that it's impossible for the student to meet.
 167          if ($fromform['attachments'] > 0 && $fromform['attachments'] < $fromform['attachmentsrequired'] ) {
 168              $errors['attachmentsrequired']  = get_string('mustrequirefewer', 'qtype_essay');
 169          }
 170  
 171          if ($fromform['responserequired']) {
 172              if (isset($fromform['minwordenabled'])) {
 173                  if (!is_numeric($fromform['minwordlimit'])) {
 174                      $errors['mingroup'] = get_string('err_numeric', 'form');
 175                  }
 176                  if ($fromform['minwordlimit'] < 0) {
 177                      $errors['mingroup'] = get_string('err_minwordlimitnegative', 'qtype_essay');
 178                  }
 179                  if (!$fromform['minwordlimit']) {
 180                      $errors['mingroup'] = get_string('err_minwordlimit', 'qtype_essay');
 181                  }
 182              }
 183              if (isset($fromform['maxwordenabled'])) {
 184                  if (!is_numeric($fromform['maxwordlimit'])) {
 185                      $errors['maxgroup'] = get_string('err_numeric', 'form');
 186                  }
 187                  if ($fromform['maxwordlimit'] < 0) {
 188                      $errors['maxgroup'] = get_string('err_maxwordlimitnegative', 'qtype_essay');
 189                  }
 190                  if (!$fromform['maxwordlimit']) {
 191                      $errors['maxgroup'] = get_string('err_maxwordlimit', 'qtype_essay');
 192                  }
 193              }
 194              if (isset($fromform['maxwordenabled']) && isset($fromform['minwordenabled'])) {
 195                  if ($fromform['maxwordlimit'] < $fromform['minwordlimit'] &&
 196                      $fromform['maxwordlimit'] > 0 && $fromform['minwordlimit'] > 0) {
 197                      $errors['maxgroup'] = get_string('err_maxminmismatch', 'qtype_essay');
 198                  }
 199              }
 200          }
 201          return $errors;
 202      }
 203  
 204      public function qtype() {
 205          return 'essay';
 206      }
 207  }