-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrenderer.php
260 lines (223 loc) · 9.5 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
<?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/>.
/**
* Knowledge check question renderer class.
*
* @package qtype_knowledgecheck
* @copyright (c) The Regents of the University of California
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Generates the output for knowledge check questions.
*
* @package qtype_knowledgecheck
* @copyright (c) The Regents of the University of California
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_knowledgecheck_renderer extends qtype_renderer {
/**
* {@inheritdoc}
*
* @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) {
$question = $qa->get_question();
$responseoutput = $this->page->get_renderer('qtype_knowledgecheck', 'format_editor');
// Answer field.
$step = $qa->get_last_step_with_qt_var('answer');
if (!$step->has_qt_var('answer') && empty($options->readonly)) {
// Question has never been answered, fill it with response template.
$step = new question_attempt_step(['answer' => $question->responsetemplate]);
}
if (empty($options->readonly)) {
$answer = $responseoutput->response_area_input('answer', $qa,
$step, $question->responsefieldlines, $options->context);
} else {
$answer = $responseoutput->response_area_read_only('answer', $qa,
$step, $question->responsefieldlines, $options->context);
}
$result = '';
$result .= html_writer::tag('div', $question->format_questiontext($qa),
['class' => 'qtext']);
$result .= html_writer::start_tag('div', ['class' => 'ablock']);
$result .= html_writer::tag('div', $answer, ['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' => $answer]),
['class' => 'validationerror']);
}
return $result;
}
/**
* {@inheritdoc}
*
* @param question_attempt $qa the question attempt to display.
* @return string HTML fragment.
*/
public function specific_feedback(question_attempt $qa) {
$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);
}
}
/**
* A format renderer for knowledge checks where the student should use the HTML editor without the file picker.
*
* Copied and modified from the Essay question type for our purposes.
*
* @package qtype_knowledgecheck
* @copyright (c) The Regents of the University of California
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_knowledgecheck_format_editor_renderer extends plugin_renderer_base {
/**
* Gets a specific class name to add to the input element.
*
* @return string specific class name to add to the input element.
*/
protected function class_name() {
return 'qtype_knowledgecheck_editor';
}
/**
* Render the student's response when the question is in read-only mode.
*
* @param string $name the variable name this input edits.
* @param question_attempt $qa the question attempt being display.
* @param question_attempt_step $step the current step.
* @param int $lines approximate size of input box to display.
* @param object $context the context teh output belongs to.
* @return string html to display the response.
*/
public function response_area_read_only($name, $qa, $step, $lines, $context) {
return html_writer::tag('div', $this->prepare_response($name, $qa, $step, $context),
['class' => $this->class_name() . ' qtype_knowledgecheck_editor readonly']);
}
/**
* Render the student's response when the question is in read-only mode.
*
* @param string $name the variable name this input edits.
* @param question_attempt $qa the question attempt being display.
* @param question_attempt_step $step the current step.
* @param int $lines approximate size of input box to display.
* @param object $context the context teh output belongs to.
* @return string html to display the response for editing.
*/
public function response_area_input($name, $qa, $step, $lines, $context) {
global $CFG;
require_once($CFG->dirroot . '/repository/lib.php');
$inputname = $qa->get_qt_field_name($name);
$responseformat = $step->get_qt_var($name . 'format');
$id = $inputname . '_id';
$editor = editors_get_preferred_editor($responseformat);
$strformats = format_text_menu();
$formats = $editor->get_supported_formats();
foreach ($formats as $fid) {
$formats[$fid] = $strformats[$fid];
}
list($draftitemid, $response) = $this->prepare_response_for_editing(
$name, $step, $context);
$editor->use_editor($id, $this->get_editor_options($context),
$this->get_filepicker_options($context, $draftitemid));
$output = '';
$output .= html_writer::start_tag('div', ['class' =>
$this->class_name() . ' qtype_knowledgecheck_response']);
$output .= html_writer::tag('div', html_writer::tag('textarea', s($response),
['id' => $id, 'name' => $inputname, 'rows' => $lines, 'cols' => 60]));
$output .= html_writer::start_tag('div');
if (count($formats) == 1) {
reset($formats);
$output .= html_writer::empty_tag('input', ['type' => 'hidden',
'name' => $inputname . 'format', 'value' => key($formats)]);
} else {
$output .= html_writer::label(get_string('format'), 'menu' . $inputname . 'format', false);
$output .= ' ';
$output .= html_writer::select($formats, $inputname . 'format', $responseformat, '');
}
$output .= html_writer::end_tag('div');
$output .= $this->filepicker_html($inputname, $draftitemid);
$output .= html_writer::end_tag('div');
return $output;
}
/**
* Prepare the response for read-only display.
*
* @param string $name the variable name this input edits.
* @param question_attempt $qa the question attempt being display.
* @param question_attempt_step $step the current step.
* @param object $context the context the attempt belongs to.
* @return string the response prepared for display.
*/
protected function prepare_response($name, question_attempt $qa,
question_attempt_step $step, $context) {
if (!$step->has_qt_var($name)) {
return '';
}
$formatoptions = new stdClass();
$formatoptions->para = false;
return format_text($step->get_qt_var($name), $step->get_qt_var($name . 'format'),
$formatoptions);
}
/**
* Prepare the response for editing.
*
* @param string $name the variable name this input edits.
* @param question_attempt_step $step the current step.
* @param object $context the context the attempt belongs to.
* @return string the response prepared for display.
*/
protected function prepare_response_for_editing($name,
question_attempt_step $step, $context) {
return [0, $step->get_qt_var($name)];
}
/**
* Get editor options for question response text area.
*
* @param object $context the context the attempt belongs to.
* @return array options for the editor.
*/
protected function get_editor_options($context) {
return ['context' => $context];
}
/**
* Get filepicker options for the editor.
*
* @param object $context the context the attempt belongs to.
* @param int $draftitemid draft item id.
* @return array filepicker options for the editor.
*/
protected function get_filepicker_options($context, $draftitemid) {
return ['return_types' => FILE_INTERNAL | FILE_EXTERNAL];
}
/**
* Get HTML for the filepicker, if used.
*
* @param string $inputname input field name.
* @param int $draftitemid draft file area itemid.
* @return string HTML for the filepicker, if used.
*/
protected function filepicker_html($inputname, $draftitemid) {
return '';
}
}