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