2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * True-false question renderer class.
21 * @subpackage truefalse
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
31 * Generates the output for true-false questions.
33 * @copyright 2009 The Open University
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class qtype_truefalse_renderer
extends qtype_renderer
{
37 public function formulation_and_controls(question_attempt
$qa,
38 question_display_options
$options) {
40 $question = $qa->get_question();
41 $response = $qa->get_last_qt_var('answer', '');
43 $inputname = $qa->get_qt_field_name('answer');
44 $trueattributes = array(
48 'id' => $inputname . 'true',
50 $falseattributes = array(
54 'id' => $inputname . 'false',
57 if ($options->readonly
) {
58 $trueattributes['disabled'] = 'disabled';
59 $falseattributes['disabled'] = 'disabled';
62 // Work out which radio button to select (if any).
64 $falsechecked = false;
65 $responsearray = array();
67 $trueattributes['checked'] = 'checked';
69 $responsearray = array('answer' => 1);
70 } else if ($response !== '') {
71 $falseattributes['checked'] = 'checked';
73 $responsearray = array('answer' => 1);
76 // Work out visual feedback for answer correctness.
79 $truefeedbackimg = '';
80 $falsefeedbackimg = '';
81 if ($options->correctness
) {
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
));
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'));
99 $result .= html_writer
::tag('div', $question->format_questiontext($qa),
100 array('class' => 'qtext'));
102 $result .= html_writer
::start_tag('fieldset', array('class' => 'ablock'));
103 $questionnumber = $options->add_question_identifier_to_label(get_string('selectone', 'qtype_truefalse'), true, true);
104 $result .= html_writer
::tag('legend', $questionnumber,
105 array('class' => 'prompt h6 font-weight-normal'));
107 $result .= html_writer
::start_tag('div', array('class' => 'answer'));
108 $result .= html_writer
::tag('div', $radiotrue . ' ' . $truefeedbackimg,
109 array('class' => 'r0' . $trueclass));
110 $result .= html_writer
::tag('div', $radiofalse . ' ' . $falsefeedbackimg,
111 array('class' => 'r1' . $falseclass));
112 $result .= html_writer
::end_tag('div'); // Answer.
114 $result .= html_writer
::end_tag('fieldset'); // Ablock.
116 if ($qa->get_state() == question_state
::$invalid) {
117 $result .= html_writer
::nonempty_tag('div',
118 $question->get_validation_error($responsearray),
119 array('class' => 'validationerror'));
125 public function specific_feedback(question_attempt
$qa) {
126 $question = $qa->get_question();
127 $response = $qa->get_last_qt_var('answer', '');
130 return $question->format_text($question->truefeedback
, $question->truefeedbackformat
,
131 $qa, 'question', 'answerfeedback', $question->trueanswerid
);
132 } else if ($response !== '') {
133 return $question->format_text($question->falsefeedback
, $question->falsefeedbackformat
,
134 $qa, 'question', 'answerfeedback', $question->falseanswerid
);
138 public function correct_response(question_attempt
$qa) {
139 $question = $qa->get_question();
141 if ($question->rightanswer
) {
142 return get_string('correctanswertrue', 'qtype_truefalse');
144 return get_string('correctanswerfalse', 'qtype_truefalse');