forked from dualcube/moodle-qtype_ddmatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
271 lines (225 loc) · 9.92 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Drag&drop matching question renderer class.
*
* @package qtype_ddmatch
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Generates the output for drag&drop matching questions.
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_ddmatch_renderer extends qtype_with_combined_feedback_renderer {
/**
* Generate the HTML required for a ddmatch question
*
* @param $qa question_attempt The question attempt
* @param $options question_display_options The options for display
*/
public function formulation_and_controls(question_attempt $qa, question_display_options $options) {
// We use the question quite a lot so store a reference to it once.
$question = $qa->get_question();
// Put together the basic question text and answer block.
$output = '';
$output .= $this->construct_questiontext($question->format_questiontext($qa));
$output .= $this->construct_answerblock($qa, $question, $options);
$this->page->requires->string_for_js('draganswerhere', 'qtype_ddmatch');
$this->page->requires->yui_module('moodle-qtype_ddmatch-dragdrop',
'M.qtype.ddmatch.init_dragdrop', array(array(
'questionid' => $qa->get_outer_question_div_unique_id(),
'readonly' => $options->readonly,
))
);
if ($qa->get_state() === question_state::$invalid) {
$response = $qa->get_last_qt_data();
$output .= html_writer::nonempty_tag('div',
$question->get_validation_error($response),
array('class' => 'validationerror'));
}
return $output;
}
/**
* Format the question choices for display
*
* @param question_attempt qa
*/
public function format_choices(question_attempt $qa) {
$question = $qa->get_question();
$choices = array();
foreach ($question->get_choice_order() as $key => $choiceid) {
$choice = $question->choices[$choiceid];
$choice = $question->format_text(
$choice, $question->choiceformat[$choiceid],
$qa, 'qtype_ddmatch', 'subanswer', $choiceid);
$choices[$key] = $choice;
}
return $choices;
}
public function specific_feedback(question_attempt $qa) {
return $this->combined_feedback($qa);
}
public function correct_response(question_attempt $qa) {
if ($qa->get_state()->is_correct()) {
// The answer was correct so we don't need to do anything further.
return '';
}
$question = $qa->get_question();
$stemorder = $question->get_stem_order();
$choices = $this->format_choices($qa, true);
$table = new html_table();
$table->attributes['class'] = 'generaltable correctanswertable';
$table->size = array('50%', '50%');
foreach ($stemorder as $key => $stemid) {
$row = new html_table_row();
$row->cells[] = $question->format_text($question->stems[$stemid],
$question->stemformat[$stemid], $qa,
'qtype_ddmatch', 'subquestion', $stemid);
$row->cells[] = $choices[$question->get_right_choice_for($stemid)];
$table->data[] = $row;
}
return get_string('correctansweris', 'qtype_match', html_writer::table($table));
}
/**
* Construct the question text displayed to the user
*
* @param questiontext The question text to user
* @return String the rendered question text
*/
public function construct_questiontext($questiontext) {
return html_writer::tag('div', $questiontext, array(
'class' => 'qtext',
));
}
/**
* Construct the answer block area
*
* @param question_attempt $qa
*/
public function construct_answerblock($qa, $question, $options) {
$stemorder = $question->get_stem_order();
$response = $qa->get_last_qt_data();
$choices = $this->format_choices($qa);
$o = html_writer::start_tag('div', array('class' => 'ablock'));
$o .= html_writer::start_tag('div', array('class' => 'divanswer'));
$o .= html_writer::start_tag('table', array('class' => 'answer'));
$o .= html_writer::start_tag('tbody');
$parity = 0;
$curfieldname = null;
foreach ($stemorder as $key => $stemid) {
$o .= html_writer::start_tag('tr', array('class' => 'r' . $parity));
$o .= html_writer::tag('td', $this->construct_stem_cell($qa, $question, $stemid),
array('class' => 'text'));
$classes = array('control');
$feedbackimage = '';
$curfieldname = $question->get_field_name($key);
if (array_key_exists($curfieldname, $response)) {
$selected = (int) $response[$curfieldname];
} else {
$selected = 0;
}
$fraction = (int) ($selected && $selected == $question->get_right_choice_for($stemid));
if ($options->correctness && $selected) {
$classes[] = $this->feedback_class($fraction);
$feedbackimage = $this->feedback_image($fraction);
}
$dragdropclasses = $classes;
$classes[] = 'hiddenifjs';
$dragdropclasses[] = 'visibleifjs';
$o .= html_writer::tag('td',
$this->construct_choice_cell_select($qa, $options, $choices, $stemid, $curfieldname, $selected) .
' ' . $feedbackimage, array('class' => implode(' ', $classes)));
$o .= html_writer::tag('td',
$this->construct_choice_cell_dragdrop($qa, $options, $choices, $stemid, $curfieldname, $selected) .
' ' . $feedbackimage, array('class' => implode(' ', $dragdropclasses)));
$o .= html_writer::end_tag('tr');
$parity = 1 - $parity;
}
$o .= html_writer::end_tag('tbody');
$o .= html_writer::end_tag('table');
$o .= html_writer::end_tag('div');
$o .= $this->construct_available_dragdrop_choices($qa, $question);
$o .= html_writer::end_tag('div');
$o .= html_writer::tag('div', '', array('class' => 'clearer'));
return $o;
}
private function construct_stem_cell($qa, $question, $stemid) {
return $question->format_text(
$question->stems[$stemid], $question->stemformat[$stemid],
$qa, 'qtype_ddmatch', 'subquestion', $stemid);
}
private function construct_choice_cell_select($qa, $options, $choices, $stemid, $curfieldname, $selected) {
return html_writer::select($choices, $qa->get_qt_field_name($curfieldname), $selected,
array('0' => 'choose'), array('disabled' => $options->readonly));
}
private function construct_choice_cell_dragdrop($qa, $options, $choices, $stemid, $curfieldname, $selected) {
$placeholderclasses = array('placeholder');
$li = '';
// Check whether an answer has already been selected.
if ($selected !== 0) {
// An answer has already been selected, display it as well.
$question = $qa->get_question();
$choiceorder = $question->get_choice_order();
$attributes = array(
'data-id' => $selected,
'class' => 'matchdrag copy');
$li = html_writer::tag('li', $choices[$selected], $attributes);
// Add the hidden placeholder class so that the placeholder is initially hidden.
$placeholderclasses[] = 'hidden';
}
$placeholder = html_writer::tag('li', html_writer::tag('p',
get_string('draganswerhere', 'qtype_ddmatch')), array(
'class' => implode(' ', $placeholderclasses),
));
$li = $placeholder . $li;
$question = $qa->get_question();
$attributes = array(
'id' => 'ultarget'.$question->id.'_'.$stemid,
'name' => $qa->get_qt_field_name($curfieldname),
'class' => 'matchtarget matchdefault',
'data-selectname' => $qa->get_qt_field_name($curfieldname),
);
$output = html_writer::tag('ul', $li, $attributes);
return $output;
}
/**
* Construct the list of available answers for use in the drag and drop
* interface.
*
* @param $question
* @return String
*/
public function construct_available_dragdrop_choices($qa, $question) {
$choiceorder = $question->get_choice_order();
$choices = $this->format_choices($qa, true);
$uldata = '';
foreach ($choiceorder as $key => $choiceid) {
$attributes = array(
'data-id' => $key,
'class' => 'matchdrag'
);
$li = html_writer::tag('li', $choices[$key], $attributes);
$uldata .= $li;
}
$attributes = array(
'id' => 'ulorigin' . $question->id,
'class' => 'matchorigin visibleifjs');
$o = html_writer::tag('ul', $uldata, $attributes);
return $o;
}
}