remove old readme
[atutor.git] / docs / mods / _core / cats_categories / lib / admin_categories.inc.php
1 <?php
2 /****************************************************************************/
3 /* ATutor                                                                                                                                       */
4 /****************************************************************************/
5 /* Copyright (c) 2002-2010                                                  */
6 /* Inclusive Design Institute                                               */
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 validate_length($categories[$cat_id]['cat_name'], 45, VALIDATE_LENGTH_FOR_DISPLAY).'</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         $categories = array();
90
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'];
100
101                 if ($row['cat_parent'] >0) {
102                         $categories[$row['cat_parent']]['children'][] = $row['cat_id'];
103                 } else {
104                         $categories[0][] = $row['cat_id'];
105                 }
106         }
107         return $categories;
108 }
109
110 /* assigns the 'num_courses' field in the $categories array */
111 /* returns the number of uncategorized courses */
112 function assign_categories_course_count(&$categories) {
113         global $db;
114
115         $num_uncategorized = 0;
116
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'];
122                 } else {
123                         $categories[$row['cat_id']]['num_courses'] = $row['cnt'];
124                 }
125         }
126
127         return $num_uncategorized;
128 }
129
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) {
133         static $categories;
134         if (!isset($categories)) {
135                 $categories = get_categories();
136         }
137
138         $children = array();
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));
144                         }
145                 }
146         }
147         return $children;
148 }
149
150 ?>