Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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