}
}
-/**
- * Gets the child categories of a given courses category. Uses a static cache
- * to make repeat calls efficient.
- *
- * @param int $parentid the id of a course category.
- * @return array all the child course categories.
- */
-function get_child_categories($parentid) {
- static $allcategories = null;
-
- // only fill in this variable the first time
- if (null == $allcategories) {
- $allcategories = array();
-
- $categories = get_categories();
- foreach ($categories as $category) {
- if (empty($allcategories[$category->parent])) {
- $allcategories[$category->parent] = array();
- }
- $allcategories[$category->parent][] = $category;
- }
- }
-
- if (empty($allcategories[$parentid])) {
- return array();
- } else {
- return $allcategories[$parentid];
- }
-}
-
/**
* This function generates a structured array of courses and categories.
*
*/
function get_course_category_tree($id = 0, $depth = 0) {
global $DB, $CFG;
- $viewhiddencats = has_capability('moodle/category:viewhiddencategories', context_system::instance());
- $categories = get_child_categories($id);
+ require_once($CFG->libdir. '/coursecatlib.php');
+ if (!$coursecat = coursecat::get($id, IGNORE_MISSING)) {
+ return array();
+ }
+ $categories = array();
$categoryids = array();
- foreach ($categories as $key => &$category) {
- if (!$category->visible && !$viewhiddencats) {
- unset($categories[$key]);
- continue;
- }
+ foreach ($coursecat->get_children() as $child) {
+ $categories[] = $category = $child->get_db_record();
$categoryids[$category->id] = $category;
if (empty($CFG->maxcategorydepth) || $depth <= $CFG->maxcategorydepth) {
list($category->categories, $subcategories) = get_course_category_tree($category->id, $depth+1);
*/
function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1, $showcourses = true, $categorycourses=NULL) {
global $CFG;
+ require_once($CFG->libdir. '/coursecatlib.php');
// maxcategorydepth == 0 meant no limit
if (!empty($CFG->maxcategorydepth) && $depth >= $CFG->maxcategorydepth) {
return;
}
- if (!$categorycourses) {
- if ($category) {
- $categorycourses = get_category_courses_array($category->id);
- } else {
- $categorycourses = get_category_courses_array();
+ // make sure category is visible to the current user
+ if ($category) {
+ if (!$coursecat = coursecat::get($category->id, IGNORE_MISSING)) {
+ return;
}
+ } else {
+ $coursecat = coursecat::get(0);
}
- if ($category) {
- if ($category->visible or has_capability('moodle/category:viewhiddencategories', context_system::instance())) {
- print_category_info($category, $depth, $showcourses, $categorycourses[$category->id]);
- } else {
- return; // Don't bother printing children of invisible categories
- }
+ if (!$categorycourses) {
+ $categorycourses = get_category_courses_array($coursecat->id);
+ }
- } else {
- $category = new stdClass();
- $category->id = "0";
+ if ($coursecat->id) {
+ print_category_info($category, $depth, $showcourses, $categorycourses[$category->id]);
}
- if ($categories = get_child_categories($category->id)) { // Print all the children recursively
+ if ($categories = $coursecat->get_children()) { // Print all the children recursively
$countcats = count($categories);
$count = 0;
$first = true;
*/
function print_courses($category) {
global $CFG, $OUTPUT;
+ require_once($CFG->libdir. '/coursecatlib.php');
if (!is_object($category) && $category==0) {
- $categories = get_child_categories(0); // Parent = 0 ie top-level categories only
+ $categories = coursecat::get(0)->get_children(); // Parent = 0 ie top-level categories only
if (is_array($categories) && count($categories) == 1) {
$category = array_shift($categories);
$courses = get_courses_wmanagers($category->id,
}
return $subcats;
}
+
+/**
+ * Gets the child categories of a given courses category
+ *
+ * This function is deprecated. Please use functions in class coursecat:
+ * - coursecat::get($parentid)->has_children()
+ * tells if the category has children (visible or not to the current user)
+ *
+ * - coursecat::get($parentid)->get_children()
+ * returns an array of coursecat objects, each of them represents a children category visible
+ * to the current user (i.e. visible=1 or user has capability to view hidden categories)
+ *
+ * - coursecat::get($parentid)->get_children_count()
+ * returns number of children categories visible to the current user
+ *
+ * - coursecat::count_all()
+ * returns total count of all categories in the system (both visible and not)
+ *
+ * - coursecat::get_default()
+ * returns the first category (usually to be used if count_all() == 1)
+ *
+ * @deprecated since 2.5
+ *
+ * @param int $parentid the id of a course category.
+ * @return array all the child course categories.
+ */
+function get_child_categories($parentid) {
+ global $DB;
+ debugging('Function get_child_categories() is deprecated. Use coursecat::get_children() or see phpdocs for more details.',
+ DEBUG_DEVELOPER);
+
+ $rv = array();
+ $sql = context_helper::get_preload_record_columns_sql('ctx');
+ $records = $DB->get_records_sql("SELECT c.*, $sql FROM {course_categories} c ".
+ "JOIN {context} ctx on ctx.instanceid = c.id AND ctx.contextlevel = ? WHERE c.parent = ? ORDER BY c.sortorder",
+ array(CONTEXT_COURSECAT, $parentid));
+ foreach ($records as $category) {
+ context_helper::preload_from_record($category);
+ if (!$category->visible && !has_capability('moodle/category:viewhiddencategories', context_coursecat::instance($category->id))) {
+ continue;
+ }
+ $rv[] = $category;
+ }
+ return $rv;
+}