static private function _fetch_course_tree_recursion($category_array, &$sortorder) {
        if (isset($category_array['object']->gradetype) && $category_array['object']->gradetype==GRADE_TYPE_NONE) {
            return null;
        }

        // store the grade_item or grade_category instance with extra info
        $result = array('object'=>$category_array['object'], 'type'=>$category_array['type'], 'depth'=>$category_array['depth']);

        // reuse final grades if there
        if (array_key_exists('finalgrades', $category_array)) {
            $result['finalgrades'] = $category_array['finalgrades'];
        }

        // recursively resort children
        if (!empty($category_array['children'])) {
            $result['children'] = array();
            //process the category item first
            $child = null;

            foreach ($category_array['children'] as $oldorder=>$child_array) {

                if ($child_array['type'] == 'courseitem' or $child_array['type'] == 'categoryitem') {
                    $child = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
                    if (!empty($child)) {
                        $result['children'][$sortorder] = $child;
                    }
                }
            }

            foreach ($category_array['children'] as $oldorder=>$child_array) {

                if ($child_array['type'] != 'courseitem' and $child_array['type'] != 'categoryitem') {
                    $child = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
                    if (!empty($child)) {
                        $result['children'][++$sortorder] = $child;
                    }
                }
            }
        }

        return $result;
    }
static private function _fetch_course_tree_recursion($category_array, &$sortorder) {
        if (isset($category_array['object']->gradetype) && $category_array['object']->gradetype==GRADE_TYPE_NONE) {
            return null;
        }

        // store the grade_item or grade_category instance with extra info
        $result = array('object'=>$category_array['object'], 'type'=>$category_array['type'], 'depth'=>$category_array['depth']);

        // reuse final grades if there
        if (array_key_exists('finalgrades', $category_array)) {
            $result['finalgrades'] = $category_array['finalgrades'];
        }

        // recursively resort children
        if (!empty($category_array['children'])) {
            $result['children'] = array();
            //process the category item first
            $child = null;

            foreach ($category_array['children'] as $oldorder=>$child_array) {

                if ($child_array['type'] == 'courseitem' or $child_array['type'] == 'categoryitem') {
                    $child = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
                    if (!empty($child)) {
                        $result['children'][$sortorder] = $child;
                    }
                }
            }

            foreach ($category_array['children'] as $oldorder=>$child_array) {

                if ($child_array['type'] != 'courseitem' and $child_array['type'] != 'categoryitem') {
                    $child = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
                    if (!empty($child)) {
                        $result['children'][++$sortorder] = $child;
                    }
                }
            }
        }

        return $result;
    }
static private function _fetch_course_tree_recursion ( $ category_array , & $ sortorder ) { if ( isset ( $ category_array [ 'object' ] -> gradetype ) && $ category_array [ 'object' ] -> gradetype == GRADE_TYPE_NONE ) { return null ; } // store the grade_item or grade_category instance with extra info $ result = array ( 'object' => $ category_array [ 'object' ] , 'type' => $ category_array [ 'type' ] , 'depth' => $ category_array [ 'depth' ] ) ; // reuse final grades if there if ( array_key_exists ( 'finalgrades' , $ category_array ) ) { $ result [ 'finalgrades' ] = $ category_array [ 'finalgrades' ] ; } // recursively resort children if ( ! empty ( $ category_array [ 'children' ] ) ) { $ result [ 'children' ] = array ( ) ; //process the category item first $ child = null ; foreach ( $ category_array [ 'children' ] as $ oldorder => $ child_array ) { if ( $ child_array [ 'type' ] == 'courseitem' or $ child_array [ 'type' ] == 'categoryitem' ) { $ child = grade_category :: _fetch_course_tree_recursion ( $ child_array , $ sortorder ) ; if ( ! empty ( $ child ) ) { $ result [ 'children' ] [ $ sortorder ] = $ child ; } } } foreach ( $ category_array [ 'children' ] as $ oldorder => $ child_array ) { if ( $ child_array [ 'type' ] != 'courseitem' and $ child_array [ 'type' ] != 'categoryitem' ) { $ child = grade_category :: _fetch_course_tree_recursion ( $ child_array , $ sortorder ) ; if ( ! empty ( $ child ) ) { $ result [ 'children' ] [ ++ $ sortorder ] = $ child ; } } } } return $ result ; }
An internal function that recursively sorts grade categories within a course

@param array $category_array The seed of the recursion
@param int   $sortorder The current sortorder
@return array An array containing 'object', 'type', 'depth' and optionally 'children'
An internal function that recursively sorts grade categories within a course