Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
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/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
16
 
-
 
17
 
16
 
18
/**
17
/**
19
 * Numerical question renderer class.
18
 * Numerical question renderer class.
20
 *
19
 *
21
 * @package qtype_numerical
20
 * @package qtype_numerical
22
 * @copyright 2009 The Open University
21
 * @copyright 2009 The Open University
23
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Línea 24... Línea -...
24
 */
-
 
25
 
23
 */
26
 
24
 
27
/**
25
/**
28
 * Generates the output for short answer questions.
26
 * Generates the output for numeric questions.
29
 *
27
 *
30
 * @copyright 2009 The Open University
28
 * @copyright 2009 The Open University
31
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
30
 */
33
class qtype_numerical_renderer extends qtype_renderer {
-
 
34
    public function formulation_and_controls(question_attempt $qa,
-
 
35
            question_display_options $options) {
31
class qtype_numerical_renderer extends qtype_renderer {
36
 
32
    public function formulation_and_controls(question_attempt $qa, question_display_options $options) {
37
        $question = $qa->get_question();
33
        $question = $qa->get_question();
38
        $currentanswer = $qa->get_last_qt_var('answer');
34
        $currentanswer = $qa->get_last_qt_var('answer');
39
        if ($question->has_separate_unit_field()) {
35
        if ($question->has_separate_unit_field()) {
40
            $selectedunit = $qa->get_last_qt_var('unit');
36
            $selectedunit = $qa->get_last_qt_var('unit');
41
        } else {
37
        } else {
Línea 42... Línea 38...
42
            $selectedunit = null;
38
            $selectedunit = null;
43
        }
39
        }
44
 
40
 
45
        $inputname = $qa->get_qt_field_name('answer');
41
        $inputname = $qa->get_qt_field_name('answer');
46
        $inputattributes = array(
42
        $inputattributes = [
47
            'type' => 'text',
43
            'type' => 'text',
48
            'name' => $inputname,
44
            'name' => $inputname,
49
            'value' => $currentanswer,
45
            'value' => $currentanswer,
50
            'id' => $inputname,
46
            'id' => $inputname,
Línea 51... Línea 47...
51
            'size' => 30,
47
            'size' => 30,
52
            'class' => 'form-control d-inline',
48
            'class' => 'form-control d-inline',
53
        );
49
        ];
Línea 54... Línea 50...
54
 
50
 
55
        if ($options->readonly) {
51
        if ($options->readonly) {
56
            $inputattributes['readonly'] = 'readonly';
52
            $inputattributes['readonly'] = 'readonly';
57
        }
-
 
58
 
53
        }
59
        $feedbackimg = '';
54
 
60
        if ($options->correctness) {
55
        $feedbackimg = '';
61
            list($value, $unit, $multiplier) = $question->ap->apply_units(
56
        if ($options->correctness) {
62
                    $currentanswer, $selectedunit);
57
            [$value, $unit, $multiplier] = $question->ap->apply_units($currentanswer, $selectedunit);
Línea 80... Línea 75...
80
 
75
 
Línea 81... Línea 76...
81
        $input = html_writer::empty_tag('input', $inputattributes) . $feedbackimg;
76
        $input = html_writer::empty_tag('input', $inputattributes) . $feedbackimg;
82
 
77
 
83
        if ($question->has_separate_unit_field()) {
78
        if ($question->has_separate_unit_field()) {
84
            if ($question->unitdisplay == qtype_numerical::UNITRADIO) {
79
            if ($question->unitdisplay == qtype_numerical::UNITRADIO) {
85
                $choices = array();
80
                $choices = [];
86
                $i = 1;
81
                $i = 1;
87
                foreach ($question->ap->get_unit_options() as $unit) {
82
                foreach ($question->ap->get_unit_options() as $unit) {
88
                    $id = $qa->get_qt_field_name('unit') . '_' . $i++;
83
                    $id = $qa->get_qt_field_name('unit') . '_' . $i++;
89
                    $radioattrs = array('type' => 'radio', 'id' => $id, 'value' => $unit,
84
                    $radioattrs = ['type' => 'radio', 'id' => $id, 'value' => $unit,
90
                            'name' => $qa->get_qt_field_name('unit'));
85
                            'name' => $qa->get_qt_field_name('unit')];
91
                    if ($unit == $selectedunit) {
86
                    if ($unit == $selectedunit) {
92
                        $radioattrs['checked'] = 'checked';
87
                        $radioattrs['checked'] = 'checked';
-
 
88
                    }
93
                    }
89
                    $choices[] = html_writer::tag(
94
                    $choices[] = html_writer::tag('label',
90
                        'label',
-
 
91
                        html_writer::empty_tag('input', $radioattrs) . $unit,
95
                            html_writer::empty_tag('input', $radioattrs) . $unit,
92
                        ['for' => $id, 'class' => 'unitchoice']
Línea 96... Línea 93...
96
                            array('for' => $id, 'class' => 'unitchoice'));
93
                    );
97
                }
-
 
98
 
-
 
99
                $unitchoice = html_writer::tag('span', implode(' ', $choices),
94
                }
-
 
95
 
100
                        array('class' => 'unitchoices'));
96
                $unitchoice = html_writer::tag('span', implode(' ', $choices), ['class' => 'unitchoices']);
101
 
97
            } else if ($question->unitdisplay == qtype_numerical::UNITSELECT) {
-
 
98
                $unitchoice = html_writer::label(
-
 
99
                    get_string('selectunit', 'qtype_numerical'),
-
 
100
                    'menu' . $qa->get_qt_field_name('unit'),
-
 
101
                    false,
102
            } else if ($question->unitdisplay == qtype_numerical::UNITSELECT) {
102
                    ['class' => 'accesshide']
103
                $unitchoice = html_writer::label(get_string('selectunit', 'qtype_numerical'),
103
                );
-
 
104
                $unitchoice .= html_writer::select(
-
 
105
                    $question->ap->get_unit_options(),
104
                        'menu' . $qa->get_qt_field_name('unit'), false, array('class' => 'accesshide'));
106
                    $qa->get_qt_field_name('unit'),
-
 
107
                    $selectedunit,
105
                $unitchoice .= html_writer::select($question->ap->get_unit_options(),
108
                    ['' => 'choosedots'],
Línea 106... Línea 109...
106
                        $qa->get_qt_field_name('unit'), $selectedunit, array(''=>'choosedots'),
109
                    ['disabled' => $options->readonly, 'class' => 'd-inline-block']
107
                        array('disabled' => $options->readonly));
110
                );
108
            }
111
            }
109
 
112
 
110
            if ($question->ap->are_units_before()) {
113
            if ($question->ap->are_units_before()) {
111
                $input = $unitchoice . ' ' . $input;
114
                $input = $unitchoice . ' ' . $input;
Línea 112... Línea 115...
112
            } else {
115
            } else {
-
 
116
                $input = $input . ' ' . $unitchoice;
-
 
117
            }
113
                $input = $input . ' ' . $unitchoice;
118
        }
114
            }
119
 
-
 
120
        if ($placeholder) {
115
        }
121
            $inputinplace = html_writer::tag(
116
 
122
                'label',
-
 
123
                $options->add_question_identifier_to_label(get_string('answer')),
-
 
124
                ['for' => $inputattributes['id'], 'class' => 'visually-hidden']
117
        if ($placeholder) {
125
            );
-
 
126
            $inputinplace .= $input;
-
 
127
            $questiontext = substr_replace(
118
            $inputinplace = html_writer::tag('label', $options->add_question_identifier_to_label(get_string('answer')),
128
                $questiontext,
Línea 119... Línea 129...
119
                    array('for' => $inputattributes['id'], 'class' => 'sr-only'));
129
                $inputinplace,
Línea 120... Línea 130...
120
            $inputinplace .= $input;
130
                strpos($questiontext, $placeholder),
121
            $questiontext = substr_replace($questiontext, $inputinplace,
131
                strlen($placeholder)
122
                    strpos($questiontext, $placeholder), strlen($placeholder));
132
            );
123
        }
133
        }
124
 
-
 
125
        $result = html_writer::tag('div', $questiontext, array('class' => 'qtext'));
134
 
126
 
135
        $result = html_writer::tag('div', $questiontext, ['class' => 'qtext']);
127
        if (!$placeholder) {
136
 
Línea 128... Línea 137...
128
            $result .= html_writer::start_tag('div', ['class' => 'ablock d-flex flex-wrap align-items-center']);
137
        if (!$placeholder) {
129
            $label = $options->add_question_identifier_to_label(get_string('answercolon', 'qtype_numerical'), true);
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);
130
            $result .= html_writer::tag('label', $label,
140
            $result .= html_writer::tag('label', $label, ['for' => $inputattributes['id']]);
131
                array('for' => $inputattributes['id']));
141
            $result .= html_writer::tag('span', $input, ['class' => 'answer']);
-
 
142
            $result .= html_writer::end_tag('div');
132
            $result .= html_writer::tag('span', $input, array('class' => 'answer'));
143
        }
Línea 133... Línea 144...
133
            $result .= html_writer::end_tag('div');
144
 
134
        }
145
        if ($qa->get_state() == question_state::$invalid) {
Línea 148... Línea 159...
148
        if ($question->has_separate_unit_field()) {
159
        if ($question->has_separate_unit_field()) {
149
            $selectedunit = $qa->get_last_qt_var('unit');
160
            $selectedunit = $qa->get_last_qt_var('unit');
150
        } else {
161
        } else {
151
            $selectedunit = null;
162
            $selectedunit = null;
152
        }
163
        }
153
        list($value, $unit, $multiplier) = $question->ap->apply_units(
164
        [$value, $unit, $multiplier] = $question->ap->apply_units($qa->get_last_qt_var('answer'), $selectedunit);
154
                $qa->get_last_qt_var('answer'), $selectedunit);
-
 
155
        $answer = $question->get_matching_answer($value, $multiplier);
165
        $answer = $question->get_matching_answer($value, $multiplier);
Línea 156... Línea 166...
156
 
166
 
157
        if ($answer && $answer->feedback) {
167
        if ($answer && $answer->feedback) {
-
 
168
            $feedback = $question->format_text(
-
 
169
                $answer->feedback,
-
 
170
                $answer->feedbackformat,
-
 
171
                $qa,
158
            $feedback = $question->format_text($answer->feedback, $answer->feedbackformat,
172
                'question',
-
 
173
                'answerfeedback',
-
 
174
                $answer->id
159
                    $qa, 'question', 'answerfeedback', $answer->id);
175
            );
160
        } else {
176
        } else {
161
            $feedback = '';
177
            $feedback = '';
Línea 162... Línea 178...
162
        }
178
        }