MDL-15476
[moodle-linuxchix.git] / lib / grade / grade_category.php
blobd678c1a9da7335c3f35eb55defb40498fe242e46
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once('grade_object.php');
28 class grade_category extends grade_object {
29 /**
30 * The DB table.
31 * @var string $table
33 var $table = 'grade_categories';
35 /**
36 * Array of required table fields, must start with 'id'.
37 * @var array $required_fields
39 var $required_fields = array('id', 'courseid', 'parent', 'depth', 'path', 'fullname', 'aggregation',
40 'keephigh', 'droplow', 'aggregateonlygraded', 'aggregateoutcomes',
41 'aggregatesubcats', 'timecreated', 'timemodified');
43 /**
44 * The course this category belongs to.
45 * @var int $courseid
47 var $courseid;
49 /**
50 * The category this category belongs to (optional).
51 * @var int $parent
53 var $parent;
55 /**
56 * The grade_category object referenced by $this->parent (PK).
57 * @var object $parent_category
59 var $parent_category;
61 /**
62 * The number of parents this category has.
63 * @var int $depth
65 var $depth = 0;
67 /**
68 * Shows the hierarchical path for this category as /1/2/3/ (like course_categories), the last number being
69 * this category's autoincrement ID number.
70 * @var string $path
72 var $path;
74 /**
75 * The name of this category.
76 * @var string $fullname
78 var $fullname;
80 /**
81 * A constant pointing to one of the predefined aggregation strategies (none, mean, median, sum etc) .
82 * @var int $aggregation
84 var $aggregation = GRADE_AGGREGATE_MEAN;
86 /**
87 * Keep only the X highest items.
88 * @var int $keephigh
90 var $keephigh = 0;
92 /**
93 * Drop the X lowest items.
94 * @var int $droplow
96 var $droplow = 0;
98 /**
99 * Aggregate only graded items
100 * @var int $aggregateonlygraded
102 var $aggregateonlygraded = 0;
105 * Aggregate outcomes together with normal items
106 * @var int $aggregateoutcomes
108 var $aggregateoutcomes = 0;
111 * Ignore subcategories when aggregating
112 * @var int $aggregatesubcats
114 var $aggregatesubcats = 0;
117 * Array of grade_items or grade_categories nested exactly 1 level below this category
118 * @var array $children
120 var $children;
123 * A hierarchical array of all children below this category. This is stored separately from
124 * $children because it is more memory-intensive and may not be used as often.
125 * @var array $all_children
127 var $all_children;
130 * An associated grade_item object, with itemtype=category, used to calculate and cache a set of grade values
131 * for this category.
132 * @var object $grade_item
134 var $grade_item;
137 * Temporary sortorder for speedup of children resorting
139 var $sortorder;
142 * List of options which can be "forced" from site settings.
144 var $forceable = array('aggregation', 'keephigh', 'droplow', 'aggregateonlygraded', 'aggregateoutcomes', 'aggregatesubcats');
147 * Builds this category's path string based on its parents (if any) and its own id number.
148 * This is typically done just before inserting this object in the DB for the first time,
149 * or when a new parent is added or changed. It is a recursive function: once the calling
150 * object no longer has a parent, the path is complete.
152 * @static
153 * @param object $grade_category
154 * @return int The depth of this category (2 means there is one parent)
156 function build_path($grade_category) {
157 if (empty($grade_category->parent)) {
158 return '/'.$grade_category->id.'/';
159 } else {
160 $parent = get_record('grade_categories', 'id', $grade_category->parent);
161 return grade_category::build_path($parent).$grade_category->id.'/';
166 * Finds and returns a grade_category instance based on params.
167 * @static
169 * @param array $params associative arrays varname=>value
170 * @return object grade_category instance or false if none found.
172 function fetch($params) {
173 return grade_object::fetch_helper('grade_categories', 'grade_category', $params);
177 * Finds and returns all grade_category instances based on params.
178 * @static
180 * @param array $params associative arrays varname=>value
181 * @return array array of grade_category insatnces or false if none found.
183 function fetch_all($params) {
184 return grade_object::fetch_all_helper('grade_categories', 'grade_category', $params);
188 * In addition to update() as defined in grade_object, call force_regrading of parent categories, if applicable.
189 * @param string $source from where was the object updated (mod/forum, manual, etc.)
190 * @return boolean success
192 function update($source=null) {
193 // load the grade item or create a new one
194 $this->load_grade_item();
196 // force recalculation of path;
197 if (empty($this->path)) {
198 $this->path = grade_category::build_path($this);
199 $this->depth = substr_count($this->path, '/') - 1;
200 $updatechildren = true;
201 } else {
202 $updatechildren = false;
205 $this->apply_forced_settings();
207 // these are exclusive
208 if ($this->droplow > 0) {
209 $this->keephigh = 0;
210 } else if ($this->keephigh > 0) {
211 $this->droplow = 0;
214 // Recalculate grades if needed
215 if ($this->qualifies_for_regrading()) {
216 $this->force_regrading();
219 $this->timemodified = time();
221 $result = parent::update($source);
223 // now update paths in all child categories
224 if ($result and $updatechildren) {
225 if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
226 foreach ($children as $child) {
227 $child->path = null;
228 $child->depth = 0;
229 $child->update($source);
234 return $result;
238 * If parent::delete() is successful, send force_regrading message to parent category.
239 * @param string $source from where was the object deleted (mod/forum, manual, etc.)
240 * @return boolean success
242 function delete($source=null) {
243 $grade_item = $this->load_grade_item();
245 if ($this->is_course_category()) {
246 if ($categories = grade_category::fetch_all(array('courseid'=>$this->courseid))) {
247 foreach ($categories as $category) {
248 if ($category->id == $this->id) {
249 continue; // do not delete course category yet
251 $category->delete($source);
255 if ($items = grade_item::fetch_all(array('courseid'=>$this->courseid))) {
256 foreach ($items as $item) {
257 if ($item->id == $grade_item->id) {
258 continue; // do not delete course item yet
260 $item->delete($source);
264 } else {
265 $this->force_regrading();
267 $parent = $this->load_parent_category();
269 // Update children's categoryid/parent field first
270 if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
271 foreach ($children as $child) {
272 $child->set_parent($parent->id);
275 if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
276 foreach ($children as $child) {
277 $child->set_parent($parent->id);
282 // first delete the attached grade item and grades
283 $grade_item->delete($source);
285 // delete category itself
286 return parent::delete($source);
290 * In addition to the normal insert() defined in grade_object, this method sets the depth
291 * and path for this object, and update the record accordingly. The reason why this must
292 * be done here instead of in the constructor, is that they both need to know the record's
293 * id number, which only gets created at insertion time.
294 * This method also creates an associated grade_item if this wasn't done during construction.
295 * @param string $source from where was the object inserted (mod/forum, manual, etc.)
296 * @return int PK ID if successful, false otherwise
298 function insert($source=null) {
300 if (empty($this->courseid)) {
301 error('Can not insert grade category without course id!');
304 if (empty($this->parent)) {
305 $course_category = grade_category::fetch_course_category($this->courseid);
306 $this->parent = $course_category->id;
309 $this->path = null;
311 $this->timecreated = $this->timemodified = time();
313 if (!parent::insert($source)) {
314 debugging("Could not insert this category: " . print_r($this, true));
315 return false;
318 $this->force_regrading();
320 // build path and depth
321 $this->update($source);
323 return $this->id;
327 * Internal function - used only from fetch_course_category()
328 * Normal insert() can not be used for course category
329 * @param int $courseid
330 * @return bool success
332 function insert_course_category($courseid) {
333 $this->courseid = $courseid;
334 $this->fullname = '?';
335 $this->path = null;
336 $this->parent = null;
337 $this->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
339 $this->apply_default_settings();
340 $this->apply_forced_settings();
342 $this->timecreated = $this->timemodified = time();
344 if (!parent::insert('system')) {
345 debugging("Could not insert this category: " . print_r($this, true));
346 return false;
349 // build path and depth
350 $this->update('system');
352 return $this->id;
356 * Compares the values held by this object with those of the matching record in DB, and returns
357 * whether or not these differences are sufficient to justify an update of all parent objects.
358 * This assumes that this object has an id number and a matching record in DB. If not, it will return false.
359 * @return boolean
361 function qualifies_for_regrading() {
362 if (empty($this->id)) {
363 debugging("Can not regrade non existing category");
364 return false;
367 $db_item = grade_category::fetch(array('id'=>$this->id));
369 $aggregationdiff = $db_item->aggregation != $this->aggregation;
370 $keephighdiff = $db_item->keephigh != $this->keephigh;
371 $droplowdiff = $db_item->droplow != $this->droplow;
372 $aggonlygrddiff = $db_item->aggregateonlygraded != $this->aggregateonlygraded;
373 $aggoutcomesdiff = $db_item->aggregateoutcomes != $this->aggregateoutcomes;
374 $aggsubcatsdiff = $db_item->aggregatesubcats != $this->aggregatesubcats;
376 return ($aggregationdiff || $keephighdiff || $droplowdiff || $aggonlygrddiff || $aggoutcomesdiff || $aggsubcatsdiff);
380 * Marks the category and course item as needing update - categories are always regraded.
381 * @return void
383 function force_regrading() {
384 $grade_item = $this->load_grade_item();
385 $grade_item->force_regrading();
389 * Generates and saves final grades in associated category grade item.
390 * These immediate children must already have their own final grades.
391 * The category's aggregation method is used to generate final grades.
393 * Please note that category grade is either calculated or aggregated - not both at the same time.
395 * This method must be used ONLY from grade_item::regrade_final_grades(),
396 * because the calculation must be done in correct order!
398 * Steps to follow:
399 * 1. Get final grades from immediate children
400 * 3. Aggregate these grades
401 * 4. Save them in final grades of associated category grade item
403 function generate_grades($userid=null) {
404 global $CFG;
406 $this->load_grade_item();
408 if ($this->grade_item->is_locked()) {
409 return true; // no need to recalculate locked items
412 // find grade items of immediate children (category or grade items) and force site settings
413 $depends_on = $this->grade_item->depends_on();
415 if (empty($depends_on)) {
416 $items = false;
417 } else {
418 $gis = implode(',', $depends_on);
419 $sql = "SELECT *
420 FROM {$CFG->prefix}grade_items
421 WHERE id IN ($gis)";
422 $items = get_records_sql($sql);
425 if ($userid) {
426 $usersql = "AND g.userid=$userid";
427 } else {
428 $usersql = "";
431 $grade_inst = new grade_grade();
432 $fields = 'g.'.implode(',g.', $grade_inst->required_fields);
434 // where to look for final grades - include grade of this item too, we will store the results there
435 $gis = implode(',', array_merge($depends_on, array($this->grade_item->id)));
436 $sql = "SELECT $fields
437 FROM {$CFG->prefix}grade_grades g, {$CFG->prefix}grade_items gi
438 WHERE gi.id = g.itemid AND gi.id IN ($gis) $usersql
439 ORDER BY g.userid";
441 // group the results by userid and aggregate the grades for this user
442 if ($rs = get_recordset_sql($sql)) {
443 $prevuser = 0;
444 $grade_values = array();
445 $excluded = array();
446 $oldgrade = null;
447 while ($used = rs_fetch_next_record($rs)) {
448 if ($used->userid != $prevuser) {
449 $this->aggregate_grades($prevuser, $items, $grade_values, $oldgrade, $excluded);
450 $prevuser = $used->userid;
451 $grade_values = array();
452 $excluded = array();
453 $oldgrade = null;
455 $grade_values[$used->itemid] = $used->finalgrade;
456 if ($used->excluded) {
457 $excluded[] = $used->itemid;
459 if ($this->grade_item->id == $used->itemid) {
460 $oldgrade = $used;
463 $this->aggregate_grades($prevuser, $items, $grade_values, $oldgrade, $excluded);//the last one
464 rs_close($rs);
467 return true;
471 * internal function for category grades aggregation
473 * @param int $userid
474 * @param array $items
475 * @param array $grade_values
476 * @param object $oldgrade
477 * @param bool $excluded
478 * @return boolean (just plain return;)
480 function aggregate_grades($userid, $items, $grade_values, $oldgrade, $excluded) {
481 global $CFG;
482 if (empty($userid)) {
483 //ignore first call
484 return;
487 if ($oldgrade) {
488 $oldfinalgrade = $oldgrade->finalgrade;
489 $grade = new grade_grade($oldgrade, false);
490 $grade->grade_item =& $this->grade_item;
492 } else {
493 // insert final grade - it will be needed later anyway
494 $grade = new grade_grade(array('itemid'=>$this->grade_item->id, 'userid'=>$userid), false);
495 $grade->grade_item =& $this->grade_item;
496 $grade->insert('system');
497 $oldfinalgrade = null;
500 // no need to recalculate locked or overridden grades
501 if ($grade->is_locked() or $grade->is_overridden()) {
502 return;
505 // can not use own final category grade in calculation
506 unset($grade_values[$this->grade_item->id]);
509 /// sum is a special aggregation types - it adjusts the min max, does not use relative values
510 if ($this->aggregation == GRADE_AGGREGATE_SUM) {
511 $this->sum_grades($grade, $oldfinalgrade, $items, $grade_values, $excluded);
512 return;
515 // if no grades calculation possible or grading not allowed clear final grade
516 if (empty($grade_values) or empty($items) or ($this->grade_item->gradetype != GRADE_TYPE_VALUE and $this->grade_item->gradetype != GRADE_TYPE_SCALE)) {
517 $grade->finalgrade = null;
518 if (!is_null($oldfinalgrade)) {
519 $grade->update('aggregation');
521 return;
524 /// normalize the grades first - all will have value 0...1
525 // ungraded items are not used in aggregation
526 foreach ($grade_values as $itemid=>$v) {
527 if (is_null($v)) {
528 // null means no grade
529 unset($grade_values[$itemid]);
530 continue;
531 } else if (in_array($itemid, $excluded)) {
532 unset($grade_values[$itemid]);
533 continue;
535 $grade_values[$itemid] = grade_grade::standardise_score($v, $items[$itemid]->grademin, $items[$itemid]->grademax, 0, 1);
538 // use min grade if grade missing for these types
539 if (!$this->aggregateonlygraded) {
540 foreach($items as $itemid=>$value) {
541 if (!isset($grade_values[$itemid]) and !in_array($itemid, $excluded)) {
542 $grade_values[$itemid] = 0;
547 // limit and sort
548 $this->apply_limit_rules($grade_values);
549 asort($grade_values, SORT_NUMERIC);
551 // let's see we have still enough grades to do any statistics
552 if (count($grade_values) == 0) {
553 // not enough attempts yet
554 $grade->finalgrade = null;
555 if (!is_null($oldfinalgrade)) {
556 $grade->update('aggregation');
558 return;
561 // do the maths
562 $agg_grade = $this->aggregate_values($grade_values, $items);
564 // recalculate the grade back to requested range
565 $finalgrade = grade_grade::standardise_score($agg_grade, 0, 1, $this->grade_item->grademin, $this->grade_item->grademax);
567 if (is_null($finalgrade)) {
568 $grade->finalgrade = null;
569 } else {
570 $grade->finalgrade = (float)bounded_number($this->grade_item->grademin, $finalgrade, $this->grade_item->grademax);
573 // update in db if changed
574 if (grade_floats_different($grade->finalgrade, $oldfinalgrade)) {
575 $grade->update('aggregation');
578 return;
582 * Internal function - aggregation maths.
584 function aggregate_values($grade_values, $items) {
585 switch ($this->aggregation) {
586 case GRADE_AGGREGATE_MEDIAN: // Middle point value in the set: ignores frequencies
587 $num = count($grade_values);
588 $grades = array_values($grade_values);
589 if ($num % 2 == 0) {
590 $agg_grade = ($grades[intval($num/2)-1] + $grades[intval($num/2)]) / 2;
591 } else {
592 $agg_grade = $grades[intval(($num/2)-0.5)];
594 break;
596 case GRADE_AGGREGATE_MIN:
597 $agg_grade = reset($grade_values);
598 break;
600 case GRADE_AGGREGATE_MAX:
601 $agg_grade = array_pop($grade_values);
602 break;
604 case GRADE_AGGREGATE_MODE: // the most common value, average used if multimode
605 $freq = array_count_values($grade_values);
606 arsort($freq); // sort by frequency keeping keys
607 $top = reset($freq); // highest frequency count
608 $modes = array_keys($freq, $top); // search for all modes (have the same highest count)
609 rsort($modes, SORT_NUMERIC); // get highes mode
610 $agg_grade = reset($modes);
611 break;
613 case GRADE_AGGREGATE_WEIGHTED_MEAN: // Weighted average of all existing final grades, weight specified in coef
614 $weightsum = 0;
615 $sum = 0;
616 foreach($grade_values as $itemid=>$grade_value) {
617 if ($items[$itemid]->aggregationcoef <= 0) {
618 continue;
620 $weightsum += $items[$itemid]->aggregationcoef;
621 $sum += $items[$itemid]->aggregationcoef * $grade_value;
623 if ($weightsum == 0) {
624 $agg_grade = null;
625 } else {
626 $agg_grade = $sum / $weightsum;
628 break;
630 case GRADE_AGGREGATE_WEIGHTED_MEAN2: // Weighted average of all existing final grades, weight is the range of grade (ususally grademax)
631 $weightsum = 0;
632 $sum = 0;
633 foreach($grade_values as $itemid=>$grade_value) {
634 $weight = $items[$itemid]->grademax - $items[$itemid]->grademin;
635 if ($weight <= 0) {
636 continue;
638 $weightsum += $weight;
639 $sum += $weight * $grade_value;
641 if ($weightsum == 0) {
642 $agg_grade = null;
643 } else {
644 $agg_grade = $sum / $weightsum;
646 break;
648 case GRADE_AGGREGATE_EXTRACREDIT_MEAN: // special average
649 $num = 0;
650 $sum = 0;
651 foreach($grade_values as $itemid=>$grade_value) {
652 if ($items[$itemid]->aggregationcoef == 0) {
653 $num += 1;
654 $sum += $grade_value;
655 } else if ($items[$itemid]->aggregationcoef > 0) {
656 $sum += $items[$itemid]->aggregationcoef * $grade_value;
659 if ($num == 0) {
660 $agg_grade = $sum; // only extra credits or wrong coefs
661 } else {
662 $agg_grade = $sum / $num;
664 break;
666 case GRADE_AGGREGATE_MEAN: // Arithmetic average of all grade items (if ungraded aggregated, NULL counted as minimum)
667 default:
668 $num = count($grade_values);
669 $sum = array_sum($grade_values);
670 $agg_grade = $sum / $num;
671 break;
674 return $agg_grade;
678 * internal function for category grades summing
680 * @param object $grade
681 * @param int $userid
682 * @param float $oldfinalgrade
683 * @param array $items
684 * @param array $grade_values
685 * @param bool $excluded
686 * @return boolean (just plain return;)
688 function sum_grades(&$grade, $oldfinalgrade, $items, $grade_values, $excluded) {
689 // ungraded and exluded items are not used in aggregation
690 foreach ($grade_values as $itemid=>$v) {
691 if (is_null($v)) {
692 unset($grade_values[$itemid]);
693 } else if (in_array($itemid, $excluded)) {
694 unset($grade_values[$itemid]);
698 // use 0 if grade missing, droplow used and aggregating all items
699 if (!$this->aggregateonlygraded and !empty($this->droplow)) {
700 foreach($items as $itemid=>$value) {
701 if (!isset($grade_values[$itemid]) and !in_array($itemid, $excluded)) {
702 $grade_values[$itemid] = 0;
707 $max = 0;
709 //find max grade
710 foreach ($items as $item) {
711 if ($item->aggregationcoef > 0) {
712 // extra credit from this activity - does not affect total
713 continue;
715 if ($item->gradetype == GRADE_TYPE_VALUE) {
716 $max += $item->grademax;
717 } else if ($item->gradetype == GRADE_TYPE_SCALE) {
718 $max += $item->grademax - 1; // scales min is 1
722 if ($this->grade_item->grademax != $max or $this->grade_item->grademin != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE){
723 $this->grade_item->grademax = $max;
724 $this->grade_item->grademin = 0;
725 $this->grade_item->gradetype = GRADE_TYPE_VALUE;
726 $this->grade_item->update('aggregation');
729 $this->apply_limit_rules($grade_values);
731 $sum = array_sum($grade_values);
732 $grade->finalgrade = bounded_number($this->grade_item->grademin, $sum, $this->grade_item->grademax);
734 // update in db if changed
735 if (grade_floats_different($grade->finalgrade, $oldfinalgrade)) {
736 $grade->update('aggregation');
739 return;
743 * Given an array of grade values (numerical indices), applies droplow or keephigh
744 * rules to limit the final array.
745 * @param array $grade_values
746 * @return array Limited grades.
748 function apply_limit_rules(&$grade_values) {
749 arsort($grade_values, SORT_NUMERIC);
750 if (!empty($this->droplow)) {
751 for ($i = 0; $i < $this->droplow; $i++) {
752 array_pop($grade_values);
754 } elseif (!empty($this->keephigh)) {
755 while (count($grade_values) > $this->keephigh) {
756 array_pop($grade_values);
763 * Returns true if category uses special aggregation coeficient
764 * @return boolean true if coeficient used
766 function is_aggregationcoef_used() {
767 return ($this->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN
768 or $this->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN
769 or $this->aggregation == GRADE_AGGREGATE_SUM);
774 * Returns tree with all grade_items and categories as elements
775 * @static
776 * @param int $courseid
777 * @param boolean $include_category_items as category children
778 * @return array
780 function fetch_course_tree($courseid, $include_category_items=false) {
781 $course_category = grade_category::fetch_course_category($courseid);
782 $category_array = array('object'=>$course_category, 'type'=>'category', 'depth'=>1,
783 'children'=>$course_category->get_children($include_category_items));
784 $sortorder = 1;
785 $course_category->set_sortorder($sortorder);
786 $course_category->sortorder = $sortorder;
787 return grade_category::_fetch_course_tree_recursion($category_array, $sortorder);
790 function _fetch_course_tree_recursion($category_array, &$sortorder) {
791 // update the sortorder in db if needed
792 if ($category_array['object']->sortorder != $sortorder) {
793 $category_array['object']->set_sortorder($sortorder);
796 // store the grade_item or grade_category instance with extra info
797 $result = array('object'=>$category_array['object'], 'type'=>$category_array['type'], 'depth'=>$category_array['depth']);
799 // reuse final grades if there
800 if (array_key_exists('finalgrades', $category_array)) {
801 $result['finalgrades'] = $category_array['finalgrades'];
804 // recursively resort children
805 if (!empty($category_array['children'])) {
806 $result['children'] = array();
807 //process the category item first
808 $cat_item_id = null;
809 foreach($category_array['children'] as $oldorder=>$child_array) {
810 if ($child_array['type'] == 'courseitem' or $child_array['type'] == 'categoryitem') {
811 $result['children'][$sortorder] = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
814 foreach($category_array['children'] as $oldorder=>$child_array) {
815 if ($child_array['type'] != 'courseitem' and $child_array['type'] != 'categoryitem') {
816 $result['children'][++$sortorder] = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
821 return $result;
825 * Fetches and returns all the children categories and/or grade_items belonging to this category.
826 * By default only returns the immediate children (depth=1), but deeper levels can be requested,
827 * as well as all levels (0). The elements are indexed by sort order.
828 * @return array Array of child objects (grade_category and grade_item).
830 function get_children($include_category_items=false) {
832 // This function must be as fast as possible ;-)
833 // fetch all course grade items and categories into memory - we do not expect hundreds of these in course
834 // we have to limit the number of queries though, because it will be used often in grade reports
836 $cats = get_records('grade_categories', 'courseid', $this->courseid);
837 $items = get_records('grade_items', 'courseid', $this->courseid);
839 // init children array first
840 foreach ($cats as $catid=>$cat) {
841 $cats[$catid]->children = array();
844 //first attach items to cats and add category sortorder
845 foreach ($items as $item) {
846 if ($item->itemtype == 'course' or $item->itemtype == 'category') {
847 $cats[$item->iteminstance]->sortorder = $item->sortorder;
849 if (!$include_category_items) {
850 continue;
852 $categoryid = $item->iteminstance;
853 } else {
854 $categoryid = $item->categoryid;
857 // prevent problems with duplicate sortorders in db
858 $sortorder = $item->sortorder;
859 while(array_key_exists($sortorder, $cats[$categoryid]->children)) {
860 //debugging("$sortorder exists in item loop");
861 $sortorder++;
864 $cats[$categoryid]->children[$sortorder] = $item;
868 // now find the requested category and connect categories as children
869 $category = false;
870 foreach ($cats as $catid=>$cat) {
871 if (empty($cat->parent)) {
872 if ($cat->path !== '/'.$cat->id.'/') {
873 $grade_category = new grade_category($cat, false);
874 $grade_category->path = '/'.$cat->id.'/';
875 $grade_category->depth = 1;
876 $grade_category->update('system');
877 return $this->get_children($include_category_items);
879 } else {
880 if (empty($cat->path) or !preg_match('|/'.$cat->parent.'/'.$cat->id.'/$|', $cat->path)) {
881 //fix paths and depts
882 static $recursioncounter = 0; // prevents infinite recursion
883 $recursioncounter++;
884 if ($recursioncounter < 5) {
885 // fix paths and depths!
886 $grade_category = new grade_category($cat, false);
887 $grade_category->depth = 0;
888 $grade_category->path = null;
889 $grade_category->update('system');
890 return $this->get_children($include_category_items);
893 // prevent problems with duplicate sortorders in db
894 $sortorder = $cat->sortorder;
895 while(array_key_exists($sortorder, $cats[$cat->parent]->children)) {
896 //debugging("$sortorder exists in cat loop");
897 $sortorder++;
900 $cats[$cat->parent]->children[$sortorder] = &$cats[$catid];
903 if ($catid == $this->id) {
904 $category = &$cats[$catid];
908 unset($items); // not needed
909 unset($cats); // not needed
911 $children_array = grade_category::_get_children_recursion($category);
913 ksort($children_array);
915 return $children_array;
919 function _get_children_recursion($category) {
921 $children_array = array();
922 foreach($category->children as $sortorder=>$child) {
923 if (array_key_exists('itemtype', $child)) {
924 $grade_item = new grade_item($child, false);
925 if (in_array($grade_item->itemtype, array('course', 'category'))) {
926 $type = $grade_item->itemtype.'item';
927 $depth = $category->depth;
928 } else {
929 $type = 'item';
930 $depth = $category->depth; // we use this to set the same colour
932 $children_array[$sortorder] = array('object'=>$grade_item, 'type'=>$type, 'depth'=>$depth);
934 } else {
935 $children = grade_category::_get_children_recursion($child);
936 $grade_category = new grade_category($child, false);
937 if (empty($children)) {
938 $children = array();
940 $children_array[$sortorder] = array('object'=>$grade_category, 'type'=>'category', 'depth'=>$grade_category->depth, 'children'=>$children);
944 // sort the array
945 ksort($children_array);
947 return $children_array;
951 * Uses get_grade_item to load or create a grade_item, then saves it as $this->grade_item.
952 * @return object Grade_item
954 function load_grade_item() {
955 if (empty($this->grade_item)) {
956 $this->grade_item = $this->get_grade_item();
958 return $this->grade_item;
962 * Retrieves from DB and instantiates the associated grade_item object.
963 * If no grade_item exists yet, create one.
964 * @return object Grade_item
966 function get_grade_item() {
967 if (empty($this->id)) {
968 debugging("Attempt to obtain a grade_category's associated grade_item without the category's ID being set.");
969 return false;
972 if (empty($this->parent)) {
973 $params = array('courseid'=>$this->courseid, 'itemtype'=>'course', 'iteminstance'=>$this->id);
975 } else {
976 $params = array('courseid'=>$this->courseid, 'itemtype'=>'category', 'iteminstance'=>$this->id);
979 if (!$grade_items = grade_item::fetch_all($params)) {
980 // create a new one
981 $grade_item = new grade_item($params, false);
982 $grade_item->gradetype = GRADE_TYPE_VALUE;
983 $grade_item->insert('system');
985 } else if (count($grade_items) == 1){
986 // found existing one
987 $grade_item = reset($grade_items);
989 } else {
990 debugging("Found more than one grade_item attached to category id:".$this->id);
991 // return first one
992 $grade_item = reset($grade_items);
995 return $grade_item;
999 * Uses $this->parent to instantiate $this->parent_category based on the
1000 * referenced record in the DB.
1001 * @return object Parent_category
1003 function load_parent_category() {
1004 if (empty($this->parent_category) && !empty($this->parent)) {
1005 $this->parent_category = $this->get_parent_category();
1007 return $this->parent_category;
1011 * Uses $this->parent to instantiate and return a grade_category object.
1012 * @return object Parent_category
1014 function get_parent_category() {
1015 if (!empty($this->parent)) {
1016 $parent_category = new grade_category(array('id' => $this->parent));
1017 return $parent_category;
1018 } else {
1019 return null;
1024 * Returns the most descriptive field for this object. This is a standard method used
1025 * when we do not know the exact type of an object.
1026 * @return string name
1028 function get_name() {
1029 // For a course category, we return the course name if the fullname is set to '?' in the DB (empty in the category edit form)
1030 if (empty($this->parent) && $this->fullname == '?') {
1031 $course = get_record('course', 'id', $this->courseid);
1032 return format_string($course->fullname);
1033 } else {
1034 return $this->fullname;
1039 * Sets this category's parent id. A generic method shared by objects that have a parent id of some kind.
1040 * @param int parentid
1041 * @return boolean success
1043 function set_parent($parentid, $source=null) {
1044 if ($this->parent == $parentid) {
1045 return true;
1048 if ($parentid == $this->id) {
1049 error('Can not assign self as parent!');
1052 if (empty($this->parent) and $this->is_course_category()) {
1053 error('Course category can not have parent!');
1056 // find parent and check course id
1057 if (!$parent_category = grade_category::fetch(array('id'=>$parentid, 'courseid'=>$this->courseid))) {
1058 return false;
1061 $this->force_regrading();
1063 // set new parent category
1064 $this->parent = $parent_category->id;
1065 $this->parent_category =& $parent_category;
1066 $this->path = null; // remove old path and depth - will be recalculated in update()
1067 $this->depth = 0; // remove old path and depth - will be recalculated in update()
1068 $this->update($source);
1070 return $this->update($source);
1074 * Returns the final values for this grade category.
1075 * @param int $userid Optional: to retrieve a single final grade
1076 * @return mixed An array of all final_grades (stdClass objects) for this grade_item, or a single final_grade.
1078 function get_final($userid=NULL) {
1079 $this->load_grade_item();
1080 return $this->grade_item->get_final($userid);
1084 * Returns the sortorder of the associated grade_item. This method is also available in
1085 * grade_item, for cases where the object type is not known.
1086 * @return int Sort order
1088 function get_sortorder() {
1089 $this->load_grade_item();
1090 return $this->grade_item->get_sortorder();
1094 * Returns the idnumber of the associated grade_item. This method is also available in
1095 * grade_item, for cases where the object type is not known.
1096 * @return string idnumber
1098 function get_idnumber() {
1099 $this->load_grade_item();
1100 return $this->grade_item->get_idnumber();
1104 * Sets sortorder variable for this category.
1105 * This method is also available in grade_item, for cases where the object type is not know.
1106 * @param int $sortorder
1107 * @return void
1109 function set_sortorder($sortorder) {
1110 $this->load_grade_item();
1111 $this->grade_item->set_sortorder($sortorder);
1115 * Move this category after the given sortorder - does not change the parent
1116 * @param int $sortorder to place after
1118 function move_after_sortorder($sortorder) {
1119 $this->load_grade_item();
1120 $this->grade_item->move_after_sortorder($sortorder);
1124 * Return true if this is the top most category that represents the total course grade.
1125 * @return boolean
1127 function is_course_category() {
1128 $this->load_grade_item();
1129 return $this->grade_item->is_course_item();
1133 * Return the top most course category.
1134 * @static
1135 * @return object grade_category instance for course grade
1137 function fetch_course_category($courseid) {
1138 if (empty($courseid)) {
1139 debugging('Missing course id!');
1140 return false;
1143 // course category has no parent
1144 if ($course_category = grade_category::fetch(array('courseid'=>$courseid, 'parent'=>null))) {
1145 return $course_category;
1148 // create a new one
1149 $course_category = new grade_category();
1150 $course_category->insert_course_category($courseid);
1152 return $course_category;
1156 * Is grading object editable?
1157 * @return boolean
1159 function is_editable() {
1160 return true;
1164 * Returns the locked state/date of the associated grade_item. This method is also available in
1165 * grade_item, for cases where the object type is not known.
1166 * @return boolean
1168 function is_locked() {
1169 $this->load_grade_item();
1170 return $this->grade_item->is_locked();
1174 * Sets the grade_item's locked variable and updates the grade_item.
1175 * Method named after grade_item::set_locked().
1176 * @param int $locked 0, 1 or a timestamp int(10) after which date the item will be locked.
1177 * @param boolean $cascade lock/unlock child objects too
1178 * @param boolean $refresh refresh grades when unlocking
1179 * @return boolean success if category locked (not all children mayb be locked though)
1181 function set_locked($lockedstate, $cascade=false, $refresh=true) {
1182 $this->load_grade_item();
1184 $result = $this->grade_item->set_locked($lockedstate, $cascade, true);
1186 if ($cascade) {
1187 //process all children - items and categories
1188 if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
1189 foreach($children as $child) {
1190 $child->set_locked($lockedstate, true, false);
1191 if (empty($lockedstate) and $refresh) {
1192 //refresh when unlocking
1193 $child->refresh_grades();
1197 if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
1198 foreach($children as $child) {
1199 $child->set_locked($lockedstate, true, true);
1204 return $result;
1208 * Returns the hidden state/date of the associated grade_item. This method is also available in
1209 * grade_item.
1210 * @return boolean
1212 function is_hidden() {
1213 $this->load_grade_item();
1214 return $this->grade_item->is_hidden();
1218 * Check grade hidden status. Uses data from both grade item and grade.
1219 * @return boolean true if hiddenuntil, false if not
1221 function is_hiddenuntil() {
1222 $this->load_grade_item();
1223 return $this->grade_item->is_hiddenuntil();
1227 * Sets the grade_item's hidden variable and updates the grade_item.
1228 * Method named after grade_item::set_hidden().
1229 * @param int $hidden 0, 1 or a timestamp int(10) after which date the item will be hidden.
1230 * @param boolean $cascade apply to child objects too
1231 * @return void
1233 function set_hidden($hidden, $cascade=false) {
1234 $this->load_grade_item();
1235 $this->grade_item->set_hidden($hidden);
1236 if ($cascade) {
1237 if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
1238 foreach($children as $child) {
1239 $child->set_hidden($hidden, $cascade);
1242 if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
1243 foreach($children as $child) {
1244 $child->set_hidden($hidden, $cascade);
1251 * Applies default settings on this category
1252 * @return bool true if anything changed
1254 function apply_default_settings() {
1255 global $CFG;
1257 foreach ($this->forceable as $property) {
1258 if (isset($CFG->{"grade_$property"})) {
1259 if ($CFG->{"grade_$property"} == -1) {
1260 continue; //temporary bc before version bump
1262 $this->$property = $CFG->{"grade_$property"};
1268 * Applies forced settings on this category
1269 * @return bool true if anything changed
1271 function apply_forced_settings() {
1272 global $CFG;
1274 $updated = false;
1275 foreach ($this->forceable as $property) {
1276 if (isset($CFG->{"grade_$property"}) and isset($CFG->{"grade_{$property}_flag"}) and ((int)$CFG->{"grade_{$property}_flag"} & 1)) {
1277 if ($CFG->{"grade_$property"} == -1) {
1278 continue; //temporary bc before version bump
1280 $this->$property = $CFG->{"grade_$property"};
1281 $updated = true;
1285 return $updated;
1289 * Notification of change in forced category settings.
1290 * @static
1292 function updated_forced_settings() {
1293 global $CFG;
1294 $sql = "UPDATE {$CFG->prefix}grade_items SET needsupdate=1 WHERE itemtype='course' or itemtype='category'";
1295 execute_sql($sql, false);