remove old readme
[atutor.git] / mods / _standard / links / lib / links.inc.php
1 <?php\r
2 /****************************************************************************/\r
3 /* ATutor                                                                                                                                       */\r
4 /****************************************************************************/\r
5 /* Copyright (c) 2002-2010                                                  */\r
6 /* Inclusive Design Institute                                               */\r
7 /* http://atutor.ca                                                                                                                     */\r
8 /*                                                                                                                                                      */\r
9 /* This program is free software. You can redistribute it and/or                        */\r
10 /* modify it under the terms of the GNU General Public License                          */\r
11 /* as published by the Free Software Foundation.                                                        */\r
12 /****************************************************************************/\r
13 \r
14 \r
15 /**\r
16  * given an owner_type and owner_id\r
17  * returns false if user cannot manage link of owner_type\r
18  * returns true if they can\r
19  */\r
20 function links_authenticate($owner_type, $owner_id) {\r
21         global $db;\r
22 \r
23         if (empty($owner_type) || empty($owner_id)) {\r
24                 return false;\r
25         }\r
26 \r
27         //if admin or TA w/ right privs, can manage all links\r
28         //if ($_SESSION['is_admin'] || $_SESSION['privileges'] > 0) {\r
29         if (authenticate(AT_PRIV_GROUPS+AT_PRIV_LINKS, true)) {\r
30                 return true;\r
31         }\r
32 \r
33         if ($owner_type == LINK_CAT_GROUP) {\r
34                 //check if member of group\r
35                 if ($_SESSION['valid_user'] && isset($_SESSION['groups'])) {                    \r
36                         $sql="SELECT * FROM ".TABLE_PREFIX."groups_members WHERE group_id=".$owner_id." AND member_id=".$_SESSION['member_id'];\r
37                         $result = mysql_query($sql, $db);\r
38                         if ($row = mysql_fetch_assoc($result)) {\r
39                                 return true;\r
40                         }\r
41                 } \r
42         } \r
43 \r
44         return false;\r
45 }\r
46 \r
47 /* return true if user is able to manage group or course links */\r
48 function manage_links() {\r
49         global $db;\r
50 \r
51         if (authenticate(AT_PRIV_GROUPS, true) && authenticate(AT_PRIV_LINKS, true)) { //course and group links\r
52                 return LINK_CAT_AUTH_ALL;\r
53         } else if (authenticate(AT_PRIV_GROUPS, true)) { //all group links\r
54                 return LINK_CAT_AUTH_GROUP;\r
55         } else if (authenticate(AT_PRIV_LINKS, true)) { //course links\r
56                 return LINK_CAT_AUTH_COURSE;\r
57         } else if (!empty($_SESSION['groups'])) { //particular group links\r
58                 //find a group that uses links\r
59                 foreach ($_SESSION['groups'] as $group_id) {\r
60                         $sql = "SELECT modules FROM ".TABLE_PREFIX."groups WHERE group_id=$group_id";\r
61                         $result = mysql_query($sql, $db);\r
62 \r
63                         $row = mysql_fetch_assoc($result);\r
64                         $mods = explode('|', $row['modules']);\r
65 \r
66                         if (in_array("_standard/links", $mods)) {\r
67                                 return LINK_CAT_AUTH_GROUP;\r
68                         }\r
69                 }\r
70 \r
71                 return FALSE;\r
72         }\r
73 \r
74         return LINK_CAT_AUTH_NONE;\r
75 }\r
76 \r
77 \r
78 //if manage, then it's getting categories for only those that should see them\r
79 //if list, then filter out the uneditable group cats from the manage list (otherwise it's a dropdown of cats)\r
80 function get_link_categories($manage=false, $list=false) {\r
81         global $db, $_base_path;\r
82         $categories = array();\r
83 \r
84         /* get all the categories: */\r
85         /* $categories[category_id] = array(cat_name, cat_parent, num_courses, [array(children)]) */\r
86 \r
87         if ($_SESSION['groups']) {\r
88                 $groups = implode(',', $_SESSION['groups']);\r
89         } else {\r
90                 // not in any groups\r
91                 $groups = 0;\r
92         }\r
93 \r
94         //if suggest a link page\r
95         if ($_SERVER['PHP_SELF'] == $_base_path.'mods/links/add.php') {\r
96                 $sql = "SELECT * FROM ".TABLE_PREFIX."links_categories WHERE (owner_id=$_SESSION[course_id] AND owner_type=".LINK_CAT_COURSE.") ORDER BY parent_id, name";\r
97         } else if ($manage) {\r
98                 $sql = "SELECT * FROM ".TABLE_PREFIX."links_categories WHERE ";\r
99                 if ( authenticate(AT_PRIV_GROUPS, true) && authenticate(AT_PRIV_COURSE, true) ) { \r
100                         if ($list) {\r
101                                 $sql .= "(owner_id=$_SESSION[course_id] AND owner_type=".LINK_CAT_COURSE.") OR (owner_id IN ($groups) AND owner_type=".LINK_CAT_GROUP." AND name<>'')";\r
102                         } else {\r
103                                 $sql .= "(owner_id=$_SESSION[course_id] AND owner_type=".LINK_CAT_COURSE.") OR (owner_id IN ($groups) AND owner_type=".LINK_CAT_GROUP.")";\r
104                         }\r
105 \r
106                 } else if ( authenticate(AT_PRIV_LINKS, true) ) {\r
107                         $sql .= "(owner_id=$_SESSION[course_id] AND owner_type=".LINK_CAT_COURSE.")";\r
108                         if (!empty($groups)) {\r
109                                 $sql .= " OR (owner_id IN ($groups) AND owner_type=".LINK_CAT_GROUP.")";                        \r
110                         }\r
111                 } else if ( authenticate(AT_PRIV_GROUPS, true) || !empty($groups) ) { \r
112                         if ($list) {\r
113                                 $sql .= "(owner_id IN ($groups) AND owner_type=".LINK_CAT_GROUP." AND name<>'')";\r
114                         } else {\r
115                                 $sql .= "(owner_id IN ($groups) AND owner_type=".LINK_CAT_GROUP.")";\r
116                         }\r
117                 }       \r
118                 $sql .= " ORDER BY parent_id, name";\r
119         } else {\r
120                 if (!empty($groups)) {\r
121                         $sql = "SELECT * FROM ".TABLE_PREFIX."links_categories WHERE (owner_id=$_SESSION[course_id] AND owner_type=".LINK_CAT_COURSE.") OR (owner_id IN ($groups) AND owner_type=".LINK_CAT_GROUP.") ORDER BY parent_id, name";\r
122                 } else {\r
123                         $sql = "SELECT * FROM ".TABLE_PREFIX."links_categories WHERE (owner_id=$_SESSION[course_id] AND owner_type=".LINK_CAT_COURSE.") ORDER BY parent_id, name";\r
124                 }\r
125         }\r
126         $result = mysql_query($sql, $db);\r
127 \r
128         while ($row = mysql_fetch_assoc($result)) {\r
129                 //if group, get name\r
130                 if (empty($row['name'])) {\r
131                         $row['name'] = get_group_name($row['owner_id']);\r
132                         $categories[$row['cat_id']]['group'] = 1;\r
133                 }\r
134 \r
135                 $categories[$row['cat_id']]['cat_name']    = $row['name'];\r
136                 $categories[$row['cat_id']]['cat_parent']  = $row['parent_id'];\r
137 \r
138                 if ($row['parent_id'] > 0) {\r
139                         $categories[$row['parent_id']]['children'][] = $row['cat_id'];\r
140                 } else {\r
141                         $categories[0][] = $row['cat_id'];\r
142                 }\r
143         }\r
144 \r
145         return $categories;\r
146 }\r
147 \r
148 function select_link_categories($categories, $cat_id, $current_cat_id, $exclude, $depth=0, $owner=FALSE) {\r
149         global $db; \r
150 \r
151         if ($cat_id == 0 && is_array($categories[0])) {\r
152                 foreach($categories[0] as $child_cat_id) {\r
153                         select_link_categories($categories, $child_cat_id, $current_cat_id, $depth, 0, $owner);\r
154                 }\r
155         } else {\r
156                 $sql = "SELECT name, owner_type, owner_id FROM ".TABLE_PREFIX."links_categories WHERE cat_id=$cat_id";\r
157                 $result = mysql_query($sql, $db);\r
158                 $row = mysql_fetch_assoc($result);\r
159 \r
160 \r
161                 if ($exclude && ($cat_id == $current_cat_id)) {\r
162                         return;\r
163                 }\r
164 \r
165                 if ($owner) {\r
166                         echo '<option value="'.$cat_id.'-'.$row['owner_type'].'-'.$row['owner_id'].'"';\r
167                 } else  {\r
168                         echo '<option value="'.$cat_id.'"';\r
169                 }\r
170         \r
171                 if ($exclude && is_array($categories[$cat_id]['children']) && in_array($current_cat_id, $categories[$cat_id]['children'])) {\r
172                         echo ' selected="selected"';\r
173                 } else if (!$exclude && ($cat_id == $current_cat_id)) {\r
174                         echo ' selected="selected"';\r
175                 }\r
176                 echo '>';\r
177                 echo str_repeat("&nbsp;", $depth*4);\r
178                 echo $categories[$cat_id]['cat_name'].'</option>';\r
179 \r
180                 if (is_array($categories[$cat_id]['children'])) {\r
181                         foreach($categories[$cat_id]['children'] as $child_cat_id) {\r
182                                 select_link_categories($categories, $child_cat_id, $current_cat_id, $exclude, $depth+1, $owner);\r
183                         }\r
184                 }\r
185         }\r
186 }\r
187 \r
188 /**\r
189  Given a $cat_id, return IDs of all children of that ID as a comma seperated \r
190  string.\r
191  */\r
192 function get_child_categories ($cat_id, $categories) {\r
193     if (!isset ($categories)) {\r
194         $categories = get_link_categories();\r
195     }\r
196     \r
197     $category = $categories[$cat_id];\r
198     $children_string = "";\r
199     if (is_array($categories[$cat_id]['children'])){\r
200         foreach ($categories[$cat_id]['children'] as $child) {\r
201             $children_string = $child.",";\r
202         }\r
203     }\r
204     return $children_string;\r
205 }\r
206 \r
207 function get_group_name($owner_id) {\r
208         global $db;\r
209 \r
210         if (!$owner_id) {\r
211                 return false;\r
212         }\r
213 \r
214         $sql = "SELECT title FROM ".TABLE_PREFIX."groups WHERE group_id=".$owner_id;\r
215         $result = mysql_query($sql, $db);\r
216         $row = mysql_fetch_assoc($result);\r
217         return $row['title'];\r
218 }\r
219 \r
220 function get_cat_info($cat_id) {\r
221         global $db;\r
222 \r
223         $sql = "SELECT * FROM ".TABLE_PREFIX."links_categories WHERE cat_id=".$cat_id;\r
224         $result = mysql_query($sql, $db);\r
225         $row = mysql_fetch_assoc($result);\r
226 \r
227         return $row;\r
228 }\r
229 \r
230 ?>