-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrenderer.php
executable file
·224 lines (190 loc) · 8.92 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
<?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/>.
/**
* True-false question renderer class.
*
* @package qtype
* @subpackage siyavulaqt
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/filter/siyavula/lib.php');
use filter_siyavula\renderables\question_feedback_renderable;
use filter_siyavula\renderables\standalone_activity_renderable;
/**
* Generates the output for true-false questions.
*
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_siyavulaqt_renderer extends qtype_renderer {
public function formulation_and_controls(question_attempt $qa,
question_display_options $options) {
global $PAGE, $CFG, $DB;
$nextqt = optional_param('nextqr', '', PARAM_INT);
$question = $qa->get_question();
$response = $qa->get_last_qt_var('answer', '');
$inputname = $qa->get_qt_field_name('answer');
$trueattributes = array(
'type' => 'radio',
'name' => $inputname,
'value' => 1,
'id' => $inputname . 'true',
);
$falseattributes = array(
'type' => 'radio',
'name' => $inputname,
'value' => 0,
'id' => $inputname . 'false',
);
if ($options->readonly) {
$trueattributes['disabled'] = 'disabled';
$falseattributes['disabled'] = 'disabled';
}
// Work out which radio button to select (if any).
$truechecked = false;
$falsechecked = false;
$responsearray = array();
if ($response) {
$truechecked = true;
$responsearray = array('answer' => 1);
} else if ($response !== '') {
$falsechecked = true;
$responsearray = array('answer' => 1);
}
// Work out visual feedback for answer correctness.
$trueclass = '';
$falseclass = '';
$truefeedbackimg = '';
$falsefeedbackimg = '';
if ($options->correctness) {
if ($truechecked) {
$trueclass = ' ' . $this->feedback_class((int) $question->rightanswer);
$truefeedbackimg = $this->feedback_image((int) $question->rightanswer);
} else if ($falsechecked) {
$falseclass = ' ' . $this->feedback_class((int) (!$question->rightanswer));
$falsefeedbackimg = $this->feedback_image((int) (!$question->rightanswer));
}
}
$radiotrue = html_writer::empty_tag('input', $trueattributes) .
html_writer::tag('label', get_string('true', 'qtype_siyavulaqt'),
array('for' => $trueattributes['id'], 'class' => 'ml-1'));
$radiofalse = html_writer::empty_tag('input', $falseattributes) .
html_writer::tag('label', get_string('false', 'qtype_siyavulaqt'),
array('for' => $falseattributes['id'], 'class' => 'ml-1'));
// Determine if we're on the feedback page.
$url = $_SERVER["REQUEST_URI"];
$findme = '/mod/quiz/review.php';
$pos = strpos($url, $findme);
if ($pos === false) {
$isfeedback = false;
} else {
$isfeedback = true;
}
$result = '';
// Get the standalone.mustache.
$standalonepage = $question->format_questiontext($qa);
// Strip all tags of the mustache, only left the text inside <script>.
$standalonestrip = strip_tags($standalonepage);
$standalonestrip = str_replace("sy-", ' ', $standalonestrip);
// If we detect a "," then we will use [0] for the question ID, and [1] for the seed.
$standalonestrip = explode('|', $standalonestrip);
if (isset($standalonestrip[1])) {
$seed = (int) $standalonestrip[1];
$standalonestrip = $standalonestrip[0];
} else {
$standalonestrip = $standalonestrip[0];
}
$randomseed = (isset($seed) ? $seed : rand(1, 99999));
$clientip = $_SERVER['REMOTE_ADDR'];
$siyavulaconfig = get_config('filter_siyavula');
$baseurl = $siyavulaconfig->url_base;
$token = siyavula_get_user_token($siyavulaconfig, $clientip);
$usertoken = siyavula_get_external_user_token($siyavulaconfig, $clientip, $token);
$activitytype = 'standalone';
$templateid = $standalonestrip;
// Current version is Moodle 4.0 or higher use the event types. Otherwise use the older versions.
if ($CFG->version >= 2022041912) {
$PAGE->requires->js_call_amd('filter_siyavula/initmathjax', 'init');
} else {
$PAGE->requires->js_call_amd('filter_siyavula/initmathjax-backward', 'init');
}
$PAGE->requires->js_call_amd('qtype_siyavulaqt/siyavulaqt', 'init', ['chktrue' =>
$trueattributes, 'chkfalse' => $falseattributes, 'questionId' => $question->id]);
$renderer = $PAGE->get_renderer('filter_siyavula');
if (!$isfeedback) {
$activityrenderable = new standalone_activity_renderable();
$activityrenderable->wwwroot = $CFG->wwwroot;
$activityrenderable->baseurl = $baseurl;
$activityrenderable->token = $token;
$activityrenderable->usertoken = $usertoken->token;
$activityrenderable->activitytype = $activitytype;
$activityrenderable->templateid = $templateid;
$activityrenderable->randomseed = $randomseed;
$result .= $renderer->render_standalone_activity($activityrenderable);
} else {
$activityid = $DB->get_field(
'question_siyavulaqt', 'activityid', array('question' => $question->id)
);
$responseid = $DB->get_field(
'question_siyavulaqt', 'responseid', array('question' => $question->id)
);
$response = get_activity_response($token, $usertoken->token, $baseurl, $activityid, $responseid);
$activityrenderable = new question_feedback_renderable();
$activityrenderable->wwwroot = $CFG->wwwroot;
$activityrenderable->baseurl = $baseurl;
$activityrenderable->html = $response->response->question_html;
return $renderer->render_question_feedback($activityrenderable);
}
$result .= html_writer::start_tag('div', array('class' => 'ablock', 'style' => 'display: none;'));
$result .= html_writer::tag('div', get_string('selectone', 'qtype_siyavulaqt'),
array('class' => 'prompt'));
$result .= html_writer::start_tag('div', array('class' => 'answer'));
$result .= html_writer::tag('div', $radiotrue . ' ' . $truefeedbackimg,
array('class' => 'r0' . $trueclass));
$result .= html_writer::tag('div', $radiofalse . ' ' . $falsefeedbackimg,
array('class' => 'r1' . $falseclass));
$result .= html_writer::end_tag('div'); // Answer.
$result .= html_writer::end_tag('div'); // Ablock.
if ($qa->get_state() == question_state::$invalid) {
$result .= html_writer::nonempty_tag('div',
$question->get_validation_error($responsearray),
array('class' => 'validationerror'));
}
return $result;
}
public function specific_feedback(question_attempt $qa) {
$question = $qa->get_question();
$response = $qa->get_last_qt_var('answer', '');
if ($response) {
return $question->format_text($question->truefeedback, $question->truefeedbackformat,
$qa, 'question', 'answerfeedback', $question->trueanswerid);
} else if ($response !== '') {
return $question->format_text($question->falsefeedback, $question->falsefeedbackformat,
$qa, 'question', 'answerfeedback', $question->falseanswerid);
}
}
// LC: Feedback has been removed, if required again, uncomment this.
// public function correct_response(question_attempt $qa) {
// $question = $qa->get_question();
// if ($question->rightanswer) {
// return get_string('correctanswertrue', 'qtype_siyavulaqt');
// } else {
// return get_string('correctanswerfalse', 'qtype_siyavulaqt');
// }
// }
}