-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrenderer.php
269 lines (244 loc) · 11 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
<?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/>.
/**
* Regexp question renderer class.
*
* @package qtype_regexp
* @copyright 2011 Joseph REZEAU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Generates the output for regexp questions.
*
* @copyright 2011 Joseph REZEAU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_regexp_renderer extends qtype_renderer {
/**
* Generate the display of the formulation part of the question shown at runtime
* in a quiz.
*
* @param question_attempt $qa the question attempt to display.
* @param question_display_options $options controls what should and should not be displayed.
* @return string HTML fragment.
*/
public function formulation_and_controls(question_attempt $qa, question_display_options $options) {
global $CFG, $currentanswerwithhint;
require_once($CFG->dirroot.'/question/type/regexp/locallib.php');
$question = $qa->get_question();
$inputname = $qa->get_qt_field_name('answer');
$ispreview = !isset($options->attempt);
$currentanswer = remove_blanks ($qa->get_last_qt_var('answer') );
$response = $qa->get_last_qt_data();
$laststep = $qa->get_reverse_step_iterator();
$hintadded = false;
foreach ($qa->get_reverse_step_iterator() as $step) {
$hintadded = $step->has_behaviour_var('_helps') === true;
break;
}
$closest = find_closest($question, $currentanswer, $correctresponse = false, $hintadded);
$question->closest = $closest;
// If regexpadaptive behaviours replace current student response with correct beginning.
$currbehaviourname = get_class($qa->get_behaviour() );
$currstate = $qa->get_state();
// Showing / hiding regexp generated alternative sentences (for teacher only).
// Changed from javascript to print_collapsible_region OCT 2012.
// Removed for compatibility with the Embed questions plugin see https://moodle.org/plugins/filter_embedquestion.
$inputattributes = [
'type' => 'text',
'name' => $inputname,
'value' => $currentanswer,
'id' => $inputname,
'size' => 80,
'class' => 'form-control d-inline',
];
if ($options->readonly) {
$inputattributes['readonly'] = 'readonly';
}
$feedbackimg = '';
if ($options->correctness) {
$answer = $question->get_matching_answer(['answer' => $currentanswer]);
if ($answer) {
$fraction = $answer->fraction;
} else {
$fraction = 0;
}
$inputattributes['class'] .= ' ' . $this->feedback_class($fraction);
$feedbackimg = $this->feedback_image($fraction);
}
$questiontext = $question->format_questiontext($qa);
$placeholder = false;
if (preg_match('/_____+/', $questiontext, $matches)) {
$placeholder = $matches[0];
$inputattributes['size'] = round(strlen($placeholder) * 1.1);
// Added for correct display of input inside question text in the mobile version.
$inputattributes['class'] .= ' ' . 'inlineinput';
}
$input = html_writer::empty_tag('input', $inputattributes) . $feedbackimg;
if ($placeholder) {
$questiontext = substr_replace($questiontext, $input,
strpos($questiontext, $placeholder), strlen($placeholder));
}
$result = html_writer::tag('div', $questiontext, ['class' => 'qtext']);
if (!$placeholder) {
$result .= html_writer::start_tag('div', ['class' => 'ablock']);
$result .= get_string('answer', 'qtype_shortanswer',
html_writer::tag('div', $input, ['class' => 'answer']));
$result .= html_writer::end_tag('div');
}
if ($qa->get_state() == question_state::$invalid) {
$result .= html_writer::nonempty_tag('div',
$question->get_validation_error(['answer' => $currentanswer]),
['class' => 'validationerror']);
}
return $result;
}
/**
* Get feedback.
*
* @param question_attempt $qa the question attempt to display.
* @param question_display_options $options controls what should and should not be displayed.
* @return string HTML fragment.
*/
public function feedback(question_attempt $qa, question_display_options $options) {
$result = '';
$hint = null;
if ($options->feedback) {
$result .= html_writer::nonempty_tag('div', $this->specific_feedback($qa),
['class' => 'specificfeedback qtype-regexp']);
$hint = $qa->get_applicable_hint();
}
if ($options->numpartscorrect) {
$result .= html_writer::nonempty_tag('div', $this->num_parts_correct($qa),
['class' => 'numpartscorrect']);
}
if ($hint) {
$result .= $this->hint($qa, $hint);
}
if ($options->generalfeedback) {
$result .= html_writer::nonempty_tag('div', $this->general_feedback($qa),
['class' => 'generalfeedback']);
}
if ($options->rightanswer) {
$displaycorrectanswers = $this->correct_response($qa);
$result .= html_writer::nonempty_tag('div', $displaycorrectanswers,
['class' => 'rightanswer']);
}
return $result;
}
/**
* Get feedback/hint information
*
* @param question_attempt $qa
* @return string
*/
public function specific_feedback(question_attempt $qa) {
$question = $qa->get_question();
$currentanswer = remove_blanks($qa->get_last_qt_var('answer') );
$ispreview = false;
$completemessage = '';
$closestcomplete = false;
foreach ($qa->get_reverse_step_iterator() as $step) {
$hintadded = $step->has_behaviour_var('_helps') === true;
break;
}
$closest = $question->closest;
if ($hintadded) { // Hint added one letter or hint added letter and answer is complete.
$answer = $question->get_matching_answer(['answer' => $closest[0]]);
// Help has added letter OR word and answer is complete.
$isstateimprovable = $qa->get_behaviour()->is_state_improvable($qa->get_state());
if ($closest[2] == 'complete' && $isstateimprovable) {
$closestcomplete = true;
$class = '"validationerror"';
$completemessage = '<div class='.$class.'>'.get_string("clicktosubmit", "qtype_regexp").'</div>';
}
} else {
$answer = $question->get_matching_answer(['answer' => $qa->get_last_qt_var('answer')]);
}
$labelerrors = '';
$f = '';
if (!empty($closest)) {
$guesserrors = $closest[5];
if ($guesserrors) {
$labelwrongwords = '<span class="labelwrongword">'.get_string("wrongwords", "qtype_regexp").'</span>';
$labelmisplacedwords = '<span class="labelmisplacedword">'.get_string("misplacedwords", "qtype_regexp").'</span>';
switch ($guesserrors) {
case 1 :
$labelerrors = '<div>'.$labelmisplacedwords.'</div>';
break;
case 10 :
$labelerrors = '<div>'.$labelwrongwords.'</div>';
break;
case 11 :
$labelerrors = '<div>'.$labelwrongwords. ' '. $labelmisplacedwords.'</div>';
break;
}
}
// Student's response with corrections to be displayed in feedback div.
$f = '<div><span class="correctword">'.$closest[1].'<strong>'.$closest[4].'</strong></span> '.$closest[3].'</div>';
}
if ($closest[2] == 'complete') {
$answer->feedback = '';
}
if ($answer && $answer->feedback || $closestcomplete == true) {
return $question->format_text($f.$labelerrors.$answer->feedback.$completemessage,
$answer->feedbackformat, $qa, 'question', 'answerfeedback', $answer->id);
} else {
return $f.$labelerrors;
}
}
/**
* Get correct response
*
* @param question_attempt $qa
* @return string
*/
public function correct_response(question_attempt $qa) {
$question = $qa->get_question();
$displayresponses = '';
$alternateanswers = get_alternateanswers($question);
$bestcorrectanswer = $alternateanswers[1]['answers'][0];
if (count($alternateanswers) == 1 ) { // No alternative answers besides the only "correct" answer.
$displayresponses .= get_string('correctansweris', 'qtype_regexp', $bestcorrectanswer);
// No need to display alternate answers!
return $displayresponses;
} else {
$displayresponses .= get_string('bestcorrectansweris', 'qtype_regexp', $bestcorrectanswer).'<br />';
}
// Teacher can always view alternate answers; student can only view if question is set to studentshowalternate.
$canview = question_has_capability_on($question, 'view');
if ($question->studentshowalternate || $canview) {
$displayresponses .= print_collapsible_region_start('expandalternateanswers', 'id'.
$question->id, get_string('showhidealternate', 'qtype_regexp'),
'showhidealternate', true, true);
foreach ($alternateanswers as $key => $alternateanswer) {
if ($key == 1) { // First (correct) Answer.
if (count($alternateanswers) > 1) {
$displayresponses .= get_string('correctanswersare', 'qtype_regexp').'<br />';
}
} else {
$fraction = $alternateanswer['fraction'];
$displayresponses .= "<strong>$fraction</strong><br />";
foreach ($alternateanswer['answers'] as $alternate) {
$displayresponses .= $alternate.'<br />';
}
}
}
$displayresponses .= print_collapsible_region_end(true);
}
return $displayresponses;
}
}