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 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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('div', array('class' => 'ablock'));
 103          $result .= html_writer::tag('div', get_string('selectone', 'qtype_truefalse'),
 104                  array('class' => 'prompt'));
 105  
 106          $result .= html_writer::start_tag('div', array('class' => 'answer'));
 107          $result .= html_writer::tag('div', $radiotrue . ' ' . $truefeedbackimg,
 108                  array('class' => 'r0' . $trueclass));
 109          $result .= html_writer::tag('div', $radiofalse . ' ' . $falsefeedbackimg,
 110                  array('class' => 'r1' . $falseclass));
 111          $result .= html_writer::end_tag('div'); // Answer.
 112  
 113          $result .= html_writer::end_tag('div'); // Ablock.
 114  
 115          if ($qa->get_state() == question_state::$invalid) {
 116              $result .= html_writer::nonempty_tag('div',
 117                      $question->get_validation_error($responsearray),
 118                      array('class' => 'validationerror'));
 119          }
 120  
 121          return $result;
 122      }
 123  
 124      public function specific_feedback(question_attempt $qa) {
 125          $question = $qa->get_question();
 126          $response = $qa->get_last_qt_var('answer', '');
 127  
 128          if ($response) {
 129              return $question->format_text($question->truefeedback, $question->truefeedbackformat,
 130                      $qa, 'question', 'answerfeedback', $question->trueanswerid);
 131          } else if ($response !== '') {
 132              return $question->format_text($question->falsefeedback, $question->falsefeedbackformat,
 133                      $qa, 'question', 'answerfeedback', $question->falseanswerid);
 134          }
 135      }
 136  
 137      public function correct_response(question_attempt $qa) {
 138          $question = $qa->get_question();
 139  
 140          if ($question->rightanswer) {
 141              return get_string('correctanswertrue', 'qtype_truefalse');
 142          } else {
 143              return get_string('correctanswerfalse', 'qtype_truefalse');
 144          }
 145      }
 146  }