/** * Returns an array of category ids of all the subcategories for a given * category. * * @global object * @param int $catid - The id of the category whose subcategories we want to find. * @return array of category ids. */ function get_all_subcategories($catid) { global $DB; $subcats = array(); if ($categories = $DB->get_records('course_categories', array('parent' => $catid))) { foreach ($categories as $cat) { array_push($subcats, $cat->id); $subcats = array_merge($subcats, get_all_subcategories($cat->id)); } } return $subcats; }
/** * Returns an array of category ids of all the subcategories for a given * category. * @param $catid - The id of the category whose subcategories we want to find. * @return array of category ids. */ function get_all_subcategories($catid) { $subcats = array(); if ($categories = get_records('course_categories', 'parent', $catid)) { foreach ($categories as $cat) { array_push($subcats, $cat->id); $subcats = array_merge($subcats, get_all_subcategories($cat->id)); } } return $subcats; }
/** * Returns an array of category ids of all the subcategories for a given * category. * * This function is deprecated. * * To get visible children categories of the given category use: * coursecat::get($categoryid)->get_children(); * This function will return the array or coursecat objects, on each of them * you can call get_children() again * * @see coursecat::get() * @see coursecat::get_children() * * @deprecated since 2.5 * * @global object * @param int $catid - The id of the category whose subcategories we want to find. * @return array of category ids. */ function get_all_subcategories($catid) { global $DB; debugging('Function get_all_subcategories() is deprecated. Please use appropriate methods() of coursecat class. See phpdocs for more details', DEBUG_DEVELOPER); $subcats = array(); if ($categories = $DB->get_records('course_categories', array('parent' => $catid))) { foreach ($categories as $cat) { array_push($subcats, $cat->id); $subcats = array_merge($subcats, get_all_subcategories($cat->id)); } } return $subcats; }
/** * Recursive function which, given a context, find all its children context ids. * @param object $context. * @return array of children context ids. */ function get_child_contexts($context) { global $CFG; $children = array(); switch ($context->contextlevel) { case CONTEXT_BLOCK: // No children. return array(); break; case CONTEXT_MODULE: // No children. return array(); break; case CONTEXT_GROUP: // No children. return array(); break; case CONTEXT_COURSE: // Find all block instances for the course. $page = new page_course(); $page->id = $context->instanceid; $page->type = 'course-view'; if ($blocks = blocks_get_by_page_pinned($page)) { foreach ($blocks['l'] as $leftblock) { if ($child = get_context_instance(CONTEXT_BLOCK, $leftblock->id)) { array_push($children, $child->id); } } foreach ($blocks['r'] as $rightblock) { if ($child = get_context_instance(CONTEXT_BLOCK, $rightblock->id)) { array_push($children, $child->id); } } } // Find all module instances for the course. if ($modules = get_records('course_modules', 'course', $context->instanceid)) { foreach ($modules as $module) { if ($child = get_context_instance(CONTEXT_MODULE, $module->id)) { array_push($children, $child->id); } } } // Find all group instances for the course. if ($groupids = groups_get_groups($context->instanceid)) { foreach ($groupids as $groupid) { if ($child = get_context_instance(CONTEXT_GROUP, $groupid)) { array_push($children, $child->id); } } } return $children; break; case CONTEXT_COURSECAT: // We need to get the contexts for: // 1) The subcategories of the given category // 2) The courses in the given category and all its subcategories // 3) All the child contexts for these courses $categories = get_all_subcategories($context->instanceid); // Add the contexts for all the subcategories. foreach ($categories as $catid) { if ($catci = get_context_instance(CONTEXT_COURSECAT, $catid)) { array_push($children, $catci->id); } } // Add the parent category as well so we can find the contexts // for its courses. array_unshift($categories, $context->instanceid); foreach ($categories as $catid) { // Find all courses for the category. if ($courses = get_records('course', 'category', $catid)) { foreach ($courses as $course) { if ($courseci = get_context_instance(CONTEXT_COURSE, $course->id)) { array_push($children, $courseci->id); $children = array_merge($children, get_child_contexts($courseci)); } } } } return $children; break; case CONTEXT_USER: // No children. return array(); break; case CONTEXT_PERSONAL: // No children. return array(); break; case CONTEXT_SYSTEM: // Just get all the contexts except for CONTEXT_SYSTEM level. $sql = 'SELECT c.id ' . 'FROM ' . $CFG->prefix . 'context AS c ' . 'WHERE contextlevel != ' . CONTEXT_SYSTEM; $contexts = get_records_sql($sql); foreach ($contexts as $cid) { array_push($children, $cid->id); } return $children; break; default: error('This is an unknown context (' . $context->contextlevel . ') in get_child_contexts!'); return false; } }