tagging as ATutor 1.5.4-release
[atutor.git] / include / lib / admin_categories.inc.php
1 <?php
2 /****************************************************************************/
3 /* ATutor                                                                                                                                       */
4 /****************************************************************************/
5 /* Copyright (c) 2002-2006 by Greg Gay, Joel Kronenberg & Heidi Hazelton        */
6 /* Adaptive Technology Resource Centre / University of Toronto                          */
7 /* http://atutor.ca                                                                                                                     */
8 /*                                                                                                                                                      */
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; }
14
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) {
19         if ($cat_id == 0) {
20                 echo '<ul>';
21                 foreach($categories[0] as $child_cat_id) {
22                         print_categories($categories, $child_cat_id);
23                 }
24                 echo '</ul>';
25         } else {
26                 echo '<li>';
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>';
31                 } else {
32                         echo '<a href="'.$_SERVER['PHP_SELF'].'?cat_id='.$cat_id.'">'.$categories[$cat_id]['cat_name'].'</a>';
33                 }
34                 echo ' <small class="spacer">('.$categories[$cat_id]['num_courses'].' ';
35                 if ($categories[$cat_id]['num_courses'] == 1) {
36                         echo _AT('course');
37                 } else {
38                         echo _AT('courses');
39                 }
40                 
41                 echo ')</small>';
42                 if (is_array($categories[$cat_id]['children'])) {
43                         echo '<ul>';
44                         foreach($categories[$cat_id]['children'] as $child_cat_id) {
45                                 print_categories($categories, $child_cat_id);
46                         }
47                         echo '</ul>';
48                 }
49                 echo '</li>';
50         }
51 }
52
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);
62                 }
63         } else {
64                 if ($exclude && ($cat_id == $current_cat_id)) {
65                         return;
66                 }
67                 echo '<option value="'.$cat_id.'"';
68
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"';
73                 }
74                 echo '>';
75                 echo str_repeat("&nbsp;", $depth*4);
76                 echo $categories[$cat_id]['cat_name'].'</option>';
77
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);
81                         }
82                 }
83         }
84 }
85
86 function get_categories() {
87         global $db;
88
89         /* get all the categories: */
90         /* $categories[category_id] = array(cat_name, cat_parent, num_courses, [array(children)]) */
91         $sql = "SELECT * FROM ".TABLE_PREFIX."course_cats ORDER BY cat_parent, cat_name";
92         $result = mysql_query($sql, $db);
93         while ($row = mysql_fetch_assoc($result)) {
94                 $categories[$row['cat_id']]['cat_name']    = $row['cat_name'];
95                 $categories[$row['cat_id']]['cat_parent']  = $row['cat_parent'];
96                 $categories[$row['cat_id']]['num_courses'] = 0;
97                 $categories[$row['cat_id']]['theme']       = $row['theme'];
98
99                 if ($row['cat_parent'] >0) {
100                         $categories[$row['cat_parent']]['children'][] = $row['cat_id'];
101                 } else {
102                         $categories[0][] = $row['cat_id'];
103                 }
104         }
105         return $categories;
106 }
107
108 /* assigns the 'num_courses' field in the $categories array */
109 /* returns the number of uncategorized courses */
110 function assign_categories_course_count(&$categories) {
111         global $db;
112
113         $num_uncategorized = 0;
114
115         $sql = "SELECT cat_id, COUNT(*) AS cnt FROM ".TABLE_PREFIX."courses GROUP BY cat_id";
116         $result = mysql_query($sql, $db);
117         while ($row = mysql_fetch_assoc($result)) {
118                 if ($row['cat_id'] == 0) {
119                         $num_uncategorized = $row['cnt'];
120                 } else {
121                         $categories[$row['cat_id']]['num_courses'] = $row['cnt'];
122                 }
123         }
124
125         return $num_uncategorized;
126 }
127
128 /* applies $theme to all the sub-categories recursively. */
129 /* returns an array of all the subcategories */
130 function recursive_get_subcategories($category_parent_id) {
131         static $categories;
132         if (!isset($categories)) {
133                 $categories = get_categories();
134         }
135
136         $children = array();
137         if (is_array($categories[$category_parent_id]['children'])) {
138                 $children = $categories[$category_parent_id]['children'];
139                 foreach ($categories[$category_parent_id]['children'] as $category_child_id) {
140                         if ($category_child_id) {
141                                 $children =  array_merge($children, recursive_get_subcategories($category_child_id));
142                         }
143                 }
144         }
145         return $children;
146 }
147
148 ?>