moodle /lib/grade/simpletest/testgradecategory.php

Language PHP Lines 670
MD5 Hash a33a8625dd6b3d5a7e90546004cd2eeb Estimated Cost $11,518 (why?)
Repository git://github.com/moodle/moodle.git View Raw File View Project SPDX
  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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<?php

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// NOTICE OF COPYRIGHT                                                   //
//                                                                       //
// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
//          http://moodle.org                                            //
//                                                                       //
// Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
//                                                                       //
// This program 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 2 of the License, or     //
// (at your option) any later version.                                   //
//                                                                       //
// This program 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:                          //
//                                                                       //
//          http://www.gnu.org/copyleft/gpl.html                         //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

/**
 * Unit tests for grade_category object.
 *
 * @author nicolas@moodle.com
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package moodlecore
 */

if (!defined('MOODLE_INTERNAL')) {
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
}

require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');

class grade_category_test extends grade_test {

    public function __construct() {
        $this->starttime = time();
        parent::__construct();
    }

    public function __destruct() {
        $this->endtime = time();
        //var_dump($this->endtime-$this->starttime);
        
        parent::__destruct();
    }

    function test_grade_category() {
        $this->sub_test_grade_category_construct();
        $this->sub_test_grade_category_build_path();
        $this->sub_test_grade_category_fetch();
        $this->sub_test_grade_category_fetch_all();
        $this->sub_test_grade_category_update();
        $this->sub_test_grade_category_delete();
        $this->sub_test_grade_category_insert();
        $this->sub_test_grade_category_qualifies_for_regrading();
        $this->sub_test_grade_category_force_regrading();
        $this->sub_test_grade_category_aggregate_grades();
        $this->sub_test_grade_category_apply_limit_rules();
        $this->sub_test_grade_category_is_aggregationcoef_used();
        $this->sub_test_grade_category_fetch_course_tree();
        $this->sub_test_grade_category_get_children();
        $this->sub_test_grade_category_load_grade_item();
        $this->sub_test_grade_category_get_grade_item();
        $this->sub_test_grade_category_load_parent_category();
        $this->sub_test_grade_category_get_parent_category();
        $this->sub_test_grade_category_get_name();
        $this->sub_test_grade_category_set_parent();
        $this->sub_test_grade_category_get_final();
        $this->sub_test_grade_category_get_sortorder();
        $this->sub_test_grade_category_set_sortorder();
        $this->sub_test_grade_category_is_editable();
        $this->sub_test_grade_category_move_after_sortorder();
        $this->sub_test_grade_category_is_course_category();
        $this->sub_test_grade_category_fetch_course_category();
        $this->sub_test_grade_category_is_locked();
        $this->sub_test_grade_category_set_locked();
        $this->sub_test_grade_category_is_hidden();
        $this->sub_test_grade_category_set_hidden();

        //this won't work until MDL-11837 is complete
        //$this->sub_test_grade_category_generate_grades();

        //do this last as adding a second course category messes up the data
        $this->sub_test_grade_category_insert_course_category();
    }

    //adds 3 new grade categories at various depths
    function sub_test_grade_category_construct() {
        $course_category = grade_category::fetch_course_category($this->courseid);

        $params = new stdClass();

        $params->courseid = $this->courseid;
        $params->fullname = 'unittestcategory4';

        $grade_category = new grade_category($params, false);
        $grade_category->insert();
        $this->grade_categories[] = $grade_category;

        $this->assertEqual($params->courseid, $grade_category->courseid);
        $this->assertEqual($params->fullname, $grade_category->fullname);
        $this->assertEqual(2, $grade_category->depth);
        $this->assertEqual("/$course_category->id/$grade_category->id/", $grade_category->path);
        $parentpath = $grade_category->path;

        // Test a child category
        $params->parent = $grade_category->id;
        $params->fullname = 'unittestcategory5';
        $grade_category = new grade_category($params, false);
        $grade_category->insert();
        $this->grade_categories[] = $grade_category;

        $this->assertEqual(3, $grade_category->depth);
        $this->assertEqual($parentpath.$grade_category->id."/", $grade_category->path);
        $parentpath = $grade_category->path;

        // Test a third depth category
        $params->parent = $grade_category->id;
        $params->fullname = 'unittestcategory6';
        $grade_category = new grade_category($params, false);
        $grade_category->insert();
        $this->grade_categories[50] = $grade_category;//going to delete this one later hence the special index
        
        $this->assertEqual(4, $grade_category->depth);
        $this->assertEqual($parentpath.$grade_category->id."/", $grade_category->path);
    }

    function sub_test_grade_category_build_path() {
        $grade_category = new grade_category($this->grade_categories[1]);
        $this->assertTrue(method_exists($grade_category, 'build_path'));
        $path = grade_category::build_path($grade_category);
        $this->assertEqual($grade_category->path, $path);
    }

    function sub_test_grade_category_fetch() {
        $grade_category = new grade_category();
        $this->assertTrue(method_exists($grade_category, 'fetch'));

        $grade_category = grade_category::fetch(array('id'=>$this->grade_categories[0]->id));
        $this->assertEqual($this->grade_categories[0]->id, $grade_category->id);
        $this->assertEqual($this->grade_categories[0]->fullname, $grade_category->fullname);
    }

    function sub_test_grade_category_fetch_all() {
        $grade_category = new grade_category();
        $this->assertTrue(method_exists($grade_category, 'fetch_all'));

        $grade_categories = grade_category::fetch_all(array('courseid'=>$this->courseid));
        $this->assertEqual(count($this->grade_categories), count($grade_categories)-1);
    }

    function sub_test_grade_category_update() {
        global $DB;
        $grade_category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($grade_category, 'update'));

        $grade_category->fullname = 'Updated info for this unittest grade_category';
        $grade_category->path = null; // path must be recalculated if missing
        $grade_category->depth = null;
        $grade_category->aggregation = GRADE_AGGREGATE_MAX; // should force regrading

        $grade_item = $grade_category->get_grade_item();
        $this->assertEqual(0, $grade_item->needsupdate);

        $this->assertTrue($grade_category->update());

        $fullname = $DB->get_field('grade_categories', 'fullname', array('id' => $this->grade_categories[0]->id));
        $this->assertEqual($grade_category->fullname, $fullname);

        $path = $DB->get_field('grade_categories', 'path', array('id' => $this->grade_categories[0]->id));
        $this->assertEqual($grade_category->path, $path);

        $depth = $DB->get_field('grade_categories', 'depth', array('id' => $this->grade_categories[0]->id));
        $this->assertEqual($grade_category->depth, $depth);

        $grade_item = $grade_category->get_grade_item();
        $this->assertEqual(1, $grade_item->needsupdate);
    }

    function sub_test_grade_category_delete() {
        global $DB;

        $grade_category = new grade_category($this->grade_categories[50]);
        $this->assertTrue(method_exists($grade_category, 'delete'));

        $this->assertTrue($grade_category->delete());
        $this->assertFalse($DB->get_record('grade_categories', array('id' => $grade_category->id)));
    }

    function sub_test_grade_category_insert() {
        $course_category = grade_category::fetch_course_category($this->courseid);

        $grade_category = new grade_category();
        $this->assertTrue(method_exists($grade_category, 'insert'));

        $grade_category->fullname    = 'unittestcategory4';
        $grade_category->courseid    = $this->courseid;
        $grade_category->aggregation = GRADE_AGGREGATE_MEAN;
        $grade_category->aggregateonlygraded = 1;
        $grade_category->keephigh    = 100;
        $grade_category->droplow     = 10;
        $grade_category->hidden      = 0;
        $grade_category->parent      = $this->grade_categories[1]->id; //sub_test_grade_category_delete() removed the category at 0

        $grade_category->insert();

        $this->assertEqual('/'.$course_category->id.'/'.$this->grade_categories[1]->parent.'/'.$this->grade_categories[1]->id.'/'.$grade_category->id.'/', $grade_category->path);
        $this->assertEqual(4, $grade_category->depth);

        $last_grade_category = end($this->grade_categories);

        $this->assertFalse(empty($grade_category->grade_item));
        $this->assertEqual($grade_category->id, $grade_category->grade_item->iteminstance);
        $this->assertEqual('category', $grade_category->grade_item->itemtype);

        $this->assertEqual($grade_category->id, $last_grade_category->id + 1);
        $this->assertFalse(empty($grade_category->timecreated));
        $this->assertFalse(empty($grade_category->timemodified));
    }

    function sub_test_grade_category_qualifies_for_regrading() {
        $grade_category = new grade_category($this->grade_categories[1]);
        $this->assertTrue(method_exists($grade_category, 'qualifies_for_regrading'));
        $this->assertFalse($grade_category->qualifies_for_regrading());

        $grade_category->aggregation = GRADE_AGGREGATE_MAX;
        $this->assertTrue($grade_category->qualifies_for_regrading());

        $grade_category = new grade_category($this->grade_categories[1]);
        $grade_category->droplow = 99;
        $this->assertTrue($grade_category->qualifies_for_regrading());

        $grade_category = new grade_category($this->grade_categories[1]);
        $grade_category->keephigh = 99;
        $this->assertTrue($grade_category->qualifies_for_regrading());
    }

    function sub_test_grade_category_force_regrading() {
        $grade_category = new grade_category($this->grade_categories[1]);
        $this->assertTrue(method_exists($grade_category, 'force_regrading'));

        $grade_category->load_grade_item();
        $this->assertEqual(0, $grade_category->grade_item->needsupdate);

        $grade_category->force_regrading();

        $grade_category->grade_item = null;
        $grade_category->load_grade_item();

        $this->assertEqual(1, $grade_category->grade_item->needsupdate);
    }

    /**
     * Tests the calculation of grades using the various aggregation methods with and without hidden grades
     * This will not work entirely until MDL-11837 is done
     * @global type $DB 
     */
    function sub_test_grade_category_generate_grades() {
        global $DB;

        //inserting some special grade items to make testing the final grade calculation easier
        $params->courseid = $this->courseid;
        $params->fullname = 'unittestgradecalccategory';
        $params->aggregation = GRADE_AGGREGATE_MEAN;
        $params->aggregateonlygraded = 0;
        $grade_category = new grade_category($params, false);
        $grade_category->insert();

        $this->assertTrue(method_exists($grade_category, 'generate_grades'));

        $grade_category->load_grade_item();
        $cgi = $grade_category->get_grade_item();
        $cgi->grademin = 0;
        $cgi->grademax = 20;//3 grade items out of 10 but category is out of 20 to force scaling to occur
        $cgi->update();

        //3 grade items each with a maximum grade of 10
        $grade_items = array();
        for ($i=0; $i<3; $i++) {
            $grade_items[$i] = new grade_item();
            $grade_items[$i]->courseid = $this->courseid;
            $grade_items[$i]->categoryid = $grade_category->id;
            $grade_items[$i]->itemname = 'manual grade_item '.$i;
            $grade_items[$i]->itemtype = 'manual';
            $grade_items[$i]->itemnumber = 0;
            $grade_items[$i]->needsupdate = false;
            $grade_items[$i]->gradetype = GRADE_TYPE_VALUE;
            $grade_items[$i]->grademin = 0;
            $grade_items[$i]->grademax = 10;
            $grade_items[$i]->iteminfo = 'Manual grade item used for unit testing';
            $grade_items[$i]->timecreated = mktime();
            $grade_items[$i]->timemodified = mktime();
            
            //used as the weight by weighted mean and as extra credit by mean with extra credit
            //Will be 0, 1 and 2
            $grade_items[$i]->aggregationcoef = $i;
            
            $grade_items[$i]->insert();
        }

        //a grade for each grade item
        $grade_grades = array();
        for ($i=0; $i<3; $i++) {
            $grade_grades[$i] = new grade_grade();
            $grade_grades[$i]->itemid = $grade_items[$i]->id;
            $grade_grades[$i]->userid = $this->userid;
            $grade_grades[$i]->rawgrade = ($i+1)*2;//produce grade grades of 2, 4 and 6
            $grade_grades[$i]->finalgrade = ($i+1)*2;
            $grade_grades[$i]->timecreated = mktime();
            $grade_grades[$i]->timemodified = mktime();
            $grade_grades[$i]->information = '1 of 2 grade_grades';
            $grade_grades[$i]->informationformat = FORMAT_PLAIN;
            $grade_grades[$i]->feedback = 'Good, but not good enough..';
            $grade_grades[$i]->feedbackformat = FORMAT_PLAIN;

            $grade_grades[$i]->insert();
        }

        //3 grade items with 1 grade_grade each.
        //grade grades have the values 2, 4 and 6
        
        //First correct answer is the aggregate with all 3 grades
        //Second correct answer is with the first grade (value 2) hidden
        
        $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_MEDIAN, 'GRADE_AGGREGATE_MEDIAN', 8, 8);
        $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_MAX, 'GRADE_AGGREGATE_MAX', 12, 12);
        $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_MODE, 'GRADE_AGGREGATE_MODE', 12, 12);
        
        //weighted mean. note grade totals are rounded to an int to prevent rounding discrepancies. correct final grade isnt actually exactly 10
        //3 items with grades 2, 4 and 6 with weights 0, 1 and 2 and all out of 10. then doubled to be out of 20.
        $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_WEIGHTED_MEAN, 'GRADE_AGGREGATE_WEIGHTED_MEAN', 10, 10);

        //simple weighted mean
        //3 items with grades 2, 4 and 6 equally weighted and all out of 10. then doubled to be out of 20.
        $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_WEIGHTED_MEAN2, 'GRADE_AGGREGATE_WEIGHTED_MEAN2', 8, 10);

        //mean of grades with extra credit
        //3 items with grades 2, 4 and 6 with extra credit 0, 1 and 2 equally weighted and all out of 10. then doubled to be out of 20.
        $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_EXTRACREDIT_MEAN, 'GRADE_AGGREGATE_EXTRACREDIT_MEAN', 10, 13);
        
        //aggregation tests the are affected by a hidden grade currently dont work as we dont store the altered grade in the database
        //instead an in memory recalculation is done. This should be remedied by MDL-11837

        //fails with 1 grade hidden. still reports 8 as being correct
        $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_MEAN, 'GRADE_AGGREGATE_MEAN', 8, 10);
        
        //fails with 1 grade hidden. still reports 4 as being correct
        $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_MIN, 'GRADE_AGGREGATE_MIN', 4, 8);
        
        //fails with 1 grade hidden. still reports 12 as being correct
        $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_SUM, 'GRADE_AGGREGATE_SUM', 12, 10);
    }

    /**
     * Test grade category aggregation using the supplied grade objects and aggregation method
     * @param grade_category $grade_category the category to be tested
     * @param array $grade_items array of instance of grade_item
     * @param array $grade_grades array of instances of grade_grade
     * @param int $aggmethod the aggregation method to apply ie GRADE_AGGREGATE_MEAN
     * @param string $aggmethodname the name of the aggregation method to apply. Used to display any test failure messages
     * @param int $correct1 the correct final grade for the category with NO items hidden
     * @param int $correct2 the correct final grade for the category with the grade at $grade_grades[0] hidden
     * @return void
    */
    function helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, $aggmethod, $aggmethodname, $correct1, $correct2) {
        global $DB;

        $grade_category->aggregation = $aggmethod;
        $grade_category->update();

        //check grade_item isnt hidden from a previous test
        $grade_items[0]->set_hidden(0, true);
        $this->helper_test_grade_aggregation_result($grade_category, $correct1, 'Testing aggregation method('.$aggmethodname.') with no items hidden %s');

        //hide the grade item with grade of 2
        $grade_items[0]->set_hidden(1, true);
        $this->helper_test_grade_aggregation_result($grade_category, $correct2, 'Testing aggregation method('.$aggmethodname.') with 1 item hidden %s');
    }

    /**
     * Verify the value of the category grade item for $this->userid
     * @param grade_category $grade_category the category to be tested
     * @param int $correctgrade the expected grade
     * @param string msg The message that should be displayed if the correct grade is not found
     * @return void
     */
    function helper_test_grade_aggregation_result($grade_category, $correctgrade, $msg) {
        global $DB;
        
        $category_grade_item = $grade_category->get_grade_item();
        
        //this creates all the grade_grades we need
        grade_regrade_final_grades($this->courseid);
        
        $grade = $DB->get_record('grade_grades', array('itemid'=>$category_grade_item->id, 'userid'=>$this->userid));
        $this->assertWithinMargin($grade->rawgrade, $grade->rawgrademin, $grade->rawgrademax);
        $this->assertEqual(intval($correctgrade), intval($grade->finalgrade), $msg);
        
        /*
         * TODO this doesnt work as the grade_grades created by $grade_category->generate_grades(); dont
         * observe the category's max grade
        //delete the grade_grades for the category itself and check they get recreated correctly
        $DB->delete_records('grade_grades', array('itemid'=>$category_grade_item->id));
        $grade_category->generate_grades();
        
        $grade = $DB->get_record('grade_grades', array('itemid'=>$category_grade_item->id, 'userid'=>$this->userid));
        $this->assertWithinMargin($grade->rawgrade, $grade->rawgrademin, $grade->rawgrademax);
        $this->assertEqual(intval($correctgrade), intval($grade->finalgrade), $msg);
         * 
         */
    }

    function sub_test_grade_category_aggregate_grades() {
        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($category, 'aggregate_grades'));
        // tested more fully via test_grade_category_generate_grades()
    }

    function sub_test_grade_category_apply_limit_rules() {
        $items[$this->grade_items[0]->id] = new grade_item($this->grade_items[0], false);
        $items[$this->grade_items[1]->id] = new grade_item($this->grade_items[1], false);
        $items[$this->grade_items[2]->id] = new grade_item($this->grade_items[2], false);
        $items[$this->grade_items[4]->id] = new grade_item($this->grade_items[4], false);

        $category = new grade_category();
        $category->droplow = 2;
        $grades = array($this->grade_items[0]->id=>5.374,
                        $this->grade_items[1]->id=>9.4743,
                        $this->grade_items[2]->id=>2.5474,
                        $this->grade_items[4]->id=>7.3754);
        $category->apply_limit_rules($grades, $items);
        $this->assertEqual(count($grades), 2);
        $this->assertEqual($grades[$this->grade_items[1]->id], 9.4743);
        $this->assertEqual($grades[$this->grade_items[4]->id], 7.3754);

        $category = new grade_category();
        $category->keephigh = 1;
        $category->droplow = 0;
        $grades = array($this->grade_items[0]->id=>5.374,
                        $this->grade_items[1]->id=>9.4743,
                        $this->grade_items[2]->id=>2.5474,
                        $this->grade_items[4]->id=>7.3754);
        $category->apply_limit_rules($grades, $items);
        $this->assertEqual(count($grades), 1);
        $grade = reset($grades);
        $this->assertEqual(9.4743, $grade);

        $category = new grade_category();
        $category->droplow     = 2;
        $category->aggregation = GRADE_AGGREGATE_SUM;
        $items[$this->grade_items[2]->id]->aggregationcoef = 1;
        $grades = array($this->grade_items[0]->id=>5.374,
                        $this->grade_items[1]->id=>9.4743,
                        $this->grade_items[2]->id=>2.5474,
                        $this->grade_items[4]->id=>7.3754);

        $category->apply_limit_rules($grades, $items);
        $this->assertEqual(count($grades), 2);
        $this->assertEqual($grades[$this->grade_items[1]->id], 9.4743);
        $this->assertEqual($grades[$this->grade_items[2]->id], 2.5474);

        $category = new grade_category();
        $category->keephigh = 1;
        $category->droplow = 0;
        $category->aggregation = GRADE_AGGREGATE_SUM;
        $items[$this->grade_items[2]->id]->aggregationcoef = 1;
        $grades = array($this->grade_items[0]->id=>5.374,
                        $this->grade_items[1]->id=>9.4743,
                        $this->grade_items[2]->id=>2.5474,
                        $this->grade_items[4]->id=>7.3754);
        $category->apply_limit_rules($grades, $items);
        $this->assertEqual(count($grades), 2);
        $this->assertEqual($grades[$this->grade_items[1]->id], 9.4743);
        $this->assertEqual($grades[$this->grade_items[2]->id], 2.5474);
    }

    /**
     * TODO implement
     */
    function sub_test_grade_category_is_aggregationcoef_used() {

    }

    function sub_test_grade_category_fetch_course_tree() {
        $category = new grade_category();
        $this->assertTrue(method_exists($category, 'fetch_course_tree'));
        //TODO: add some tests
    }

    function sub_test_grade_category_get_children() {
        $course_category = grade_category::fetch_course_category($this->courseid);

        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($category, 'get_children'));

        $children_array = $category->get_children(0);

        $this->assertTrue(is_array($children_array));
        $this->assertFalse(empty($children_array[2]));
        $this->assertFalse(empty($children_array[2]['object']));
        $this->assertFalse(empty($children_array[2]['children']));
        $this->assertEqual($this->grade_categories[1]->id, $children_array[2]['object']->id);
        $this->assertEqual($this->grade_categories[2]->id, $children_array[5]['object']->id);
        $this->assertEqual($this->grade_items[0]->id, $children_array[2]['children'][3]['object']->id);
        $this->assertEqual($this->grade_items[1]->id, $children_array[2]['children'][4]['object']->id);
        $this->assertEqual($this->grade_items[2]->id, $children_array[5]['children'][6]['object']->id);
    }

    function sub_test_grade_category_load_grade_item() {
        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($category, 'load_grade_item'));
        $this->assertEqual(null, $category->grade_item);
        $category->load_grade_item();
        $this->assertEqual($this->grade_items[3]->id, $category->grade_item->id);
    }

    function sub_test_grade_category_get_grade_item() {
        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($category, 'get_grade_item'));
        $grade_item = $category->get_grade_item();
        $this->assertEqual($this->grade_items[3]->id, $grade_item->id);
    }

    function sub_test_grade_category_load_parent_category() {
        $category = new grade_category($this->grade_categories[1]);
        $this->assertTrue(method_exists($category, 'load_parent_category'));
        $this->assertEqual(null, $category->parent_category);
        $category->load_parent_category();
        $this->assertEqual($this->grade_categories[0]->id, $category->parent_category->id);
    }

    function sub_test_grade_category_get_parent_category() {
        $category = new grade_category($this->grade_categories[1]);
        $this->assertTrue(method_exists($category, 'get_parent_category'));
        $parent_category = $category->get_parent_category();
        $this->assertEqual($this->grade_categories[0]->id, $parent_category->id);
    }

    function sub_test_grade_category_get_name() {
        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($category, 'get_name'));
        $this->assertEqual($this->grade_categories[0]->fullname, $category->get_name());
    }

    function sub_test_grade_category_set_parent() {
        $category = new grade_category($this->grade_categories[1]);
        $this->assertTrue(method_exists($category, 'set_parent'));
        // TODO: implement detailed tests

        $course_category = grade_category::fetch_course_category($this->courseid);
        $this->assertTrue($category->set_parent($course_category->id));
        $this->assertEqual($course_category->id, $category->parent);
    }

    function sub_test_grade_category_get_final() {
        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($category, 'get_final'));
        $category->load_grade_item();
        $this->assertEqual($category->get_final(), $category->grade_item->get_final());
    }

    function sub_test_grade_category_get_sortorder() {
        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($category, 'get_sortorder'));
        $category->load_grade_item();
        $this->assertEqual($category->get_sortorder(), $category->grade_item->get_sortorder());
    }

    function sub_test_grade_category_set_sortorder() {
        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($category, 'set_sortorder'));
        $category->load_grade_item();
        $this->assertEqual($category->set_sortorder(10), $category->grade_item->set_sortorder(10));
    }

    function sub_test_grade_category_move_after_sortorder() {
        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($category, 'move_after_sortorder'));
        $category->load_grade_item();
        $this->assertEqual($category->move_after_sortorder(10), $category->grade_item->move_after_sortorder(10));
    }

    function sub_test_grade_category_is_course_category() {
        $category = grade_category::fetch_course_category($this->courseid);
        $this->assertTrue(method_exists($category, 'is_course_category'));
        $this->assertTrue($category->is_course_category());
    }

    function sub_test_grade_category_fetch_course_category() {
        $category = new grade_category();
        $this->assertTrue(method_exists($category, 'fetch_course_category'));
        $category = grade_category::fetch_course_category($this->courseid);
        $this->assertTrue(empty($category->parent));
    }
    /**
     * TODO implement
     */
    function sub_test_grade_category_is_editable() {

    }

    function sub_test_grade_category_is_locked() {
        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($category, 'is_locked'));
        $category->load_grade_item();
        $this->assertEqual($category->is_locked(), $category->grade_item->is_locked());
    }

    function sub_test_grade_category_set_locked() {
        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($category, 'set_locked'));

        //will return false as cannot lock a grade that needs updating
        $this->assertFalse($category->set_locked(1));
        grade_regrade_final_grades($this->courseid);

        //get the category from the db again
        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue($category->set_locked(1));
    }

    function sub_test_grade_category_is_hidden() {
        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($category, 'is_hidden'));
        $category->load_grade_item();
        $this->assertEqual($category->is_hidden(), $category->grade_item->is_hidden());
    }

    function sub_test_grade_category_set_hidden() {
        $category = new grade_category($this->grade_categories[0]);
        $this->assertTrue(method_exists($category, 'set_hidden'));
        $category->set_hidden(1);
        $category->load_grade_item();
        $this->assertEqual(true, $category->grade_item->is_hidden());
    }

    //beware: adding a duplicate course category messes up the data in a way that's hard to recover from
    function sub_test_grade_category_insert_course_category() {
        $grade_category = new grade_category();
        $this->assertTrue(method_exists($grade_category, 'insert_course_category'));

        $id = $grade_category->insert_course_category($this->courseid);
        $this->assertNotNull($id);
        $this->assertEqual('?', $grade_category->fullname);
        $this->assertEqual(GRADE_AGGREGATE_WEIGHTED_MEAN2, $grade_category->aggregation);
        $this->assertEqual("/$id/", $grade_category->path);
        $this->assertEqual(1, $grade_category->depth);
        $this->assertNull($grade_category->parent);
    }

    function generate_random_raw_grade($item, $userid) {
        $grade = new grade_grade();
        $grade->itemid = $item->id;
        $grade->userid = $userid;
        $grade->grademin = 0;
        $grade->grademax = 1;
        $valuetype = "grade$item->gradetype";
        $grade->rawgrade = rand(0, 1000) / 1000;
        $grade->insert();
        return $grade->rawgrade;
    }
}
Back to Top