2 /****************************************************************************/
\r
4 /****************************************************************************/
\r
5 /* Copyright (c) 2002-2010 */
\r
6 /* Inclusive Design Institute */
\r
7 /* http://atutor.ca */
\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
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
20 function links_authenticate($owner_type, $owner_id) {
\r
23 if (empty($owner_type) || empty($owner_id)) {
\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
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
47 /* return true if user is able to manage group or course links */
\r
48 function manage_links() {
\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
63 $row = mysql_fetch_assoc($result);
\r
64 $mods = explode('|', $row['modules']);
\r
66 if (in_array("_standard/links", $mods)) {
\r
67 return LINK_CAT_AUTH_GROUP;
\r
74 return LINK_CAT_AUTH_NONE;
\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
84 /* get all the categories: */
\r
85 /* $categories[category_id] = array(cat_name, cat_parent, num_courses, [array(children)]) */
\r
87 if ($_SESSION['groups']) {
\r
88 $groups = implode(',', $_SESSION['groups']);
\r
90 // not in any groups
\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
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
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
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
111 } else if ( authenticate(AT_PRIV_GROUPS, true) || !empty($groups) ) {
\r
113 $sql .= "(owner_id IN ($groups) AND owner_type=".LINK_CAT_GROUP." AND name<>'')";
\r
115 $sql .= "(owner_id IN ($groups) AND owner_type=".LINK_CAT_GROUP.")";
\r
118 $sql .= " ORDER BY parent_id, name";
\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
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
126 $result = mysql_query($sql, $db);
\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
135 $categories[$row['cat_id']]['cat_name'] = $row['name'];
\r
136 $categories[$row['cat_id']]['cat_parent'] = $row['parent_id'];
\r
138 if ($row['parent_id'] > 0) {
\r
139 $categories[$row['parent_id']]['children'][] = $row['cat_id'];
\r
141 $categories[0][] = $row['cat_id'];
\r
145 return $categories;
\r
148 function select_link_categories($categories, $cat_id, $current_cat_id, $exclude, $depth=0, $owner=FALSE) {
\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
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
161 if ($exclude && ($cat_id == $current_cat_id)) {
\r
166 echo '<option value="'.$cat_id.'-'.$row['owner_type'].'-'.$row['owner_id'].'"';
\r
168 echo '<option value="'.$cat_id.'"';
\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
177 echo str_repeat(" ", $depth*4);
\r
178 echo $categories[$cat_id]['cat_name'].'</option>';
\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
189 Given a $cat_id, return IDs of all children of that ID as a comma seperated
\r
192 function get_child_categories ($cat_id, $categories) {
\r
193 if (!isset ($categories)) {
\r
194 $categories = get_link_categories();
\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
204 return $children_string;
\r
207 function get_group_name($owner_id) {
\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
220 function get_cat_info($cat_id) {
\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