protected static function filter_collapsed_categories($courseid, $collapsed) {
        global $DB;
        if (empty($collapsed)) {
            $collapsed = array('aggregatesonly' => array(), 'gradesonly' => array());
        }
        if (empty($collapsed['aggregatesonly']) && empty($collapsed['gradesonly'])) {
            return $collapsed;
        }
        $cats = $DB->get_fieldset_select('grade_categories', 'id', 'courseid = ?', array($courseid));
        $collapsed['aggregatesonly'] = array_values(array_intersect($collapsed['aggregatesonly'], $cats));
        $collapsed['gradesonly'] = array_values(array_intersect($collapsed['gradesonly'], $cats));
        return $collapsed;
    }