Permalink
Browse files

MDL-38147 deprecated get_child_categories(), change usage to coursecat

  • Loading branch information...
marinaglancy committed Feb 22, 2013
1 parent bc81b00 commit 8db5dcb7cd3c668260cdef240dd2c66e40a5a92b
Showing with 69 additions and 54 deletions.
  1. +22 −53 course/lib.php
  2. +45 −0 lib/deprecatedlib.php
  3. +2 −1 lib/upgrade.txt
View
@@ -1219,36 +1219,6 @@ function get_category_or_system_context($categoryid) {
}
}
-/**
- * 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.
*
@@ -1265,14 +1235,14 @@ function get_child_categories($parentid) {
*/
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);
@@ -1324,33 +1294,31 @@ function get_course_category_tree($id = 0, $depth = 0) {
*/
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;
@@ -1576,9 +1544,10 @@ function can_edit_in_category($categoryid = 0) {
*/
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,
View
@@ -3741,3 +3741,48 @@ function get_all_subcategories($catid) {
}
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;
+}
View
@@ -37,7 +37,8 @@ information provided here is intended especially for developers.
* Functions responsible for managing and accessing course categories are moved to class coursecat
in lib/coursecatlib.php. The following global functions are deprecated: make_categories_list(),
category_delete_move(), category_delete_full(), move_category(), course_category_hide(),
- course_category_show(), get_course_category(), create_course_category(), get_all_subcategories()
+ course_category_show(), get_course_category(), create_course_category(), get_all_subcategories(),
+ get_child_categories()
YUI changes:
* M.util.help_icon has been deprecated. Code should be updated to use moodle-core-popuphelp

0 comments on commit 8db5dcb

Please sign in to comment.