forked from marcusgreen/moodle-qtype_wordselect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.php
213 lines (202 loc) · 8.94 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
<?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/>.
/**
* wordselect question renderer class.
*
* @package qtype_wordselect
*
* @copyright Marcus Green 2016
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Generates the output for wordselect questions.
*
* @copyright 2016 Marcus Green
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_wordselect_renderer extends qtype_with_combined_feedback_renderer {
/**
* Generate the area that contains the question text, and the controls for students to
* input their answers.
* *
* @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 $PAGE;
$question = $qa->get_question();
$PAGE->requires->js('/question/type/wordselect/selection.js');
$response = $qa->get_last_qt_data();
$correctplaces = $question->get_correct_places($question->questiontext, $question->delimitchars);
$output = html_writer::empty_tag('div', array('class' => 'introduction'));
/* this will ensure filters are applied to the introduction, done particularly for the multilang filter */
$output .= $question->format_text($question->introduction, $question->questiontextformat, $qa, 'qtype_wordselect',
'introduction', $question->id);
$output .= html_writer::empty_tag('/div');
$output .= html_writer::empty_tag('div', array('class' => 'qtext'));
foreach ($question->get_words() as $place => $word) {
$correctnoselect = false;
$wordattributes = array("role" => "checkbox");
$afterwordfeedback = '';
$wordattributes['name'] = $this->get_input_name($qa, $word, $place);
$wordattributes['id'] = $this->get_input_id($qa, $word, $place);
$iscorrectplace = $question->is_correct_place($correctplaces, $place);
$checkbox = "";
/* if the current word/place exists in the response */
$isselected = $question->is_word_selected($place, $response);
if ($isselected) {
$wordattributes['class'] = 'selected';
}
if ($isselected && $options->correctness == 1) {
if ($iscorrectplace) {
$afterwordfeedback = $this->feedback_image(1);
$wordattributes['title'] = get_string('correctresponse', 'qtype_wordselect');
$wordattributes['class'] = 'correctresponse';
} else {
$afterwordfeedback = $this->feedback_image(0);
$wordattributes['title'] = ' ' . get_string('incorrectresponse', 'qtype_wordselect');
}
} else if ($iscorrectplace) {
if ($options->correctness == 1) {
if ($options->rightanswer == 1) {
/* $options->rightanswer is the setting for the quiz
* to show the non selected correct answers
* once the attempt is complete.
* if the word is a correct answer but not selected
* and the marking is complete (correctness==1)
*/
$wordattributes['title'] = get_string('correctanswer', 'qtype_wordselect');
$wordattributes['class'] = 'correct';
$correctnoselect = true;
}
}
}
/* skip empty places when tabbing */
if ($word > "") {
$wordattributes['tabindex'] = '1';
}
if ($options->readonly) {
$wordattributes['tabindex'] = '';
if ($iscorrectplace && ($isselected == true)) {
$wordattributes['class'] = 'readonly correctresponse';
}
if ((!($iscorrectplace)) && ($isselected == true)) {
$wordattributes['class'] = 'readonly incorrect ';
}
} else {
$qasdata = $qa->get_last_qt_var($question->field($place));
/* when scrolling back and forth between questions
* previously selected value into each place. This
* is retrieved from the question_attempt_step_data
* table
*/
if (($qasdata == "on") || ($qasdata == "true")) {
$wordattributes['class'] = 'selected selectable';
$wordattributes['aria-checked'] = 'true';
} else {
$wordattributes['class'] = 'selectable';
$wordattributes['aria-checked'] = 'false';
}
$properties = array(
'type' => 'checkbox',
'name' => $wordattributes['name'],
'id' => $wordattributes['name'],
'hidden' => 'true');
if ($isselected == true) {
$properties['checked'] = "true";
$wordattributes['aria-checked'] = 'true';
}
$checkbox = html_writer::empty_tag('input', $properties);
}
/* the @ supresses error messages if selectable is empty */
if (@strpos($question->selectable, $word) !== false) {
if ($correctnoselect == true) {
$word = "[" . $word . "]";
}
$output .= $checkbox;
$output .= html_writer::tag('span', $word, $wordattributes);
$output .= $afterwordfeedback;
$output .= ' ';
} else {
/* for non selectable items such as the tags for tables etc */
$output .= ' ' . $word;
}
}
/* this ensures that any files inserted through the editor menu will display */
$output = $question->format_text($output, $question->questiontextformat, $qa, 'question', 'questiontext', $question->id);
$output .= html_writer::empty_tag('/div');
return $output;
}
/**
* Creates the name of the field/checkbox
* that identifies the selectable item
*
* @param question_attempt $qa
* @param string $word
* @param int $place
* @return string
*/
protected function get_input_name(question_attempt $qa, $word, $place) {
/* prefix is the number of this question attempt */
$qprefix = $qa->get_qt_field_name('');
$inputname = $qprefix . 'p' . ($place);
return $inputname;
}
/**
* TODO document the difference to get_input_name
* @param question_attempt $qa
* @param string $word
* @param int $place
* @return string
*/
protected function get_input_id(question_attempt $qa, $word, $place) {
return $this->get_input_name($qa, $word, $place);
}
/**
* TODO This seems to call a function in the same file with the same
* parameters implying it could be eliminated
* @param question_attempt $qa
* @return string
*/
protected function specific_feedback(question_attempt $qa) {
return $this->combined_feedback($qa);
}
/**
* Get feedback for correct/partially correct/incorrect
* @param question_attempt $qa
* @return string
*/
protected function combined_feedback(question_attempt $qa) {
$question = $qa->get_question();
$state = $qa->get_state();
if (!$state->is_finished()) {
$response = $qa->get_last_qt_data();
if (!$qa->get_question()->is_gradable_response($response)) {
return '';
}
list($notused, $state) = $qa->get_question()->grade_response($response);
}
$feedback = '';
$field = $state->get_feedback_class() . 'feedback';
$format = $state->get_feedback_class() . 'feedbackformat';
if ($question->$field) {
$feedback .= $question->format_text($question->$field, $question->$format, $qa, 'question', $field, $question->id);
}
return $feedback;
}
}