MDL-69823 qtype: Support core question types
[moodle.git] / question / type / truefalse / renderer.php
CommitLineData
d1b7e03d 1<?php
d1b7e03d
TH
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
d1b7e03d
TH
17/**
18 * True-false question renderer class.
19 *
7764183a
TH
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
d1b7e03d
TH
24 */
25
26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
d1b7e03d
TH
30/**
31 * Generates the output for true-false questions.
32 *
7764183a
TH
33 * @copyright 2009 The Open University
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
35 */
36class 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
3d9645ae 62 // Work out which radio button to select (if any).
d1b7e03d
TH
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'),
5cac5fa4 93 array('for' => $trueattributes['id'], 'class' => 'ml-1'));
d1b7e03d
TH
94 $radiofalse = html_writer::empty_tag('input', $falseattributes) .
95 html_writer::tag('label', get_string('false', 'qtype_truefalse'),
5cac5fa4 96 array('for' => $falseattributes['id'], 'class' => 'ml-1'));
d1b7e03d
TH
97
98 $result = '';
c76145d3 99 $result .= html_writer::tag('div', $question->format_questiontext($qa),
d1b7e03d
TH
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'));
a18fda20 107 $result .= html_writer::tag('div', $radiotrue . ' ' . $truefeedbackimg,
d1b7e03d 108 array('class' => 'r0' . $trueclass));
a18fda20 109 $result .= html_writer::tag('div', $radiofalse . ' ' . $falsefeedbackimg,
d1b7e03d 110 array('class' => 'r1' . $falseclass));
3d9645ae 111 $result .= html_writer::end_tag('div'); // Answer.
d1b7e03d 112
3d9645ae 113 $result .= html_writer::end_tag('div'); // Ablock.
d1b7e03d
TH
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) {
22cebed5
TH
129 return $question->format_text($question->truefeedback, $question->truefeedbackformat,
130 $qa, 'question', 'answerfeedback', $question->trueanswerid);
188e1744 131 } else if ($response !== '') {
22cebed5
TH
132 return $question->format_text($question->falsefeedback, $question->falsefeedbackformat,
133 $qa, 'question', 'answerfeedback', $question->falseanswerid);
d1b7e03d
TH
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}