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
 * Drag-and-drop words into sentences question renderer class.
19
 *
20
 * @package   qtype_ddwtos
21
 * @copyright 2010 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
require_once($CFG->dirroot . '/question/type/gapselect/rendererbase.php');
29
 
30
 
31
/**
32
 * Generates the output for drag-and-drop words into sentences questions.
33
 *
34
 * @copyright  2010 The Open University
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class qtype_ddwtos_renderer extends qtype_elements_embedded_in_question_text_renderer {
38
 
39
    public function formulation_and_controls(question_attempt $qa,
40
            question_display_options $options) {
41
 
42
        $result = parent::formulation_and_controls($qa, $options);
43
 
44
        $this->page->requires->js_call_amd('qtype_ddwtos/ddwtos', 'init',
45
                [$qa->get_outer_question_div_unique_id(), $options->readonly]);
46
        return $result;
47
    }
48
 
49
    protected function post_qtext_elements(question_attempt $qa,
50
            question_display_options $options) {
51
        $result = '';
52
        $question = $qa->get_question();
53
 
54
        $dragboxs = '';
55
        foreach ($question->choices as $group => $choices) {
56
            $dragboxs .= $this->drag_boxes($qa, $group,
57
                    $question->get_ordered_choices($group), $options);
58
        }
59
 
60
        $classes = array('answercontainer');
61
        if ($options->readonly) {
62
            $classes[] = 'readonly';
63
        }
64
        $result .= html_writer::tag('div', $dragboxs, array('class' => implode(' ', $classes)));
65
 
66
        // We abuse the clear_wrong method to output the hidden form fields we
67
        // want irrespective of whether we are actually clearing the wrong
68
        // bits of the response.
69
        if (!$options->clearwrong) {
70
            $result .= $this->clear_wrong($qa, false);
71
        }
72
        return $result;
73
    }
74
 
75
    protected function embedded_element(question_attempt $qa, $place,
76
            question_display_options $options) {
77
        $question = $qa->get_question();
78
        $group = $question->places[$place];
79
        $label = $options->add_question_identifier_to_label(get_string('blanknumber', 'qtype_ddwtos', $place));
80
        $boxcontents = '&#160;' . html_writer::tag('span', $label, array('class' => 'accesshide'));
81
 
82
        $value = $qa->get_last_qt_var($question->field($place));
83
 
84
        $attributes = array(
85
            'class' => 'place' . $place . ' drop active group' . $group
86
        );
87
 
88
        if ($options->readonly) {
89
            $attributes['class'] .= ' readonly';
90
        } else {
91
            $attributes['tabindex'] = '0';
92
        }
93
 
94
        $feedbackimage = '';
95
        if ($options->correctness) {
96
            $response = $qa->get_last_qt_data();
97
            $fieldname = $question->field($place);
98
            if (array_key_exists($fieldname, $response)) {
99
                $fraction = (int) ($response[$fieldname] ==
100
                        $question->get_right_choice_for($place));
101
                $feedbackimage = $this->feedback_image($fraction);
102
            }
103
        }
104
 
105
        return html_writer::tag('span', $boxcontents, $attributes) . ' ' . $feedbackimage;
106
    }
107
 
108
    protected function drag_boxes($qa, $group, $choices, question_display_options $options) {
109
        $boxes = '';
1441 ariadna 110
 
1 efrain 111
        foreach ($choices as $key => $choice) {
1441 ariadna 112
            $content = question_utils::format_question_fragment($choice->text, $this->page->context);
1 efrain 113
            $infinite = '';
114
            if ($choice->infinite) {
115
                $infinite = ' infinite';
116
            }
117
 
118
            $boxes .= html_writer::tag('span', $content, [
119
                    'class' => 'draghome user-select-none choice' . $key . ' group' .
120
                            $choice->draggroup . $infinite]) . ' ';
121
        }
122
 
123
        return html_writer::nonempty_tag('div', $boxes,
124
            ['class' => 'user-select-none draggrouphomes' . $choice->draggroup]);
125
    }
126
 
127
    /**
128
     * Actually, this question type abuses this method to always output the
129
     * hidden fields it needs.
130
     *
131
     * @param question_attempt $qa the question attempt.
132
     * @param bool $reallyclear whether we are really clearing the responses, or just outputting them.
133
     * @return string HTML to output.
134
     */
135
    public function clear_wrong(question_attempt $qa, $reallyclear = true) {
136
        $question = $qa->get_question();
137
        $response = $qa->get_last_qt_data();
138
 
139
        if (!empty($response) && $reallyclear) {
140
            $cleanresponse = $question->clear_wrong_from_response($response);
141
        } else {
142
            $cleanresponse = $response;
143
        }
144
 
145
        $output = '';
146
        foreach ($question->places as $place => $group) {
147
            $fieldname = $question->field($place);
148
            if (array_key_exists($fieldname, $response)) {
149
                $value = (string) $response[$fieldname];
150
            } else {
151
                $value = '0';
152
            }
153
            if (array_key_exists($fieldname, $cleanresponse)) {
154
                $cleanvalue = (string) $cleanresponse[$fieldname];
155
            } else {
156
                $cleanvalue = '0';
157
            }
158
            if ($cleanvalue === $value) {
159
                // Normal case: just one hidden input, to store the
160
                // current value and be the value submitted.
161
                $output .= html_writer::empty_tag('input', array(
162
                        'type' => 'hidden',
163
                        'id' => $this->box_id($qa, 'p' . $place),
164
                        'class' => 'placeinput place' . $place . ' group' . $group,
165
                        'name' => $qa->get_qt_field_name($fieldname),
166
                        'value' => s($value)));
167
            } else {
168
                // The case, which only happens when the question is read-only, where
169
                // we want to show the drag item in a given place (first hidden input),
170
                // but when submitted, we want it to go to a different place (second input).
171
                $output .= html_writer::empty_tag('input', array(
172
                        'type' => 'hidden',
173
                        'id' => $this->box_id($qa, 'p' . $place),
174
                        'class' => 'placeinput place' . $place . ' group' . $group,
175
                        'value' => s($value))) .
176
                        html_writer::empty_tag('input', array(
177
                        'type' => 'hidden',
178
                        'name' => $qa->get_qt_field_name($fieldname),
179
                        'value' => s($cleanvalue)));
180
            }
181
        }
182
        return $output;
183
    }
184
 
185
}