Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

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