3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
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. //
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: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once('grade_object.php');
28 class grade_category
extends grade_object
{
33 var $table = 'grade_categories';
36 * Array of class variables that are not part of the DB table fields
37 * @var array $nonfields
39 var $nonfields = array('table', 'required_fields', 'nonfields', 'children', 'all_children', 'grade_item', 'parent_category', 'sortorder');
42 * The course this category belongs to.
48 * The category this category belongs to (optional).
54 * The grade_category object referenced by $this->parent (PK).
55 * @var object $parent_category
60 * The number of parents this category has.
66 * Shows the hierarchical path for this category as /1/2/3 (like course_categories), the last number being
67 * this category's autoincrement ID number.
73 * The name of this category.
74 * @var string $fullname
79 * A constant pointing to one of the predefined aggregation strategies (none, mean, median, sum etc) .
80 * @var int $aggregation
82 var $aggregation = GRADE_AGGREGATE_MEAN_ALL
;
85 * Keep only the X highest items.
91 * Drop the X lowest items.
97 * Array of grade_items or grade_categories nested exactly 1 level below this category
98 * @var array $children
103 * A hierarchical array of all children below this category. This is stored separately from
104 * $children because it is more memory-intensive and may not be used as often.
105 * @var array $all_children
110 * An associated grade_item object, with itemtype=category, used to calculate and cache a set of grade values
112 * @var object $grade_item
117 * Temporary sortorder for speedup of children resorting
122 * Builds this category's path string based on its parents (if any) and its own id number.
123 * This is typically done just before inserting this object in the DB for the first time,
124 * or when a new parent is added or changed. It is a recursive function: once the calling
125 * object no longer has a parent, the path is complete.
128 * @param object $grade_category
129 * @return int The depth of this category (2 means there is one parent)
131 function build_path($grade_category) {
132 if (empty($grade_category->parent
)) {
133 return '/'.$grade_category->id
;
135 $parent = get_record('grade_categories', 'id', $grade_category->parent
);
136 return grade_category
::build_path($parent).'/'.$grade_category->id
;
141 * Finds and returns a grade_category instance based on params.
144 * @param array $params associative arrays varname=>value
145 * @return object grade_category instance or false if none found.
147 function fetch($params) {
148 return grade_object
::fetch_helper('grade_categories', 'grade_category', $params);
152 * Finds and returns all grade_category instances based on params.
155 * @param array $params associative arrays varname=>value
156 * @return array array of grade_category insatnces or false if none found.
158 function fetch_all($params) {
159 return grade_object
::fetch_all_helper('grade_categories', 'grade_category', $params);
163 * In addition to update() as defined in grade_object, call force_regrading of parent categories, if applicable.
164 * @param string $source from where was the object updated (mod/forum, manual, etc.)
165 * @return boolean success
167 function update($source=null) {
168 // load the grade item or create a new one
169 $this->load_grade_item();
171 // force recalculation of path;
172 if (empty($this->path
)) {
173 $this->path
= grade_category
::build_path($this);
174 $this->depth
= substr_count($this->path
, '/');
178 // Recalculate grades if needed
179 if ($this->qualifies_for_regrading()) {
180 $this->force_regrading();
183 return parent
::update($source);
187 * If parent::delete() is successful, send force_regrading message to parent category.
188 * @param string $source from where was the object deleted (mod/forum, manual, etc.)
189 * @return boolean success
191 function delete($source=null) {
192 if ($this->is_course_category()) {
193 debuggin('Can not delete top course category!');
197 $this->force_regrading();
199 $grade_item = $this->load_grade_item();
200 $parent = $this->load_parent_category();
202 // Update children's categoryid/parent field first
203 if ($children = grade_item
::fetch_all(array('categoryid'=>$this->id
))) {
204 foreach ($children as $child) {
205 $child->set_parent($parent->id
);
208 if ($children = grade_category
::fetch_all(array('parent'=>$this->id
))) {
209 foreach ($children as $child) {
210 $child->set_parent($parent->id
);
214 // first delete the attached grade item and grades
215 $grade_item->delete($source);
217 // delete category itself
218 return parent
::delete($source);
222 * In addition to the normal insert() defined in grade_object, this method sets the depth
223 * and path for this object, and update the record accordingly. The reason why this must
224 * be done here instead of in the constructor, is that they both need to know the record's
225 * id number, which only gets created at insertion time.
226 * This method also creates an associated grade_item if this wasn't done during construction.
227 * @param string $source from where was the object inserted (mod/forum, manual, etc.)
228 * @return int PK ID if successful, false otherwise
230 function insert($source=null) {
232 if (empty($this->courseid
)) {
233 error('Can not insert grade category without course id!');
236 if (empty($this->parent
)) {
237 $course_category = grade_category
::fetch_course_category($this->courseid
);
238 $this->parent
= $course_category->id
;
243 if (!parent
::insert($source)) {
244 debugging("Could not insert this category: " . print_r($this, true));
248 $this->force_regrading();
250 // build path and depth
251 $this->update($source);
256 function insert_course_category($courseid) {
257 $this->courseid
= $courseid;
258 $this->fullname
= 'course grade category';
260 $this->parent
= null;
261 $this->aggregate
= GRADE_AGGREGATE_MEAN_ALL
;
263 if (!parent
::insert('system')) {
264 debugging("Could not insert this category: " . print_r($this, true));
268 // build path and depth
269 $this->update('system');
275 * Compares the values held by this object with those of the matching record in DB, and returns
276 * whether or not these differences are sufficient to justify an update of all parent objects.
277 * This assumes that this object has an id number and a matching record in DB. If not, it will return false.
280 function qualifies_for_regrading() {
281 if (empty($this->id
)) {
282 debugging("Can not regrade non existing category");
286 $db_item = grade_category
::fetch(array('id'=>$this->id
));
288 $aggregationdiff = $db_item->aggregation
!= $this->aggregation
;
289 $keephighdiff = $db_item->keephigh
!= $this->keephigh
;
290 $droplowdiff = $db_item->droplow
!= $this->droplow
;
292 return ($aggregationdiff ||
$keephighdiff ||
$droplowdiff);
296 * Marks the category and course item as needing update - categories are always regraded.
299 function force_regrading() {
300 $grade_item = $this->load_grade_item();
301 $grade_item->force_regrading();
305 * Generates and saves raw_grades in associated category grade item.
306 * These immediate children must alrady have their own final grades.
307 * The category's aggregation method is used to generate raw grades.
309 * Please note that category grade is either calculated or aggregated - not both at the same time.
311 * This method must be used ONLY from grade_item::regrade_final_grades(),
312 * because the calculation must be done in correct order!
315 * 1. Get final grades from immediate children
316 * 3. Aggregate these grades
317 * 4. Save them in raw grades of associated category grade item
319 function generate_grades($userid=null) {
322 $this->load_grade_item();
324 if ($this->grade_item
->is_locked()) {
325 return true; // no need to recalculate locked items
328 $this->grade_item
->load_scale();
330 // find grade items of immediate children (category or grade items)
331 $depends_on = $this->grade_item
->depends_on();
333 if (empty($depends_on)) {
336 $gis = implode(',', $depends_on);
338 FROM {$CFG->prefix}grade_items
340 $items = get_records_sql($sql);
344 $usersql = "AND g.userid=$userid";
349 // where to look for final grades - include grade of this item too, we will store the results there
350 $gis = implode(',', array_merge($depends_on, array($this->grade_item
->id
)));
352 FROM {$CFG->prefix}grade_grades g, {$CFG->prefix}grade_items gi
353 WHERE gi.id = g.itemid AND gi.id IN ($gis) $usersql
356 // group the results by userid and aggregate the grades for this user
357 if ($rs = get_recordset_sql($sql)) {
358 if ($rs->RecordCount() > 0) {
360 $grade_values = array();
363 while ($used = rs_fetch_next_record($rs)) {
364 if ($used->userid
!= $prevuser) {
365 $this->aggregate_grades($prevuser, $items, $grade_values, $oldgrade, $excluded);
366 $prevuser = $used->userid
;
367 $grade_values = array();
371 $grade_values[$used->itemid
] = $used->finalgrade
;
372 if ($used->excluded
) {
373 $excluded[] = $used->itemid
;
375 if ($this->grade_item
->id
== $used->itemid
) {
379 $this->aggregate_grades($prevuser, $items, $grade_values, $oldgrade, $excluded);//the last one
388 * internal function for category grades aggregation
390 function aggregate_grades($userid, $items, $grade_values, $oldgrade, $excluded) {
391 if (empty($userid)) {
397 $grade = new grade_grade($oldgrade, false);
398 $grade->grade_item
=& $this->grade_item
;
401 // insert final grade - it will be needed later anyway
402 $grade = new grade_grade(array('itemid'=>$this->grade_item
->id
, 'userid'=>$userid), false);
403 $grade->insert('system');
404 $grade->grade_item
=& $this->grade_item
;
406 $oldgrade = new object();
407 $oldgrade->finalgrade
= $grade->finalgrade
;
408 $oldgrade->rawgrade
= $grade->rawgrade
;
409 $oldgrade->rawgrademin
= $grade->rawgrademin
;
410 $oldgrade->rawgrademax
= $grade->rawgrademax
;
411 $oldgrade->rawscaleid
= $grade->rawscaleid
;
414 // no need to recalculate locked or overridden grades
415 if ($grade->is_locked() or $grade->is_overridden()) {
419 // can not use own final category grade in calculation
420 unset($grade_values[$this->grade_item
->id
]);
422 // if no grades calculation possible or grading not allowed clear both final and raw
423 if (empty($grade_values) or empty($items) or ($this->grade_item
->gradetype
!= GRADE_TYPE_VALUE
and $this->grade_item
->gradetype
!= GRADE_TYPE_SCALE
)) {
424 $grade->finalgrade
= null;
425 $grade->rawgrade
= null;
426 if ($grade->finalgrade
!== $oldgrade->finalgrade
or $grade->rawgrade
!== $oldgrade->rawgrade
) {
427 $grade->update('system');
432 /// normalize the grades first - all will have value 0...1
433 // ungraded items are not used in aggregation
434 foreach ($grade_values as $itemid=>$v) {
436 // null means no grade
437 unset($grade_values[$itemid]);
439 } else if (in_array($itemid, $excluded)) {
440 unset($grade_values[$itemid]);
444 $grade_values[$itemid] = grade_grade
::standardise_score($v, $items[$itemid]->grademin
, $items[$itemid]->grademax
, 0, 1);
447 // use min grade if grade missing for these types
448 switch ($this->aggregation
) {
449 case GRADE_AGGREGATE_MEAN_ALL
:
450 case GRADE_AGGREGATE_MEDIAN_ALL
:
451 case GRADE_AGGREGATE_MIN_ALL
:
452 case GRADE_AGGREGATE_MAX_ALL
:
453 case GRADE_AGGREGATE_MODE_ALL
:
454 case GRADE_AGGREGATE_WEIGHTED_MEAN_ALL
:
455 case GRADE_AGGREGATE_EXTRACREDIT_MEAN_ALL
:
456 foreach($items as $itemid=>$value) {
457 if (!isset($grade_values[$itemid]) and !in_array($itemid, $excluded)) {
458 $grade_values[$itemid] = 0;
465 $this->apply_limit_rules($grade_values);
466 asort($grade_values, SORT_NUMERIC
);
468 // let's see we have still enough grades to do any statistics
469 if (count($grade_values) == 0) {
470 // not enough attempts yet
471 $grade->finalgrade
= null;
472 $grade->rawgrade
= null;
473 if ($grade->finalgrade
!== $oldgrade->finalgrade
or $grade->rawgrade
!== $oldgrade->rawgrade
) {
474 $grade->update('system');
479 /// start the aggregation
480 switch ($this->aggregation
) {
481 case GRADE_AGGREGATE_MEDIAN_ALL
: // Middle point value in the set: ignores frequencies
482 case GRADE_AGGREGATE_MEDIAN_GRADED
:
483 $num = count($grade_values);
484 $grades = array_values($grade_values);
486 $rawgrade = ($grades[intval($num/2)-1] +
$grades[intval($num/2)]) / 2;
488 $rawgrade = $grades[intval(($num/2)-0.5)];
492 case GRADE_AGGREGATE_MIN_ALL
:
493 case GRADE_AGGREGATE_MIN_GRADED
:
494 $rawgrade = reset($grade_values);
497 case GRADE_AGGREGATE_MAX_ALL
:
498 case GRADE_AGGREGATE_MAX_GRADED
:
499 $rawgrade = array_pop($grade_values);
502 case GRADE_AGGREGATE_MODE_ALL
: // the most common value, average used if multimode
503 case GRADE_AGGREGATE_MODE_GRADED
:
504 $freq = array_count_values($grade_values);
505 arsort($freq); // sort by frequency keeping keys
506 $top = reset($freq); // highest frequency count
507 $modes = array_keys($freq, $top); // search for all modes (have the same highest count)
508 rsort($modes, SORT_NUMERIC
); // get highes mode
509 $rawgrade = reset($modes);
512 case GRADE_AGGREGATE_WEIGHTED_MEAN_GRADED
: // Weighted average of all existing final grades
513 case GRADE_AGGREGATE_WEIGHTED_MEAN_ALL
:
516 foreach($grade_values as $itemid=>$grade_value) {
517 if ($items[$itemid]->aggregationcoef
<= 0) {
520 $weightsum +
= $items[$itemid]->aggregationcoef
;
521 $sum +
= $items[$itemid]->aggregationcoef
* $grade_value;
523 if ($weightsum == 0) {
526 $rawgrade = $sum / $weightsum;
530 case GRADE_AGGREGATE_EXTRACREDIT_MEAN_ALL
: // special average
531 case GRADE_AGGREGATE_EXTRACREDIT_MEAN_GRADED
:
534 foreach($grade_values as $itemid=>$grade_value) {
535 if ($items[$itemid]->aggregationcoef
== 0) {
537 $sum +
= $grade_value;
538 } else if ($items[$itemid]->aggregationcoef
> 0) {
539 $sum +
= $items[$itemid]->aggregationcoef
* $grade_value;
543 $rawgrade = $sum; // only extra credits or wrong coefs
545 $rawgrade = $sum / $num;
549 case GRADE_AGGREGATE_MEAN_ALL
: // Arithmetic average of all grade items including even NULLs; NULL grade counted as minimum
550 case GRADE_AGGREGATE_MEAN_GRADED
: // Arithmetic average of all final grades, unfinished are not calculated
552 $num = count($grade_values);
553 $sum = array_sum($grade_values);
554 $rawgrade = $sum / $num;
558 /// prepare update of new raw grade
559 $grade->rawgrademin
= $this->grade_item
->grademin
;
560 $grade->rawgrademax
= $this->grade_item
->grademax
;
561 $grade->rawscaleid
= $this->grade_item
->scaleid
;
563 // recalculate the rawgrade back to requested range
564 $grade->rawgrade
= grade_grade
::standardise_score($rawgrade, 0, 1, $grade->rawgrademin
, $grade->rawgrademax
);
566 // calculate final grade
567 $grade->finalgrade
= $this->grade_item
->adjust_grade($grade->rawgrade
, $grade->rawgrademin
, $grade->rawgrademax
);
569 // update in db if changed
570 if ( $grade->finalgrade
!== $oldgrade->finalgrade
571 or $grade->rawgrade
!== $oldgrade->rawgrade
572 or $grade->rawgrademin
!== $oldgrade->rawgrademin
573 or $grade->rawgrademax
!== $oldgrade->rawgrademax
574 or $grade->rawscaleid
!== $oldgrade->rawscaleid
) {
576 $grade->update('system');
583 * Given an array of grade values (numerical indices), applies droplow or keephigh
584 * rules to limit the final array.
585 * @param array $grade_values
586 * @return array Limited grades.
588 function apply_limit_rules(&$grade_values) {
589 arsort($grade_values, SORT_NUMERIC
);
590 if (!empty($this->droplow
)) {
591 for ($i = 0; $i < $this->droplow
; $i++
) {
592 array_pop($grade_values);
594 } elseif (!empty($this->keephigh
)) {
595 while (count($grade_values) > $this->keephigh
) {
596 array_pop($grade_values);
603 * Returns true if category uses special aggregation coeficient
604 * @return boolean true if coeficient used
606 function is_aggregationcoef_used() {
607 return ($this->aggregation
== GRADE_AGGREGATE_WEIGHTED_MEAN_ALL
608 or $this->aggregation
== GRADE_AGGREGATE_WEIGHTED_MEAN_GRADED
609 or $this->aggregation
== GRADE_AGGREGATE_EXTRACREDIT_MEAN_ALL
610 or $this->aggregation
== GRADE_AGGREGATE_EXTRACREDIT_MEAN_GRADED
);
615 * Returns true if this category has any child grade_category or grade_item.
616 * @return int number of direct children, or false if none found.
618 function has_children() {
619 return count_records('grade_categories', 'parent', $this->id
) +
count_records('grade_items', 'categoryid', $this->id
);
623 * Returns tree with all grade_items and categories as elements
625 * @param int $courseid
626 * @param boolean $include_category_items as category children
629 function fetch_course_tree($courseid, $include_category_items=false) {
630 $course_category = grade_category
::fetch_course_category($courseid);
631 $category_array = array('object'=>$course_category, 'type'=>'category', 'depth'=>1,
632 'children'=>$course_category->get_children($include_category_items));
634 $course_category->set_sortorder($sortorder);
635 $course_category->sortorder
= $sortorder;
636 return grade_category
::_fetch_course_tree_recursion($category_array, $sortorder);
639 function _fetch_course_tree_recursion($category_array, &$sortorder) {
640 // update the sortorder in db if needed
641 if ($category_array['object']->sortorder
!= $sortorder) {
642 $category_array['object']->set_sortorder($sortorder);
645 // store the grade_item or grade_category instance with extra info
646 $result = array('object'=>$category_array['object'], 'type'=>$category_array['type'], 'depth'=>$category_array['depth']);
648 // reuse final grades if there
649 if (array_key_exists('finalgrades', $category_array)) {
650 $result['finalgrades'] = $category_array['finalgrades'];
653 // recursively resort children
654 if (!empty($category_array['children'])) {
655 $result['children'] = array();
656 foreach($category_array['children'] as $oldorder=>$child_array) {
657 if ($child_array['type'] == 'courseitem' or $child_array['type'] == 'categoryitem') {
658 $result['children'][$sortorder] = grade_category
::_fetch_course_tree_recursion($child_array, $sortorder);
660 $result['children'][++
$sortorder] = grade_category
::_fetch_course_tree_recursion($child_array, $sortorder);
669 * Fetches and returns all the children categories and/or grade_items belonging to this category.
670 * By default only returns the immediate children (depth=1), but deeper levels can be requested,
671 * as well as all levels (0). The elements are indexed by sort order.
672 * @return array Array of child objects (grade_category and grade_item).
674 function get_children($include_category_items=false) {
676 // This function must be as fast as possible ;-)
677 // fetch all course grade items and categories into memory - we do not expect hundreds of these in course
678 // we have to limit the number of queries though, because it will be used often in grade reports
680 $cats = get_records('grade_categories', 'courseid', $this->courseid
);
681 $items = get_records('grade_items', 'courseid', $this->courseid
);
683 // init children array first
684 foreach ($cats as $catid=>$cat) {
685 $cats[$catid]->children
= array();
688 //first attach items to cats and add category sortorder
689 foreach ($items as $item) {
690 if ($item->itemtype
== 'course' or $item->itemtype
== 'category') {
691 $cats[$item->iteminstance
]->sortorder
= $item->sortorder
;
693 if (!$include_category_items) {
696 $categoryid = $item->iteminstance
;
698 $categoryid = $item->categoryid
;
701 // prevent problems with duplicate sortorders in db
702 $sortorder = $item->sortorder
;
703 while(array_key_exists($sortorder, $cats[$categoryid]->children
)) {
704 //debugging("$sortorder exists in item loop");
708 $cats[$categoryid]->children
[$sortorder] = $item;
712 // now find the requested category and connect categories as children
714 foreach ($cats as $catid=>$cat) {
715 if (!empty($cat->parent
)) {
716 // prevent problems with duplicate sortorders in db
717 $sortorder = $cat->sortorder
;
718 while(array_key_exists($sortorder, $cats[$cat->parent
]->children
)) {
719 //debugging("$sortorder exists in cat loop");
723 $cats[$cat->parent
]->children
[$sortorder] = $cat;
726 if ($catid == $this->id
) {
727 $category = &$cats[$catid];
731 unset($items); // not needed
732 unset($cats); // not needed
734 $children_array = grade_category
::_get_children_recursion($category);
736 ksort($children_array);
738 return $children_array;
742 function _get_children_recursion($category) {
744 $children_array = array();
745 foreach($category->children
as $sortorder=>$child) {
746 if (array_key_exists('itemtype', $child)) {
747 $grade_item = new grade_item($child, false);
748 if (in_array($grade_item->itemtype
, array('course', 'category'))) {
749 $type = $grade_item->itemtype
.'item';
750 $depth = $category->depth
;
753 $depth = $category->depth
; // we use this to set the same colour
755 $children_array[$sortorder] = array('object'=>$grade_item, 'type'=>$type, 'depth'=>$depth);
758 $children = grade_category
::_get_children_recursion($child);
759 $grade_category = new grade_category($child, false);
760 if (empty($children)) {
763 $children_array[$sortorder] = array('object'=>$grade_category, 'type'=>'category', 'depth'=>$grade_category->depth
, 'children'=>$children);
768 ksort($children_array);
770 return $children_array;
774 * Uses get_grade_item to load or create a grade_item, then saves it as $this->grade_item.
775 * @return object Grade_item
777 function load_grade_item() {
778 if (empty($this->grade_item
)) {
779 $this->grade_item
= $this->get_grade_item();
781 return $this->grade_item
;
785 * Retrieves from DB and instantiates the associated grade_item object.
786 * If no grade_item exists yet, create one.
787 * @return object Grade_item
789 function get_grade_item() {
790 if (empty($this->id
)) {
791 debugging("Attempt to obtain a grade_category's associated grade_item without the category's ID being set.");
795 if (empty($this->parent
)) {
796 $params = array('courseid'=>$this->courseid
, 'itemtype'=>'course', 'iteminstance'=>$this->id
);
799 $params = array('courseid'=>$this->courseid
, 'itemtype'=>'category', 'iteminstance'=>$this->id
);
802 if (!$grade_items = grade_item
::fetch_all($params)) {
804 $grade_item = new grade_item($params, false);
805 $grade_item->gradetype
= GRADE_TYPE_VALUE
;
806 $grade_item->insert('system');
808 } else if (count($grade_items) == 1){
809 // found existing one
810 $grade_item = reset($grade_items);
813 debugging("Found more than one grade_item attached to category id:".$this->id
);
815 $grade_item = reset($grade_items);
822 * Uses $this->parent to instantiate $this->parent_category based on the
823 * referenced record in the DB.
824 * @return object Parent_category
826 function load_parent_category() {
827 if (empty($this->parent_category
) && !empty($this->parent
)) {
828 $this->parent_category
= $this->get_parent_category();
830 return $this->parent_category
;
834 * Uses $this->parent to instantiate and return a grade_category object.
835 * @return object Parent_category
837 function get_parent_category() {
838 if (!empty($this->parent
)) {
839 $parent_category = new grade_category(array('id' => $this->parent
));
840 return $parent_category;
847 * Returns the most descriptive field for this object. This is a standard method used
848 * when we do not know the exact type of an object.
849 * @return string name
851 function get_name() {
852 if (empty($this->parent
)) {
853 return "Top course category"; //TODO: localize
855 return $this->fullname
;
860 * Sets this category's parent id. A generic method shared by objects that have a parent id of some kind.
861 * @param int parentid
862 * @return boolean success
864 function set_parent($parentid, $source=null) {
865 if ($this->parent
== $parentid) {
869 if ($parentid == $this->id
) {
870 error('Can not assign self as parent!');
873 if (empty($this->parent
) and $this->is_course_category()) {
874 error('Course category can not have parent!');
877 // find parent and check course id
878 if (!$parent_category = grade_category
::fetch(array('id'=>$parentid, 'courseid'=>$this->courseid
))) {
882 $this->force_regrading();
884 // set new parent category
885 $this->parent
= $parent_category->id
;
886 $this->parent_category
=& $parent_category;
887 $this->path
= null; // remove old path and depth - will be recalculated in update()
888 $this->depth
= null; // remove old path and depth - will be recalculated in update()
889 $this->update($source);
891 return $this->update($source);
895 * Returns the final values for this grade category.
896 * @param int $userid Optional: to retrieve a single final grade
897 * @return mixed An array of all final_grades (stdClass objects) for this grade_item, or a single final_grade.
899 function get_final($userid=NULL) {
900 $this->load_grade_item();
901 return $this->grade_item
->get_final($userid);
905 * Returns the sortorder of the associated grade_item. This method is also available in
906 * grade_item, for cases where the object type is not known.
907 * @return int Sort order
909 function get_sortorder() {
910 $this->load_grade_item();
911 return $this->grade_item
->get_sortorder();
915 * Sets sortorder variable for this category.
916 * This method is also available in grade_item, for cases where the object type is not know.
917 * @param int $sortorder
920 function set_sortorder($sortorder) {
921 $this->load_grade_item();
922 $this->grade_item
->set_sortorder($sortorder);
926 * Move this category after the given sortorder - does not change the parent
927 * @param int $sortorder to place after
929 function move_after_sortorder($sortorder) {
930 $this->load_grade_item();
931 $this->grade_item
->move_after_sortorder($sortorder);
935 * Return true if this is the top most category that represents the total course grade.
938 function is_course_category() {
939 $this->load_grade_item();
940 return $this->grade_item
->is_course_item();
944 * Return the top most course category.
946 * @return object grade_category instance for course grade
948 function fetch_course_category($courseid) {
950 // course category has no parent
951 if ($course_category = grade_category
::fetch(array('courseid'=>$courseid, 'parent'=>null))) {
952 return $course_category;
956 $course_category = new grade_category();
957 $course_category->insert_course_category($courseid);
959 return $course_category;
963 * Is grading object editable?
966 function is_editable() {
971 * Returns the locked state/date of the associated grade_item. This method is also available in
972 * grade_item, for cases where the object type is not known.
975 function is_locked() {
976 $this->load_grade_item();
977 return $this->grade_item
->is_locked();
981 * Sets the grade_item's locked variable and updates the grade_item.
982 * Method named after grade_item::set_locked().
983 * @param int $locked 0, 1 or a timestamp int(10) after which date the item will be locked.
984 * @return boolean success if category locked (not all children mayb be locked though)
986 function set_locked($lockedstate) {
987 $this->load_grade_item();
988 $result = $this->grade_item
->set_locked($lockedstate);
989 if ($children = grade_item
::fetch_all(array('categoryid'=>$this->id
))) {
990 foreach($children as $child) {
991 $child->set_locked($lockedstate);
994 if ($children = grade_category
::fetch_all(array('parent'=>$this->id
))) {
995 foreach($children as $child) {
996 $child->set_locked($lockedstate);
1003 * Returns the hidden state/date of the associated grade_item. This method is also available in
1007 function is_hidden() {
1008 $this->load_grade_item();
1009 return $this->grade_item
->is_hidden();
1013 * Sets the grade_item's hidden variable and updates the grade_item.
1014 * Method named after grade_item::set_hidden().
1015 * @param int $hidden 0, 1 or a timestamp int(10) after which date the item will be hidden.
1018 function set_hidden($hidden) {
1019 $this->load_grade_item();
1020 $this->grade_item
->set_hidden($hidden);
1021 if ($children = grade_item
::fetch_all(array('categoryid'=>$this->id
))) {
1022 foreach($children as $child) {
1023 $child->set_hidden($hidden);
1026 if ($children = grade_category
::fetch_all(array('parent'=>$this->id
))) {
1027 foreach($children as $child) {
1028 $child->set_hidden($hidden);