-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrenderer.php
229 lines (204 loc) · 8.36 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
<?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/>.
/**
* Draw lines question renderer class.
*
* @package qtype_drawlines
* @copyright 2024 The Open University
* @author The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use qtype_drawlines\line;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/question/type/rendererbase.php');
/**
* Generates the output for draw lines questions.
*
* @copyright 2024 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_drawlines_renderer extends qtype_with_combined_feedback_renderer {
/**
* Returns the URL for an image
*
* @param object $qa Question attempt object
* @param string $filearea File area descriptor
* @param int $itemid Item id to get
* @return string Output url, or null if not found
*/
protected static function get_url_for_image(question_attempt $qa, $filearea, $itemid = 0) {
$question = $qa->get_question();
$qubaid = $qa->get_usage_id();
$slot = $qa->get_slot();
$fs = get_file_storage();
if ($filearea == 'bgimage') {
$itemid = $question->id;
}
$componentname = $question->qtype->plugin_name();
$draftfiles = $fs->get_area_files($question->contextid, $componentname,
$filearea, $itemid, 'id');
if ($draftfiles) {
foreach ($draftfiles as $file) {
if ($file->is_directory()) {
continue;
}
$url = moodle_url::make_pluginfile_url($question->contextid, $componentname,
$filearea, "$qubaid/$slot/{$itemid}", '/',
$file->get_filename());
return $url->out();
}
}
return null;
}
/**
* Returns a hidden field for a qt variable
*
* @param object $qa Question attempt object
* @param string $varname The hidden var name
* @param string $value The hidden value
* @param array $classes Any additional css classes to apply
* @return array Array with field name and the html of the tag
*/
protected function hidden_field_for_qt_var(question_attempt $qa, $varname, $value = null, $classes = null) {
if ($value === null) {
$value = $qa->get_last_qt_var($varname);
}
$fieldname = $qa->get_qt_field_name($varname);
$attributes = ['type' => 'hidden',
'id' => str_replace(':', '_', $fieldname),
'name' => $fieldname,
'value' => $value];
if ($classes !== null) {
$attributes['class'] = join(' ', $classes);
}
return [$fieldname, html_writer::empty_tag('input', $attributes)."\n"];
}
/**
* Generate the hidden fields on the preview page to capture the responses.
* @param question_attempt $qa
* @param $choicenumber
* @param $value
* @param $class
* @return mixed
*/
protected function hidden_field_choice(question_attempt $qa, $choicenumber, $value = null, $class = null) {
$varname = $qa->get_question()->field($choicenumber);
$classes = ['choices', 'choice'. $choicenumber];
[, $html] = $this->hidden_field_for_qt_var($qa, $varname, $value, $classes);
return $html;
}
#[\Override]
public function formulation_and_controls(question_attempt $qa, question_display_options $options) {
$question = $qa->get_question();
$response = $qa->get_last_qt_data();
$visibilityzones = $response;
$componentname = $question->qtype->plugin_name();
$questiontext = $question->format_questiontext($qa);
$dropareaclass = 'droparea';
$draghomesclass = 'draghomes';
if ($options->readonly) {
$dropareaclass .= ' readonly';
$draghomesclass .= ' readonly';
}
$output = html_writer::div($questiontext, 'qtext');
$output .= html_writer::start_div('ddarea');
$output .= html_writer::start_div($dropareaclass);
$output .= html_writer::img(self::get_url_for_image($qa, 'bgimage'), get_string('dropbackground', 'qtype_drawlines'),
['class' => 'dropbackground']);
$output .= html_writer::start_div('que-dlines-dropzone');
$output .= html_writer::end_div();
$output .= html_writer::end_div();
$output .= html_writer::start_div($draghomesclass);
if (!$options->readonly) {
$attr['tabindex'] = 0;
}
if ($qa->get_state() == question_state::$invalid) {
$output .= html_writer::div($question->get_validation_error($qa->get_last_qt_data()), 'validationerror');
}
$output .= html_writer::end_div();
$hiddenfields = '';
foreach ($question->lines as $line) {
$hiddenfields .= $this->hidden_field_choice($qa, $line->number - 1);
}
$output .= html_writer::div($hiddenfields, 'dragchoices');
// Call to js.
$this->page->requires->js_call_amd('qtype_drawlines/question', 'init',
[$qa->get_outer_question_div_unique_id(), $options->readonly, $visibilityzones, $question->lines]);
$output .= html_writer::end_div();
return $output;
}
#[\Override]
public function specific_feedback(question_attempt $qa) {
return $this->combined_feedback($qa);
}
#[\Override]
public function correct_response(question_attempt $qa) {
$question = $qa->get_question();
$rightanswers = [];
$lineindicator = '';
foreach ($question->lines as $line) {
switch ($line->type) {
case 'linesinglearrow':
$lineindicator = ' ⟶ ';
break;
case 'linedoublearrows':
$lineindicator = ' ⟷ ';
break;
case 'linesegment':
$lineindicator = ' --- ';
break;
case 'lineinfinite':
$lineindicator = ' --o--o-- ';
break;
}
$rightanswers[] = "Line " . $line->number . ': ' . line::get_coordinates($line->zonestart)
. $lineindicator . line::get_coordinates($line->zoneend);
}
return $this->correct_choices($rightanswers);
}
/**
* Function returns string based on number of correct answers.
*
* @param array $right An Array of correct responses to the current question
* @return string based on number of correct responses
*/
protected function correct_choices(array $right): string {
// Return appropriate string for single/multiple correct answer(s).
$correctanswers = "<br>" . implode("<br>", $right);
if (count($right) == 1) {
return get_string('correctansweris', 'qtype_drawlines', $correctanswers);
} else if (count($right) > 2) {
return get_string('correctanswersare', 'qtype_drawlines', $correctanswers);
} else {
return "";
}
}
#[\Override]
protected function num_parts_correct(question_attempt $qa): string {
$a = new stdClass();
list($a->num, $a->outof) = $qa->get_question()->get_num_parts_right(
$qa->get_last_qt_data());
if (is_null($a->outof)) {
return '';
} else if ($a->num == 1) {
return html_writer::tag('p', get_string('yougot1right', 'qtype_drawlines'));
} else {
$f = new NumberFormatter(current_language(), NumberFormatter::SPELLOUT);
$a->num = $f->format($a->num);
return html_writer::tag('p', get_string('yougotnright', 'qtype_drawlines', $a));
}
}
}