2 /****************************************************************************/
4 /****************************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
9 /* This program is free software. You can redistribute it and/or */
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /****************************************************************************/
13 if (!defined('AT_INCLUDE_PATH')) { exit; }
15 /* prints out the given $categories as an HTML list */
16 /* $categories: categories given, where the key is the cat_id */
17 /* $cat_id: the current category id */
18 function print_categories($categories, $cat_id) {
21 foreach($categories[0] as $child_cat_id) {
22 print_categories($categories, $child_cat_id);
27 if ($cat_id == $_REQUEST['cat_id']) {
28 echo '<strong>'.$categories[$cat_id]['cat_name'].'</strong>';
29 } else if ($cat_id == $_REQUEST['pcat_id']) {
30 echo '<a href="'.$_SERVER['PHP_SELF'].'?cat_id='.$cat_id.'"><b>'.$categories[$cat_id]['cat_name'].'</b></a>';
32 echo '<a href="'.$_SERVER['PHP_SELF'].'?cat_id='.$cat_id.'">'.$categories[$cat_id]['cat_name'].'</a>';
34 echo ' <small class="spacer">('.$categories[$cat_id]['num_courses'].' ';
35 if ($categories[$cat_id]['num_courses'] == 1) {
42 if (is_array($categories[$cat_id]['children'])) {
44 foreach($categories[$cat_id]['children'] as $child_cat_id) {
45 print_categories($categories, $child_cat_id);
53 /* generates a <select> of the given $categories */
54 /* $cat_id: the current cat id to start the traversal */
55 /* $current_cat_id: the current category id, will be set to "selected" if $exclude is false o/w the parent will be selected */
56 /* $exclude: whether or not the children of $current_cat_id should be excluded or not. */
57 /* $depth: just keeps track of how deep the $cat_id is */
58 function select_categories($categories, $cat_id, $current_cat_id, $exclude, $depth=0) {
59 if ($cat_id == 0 && is_array($categories[0])) {
60 foreach($categories[0] as $child_cat_id) {
61 select_categories($categories, $child_cat_id, $current_cat_id, $exclude);
64 if ($exclude && ($cat_id == $current_cat_id)) {
67 echo '<option value="'.$cat_id.'"';
69 if ($exclude && is_array($categories[$cat_id]['children']) && in_array($current_cat_id, $categories[$cat_id]['children'])) {
70 echo ' selected="selected"';
71 } else if (!$exclude && ($cat_id == $current_cat_id)) {
72 echo ' selected="selected"';
75 echo str_repeat(" ", $depth*4);
76 echo validate_length($categories[$cat_id]['cat_name'], 45, VALIDATE_LENGTH_FOR_DISPLAY).'</option>';
78 if (is_array($categories[$cat_id]['children'])) {
79 foreach($categories[$cat_id]['children'] as $child_cat_id) {
80 select_categories($categories, $child_cat_id, $current_cat_id, $exclude, $depth+1);
86 function get_categories() {
89 $categories = array();
91 /* get all the categories: */
92 /* $categories[category_id] = array(cat_name, cat_parent, num_courses, [array(children)]) */
93 $sql = "SELECT * FROM ".TABLE_PREFIX."course_cats ORDER BY cat_parent, cat_name";
94 $result = mysql_query($sql, $db);
95 while ($row = mysql_fetch_assoc($result)) {
96 $categories[$row['cat_id']]['cat_name'] = $row['cat_name'];
97 $categories[$row['cat_id']]['cat_parent'] = $row['cat_parent'];
98 $categories[$row['cat_id']]['num_courses'] = 0;
99 $categories[$row['cat_id']]['theme'] = $row['theme'];
101 if ($row['cat_parent'] >0) {
102 $categories[$row['cat_parent']]['children'][] = $row['cat_id'];
104 $categories[0][] = $row['cat_id'];
110 /* assigns the 'num_courses' field in the $categories array */
111 /* returns the number of uncategorized courses */
112 function assign_categories_course_count(&$categories) {
115 $num_uncategorized = 0;
117 $sql = "SELECT cat_id, COUNT(*) AS cnt FROM ".TABLE_PREFIX."courses GROUP BY cat_id";
118 $result = mysql_query($sql, $db);
119 while ($row = mysql_fetch_assoc($result)) {
120 if ($row['cat_id'] == 0) {
121 $num_uncategorized = $row['cnt'];
123 $categories[$row['cat_id']]['num_courses'] = $row['cnt'];
127 return $num_uncategorized;
130 /* applies $theme to all the sub-categories recursively. */
131 /* returns an array of all the subcategories */
132 function recursive_get_subcategories($category_parent_id) {
134 if (!isset($categories)) {
135 $categories = get_categories();
139 if (isset($categories[$category_parent_id]['children']) && is_array($categories[$category_parent_id]['children'])) {
140 $children = $categories[$category_parent_id]['children'];
141 foreach ($categories[$category_parent_id]['children'] as $category_child_id) {
142 if ($category_child_id) {
143 $children = array_merge($children, recursive_get_subcategories($category_child_id));