| Línea 32... |
Línea 32... |
| 32 |
*
|
32 |
*
|
| 33 |
* @package mod_choice
|
33 |
* @package mod_choice
|
| 34 |
* @category external
|
34 |
* @category external
|
| 35 |
* @copyright 2015 Costantino Cito <ccito@cvaconsulting.com>
|
35 |
* @copyright 2015 Costantino Cito <ccito@cvaconsulting.com>
|
| 36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| - |
|
37 |
* @covers \mod_choice_external
|
| 37 |
*/
|
38 |
*/
|
| 38 |
class externallib_test extends externallib_advanced_testcase {
|
39 |
final class externallib_test extends externallib_advanced_testcase {
|
| Línea 39... |
Línea 40... |
| 39 |
|
40 |
|
| 40 |
/**
|
41 |
/**
|
| 41 |
* Test get_choice_results
|
42 |
* Test get_choice_results
|
| 42 |
*/
|
43 |
*/
|
| Línea 143... |
Línea 144... |
| 143 |
// But we can see totals and percentages.
|
144 |
// But we can see totals and percentages.
|
| 144 |
$this->assertEquals(1, $resultsarr[$myanswer]['numberofuser']);
|
145 |
$this->assertEquals(1, $resultsarr[$myanswer]['numberofuser']);
|
| 145 |
}
|
146 |
}
|
| Línea 146... |
Línea 147... |
| 146 |
|
147 |
|
| - |
|
148 |
/**
|
| - |
|
149 |
* Test get_choice_results using groups.
|
| - |
|
150 |
*/
|
| - |
|
151 |
public function test_get_choice_results_with_groups(): void {
|
| - |
|
152 |
$this->resetAfterTest(true);
|
| - |
|
153 |
|
| - |
|
154 |
$course = $this->getDataGenerator()->create_course();
|
| - |
|
155 |
$group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
|
| - |
|
156 |
$group2 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
|
| - |
|
157 |
$choicegenerator = $this->getDataGenerator()->get_plugin_generator('mod_choice');
|
| - |
|
158 |
|
| - |
|
159 |
// Create 3 choices: one with separate groups, one with visible groups and one with no groups.
|
| - |
|
160 |
$commonparams = [
|
| - |
|
161 |
'course' => $course->id,
|
| - |
|
162 |
'option' => ['fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza'],
|
| - |
|
163 |
'name' => 'Separate groups choice',
|
| - |
|
164 |
'showresults' => CHOICE_SHOWRESULTS_ALWAYS,
|
| - |
|
165 |
'publish' => 1,
|
| - |
|
166 |
];
|
| - |
|
167 |
|
| - |
|
168 |
$nogroupschoice = $choicegenerator->create_instance(array_merge($commonparams, ['groupmode' => NOGROUPS]));
|
| - |
|
169 |
$separatechoice = $choicegenerator->create_instance(array_merge($commonparams, ['groupmode' => SEPARATEGROUPS]));
|
| - |
|
170 |
$visiblechoice = $choicegenerator->create_instance(array_merge($commonparams, ['groupmode' => VISIBLEGROUPS]));
|
| - |
|
171 |
$nogroupsoptions = array_keys(choice_get_choice($nogroupschoice->id)->option);
|
| - |
|
172 |
$separateoptions = array_keys(choice_get_choice($separatechoice->id)->option);
|
| - |
|
173 |
$visibleoptions = array_keys(choice_get_choice($visiblechoice->id)->option);
|
| - |
|
174 |
$nogroupscm = get_coursemodule_from_id('choice', $nogroupschoice->cmid);
|
| - |
|
175 |
$separatecm = get_coursemodule_from_id('choice', $separatechoice->cmid);
|
| - |
|
176 |
$visiblecm = get_coursemodule_from_id('choice', $visiblechoice->cmid);
|
| - |
|
177 |
|
| - |
|
178 |
// Enrol 3 students in the course. One student will have no group, the others will belong one to each group.
|
| - |
|
179 |
$student1 = $this->getDataGenerator()->create_user();
|
| - |
|
180 |
$student2 = $this->getDataGenerator()->create_user();
|
| - |
|
181 |
$studentnogroup = $this->getDataGenerator()->create_user();
|
| - |
|
182 |
|
| - |
|
183 |
self::getDataGenerator()->enrol_user($student1->id, $course->id, 'student');
|
| - |
|
184 |
self::getDataGenerator()->enrol_user($student2->id, $course->id, 'student');
|
| - |
|
185 |
self::getDataGenerator()->enrol_user($studentnogroup->id, $course->id, 'student');
|
| - |
|
186 |
|
| - |
|
187 |
groups_add_member($group1, $student1);
|
| - |
|
188 |
groups_add_member($group2, $student2);
|
| - |
|
189 |
|
| - |
|
190 |
// Add answers for both users in all choices.
|
| - |
|
191 |
$this->setUser($student1);
|
| - |
|
192 |
choice_user_submit_response($nogroupsoptions[0], $nogroupschoice, $student1->id, $course, $nogroupscm);
|
| - |
|
193 |
choice_user_submit_response($separateoptions[0], $separatechoice, $student1->id, $course, $separatecm);
|
| - |
|
194 |
choice_user_submit_response($visibleoptions[0], $visiblechoice, $student1->id, $course, $visiblecm);
|
| - |
|
195 |
|
| - |
|
196 |
$this->setUser($student2);
|
| - |
|
197 |
choice_user_submit_response($nogroupsoptions[1], $nogroupschoice, $student2->id, $course, $nogroupscm);
|
| - |
|
198 |
choice_user_submit_response($separateoptions[1], $separatechoice, $student2->id, $course, $separatecm);
|
| - |
|
199 |
choice_user_submit_response($visibleoptions[1], $visiblechoice, $student2->id, $course, $visiblecm);
|
| - |
|
200 |
|
| - |
|
201 |
// No groups: check that the groupid parameter is ignored.
|
| - |
|
202 |
$results = mod_choice_external::get_choice_results($nogroupschoice->id, $group1->id);
|
| - |
|
203 |
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results);
|
| - |
|
204 |
|
| - |
|
205 |
$resultsarr = [];
|
| - |
|
206 |
foreach ($results['options'] as $option) {
|
| - |
|
207 |
$resultsarr[$option['id']] = $option['userresponses'];
|
| - |
|
208 |
}
|
| - |
|
209 |
$this->assertEquals($resultsarr[$nogroupsoptions[0]][0]['userid'], $student1->id);
|
| - |
|
210 |
$this->assertEquals($resultsarr[$nogroupsoptions[1]][0]['userid'], $student2->id);
|
| - |
|
211 |
|
| - |
|
212 |
$results = mod_choice_external::get_choice_results($nogroupschoice->id, $group2->id);
|
| - |
|
213 |
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results);
|
| - |
|
214 |
|
| - |
|
215 |
$resultsarr = [];
|
| - |
|
216 |
foreach ($results['options'] as $option) {
|
| - |
|
217 |
$resultsarr[$option['id']] = $option['userresponses'];
|
| - |
|
218 |
}
|
| - |
|
219 |
$this->assertEquals($resultsarr[$nogroupsoptions[0]][0]['userid'], $student1->id);
|
| - |
|
220 |
$this->assertEquals($resultsarr[$nogroupsoptions[1]][0]['userid'], $student2->id);
|
| - |
|
221 |
|
| - |
|
222 |
// Separate groups: check that students can only see results of the group they belong.
|
| - |
|
223 |
$this->setUser($student1);
|
| - |
|
224 |
$results = mod_choice_external::get_choice_results($separatechoice->id, $group1->id);
|
| - |
|
225 |
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results);
|
| - |
|
226 |
|
| - |
|
227 |
$resultsarr = [];
|
| - |
|
228 |
foreach ($results['options'] as $option) {
|
| - |
|
229 |
$resultsarr[$option['id']] = $option['userresponses'];
|
| - |
|
230 |
}
|
| - |
|
231 |
$this->assertEquals($resultsarr[$separateoptions[0]][0]['userid'], $student1->id);
|
| - |
|
232 |
$this->assertCount(0, $resultsarr[$separateoptions[1]]); // The answer for the group 2 is not returned.
|
| - |
|
233 |
|
| - |
|
234 |
try {
|
| - |
|
235 |
mod_choice_external::get_choice_results($separatechoice->id, $group2->id);
|
| - |
|
236 |
$this->fail('Exception expected due to not visible group.');
|
| - |
|
237 |
} catch (\moodle_exception $e) {
|
| - |
|
238 |
$this->assertEquals('notingroup', $e->errorcode);
|
| - |
|
239 |
}
|
| - |
|
240 |
|
| - |
|
241 |
try {
|
| - |
|
242 |
mod_choice_external::get_choice_results($separatechoice->id, 0); // All participants also throws error.
|
| - |
|
243 |
$this->fail('Exception expected due to not visible group.');
|
| - |
|
244 |
} catch (\moodle_exception $e) {
|
| - |
|
245 |
$this->assertEquals('notingroup', $e->errorcode);
|
| - |
|
246 |
}
|
| - |
|
247 |
|
| - |
|
248 |
$this->setUser($student2);
|
| - |
|
249 |
$results = mod_choice_external::get_choice_results($separatechoice->id, $group2->id);
|
| - |
|
250 |
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results);
|
| - |
|
251 |
|
| - |
|
252 |
$resultsarr = [];
|
| - |
|
253 |
foreach ($results['options'] as $option) {
|
| - |
|
254 |
$resultsarr[$option['id']] = $option['userresponses'];
|
| - |
|
255 |
}
|
| - |
|
256 |
$this->assertEquals($resultsarr[$separateoptions[1]][0]['userid'], $student2->id);
|
| - |
|
257 |
$this->assertCount(0, $resultsarr[$separateoptions[0]]); // The answer for the group 1 is not returned.
|
| - |
|
258 |
|
| - |
|
259 |
try {
|
| - |
|
260 |
mod_choice_external::get_choice_results($separatechoice->id, $group1->id);
|
| - |
|
261 |
$this->fail('Exception expected due to not visible group.');
|
| - |
|
262 |
} catch (\moodle_exception $e) {
|
| - |
|
263 |
$this->assertEquals('notingroup', $e->errorcode);
|
| - |
|
264 |
}
|
| - |
|
265 |
|
| - |
|
266 |
// Visible groups: check that students can see results of all groups, including 'All participants'.
|
| - |
|
267 |
$this->setUser($student1);
|
| - |
|
268 |
$results = mod_choice_external::get_choice_results($visiblechoice->id, $group1->id);
|
| - |
|
269 |
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results);
|
| - |
|
270 |
|
| - |
|
271 |
$resultsarr = [];
|
| - |
|
272 |
foreach ($results['options'] as $option) {
|
| - |
|
273 |
$resultsarr[$option['id']] = $option['userresponses'];
|
| - |
|
274 |
}
|
| - |
|
275 |
$this->assertEquals($resultsarr[$visibleoptions[0]][0]['userid'], $student1->id);
|
| - |
|
276 |
$this->assertCount(0, $resultsarr[$visibleoptions[1]]); // The answer for the other group is not returned.
|
| - |
|
277 |
|
| - |
|
278 |
$results = mod_choice_external::get_choice_results($visiblechoice->id, $group2->id);
|
| - |
|
279 |
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results);
|
| - |
|
280 |
|
| - |
|
281 |
$resultsarr = [];
|
| - |
|
282 |
foreach ($results['options'] as $option) {
|
| - |
|
283 |
$resultsarr[$option['id']] = $option['userresponses'];
|
| - |
|
284 |
}
|
| - |
|
285 |
$this->assertEquals($resultsarr[$visibleoptions[1]][0]['userid'], $student2->id);
|
| - |
|
286 |
$this->assertCount(0, $resultsarr[$visibleoptions[0]]); // The answer for the other group is not returned.
|
| - |
|
287 |
|
| - |
|
288 |
$results = mod_choice_external::get_choice_results($visiblechoice->id, 0);
|
| - |
|
289 |
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results);
|
| - |
|
290 |
|
| - |
|
291 |
$resultsarr = [];
|
| - |
|
292 |
foreach ($results['options'] as $option) {
|
| - |
|
293 |
$resultsarr[$option['id']] = $option['userresponses'];
|
| - |
|
294 |
}
|
| - |
|
295 |
$this->assertEquals($resultsarr[$visibleoptions[0]][0]['userid'], $student1->id);
|
| - |
|
296 |
$this->assertEquals($resultsarr[$visibleoptions[1]][0]['userid'], $student2->id);
|
| - |
|
297 |
|
| - |
|
298 |
// User with no groups can view results for the visible groups choice, but not for separate groups.
|
| - |
|
299 |
$this->setUser($studentnogroup);
|
| - |
|
300 |
$results = mod_choice_external::get_choice_results($visiblechoice->id, 0);
|
| - |
|
301 |
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results);
|
| - |
|
302 |
|
| - |
|
303 |
$resultsarr = [];
|
| - |
|
304 |
foreach ($results['options'] as $option) {
|
| - |
|
305 |
$resultsarr[$option['id']] = $option['userresponses'];
|
| - |
|
306 |
}
|
| - |
|
307 |
$this->assertEquals($resultsarr[$visibleoptions[0]][0]['userid'], $student1->id);
|
| - |
|
308 |
$this->assertEquals($resultsarr[$visibleoptions[1]][0]['userid'], $student2->id);
|
| - |
|
309 |
|
| - |
|
310 |
try {
|
| - |
|
311 |
mod_choice_external::get_choice_results($separatechoice->id, 0);
|
| - |
|
312 |
$this->fail('Exception expected due to not visible group.');
|
| - |
|
313 |
} catch (\moodle_exception $e) {
|
| - |
|
314 |
$this->assertEquals('notingroup', $e->errorcode);
|
| - |
|
315 |
}
|
| - |
|
316 |
}
|
| - |
|
317 |
|
| 147 |
/**
|
318 |
/**
|
| 148 |
* Test get_choice_options
|
319 |
* Test get_choice_options
|
| 149 |
*/
|
320 |
*/
|
| 150 |
public function test_get_choice_options(): void {
|
321 |
public function test_get_choice_options(): void {
|