MDL-71669 editor_atto: Fire custom event when toggling button highlight
[moodle.git] / lib / grade / grade_category.php
blobbbcf41b7db36279889e671ac08c26db18379ac4a
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.
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/>.
17 /**
18 * Definition of a class to represent a grade category
20 * @package core_grades
21 * @copyright 2006 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once(__DIR__ . '/grade_object.php');
29 /**
30 * grade_category is an object mapped to DB table {prefix}grade_categories
32 * @package core_grades
33 * @category grade
34 * @copyright 2007 Nicolas Connault
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class grade_category extends grade_object {
38 /**
39 * The DB table.
40 * @var string $table
42 public $table = 'grade_categories';
44 /**
45 * Array of required table fields, must start with 'id'.
46 * @var array $required_fields
48 public $required_fields = array('id', 'courseid', 'parent', 'depth', 'path', 'fullname', 'aggregation',
49 'keephigh', 'droplow', 'aggregateonlygraded', 'aggregateoutcomes',
50 'timecreated', 'timemodified', 'hidden');
52 /**
53 * The course this category belongs to.
54 * @var int $courseid
56 public $courseid;
58 /**
59 * The category this category belongs to (optional).
60 * @var int $parent
62 public $parent;
64 /**
65 * The grade_category object referenced by $this->parent (PK).
66 * @var grade_category $parent_category
68 public $parent_category;
70 /**
71 * The number of parents this category has.
72 * @var int $depth
74 public $depth = 0;
76 /**
77 * Shows the hierarchical path for this category as /1/2/3/ (like course_categories), the last number being
78 * this category's autoincrement ID number.
79 * @var string $path
81 public $path;
83 /**
84 * The name of this category.
85 * @var string $fullname
87 public $fullname;
89 /**
90 * A constant pointing to one of the predefined aggregation strategies (none, mean, median, sum etc) .
91 * @var int $aggregation
93 public $aggregation = GRADE_AGGREGATE_SUM;
95 /**
96 * Keep only the X highest items.
97 * @var int $keephigh
99 public $keephigh = 0;
102 * Drop the X lowest items.
103 * @var int $droplow
105 public $droplow = 0;
108 * Aggregate only graded items
109 * @var int $aggregateonlygraded
111 public $aggregateonlygraded = 0;
114 * Aggregate outcomes together with normal items
115 * @var int $aggregateoutcomes
117 public $aggregateoutcomes = 0;
120 * Array of grade_items or grade_categories nested exactly 1 level below this category
121 * @var array $children
123 public $children;
126 * A hierarchical array of all children below this category. This is stored separately from
127 * $children because it is more memory-intensive and may not be used as often.
128 * @var array $all_children
130 public $all_children;
133 * An associated grade_item object, with itemtype=category, used to calculate and cache a set of grade values
134 * for this category.
135 * @var grade_item $grade_item
137 public $grade_item;
140 * Temporary sortorder for speedup of children resorting
141 * @var int $sortorder
143 public $sortorder;
146 * List of options which can be "forced" from site settings.
147 * @var array $forceable
149 public $forceable = array('aggregation', 'keephigh', 'droplow', 'aggregateonlygraded', 'aggregateoutcomes');
152 * String representing the aggregation coefficient. Variable is used as cache.
153 * @var string $coefstring
155 public $coefstring = null;
158 * Static variable storing the result from {@link self::can_apply_limit_rules}.
159 * @var bool
161 protected $canapplylimitrules;
164 * Builds this category's path string based on its parents (if any) and its own id number.
165 * This is typically done just before inserting this object in the DB for the first time,
166 * or when a new parent is added or changed. It is a recursive function: once the calling
167 * object no longer has a parent, the path is complete.
169 * @param grade_category $grade_category A Grade_Category object
170 * @return string The category's path string
172 public static function build_path($grade_category) {
173 global $DB;
175 if (empty($grade_category->parent)) {
176 return '/'.$grade_category->id.'/';
178 } else {
179 $parent = $DB->get_record('grade_categories', array('id' => $grade_category->parent));
180 return grade_category::build_path($parent).$grade_category->id.'/';
185 * Finds and returns a grade_category instance based on params.
187 * @param array $params associative arrays varname=>value
188 * @return grade_category The retrieved grade_category instance or false if none found.
190 public static function fetch($params) {
191 if ($records = self::retrieve_record_set($params)) {
192 return reset($records);
195 $record = grade_object::fetch_helper('grade_categories', 'grade_category', $params);
197 // We store it as an array to keep a key => result set interface in the cache, grade_object::fetch_helper is
198 // managing exceptions. We return only the first element though.
199 $records = false;
200 if ($record) {
201 $records = array($record->id => $record);
204 self::set_record_set($params, $records);
206 return $record;
210 * Finds and returns all grade_category instances based on params.
212 * @param array $params associative arrays varname=>value
213 * @return array array of grade_category insatnces or false if none found.
215 public static function fetch_all($params) {
216 if ($records = self::retrieve_record_set($params)) {
217 return $records;
220 $records = grade_object::fetch_all_helper('grade_categories', 'grade_category', $params);
221 self::set_record_set($params, $records);
223 return $records;
227 * In addition to update() as defined in grade_object, call force_regrading of parent categories, if applicable.
229 * @param string $source from where was the object updated (mod/forum, manual, etc.)
230 * @return bool success
232 public function update($source=null) {
233 // load the grade item or create a new one
234 $this->load_grade_item();
236 // force recalculation of path;
237 if (empty($this->path)) {
238 $this->path = grade_category::build_path($this);
239 $this->depth = substr_count($this->path, '/') - 1;
240 $updatechildren = true;
242 } else {
243 $updatechildren = false;
246 $this->apply_forced_settings();
248 // these are exclusive
249 if ($this->droplow > 0) {
250 $this->keephigh = 0;
252 } else if ($this->keephigh > 0) {
253 $this->droplow = 0;
256 // Recalculate grades if needed
257 if ($this->qualifies_for_regrading()) {
258 $this->force_regrading();
261 $this->timemodified = time();
263 $result = parent::update($source);
265 // now update paths in all child categories
266 if ($result and $updatechildren) {
268 if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
270 foreach ($children as $child) {
271 $child->path = null;
272 $child->depth = 0;
273 $child->update($source);
278 return $result;
282 * If parent::delete() is successful, send force_regrading message to parent category.
284 * @param string $source from where was the object deleted (mod/forum, manual, etc.)
285 * @return bool success
287 public function delete($source=null) {
288 global $DB;
290 $transaction = $DB->start_delegated_transaction();
291 $grade_item = $this->load_grade_item();
293 if ($this->is_course_category()) {
295 if ($categories = grade_category::fetch_all(array('courseid'=>$this->courseid))) {
297 foreach ($categories as $category) {
299 if ($category->id == $this->id) {
300 continue; // do not delete course category yet
302 $category->delete($source);
306 if ($items = grade_item::fetch_all(array('courseid'=>$this->courseid))) {
308 foreach ($items as $item) {
310 if ($item->id == $grade_item->id) {
311 continue; // do not delete course item yet
313 $item->delete($source);
317 } else {
318 $this->force_regrading();
320 $parent = $this->load_parent_category();
322 // Update children's categoryid/parent field first
323 if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
324 foreach ($children as $child) {
325 $child->set_parent($parent->id);
329 if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
330 foreach ($children as $child) {
331 $child->set_parent($parent->id);
336 // first delete the attached grade item and grades
337 $grade_item->delete($source);
339 // delete category itself
340 $success = parent::delete($source);
342 $transaction->allow_commit();
343 return $success;
347 * In addition to the normal insert() defined in grade_object, this method sets the depth
348 * and path for this object, and update the record accordingly.
350 * We do this here instead of in the constructor as they both need to know the record's
351 * ID number, which only gets created at insertion time.
352 * This method also creates an associated grade_item if this wasn't done during construction.
354 * @param string $source from where was the object inserted (mod/forum, manual, etc.)
355 * @return int PK ID if successful, false otherwise
357 public function insert($source=null) {
359 if (empty($this->courseid)) {
360 print_error('cannotinsertgrade');
363 if (empty($this->parent)) {
364 $course_category = grade_category::fetch_course_category($this->courseid);
365 $this->parent = $course_category->id;
368 $this->path = null;
370 $this->timecreated = $this->timemodified = time();
372 if (!parent::insert($source)) {
373 debugging("Could not insert this category: " . print_r($this, true));
374 return false;
377 $this->force_regrading();
379 // build path and depth
380 $this->update($source);
382 return $this->id;
386 * Internal function - used only from fetch_course_category()
387 * Normal insert() can not be used for course category
389 * @param int $courseid The course ID
390 * @return int The ID of the new course category
392 public function insert_course_category($courseid) {
393 $this->courseid = $courseid;
394 $this->fullname = '?';
395 $this->path = null;
396 $this->parent = null;
397 $this->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
399 $this->apply_default_settings();
400 $this->apply_forced_settings();
402 $this->timecreated = $this->timemodified = time();
404 if (!parent::insert('system')) {
405 debugging("Could not insert this category: " . print_r($this, true));
406 return false;
409 // build path and depth
410 $this->update('system');
412 return $this->id;
416 * Compares the values held by this object with those of the matching record in DB, and returns
417 * whether or not these differences are sufficient to justify an update of all parent objects.
418 * This assumes that this object has an ID number and a matching record in DB. If not, it will return false.
420 * @return bool
422 public function qualifies_for_regrading() {
423 if (empty($this->id)) {
424 debugging("Can not regrade non existing category");
425 return false;
428 $db_item = grade_category::fetch(array('id'=>$this->id));
430 $aggregationdiff = $db_item->aggregation != $this->aggregation;
431 $keephighdiff = $db_item->keephigh != $this->keephigh;
432 $droplowdiff = $db_item->droplow != $this->droplow;
433 $aggonlygrddiff = $db_item->aggregateonlygraded != $this->aggregateonlygraded;
434 $aggoutcomesdiff = $db_item->aggregateoutcomes != $this->aggregateoutcomes;
436 return ($aggregationdiff || $keephighdiff || $droplowdiff || $aggonlygrddiff || $aggoutcomesdiff);
440 * Marks this grade categories' associated grade item as needing regrading
442 public function force_regrading() {
443 $grade_item = $this->load_grade_item();
444 $grade_item->force_regrading();
448 * Something that should be called before we start regrading the whole course.
450 * @return void
452 public function pre_regrade_final_grades() {
453 $this->auto_update_weights();
454 $this->auto_update_max();
458 * Generates and saves final grades in associated category grade item.
459 * These immediate children must already have their own final grades.
460 * The category's aggregation method is used to generate final grades.
462 * Please note that category grade is either calculated or aggregated, not both at the same time.
464 * This method must be used ONLY from grade_item::regrade_final_grades(),
465 * because the calculation must be done in correct order!
467 * Steps to follow:
468 * 1. Get final grades from immediate children
469 * 3. Aggregate these grades
470 * 4. Save them in final grades of associated category grade item
472 * @param int $userid The user ID if final grade generation should be limited to a single user
473 * @return bool
475 public function generate_grades($userid=null) {
476 global $CFG, $DB;
478 $this->load_grade_item();
480 if ($this->grade_item->is_locked()) {
481 return true; // no need to recalculate locked items
484 // find grade items of immediate children (category or grade items) and force site settings
485 $depends_on = $this->grade_item->depends_on();
487 if (empty($depends_on)) {
488 $items = false;
490 } else {
491 list($usql, $params) = $DB->get_in_or_equal($depends_on);
492 $sql = "SELECT *
493 FROM {grade_items}
494 WHERE id $usql";
495 $items = $DB->get_records_sql($sql, $params);
496 foreach ($items as $id => $item) {
497 $items[$id] = new grade_item($item, false);
501 $grade_inst = new grade_grade();
502 $fields = 'g.'.implode(',g.', $grade_inst->required_fields);
504 // where to look for final grades - include grade of this item too, we will store the results there
505 $gis = array_merge($depends_on, array($this->grade_item->id));
506 list($usql, $params) = $DB->get_in_or_equal($gis);
508 if ($userid) {
509 $usersql = "AND g.userid=?";
510 $params[] = $userid;
512 } else {
513 $usersql = "";
516 $sql = "SELECT $fields
517 FROM {grade_grades} g, {grade_items} gi
518 WHERE gi.id = g.itemid AND gi.id $usql $usersql
519 ORDER BY g.userid";
521 // group the results by userid and aggregate the grades for this user
522 $rs = $DB->get_recordset_sql($sql, $params);
523 if ($rs->valid()) {
524 $prevuser = 0;
525 $grade_values = array();
526 $excluded = array();
527 $oldgrade = null;
528 $grademaxoverrides = array();
529 $grademinoverrides = array();
531 foreach ($rs as $used) {
532 $grade = new grade_grade($used, false);
533 if (isset($items[$grade->itemid])) {
534 // Prevent grade item to be fetched from DB.
535 $grade->grade_item =& $items[$grade->itemid];
536 } else if ($grade->itemid == $this->grade_item->id) {
537 // This grade's grade item is not in $items.
538 $grade->grade_item =& $this->grade_item;
540 if ($grade->userid != $prevuser) {
541 $this->aggregate_grades($prevuser,
542 $items,
543 $grade_values,
544 $oldgrade,
545 $excluded,
546 $grademinoverrides,
547 $grademaxoverrides);
548 $prevuser = $grade->userid;
549 $grade_values = array();
550 $excluded = array();
551 $oldgrade = null;
552 $grademaxoverrides = array();
553 $grademinoverrides = array();
555 $grade_values[$grade->itemid] = $grade->finalgrade;
556 $grademaxoverrides[$grade->itemid] = $grade->get_grade_max();
557 $grademinoverrides[$grade->itemid] = $grade->get_grade_min();
559 if ($grade->excluded) {
560 $excluded[] = $grade->itemid;
563 if ($this->grade_item->id == $grade->itemid) {
564 $oldgrade = $grade;
567 $this->aggregate_grades($prevuser,
568 $items,
569 $grade_values,
570 $oldgrade,
571 $excluded,
572 $grademinoverrides,
573 $grademaxoverrides);//the last one
575 $rs->close();
577 return true;
581 * Internal function for grade category grade aggregation
583 * @param int $userid The User ID
584 * @param array $items Grade items
585 * @param array $grade_values Array of grade values
586 * @param object $oldgrade Old grade
587 * @param array $excluded Excluded
588 * @param array $grademinoverrides User specific grademin values if different to the grade_item grademin (key is itemid)
589 * @param array $grademaxoverrides User specific grademax values if different to the grade_item grademax (key is itemid)
591 private function aggregate_grades($userid,
592 $items,
593 $grade_values,
594 $oldgrade,
595 $excluded,
596 $grademinoverrides,
597 $grademaxoverrides) {
598 global $CFG, $DB;
600 // Remember these so we can set flags on them to describe how they were used in the aggregation.
601 $novalue = array();
602 $dropped = array();
603 $extracredit = array();
604 $usedweights = array();
606 if (empty($userid)) {
607 //ignore first call
608 return;
611 if ($oldgrade) {
612 $oldfinalgrade = $oldgrade->finalgrade;
613 $grade = new grade_grade($oldgrade, false);
614 $grade->grade_item =& $this->grade_item;
616 } else {
617 // insert final grade - it will be needed later anyway
618 $grade = new grade_grade(array('itemid'=>$this->grade_item->id, 'userid'=>$userid), false);
619 $grade->grade_item =& $this->grade_item;
620 $grade->insert('system');
621 $oldfinalgrade = null;
624 // no need to recalculate locked or overridden grades
625 if ($grade->is_locked() or $grade->is_overridden()) {
626 return;
629 // can not use own final category grade in calculation
630 unset($grade_values[$this->grade_item->id]);
632 // Make sure a grade_grade exists for every grade_item.
633 // We need to do this so we can set the aggregationstatus
634 // with a set_field call instead of checking if each one exists and creating/updating.
635 if (!empty($items)) {
636 list($ggsql, $params) = $DB->get_in_or_equal(array_keys($items), SQL_PARAMS_NAMED, 'g');
639 $params['userid'] = $userid;
640 $sql = "SELECT itemid
641 FROM {grade_grades}
642 WHERE itemid $ggsql AND userid = :userid";
643 $existingitems = $DB->get_records_sql($sql, $params);
645 $notexisting = array_diff(array_keys($items), array_keys($existingitems));
646 foreach ($notexisting as $itemid) {
647 $gradeitem = $items[$itemid];
648 $gradegrade = new grade_grade(array('itemid' => $itemid,
649 'userid' => $userid,
650 'rawgrademin' => $gradeitem->grademin,
651 'rawgrademax' => $gradeitem->grademax), false);
652 $gradegrade->grade_item = $gradeitem;
653 $gradegrade->insert('system');
657 // if no grades calculation possible or grading not allowed clear final grade
658 if (empty($grade_values) or empty($items) or ($this->grade_item->gradetype != GRADE_TYPE_VALUE and $this->grade_item->gradetype != GRADE_TYPE_SCALE)) {
659 $grade->finalgrade = null;
661 if (!is_null($oldfinalgrade)) {
662 $grade->timemodified = time();
663 $success = $grade->update('aggregation');
665 // If successful trigger a user_graded event.
666 if ($success) {
667 \core\event\user_graded::create_from_grade($grade, \core\event\base::USER_OTHER)->trigger();
670 $dropped = $grade_values;
671 $this->set_usedinaggregation($userid, $usedweights, $novalue, $dropped, $extracredit);
672 return;
675 // Normalize the grades first - all will have value 0...1
676 // ungraded items are not used in aggregation.
677 foreach ($grade_values as $itemid=>$v) {
678 if (is_null($v)) {
679 // If null, it means no grade.
680 if ($this->aggregateonlygraded) {
681 unset($grade_values[$itemid]);
682 // Mark this item as "excluded empty" because it has no grade.
683 $novalue[$itemid] = 0;
684 continue;
687 if (in_array($itemid, $excluded)) {
688 unset($grade_values[$itemid]);
689 $dropped[$itemid] = 0;
690 continue;
692 // Check for user specific grade min/max overrides.
693 $usergrademin = $items[$itemid]->grademin;
694 $usergrademax = $items[$itemid]->grademax;
695 if (isset($grademinoverrides[$itemid])) {
696 $usergrademin = $grademinoverrides[$itemid];
698 if (isset($grademaxoverrides[$itemid])) {
699 $usergrademax = $grademaxoverrides[$itemid];
701 if ($this->aggregation == GRADE_AGGREGATE_SUM) {
702 // Assume that the grademin is 0 when standardising the score, to preserve negative grades.
703 $grade_values[$itemid] = grade_grade::standardise_score($v, 0, $usergrademax, 0, 1);
704 } else {
705 $grade_values[$itemid] = grade_grade::standardise_score($v, $usergrademin, $usergrademax, 0, 1);
710 // For items with no value, and not excluded - either set their grade to 0 or exclude them.
711 foreach ($items as $itemid=>$value) {
712 if (!isset($grade_values[$itemid]) and !in_array($itemid, $excluded)) {
713 if (!$this->aggregateonlygraded) {
714 $grade_values[$itemid] = 0;
715 } else {
716 // We are specifically marking these items as "excluded empty".
717 $novalue[$itemid] = 0;
722 // limit and sort
723 $allvalues = $grade_values;
724 if ($this->can_apply_limit_rules()) {
725 $this->apply_limit_rules($grade_values, $items);
728 $moredropped = array_diff($allvalues, $grade_values);
729 foreach ($moredropped as $drop => $unused) {
730 $dropped[$drop] = 0;
733 foreach ($grade_values as $itemid => $val) {
734 if (self::is_extracredit_used() && ($items[$itemid]->aggregationcoef > 0)) {
735 $extracredit[$itemid] = 0;
739 asort($grade_values, SORT_NUMERIC);
741 // let's see we have still enough grades to do any statistics
742 if (count($grade_values) == 0) {
743 // not enough attempts yet
744 $grade->finalgrade = null;
746 if (!is_null($oldfinalgrade)) {
747 $grade->timemodified = time();
748 $success = $grade->update('aggregation');
750 // If successful trigger a user_graded event.
751 if ($success) {
752 \core\event\user_graded::create_from_grade($grade, \core\event\base::USER_OTHER)->trigger();
755 $this->set_usedinaggregation($userid, $usedweights, $novalue, $dropped, $extracredit);
756 return;
759 // do the maths
760 $result = $this->aggregate_values_and_adjust_bounds($grade_values,
761 $items,
762 $usedweights,
763 $grademinoverrides,
764 $grademaxoverrides);
765 $agg_grade = $result['grade'];
767 // Set the actual grademin and max to bind the grade properly.
768 $this->grade_item->grademin = $result['grademin'];
769 $this->grade_item->grademax = $result['grademax'];
771 if ($this->aggregation == GRADE_AGGREGATE_SUM) {
772 // The natural aggregation always displays the range as coming from 0 for categories.
773 // However, when we bind the grade we allow for negative values.
774 $result['grademin'] = 0;
777 // Recalculate the grade back to requested range.
778 $finalgrade = grade_grade::standardise_score($agg_grade, 0, 1, $result['grademin'], $result['grademax']);
779 $grade->finalgrade = $this->grade_item->bounded_grade($finalgrade);
781 $oldrawgrademin = $grade->rawgrademin;
782 $oldrawgrademax = $grade->rawgrademax;
783 $grade->rawgrademin = $result['grademin'];
784 $grade->rawgrademax = $result['grademax'];
786 // Update in db if changed.
787 if (grade_floats_different($grade->finalgrade, $oldfinalgrade) ||
788 grade_floats_different($grade->rawgrademax, $oldrawgrademax) ||
789 grade_floats_different($grade->rawgrademin, $oldrawgrademin)) {
790 $grade->timemodified = time();
791 $success = $grade->update('aggregation');
793 // If successful trigger a user_graded event.
794 if ($success) {
795 \core\event\user_graded::create_from_grade($grade, \core\event\base::USER_OTHER)->trigger();
799 $this->set_usedinaggregation($userid, $usedweights, $novalue, $dropped, $extracredit);
801 return;
805 * Set the flags on the grade_grade items to indicate how individual grades are used
806 * in the aggregation.
808 * WARNING: This function is called a lot during gradebook recalculation, be very performance considerate.
810 * @param int $userid The user we have aggregated the grades for.
811 * @param array $usedweights An array with keys for each of the grade_item columns included in the aggregation. The value are the relative weight.
812 * @param array $novalue An array with keys for each of the grade_item columns skipped because
813 * they had no value in the aggregation.
814 * @param array $dropped An array with keys for each of the grade_item columns dropped
815 * because of any drop lowest/highest settings in the aggregation.
816 * @param array $extracredit An array with keys for each of the grade_item columns
817 * considered extra credit by the aggregation.
819 private function set_usedinaggregation($userid, $usedweights, $novalue, $dropped, $extracredit) {
820 global $DB;
822 // We want to know all current user grades so we can decide whether they need to be updated or they already contain the
823 // expected value.
824 $sql = "SELECT gi.id, gg.aggregationstatus, gg.aggregationweight FROM {grade_grades} gg
825 JOIN {grade_items} gi ON (gg.itemid = gi.id)
826 WHERE gg.userid = :userid";
827 $params = array('categoryid' => $this->id, 'userid' => $userid);
829 // These are all grade_item ids which grade_grades will NOT end up being 'unknown' (because they are not unknown or
830 // because we will update them to something different that 'unknown').
831 $giids = array_keys($usedweights + $novalue + $dropped + $extracredit);
833 if ($giids) {
834 // We include grade items that might not be in categoryid.
835 list($itemsql, $itemlist) = $DB->get_in_or_equal($giids, SQL_PARAMS_NAMED, 'gg');
836 $sql .= ' AND (gi.categoryid = :categoryid OR gi.id ' . $itemsql . ')';
837 $params = $params + $itemlist;
838 } else {
839 $sql .= ' AND gi.categoryid = :categoryid';
841 $currentgrades = $DB->get_recordset_sql($sql, $params);
843 // We will store here the grade_item ids that need to be updated on db.
844 $toupdate = array();
846 if ($currentgrades->valid()) {
848 // Iterate through the user grades to see if we really need to update any of them.
849 foreach ($currentgrades as $currentgrade) {
851 // Unset $usedweights that we do not need to update.
852 if (!empty($usedweights) && isset($usedweights[$currentgrade->id]) && $currentgrade->aggregationstatus === 'used') {
853 // We discard the ones that already have the contribution specified in $usedweights and are marked as 'used'.
854 if (grade_floats_equal($currentgrade->aggregationweight, $usedweights[$currentgrade->id])) {
855 unset($usedweights[$currentgrade->id]);
857 // Used weights can be present in multiple set_usedinaggregation arguments.
858 if (!isset($novalue[$currentgrade->id]) && !isset($dropped[$currentgrade->id]) &&
859 !isset($extracredit[$currentgrade->id])) {
860 continue;
864 // No value grades.
865 if (!empty($novalue) && isset($novalue[$currentgrade->id])) {
866 if ($currentgrade->aggregationstatus !== 'novalue' ||
867 grade_floats_different($currentgrade->aggregationweight, 0)) {
868 $toupdate['novalue'][] = $currentgrade->id;
870 continue;
873 // Dropped grades.
874 if (!empty($dropped) && isset($dropped[$currentgrade->id])) {
875 if ($currentgrade->aggregationstatus !== 'dropped' ||
876 grade_floats_different($currentgrade->aggregationweight, 0)) {
877 $toupdate['dropped'][] = $currentgrade->id;
879 continue;
882 // Extra credit grades.
883 if (!empty($extracredit) && isset($extracredit[$currentgrade->id])) {
885 // If this grade item is already marked as 'extra' and it already has the provided $usedweights value would be
886 // silly to update to 'used' to later update to 'extra'.
887 if (!empty($usedweights) && isset($usedweights[$currentgrade->id]) &&
888 grade_floats_equal($currentgrade->aggregationweight, $usedweights[$currentgrade->id])) {
889 unset($usedweights[$currentgrade->id]);
892 // Update the item to extra if it is not already marked as extra in the database or if the item's
893 // aggregationweight will be updated when going through $usedweights items.
894 if ($currentgrade->aggregationstatus !== 'extra' ||
895 (!empty($usedweights) && isset($usedweights[$currentgrade->id]))) {
896 $toupdate['extracredit'][] = $currentgrade->id;
898 continue;
901 // If is not in any of the above groups it should be set to 'unknown', checking that the item is not already
902 // unknown, if it is we don't need to update it.
903 if ($currentgrade->aggregationstatus !== 'unknown' || grade_floats_different($currentgrade->aggregationweight, 0)) {
904 $toupdate['unknown'][] = $currentgrade->id;
907 $currentgrades->close();
910 // Update items to 'unknown' status.
911 if (!empty($toupdate['unknown'])) {
912 list($itemsql, $itemlist) = $DB->get_in_or_equal($toupdate['unknown'], SQL_PARAMS_NAMED, 'g');
914 $itemlist['userid'] = $userid;
916 $sql = "UPDATE {grade_grades}
917 SET aggregationstatus = 'unknown',
918 aggregationweight = 0
919 WHERE itemid $itemsql AND userid = :userid";
920 $DB->execute($sql, $itemlist);
923 // Update items to 'used' status and setting the proper weight.
924 if (!empty($usedweights)) {
925 // The usedweights items are updated individually to record the weights.
926 foreach ($usedweights as $gradeitemid => $contribution) {
927 $sql = "UPDATE {grade_grades}
928 SET aggregationstatus = 'used',
929 aggregationweight = :contribution
930 WHERE itemid = :itemid AND userid = :userid";
932 $params = array('contribution' => $contribution, 'itemid' => $gradeitemid, 'userid' => $userid);
933 $DB->execute($sql, $params);
937 // Update items to 'novalue' status.
938 if (!empty($toupdate['novalue'])) {
939 list($itemsql, $itemlist) = $DB->get_in_or_equal($toupdate['novalue'], SQL_PARAMS_NAMED, 'g');
941 $itemlist['userid'] = $userid;
943 $sql = "UPDATE {grade_grades}
944 SET aggregationstatus = 'novalue',
945 aggregationweight = 0
946 WHERE itemid $itemsql AND userid = :userid";
948 $DB->execute($sql, $itemlist);
951 // Update items to 'dropped' status.
952 if (!empty($toupdate['dropped'])) {
953 list($itemsql, $itemlist) = $DB->get_in_or_equal($toupdate['dropped'], SQL_PARAMS_NAMED, 'g');
955 $itemlist['userid'] = $userid;
957 $sql = "UPDATE {grade_grades}
958 SET aggregationstatus = 'dropped',
959 aggregationweight = 0
960 WHERE itemid $itemsql AND userid = :userid";
962 $DB->execute($sql, $itemlist);
965 // Update items to 'extracredit' status.
966 if (!empty($toupdate['extracredit'])) {
967 list($itemsql, $itemlist) = $DB->get_in_or_equal($toupdate['extracredit'], SQL_PARAMS_NAMED, 'g');
969 $itemlist['userid'] = $userid;
971 $DB->set_field_select('grade_grades',
972 'aggregationstatus',
973 'extra',
974 "itemid $itemsql AND userid = :userid",
975 $itemlist);
980 * Internal function that calculates the aggregated grade and new min/max for this grade category
982 * Must be public as it is used by grade_grade::get_hiding_affected()
984 * @param array $grade_values An array of values to be aggregated
985 * @param array $items The array of grade_items
986 * @since Moodle 2.6.5, 2.7.2
987 * @param array & $weights If provided, will be filled with the normalized weights
988 * for each grade_item as used in the aggregation.
989 * Some rules for the weights are:
990 * 1. The weights must add up to 1 (unless there are extra credit)
991 * 2. The contributed points column must add up to the course
992 * final grade and this column is calculated from these weights.
993 * @param array $grademinoverrides User specific grademin values if different to the grade_item grademin (key is itemid)
994 * @param array $grademaxoverrides User specific grademax values if different to the grade_item grademax (key is itemid)
995 * @return array containing values for:
996 * 'grade' => the new calculated grade
997 * 'grademin' => the new calculated min grade for the category
998 * 'grademax' => the new calculated max grade for the category
1000 public function aggregate_values_and_adjust_bounds($grade_values,
1001 $items,
1002 & $weights = null,
1003 $grademinoverrides = array(),
1004 $grademaxoverrides = array()) {
1005 global $CFG;
1007 $category_item = $this->load_grade_item();
1008 $grademin = $category_item->grademin;
1009 $grademax = $category_item->grademax;
1011 switch ($this->aggregation) {
1013 case GRADE_AGGREGATE_MEDIAN: // Middle point value in the set: ignores frequencies
1014 $num = count($grade_values);
1015 $grades = array_values($grade_values);
1017 // The median gets 100% - others get 0.
1018 if ($weights !== null && $num > 0) {
1019 $count = 0;
1020 foreach ($grade_values as $itemid=>$grade_value) {
1021 if (($num % 2 == 0) && ($count == intval($num/2)-1 || $count == intval($num/2))) {
1022 $weights[$itemid] = 0.5;
1023 } else if (($num % 2 != 0) && ($count == intval(($num/2)-0.5))) {
1024 $weights[$itemid] = 1.0;
1025 } else {
1026 $weights[$itemid] = 0;
1028 $count++;
1031 if ($num % 2 == 0) {
1032 $agg_grade = ($grades[intval($num/2)-1] + $grades[intval($num/2)]) / 2;
1033 } else {
1034 $agg_grade = $grades[intval(($num/2)-0.5)];
1037 break;
1039 case GRADE_AGGREGATE_MIN:
1040 $agg_grade = reset($grade_values);
1041 // Record the weights as used.
1042 if ($weights !== null) {
1043 foreach ($grade_values as $itemid=>$grade_value) {
1044 $weights[$itemid] = 0;
1047 // Set the first item to 1.
1048 $itemids = array_keys($grade_values);
1049 $weights[reset($itemids)] = 1;
1050 break;
1052 case GRADE_AGGREGATE_MAX:
1053 // Record the weights as used.
1054 if ($weights !== null) {
1055 foreach ($grade_values as $itemid=>$grade_value) {
1056 $weights[$itemid] = 0;
1059 // Set the last item to 1.
1060 $itemids = array_keys($grade_values);
1061 $weights[end($itemids)] = 1;
1062 $agg_grade = end($grade_values);
1063 break;
1065 case GRADE_AGGREGATE_MODE: // the most common value
1066 // array_count_values only counts INT and STRING, so if grades are floats we must convert them to string
1067 $converted_grade_values = array();
1069 foreach ($grade_values as $k => $gv) {
1071 if (!is_int($gv) && !is_string($gv)) {
1072 $converted_grade_values[$k] = (string) $gv;
1074 } else {
1075 $converted_grade_values[$k] = $gv;
1077 if ($weights !== null) {
1078 $weights[$k] = 0;
1082 $freq = array_count_values($converted_grade_values);
1083 arsort($freq); // sort by frequency keeping keys
1084 $top = reset($freq); // highest frequency count
1085 $modes = array_keys($freq, $top); // search for all modes (have the same highest count)
1086 rsort($modes, SORT_NUMERIC); // get highest mode
1087 $agg_grade = reset($modes);
1088 // Record the weights as used.
1089 if ($weights !== null && $top > 0) {
1090 foreach ($grade_values as $k => $gv) {
1091 if ($gv == $agg_grade) {
1092 $weights[$k] = 1.0 / $top;
1096 break;
1098 case GRADE_AGGREGATE_WEIGHTED_MEAN: // Weighted average of all existing final grades, weight specified in coef
1099 $weightsum = 0;
1100 $sum = 0;
1102 foreach ($grade_values as $itemid=>$grade_value) {
1103 if ($weights !== null) {
1104 $weights[$itemid] = $items[$itemid]->aggregationcoef;
1106 if ($items[$itemid]->aggregationcoef <= 0) {
1107 continue;
1109 $weightsum += $items[$itemid]->aggregationcoef;
1110 $sum += $items[$itemid]->aggregationcoef * $grade_value;
1112 if ($weightsum == 0) {
1113 $agg_grade = null;
1115 } else {
1116 $agg_grade = $sum / $weightsum;
1117 if ($weights !== null) {
1118 // Normalise the weights.
1119 foreach ($weights as $itemid => $weight) {
1120 $weights[$itemid] = $weight / $weightsum;
1125 break;
1127 case GRADE_AGGREGATE_WEIGHTED_MEAN2:
1128 // Weighted average of all existing final grades with optional extra credit flag,
1129 // weight is the range of grade (usually grademax)
1130 $this->load_grade_item();
1131 $weightsum = 0;
1132 $sum = null;
1134 foreach ($grade_values as $itemid=>$grade_value) {
1135 if ($items[$itemid]->aggregationcoef > 0) {
1136 continue;
1139 $weight = $items[$itemid]->grademax - $items[$itemid]->grademin;
1140 if ($weight <= 0) {
1141 continue;
1144 $weightsum += $weight;
1145 $sum += $weight * $grade_value;
1148 // Handle the extra credit items separately to calculate their weight accurately.
1149 foreach ($grade_values as $itemid => $grade_value) {
1150 if ($items[$itemid]->aggregationcoef <= 0) {
1151 continue;
1154 $weight = $items[$itemid]->grademax - $items[$itemid]->grademin;
1155 if ($weight <= 0) {
1156 $weights[$itemid] = 0;
1157 continue;
1160 $oldsum = $sum;
1161 $weightedgrade = $weight * $grade_value;
1162 $sum += $weightedgrade;
1164 if ($weights !== null) {
1165 if ($weightsum <= 0) {
1166 $weights[$itemid] = 0;
1167 continue;
1170 $oldgrade = $oldsum / $weightsum;
1171 $grade = $sum / $weightsum;
1172 $normoldgrade = grade_grade::standardise_score($oldgrade, 0, 1, $grademin, $grademax);
1173 $normgrade = grade_grade::standardise_score($grade, 0, 1, $grademin, $grademax);
1174 $boundedoldgrade = $this->grade_item->bounded_grade($normoldgrade);
1175 $boundedgrade = $this->grade_item->bounded_grade($normgrade);
1177 if ($boundedgrade - $boundedoldgrade <= 0) {
1178 // Nothing new was added to the grade.
1179 $weights[$itemid] = 0;
1180 } else if ($boundedgrade < $normgrade) {
1181 // The grade has been bounded, the extra credit item needs to have a different weight.
1182 $gradediff = $boundedgrade - $normoldgrade;
1183 $gradediffnorm = grade_grade::standardise_score($gradediff, $grademin, $grademax, 0, 1);
1184 $weights[$itemid] = $gradediffnorm / $grade_value;
1185 } else {
1186 // Default weighting.
1187 $weights[$itemid] = $weight / $weightsum;
1192 if ($weightsum == 0) {
1193 $agg_grade = $sum; // only extra credits
1195 } else {
1196 $agg_grade = $sum / $weightsum;
1199 // Record the weights as used.
1200 if ($weights !== null) {
1201 foreach ($grade_values as $itemid=>$grade_value) {
1202 if ($items[$itemid]->aggregationcoef > 0) {
1203 // Ignore extra credit items, the weights have already been computed.
1204 continue;
1206 if ($weightsum > 0) {
1207 $weight = $items[$itemid]->grademax - $items[$itemid]->grademin;
1208 $weights[$itemid] = $weight / $weightsum;
1209 } else {
1210 $weights[$itemid] = 0;
1214 break;
1216 case GRADE_AGGREGATE_EXTRACREDIT_MEAN: // special average
1217 $this->load_grade_item();
1218 $num = 0;
1219 $sum = null;
1221 foreach ($grade_values as $itemid=>$grade_value) {
1222 if ($items[$itemid]->aggregationcoef == 0) {
1223 $num += 1;
1224 $sum += $grade_value;
1225 if ($weights !== null) {
1226 $weights[$itemid] = 1;
1231 // Treating the extra credit items separately to get a chance to calculate their effective weights.
1232 foreach ($grade_values as $itemid=>$grade_value) {
1233 if ($items[$itemid]->aggregationcoef > 0) {
1234 $oldsum = $sum;
1235 $sum += $items[$itemid]->aggregationcoef * $grade_value;
1237 if ($weights !== null) {
1238 if ($num <= 0) {
1239 // The category only contains extra credit items, not setting the weight.
1240 continue;
1243 $oldgrade = $oldsum / $num;
1244 $grade = $sum / $num;
1245 $normoldgrade = grade_grade::standardise_score($oldgrade, 0, 1, $grademin, $grademax);
1246 $normgrade = grade_grade::standardise_score($grade, 0, 1, $grademin, $grademax);
1247 $boundedoldgrade = $this->grade_item->bounded_grade($normoldgrade);
1248 $boundedgrade = $this->grade_item->bounded_grade($normgrade);
1250 if ($boundedgrade - $boundedoldgrade <= 0) {
1251 // Nothing new was added to the grade.
1252 $weights[$itemid] = 0;
1253 } else if ($boundedgrade < $normgrade) {
1254 // The grade has been bounded, the extra credit item needs to have a different weight.
1255 $gradediff = $boundedgrade - $normoldgrade;
1256 $gradediffnorm = grade_grade::standardise_score($gradediff, $grademin, $grademax, 0, 1);
1257 $weights[$itemid] = $gradediffnorm / $grade_value;
1258 } else {
1259 // Default weighting.
1260 $weights[$itemid] = 1.0 / $num;
1266 if ($weights !== null && $num > 0) {
1267 foreach ($grade_values as $itemid=>$grade_value) {
1268 if ($items[$itemid]->aggregationcoef > 0) {
1269 // Extra credit weights were already calculated.
1270 continue;
1272 if ($weights[$itemid]) {
1273 $weights[$itemid] = 1.0 / $num;
1278 if ($num == 0) {
1279 $agg_grade = $sum; // only extra credits or wrong coefs
1281 } else {
1282 $agg_grade = $sum / $num;
1285 break;
1287 case GRADE_AGGREGATE_SUM: // Add up all the items.
1288 $this->load_grade_item();
1289 $num = count($grade_values);
1290 $sum = 0;
1292 // This setting indicates if we should use algorithm prior to MDL-49257 fix for calculating extra credit weights.
1293 // Even though old algorith has bugs in it, we need to preserve existing grades.
1294 $gradebookcalculationfreeze = 'gradebook_calculations_freeze_' . $this->courseid;
1295 $oldextracreditcalculation = isset($CFG->$gradebookcalculationfreeze)
1296 && ($CFG->$gradebookcalculationfreeze <= 20150619);
1298 $sumweights = 0;
1299 $grademin = 0;
1300 $grademax = 0;
1301 $extracredititems = array();
1302 foreach ($grade_values as $itemid => $gradevalue) {
1303 // We need to check if the grademax/min was adjusted per user because of excluded items.
1304 $usergrademin = $items[$itemid]->grademin;
1305 $usergrademax = $items[$itemid]->grademax;
1306 if (isset($grademinoverrides[$itemid])) {
1307 $usergrademin = $grademinoverrides[$itemid];
1309 if (isset($grademaxoverrides[$itemid])) {
1310 $usergrademax = $grademaxoverrides[$itemid];
1313 // Keep track of the extra credit items, we will need them later on.
1314 if ($items[$itemid]->aggregationcoef > 0) {
1315 $extracredititems[$itemid] = $items[$itemid];
1318 // Ignore extra credit and items with a weight of 0.
1319 if (!isset($extracredititems[$itemid]) && $items[$itemid]->aggregationcoef2 > 0) {
1320 $grademin += $usergrademin;
1321 $grademax += $usergrademax;
1322 $sumweights += $items[$itemid]->aggregationcoef2;
1325 $userweights = array();
1326 $totaloverriddenweight = 0;
1327 $totaloverriddengrademax = 0;
1328 // We first need to rescale all manually assigned weights down by the
1329 // percentage of weights missing from the category.
1330 foreach ($grade_values as $itemid => $gradevalue) {
1331 if ($items[$itemid]->weightoverride) {
1332 if ($items[$itemid]->aggregationcoef2 <= 0) {
1333 // Records the weight of 0 and continue.
1334 $userweights[$itemid] = 0;
1335 continue;
1337 $userweights[$itemid] = $sumweights ? ($items[$itemid]->aggregationcoef2 / $sumweights) : 0;
1338 if (!$oldextracreditcalculation && isset($extracredititems[$itemid])) {
1339 // Extra credit items do not affect totals.
1340 continue;
1342 $totaloverriddenweight += $userweights[$itemid];
1343 $usergrademax = $items[$itemid]->grademax;
1344 if (isset($grademaxoverrides[$itemid])) {
1345 $usergrademax = $grademaxoverrides[$itemid];
1347 $totaloverriddengrademax += $usergrademax;
1350 $nonoverriddenpoints = $grademax - $totaloverriddengrademax;
1352 // Then we need to recalculate the automatic weights except for extra credit items.
1353 foreach ($grade_values as $itemid => $gradevalue) {
1354 if (!$items[$itemid]->weightoverride && ($oldextracreditcalculation || !isset($extracredititems[$itemid]))) {
1355 $usergrademax = $items[$itemid]->grademax;
1356 if (isset($grademaxoverrides[$itemid])) {
1357 $usergrademax = $grademaxoverrides[$itemid];
1359 if ($nonoverriddenpoints > 0) {
1360 $userweights[$itemid] = ($usergrademax/$nonoverriddenpoints) * (1 - $totaloverriddenweight);
1361 } else {
1362 $userweights[$itemid] = 0;
1363 if ($items[$itemid]->aggregationcoef2 > 0) {
1364 // Items with a weight of 0 should not count for the grade max,
1365 // though this only applies if the weight was changed to 0.
1366 $grademax -= $usergrademax;
1372 // Now when we finally know the grademax we can adjust the automatic weights of extra credit items.
1373 if (!$oldextracreditcalculation) {
1374 foreach ($grade_values as $itemid => $gradevalue) {
1375 if (!$items[$itemid]->weightoverride && isset($extracredititems[$itemid])) {
1376 $usergrademax = $items[$itemid]->grademax;
1377 if (isset($grademaxoverrides[$itemid])) {
1378 $usergrademax = $grademaxoverrides[$itemid];
1380 $userweights[$itemid] = $grademax ? ($usergrademax / $grademax) : 0;
1385 // We can use our freshly corrected weights below.
1386 foreach ($grade_values as $itemid => $gradevalue) {
1387 if (isset($extracredititems[$itemid])) {
1388 // We skip the extra credit items first.
1389 continue;
1391 $sum += $gradevalue * $userweights[$itemid] * $grademax;
1392 if ($weights !== null) {
1393 $weights[$itemid] = $userweights[$itemid];
1397 // No we proceed with the extra credit items. They might have a different final
1398 // weight in case the final grade was bounded. So we need to treat them different.
1399 // Also, as we need to use the bounded_grade() method, we have to inject the
1400 // right values there, and restore them afterwards.
1401 $oldgrademax = $this->grade_item->grademax;
1402 $oldgrademin = $this->grade_item->grademin;
1403 foreach ($grade_values as $itemid => $gradevalue) {
1404 if (!isset($extracredititems[$itemid])) {
1405 continue;
1407 $oldsum = $sum;
1408 $weightedgrade = $gradevalue * $userweights[$itemid] * $grademax;
1409 $sum += $weightedgrade;
1411 // Only go through this when we need to record the weights.
1412 if ($weights !== null) {
1413 if ($grademax <= 0) {
1414 // There are only extra credit items in this category,
1415 // all the weights should be accurate (and be 0).
1416 $weights[$itemid] = $userweights[$itemid];
1417 continue;
1420 $oldfinalgrade = $this->grade_item->bounded_grade($oldsum);
1421 $newfinalgrade = $this->grade_item->bounded_grade($sum);
1422 $finalgradediff = $newfinalgrade - $oldfinalgrade;
1423 if ($finalgradediff <= 0) {
1424 // This item did not contribute to the category total at all.
1425 $weights[$itemid] = 0;
1426 } else if ($finalgradediff < $weightedgrade) {
1427 // The weight needs to be adjusted because only a portion of the
1428 // extra credit item contributed to the category total.
1429 $weights[$itemid] = $finalgradediff / ($gradevalue * $grademax);
1430 } else {
1431 // The weight was accurate.
1432 $weights[$itemid] = $userweights[$itemid];
1436 $this->grade_item->grademax = $oldgrademax;
1437 $this->grade_item->grademin = $oldgrademin;
1439 if ($grademax > 0) {
1440 $agg_grade = $sum / $grademax; // Re-normalize score.
1441 } else {
1442 // Every item in the category is extra credit.
1443 $agg_grade = $sum;
1444 $grademax = $sum;
1447 break;
1449 case GRADE_AGGREGATE_MEAN: // Arithmetic average of all grade items (if ungraded aggregated, NULL counted as minimum)
1450 default:
1451 $num = count($grade_values);
1452 $sum = array_sum($grade_values);
1453 $agg_grade = $sum / $num;
1454 // Record the weights evenly.
1455 if ($weights !== null && $num > 0) {
1456 foreach ($grade_values as $itemid=>$grade_value) {
1457 $weights[$itemid] = 1.0 / $num;
1460 break;
1463 return array('grade' => $agg_grade, 'grademin' => $grademin, 'grademax' => $grademax);
1467 * Internal function that calculates the aggregated grade for this grade category
1469 * Must be public as it is used by grade_grade::get_hiding_affected()
1471 * @deprecated since Moodle 2.8
1472 * @param array $grade_values An array of values to be aggregated
1473 * @param array $items The array of grade_items
1474 * @return float The aggregate grade for this grade category
1476 public function aggregate_values($grade_values, $items) {
1477 debugging('grade_category::aggregate_values() is deprecated.
1478 Call grade_category::aggregate_values_and_adjust_bounds() instead.', DEBUG_DEVELOPER);
1479 $result = $this->aggregate_values_and_adjust_bounds($grade_values, $items);
1480 return $result['grade'];
1484 * Some aggregation types may need to update their max grade.
1486 * This must be executed after updating the weights as it relies on them.
1488 * @return void
1490 private function auto_update_max() {
1491 global $CFG, $DB;
1492 if ($this->aggregation != GRADE_AGGREGATE_SUM) {
1493 // not needed at all
1494 return;
1497 // Find grade items of immediate children (category or grade items) and force site settings.
1498 $this->load_grade_item();
1499 $depends_on = $this->grade_item->depends_on();
1501 // Check to see if the gradebook is frozen. This allows grades to not be altered at all until a user verifies that they
1502 // wish to update the grades.
1503 $gradebookcalculationfreeze = 'gradebook_calculations_freeze_' . $this->courseid;
1504 $oldextracreditcalculation = isset($CFG->$gradebookcalculationfreeze) && ($CFG->$gradebookcalculationfreeze <= 20150627);
1505 // Only run if the gradebook isn't frozen.
1506 if (!$oldextracreditcalculation) {
1507 // Don't automatically update the max for calculated items.
1508 if ($this->grade_item->is_calculated()) {
1509 return;
1513 $items = false;
1514 if (!empty($depends_on)) {
1515 list($usql, $params) = $DB->get_in_or_equal($depends_on);
1516 $sql = "SELECT *
1517 FROM {grade_items}
1518 WHERE id $usql";
1519 $items = $DB->get_records_sql($sql, $params);
1522 if (!$items) {
1524 if ($this->grade_item->grademax != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE) {
1525 $this->grade_item->grademax = 0;
1526 $this->grade_item->grademin = 0;
1527 $this->grade_item->gradetype = GRADE_TYPE_VALUE;
1528 $this->grade_item->update('aggregation');
1530 return;
1533 //find max grade possible
1534 $maxes = array();
1536 foreach ($items as $item) {
1538 if ($item->aggregationcoef > 0) {
1539 // extra credit from this activity - does not affect total
1540 continue;
1541 } else if ($item->aggregationcoef2 <= 0) {
1542 // Items with a weight of 0 do not affect the total.
1543 continue;
1546 if ($item->gradetype == GRADE_TYPE_VALUE) {
1547 $maxes[$item->id] = $item->grademax;
1549 } else if ($item->gradetype == GRADE_TYPE_SCALE) {
1550 $maxes[$item->id] = $item->grademax; // 0 = nograde, 1 = first scale item, 2 = second scale item
1554 if ($this->can_apply_limit_rules()) {
1555 // Apply droplow and keephigh.
1556 $this->apply_limit_rules($maxes, $items);
1558 $max = array_sum($maxes);
1560 // update db if anything changed
1561 if ($this->grade_item->grademax != $max or $this->grade_item->grademin != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE) {
1562 $this->grade_item->grademax = $max;
1563 $this->grade_item->grademin = 0;
1564 $this->grade_item->gradetype = GRADE_TYPE_VALUE;
1565 $this->grade_item->update('aggregation');
1570 * Recalculate the weights of the grade items in this category.
1572 * The category total is not updated here, a further call to
1573 * {@link self::auto_update_max()} is required.
1575 * @return void
1577 private function auto_update_weights() {
1578 global $CFG;
1579 if ($this->aggregation != GRADE_AGGREGATE_SUM) {
1580 // This is only required if we are using natural weights.
1581 return;
1583 $children = $this->get_children();
1585 $gradeitem = null;
1587 // Calculate the sum of the grademax's of all the items within this category.
1588 $totalnonoverriddengrademax = 0;
1589 $totalgrademax = 0;
1591 // Out of 1, how much weight has been manually overriden by a user?
1592 $totaloverriddenweight = 0;
1593 $totaloverriddengrademax = 0;
1595 // Has every assessment in this category been overridden?
1596 $automaticgradeitemspresent = false;
1597 // Does the grade item require normalising?
1598 $requiresnormalising = false;
1600 // This array keeps track of the id and weight of every grade item that has been overridden.
1601 $overridearray = array();
1602 foreach ($children as $sortorder => $child) {
1603 $gradeitem = null;
1605 if ($child['type'] == 'item') {
1606 $gradeitem = $child['object'];
1607 } else if ($child['type'] == 'category') {
1608 $gradeitem = $child['object']->load_grade_item();
1611 if ($gradeitem->gradetype == GRADE_TYPE_NONE || $gradeitem->gradetype == GRADE_TYPE_TEXT) {
1612 // Text items and none items do not have a weight.
1613 continue;
1614 } else if (!$this->aggregateoutcomes && $gradeitem->is_outcome_item()) {
1615 // We will not aggregate outcome items, so we can ignore them.
1616 continue;
1617 } else if (empty($CFG->grade_includescalesinaggregation) && $gradeitem->gradetype == GRADE_TYPE_SCALE) {
1618 // The scales are not included in the aggregation, ignore them.
1619 continue;
1622 // Record the ID and the weight for this grade item.
1623 $overridearray[$gradeitem->id] = array();
1624 $overridearray[$gradeitem->id]['extracredit'] = intval($gradeitem->aggregationcoef);
1625 $overridearray[$gradeitem->id]['weight'] = $gradeitem->aggregationcoef2;
1626 $overridearray[$gradeitem->id]['weightoverride'] = intval($gradeitem->weightoverride);
1627 // If this item has had its weight overridden then set the flag to true, but
1628 // only if all previous items were also overridden. Note that extra credit items
1629 // are counted as overridden grade items.
1630 if (!$gradeitem->weightoverride && $gradeitem->aggregationcoef == 0) {
1631 $automaticgradeitemspresent = true;
1634 if ($gradeitem->aggregationcoef > 0) {
1635 // An extra credit grade item doesn't contribute to $totaloverriddengrademax.
1636 continue;
1637 } else if ($gradeitem->weightoverride > 0 && $gradeitem->aggregationcoef2 <= 0) {
1638 // An overriden item that defines a weight of 0 does not contribute to $totaloverriddengrademax.
1639 continue;
1642 $totalgrademax += $gradeitem->grademax;
1643 if ($gradeitem->weightoverride > 0) {
1644 $totaloverriddenweight += $gradeitem->aggregationcoef2;
1645 $totaloverriddengrademax += $gradeitem->grademax;
1649 // Initialise this variable (used to keep track of the weight override total).
1650 $normalisetotal = 0;
1651 // Keep a record of how much the override total is to see if it is above 100. It it is then we need to set the
1652 // other weights to zero and normalise the others.
1653 $overriddentotal = 0;
1654 // If the overridden weight total is higher than 1 then set the other untouched weights to zero.
1655 $setotherweightstozero = false;
1656 // Total up all of the weights.
1657 foreach ($overridearray as $gradeitemdetail) {
1658 // If the grade item has extra credit, then don't add it to the normalisetotal.
1659 if (!$gradeitemdetail['extracredit']) {
1660 $normalisetotal += $gradeitemdetail['weight'];
1662 // The overridden total comprises of items that are set as overridden, that aren't extra credit and have a value
1663 // greater than zero.
1664 if ($gradeitemdetail['weightoverride'] && !$gradeitemdetail['extracredit'] && $gradeitemdetail['weight'] > 0) {
1665 // Add overriden weights up to see if they are greater than 1.
1666 $overriddentotal += $gradeitemdetail['weight'];
1669 if ($overriddentotal > 1) {
1670 // Make sure that this catergory of weights gets normalised.
1671 $requiresnormalising = true;
1672 // The normalised weights are only the overridden weights, so we just use the total of those.
1673 $normalisetotal = $overriddentotal;
1676 $totalnonoverriddengrademax = $totalgrademax - $totaloverriddengrademax;
1678 // This setting indicates if we should use algorithm prior to MDL-49257 fix for calculating extra credit weights.
1679 // Even though old algorith has bugs in it, we need to preserve existing grades.
1680 $gradebookcalculationfreeze = (int)get_config('core', 'gradebook_calculations_freeze_' . $this->courseid);
1681 $oldextracreditcalculation = $gradebookcalculationfreeze && ($gradebookcalculationfreeze <= 20150619);
1683 reset($children);
1684 foreach ($children as $sortorder => $child) {
1685 $gradeitem = null;
1687 if ($child['type'] == 'item') {
1688 $gradeitem = $child['object'];
1689 } else if ($child['type'] == 'category') {
1690 $gradeitem = $child['object']->load_grade_item();
1693 if ($gradeitem->gradetype == GRADE_TYPE_NONE || $gradeitem->gradetype == GRADE_TYPE_TEXT) {
1694 // Text items and none items do not have a weight, no need to set their weight to
1695 // zero as they must never be used during aggregation.
1696 continue;
1697 } else if (!$this->aggregateoutcomes && $gradeitem->is_outcome_item()) {
1698 // We will not aggregate outcome items, so we can ignore updating their weights.
1699 continue;
1700 } else if (empty($CFG->grade_includescalesinaggregation) && $gradeitem->gradetype == GRADE_TYPE_SCALE) {
1701 // We will not aggregate the scales, so we can ignore upating their weights.
1702 continue;
1703 } else if (!$oldextracreditcalculation && $gradeitem->aggregationcoef > 0 && $gradeitem->weightoverride) {
1704 // For an item with extra credit ignore other weigths and overrides but do not change anything at all
1705 // if it's weight was already overridden.
1706 continue;
1709 // Store the previous value here, no need to update if it is the same value.
1710 $prevaggregationcoef2 = $gradeitem->aggregationcoef2;
1712 if (!$oldextracreditcalculation && $gradeitem->aggregationcoef > 0 && !$gradeitem->weightoverride) {
1713 // For an item with extra credit ignore other weigths and overrides.
1714 $gradeitem->aggregationcoef2 = $totalgrademax ? ($gradeitem->grademax / $totalgrademax) : 0;
1716 } else if (!$gradeitem->weightoverride) {
1717 // Calculations with a grade maximum of zero will cause problems. Just set the weight to zero.
1718 if ($totaloverriddenweight >= 1 || $totalnonoverriddengrademax == 0 || $gradeitem->grademax == 0) {
1719 // There is no more weight to distribute.
1720 $gradeitem->aggregationcoef2 = 0;
1721 } else {
1722 // Calculate this item's weight as a percentage of the non-overridden total grade maxes
1723 // then convert it to a proportion of the available non-overriden weight.
1724 $gradeitem->aggregationcoef2 = ($gradeitem->grademax/$totalnonoverriddengrademax) *
1725 (1 - $totaloverriddenweight);
1728 } else if ((!$automaticgradeitemspresent && $normalisetotal != 1) || ($requiresnormalising)
1729 || $overridearray[$gradeitem->id]['weight'] < 0) {
1730 // Just divide the overriden weight for this item against the total weight override of all
1731 // items in this category.
1732 if ($normalisetotal == 0 || $overridearray[$gradeitem->id]['weight'] < 0) {
1733 // If the normalised total equals zero, or the weight value is less than zero,
1734 // set the weight for the grade item to zero.
1735 $gradeitem->aggregationcoef2 = 0;
1736 } else {
1737 $gradeitem->aggregationcoef2 = $overridearray[$gradeitem->id]['weight'] / $normalisetotal;
1741 if (grade_floatval($prevaggregationcoef2) !== grade_floatval($gradeitem->aggregationcoef2)) {
1742 // Update the grade item to reflect these changes.
1743 $gradeitem->update();
1749 * Given an array of grade values (numerical indices) applies droplow or keephigh rules to limit the final array.
1751 * @param array $grade_values itemid=>$grade_value float
1752 * @param array $items grade item objects
1753 * @return array Limited grades.
1755 public function apply_limit_rules(&$grade_values, $items) {
1756 $extraused = $this->is_extracredit_used();
1758 if (!empty($this->droplow)) {
1759 asort($grade_values, SORT_NUMERIC);
1760 $dropped = 0;
1762 // If we have fewer grade items available to drop than $this->droplow, use this flag to escape the loop
1763 // May occur because of "extra credit" or if droplow is higher than the number of grade items
1764 $droppedsomething = true;
1766 while ($dropped < $this->droplow && $droppedsomething) {
1767 $droppedsomething = false;
1769 $grade_keys = array_keys($grade_values);
1770 $gradekeycount = count($grade_keys);
1772 if ($gradekeycount === 0) {
1773 //We've dropped all grade items
1774 break;
1777 $originalindex = $founditemid = $foundmax = null;
1779 // Find the first remaining grade item that is available to be dropped
1780 foreach ($grade_keys as $gradekeyindex=>$gradekey) {
1781 if (!$extraused || $items[$gradekey]->aggregationcoef <= 0) {
1782 // Found a non-extra credit grade item that is eligible to be dropped
1783 $originalindex = $gradekeyindex;
1784 $founditemid = $grade_keys[$originalindex];
1785 $foundmax = $items[$founditemid]->grademax;
1786 break;
1790 if (empty($founditemid)) {
1791 // No grade items available to drop
1792 break;
1795 // Now iterate over the remaining grade items
1796 // We're looking for other grade items with the same grade value but a higher grademax
1797 $i = 1;
1798 while ($originalindex + $i < $gradekeycount) {
1800 $possibleitemid = $grade_keys[$originalindex+$i];
1801 $i++;
1803 if ($grade_values[$founditemid] != $grade_values[$possibleitemid]) {
1804 // The next grade item has a different grade value. Stop looking.
1805 break;
1808 if ($extraused && $items[$possibleitemid]->aggregationcoef > 0) {
1809 // Don't drop extra credit grade items. Continue the search.
1810 continue;
1813 if ($foundmax < $items[$possibleitemid]->grademax) {
1814 // Found a grade item with the same grade value and a higher grademax
1815 $foundmax = $items[$possibleitemid]->grademax;
1816 $founditemid = $possibleitemid;
1817 // Continue searching to see if there is an even higher grademax
1821 // Now drop whatever grade item we have found
1822 unset($grade_values[$founditemid]);
1823 $dropped++;
1824 $droppedsomething = true;
1827 } else if (!empty($this->keephigh)) {
1828 arsort($grade_values, SORT_NUMERIC);
1829 $kept = 0;
1831 foreach ($grade_values as $itemid=>$value) {
1833 if ($extraused and $items[$itemid]->aggregationcoef > 0) {
1834 // we keep all extra credits
1836 } else if ($kept < $this->keephigh) {
1837 $kept++;
1839 } else {
1840 unset($grade_values[$itemid]);
1847 * Returns whether or not we can apply the limit rules.
1849 * There are cases where drop lowest or keep highest should not be used
1850 * at all. This method will determine whether or not this logic can be
1851 * applied considering the current setup of the category.
1853 * @return bool
1855 public function can_apply_limit_rules() {
1856 if ($this->canapplylimitrules !== null) {
1857 return $this->canapplylimitrules;
1860 // Set it to be supported by default.
1861 $this->canapplylimitrules = true;
1863 // Natural aggregation.
1864 if ($this->aggregation == GRADE_AGGREGATE_SUM) {
1865 $canapply = true;
1867 // Check until one child breaks the rules.
1868 $gradeitems = $this->get_children();
1869 $validitems = 0;
1870 $lastweight = null;
1871 $lastmaxgrade = null;
1872 foreach ($gradeitems as $gradeitem) {
1873 $gi = $gradeitem['object'];
1875 if ($gradeitem['type'] == 'category') {
1876 // Sub categories are not allowed because they can have dynamic weights/maxgrades.
1877 $canapply = false;
1878 break;
1881 if ($gi->aggregationcoef > 0) {
1882 // Extra credit items are not allowed.
1883 $canapply = false;
1884 break;
1887 if ($lastweight !== null && $lastweight != $gi->aggregationcoef2) {
1888 // One of the weight differs from another item.
1889 $canapply = false;
1890 break;
1893 if ($lastmaxgrade !== null && $lastmaxgrade != $gi->grademax) {
1894 // One of the max grade differ from another item. This is not allowed for now
1895 // because we could be end up with different max grade between users for this category.
1896 $canapply = false;
1897 break;
1900 $lastweight = $gi->aggregationcoef2;
1901 $lastmaxgrade = $gi->grademax;
1904 $this->canapplylimitrules = $canapply;
1907 return $this->canapplylimitrules;
1911 * Returns true if category uses extra credit of any kind
1913 * @return bool True if extra credit used
1915 public function is_extracredit_used() {
1916 return self::aggregation_uses_extracredit($this->aggregation);
1920 * Returns true if aggregation passed is using extracredit.
1922 * @param int $aggregation Aggregation const.
1923 * @return bool True if extra credit used
1925 public static function aggregation_uses_extracredit($aggregation) {
1926 return ($aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2
1927 or $aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN
1928 or $aggregation == GRADE_AGGREGATE_SUM);
1932 * Returns true if category uses special aggregation coefficient
1934 * @return bool True if an aggregation coefficient is being used
1936 public function is_aggregationcoef_used() {
1937 return self::aggregation_uses_aggregationcoef($this->aggregation);
1942 * Returns true if aggregation uses aggregationcoef
1944 * @param int $aggregation Aggregation const.
1945 * @return bool True if an aggregation coefficient is being used
1947 public static function aggregation_uses_aggregationcoef($aggregation) {
1948 return ($aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN
1949 or $aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2
1950 or $aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN
1951 or $aggregation == GRADE_AGGREGATE_SUM);
1956 * Recursive function to find which weight/extra credit field to use in the grade item form.
1958 * @param string $first Whether or not this is the first item in the recursion
1959 * @return string
1961 public function get_coefstring($first=true) {
1962 if (!is_null($this->coefstring)) {
1963 return $this->coefstring;
1966 $overriding_coefstring = null;
1968 // Stop recursing upwards if this category has no parent
1969 if (!$first) {
1971 if ($parent_category = $this->load_parent_category()) {
1972 return $parent_category->get_coefstring(false);
1974 } else {
1975 return null;
1978 } else if ($first) {
1980 if ($parent_category = $this->load_parent_category()) {
1981 $overriding_coefstring = $parent_category->get_coefstring(false);
1985 // If an overriding coefstring has trickled down from one of the parent categories, return it. Otherwise, return self.
1986 if (!is_null($overriding_coefstring)) {
1987 return $overriding_coefstring;
1990 // No parent category is overriding this category's aggregation, return its string
1991 if ($this->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
1992 $this->coefstring = 'aggregationcoefweight';
1994 } else if ($this->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
1995 $this->coefstring = 'aggregationcoefextrasum';
1997 } else if ($this->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
1998 $this->coefstring = 'aggregationcoefextraweight';
2000 } else if ($this->aggregation == GRADE_AGGREGATE_SUM) {
2001 $this->coefstring = 'aggregationcoefextraweightsum';
2003 } else {
2004 $this->coefstring = 'aggregationcoef';
2006 return $this->coefstring;
2010 * Returns tree with all grade_items and categories as elements
2012 * @param int $courseid The course ID
2013 * @param bool $include_category_items as category children
2014 * @return array
2016 public static function fetch_course_tree($courseid, $include_category_items=false) {
2017 $course_category = grade_category::fetch_course_category($courseid);
2018 $category_array = array('object'=>$course_category, 'type'=>'category', 'depth'=>1,
2019 'children'=>$course_category->get_children($include_category_items));
2021 $course_category->sortorder = $course_category->get_sortorder();
2022 $sortorder = $course_category->get_sortorder();
2023 return grade_category::_fetch_course_tree_recursion($category_array, $sortorder);
2027 * An internal function that recursively sorts grade categories within a course
2029 * @param array $category_array The seed of the recursion
2030 * @param int $sortorder The current sortorder
2031 * @return array An array containing 'object', 'type', 'depth' and optionally 'children'
2033 static private function _fetch_course_tree_recursion($category_array, &$sortorder) {
2034 if (isset($category_array['object']->gradetype) && $category_array['object']->gradetype==GRADE_TYPE_NONE) {
2035 return null;
2038 // store the grade_item or grade_category instance with extra info
2039 $result = array('object'=>$category_array['object'], 'type'=>$category_array['type'], 'depth'=>$category_array['depth']);
2041 // reuse final grades if there
2042 if (array_key_exists('finalgrades', $category_array)) {
2043 $result['finalgrades'] = $category_array['finalgrades'];
2046 // recursively resort children
2047 if (!empty($category_array['children'])) {
2048 $result['children'] = array();
2049 //process the category item first
2050 $child = null;
2052 foreach ($category_array['children'] as $oldorder=>$child_array) {
2054 if ($child_array['type'] == 'courseitem' or $child_array['type'] == 'categoryitem') {
2055 $child = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
2056 if (!empty($child)) {
2057 $result['children'][$sortorder] = $child;
2062 foreach ($category_array['children'] as $oldorder=>$child_array) {
2064 if ($child_array['type'] != 'courseitem' and $child_array['type'] != 'categoryitem') {
2065 $child = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
2066 if (!empty($child)) {
2067 $result['children'][++$sortorder] = $child;
2073 return $result;
2077 * Fetches and returns all the children categories and/or grade_items belonging to this category.
2078 * By default only returns the immediate children (depth=1), but deeper levels can be requested,
2079 * as well as all levels (0). The elements are indexed by sort order.
2081 * @param bool $include_category_items Whether or not to include category grade_items in the children array
2082 * @return array Array of child objects (grade_category and grade_item).
2084 public function get_children($include_category_items=false) {
2085 global $DB;
2087 // This function must be as fast as possible ;-)
2088 // fetch all course grade items and categories into memory - we do not expect hundreds of these in course
2089 // we have to limit the number of queries though, because it will be used often in grade reports
2091 $cats = $DB->get_records('grade_categories', array('courseid' => $this->courseid));
2092 $items = $DB->get_records('grade_items', array('courseid' => $this->courseid));
2094 // init children array first
2095 foreach ($cats as $catid=>$cat) {
2096 $cats[$catid]->children = array();
2099 //first attach items to cats and add category sortorder
2100 foreach ($items as $item) {
2102 if ($item->itemtype == 'course' or $item->itemtype == 'category') {
2103 $cats[$item->iteminstance]->sortorder = $item->sortorder;
2105 if (!$include_category_items) {
2106 continue;
2108 $categoryid = $item->iteminstance;
2110 } else {
2111 $categoryid = $item->categoryid;
2112 if (empty($categoryid)) {
2113 debugging('Found a grade item that isnt in a category');
2117 // prevent problems with duplicate sortorders in db
2118 $sortorder = $item->sortorder;
2120 while (array_key_exists($categoryid, $cats)
2121 && array_key_exists($sortorder, $cats[$categoryid]->children)) {
2123 $sortorder++;
2126 $cats[$categoryid]->children[$sortorder] = $item;
2130 // now find the requested category and connect categories as children
2131 $category = false;
2133 foreach ($cats as $catid=>$cat) {
2135 if (empty($cat->parent)) {
2137 if ($cat->path !== '/'.$cat->id.'/') {
2138 $grade_category = new grade_category($cat, false);
2139 $grade_category->path = '/'.$cat->id.'/';
2140 $grade_category->depth = 1;
2141 $grade_category->update('system');
2142 return $this->get_children($include_category_items);
2145 } else {
2147 if (empty($cat->path) or !preg_match('|/'.$cat->parent.'/'.$cat->id.'/$|', $cat->path)) {
2148 //fix paths and depts
2149 static $recursioncounter = 0; // prevents infinite recursion
2150 $recursioncounter++;
2152 if ($recursioncounter < 5) {
2153 // fix paths and depths!
2154 $grade_category = new grade_category($cat, false);
2155 $grade_category->depth = 0;
2156 $grade_category->path = null;
2157 $grade_category->update('system');
2158 return $this->get_children($include_category_items);
2161 // prevent problems with duplicate sortorders in db
2162 $sortorder = $cat->sortorder;
2164 while (array_key_exists($sortorder, $cats[$cat->parent]->children)) {
2165 //debugging("$sortorder exists in cat loop");
2166 $sortorder++;
2169 $cats[$cat->parent]->children[$sortorder] = &$cats[$catid];
2172 if ($catid == $this->id) {
2173 $category = &$cats[$catid];
2177 unset($items); // not needed
2178 unset($cats); // not needed
2180 $children_array = array();
2181 if (is_object($category)) {
2182 $children_array = grade_category::_get_children_recursion($category);
2183 ksort($children_array);
2186 return $children_array;
2191 * Private method used to retrieve all children of this category recursively
2193 * @param grade_category $category Source of current recursion
2194 * @return array An array of child grade categories
2196 private static function _get_children_recursion($category) {
2198 $children_array = array();
2199 foreach ($category->children as $sortorder=>$child) {
2201 if (property_exists($child, 'itemtype')) {
2202 $grade_item = new grade_item($child, false);
2204 if (in_array($grade_item->itemtype, array('course', 'category'))) {
2205 $type = $grade_item->itemtype.'item';
2206 $depth = $category->depth;
2208 } else {
2209 $type = 'item';
2210 $depth = $category->depth; // we use this to set the same colour
2212 $children_array[$sortorder] = array('object'=>$grade_item, 'type'=>$type, 'depth'=>$depth);
2214 } else {
2215 $children = grade_category::_get_children_recursion($child);
2216 $grade_category = new grade_category($child, false);
2218 if (empty($children)) {
2219 $children = array();
2221 $children_array[$sortorder] = array('object'=>$grade_category, 'type'=>'category', 'depth'=>$grade_category->depth, 'children'=>$children);
2225 // sort the array
2226 ksort($children_array);
2228 return $children_array;
2232 * Uses {@link get_grade_item()} to load or create a grade_item, then saves it as $this->grade_item.
2234 * @return grade_item
2236 public function load_grade_item() {
2237 if (empty($this->grade_item)) {
2238 $this->grade_item = $this->get_grade_item();
2240 return $this->grade_item;
2244 * Retrieves this grade categories' associated grade_item from the database
2246 * If no grade_item exists yet, creates one.
2248 * @return grade_item
2250 public function get_grade_item() {
2251 if (empty($this->id)) {
2252 debugging("Attempt to obtain a grade_category's associated grade_item without the category's ID being set.");
2253 return false;
2256 if (empty($this->parent)) {
2257 $params = array('courseid'=>$this->courseid, 'itemtype'=>'course', 'iteminstance'=>$this->id);
2259 } else {
2260 $params = array('courseid'=>$this->courseid, 'itemtype'=>'category', 'iteminstance'=>$this->id);
2263 if (!$grade_items = grade_item::fetch_all($params)) {
2264 // create a new one
2265 $grade_item = new grade_item($params, false);
2266 $grade_item->gradetype = GRADE_TYPE_VALUE;
2267 $grade_item->insert('system');
2269 } else if (count($grade_items) == 1) {
2270 // found existing one
2271 $grade_item = reset($grade_items);
2273 } else {
2274 debugging("Found more than one grade_item attached to category id:".$this->id);
2275 // return first one
2276 $grade_item = reset($grade_items);
2279 return $grade_item;
2283 * Uses $this->parent to instantiate $this->parent_category based on the referenced record in the DB
2285 * @return grade_category The parent category
2287 public function load_parent_category() {
2288 if (empty($this->parent_category) && !empty($this->parent)) {
2289 $this->parent_category = $this->get_parent_category();
2291 return $this->parent_category;
2295 * Uses $this->parent to instantiate and return a grade_category object
2297 * @return grade_category Returns the parent category or null if this category has no parent
2299 public function get_parent_category() {
2300 if (!empty($this->parent)) {
2301 $parent_category = new grade_category(array('id' => $this->parent));
2302 return $parent_category;
2303 } else {
2304 return null;
2309 * Returns the most descriptive field for this grade category
2311 * @return string name
2313 public function get_name() {
2314 global $DB;
2315 // For a course category, we return the course name if the fullname is set to '?' in the DB (empty in the category edit form)
2316 if (empty($this->parent) && $this->fullname == '?') {
2317 $course = $DB->get_record('course', array('id'=> $this->courseid));
2318 return format_string($course->fullname, false, array("context" => context_course::instance($this->courseid)));
2320 } else {
2321 // Grade categories can't be set up at system context (unlike scales and outcomes)
2322 // We therefore must have a courseid, and don't need to handle system contexts when filtering.
2323 return format_string($this->fullname, false, array("context" => context_course::instance($this->courseid)));
2328 * Describe the aggregation settings for this category so the reports make more sense.
2330 * @return string description
2332 public function get_description() {
2333 $allhelp = array();
2334 if ($this->aggregation != GRADE_AGGREGATE_SUM) {
2335 $aggrstrings = grade_helper::get_aggregation_strings();
2336 $allhelp[] = $aggrstrings[$this->aggregation];
2339 if ($this->droplow && $this->can_apply_limit_rules()) {
2340 $allhelp[] = get_string('droplowestvalues', 'grades', $this->droplow);
2342 if ($this->keephigh && $this->can_apply_limit_rules()) {
2343 $allhelp[] = get_string('keephighestvalues', 'grades', $this->keephigh);
2345 if (!$this->aggregateonlygraded) {
2346 $allhelp[] = get_string('aggregatenotonlygraded', 'grades');
2348 if ($allhelp) {
2349 return implode('. ', $allhelp) . '.';
2351 return '';
2355 * Sets this category's parent id
2357 * @param int $parentid The ID of the category that is the new parent to $this
2358 * @param string $source From where was the object updated (mod/forum, manual, etc.)
2359 * @return bool success
2361 public function set_parent($parentid, $source=null) {
2362 if ($this->parent == $parentid) {
2363 return true;
2366 if ($parentid == $this->id) {
2367 print_error('cannotassignselfasparent');
2370 if (empty($this->parent) and $this->is_course_category()) {
2371 print_error('cannothaveparentcate');
2374 // find parent and check course id
2375 if (!$parent_category = grade_category::fetch(array('id'=>$parentid, 'courseid'=>$this->courseid))) {
2376 return false;
2379 $this->force_regrading();
2381 // set new parent category
2382 $this->parent = $parent_category->id;
2383 $this->parent_category =& $parent_category;
2384 $this->path = null; // remove old path and depth - will be recalculated in update()
2385 $this->depth = 0; // remove old path and depth - will be recalculated in update()
2386 $this->update($source);
2388 return $this->update($source);
2392 * Returns the final grade values for this grade category.
2394 * @param int $userid Optional user ID to retrieve a single user's final grade
2395 * @return mixed An array of all final_grades (stdClass objects) for this grade_item, or a single final_grade.
2397 public function get_final($userid=null) {
2398 $this->load_grade_item();
2399 return $this->grade_item->get_final($userid);
2403 * Returns the sortorder of the grade categories' associated grade_item
2405 * This method is also available in grade_item for cases where the object type is not known.
2407 * @return int Sort order
2409 public function get_sortorder() {
2410 $this->load_grade_item();
2411 return $this->grade_item->get_sortorder();
2415 * Returns the idnumber of the grade categories' associated grade_item.
2417 * This method is also available in grade_item for cases where the object type is not known.
2419 * @return string idnumber
2421 public function get_idnumber() {
2422 $this->load_grade_item();
2423 return $this->grade_item->get_idnumber();
2427 * Sets the sortorder variable for this category.
2429 * This method is also available in grade_item, for cases where the object type is not know.
2431 * @param int $sortorder The sortorder to assign to this category
2433 public function set_sortorder($sortorder) {
2434 $this->load_grade_item();
2435 $this->grade_item->set_sortorder($sortorder);
2439 * Move this category after the given sortorder
2441 * Does not change the parent
2443 * @param int $sortorder to place after.
2444 * @return void
2446 public function move_after_sortorder($sortorder) {
2447 $this->load_grade_item();
2448 $this->grade_item->move_after_sortorder($sortorder);
2452 * Return true if this is the top most category that represents the total course grade.
2454 * @return bool
2456 public function is_course_category() {
2457 $this->load_grade_item();
2458 return $this->grade_item->is_course_item();
2462 * Return the course level grade_category object
2464 * @param int $courseid The Course ID
2465 * @return grade_category Returns the course level grade_category instance
2467 public static function fetch_course_category($courseid) {
2468 if (empty($courseid)) {
2469 debugging('Missing course id!');
2470 return false;
2473 // course category has no parent
2474 if ($course_category = grade_category::fetch(array('courseid'=>$courseid, 'parent'=>null))) {
2475 return $course_category;
2478 // create a new one
2479 $course_category = new grade_category();
2480 $course_category->insert_course_category($courseid);
2482 return $course_category;
2486 * Is grading object editable?
2488 * @return bool
2490 public function is_editable() {
2491 return true;
2495 * Returns the locked state/date of the grade categories' associated grade_item.
2497 * This method is also available in grade_item, for cases where the object type is not known.
2499 * @return bool
2501 public function is_locked() {
2502 $this->load_grade_item();
2503 return $this->grade_item->is_locked();
2507 * Sets the grade_item's locked variable and updates the grade_item.
2509 * Calls set_locked() on the categories' grade_item
2511 * @param int $lockedstate 0, 1 or a timestamp int(10) after which date the item will be locked.
2512 * @param bool $cascade lock/unlock child objects too
2513 * @param bool $refresh refresh grades when unlocking
2514 * @return bool success if category locked (not all children mayb be locked though)
2516 public function set_locked($lockedstate, $cascade=false, $refresh=true) {
2517 $this->load_grade_item();
2519 $result = $this->grade_item->set_locked($lockedstate, $cascade, true);
2521 if ($cascade) {
2522 //process all children - items and categories
2523 if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
2525 foreach ($children as $child) {
2526 $child->set_locked($lockedstate, true, false);
2528 if (empty($lockedstate) and $refresh) {
2529 //refresh when unlocking
2530 $child->refresh_grades();
2535 if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
2537 foreach ($children as $child) {
2538 $child->set_locked($lockedstate, true, true);
2543 return $result;
2547 * Overrides grade_object::set_properties() to add special handling for changes to category aggregation types
2549 * @param stdClass $instance the object to set the properties on
2550 * @param array|stdClass $params Either an associative array or an object containing property name, property value pairs
2552 public static function set_properties(&$instance, $params) {
2553 global $DB;
2555 $fromaggregation = $instance->aggregation;
2557 parent::set_properties($instance, $params);
2559 // The aggregation method is changing and this category has already been saved.
2560 if (isset($params->aggregation) && !empty($instance->id)) {
2561 $achildwasdupdated = false;
2563 // Get all its children.
2564 $children = $instance->get_children();
2565 foreach ($children as $child) {
2566 $item = $child['object'];
2567 if ($child['type'] == 'category') {
2568 $item = $item->load_grade_item();
2571 // Set the new aggregation fields.
2572 if ($item->set_aggregation_fields_for_aggregation($fromaggregation, $params->aggregation)) {
2573 $item->update();
2574 $achildwasdupdated = true;
2578 // If this is the course category, it is possible that its grade item was set as needsupdate
2579 // by one of its children. If we keep a reference to that stale object we might cause the
2580 // needsupdate flag to be lost. It's safer to just reload the grade_item from the database.
2581 if ($achildwasdupdated && !empty($instance->grade_item) && $instance->is_course_category()) {
2582 $instance->grade_item = null;
2583 $instance->load_grade_item();
2589 * Sets the grade_item's hidden variable and updates the grade_item.
2591 * Overrides grade_item::set_hidden() to add cascading of the hidden value to grade items in this grade category
2593 * @param int $hidden 0 mean always visible, 1 means always hidden and a number > 1 is a timestamp to hide until
2594 * @param bool $cascade apply to child objects too
2596 public function set_hidden($hidden, $cascade=false) {
2597 $this->load_grade_item();
2598 //this hides the category itself and everything it contains
2599 parent::set_hidden($hidden, $cascade);
2601 if ($cascade) {
2603 // This hides the associated grade item (the course/category total).
2604 $this->grade_item->set_hidden($hidden, $cascade);
2606 if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
2608 foreach ($children as $child) {
2609 if ($child->can_control_visibility()) {
2610 $child->set_hidden($hidden, $cascade);
2615 if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
2617 foreach ($children as $child) {
2618 $child->set_hidden($hidden, $cascade);
2623 //if marking category visible make sure parent category is visible MDL-21367
2624 if( !$hidden ) {
2625 $category_array = grade_category::fetch_all(array('id'=>$this->parent));
2626 if ($category_array && array_key_exists($this->parent, $category_array)) {
2627 $category = $category_array[$this->parent];
2628 //call set_hidden on the category regardless of whether it is hidden as its parent might be hidden
2629 $category->set_hidden($hidden, false);
2635 * Applies default settings on this category
2637 * @return bool True if anything changed
2639 public function apply_default_settings() {
2640 global $CFG;
2642 foreach ($this->forceable as $property) {
2644 if (isset($CFG->{"grade_$property"})) {
2646 if ($CFG->{"grade_$property"} == -1) {
2647 continue; //temporary bc before version bump
2649 $this->$property = $CFG->{"grade_$property"};
2655 * Applies forced settings on this category
2657 * @return bool True if anything changed
2659 public function apply_forced_settings() {
2660 global $CFG;
2662 $updated = false;
2664 foreach ($this->forceable as $property) {
2666 if (isset($CFG->{"grade_$property"}) and isset($CFG->{"grade_{$property}_flag"}) and
2667 ((int) $CFG->{"grade_{$property}_flag"} & 1)) {
2669 if ($CFG->{"grade_$property"} == -1) {
2670 continue; //temporary bc before version bump
2672 $this->$property = $CFG->{"grade_$property"};
2673 $updated = true;
2677 return $updated;
2681 * Notification of change in forced category settings.
2683 * Causes all course and category grade items to be marked as needing to be updated
2685 public static function updated_forced_settings() {
2686 global $CFG, $DB;
2687 $params = array(1, 'course', 'category');
2688 $sql = "UPDATE {grade_items} SET needsupdate=? WHERE itemtype=? or itemtype=?";
2689 $DB->execute($sql, $params);
2693 * Determine the default aggregation values for a given aggregation method.
2695 * @param int $aggregationmethod The aggregation method constant value.
2696 * @return array Containing the keys 'aggregationcoef', 'aggregationcoef2' and 'weightoverride'.
2698 public static function get_default_aggregation_coefficient_values($aggregationmethod) {
2699 $defaultcoefficients = array(
2700 'aggregationcoef' => 0,
2701 'aggregationcoef2' => 0,
2702 'weightoverride' => 0
2705 switch ($aggregationmethod) {
2706 case GRADE_AGGREGATE_WEIGHTED_MEAN:
2707 $defaultcoefficients['aggregationcoef'] = 1;
2708 break;
2709 case GRADE_AGGREGATE_SUM:
2710 $defaultcoefficients['aggregationcoef2'] = 1;
2711 break;
2714 return $defaultcoefficients;
2718 * Cleans the cache.
2720 * We invalidate them all so it can be completely reloaded.
2722 * Being conservative here, if there is a new grade_category we purge them, the important part
2723 * is that this is not purged when there are no changes in grade_categories.
2725 * @param bool $deleted
2726 * @return void
2728 protected function notify_changed($deleted) {
2729 self::clean_record_set();
2733 * Generates a unique key per query.
2735 * Not unique between grade_object children. self::retrieve_record_set and self::set_record_set will be in charge of
2736 * selecting the appropriate cache.
2738 * @param array $params An array of conditions like $fieldname => $fieldvalue
2739 * @return string
2741 protected static function generate_record_set_key($params) {
2742 return sha1(json_encode($params));
2746 * Tries to retrieve a record set from the cache.
2748 * @param array $params The query params
2749 * @return grade_object[]|bool An array of grade_objects or false if not found.
2751 protected static function retrieve_record_set($params) {
2752 $cache = cache::make('core', 'grade_categories');
2753 return $cache->get(self::generate_record_set_key($params));
2757 * Sets a result to the records cache, even if there were no results.
2759 * @param string $params The query params
2760 * @param grade_object[]|bool $records An array of grade_objects or false if there are no records matching the $key filters
2761 * @return void
2763 protected static function set_record_set($params, $records) {
2764 $cache = cache::make('core', 'grade_categories');
2765 return $cache->set(self::generate_record_set_key($params), $records);
2769 * Cleans the cache.
2771 * Aggressive deletion to be conservative given the gradebook design.
2772 * The key is based on the requested params, not easy nor worth to purge selectively.
2774 * @return void
2776 public static function clean_record_set() {
2777 cache_helper::purge_by_event('changesingradecategories');