Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core;
18
 
19
use core_grading_external;
20
use core_external\external_api;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
global $CFG;
25
 
26
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
27
 
28
/**
29
 * Unit tests for the grading API defined in core_grading_external class.
30
 *
31
 * @package core
32
 * @category test
33
 * @copyright 2013 Paul Charsley
34
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
final class grading_external_test extends \externallib_advanced_testcase {
1 efrain 37
 
38
    /**
39
     * Test get_definitions
40
     */
11 efrain 41
    public function test_get_definitions(): void {
1 efrain 42
        global $DB, $CFG, $USER;
43
 
44
        $this->resetAfterTest(true);
45
        // Create a course and assignment.
46
        $coursedata['idnumber'] = 'idnumbercourse';
47
        $coursedata['fullname'] = 'Lightwork Course';
48
        $coursedata['summary'] = 'Lightwork Course description';
49
        $coursedata['summaryformat'] = FORMAT_MOODLE;
50
        $course = self::getDataGenerator()->create_course($coursedata);
51
 
52
        $assigndata['course'] = $course->id;
53
        $assigndata['name'] = 'lightwork assignment';
54
 
55
        $cm = self::getDataGenerator()->create_module('assign', $assigndata);
56
 
57
        // Create manual enrolment record.
58
        $manualenroldata['enrol'] = 'manual';
59
        $manualenroldata['status'] = 0;
60
        $manualenroldata['courseid'] = $course->id;
61
        $enrolid = $DB->insert_record('enrol', $manualenroldata);
62
 
63
        // Create a teacher and give them capabilities.
64
        $coursecontext = \context_course::instance($course->id);
65
        $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $coursecontext->id, 3);
66
        $modulecontext = \context_module::instance($cm->cmid);
67
        $this->assignUserCapability('mod/assign:grade', $modulecontext->id, $roleid);
68
 
69
        // Create the teacher's enrolment record.
70
        $userenrolmentdata['status'] = 0;
71
        $userenrolmentdata['enrolid'] = $enrolid;
72
        $userenrolmentdata['userid'] = $USER->id;
73
        $DB->insert_record('user_enrolments', $userenrolmentdata);
74
 
75
        // Create a grading area.
76
        $gradingarea = array(
77
            'contextid' => $modulecontext->id,
78
            'component' => 'mod_assign',
79
            'areaname' => 'submissions',
80
            'activemethod' => 'rubric'
81
        );
82
        $areaid = $DB->insert_record('grading_areas', $gradingarea);
83
 
84
        // Create a rubric grading definition.
85
        $rubricdefinition = array (
86
            'areaid' => $areaid,
87
            'method' => 'rubric',
88
            'name' => 'test',
89
            'status' => 20,
90
            'copiedfromid' => 1,
91
            'timecreated' => 1,
92
            'usercreated' => $USER->id,
93
            'timemodified' => 1,
94
            'usermodified' => $USER->id,
95
            'timecopied' => 0
96
        );
97
        $definitionid = $DB->insert_record('grading_definitions', $rubricdefinition);
98
 
99
        // Create a criterion with levels.
100
        $rubriccriteria1 = array (
101
            'definitionid' => $definitionid,
102
            'sortorder' => 1,
103
            'description' => 'Demonstrate an understanding of disease control',
104
            'descriptionformat' => 0
105
        );
106
        $criterionid1 = $DB->insert_record('gradingform_rubric_criteria', $rubriccriteria1);
107
        $rubriclevel1 = array (
108
            'criterionid' => $criterionid1,
109
            'score' => 5,
110
            'definition' => 'pass',
111
            'definitionformat' => 0
112
        );
113
        $DB->insert_record('gradingform_rubric_levels', $rubriclevel1);
114
        $rubriclevel2 = array (
115
            'criterionid' => $criterionid1,
116
            'score' => 10,
117
            'definition' => 'excellent',
118
            'definitionformat' => 0
119
        );
120
        $DB->insert_record('gradingform_rubric_levels', $rubriclevel2);
121
 
122
        // Create a second criterion with levels.
123
        $rubriccriteria2 = array (
124
            'definitionid' => $definitionid,
125
            'sortorder' => 2,
126
            'description' => 'Demonstrate an understanding of brucellosis',
127
            'descriptionformat' => 0
128
        );
129
        $criterionid2 = $DB->insert_record('gradingform_rubric_criteria', $rubriccriteria2);
130
        $rubriclevel1 = array (
131
            'criterionid' => $criterionid2,
132
            'score' => 5,
133
            'definition' => 'pass',
134
            'definitionformat' => 0
135
        );
136
        $DB->insert_record('gradingform_rubric_levels', $rubriclevel1);
137
        $rubriclevel2 = array (
138
            'criterionid' => $criterionid2,
139
            'score' => 10,
140
            'definition' => 'excellent',
141
            'definitionformat' => 0
142
        );
143
        $DB->insert_record('gradingform_rubric_levels', $rubriclevel2);
144
 
145
        // Call the external function.
146
        $cmids = array ($cm->cmid);
147
        $areaname = 'submissions';
148
        $result = core_grading_external::get_definitions($cmids, $areaname);
149
        $result = external_api::clean_returnvalue(core_grading_external::get_definitions_returns(), $result);
150
 
151
        $this->assertEquals(1, count($result['areas']));
152
        $this->assertEquals(1, count($result['areas'][0]['definitions']));
153
        $definition = $result['areas'][0]['definitions'][0];
154
 
155
        $this->assertEquals($rubricdefinition['method'], $definition['method']);
156
        $this->assertEquals($USER->id, $definition['usercreated']);
157
 
158
        require_once("$CFG->dirroot/grade/grading/lib.php");
159
        require_once($CFG->dirroot.'/grade/grading/form/'.$rubricdefinition['method'].'/lib.php');
160
 
161
        $gradingmanager = get_grading_manager($areaid);
162
 
163
        $this->assertEquals(1, count($definition[$rubricdefinition['method']]));
164
 
165
        $rubricdetails = $definition[$rubricdefinition['method']];
166
        $details = call_user_func('gradingform_'.$rubricdefinition['method'].'_controller::get_external_definition_details');
167
 
168
        $this->assertEquals(2, count($rubricdetails[key($details)]));
169
 
170
        $found = false;
171
        foreach ($rubricdetails[key($details)] as $criterion) {
172
            if ($criterion['id'] == $criterionid1) {
173
                $this->assertEquals($rubriccriteria1['description'], $criterion['description']);
174
                $this->assertEquals(2, count($criterion['levels']));
175
                $found = true;
176
                break;
177
            }
178
        }
179
        $this->assertTrue($found);
180
    }
181
 
182
    /**
183
     * Test get_gradingform_instances
184
     */
11 efrain 185
    public function test_get_gradingform_instances(): void {
1 efrain 186
        global $DB, $USER;
187
 
188
        $this->resetAfterTest(true);
189
        // Create a course and assignment.
190
        $coursedata['idnumber'] = 'idnumbercourse';
191
        $coursedata['fullname'] = 'Lightwork Course';
192
        $coursedata['summary'] = 'Lightwork Course description';
193
        $coursedata['summaryformat'] = FORMAT_MOODLE;
194
        $course = self::getDataGenerator()->create_course($coursedata);
195
 
196
        $assigndata['course'] = $course->id;
197
        $assigndata['name'] = 'lightwork assignment';
198
 
199
        $assign = self::getDataGenerator()->create_module('assign', $assigndata);
200
 
201
        // Create manual enrolment record.
202
        $manualenroldata['enrol'] = 'manual';
203
        $manualenroldata['status'] = 0;
204
        $manualenroldata['courseid'] = $course->id;
205
        $enrolid = $DB->insert_record('enrol', $manualenroldata);
206
 
207
        // Create a teacher and give them capabilities.
208
        $coursecontext = \context_course::instance($course->id);
209
        $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $coursecontext->id, 3);
210
        $modulecontext = \context_module::instance($assign->cmid);
211
        $this->assignUserCapability('mod/assign:grade', $modulecontext->id, $roleid);
212
 
213
        // Create the teacher's enrolment record.
214
        $userenrolmentdata['status'] = 0;
215
        $userenrolmentdata['enrolid'] = $enrolid;
216
        $userenrolmentdata['userid'] = $USER->id;
217
        $DB->insert_record('user_enrolments', $userenrolmentdata);
218
 
219
        // Create a student with an assignment grade.
220
        $student = self::getDataGenerator()->create_user();
221
        $assigngrade = new \stdClass();
222
        $assigngrade->assignment = $assign->id;
223
        $assigngrade->userid = $student->id;
224
        $assigngrade->timecreated = time();
225
        $assigngrade->timemodified = $assigngrade->timecreated;
226
        $assigngrade->grader = $USER->id;
227
        $assigngrade->grade = 50;
228
        $assigngrade->attemptnumber = 0;
229
        $gid = $DB->insert_record('assign_grades', $assigngrade);
230
 
231
        // Create a grading area.
232
        $gradingarea = array(
233
            'contextid' => $modulecontext->id,
234
            'component' => 'mod_assign',
235
            'areaname' => 'submissions',
236
            'activemethod' => 'rubric'
237
        );
238
        $areaid = $DB->insert_record('grading_areas', $gradingarea);
239
 
240
        // Create a rubric grading definition.
241
        $rubricdefinition = array (
242
            'areaid' => $areaid,
243
            'method' => 'rubric',
244
            'name' => 'test',
245
            'status' => 20,
246
            'copiedfromid' => 1,
247
            'timecreated' => 1,
248
            'usercreated' => $USER->id,
249
            'timemodified' => 1,
250
            'usermodified' => $USER->id,
251
            'timecopied' => 0
252
        );
253
        $definitionid = $DB->insert_record('grading_definitions', $rubricdefinition);
254
 
255
        // Create a criterion with a level.
256
        $rubriccriteria = array (
257
            'definitionid' => $definitionid,
258
            'sortorder' => 1,
259
            'description' => 'Demonstrate an understanding of disease control',
260
            'descriptionformat' => 0
261
        );
262
        $criterionid = $DB->insert_record('gradingform_rubric_criteria', $rubriccriteria);
263
        $rubriclevel = array (
264
            'criterionid' => $criterionid,
265
            'score' => 50,
266
            'definition' => 'pass',
267
            'definitionformat' => 0
268
        );
269
        $levelid = $DB->insert_record('gradingform_rubric_levels', $rubriclevel);
270
 
271
        // Create a grading instance.
272
        $instance = array (
273
            'definitionid' => $definitionid,
274
            'raterid' => $USER->id,
275
            'itemid' => $gid,
276
            'status' => 1,
1441 ariadna 277
            'feedback' => 'Fabulous',
278
            'feedbackformat' => FORMAT_HTML,
1 efrain 279
            'timemodified' => 1
280
        );
281
        $instanceid = $DB->insert_record('grading_instances', $instance);
282
 
283
        // Create a filling.
284
        $filling = array (
285
            'instanceid' => $instanceid,
286
            'criterionid' => $criterionid,
287
            'levelid' => $levelid,
288
            'remark' => 'excellent work',
289
            'remarkformat' => 0
290
        );
291
        $DB->insert_record('gradingform_rubric_fillings', $filling);
292
 
293
        // Call the external function.
294
        $result = core_grading_external::get_gradingform_instances($definitionid, 0);
295
        $result = external_api::clean_returnvalue(core_grading_external::get_gradingform_instances_returns(), $result);
296
 
297
        $this->assertEquals(1, count($result['instances']));
298
        $this->assertEquals($USER->id, $result['instances'][0]['raterid']);
299
        $this->assertEquals($gid, $result['instances'][0]['itemid']);
300
        $this->assertEquals(1, $result['instances'][0]['status']);
1441 ariadna 301
        $this->assertEquals('Fabulous', $result['instances'][0]['feedback']);
302
        $this->assertEquals(FORMAT_HTML, $result['instances'][0]['feedbackformat']);
1 efrain 303
        $this->assertEquals(1, $result['instances'][0]['timemodified']);
1441 ariadna 304
        $this->assertCount(1, $result['instances'][0]['rubric']);
305
        $this->assertCount(1, $result['instances'][0]['rubric']['criteria']);
306
 
1 efrain 307
        $criteria = $result['instances'][0]['rubric']['criteria'];
308
        $this->assertEquals($criterionid, $criteria[0]['criterionid']);
309
        $this->assertEquals($levelid, $criteria[0]['levelid']);
310
        $this->assertEquals('excellent work', $criteria[0]['remark']);
311
    }
312
 
313
    /**
314
     *
315
     * Test save_definitions for rubric grading method
316
     */
11 efrain 317
    public function test_save_definitions_rubric(): void {
1 efrain 318
        global $DB, $CFG, $USER;
319
 
320
        $this->resetAfterTest(true);
321
        // Create a course and assignment.
322
        $course = self::getDataGenerator()->create_course();
323
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
324
        $params['course'] = $course->id;
325
        $instance = $generator->create_instance($params);
326
        $cm = get_coursemodule_from_instance('assign', $instance->id);
327
        $context = \context_module::instance($cm->id);
328
        $coursecontext = \context_course::instance($course->id);
329
 
330
        // Create the teacher.
331
        $teacher = self::getDataGenerator()->create_user();
332
        $USER->id = $teacher->id;
333
        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
334
        $this->assignUserCapability('moodle/grade:managegradingforms', $context->id, $teacherrole->id);
335
        $this->getDataGenerator()->enrol_user($teacher->id,
336
                                              $course->id,
337
                                              $teacherrole->id);
338
 
339
        // The grading area to insert.
340
        $gradingarea = array(
341
            'cmid' => $cm->id,
342
            'contextid' => $context->id,
343
            'component' => 'mod_assign',
344
            'areaname'  => 'submissions',
345
            'activemethod' => 'rubric'
346
        );
347
 
348
        // The rubric definition to insert.
349
        $rubricdefinition = array(
350
            'method' => 'rubric',
351
            'name' => 'test',
352
            'description' => '',
353
            'status' => 20,
354
            'copiedfromid' => 1,
355
            'timecreated' => 1,
356
            'usercreated' => $teacher->id,
357
            'timemodified' => 1,
358
            'usermodified' => $teacher->id,
359
            'timecopied' => 0
360
        );
361
 
362
        // The criterion to insert.
363
        $rubriccriteria1 = array (
364
             'sortorder' => 1,
365
             'description' => 'Demonstrate an understanding of disease control',
366
             'descriptionformat' => 0
367
        );
368
 
369
        // 3 levels for the criterion.
370
        $rubriclevel1 = array (
371
            'score' => 50,
372
            'definition' => 'pass',
373
            'definitionformat' => 0
374
        );
375
        $rubriclevel2 = array (
376
            'score' => 100,
377
            'definition' => 'excellent',
378
            'definitionformat' => 0
379
        );
380
        $rubriclevel3 = array (
381
            'score' => 0,
382
            'definition' => 'fail',
383
            'definitionformat' => 0
384
        );
385
 
386
        $rubriccriteria1['levels'] = array($rubriclevel1, $rubriclevel2, $rubriclevel3);
387
        $rubricdefinition['rubric'] = array('rubric_criteria' => array($rubriccriteria1));
388
        $gradingarea['definitions'] = array($rubricdefinition);
389
 
390
        $results = core_grading_external::save_definitions(array($gradingarea));
391
 
392
        $area = $DB->get_record('grading_areas',
393
                                array('contextid' => $context->id, 'component' => 'mod_assign', 'areaname' => 'submissions'),
394
                                '*', MUST_EXIST);
395
        $this->assertEquals($area->activemethod, 'rubric');
396
 
397
        $definition = $DB->get_record('grading_definitions', array('areaid' => $area->id, 'method' => 'rubric'), '*', MUST_EXIST);
398
        $this->assertEquals($rubricdefinition['name'], $definition->name);
399
 
400
        $criterion1 = $DB->get_record('gradingform_rubric_criteria', array('definitionid' => $definition->id), '*', MUST_EXIST);
401
        $levels = $DB->get_records('gradingform_rubric_levels', array('criterionid' => $criterion1->id));
402
        $validlevelcount = 0;
403
        $expectedvalue = true;
404
        foreach ($levels as $level) {
405
            if ($level->score == 0) {
406
                $this->assertEquals('fail', $level->definition);
407
                $validlevelcount++;
408
            } else if ($level->score == 50) {
409
                $this->assertEquals('pass', $level->definition);
410
                $validlevelcount++;
411
            } else if ($level->score == 100) {
412
                $this->assertEquals('excellent', $level->definition);
413
                $excellentlevelid = $level->id;
414
                $validlevelcount++;
415
            } else {
416
                $expectedvalue = false;
417
            }
418
        }
419
        $this->assertEquals(3, $validlevelcount);
420
        $this->assertTrue($expectedvalue, 'A level with an unexpected score was found');
421
 
422
        // Test add a new level and modify an existing.
423
        // Test add a new criteria and modify an existing.
424
        // Test modify a definition.
425
 
426
        // The rubric definition to update.
427
        $rubricdefinition = array(
428
            'id' => $definition->id,
429
            'method' => 'rubric',
430
            'name' => 'test changed',
431
            'description' => '',
432
            'status' => 20,
433
            'copiedfromid' => 1,
434
            'timecreated' => 1,
435
            'usercreated' => $teacher->id,
436
            'timemodified' => 1,
437
            'usermodified' => $teacher->id,
438
            'timecopied' => 0
439
        );
440
 
441
        // A criterion to update.
442
        $rubriccriteria1 = array (
443
             'id' => $criterion1->id,
444
             'sortorder' => 1,
445
             'description' => 'Demonstrate an understanding of rabies control',
446
             'descriptionformat' => 0
447
        );
448
 
449
        // A new criterion to add.
450
        $rubriccriteria2 = array (
451
             'sortorder' => 2,
452
             'description' => 'Demonstrate an understanding of anthrax control',
453
             'descriptionformat' => 0
454
        );
455
 
456
        // A level to update.
457
        $rubriclevel2 = array (
458
            'id' => $excellentlevelid,
459
            'score' => 75,
460
            'definition' => 'excellent',
461
            'definitionformat' => 0
462
        );
463
 
464
        // A level to insert.
465
        $rubriclevel4 = array (
466
            'score' => 100,
467
            'definition' => 'superb',
468
            'definitionformat' => 0
469
        );
470
 
471
        $rubriccriteria1['levels'] = array($rubriclevel1, $rubriclevel2, $rubriclevel3, $rubriclevel4);
472
        $rubricdefinition['rubric'] = array('rubric_criteria' => array($rubriccriteria1, $rubriccriteria2));
473
        $gradingarea['definitions'] = array($rubricdefinition);
474
 
475
        $results = core_grading_external::save_definitions(array($gradingarea));
476
 
477
        // Test definition name change.
478
        $definition = $DB->get_record('grading_definitions', array('id' => $definition->id), '*', MUST_EXIST);
479
        $this->assertEquals('test changed', $definition->name);
480
 
481
        // Test criteria description change.
482
        $modifiedcriteria = $DB->get_record('gradingform_rubric_criteria', array('id' => $criterion1->id), '*', MUST_EXIST);
483
        $this->assertEquals('Demonstrate an understanding of rabies control', $modifiedcriteria->description);
484
 
485
        // Test new criteria added.
486
        $newcriteria = $DB->get_record('gradingform_rubric_criteria',
487
                                       array('definitionid' => $definition->id, 'sortorder' => 2), '*', MUST_EXIST);
488
        $this->assertEquals('Demonstrate an understanding of anthrax control', $newcriteria->description);
489
 
490
        // Test excellent level score change from 100 to 75.
491
        $modifiedlevel = $DB->get_record('gradingform_rubric_levels', array('id' => $excellentlevelid), '*', MUST_EXIST);
492
        $this->assertEquals(75, $modifiedlevel->score);
493
 
494
        // Test new superb level added.
495
        $newlevel = $DB->get_record('gradingform_rubric_levels',
496
                                       array('criterionid' => $criterion1->id, 'score' => 100), '*', MUST_EXIST);
497
        $this->assertEquals('superb', $newlevel->definition);
498
 
499
        // Test remove a level
500
        // Test remove a criterion
501
        // The rubric definition with the removed criterion and levels.
502
        $rubricdefinition = array(
503
            'id' => $definition->id,
504
            'method' => 'rubric',
505
            'name' => 'test changed',
506
            'description' => '',
507
            'status' => 20,
508
            'copiedfromid' => 1,
509
            'timecreated' => 1,
510
            'usercreated' => $teacher->id,
511
            'timemodified' => 1,
512
            'usermodified' => $teacher->id,
513
            'timecopied' => 0
514
        );
515
 
516
        $rubriccriteria1 = array (
517
             'id' => $criterion1->id,
518
             'sortorder' => 1,
519
             'description' => 'Demonstrate an understanding of rabies control',
520
             'descriptionformat' => 0
521
        );
522
 
523
        $rubriclevel1 = array (
524
            'score' => 0,
525
            'definition' => 'fail',
526
            'definitionformat' => 0
527
        );
528
        $rubriclevel2 = array (
529
            'score' => 100,
530
            'definition' => 'pass',
531
            'definitionformat' => 0
532
        );
533
 
534
        $rubriccriteria1['levels'] = array($rubriclevel1, $rubriclevel2);
535
        $rubricdefinition['rubric'] = array('rubric_criteria' => array($rubriccriteria1));
536
        $gradingarea['definitions'] = array($rubricdefinition);
537
 
538
        $results = core_grading_external::save_definitions(array($gradingarea));
539
 
540
        // Only 1 criterion should now exist.
541
        $this->assertEquals(1, $DB->count_records('gradingform_rubric_criteria', array('definitionid' => $definition->id)));
542
        $criterion1 = $DB->get_record('gradingform_rubric_criteria', array('definitionid' => $definition->id), '*', MUST_EXIST);
543
        $this->assertEquals('Demonstrate an understanding of rabies control', $criterion1->description);
544
        // This criterion should only have 2 levels.
545
        $this->assertEquals(2, $DB->count_records('gradingform_rubric_levels', array('criterionid' => $criterion1->id)));
546
 
547
        $gradingarea['activemethod'] = 'invalid';
548
        $this->expectException('moodle_exception');
549
        $results = core_grading_external::save_definitions(array($gradingarea));
550
    }
551
 
552
    /**
553
     *
554
     * Tests save_definitions for the marking guide grading method
555
     */
11 efrain 556
    public function test_save_definitions_marking_guide(): void {
1 efrain 557
        global $DB, $CFG, $USER;
558
 
559
        $this->resetAfterTest(true);
560
        // Create a course and assignment.
561
        $course = self::getDataGenerator()->create_course();
562
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
563
        $params['course'] = $course->id;
564
        $instance = $generator->create_instance($params);
565
        $cm = get_coursemodule_from_instance('assign', $instance->id);
566
        $context = \context_module::instance($cm->id);
567
        $coursecontext = \context_course::instance($course->id);
568
 
569
        // Create the teacher.
570
        $teacher = self::getDataGenerator()->create_user();
571
        $USER->id = $teacher->id;
572
        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
573
        $this->assignUserCapability('moodle/grade:managegradingforms', $context->id, $teacherrole->id);
574
        $this->getDataGenerator()->enrol_user($teacher->id,
575
                                              $course->id,
576
                                              $teacherrole->id);
577
 
578
        // Test insert a grading area with guide definition, criteria and comments.
579
        $gradingarea = array(
580
            'cmid' => $cm->id,
581
            'contextid' => $context->id,
582
            'component' => 'mod_assign',
583
            'areaname'  => 'submissions',
584
            'activemethod' => 'guide'
585
        );
586
 
587
        $guidedefinition = array(
588
            'method' => 'guide',
589
            'name' => 'test',
590
            'description' => '',
591
            'status' => 20,
592
            'copiedfromid' => 1,
593
            'timecreated' => 1,
594
            'usercreated' => $teacher->id,
595
            'timemodified' => 1,
596
            'usermodified' => $teacher->id,
597
            'timecopied' => 0
598
        );
599
 
600
        $guidecomment = array(
601
             'sortorder' => 1,
602
             'description' => 'Students need to show that they understand the control of zoonoses',
603
             'descriptionformat' => 0
604
        );
605
        $guidecriteria1 = array (
606
             'sortorder' => 1,
607
             'shortname' => 'Rabies Control',
608
             'description' => 'Understand rabies control techniques',
609
             'descriptionformat' => 0,
610
             'descriptionmarkers' => 'Student must demonstrate that they understand rabies control',
611
             'descriptionmarkersformat' => 0,
612
             'maxscore' => 50
613
        );
614
        $guidecriteria2 = array (
615
             'sortorder' => 2,
616
             'shortname' => 'Anthrax Control',
617
             'description' => 'Understand anthrax control',
618
             'descriptionformat' => 0,
619
             'descriptionmarkers' => 'Student must demonstrate that they understand anthrax control',
620
             'descriptionmarkersformat' => 0,
621
             'maxscore' => 50
622
        );
623
 
624
        $guidedefinition['guide'] = array('guide_criteria' => array($guidecriteria1, $guidecriteria2),
625
                                          'guide_comments' => array($guidecomment));
626
        $gradingarea['definitions'] = array($guidedefinition);
627
 
628
        $results = core_grading_external::save_definitions(array($gradingarea));
629
        $area = $DB->get_record('grading_areas',
630
                                array('contextid' => $context->id, 'component' => 'mod_assign', 'areaname' => 'submissions'),
631
                                '*', MUST_EXIST);
632
        $this->assertEquals($area->activemethod, 'guide');
633
 
634
        $definition = $DB->get_record('grading_definitions', array('areaid' => $area->id, 'method' => 'guide'), '*', MUST_EXIST);
635
        $this->assertEquals($guidedefinition['name'], $definition->name);
636
        $this->assertEquals(2, $DB->count_records('gradingform_guide_criteria', array('definitionid' => $definition->id)));
637
        $this->assertEquals(1, $DB->count_records('gradingform_guide_comments', array('definitionid' => $definition->id)));
638
 
639
        // Test removal of a criteria.
640
        $guidedefinition['guide'] = array('guide_criteria' => array($guidecriteria1),
641
                                          'guide_comments' => array($guidecomment));
642
        $gradingarea['definitions'] = array($guidedefinition);
643
        $results = core_grading_external::save_definitions(array($gradingarea));
644
        $this->assertEquals(1, $DB->count_records('gradingform_guide_criteria', array('definitionid' => $definition->id)));
645
 
646
        // Test an invalid method in the definition.
647
        $guidedefinition['method'] = 'invalid';
648
        $gradingarea['definitions'] = array($guidedefinition);
649
        $this->expectException('invalid_parameter_exception');
650
        $results = core_grading_external::save_definitions(array($gradingarea));
651
    }
652
}