Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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