forked from moodleou/moodle-qtype_ddimageortext
/
questionbase.php
165 lines (148 loc) · 5.97 KB
/
questionbase.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
<?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/>.
/**
* Drag-and-drop onto image question definition class.
*
* @package qtype_ddimageortext
* @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 . '/question/type/gapselect/questionbase.php');
/**
* Represents a drag-and-drop onto image question.
*
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_ddtoimage_question_base extends qtype_gapselect_question_base {
public function clear_wrong_from_response(array $response) {
foreach ($this->places as $place => $notused) {
if (array_key_exists($this->field($place), $response) &&
$response[$this->field($place)] != $this->get_right_choice_for($place)) {
$response[$this->field($place)] = '';
}
}
return $response;
}
public function get_right_choice_for($placeno) {
$place = $this->places[$placeno];
foreach ($this->choiceorder[$place->group] as $choicekey => $choiceid) {
if ($this->rightchoices[$placeno] == $choiceid) {
return $choicekey;
}
}
}
public function summarise_response(array $response) {
$allblank = true;
foreach ($this->places as $placeno => $place) {
$summariseplace = $place->summarise();
if (array_key_exists($this->field($placeno), $response) &&
$response[$this->field($placeno)]) {
$selected = $this->get_selected_choice($place->group,
$response[$this->field($placeno)]);
$summarisechoice = $selected->summarise();
$allblank = false;
} else {
$summarisechoice = '';
}
$choices[] = "$summariseplace -> {{$summarisechoice}}";
}
if ($allblank) {
return null;
}
return implode(' ', $choices);
}
public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
if ($filearea == 'bgimage' || $filearea == 'dragimage') {
$validfilearea = true;
} else {
$validfilearea = false;
}
if ($component == 'qtype_ddimageortext' && $validfilearea) {
$question = $qa->get_question();
$itemid = reset($args);
if ($filearea == 'bgimage') {
return $itemid == $question->id;
} else if ($filearea == 'dragimage') {
foreach ($question->choices as $group) {
foreach ($group as $drag) {
if ($drag->id == $itemid) {
return true;
}
}
}
return false;
}
} else {
return parent::check_file_access($qa, $options, $component,
$filearea, $args, $forcedownload);
}
}
public function get_validation_error(array $response) {
if ($this->is_complete_response($response)) {
return '';
}
return get_string('pleasedraganimagetoeachdropregion', 'qtype_ddimageortext');
}
public function classify_response(array $response) {
$parts = array();
foreach ($this->places as $placeno => $place) {
$group = $place->group;
if (!array_key_exists($this->field($placeno), $response) ||
!$response[$this->field($placeno)]) {
$parts[$placeno] = question_classified_response::no_response();
continue;
}
$fieldname = $this->field($placeno);
$choiceno = $this->choiceorder[$group][$response[$fieldname]];
$choice = $this->choices[$group][$choiceno];
$correct = $this->get_right_choice_for($placeno) == $response[$fieldname];
$parts[$placeno] = new question_classified_response(
$choiceno, $choice->summarise(), $correct?1:0);
}
return $parts;
}
public function get_random_guess_score() {
$accum = 0;
foreach ($this->places as $place) {
foreach ($this->choices[$place->group] as $choice) {
if ($choice->infinite) {
return null;
}
}
$accum += 1 / count($this->choices[$place->group]);
}
return $accum / count($this->places);
}
public function get_question_summary() {
$summary = '';
if (!html_is_blank($this->questiontext)) {
$question = $this->html_to_text($this->questiontext, $this->questiontextformat);
$summary .= $question . '; ';
}
$places = array();
foreach ($this->places as $place) {
$cs = array();
foreach ($this->choices[$place->group] as $choice) {
$cs[] = $choice->summarise();
}
$places[] = '[[' . $place->summarise() . ']] -> {' . implode(' / ', $cs) . '}';
}
$summary .= implode('; ', $places);
return $summary;
}
}