forked from marcusgreen/moodle-qtype_gapfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
213 lines (189 loc) · 8.37 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/>.
/**
* gapfill question renderer class.
*
* @package qtype
* @subpackage gapfill
* @copyright © 2012 Marcus Green
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class qtype_gapfill_renderer extends qtype_with_combined_feedback_renderer {
public $correct_responses = array();
public $marked_responses = array();
public function formulation_and_controls(question_attempt $qa, question_display_options $options) {
global $PAGE;
$question = $qa->get_question();
if ($question->answerdisplay == "dragdrop") {
$PAGE->requires->js('/question/type/gapfill/jquery/jquery-1.4.2.js');
$PAGE->requires->js('/question/type/gapfill/jquery/ui/jquery.ui.core.min.js');
$PAGE->requires->js('/question/type/gapfill/jquery/ui/jquery.ui.widget.min.js');
$PAGE->requires->js('/question/type/gapfill/jquery/ui/jquery.ui.mouse.min.js');
$PAGE->requires->js('/question/type/gapfill/jquery/ui/jquery.ui.draggable.min.js');
$PAGE->requires->js('/question/type/gapfill/jquery/ui/jquery.ui.droppable.min.js');
$PAGE->requires->js('/question/type/gapfill/dragdrop.js');
}
$answers = $qa->get_step(0)->get_qt_var('_allanswers');
$output = '';
if ($question->answerdisplay == "dragdrop") {
$ddclass = " draggable answers ";
$answers = $this->get_answers('dragdrop', $answers);
foreach ($answers as $value) {
$output.= '<span class="' . $ddclass . '">' . $value . "</span> ";
}
$output.="</br></br>";
}
$marked_gaps = $question->get_marked_gaps($qa, $options);
foreach ($question->textfragments as $place => $fragment) {
if ($place > 0) {
$output.=$this->embedded_element($qa, $place, $options, $marked_gaps);
}
/* format the non entry field parts of the question text, this will also
ensure images get displayed */
$output .= $question->format_text($fragment, $question->questiontextformat,
$qa, 'question', 'questiontext', $question->id);
}
print ("<br/>");
if ($qa->get_state() == question_state::$invalid) {
$output.= html_writer::nonempty_tag('div', $question->get_validation_error(array('answer' =>
$output)), array('class' => 'validationerror'));
}
return $output;
}
public function embedded_element(question_attempt $qa, $place, question_display_options $options, $marked_gaps) {
/* fraction is the mark associated with this field, always 1 or 0 for this question type */
$question = $qa->get_question();
$fieldname = $question->field($place);
$currentanswer = $qa->get_last_qt_var($fieldname);
$rightanswer = $question->get_right_choice_for($place);
$size = strlen($rightanswer);
/* $options->correctness is really about it being ready to mark, */
$feedbackimage = "";
$inputclass = "";
if (($options->correctness) or ($options->numpartscorrect)) {
$gap = $marked_gaps['p' . $place];
$fraction = $gap['fraction'];
$response = $qa->get_last_qt_data();
if ($fraction == 1) {
array_push($this->correct_responses, $response[$fieldname]);
$feedbackimage = $this->feedback_image($fraction);
/* sets the field background to green or yellow if fraction is 1 */
$inputclass = $this->get_input_class($marked_gaps, $qa, $fraction, $fieldname);
} else {
/* set background to red and image to cross if fraction is 0 */
$feedbackimage = $this->feedback_image($fraction);
$inputclass = $this->feedback_class($fraction);
}
}
$qprefix = $qa->get_qt_field_name('');
$inputname = $qprefix . 'p' . $place;
$inputattributes = array(
'type' => "text",
'name' => $inputname,
'value' => $currentanswer,
'id' => $inputname,
'size' => $size,
'class' => 'droppable ' . $inputclass
);
/* When previewing after a quiz is complete */
if ($options->readonly) {
$readonly = array('disabled' => 'true');
$inputattributes = array_merge($inputattributes, $readonly);
}
if ($question->answerdisplay == "dropdown") {
$inputattributes['type'] = "select";
$inputattributes['size'] = "";
$inputattributes['class'] = $inputclass;
$answers = $qa->get_step(0)->get_qt_var('_allanswers');
$selectoptions = $this->get_answers('dropdown', $answers);
$selecthtml = html_writer::select($selectoptions, $inputname,
$currentanswer, ' ', $inputattributes) . ' ' . $feedbackimage;
return $selecthtml;
} else {
return html_writer::empty_tag('input', $inputattributes) . $feedbackimage;
}
}
/**
*
* @param array $marked_gaps
* @param question_attempt $qa
* @param type $fraction either 0 or 1 for correct or incorrect
* @param type $fieldname p1, p2, p3 etc
* @return string set the feedback class to green unless noduplicates is set
* then check if this is a duplicated value and if it is set the background
* to yellow.
*/
public function get_input_class(array $marked_gaps, question_attempt $qa, $fraction, $fieldname) {
$response = $qa->get_last_qt_data();
$question = $qa->get_question();
$inputclass = $this->feedback_class($fraction);
foreach ($marked_gaps as $gap) {
if ($response[$fieldname] == $gap['value']) {
if ($gap['duplicate'] == 'true') {
if ($question->noduplicates == 1) {
$inputclass = ' correctduplicate';
}
}
}
}
return $inputclass;
}
public function specific_feedback(question_attempt $qa) {
return $this->combined_feedback($qa) . $this->get_duplicate_feedback($qa);
}
/**
* @param type questionattemtp $qa
* @return type string
* if noduplicates is set check if any responses
* are duplicate values
*/
public function get_duplicate_feedback(question_attempt $qa) {
$question = $qa->get_question();
if ($question->noduplicates == 0 ) {
return;
}
$arr_unique = array_unique($this->correct_responses);
if (count($arr_unique) != count($this->correct_responses)) {
return get_string('duplicatepartialcredit', 'qtype_gapfill');
}
}
public function get_answers($answerdisplay, $answers) {
// Turn string into an array.
$answers = explode(",", $answers);
if ($answerdisplay == 'dragdrop') {
return $answers;
}
if ($answerdisplay == 'dropdown') {
// Make the key and value the same in the array.
$answers = array_combine($answers, $answers);
return $answers;
}
}
/* overriding base class method purely to return a string yougotnrightcount
* instead of default yougotnright
*/
protected function num_parts_correct(question_attempt $qa) {
$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 {
return get_string('yougotnrightcount', 'qtype_gapfill', $a);
}
}
}