1 |
efrain |
1 |
<?php
|
|
|
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 |
*/
|
|
|
33 |
class 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');
|
|
|
39 |
if ($question->has_separate_unit_field()) {
|
|
|
40 |
$selectedunit = $qa->get_last_qt_var('unit');
|
|
|
41 |
} else {
|
|
|
42 |
$selectedunit = null;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
$inputname = $qa->get_qt_field_name('answer');
|
|
|
46 |
$inputattributes = array(
|
|
|
47 |
'type' => 'text',
|
|
|
48 |
'name' => $inputname,
|
|
|
49 |
'value' => $currentanswer,
|
|
|
50 |
'id' => $inputname,
|
|
|
51 |
'size' => 30,
|
|
|
52 |
'class' => 'form-control d-inline',
|
|
|
53 |
);
|
|
|
54 |
|
|
|
55 |
if ($options->readonly) {
|
|
|
56 |
$inputattributes['readonly'] = 'readonly';
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
$feedbackimg = '';
|
|
|
60 |
if ($options->correctness) {
|
|
|
61 |
list($value, $unit, $multiplier) = $question->ap->apply_units(
|
|
|
62 |
$currentanswer, $selectedunit);
|
|
|
63 |
$answer = $question->get_matching_answer($value, $multiplier);
|
|
|
64 |
if ($answer) {
|
|
|
65 |
$unitisright = $question->is_unit_right($answer, $value, $multiplier);
|
|
|
66 |
$fraction = $question->apply_unit_penalty($answer->fraction, $unitisright);
|
|
|
67 |
} else {
|
|
|
68 |
$fraction = 0;
|
|
|
69 |
}
|
|
|
70 |
$inputattributes['class'] .= ' ' . $this->feedback_class($fraction);
|
|
|
71 |
$feedbackimg = $this->feedback_image($fraction);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
$questiontext = $question->format_questiontext($qa);
|
|
|
75 |
$placeholder = false;
|
|
|
76 |
if (preg_match('/_____+/', $questiontext, $matches)) {
|
|
|
77 |
$placeholder = $matches[0];
|
|
|
78 |
$inputattributes['size'] = round(strlen($placeholder) * 1.1);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
$input = html_writer::empty_tag('input', $inputattributes) . $feedbackimg;
|
|
|
82 |
|
|
|
83 |
if ($question->has_separate_unit_field()) {
|
|
|
84 |
if ($question->unitdisplay == qtype_numerical::UNITRADIO) {
|
|
|
85 |
$choices = array();
|
|
|
86 |
$i = 1;
|
|
|
87 |
foreach ($question->ap->get_unit_options() as $unit) {
|
|
|
88 |
$id = $qa->get_qt_field_name('unit') . '_' . $i++;
|
|
|
89 |
$radioattrs = array('type' => 'radio', 'id' => $id, 'value' => $unit,
|
|
|
90 |
'name' => $qa->get_qt_field_name('unit'));
|
|
|
91 |
if ($unit == $selectedunit) {
|
|
|
92 |
$radioattrs['checked'] = 'checked';
|
|
|
93 |
}
|
|
|
94 |
$choices[] = html_writer::tag('label',
|
|
|
95 |
html_writer::empty_tag('input', $radioattrs) . $unit,
|
|
|
96 |
array('for' => $id, 'class' => 'unitchoice'));
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
$unitchoice = html_writer::tag('span', implode(' ', $choices),
|
|
|
100 |
array('class' => 'unitchoices'));
|
|
|
101 |
|
|
|
102 |
} else if ($question->unitdisplay == qtype_numerical::UNITSELECT) {
|
|
|
103 |
$unitchoice = html_writer::label(get_string('selectunit', 'qtype_numerical'),
|
|
|
104 |
'menu' . $qa->get_qt_field_name('unit'), false, array('class' => 'accesshide'));
|
|
|
105 |
$unitchoice .= html_writer::select($question->ap->get_unit_options(),
|
|
|
106 |
$qa->get_qt_field_name('unit'), $selectedunit, array(''=>'choosedots'),
|
|
|
107 |
array('disabled' => $options->readonly));
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
if ($question->ap->are_units_before()) {
|
|
|
111 |
$input = $unitchoice . ' ' . $input;
|
|
|
112 |
} else {
|
|
|
113 |
$input = $input . ' ' . $unitchoice;
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
if ($placeholder) {
|
|
|
118 |
$inputinplace = html_writer::tag('label', $options->add_question_identifier_to_label(get_string('answer')),
|
|
|
119 |
array('for' => $inputattributes['id'], 'class' => 'sr-only'));
|
|
|
120 |
$inputinplace .= $input;
|
|
|
121 |
$questiontext = substr_replace($questiontext, $inputinplace,
|
|
|
122 |
strpos($questiontext, $placeholder), strlen($placeholder));
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
$result = html_writer::tag('div', $questiontext, array('class' => 'qtext'));
|
|
|
126 |
|
|
|
127 |
if (!$placeholder) {
|
|
|
128 |
$result .= html_writer::start_tag('div', ['class' => 'ablock d-flex flex-wrap align-items-center']);
|
|
|
129 |
$label = $options->add_question_identifier_to_label(get_string('answercolon', 'qtype_numerical'), true);
|
|
|
130 |
$result .= html_writer::tag('label', $label,
|
|
|
131 |
array('for' => $inputattributes['id']));
|
|
|
132 |
$result .= html_writer::tag('span', $input, array('class' => 'answer'));
|
|
|
133 |
$result .= html_writer::end_tag('div');
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
if ($qa->get_state() == question_state::$invalid) {
|
|
|
137 |
$result .= html_writer::nonempty_tag('div',
|
|
|
138 |
$question->get_validation_error(array('answer' => $currentanswer, 'unit' => $selectedunit)),
|
|
|
139 |
array('class' => 'validationerror'));
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
return $result;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
public function specific_feedback(question_attempt $qa) {
|
|
|
146 |
$question = $qa->get_question();
|
|
|
147 |
|
|
|
148 |
if ($question->has_separate_unit_field()) {
|
|
|
149 |
$selectedunit = $qa->get_last_qt_var('unit');
|
|
|
150 |
} else {
|
|
|
151 |
$selectedunit = null;
|
|
|
152 |
}
|
|
|
153 |
list($value, $unit, $multiplier) = $question->ap->apply_units(
|
|
|
154 |
$qa->get_last_qt_var('answer'), $selectedunit);
|
|
|
155 |
$answer = $question->get_matching_answer($value, $multiplier);
|
|
|
156 |
|
|
|
157 |
if ($answer && $answer->feedback) {
|
|
|
158 |
$feedback = $question->format_text($answer->feedback, $answer->feedbackformat,
|
|
|
159 |
$qa, 'question', 'answerfeedback', $answer->id);
|
|
|
160 |
} else {
|
|
|
161 |
$feedback = '';
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
if ($question->unitgradingtype && !$question->ap->is_known_unit($unit)) {
|
|
|
165 |
$feedback .= html_writer::tag('p', get_string('unitincorrect', 'qtype_numerical'));
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
return $feedback;
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
public function correct_response(question_attempt $qa) {
|
|
|
172 |
$question = $qa->get_question();
|
|
|
173 |
$answer = $question->get_correct_answer();
|
|
|
174 |
if (!$answer) {
|
|
|
175 |
return '';
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
$response = str_replace('.', $question->ap->get_point(), $answer->answer);
|
|
|
179 |
if ($question->unitdisplay != qtype_numerical::UNITNONE) {
|
|
|
180 |
$response = $question->ap->add_unit($response);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
return get_string('correctansweris', 'qtype_shortanswer', $response);
|
|
|
184 |
}
|
|
|
185 |
}
|