forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrade_category.php
1650 lines (1344 loc) · 55.4 KB
/
grade_category.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
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Definitions of constants for gradebook
*
* @package core
* @subpackage grade
* @copyright 2006 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once('grade_object.php');
/**
* Grade_Category is an object mapped to DB table {prefix}grade_categories
*
* @package moodlecore
* @subpackage grade
* @copyright 2007 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class grade_category extends grade_object {
/**
* The DB table.
* @var string $table
*/
public $table = 'grade_categories';
/**
* Array of required table fields, must start with 'id'.
* @var array $required_fields
*/
public $required_fields = array('id', 'courseid', 'parent', 'depth', 'path', 'fullname', 'aggregation',
'keephigh', 'droplow', 'aggregateonlygraded', 'aggregateoutcomes',
'aggregatesubcats', 'timecreated', 'timemodified', 'hidden');
/**
* The course this category belongs to.
* @var int $courseid
*/
public $courseid;
/**
* The category this category belongs to (optional).
* @var int $parent
*/
public $parent;
/**
* The grade_category object referenced by $this->parent (PK).
* @var object $parent_category
*/
public $parent_category;
/**
* The number of parents this category has.
* @var int $depth
*/
public $depth = 0;
/**
* Shows the hierarchical path for this category as /1/2/3/ (like course_categories), the last number being
* this category's autoincrement ID number.
* @var string $path
*/
public $path;
/**
* The name of this category.
* @var string $fullname
*/
public $fullname;
/**
* A constant pointing to one of the predefined aggregation strategies (none, mean, median, sum etc) .
* @var int $aggregation
*/
public $aggregation = GRADE_AGGREGATE_MEAN;
/**
* Keep only the X highest items.
* @var int $keephigh
*/
public $keephigh = 0;
/**
* Drop the X lowest items.
* @var int $droplow
*/
public $droplow = 0;
/**
* Aggregate only graded items
* @var int $aggregateonlygraded
*/
public $aggregateonlygraded = 0;
/**
* Aggregate outcomes together with normal items
* @var int $aggregateoutcomes
*/
public $aggregateoutcomes = 0;
/**
* Ignore subcategories when aggregating
* @var int $aggregatesubcats
*/
public $aggregatesubcats = 0;
/**
* Array of grade_items or grade_categories nested exactly 1 level below this category
* @var array $children
*/
public $children;
/**
* A hierarchical array of all children below this category. This is stored separately from
* $children because it is more memory-intensive and may not be used as often.
* @var array $all_children
*/
public $all_children;
/**
* An associated grade_item object, with itemtype=category, used to calculate and cache a set of grade values
* for this category.
* @var object $grade_item
*/
public $grade_item;
/**
* Temporary sortorder for speedup of children resorting
*/
public $sortorder;
/**
* List of options which can be "forced" from site settings.
*/
public $forceable = array('aggregation', 'keephigh', 'droplow', 'aggregateonlygraded', 'aggregateoutcomes', 'aggregatesubcats');
/**
* String representing the aggregation coefficient. Variable is used as cache.
*/
public $coefstring = null;
/**
* Builds this category's path string based on its parents (if any) and its own id number.
* This is typically done just before inserting this object in the DB for the first time,
* or when a new parent is added or changed. It is a recursive function: once the calling
* object no longer has a parent, the path is complete.
*
* @param object $grade_category A Grade_Category object
* @return int The depth of this category (2 means there is one parent)
* @static
*/
public static function build_path($grade_category) {
global $DB;
if (empty($grade_category->parent)) {
return '/'.$grade_category->id.'/';
} else {
$parent = $DB->get_record('grade_categories', array('id' => $grade_category->parent));
return grade_category::build_path($parent).$grade_category->id.'/';
}
}
/**
* Finds and returns a grade_category instance based on params.
* @static
*
* @param array $params associative arrays varname=>value
* @return object grade_category instance or false if none found.
*/
public static function fetch($params) {
return grade_object::fetch_helper('grade_categories', 'grade_category', $params);
}
/**
* Finds and returns all grade_category instances based on params.
* @static
*
* @param array $params associative arrays varname=>value
* @return array array of grade_category insatnces or false if none found.
*/
public static function fetch_all($params) {
return grade_object::fetch_all_helper('grade_categories', 'grade_category', $params);
}
/**
* In addition to update() as defined in grade_object, call force_regrading of parent categories, if applicable.
* @param string $source from where was the object updated (mod/forum, manual, etc.)
* @return boolean success
*/
public function update($source=null) {
// load the grade item or create a new one
$this->load_grade_item();
// force recalculation of path;
if (empty($this->path)) {
$this->path = grade_category::build_path($this);
$this->depth = substr_count($this->path, '/') - 1;
$updatechildren = true;
} else {
$updatechildren = false;
}
$this->apply_forced_settings();
// these are exclusive
if ($this->droplow > 0) {
$this->keephigh = 0;
} else if ($this->keephigh > 0) {
$this->droplow = 0;
}
// Recalculate grades if needed
if ($this->qualifies_for_regrading()) {
$this->force_regrading();
}
$this->timemodified = time();
$result = parent::update($source);
// now update paths in all child categories
if ($result and $updatechildren) {
if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
foreach ($children as $child) {
$child->path = null;
$child->depth = 0;
$child->update($source);
}
}
}
return $result;
}
/**
* If parent::delete() is successful, send force_regrading message to parent category.
* @param string $source from where was the object deleted (mod/forum, manual, etc.)
* @return boolean success
*/
public function delete($source=null) {
$grade_item = $this->load_grade_item();
if ($this->is_course_category()) {
if ($categories = grade_category::fetch_all(array('courseid'=>$this->courseid))) {
foreach ($categories as $category) {
if ($category->id == $this->id) {
continue; // do not delete course category yet
}
$category->delete($source);
}
}
if ($items = grade_item::fetch_all(array('courseid'=>$this->courseid))) {
foreach ($items as $item) {
if ($item->id == $grade_item->id) {
continue; // do not delete course item yet
}
$item->delete($source);
}
}
} else {
$this->force_regrading();
$parent = $this->load_parent_category();
// Update children's categoryid/parent field first
if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
foreach ($children as $child) {
$child->set_parent($parent->id);
}
}
if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
foreach ($children as $child) {
$child->set_parent($parent->id);
}
}
}
// first delete the attached grade item and grades
$grade_item->delete($source);
// delete category itself
return parent::delete($source);
}
/**
* In addition to the normal insert() defined in grade_object, this method sets the depth
* and path for this object, and update the record accordingly. The reason why this must
* be done here instead of in the constructor, is that they both need to know the record's
* id number, which only gets created at insertion time.
* This method also creates an associated grade_item if this wasn't done during construction.
* @param string $source from where was the object inserted (mod/forum, manual, etc.)
* @return int PK ID if successful, false otherwise
*/
public function insert($source=null) {
if (empty($this->courseid)) {
print_error('cannotinsertgrade');
}
if (empty($this->parent)) {
$course_category = grade_category::fetch_course_category($this->courseid);
$this->parent = $course_category->id;
}
$this->path = null;
$this->timecreated = $this->timemodified = time();
if (!parent::insert($source)) {
debugging("Could not insert this category: " . print_r($this, true));
return false;
}
$this->force_regrading();
// build path and depth
$this->update($source);
return $this->id;
}
/**
* Internal function - used only from fetch_course_category()
* Normal insert() can not be used for course category
*
* @param int $courseid The course ID
*
* @return bool success
*/
public function insert_course_category($courseid) {
$this->courseid = $courseid;
$this->fullname = '?';
$this->path = null;
$this->parent = null;
$this->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
$this->apply_default_settings();
$this->apply_forced_settings();
$this->timecreated = $this->timemodified = time();
if (!parent::insert('system')) {
debugging("Could not insert this category: " . print_r($this, true));
return false;
}
// build path and depth
$this->update('system');
return $this->id;
}
/**
* Compares the values held by this object with those of the matching record in DB, and returns
* whether or not these differences are sufficient to justify an update of all parent objects.
* This assumes that this object has an id number and a matching record in DB. If not, it will return false.
* @return boolean
*/
public function qualifies_for_regrading() {
if (empty($this->id)) {
debugging("Can not regrade non existing category");
return false;
}
$db_item = grade_category::fetch(array('id'=>$this->id));
$aggregationdiff = $db_item->aggregation != $this->aggregation;
$keephighdiff = $db_item->keephigh != $this->keephigh;
$droplowdiff = $db_item->droplow != $this->droplow;
$aggonlygrddiff = $db_item->aggregateonlygraded != $this->aggregateonlygraded;
$aggoutcomesdiff = $db_item->aggregateoutcomes != $this->aggregateoutcomes;
$aggsubcatsdiff = $db_item->aggregatesubcats != $this->aggregatesubcats;
return ($aggregationdiff || $keephighdiff || $droplowdiff || $aggonlygrddiff || $aggoutcomesdiff || $aggsubcatsdiff);
}
/**
* Marks the category and course item as needing update - categories are always regraded.
* @return void
*/
public function force_regrading() {
$grade_item = $this->load_grade_item();
$grade_item->force_regrading();
}
/**
* Generates and saves final grades in associated category grade item.
* These immediate children must already have their own final grades.
* The category's aggregation method is used to generate final grades.
*
* Please note that category grade is either calculated or aggregated - not both at the same time.
*
* This method must be used ONLY from grade_item::regrade_final_grades(),
* because the calculation must be done in correct order!
*
* Steps to follow:
* 1. Get final grades from immediate children
* 3. Aggregate these grades
* 4. Save them in final grades of associated category grade item
*
* @param int $userid The user ID
*
* @return bool
*/
public function generate_grades($userid=null) {
global $CFG, $DB;
$this->load_grade_item();
if ($this->grade_item->is_locked()) {
return true; // no need to recalculate locked items
}
// find grade items of immediate children (category or grade items) and force site settings
$depends_on = $this->grade_item->depends_on();
if (empty($depends_on)) {
$items = false;
} else {
list($usql, $params) = $DB->get_in_or_equal($depends_on);
$sql = "SELECT *
FROM {grade_items}
WHERE id $usql";
$items = $DB->get_records_sql($sql, $params);
}
// needed mostly for SUM agg type
$this->auto_update_max($items);
$grade_inst = new grade_grade();
$fields = 'g.'.implode(',g.', $grade_inst->required_fields);
// where to look for final grades - include grade of this item too, we will store the results there
$gis = array_merge($depends_on, array($this->grade_item->id));
list($usql, $params) = $DB->get_in_or_equal($gis);
if ($userid) {
$usersql = "AND g.userid=?";
$params[] = $userid;
} else {
$usersql = "";
}
$sql = "SELECT $fields
FROM {grade_grades} g, {grade_items} gi
WHERE gi.id = g.itemid AND gi.id $usql $usersql
ORDER BY g.userid";
// group the results by userid and aggregate the grades for this user
if ($rs = $DB->get_recordset_sql($sql, $params)) {
$prevuser = 0;
$grade_values = array();
$excluded = array();
$oldgrade = null;
foreach ($rs as $used) {
if ($used->userid != $prevuser) {
$this->aggregate_grades($prevuser, $items, $grade_values, $oldgrade, $excluded);
$prevuser = $used->userid;
$grade_values = array();
$excluded = array();
$oldgrade = null;
}
$grade_values[$used->itemid] = $used->finalgrade;
if ($used->excluded) {
$excluded[] = $used->itemid;
}
if ($this->grade_item->id == $used->itemid) {
$oldgrade = $used;
}
}
$rs->close();
$this->aggregate_grades($prevuser, $items, $grade_values, $oldgrade, $excluded);//the last one
}
return true;
}
/**
* internal function for category grades aggregation
*
* @param int $userid The User ID
* @param array $items Grade items
* @param array $grade_values Array of grade values
* @param object $oldgrade Old grade
* @param array $excluded Excluded
*
* @return boolean (just plain return;)
* @todo Document correctly
*/
private function aggregate_grades($userid, $items, $grade_values, $oldgrade, $excluded) {
global $CFG;
if (empty($userid)) {
//ignore first call
return;
}
if ($oldgrade) {
$oldfinalgrade = $oldgrade->finalgrade;
$grade = new grade_grade($oldgrade, false);
$grade->grade_item =& $this->grade_item;
} else {
// insert final grade - it will be needed later anyway
$grade = new grade_grade(array('itemid'=>$this->grade_item->id, 'userid'=>$userid), false);
$grade->grade_item =& $this->grade_item;
$grade->insert('system');
$oldfinalgrade = null;
}
// no need to recalculate locked or overridden grades
if ($grade->is_locked() or $grade->is_overridden()) {
return;
}
// can not use own final category grade in calculation
unset($grade_values[$this->grade_item->id]);
// sum is a special aggregation types - it adjusts the min max, does not use relative values
if ($this->aggregation == GRADE_AGGREGATE_SUM) {
$this->sum_grades($grade, $oldfinalgrade, $items, $grade_values, $excluded);
return;
}
// if no grades calculation possible or grading not allowed clear final grade
if (empty($grade_values) or empty($items) or ($this->grade_item->gradetype != GRADE_TYPE_VALUE and $this->grade_item->gradetype != GRADE_TYPE_SCALE)) {
$grade->finalgrade = null;
if (!is_null($oldfinalgrade)) {
$grade->update('aggregation');
}
return;
}
// normalize the grades first - all will have value 0...1
// ungraded items are not used in aggregation
foreach ($grade_values as $itemid=>$v) {
if (is_null($v)) {
// null means no grade
unset($grade_values[$itemid]);
continue;
} else if (in_array($itemid, $excluded)) {
unset($grade_values[$itemid]);
continue;
}
$grade_values[$itemid] = grade_grade::standardise_score($v, $items[$itemid]->grademin, $items[$itemid]->grademax, 0, 1);
}
// use min grade if grade missing for these types
if (!$this->aggregateonlygraded) {
foreach ($items as $itemid=>$value) {
if (!isset($grade_values[$itemid]) and !in_array($itemid, $excluded)) {
$grade_values[$itemid] = 0;
}
}
}
// limit and sort
$this->apply_limit_rules($grade_values, $items);
asort($grade_values, SORT_NUMERIC);
// let's see we have still enough grades to do any statistics
if (count($grade_values) == 0) {
// not enough attempts yet
$grade->finalgrade = null;
if (!is_null($oldfinalgrade)) {
$grade->update('aggregation');
}
return;
}
// do the maths
$agg_grade = $this->aggregate_values($grade_values, $items);
// recalculate the grade back to requested range
$finalgrade = grade_grade::standardise_score($agg_grade, 0, 1, $this->grade_item->grademin, $this->grade_item->grademax);
$grade->finalgrade = $this->grade_item->bounded_grade($finalgrade);
// update in db if changed
if (grade_floats_different($grade->finalgrade, $oldfinalgrade)) {
$grade->update('aggregation');
}
return;
}
/**
* Internal function - aggregation maths.
* Must be public: used by grade_grade::get_hiding_affected()
*
* @param array $grade_values The values being aggregated
* @param array $items The array of grade_items
*
* @return float
*/
public function aggregate_values($grade_values, $items) {
switch ($this->aggregation) {
case GRADE_AGGREGATE_MEDIAN: // Middle point value in the set: ignores frequencies
$num = count($grade_values);
$grades = array_values($grade_values);
if ($num % 2 == 0) {
$agg_grade = ($grades[intval($num/2)-1] + $grades[intval($num/2)]) / 2;
} else {
$agg_grade = $grades[intval(($num/2)-0.5)];
}
break;
case GRADE_AGGREGATE_MIN:
$agg_grade = reset($grade_values);
break;
case GRADE_AGGREGATE_MAX:
$agg_grade = array_pop($grade_values);
break;
case GRADE_AGGREGATE_MODE: // the most common value, average used if multimode
// array_count_values only counts INT and STRING, so if grades are floats we must convert them to string
$converted_grade_values = array();
foreach ($grade_values as $k => $gv) {
if (!is_int($gv) && !is_string($gv)) {
$converted_grade_values[$k] = (string) $gv;
} else {
$converted_grade_values[$k] = $gv;
}
}
$freq = array_count_values($converted_grade_values);
arsort($freq); // sort by frequency keeping keys
$top = reset($freq); // highest frequency count
$modes = array_keys($freq, $top); // search for all modes (have the same highest count)
rsort($modes, SORT_NUMERIC); // get highest mode
$agg_grade = reset($modes);
break;
case GRADE_AGGREGATE_WEIGHTED_MEAN: // Weighted average of all existing final grades, weight specified in coef
$weightsum = 0;
$sum = 0;
foreach ($grade_values as $itemid=>$grade_value) {
if ($items[$itemid]->aggregationcoef <= 0) {
continue;
}
$weightsum += $items[$itemid]->aggregationcoef;
$sum += $items[$itemid]->aggregationcoef * $grade_value;
}
if ($weightsum == 0) {
$agg_grade = null;
} else {
$agg_grade = $sum / $weightsum;
}
break;
case GRADE_AGGREGATE_WEIGHTED_MEAN2:
// Weighted average of all existing final grades with optional extra credit flag,
// weight is the range of grade (usually grademax)
$weightsum = 0;
$sum = null;
foreach ($grade_values as $itemid=>$grade_value) {
$weight = $items[$itemid]->grademax - $items[$itemid]->grademin;
if ($weight <= 0) {
continue;
}
if ($items[$itemid]->aggregationcoef == 0) {
$weightsum += $weight;
}
$sum += $weight * $grade_value;
}
if ($weightsum == 0) {
$agg_grade = $sum; // only extra credits
} else {
$agg_grade = $sum / $weightsum;
}
break;
case GRADE_AGGREGATE_EXTRACREDIT_MEAN: // special average
$num = 0;
$sum = null;
foreach ($grade_values as $itemid=>$grade_value) {
if ($items[$itemid]->aggregationcoef == 0) {
$num += 1;
$sum += $grade_value;
} else if ($items[$itemid]->aggregationcoef > 0) {
$sum += $items[$itemid]->aggregationcoef * $grade_value;
}
}
if ($num == 0) {
$agg_grade = $sum; // only extra credits or wrong coefs
} else {
$agg_grade = $sum / $num;
}
break;
case GRADE_AGGREGATE_MEAN: // Arithmetic average of all grade items (if ungraded aggregated, NULL counted as minimum)
default:
$num = count($grade_values);
$sum = array_sum($grade_values);
$agg_grade = $sum / $num;
break;
}
return $agg_grade;
}
/**
* Some aggregation types may update max grade
* @param array $items sub items
* @return void
*/
private function auto_update_max($items) {
if ($this->aggregation != GRADE_AGGREGATE_SUM) {
// not needed at all
return;
}
if (!$items) {
if ($this->grade_item->grademax != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE) {
$this->grade_item->grademax = 0;
$this->grade_item->grademin = 0;
$this->grade_item->gradetype = GRADE_TYPE_VALUE;
$this->grade_item->update('aggregation');
}
return;
}
//find max grade possible
$maxes = array();
foreach ($items as $item) {
if ($item->aggregationcoef > 0) {
// extra credit from this activity - does not affect total
continue;
}
if ($item->gradetype == GRADE_TYPE_VALUE) {
$maxes[$item->id] = $item->grademax;
} else if ($item->gradetype == GRADE_TYPE_SCALE) {
$maxes[$item->id] = $item->grademax; // 0 = nograde, 1 = first scale item, 2 = second scale item
}
}
// apply droplow and keephigh
$this->apply_limit_rules($maxes, $items);
$max = array_sum($maxes);
// update db if anything changed
if ($this->grade_item->grademax != $max or $this->grade_item->grademin != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE) {
$this->grade_item->grademax = $max;
$this->grade_item->grademin = 0;
$this->grade_item->gradetype = GRADE_TYPE_VALUE;
$this->grade_item->update('aggregation');
}
}
/**
* internal function for category grades summing
*
* @param grade_grade &$grade The grade item
* @param float $oldfinalgrade Old Final grade?
* @param array $items Grade items
* @param array $grade_values Grade values
* @param array $excluded Excluded
*
* @return boolean (just plain return;)
*/
private function sum_grades(&$grade, $oldfinalgrade, $items, $grade_values, $excluded) {
if (empty($items)) {
return null;
}
// ungraded and excluded items are not used in aggregation
foreach ($grade_values as $itemid=>$v) {
if (is_null($v)) {
unset($grade_values[$itemid]);
} else if (in_array($itemid, $excluded)) {
unset($grade_values[$itemid]);
}
}
// use 0 if grade missing, droplow used and aggregating all items
if (!$this->aggregateonlygraded and !empty($this->droplow)) {
foreach ($items as $itemid=>$value) {
if (!isset($grade_values[$itemid]) and !in_array($itemid, $excluded)) {
$grade_values[$itemid] = 0;
}
}
}
$this->apply_limit_rules($grade_values, $items);
$sum = array_sum($grade_values);
$grade->finalgrade = $this->grade_item->bounded_grade($sum);
// update in db if changed
if (grade_floats_different($grade->finalgrade, $oldfinalgrade)) {
$grade->update('aggregation');
}
return;
}
/**
* Given an array of grade values (numerical indices), applies droplow or keephigh
* rules to limit the final array.
*
* @param array &$grade_values itemid=>$grade_value float
* @param array $items grade item objects
*
* @return array Limited grades.
*/
public function apply_limit_rules(&$grade_values, $items) {
$extraused = $this->is_extracredit_used();
if (!empty($this->droplow)) {
asort($grade_values, SORT_NUMERIC);
$dropped = 0;
foreach ($grade_values as $itemid=>$value) {
if ($dropped < $this->droplow) {
if ($extraused and $items[$itemid]->aggregationcoef > 0) {
// no drop low for extra credits
} else {
unset($grade_values[$itemid]);
$dropped++;
}
} else {
// we have dropped enough
break;
}
}
} else if (!empty($this->keephigh)) {
arsort($grade_values, SORT_NUMERIC);
$kept = 0;
foreach ($grade_values as $itemid=>$value) {
if ($extraused and $items[$itemid]->aggregationcoef > 0) {
// we keep all extra credits
} else if ($kept < $this->keephigh) {
$kept++;
} else {
unset($grade_values[$itemid]);
}
}
}
}
/**
* Returns true if category uses extra credit of any kind
*
* @return boolean true if extra credit used
*/
function is_extracredit_used() {
return ($this->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2
or $this->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN
or $this->aggregation == GRADE_AGGREGATE_SUM);
}
/**
* Returns true if category uses special aggregation coefficient
*
* @return boolean true if coefficient used
*/
public function is_aggregationcoef_used() {
return ($this->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN
or $this->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2
or $this->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN
or $this->aggregation == GRADE_AGGREGATE_SUM);
}
/**
* Recursive function to find which weight/extra credit field to use in the grade item form. Inherits from a parent category
* if that category has aggregatesubcats set to true.
*
* @param string $first Whether or not this is the first item in the recursion
*
* @return string
*/
public function get_coefstring($first=true) {
if (!is_null($this->coefstring)) {
return $this->coefstring;
}
$overriding_coefstring = null;
// Stop recursing upwards if this category aggregates subcats or has no parent
if (!$first && !$this->aggregatesubcats) {
if ($parent_category = $this->load_parent_category()) {
return $parent_category->get_coefstring(false);
} else {
return null;
}
} else if ($first) {
if (!$this->aggregatesubcats) {
if ($parent_category = $this->load_parent_category()) {
$overriding_coefstring = $parent_category->get_coefstring(false);
}
}
}
// If an overriding coefstring has trickled down from one of the parent categories, return it. Otherwise, return self.
if (!is_null($overriding_coefstring)) {
return $overriding_coefstring;
}
// No parent category is overriding this category's aggregation, return its string
if ($this->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
$this->coefstring = 'aggregationcoefweight';
} else if ($this->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
$this->coefstring = 'aggregationcoefextrasum';
} else if ($this->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
$this->coefstring = 'aggregationcoefextraweight';
} else if ($this->aggregation == GRADE_AGGREGATE_SUM) {
$this->coefstring = 'aggregationcoefextrasum';
} else {