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/>.
19 * Numerical question renderer class.
21 * @package qtype_numerical
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 * Generates the output for short answer questions.
30 * @copyright 2009 The Open University
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class qtype_numerical_renderer
extends qtype_renderer
{
34 public function formulation_and_controls(question_attempt
$qa,
35 question_display_options
$options) {
37 $question = $qa->get_question();
38 $currentanswer = $qa->get_last_qt_var('answer');
39 if ($question->has_separate_unit_field()) {
40 $selectedunit = $qa->get_last_qt_var('unit');
45 $inputname = $qa->get_qt_field_name('answer');
46 $inputattributes = array(
49 'value' => $currentanswer,
54 if ($options->readonly
) {
55 $inputattributes['readonly'] = 'readonly';
59 if ($options->correctness
) {
60 list($value, $unit) = $question->ap
->apply_units($currentanswer, $selectedunit);
61 $answer = $question->get_matching_answer($value);
63 $fraction = $question->apply_unit_penalty($answer->fraction
, $unit);
67 $inputattributes['class'] = $this->feedback_class($fraction);
68 $feedbackimg = $this->feedback_image($fraction);
71 $questiontext = $question->format_questiontext($qa);
73 if (preg_match('/_____+/', $questiontext, $matches)) {
74 $placeholder = $matches[0];
75 $inputattributes['size'] = round(strlen($placeholder) * 1.1);
78 $input = html_writer
::empty_tag('input', $inputattributes) . $feedbackimg;
80 if ($question->has_separate_unit_field()) {
81 if ($question->unitdisplay
== qtype_numerical
::UNITRADIO
) {
84 foreach ($question->ap
->get_unit_options() as $unit) {
85 $id = $qa->get_qt_field_name('unit') . '_' . $i++
;
86 $radioattrs = array('type' => 'radio', 'id' => $id, 'value' => $unit,
87 'name' => $qa->get_qt_field_name('unit'));
88 if ($unit == $selectedunit) {
89 $radioattrs['checked'] = 'checked';
91 $choices[] = html_writer
::tag('label',
92 html_writer
::empty_tag('input', $radioattrs) . $unit,
93 array('for' => $id, 'class' => 'unitchoice'));
96 $unitchoice = html_writer
::tag('span', implode(' ', $choices),
97 array('class' => 'unitchoices'));
99 } else if ($question->unitdisplay
== qtype_numerical
::UNITSELECT
) {
100 $unitchoice = html_writer
::select($question->ap
->get_unit_options(),
101 $qa->get_qt_field_name('unit'), $selectedunit, array(''=>'choosedots'),
102 array('disabled' => $options->readonly
));
105 if ($question->ap
->are_units_before()) {
106 $input = $unitchoice . ' ' . $input;
108 $input = $input . ' ' . $unitchoice;
113 $questiontext = substr_replace($questiontext, $input,
114 strpos($questiontext, $placeholder), strlen($placeholder));
117 $result = html_writer
::tag('div', $questiontext, array('class' => 'qtext'));
120 $result .= html_writer
::start_tag('div', array('class' => 'ablock'));
121 $result .= get_string('answer', 'qtype_shortanswer',
122 html_writer
::tag('div', $input, array('class' => 'answer')));
123 $result .= html_writer
::end_tag('div');
126 if ($qa->get_state() == question_state
::$invalid) {
127 $result .= html_writer
::nonempty_tag('div',
128 $question->get_validation_error(array('answer' => $currentanswer)),
129 array('class' => 'validationerror'));
135 public function specific_feedback(question_attempt
$qa) {
136 $question = $qa->get_question();
138 if ($question->has_separate_unit_field()) {
139 $selectedunit = $qa->get_last_qt_var('unit');
141 $selectedunit = null;
143 list($value, $unit) = $question->ap
->apply_units(
144 $qa->get_last_qt_var('answer'), $selectedunit);
145 $answer = $question->get_matching_answer($value);
147 if ($answer && $answer->feedback
) {
148 $feedback = $question->format_text($answer->feedback
, $answer->feedbackformat
,
149 $qa, 'question', 'answerfeedback', $answer->id
);
154 if ($question->unitgradingtype
&& !$question->ap
->is_known_unit($unit)) {
155 $feedback .= html_writer
::tag('p', get_string('unitincorrect', 'qtype_numerical'));
161 public function correct_response(question_attempt
$qa) {
162 $question = $qa->get_question();
163 $answer = $question->get_correct_answer();
168 $response = str_replace('.', $question->ap
->get_point(), $answer->answer
);
169 if ($question->unitdisplay
!= qtype_numerical
::UNITNONE
) {
170 $response = $question->ap
->add_unit($response);
173 return get_string('correctansweris', 'qtype_shortanswer', $response);