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   * 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', 'editor');
  47  
  48          $mform->addElement('select', 'responserequired',
  49                  get_string('responserequired', 'qtype_essay'), $qtype->response_required_options());
  50          $mform->setDefault('responserequired', 1);
  51          $mform->disabledIf('responserequired', 'responseformat', 'eq', 'noinline');
  52  
  53          $mform->addElement('select', 'responsefieldlines',
  54                  get_string('responsefieldlines', 'qtype_essay'), $qtype->response_sizes());
  55          $mform->setDefault('responsefieldlines', 15);
  56          $mform->disabledIf('responsefieldlines', 'responseformat', 'eq', 'noinline');
  57  
  58          $mform->addElement('select', 'attachments',
  59                  get_string('allowattachments', 'qtype_essay'), $qtype->attachment_options());
  60          $mform->setDefault('attachments', 0);
  61  
  62          $mform->addElement('select', 'attachmentsrequired',
  63                  get_string('attachmentsrequired', 'qtype_essay'), $qtype->attachments_required_options());
  64          $mform->setDefault('attachmentsrequired', 0);
  65          $mform->addHelpButton('attachmentsrequired', 'attachmentsrequired', 'qtype_essay');
  66          $mform->disabledIf('attachmentsrequired', 'attachments', 'eq', 0);
  67  
  68          $mform->addElement('filetypes', 'filetypeslist', get_string('acceptedfiletypes', 'qtype_essay'));
  69          $mform->addHelpButton('filetypeslist', 'acceptedfiletypes', 'qtype_essay');
  70          $mform->disabledIf('filetypeslist', 'attachments', 'eq', 0);
  71  
  72          $mform->addElement('select', 'maxbytes', get_string('maxbytes', 'qtype_essay'), $qtype->max_file_size_options());
  73          $mform->setDefault('maxbytes', '0');
  74          $mform->disabledIf('maxbytes', 'attachments', 'eq', 0);
  75  
  76          $mform->addElement('header', 'responsetemplateheader', get_string('responsetemplateheader', 'qtype_essay'));
  77          $mform->addElement('editor', 'responsetemplate', get_string('responsetemplate', 'qtype_essay'),
  78                  array('rows' => 10),  array_merge($this->editoroptions, array('maxfiles' => 0)));
  79          $mform->addHelpButton('responsetemplate', 'responsetemplate', 'qtype_essay');
  80  
  81          $mform->addElement('header', 'graderinfoheader', get_string('graderinfoheader', 'qtype_essay'));
  82          $mform->setExpanded('graderinfoheader');
  83          $mform->addElement('editor', 'graderinfo', get_string('graderinfo', 'qtype_essay'),
  84                  array('rows' => 10), $this->editoroptions);
  85      }
  86  
  87      protected function data_preprocessing($question) {
  88          $question = parent::data_preprocessing($question);
  89  
  90          if (empty($question->options)) {
  91              return $question;
  92          }
  93  
  94          $question->responseformat = $question->options->responseformat;
  95          $question->responserequired = $question->options->responserequired;
  96          $question->responsefieldlines = $question->options->responsefieldlines;
  97          $question->attachments = $question->options->attachments;
  98          $question->attachmentsrequired = $question->options->attachmentsrequired;
  99          $question->filetypeslist = $question->options->filetypeslist;
 100          $question->maxbytes = $question->options->maxbytes;
 101  
 102          $draftid = file_get_submitted_draft_itemid('graderinfo');
 103          $question->graderinfo = array();
 104          $question->graderinfo['text'] = file_prepare_draft_area(
 105              $draftid,           // Draftid
 106              $this->context->id, // context
 107              'qtype_essay',      // component
 108              'graderinfo',       // filarea
 109              !empty($question->id) ? (int) $question->id : null, // itemid
 110              $this->fileoptions, // options
 111              $question->options->graderinfo // text.
 112          );
 113          $question->graderinfo['format'] = $question->options->graderinfoformat;
 114          $question->graderinfo['itemid'] = $draftid;
 115  
 116          $question->responsetemplate = array(
 117              'text' => $question->options->responsetemplate,
 118              'format' => $question->options->responsetemplateformat,
 119          );
 120  
 121          return $question;
 122      }
 123  
 124      public function validation($fromform, $files) {
 125          $errors = parent::validation($fromform, $files);
 126  
 127          // Don't allow both 'no inline response' and 'no attachments' to be selected,
 128          // as these options would result in there being no input requested from the user.
 129          if ($fromform['responseformat'] == 'noinline' && !$fromform['attachments']) {
 130              $errors['attachments'] = get_string('mustattach', 'qtype_essay');
 131          }
 132  
 133          // If 'no inline response' is set, force the teacher to require attachments;
 134          // otherwise there will be nothing to grade.
 135          if ($fromform['responseformat'] == 'noinline' && !$fromform['attachmentsrequired']) {
 136              $errors['attachmentsrequired'] = get_string('mustrequire', 'qtype_essay');
 137          }
 138  
 139          // Don't allow the teacher to require more attachments than they allow; as this would
 140          // create a condition that it's impossible for the student to meet.
 141          if ($fromform['attachments'] != -1 && $fromform['attachments'] < $fromform['attachmentsrequired'] ) {
 142              $errors['attachmentsrequired']  = get_string('mustrequirefewer', 'qtype_essay');
 143          }
 144  
 145          return $errors;
 146      }
 147  
 148      public function qtype() {
 149          return 'essay';
 150      }
 151  }