MDL-20636 Current work-in-progress converting the question engine to Moodle 2.0
[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
18
19/**
20 * True-false question renderer class.
21 *
22 * @package qtype_truefalse
23 * @copyright 2009 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
28/**
29 * Generates the output for true-false questions.
30 *
31 * @copyright 2009 The Open University
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class qtype_truefalse_renderer extends qtype_renderer {
35 public function formulation_and_controls(question_attempt $qa,
36 question_display_options $options) {
37
38 $question = $qa->get_question();
39 $response = $qa->get_last_qt_var('answer', '');
40
41 $inputname = $qa->get_qt_field_name('answer');
42 $trueattributes = array(
43 'type' => 'radio',
44 'name' => $inputname,
45 'value' => 1,
46 'id' => $inputname . 'true',
47 );
48 $falseattributes = array(
49 'type' => 'radio',
50 'name' => $inputname,
51 'value' => 0,
52 'id' => $inputname . 'false',
53 );
54
55 if ($options->readonly) {
56 $trueattributes['disabled'] = 'disabled';
57 $falseattributes['disabled'] = 'disabled';
58 }
59
60 // Work out which radio button to select (if any)
61 $truechecked = false;
62 $falsechecked = false;
63 $responsearray = array();
64 if ($response) {
65 $trueattributes['checked'] = 'checked';
66 $truechecked = true;
67 $responsearray = array('answer' => 1);
68 } else if ($response !== '') {
69 $falseattributes['checked'] = 'checked';
70 $falsechecked = true;
71 $responsearray = array('answer' => 1);
72 }
73
74 // Work out visual feedback for answer correctness.
75 $trueclass = '';
76 $falseclass = '';
77 $truefeedbackimg = '';
78 $falsefeedbackimg = '';
79 if ($options->correctness) {
80 if ($truechecked) {
81 $trueclass = ' ' . $this->feedback_class((int) $question->rightanswer);
82 $truefeedbackimg = $this->feedback_image((int) $question->rightanswer);
83 } else if ($falsechecked) {
84 $falseclass = ' ' . $this->feedback_class((int) (!$question->rightanswer));
85 $falsefeedbackimg = $this->feedback_image((int) (!$question->rightanswer));
86 }
87 }
88
89 $radiotrue = html_writer::empty_tag('input', $trueattributes) .
90 html_writer::tag('label', get_string('true', 'qtype_truefalse'),
91 array('for' => $trueattributes['id']));
92 $radiofalse = html_writer::empty_tag('input', $falseattributes) .
93 html_writer::tag('label', get_string('false', 'qtype_truefalse'),
94 array('for' => $falseattributes['id']));
95
96 $result = '';
97 $result .= html_writer::tag('div', $question->format_questiontext(),
98 array('class' => 'qtext'));
99
100 $result .= html_writer::start_tag('div', array('class' => 'ablock'));
101 $result .= html_writer::tag('div', get_string('selectone', 'qtype_truefalse'),
102 array('class' => 'prompt'));
103
104 $result .= html_writer::start_tag('div', array('class' => 'answer'));
105 $result .= html_writer::tag('span', $radiotrue . ' ' . $truefeedbackimg,
106 array('class' => 'r0' . $trueclass));
107 $result .= html_writer::tag('span', $radiofalse . ' ' . $falsefeedbackimg,
108 array('class' => 'r1' . $falseclass));
109 $result .= html_writer::end_tag('div'); // answer
110
111 $result .= html_writer::end_tag('div'); // ablock
112
113 if ($qa->get_state() == question_state::$invalid) {
114 $result .= html_writer::nonempty_tag('div',
115 $question->get_validation_error($responsearray),
116 array('class' => 'validationerror'));
117 }
118
119 return $result;
120 }
121
122 public function specific_feedback(question_attempt $qa) {
123 $question = $qa->get_question();
124 $response = $qa->get_last_qt_var('answer', '');
125
126 if ($response) {
127 return $question->format_text($question->truefeedback);
128 } else {
129 return $question->format_text($question->falsefeedback);
130 }
131 }
132
133 public function correct_response(question_attempt $qa) {
134 $question = $qa->get_question();
135
136 if ($question->rightanswer) {
137 return get_string('correctanswertrue', 'qtype_truefalse');
138 } else {
139 return get_string('correctanswerfalse', 'qtype_truefalse');
140 }
141 }
142}