tagging as ATutor 1.5.4-release
[atutor.git] / links / index.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 // $Id$
14
15 define('AT_INCLUDE_PATH', '../include/');
16
17 require (AT_INCLUDE_PATH.'vitals.inc.php');
18 require (AT_INCLUDE_PATH.'lib/links.inc.php');
19
20 if (!manage_links()) {
21         $_pages['links/index.php']['children']  = array('links/add.php');
22 }
23
24 if (isset($_GET['view'])) {
25         $_GET['view'] = intval($_GET['view']);
26         //add to the num hits
27         $sql = "SELECT Url, hits FROM ".TABLE_PREFIX."links WHERE link_id=$_GET[view]";
28         $results = mysql_query($sql,$db);
29
30         if ($row = mysql_fetch_assoc($results)) { 
31                 if (!authenticate(AT_PRIV_LINKS, AT_PRIV_RETURN)) {
32
33                         $row['hits']++;
34                         $sql = "UPDATE ".TABLE_PREFIX."links SET hits=$row[hits] WHERE link_id=$_GET[view]";
35                         mysql_query($sql,$db);
36                 }
37
38                 //redirect
39                 header('Location: ' . $row['Url']);
40                 exit;
41         }
42 }
43
44 require (AT_INCLUDE_PATH.'header.inc.php');
45
46 if ($_GET['reset_filter']) {
47         unset($_GET);
48 }
49
50 $_GET['cat_parent_id'] = intval($_GET['cat_parent_id']);
51
52 //get appropriate categories
53 $categories = get_link_categories();
54
55 //ascending decscending columns...
56 $page_string = '';
57 $orders = array('asc' => 'desc', 'desc' => 'asc');
58 $cols   = array('LinkName' => 1, 'name' => 1, 'description' => 1);
59
60 if (isset($_GET['asc'])) {
61         $order = 'asc';
62         $col   = isset($cols[$_GET['asc']]) ? $_GET['asc'] : 'LinkName';
63 } else if (isset($_GET['desc'])) {
64         $order = 'desc';
65         $col   = isset($cols[$_GET['desc']]) ? $_GET['desc'] : 'LinkName';
66 } else {
67         // no order set
68         $order = 'asc';
69         $col   = 'LinkName';
70 }
71
72 //search
73 if ($_GET['search']) {
74         $_GET['search'] = trim($_GET['search']);
75         $page_string .= SEP.'search='.urlencode($_GET['search']);
76         $search = $addslashes($_GET['search']);
77         $search = str_replace(array('%','_'), array('\%', '\_'), $search);
78         $search = '%'.$search.'%';
79         $search = "((LinkName LIKE '$search') OR (description LIKE '$search'))";
80 } else {
81         $search = '1';
82 }
83
84 //view links of a child category
85 if ($_GET['cat_parent_id']) {
86     $children = get_child_categories ($_GET['cat_parent_id'], $categories);
87     $cat_sql = "C.cat_id IN ($children $_GET[cat_parent_id])";
88         $parent_id = intval($_GET['cat_parent_id']);
89 } else {
90     $cat_sql = '1';   
91     $parent_id = 0;     
92 }
93
94 //get links
95 $tmp_groups = implode(',', $_SESSION['groups']);
96
97 if (!empty($tmp_groups)) {
98         $sql = "SELECT * FROM ".TABLE_PREFIX."links L INNER JOIN ".TABLE_PREFIX."links_categories C USING (cat_id) WHERE ((owner_id=$_SESSION[course_id] AND owner_type=".LINK_CAT_COURSE.") OR (owner_id IN ($tmp_groups) AND owner_type=".LINK_CAT_GROUP.")) AND L.Approved=1 AND $search AND $cat_sql ORDER BY $col $order";
99 } else {
100         $sql = "SELECT * FROM ".TABLE_PREFIX."links L INNER JOIN ".TABLE_PREFIX."links_categories C USING (cat_id) WHERE (owner_id=$_SESSION[course_id] AND owner_type=".LINK_CAT_COURSE.") AND L.Approved=1 AND $search AND $cat_sql ORDER BY $col $order";
101 }
102 $result = mysql_query($sql, $db);
103 $num_results = mysql_num_rows($result);
104
105 ?>
106 <?php if ($num_results > 0 || isset($_GET['filter'])): ?>
107 <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
108
109 <div class="input-form">
110         <div class="row">
111                         <h3><?php echo _AT('results_found', $num_results); ?></h3>
112                 </div>
113
114         <div class="row">
115                         <label for="category_parent"><?php echo _AT('select_cat'); ?></label>
116                 <br />
117                         <?php if (!empty($categories)): ?>
118                                 <select name="cat_parent_id" id="category_parent"><?php
119                                                 if ($parent_id) {
120                                                         $current_cat_id = $parent_id;
121                                                         $exclude = false; /* don't exclude the children */
122                                                 } else {
123                                                         $current_cat_id = $cat_id;
124                                                         $exclude = true; /* exclude the children */
125                                                 }
126
127                                                 echo '<option value="0">&nbsp;&nbsp;&nbsp; '._AT('cats_all').' &nbsp;&nbsp;&nbsp;</option>';
128                                                 select_link_categories($categories, 0, $current_cat_id, FALSE);
129                                         ?>
130                                 </select>
131                         <?php endif; ?>
132                 </div>
133
134                 <div class="row">
135                         <label for="search"><?php echo _AT('search'); ?> (<?php echo _AT('title').', '._AT('description'); ?>)</label><br />
136                         <input type="text" name="search" id="search" size="20" value="<?php echo htmlspecialchars($_GET['search']); ?>" />
137                 </div>
138
139
140                 <div class="row buttons">
141                         <input type="submit" name="filter" value="<?php echo _AT('filter'); ?>" />
142                         <input type="submit" name="reset_filter" value="<?php echo _AT('reset_filter'); ?>" />
143                 </div>
144 </div>
145 </form>
146 <?php endif; ?>
147
148 <table class="data static" summary="" rules="cols">
149 <colgroup>
150         <?php if ($col == 'LinkName'): ?>
151                 <col class="sort" />
152                 <col span="2" />
153         <?php elseif($col == 'name'): ?>
154                 <col />
155                 <col class="sort" />
156                 <col />
157         <?php elseif($col == 'description'): ?>
158                 <col span="2" />
159                 <col class="sort" />
160         <?php endif; ?>
161 </colgroup>
162 <thead>
163 <tr>
164         <th scope="col"><a href="links/index.php?<?php echo $orders[$order]; ?>=LinkName<?php echo $page_string; ?>"><?php echo _AT('title');          ?></a></th>
165         <th scope="col"><a href="links/index.php?<?php echo $orders[$order]; ?>=name<?php echo $page_string; ?>"><?php echo _AT('category');           ?></a></th>
166         <th scope="col"><a href="links/index.php?<?php echo $orders[$order]; ?>=description<?php echo $page_string; ?>"><?php echo _AT('description'); ?></a></th>
167 </tr>
168 </thead>
169 <tbody>
170         <?php if ($row = mysql_fetch_assoc($result)) : ?>
171         <?php
172         do {
173                 ?>
174                 <tr onmousedown="document.form['m<?php echo $row['link_id']; ?>'].checked = true;">
175                         <td><a href="links/index.php?view=<?php echo $row['link_id']; ?>" target="_new" title="<?php echo AT_print($row['LinkName'], 'links.LinkName'); ?>"><?php echo AT_print($row['LinkName'], 'links.LinkName'); ?></a></td>
176                         <td><?php 
177                                 if (empty($row['name'])) {
178                                         $row['name'] = get_group_name($row['owner_id']);
179                                 }
180                                 echo AT_print($row['name'], 'links.name'); 
181                         ?></td>
182                         <td><?php echo AT_print($row['Description'], 'links.Description'); ?></td>
183                 </tr>
184         <?php 
185                 } while ($row = mysql_fetch_assoc($result)); ?>
186         <?php else: ?>
187                 <tr>
188                         <td colspan="3"><?php echo _AT('none_found'); ?></td>
189                 </tr>
190         <?php endif; ?>
191 </tbody>
192 </table>
193
194 <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>