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
 * Matching question renderer class.
19
 *
20
 * @package   qtype_match
21
 * @copyright 2009 The Open University
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
 
29
/**
30
 * Generates the output for matching questions.
31
 *
32
 * @copyright 2009 The Open University
33
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class qtype_match_renderer extends qtype_with_combined_feedback_renderer {
36
 
37
    public function formulation_and_controls(question_attempt $qa,
38
            question_display_options $options) {
39
 
40
        $question = $qa->get_question();
41
        $stemorder = $question->get_stem_order();
42
        $response = $qa->get_last_qt_data();
43
 
44
        $choices = $this->format_choices($question);
45
 
1441 ariadna 46
        $questiontextid = $qa->get_qt_field_name('qtext');
1 efrain 47
        $result = '';
48
        $result .= html_writer::tag('div', $question->format_questiontext($qa),
1441 ariadna 49
                array('class' => 'qtext', 'id' => $questiontextid));
1 efrain 50
 
51
        $result .= html_writer::start_tag('div', array('class' => 'ablock'));
1441 ariadna 52
        $result .= html_writer::start_tag('table', array('class' => 'answer', 'role' => 'presentation'));
53
        $result .= html_writer::start_tag('tbody', ['role' => 'presentation']);
1 efrain 54
 
55
        $parity = 0;
56
        $i = 1;
57
        foreach ($stemorder as $key => $stemid) {
58
 
1441 ariadna 59
            $result .= html_writer::start_tag('tr', array('class' => 'r' . $parity, 'role' => 'presentation'));
1 efrain 60
            $fieldname = 'sub' . $key;
61
 
1441 ariadna 62
            $itemtextid = $qa->get_qt_field_name($fieldname . '_itemtext');
1 efrain 63
            $result .= html_writer::tag('td', $this->format_stem_text($qa, $stemid),
1441 ariadna 64
                    array('class' => 'text', 'id' => $itemtextid, 'role' => 'presentation'));
1 efrain 65
 
66
            $classes = 'control';
67
            $feedbackimage = '';
68
 
69
            if (array_key_exists($fieldname, $response)) {
70
                $selected = $response[$fieldname];
71
            } else {
72
                $selected = 0;
73
            }
74
 
75
            $fraction = (int) ($selected && $selected == $question->get_right_choice_for($stemid));
76
 
77
            if ($options->correctness && $selected) {
78
                $classes .= ' ' . $this->feedback_class($fraction);
79
                $feedbackimage = $this->feedback_image($fraction);
80
            }
81
 
1441 ariadna 82
            // We only want to add the question text to the first answer field to
83
            // avoid repetition of the question text for the subsequent answer fields.
84
            if ($i == 1) {
85
                $ariadescribedbyids = $questiontextid . ' ' . $itemtextid;
86
            } else {
87
                $ariadescribedbyids = $itemtextid;
88
            }
89
 
1 efrain 90
            $labeltext = $options->add_question_identifier_to_label(get_string('answer', 'qtype_match', $i));
91
            $result .= html_writer::tag('td',
92
                    html_writer::label($labeltext,
93
                            'menu' . $qa->get_qt_field_name('sub' . $key), false,
94
                            array('class' => 'accesshide')) .
95
                    html_writer::select($choices, $qa->get_qt_field_name('sub' . $key), $selected,
1441 ariadna 96
                            ['0' => 'choose'],
97
                            [
98
                                'disabled' => $options->readonly,
99
                                'class' => 'form-select d-inline-block ms-1',
100
                                'aria-describedby' => $ariadescribedbyids,
101
                            ]) .
102
                    ' ' . $feedbackimage, array('class' => $classes, 'role' => 'presentation'));
1 efrain 103
 
104
            $result .= html_writer::end_tag('tr');
105
            $parity = 1 - $parity;
106
            $i++;
107
        }
108
        $result .= html_writer::end_tag('tbody');
109
        $result .= html_writer::end_tag('table');
110
 
111
        $result .= html_writer::end_tag('div'); // Closes <div class="ablock">.
112
 
113
        if ($qa->get_state() == question_state::$invalid) {
114
            $result .= html_writer::nonempty_tag('div',
115
                    $question->get_validation_error($response),
116
                    array('class' => 'validationerror'));
117
        }
118
 
119
        return $result;
120
    }
121
 
122
    public function specific_feedback(question_attempt $qa) {
123
        return $this->combined_feedback($qa);
124
    }
125
 
126
    /**
127
     * Format each question stem. Overwritten by randomsamatch renderer.
128
     *
129
     * @param question_attempt $qa
130
     * @param integer $stemid stem index
131
     * @return string
132
     */
133
    public function format_stem_text($qa, $stemid) {
134
        $question = $qa->get_question();
135
        return $question->format_text(
136
                    $question->stems[$stemid], $question->stemformat[$stemid],
137
                    $qa, 'qtype_match', 'subquestion', $stemid);
138
    }
139
 
140
    protected function format_choices($question) {
141
        $choices = array();
142
        foreach ($question->get_choice_order() as $key => $choiceid) {
143
            $choices[$key] = format_string($question->choices[$choiceid]);
144
        }
145
        return $choices;
146
    }
147
 
148
    public function correct_response(question_attempt $qa) {
149
        $question = $qa->get_question();
150
        $stemorder = $question->get_stem_order();
151
 
152
        $choices = $this->format_choices($question);
153
        $right = array();
154
        foreach ($stemorder as $key => $stemid) {
155
            if (!isset($choices[$question->get_right_choice_for($stemid)])) {
156
                continue;
157
            }
158
            $right[] = $question->make_html_inline($this->format_stem_text($qa, $stemid)) . ' &#x2192; ' .
159
                    $choices[$question->get_right_choice_for($stemid)];
160
        }
161
 
162
        if (!empty($right)) {
163
            return get_string('correctansweris', 'qtype_match', implode(', ', $right));
164
        }
165
    }
166
}