Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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 true-false question type.
  19   *
  20   * @package    qtype
  21   * @subpackage truefalse
  22   * @copyright  2007 Jamie Pratt
  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  require_once($CFG->dirroot.'/question/type/edit_question_form.php');
  31  
  32  
  33  /**
  34   * True-false question editing form definition.
  35   *
  36   * @copyright  2007 Jamie Pratt
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class qtype_truefalse_edit_form extends question_edit_form {
  40      /**
  41       * Add question-type specific form fields.
  42       *
  43       * @param object $mform the form being built.
  44       */
  45      protected function definition_inner($mform) {
  46          $mform->addElement('select', 'correctanswer',
  47                  get_string('correctanswer', 'qtype_truefalse'), array(
  48                  0 => get_string('false', 'qtype_truefalse'),
  49                  1 => get_string('true', 'qtype_truefalse')));
  50  
  51          $mform->addElement('selectyesno', 'showstandardinstruction', get_string('showstandardinstruction', 'qtype_truefalse'));
  52          $mform->addHelpButton('showstandardinstruction', 'showstandardinstruction', 'qtype_truefalse');
  53          $mform->setDefault('showstandardinstruction', $this->get_default_value('showstandardinstruction', 0));
  54  
  55          $mform->addElement('editor', 'feedbacktrue',
  56                  get_string('feedbacktrue', 'qtype_truefalse'), array('rows' => 10), $this->editoroptions);
  57          $mform->setType('feedbacktrue', PARAM_RAW);
  58  
  59          $mform->addElement('editor', 'feedbackfalse',
  60                  get_string('feedbackfalse', 'qtype_truefalse'), array('rows' => 10), $this->editoroptions);
  61          $mform->setType('feedbackfalse', PARAM_RAW);
  62  
  63          $mform->addElement('header', 'multitriesheader',
  64                  get_string('settingsformultipletries', 'question'));
  65  
  66          $mform->addElement('hidden', 'penalty', 1);
  67          $mform->setType('penalty', PARAM_FLOAT);
  68  
  69          $mform->addElement('static', 'penaltymessage',
  70                  get_string('penaltyforeachincorrecttry', 'question'), 1);
  71          $mform->addHelpButton('penaltymessage', 'penaltyforeachincorrecttry', 'question');
  72      }
  73  
  74      public function data_preprocessing($question) {
  75          $question = parent::data_preprocessing($question);
  76  
  77          if (!empty($question->options->trueanswer)) {
  78              $trueanswer = $question->options->answers[$question->options->trueanswer];
  79              $question->correctanswer = ($trueanswer->fraction != 0);
  80  
  81              $draftid = file_get_submitted_draft_itemid('trueanswer');
  82              $answerid = $question->options->trueanswer;
  83  
  84              $question->feedbacktrue = array();
  85              $question->feedbacktrue['format'] = $trueanswer->feedbackformat;
  86              $question->feedbacktrue['text'] = file_prepare_draft_area(
  87                  $draftid,             // Draftid
  88                  $this->context->id,   // context
  89                  'question',           // component
  90                  'answerfeedback',     // filarea
  91                  !empty($answerid) ? (int) $answerid : null, // itemid
  92                  $this->fileoptions,   // options
  93                  $trueanswer->feedback // text.
  94              );
  95              $question->feedbacktrue['itemid'] = $draftid;
  96          }
  97  
  98          if (!empty($question->options->falseanswer)) {
  99              $falseanswer = $question->options->answers[$question->options->falseanswer];
 100  
 101              $draftid = file_get_submitted_draft_itemid('falseanswer');
 102              $answerid = $question->options->falseanswer;
 103  
 104              $question->feedbackfalse = array();
 105              $question->feedbackfalse['format'] = $falseanswer->feedbackformat;
 106              $question->feedbackfalse['text'] = file_prepare_draft_area(
 107                  $draftid,              // Draftid
 108                  $this->context->id,    // context
 109                  'question',            // component
 110                  'answerfeedback',      // filarea
 111                  !empty($answerid) ? (int) $answerid : null, // itemid
 112                  $this->fileoptions,    // options
 113                  $falseanswer->feedback // text.
 114              );
 115              $question->feedbackfalse['itemid'] = $draftid;
 116          }
 117  
 118          if (!empty($question->options)) {
 119              $question->showstandardinstruction = !empty($question->options->showstandardinstruction);
 120          }
 121  
 122          return $question;
 123      }
 124  
 125      public function qtype() {
 126          return 'truefalse';
 127      }
 128  }