-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathrenderer.php
264 lines (234 loc) · 10.4 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
<?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/>.
use qtype_pmatch\local\spell\qtype_pmatch_spell_checker;
/**
* Pattern-match question renderer class.
*
* @package qtype_pmatch
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_pmatch_renderer extends qtype_renderer {
public function formulation_and_controls(question_attempt $qa, question_display_options $options) {
/** @var qtype_pmatch_question $question */
$question = $qa->get_question();
$currentanswer = $question->modify_current_answer($qa->get_last_qt_var('answer'), $options);
$inputname = $qa->get_qt_field_name('answer');
$attributes = [
'class' => 'answerinputfield',
'name' => $inputname,
'id' => $inputname,
'aria-labelledby' => $inputname . '-label',
];
$attributes = array_merge($attributes, $this->display_spellcheck($question));
if ($options->readonly) {
$attributes['readonly'] = 'readonly';
}
$feedbackimg = '';
if ($options->correctness) {
$answer = $question->get_matching_answer(['answer' => $currentanswer]);
if ($answer) {
$fraction = $answer->fraction;
} else {
$fraction = 0;
}
$attributes['class'] .= ' '.$this->feedback_class($fraction);
$feedbackimg = $this->feedback_image($fraction);
}
$htmlresponse = $question->allowsubscript || $question->allowsuperscript;
if ($htmlresponse) {
$editor = get_texteditor('ousupsub');
if ($editor === false) {
$htmlresponse = false;
}
}
// Distinguish between answer input with or without supsub editor (used for mobileApp).
if ($htmlresponse) {
$attributes['class'] .= " answer-supsub";
}
$questiontext = $question->format_questiontext($qa);
$rows = 2;
$cols = 50;
$placeholder = false;
if (preg_match('/__(\d+)x(\d+)__/i', $questiontext, $matches)) {
$placeholder = $matches[0];
$cols = $matches[1];
$rows = $matches[2];
} else {
if (preg_match('/__([0-9]+)__/', $questiontext, $matches)) {
$placeholder = $matches[0];
$cols = $matches[1];
} else if (preg_match('/_____+/', $questiontext, $matches)) {
$placeholder = $matches[0];
$cols = strlen($placeholder);
}
}
$rows = round($rows * 1.1);
$cols = round($cols * 1.1);
if ($htmlresponse && $options->readonly) {
$input = html_writer::tag('span', $currentanswer, $attributes) . $feedbackimg;
} else if ($htmlresponse) {
$attributes['rows'] = 2;
$attributes['cols'] = $cols;
$input = html_writer::tag('textarea', $currentanswer, $attributes) . $feedbackimg;
} else if ($rows > 1) {
$attributes['rows'] = $rows;
$attributes['cols'] = $cols;
$input = html_writer::tag('textarea', $currentanswer, $attributes) . $feedbackimg;
} else {
$inputattributes = [
'type' => 'text',
'value' => $currentanswer
];
$inputattributes['size'] = $cols;
$input = html_writer::empty_tag('input', $inputattributes + $attributes) . $feedbackimg;
}
$resetbutton = $this->reset_button($question, $options, $qa->get_qt_field_name('resetbutton'), $inputname);
if ($placeholder) {
$inputinplace = html_writer::tag('label', get_string('answer'),
['for' => $attributes['id'], 'class' => 'accesshide']);
$inputinplace .= $input . $resetbutton;
$questiontext = substr_replace($questiontext, $inputinplace,
strpos($questiontext, $placeholder), strlen($placeholder));
}
$result = $this->question_tests_link($question, $options);
$result .= html_writer::tag('div', $questiontext, ['class' => 'qtext']);
if (!$placeholder) {
$result .= html_writer::start_tag('div', ['class' => 'ablock', 'id' => $inputname . '-label']);
$result .= html_writer::tag('label', get_string('answercolon', 'qtype_numerical'), ['for' => $attributes['id']]);
$result .= html_writer::tag('div', $input, ['class' => 'answer']) . $resetbutton;
$result .= html_writer::end_tag('div');
}
if ($htmlresponse && !$options->readonly) {
if ($question->allowsubscript && $question->allowsuperscript) {
$supsub = 'both';
} else if ($question->allowsuperscript) {
$supsub = 'sup';
} else if ($question->allowsubscript) {
$supsub = 'sub';
}
$editor->use_editor($attributes['id'], ['supsub' => $supsub]);
}
if ($qa->get_state() == question_state::$invalid) {
$result .= html_writer::nonempty_tag('div',
$question->get_validation_error(['answer' => $currentanswer]),
['class' => 'validationerror']);
}
// Show the error if the question is using a language that does not available on the server.
if ($question->user_can_see_missing_dict_warning() && $question->is_spell_check_laguage_available()) {
$missinglangname = qtype_pmatch_spell_checker::get_display_name_for_language_code($question->applydictionarycheck);
$result .= html_writer::nonempty_tag('div',
get_string('apply_spellchecker_missing_language_attempt', 'qtype_pmatch', $missinglangname),
['class' => 'validationerror']);
}
return $result;
}
/**
* Render a button to reset the response input to the template.
*
* @param qtype_pmatch_question $question the pmatch question
* @param question_display_options $options display options.
* @param string $resetbuttonid id to use for this reset button.
* @param string $inputname id for the input the button should affect.
* @return string HTML to output.
*/
public function reset_button(
qtype_pmatch_question $question,
question_display_options $options,
string $resetbuttonid,
string $inputname,
): string {
if ($options->readonly || !$question->responsetemplate) {
return '';
}
$this->page->requires->js_call_amd('qtype_pmatch/reset_button', 'initResetButton', [
$resetbuttonid,
$inputname,
$question->responsetemplate,
]);
return html_writer::tag(
'button',
$options->add_question_identifier_to_label(get_string('reset', 'core'), true),
[
'id' => $resetbuttonid,
'class' => 'submit btn btn-secondary align-middle ml-1',
'type' => 'button',
],
);
}
public function specific_feedback(question_attempt $qa) {
/** @var qtype_pmatch_question $question */
$question = $qa->get_question();
$answer = $question->get_matching_answer(['answer' => $qa->get_last_qt_var('answer')]);
if (!$answer || !$answer->feedback) {
return '';
}
return $question->format_text($answer->feedback, $answer->feedbackformat,
$qa, 'question', 'answerfeedback', $answer->id);
}
public function correct_response(question_attempt $qa) {
return '';
}
/**
* Displays a link to run the question tests, if applicable.
* @param qtype_pmatch_question $question
* @param question_display_options $options
* @return string HTML fragment.
*/
public function question_tests_link(qtype_pmatch_question $question, question_display_options $options) {
if (!empty($options->suppressruntestslink)) {
return '';
}
if (!$question->user_can_view()) {
return '';
}
$link = html_writer::link(new moodle_url(
'/question/type/pmatch/testquestion.php', ['id' => $question->id]),
get_string('testthisquestion', 'qtype_pmatch'));
return html_writer::tag('div', $link, ['class' => 'questiontestslink']);
}
public function back_to_test_question_link($qid) {
return html_writer::tag('p', html_writer::link(
new moodle_url('/question/type/pmatch/testquestion.php', ['id' => $qid]),
get_string('testquestionbacklink', 'qtype_pmatch')));
}
public function display_feedback($feedback) {
$html = html_writer::tag('p', html_writer::div(
get_string('savedxresponses', 'qtype_pmatch', ($feedback->saved))));
$total = count($feedback->duplicates) + count($feedback->problems);
if ($total) {
$html .= html_writer::div(get_string('xresponsesproblems', 'qtype_pmatch',
$total));
$feebacklist = array_merge($feedback->duplicates, $feedback->problems);
$html .= html_writer::alist($feebacklist);
}
return $html;
}
/**
* Check whether we want to display spell check
*
* @param qtype_pmatch_question $question object that contain question properties.
* @return array spell-check attribute.
*/
public function display_spellcheck(qtype_pmatch_question $question): array {
$attribute['spellcheck'] = 'false';
if ($question->applydictionarycheck !== qtype_pmatch_spell_checker::DO_NOT_CHECK_OPTION) {
$attribute['spellcheck'] = 'true';
$attribute['lang'] = str_replace('_', '-', $question->applydictionarycheck);
}
return $attribute;
}
}