/** * Return all the categories from the node $parent (recursivly). Also returns * the number of courses (nbCourses) directly linked to each category and the * level of each category. * * @param int identifier of the parent from wich we want to get * the categories tree (default: 0) * @param int level where we start (default: 0) * @param bool visibility (1 = only visible, 0 = only invisible, null = all; default: null) * @return array collection of all the categories organized hierarchically and ordered by rank */ function claro_get_all_categories($parent = 0, $level = 0, $visibility = null) { // Get table name $tbl_mdb_names = claro_sql_get_main_tbl(); $tbl_category = $tbl_mdb_names['category']; $tbl_course = $tbl_mdb_names['course']; $tbl_rel_course_category = $tbl_mdb_names['rel_course_category']; // Retrieve all children of the id $parent $sql = "SELECT COUNT(rcc.courseId) AS nbCourses,\n ca.id, ca.name,\n ca.code,\n ca.idParent,\n ca.rank,\n ca.visible,\n ca.canHaveCoursesChild,\n co.intitule AS dedicatedCourse,\n co.code AS dedicatedCourseCode\n \n FROM `" . $tbl_category . "` AS ca\n \n LEFT JOIN `" . $tbl_rel_course_category . "` AS rcc\n ON rcc.categoryId = ca.id\n \n LEFT JOIN `" . $tbl_course . "` AS co\n ON rcc.courseId = co.cours_id AND rcc.rootCourse = 1\n \n WHERE ca.idParent = " . (int) $parent; if (!is_null($visibility)) { $sql .= "\n AND ca.visible = " . $visibility; } $sql .= "\n GROUP BY ca.`id`"; if (get_conf('categories_order_by') == 'rank') { $sql .= "\n ORDER BY ca.`rank`"; } elseif (get_conf('categories_order_by') == 'alpha_asc') { $sql .= "\n ORDER BY ca.`name` ASC"; } elseif (get_conf('categories_order_by') == 'alpha_desc') { $sql .= "\n ORDER BY ca.`name` DESC"; } $result = Claroline::getDatabase()->query($sql); $result_array = array(); // Get each child foreach ($result as $row) { $row['level'] = $level; $result_array[] = $row; // Call this function again to get the next level of the tree $result_array = array_merge($result_array, claro_get_all_categories($row['id'], $level + 1)); } return $result_array; }
/** * Select all categories in database from a certain point, ordered by rank. Use the attribute "level" for * display purpose. For instance: echo str_repeat(' ', 4*$category['level']) . $category['name']; * * @param int $start_node the parent from wich we want to get the categories tree (default: 0) * @param int $start_level the level where we start (default: 0) * @param bool $visibility (1 = only visible, 0 = only invisible, null = all; default: null) * @return iterator containing all the categories organized hierarchically and ordered by rank */ public static function getAllCategories($start_node = 0, $start_level = 0, $visibility = null) { return claro_get_all_categories($start_node, $start_level, $visibility); }