MDL-62454 theme_boost: numerical qtype answer inline
[moodle.git] / question / type / numerical / renderer.php
CommitLineData
f77f47ec 1<?php
f77f47ec
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
17
18/**
19 * Numerical question renderer class.
20 *
21 * @package qtype_numerical
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27/**
28 * Generates the output for short answer questions.
29 *
30 * @copyright 2009 The Open University
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
33class qtype_numerical_renderer extends qtype_renderer {
34 public function formulation_and_controls(question_attempt $qa,
35 question_display_options $options) {
36
37 $question = $qa->get_question();
38 $currentanswer = $qa->get_last_qt_var('answer');
8566369f 39 if ($question->has_separate_unit_field()) {
5073fb74
TH
40 $selectedunit = $qa->get_last_qt_var('unit');
41 } else {
42 $selectedunit = null;
43 }
f77f47ec
TH
44
45 $inputname = $qa->get_qt_field_name('answer');
46 $inputattributes = array(
47 'type' => 'text',
48 'name' => $inputname,
49 'value' => $currentanswer,
50 'id' => $inputname,
5fe9aca3
BB
51 'size' => 30,
52 'class' => 'form-control d-inline',
f77f47ec
TH
53 );
54
55 if ($options->readonly) {
56 $inputattributes['readonly'] = 'readonly';
57 }
58
59 $feedbackimg = '';
60 if ($options->correctness) {
3a6eb8ef
TH
61 list($value, $unit, $multiplier) = $question->ap->apply_units(
62 $currentanswer, $selectedunit);
63 $answer = $question->get_matching_answer($value, $multiplier);
f77f47ec 64 if ($answer) {
3a6eb8ef 65 $fraction = $question->apply_unit_penalty($answer->fraction, $answer->unitisright);
f77f47ec
TH
66 } else {
67 $fraction = 0;
68 }
12d6dfc1 69 $inputattributes['class'] .= ' ' . $this->feedback_class($fraction);
f77f47ec
TH
70 $feedbackimg = $this->feedback_image($fraction);
71 }
72
544de1c0 73 $questiontext = $question->format_questiontext($qa);
f77f47ec
TH
74 $placeholder = false;
75 if (preg_match('/_____+/', $questiontext, $matches)) {
76 $placeholder = $matches[0];
77 $inputattributes['size'] = round(strlen($placeholder) * 1.1);
78 }
79
c3cdf1e4 80 $input = html_writer::empty_tag('input', $inputattributes) . $feedbackimg;
f77f47ec 81
8566369f
TH
82 if ($question->has_separate_unit_field()) {
83 if ($question->unitdisplay == qtype_numerical::UNITRADIO) {
84 $choices = array();
85 $i = 1;
86 foreach ($question->ap->get_unit_options() as $unit) {
87 $id = $qa->get_qt_field_name('unit') . '_' . $i++;
88 $radioattrs = array('type' => 'radio', 'id' => $id, 'value' => $unit,
89 'name' => $qa->get_qt_field_name('unit'));
90 if ($unit == $selectedunit) {
91 $radioattrs['checked'] = 'checked';
92 }
93 $choices[] = html_writer::tag('label',
94 html_writer::empty_tag('input', $radioattrs) . $unit,
95 array('for' => $id, 'class' => 'unitchoice'));
96 }
97
98 $unitchoice = html_writer::tag('span', implode(' ', $choices),
99 array('class' => 'unitchoices'));
100
101 } else if ($question->unitdisplay == qtype_numerical::UNITSELECT) {
c3cdf1e4 102 $unitchoice = html_writer::label(get_string('selectunit', 'qtype_numerical'),
83690170
RW
103 'menu' . $qa->get_qt_field_name('unit'), false, array('class' => 'accesshide'));
104 $unitchoice .= html_writer::select($question->ap->get_unit_options(),
8566369f
TH
105 $qa->get_qt_field_name('unit'), $selectedunit, array(''=>'choosedots'),
106 array('disabled' => $options->readonly));
107 }
108
5073fb74 109 if ($question->ap->are_units_before()) {
8566369f 110 $input = $unitchoice . ' ' . $input;
5073fb74 111 } else {
8566369f 112 $input = $input . ' ' . $unitchoice;
5073fb74
TH
113 }
114 }
115
f77f47ec 116 if ($placeholder) {
c3cdf1e4
FM
117 $inputinplace = html_writer::tag('label', get_string('answer'),
118 array('for' => $inputattributes['id'], 'class' => 'accesshide'));
119 $inputinplace .= $input;
120 $questiontext = substr_replace($questiontext, $inputinplace,
f77f47ec
TH
121 strpos($questiontext, $placeholder), strlen($placeholder));
122 }
123
124 $result = html_writer::tag('div', $questiontext, array('class' => 'qtext'));
125
126 if (!$placeholder) {
12d6dfc1 127 $result .= html_writer::start_tag('div', array('class' => 'ablock form-inline'));
c3cdf1e4
FM
128 $result .= html_writer::tag('label', get_string('answercolon', 'qtype_numerical'), array('for' => $inputattributes['id']));
129 $result .= html_writer::tag('span', $input, array('class' => 'answer'));
f77f47ec
TH
130 $result .= html_writer::end_tag('div');
131 }
132
133 if ($qa->get_state() == question_state::$invalid) {
134 $result .= html_writer::nonempty_tag('div',
c9340ac1 135 $question->get_validation_error(array('answer' => $currentanswer, 'unit' => $selectedunit)),
f77f47ec
TH
136 array('class' => 'validationerror'));
137 }
138
139 return $result;
140 }
141
142 public function specific_feedback(question_attempt $qa) {
143 $question = $qa->get_question();
144
8566369f 145 if ($question->has_separate_unit_field()) {
5073fb74
TH
146 $selectedunit = $qa->get_last_qt_var('unit');
147 } else {
148 $selectedunit = null;
149 }
3a6eb8ef 150 list($value, $unit, $multiplier) = $question->ap->apply_units(
5073fb74 151 $qa->get_last_qt_var('answer'), $selectedunit);
3a6eb8ef 152 $answer = $question->get_matching_answer($value, $multiplier);
ae3e2e6e
TH
153
154 if ($answer && $answer->feedback) {
155 $feedback = $question->format_text($answer->feedback, $answer->feedbackformat,
156 $qa, 'question', 'answerfeedback', $answer->id);
157 } else {
158 $feedback = '';
159 }
160
161 if ($question->unitgradingtype && !$question->ap->is_known_unit($unit)) {
162 $feedback .= html_writer::tag('p', get_string('unitincorrect', 'qtype_numerical'));
f77f47ec
TH
163 }
164
ae3e2e6e 165 return $feedback;
f77f47ec
TH
166 }
167
168 public function correct_response(question_attempt $qa) {
5073fb74
TH
169 $question = $qa->get_question();
170 $answer = $question->get_correct_answer();
f77f47ec
TH
171 if (!$answer) {
172 return '';
173 }
174
eef75d4b 175 $response = str_replace('.', $question->ap->get_point(), $answer->answer);
5073fb74
TH
176 if ($question->unitdisplay != qtype_numerical::UNITNONE) {
177 $response = $question->ap->add_unit($response);
178 }
179
8566369f 180 return get_string('correctansweris', 'qtype_shortanswer', $response);
f77f47ec
TH
181 }
182}