}
}
-
-/**
- * This functions get all the course categories in proper order
- * (!)note this only gets course category contexts, and not the site
- * context
- * @param object $context
- * @return array of contextids
- */
-function get_parent_cats($context) {
- global $COURSE;
-
- switch ($context->contextlevel) {
- // a category can be the parent of another category
- // there is no limit of depth in this case
- case CONTEXT_COURSECAT:
- static $categoryparents = null; // cache for parent categories
- if (!isset($categoryparents)) {
- $categoryparents = array();
- }
- if (array_key_exists($context->instanceid, $categoryparents)) {
- return $categoryparents[$context->instanceid];
- }
-
- if (!$cat = get_record('course_categories','id',$context->instanceid)) {
- //error?
- return array();
- }
- $parents = array();
- while (!empty($cat->parent)) {
- if (!$catcontext = get_context_instance(CONTEXT_COURSECAT, $cat->parent)) {
- debugging('Incorrect category parent');
- break;
- }
- $parents[] = $catcontext->id;
- $cat = get_record('course_categories','id',$cat->parent);
- }
- return $categoryparents[$context->instanceid] = array_reverse($parents);
- break;
-
- // a course always fall into a category, unless it's a site course
- // this happens when SITEID == $course->id
- // in this case the parent of the course is site context
- case CONTEXT_COURSE:
- static $courseparents = null; // cache course parents
- if (!isset($courseparents)) {
- $courseparents = array();
- }
- if (array_key_exists($context->instanceid, $courseparents)) {
- return $courseparents[$context->instanceid];
- }
-
- if (count($courseparents) > 1000) {
- $courseparents = array(); // max cache size when looping through thousands of courses
- }
-
- if ($context->instanceid == SITEID) {
- return $courseparents[$context->instanceid] = array(); // frontpage course does not have parent cats
- }
- if ($context->instanceid == $COURSE->id) {
- $course = $COURSE;
- } else if (!$course = get_record('course', 'id', $context->instanceid)) {
- //error?
- return array();;
- }
-
- if (empty($course->category)) {
- // this should not happen
- return $courseparents[$context->instanceid] = array();
- }
-
- if (!$catcontext = get_context_instance(CONTEXT_COURSECAT, $course->category)) {
- debugging('Incorect course category');
- return array();;
- }
-
- return $courseparents[$context->instanceid] = array_merge(get_parent_cats($catcontext), array($catcontext->id)); //recursion :-)
- break;
-
- default:
- // something is very wrong!
- return array();
- break;
- }
-}
-
-
function has_capability($capability, $context=NULL, $userid=NULL, $doanything=true) {
global $USER, $CONTEXT, $ACCESS, $CFG, $DIRTYCONTEXTS;