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] [Versions 401 and 402] [Versions 401 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   * True-false question renderer class.
  19   *
  20   * @package    qtype
  21   * @subpackage truefalse
  22   * @copyright  2009 The Open University
  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   * Generates the output for true-false questions.
  32   *
  33   * @copyright  2009 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class qtype_truefalse_renderer extends qtype_renderer {
  37      public function formulation_and_controls(question_attempt $qa,
  38              question_display_options $options) {
  39  
  40          $question = $qa->get_question();
  41          $response = $qa->get_last_qt_var('answer', '');
  42  
  43          $inputname = $qa->get_qt_field_name('answer');
  44          $trueattributes = array(
  45              'type' => 'radio',
  46              'name' => $inputname,
  47              'value' => 1,
  48              'id' => $inputname . 'true',
  49          );
  50          $falseattributes = array(
  51              'type' => 'radio',
  52              'name' => $inputname,
  53              'value' => 0,
  54              'id' => $inputname . 'false',
  55          );
  56  
  57          if ($options->readonly) {
  58              $trueattributes['disabled'] = 'disabled';
  59              $falseattributes['disabled'] = 'disabled';
  60          }
  61  
  62          // Work out which radio button to select (if any).
  63          $truechecked = false;
  64          $falsechecked = false;
  65          $responsearray = array();
  66          if ($response) {
  67              $trueattributes['checked'] = 'checked';
  68              $truechecked = true;
  69              $responsearray = array('answer' => 1);
  70          } else if ($response !== '') {
  71              $falseattributes['checked'] = 'checked';
  72              $falsechecked = true;
  73              $responsearray = array('answer' => 1);
  74          }
  75  
  76          // Work out visual feedback for answer correctness.
  77          $trueclass = '';
  78          $falseclass = '';
  79          $truefeedbackimg = '';
  80          $falsefeedbackimg = '';
  81          if ($options->correctness) {
  82              if ($truechecked) {
  83                  $trueclass = ' ' . $this->feedback_class((int) $question->rightanswer);
  84                  $truefeedbackimg = $this->feedback_image((int) $question->rightanswer);
  85              } else if ($falsechecked) {
  86                  $falseclass = ' ' . $this->feedback_class((int) (!$question->rightanswer));
  87                  $falsefeedbackimg = $this->feedback_image((int) (!$question->rightanswer));
  88              }
  89          }
  90  
  91          $radiotrue = html_writer::empty_tag('input', $trueattributes) .
  92                  html_writer::tag('label', get_string('true', 'qtype_truefalse'),
  93                  array('for' => $trueattributes['id'], 'class' => 'ml-1'));
  94          $radiofalse = html_writer::empty_tag('input', $falseattributes) .
  95                  html_writer::tag('label', get_string('false', 'qtype_truefalse'),
  96                  array('for' => $falseattributes['id'], 'class' => 'ml-1'));
  97  
  98          $result = '';
  99          $result .= html_writer::tag('div', $question->format_questiontext($qa),
 100                  array('class' => 'qtext'));
 101  
 102          $result .= html_writer::start_tag('fieldset', array('class' => 'ablock'));
 103          if (!empty($question->showstandardinstruction)) {
 104              $legendclass = '';
 105              $questionnumber = $options->add_question_identifier_to_label(get_string('selectone', 'qtype_truefalse'), true, true);
 106          } else {
 107              $legendclass = 'sr-only';
 108              $questionnumber = $options->add_question_identifier_to_label(get_string('answer'), true, true);
 109          }
 110          $result .= html_writer::tag('legend', $questionnumber,
 111              array('class' => 'prompt h6 font-weight-normal ' . $legendclass));
 112  
 113          $result .= html_writer::start_tag('div', array('class' => 'answer'));
 114          $result .= html_writer::tag('div', $radiotrue . ' ' . $truefeedbackimg,
 115                  array('class' => 'r0' . $trueclass));
 116          $result .= html_writer::tag('div', $radiofalse . ' ' . $falsefeedbackimg,
 117                  array('class' => 'r1' . $falseclass));
 118          $result .= html_writer::end_tag('div'); // Answer.
 119  
 120          $result .= html_writer::end_tag('fieldset'); // Ablock.
 121  
 122          if ($qa->get_state() == question_state::$invalid) {
 123              $result .= html_writer::nonempty_tag('div',
 124                      $question->get_validation_error($responsearray),
 125                      array('class' => 'validationerror'));
 126          }
 127  
 128          return $result;
 129      }
 130  
 131      public function specific_feedback(question_attempt $qa) {
 132          $question = $qa->get_question();
 133          $response = $qa->get_last_qt_var('answer', '');
 134  
 135          if ($response) {
 136              return $question->format_text($question->truefeedback, $question->truefeedbackformat,
 137                      $qa, 'question', 'answerfeedback', $question->trueanswerid);
 138          } else if ($response !== '') {
 139              return $question->format_text($question->falsefeedback, $question->falsefeedbackformat,
 140                      $qa, 'question', 'answerfeedback', $question->falseanswerid);
 141          }
 142      }
 143  
 144      public function correct_response(question_attempt $qa) {
 145          $question = $qa->get_question();
 146  
 147          if ($question->rightanswer) {
 148              return get_string('correctanswertrue', 'qtype_truefalse');
 149          } else {
 150              return get_string('correctanswerfalse', 'qtype_truefalse');
 151          }
 152      }
 153  }