MDL-20636 Split question-type specific styles into the separate plugins.
[moodle.git] / question / type / truefalse / renderer.php
CommitLineData
d1b7e03d
TH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
d1b7e03d
TH
18/**
19 * True-false question renderer class.
20 *
7764183a
TH
21 * @package qtype
22 * @subpackage truefalse
23 * @copyright 2009 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
25 */
26
27
a17b297d
TH
28defined('MOODLE_INTERNAL') || die();
29
30
d1b7e03d
TH
31/**
32 * Generates the output for true-false questions.
33 *
7764183a
TH
34 * @copyright 2009 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
36 */
37class qtype_truefalse_renderer extends qtype_renderer {
38 public function formulation_and_controls(question_attempt $qa,
39 question_display_options $options) {
40
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']));
95 $radiofalse = html_writer::empty_tag('input', $falseattributes) .
96 html_writer::tag('label', get_string('false', 'qtype_truefalse'),
97 array('for' => $falseattributes['id']));
98
99 $result = '';
c76145d3 100 $result .= html_writer::tag('div', $question->format_questiontext($qa),
d1b7e03d
TH
101 array('class' => 'qtext'));
102
103 $result .= html_writer::start_tag('div', array('class' => 'ablock'));
104 $result .= html_writer::tag('div', get_string('selectone', 'qtype_truefalse'),
105 array('class' => 'prompt'));
106
107 $result .= html_writer::start_tag('div', array('class' => 'answer'));
a18fda20 108 $result .= html_writer::tag('div', $radiotrue . ' ' . $truefeedbackimg,
d1b7e03d 109 array('class' => 'r0' . $trueclass));
a18fda20 110 $result .= html_writer::tag('div', $radiofalse . ' ' . $falsefeedbackimg,
d1b7e03d
TH
111 array('class' => 'r1' . $falseclass));
112 $result .= html_writer::end_tag('div'); // answer
113
114 $result .= html_writer::end_tag('div'); // ablock
115
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'));
120 }
121
122 return $result;
123 }
124
125 public function specific_feedback(question_attempt $qa) {
126 $question = $qa->get_question();
127 $response = $qa->get_last_qt_var('answer', '');
128
129 if ($response) {
7a719748
TH
130 return $question->format_text($question->truefeedback, $qa,
131 'question', 'answerfeedback', $question->trueanswerid);
d1b7e03d 132 } else {
7a719748
TH
133 return $question->format_text($question->falsefeedback, $qa,
134 'question', 'answerfeedback', $question->falseanswerid);
d1b7e03d
TH
135 }
136 }
137
138 public function correct_response(question_attempt $qa) {
139 $question = $qa->get_question();
140
141 if ($question->rightanswer) {
142 return get_string('correctanswertrue', 'qtype_truefalse');
143 } else {
144 return get_string('correctanswerfalse', 'qtype_truefalse');
145 }
146 }
147}