remove old readme
[atutor.git] / docs / include / html / browse.inc.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12 if (!defined('AT_INCLUDE_PATH')) { exit; }
13 require(AT_INCLUDE_PATH.'../mods/_core/cats_categories/lib/admin_categories.inc.php');
14
15 $cats   = array();
16 $cats[0] = _AT('cats_uncategorized');
17
18 $sql = "SELECT cat_id, cat_name FROM ".TABLE_PREFIX."course_cats";
19 $result = mysql_query($sql,$db);
20 while($row = mysql_fetch_array($result)) {
21         $cats[$row['cat_id']] = $row['cat_name'];
22 }
23
24 if ($_GET['reset_filter']) { unset($_GET); }
25
26 $page_string = '';
27
28 if (isset($_GET['access']) && in_array($_GET['access'], array('public','private','protected'))) {
29         $page_string .= SEP.'access='.$_GET['access'];
30         $sql_access = "='{$_GET['access']}'";
31 } else {
32         $sql_access     = '<>-1';
33         $_GET['access'] = '';
34 }
35
36 if (isset($_GET['category']) && ($_GET['category'] > -1)) {
37         $_GET['category'] = intval($_GET['category']);
38         $page_string .= SEP.'category='.$_GET['category'];
39         $sql_category = '='.$_GET['category'];
40 } else {
41         $sql_category     = '<>-1';
42         $_GET['category'] = -1; // all (because 0 = uncategorized)
43 }
44
45 if (isset($_GET['include']) && $_GET['include'] == 'one') {
46         $checked_include_one = ' checked="checked"';
47         $page_string .= SEP.'include=one';
48 } else {
49         $_GET['include'] = 'all';
50         $checked_include_all = ' checked="checked"';
51         $page_string .= SEP.'include=all';
52 }
53
54 if (!empty($_GET['search'])) {
55         $page_string .= SEP.'search='.urlencode($stripslashes($_GET['search']));
56         $search = $addslashes($_GET['search']);
57         $search = explode(' ', $search);
58
59         if ($_GET['include'] == 'all') {
60                 $predicate = 'AND ';
61         } else {
62                 $predicate = 'OR ';
63         }
64
65         $sql_search = '';
66         foreach ($search as $term) {
67                 $term = trim($term);
68                 $term = str_replace(array('%','_'), array('\%', '\_'), $term);
69                 if ($term) {
70                         $term = '%'.$term.'%';
71                         $sql_search .= "((title LIKE '$term') OR (description LIKE '$term')) $predicate";
72                 }
73         }
74         $sql_search = '('.substr($sql_search, 0, -strlen($predicate)).')';
75 } else {
76         $sql_search = '1';
77 }
78
79 $sql    = "SELECT COUNT(course_id) AS cnt FROM ".TABLE_PREFIX."courses WHERE access $sql_access AND cat_id $sql_category AND $sql_search AND hide=0";
80 $result = mysql_query($sql, $db);
81 $row = mysql_fetch_assoc($result);
82 $num_results = $row['cnt'];
83
84 $sql    = "SELECT * FROM ".TABLE_PREFIX."courses WHERE access $sql_access AND cat_id $sql_category AND $sql_search AND hide=0 ORDER BY title";
85 $courses_result = mysql_query($sql, $db);
86
87 // add "enroll me" link if the user is not the course owner and is not enrolled
88 while ($row = mysql_fetch_assoc($courses_result)) {
89         if ($_SESSION['member_id'] > 0) {
90                 $sql    = "SELECT * FROM ".TABLE_PREFIX."course_enrollment WHERE member_id=$_SESSION[member_id] AND course_id=".$row['course_id'];
91                 $result = mysql_query($sql, $db);
92                 
93                 if ($row['access'] == 'private') {
94                         $enroll_link = '<a href="'.$_base_path.'users/private_enroll.php?course='.$row['course_id'].'">'. _AT('enroll_me').'</a>';
95                 } else {
96                         $enroll_link = '<a href="'.$_base_path.'enroll.php?course='.$row['course_id'].'">'. _AT('enroll_me').'</a>';
97                 }
98                 
99                 if (mysql_num_rows($result) == 0 && $_SESSION['member_id'] <> $row['member_id']) {
100                         $row['enroll_link'] = $enroll_link;
101                 } else if ($row['access'] == 'private') {
102                         $enrollment_row = mysql_fetch_assoc($result);
103                         if ($enrollment_row['approved'] == 'n') $row['enroll_link'] = $enroll_link;
104                 }
105         }
106         $courses_rows[] = $row;
107 }
108
109 // get the categories <select>, if there are any.
110 // we need ob_start/ob_clean, because select_categories() outputs directly.
111 // we do this so that if there are no categories, then the option doesn't appear.
112 ob_start();
113 select_categories(get_categories(), 0, $_GET['category'], false);
114 $categories_select = ob_get_contents();
115 ob_clean();
116
117 $has_categories = false;
118 if ($categories_select != '<option value="0"></option>') {
119         $has_categories = true;
120 }
121
122 if ($_SESSION['first_login']) {
123     $msg->addInfo(array('FIRST_PREFS', $_base_path.'users/pref_wizard/index.php'));
124 }
125
126 $savant->assign('cats', $cats);
127 $savant->assign('num_results', $num_results);
128 $savant->assign('has_categories', $has_categories);
129 $savant->assign('categories_select', $categories_select);
130 $savant->assign('checked_include_all', $checked_include_all);
131 $savant->assign('checked_include_one', $checked_include_one);
132 $savant->assign('courses_rows', $courses_rows);
133
134 $savant->display('users/browse.tmpl.php');
135
136 ?>