fa01c50cbebf734c63699025d74430f3bccf1d6f
[atutor.git] / docs / include / html / search.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 // $Id$\r
14 \r
15 // NOTE! please see include/lib/search.inc.php NOTE!\r
16 \r
17 //initialize variables\r
18 $checked_include_one                    = '';\r
19 $checked_include_all                    = '';\r
20 $checked_find_in_course                 = '';\r
21 $checked_find_in_my_courses             = '';\r
22 $checked_find_in_all_courses    = '';\r
23 $checked_sw_all                                 = '';\r
24 $checked_sw_content                             = '';\r
25 $checked_sw_forums                              = '';\r
26 $checked_display_as_courses             = '';\r
27 $checked_display_as_pages               = '';\r
28 $checked_display_as_summaries   = '';\r
29 \r
30 /* some error checking can go here: */\r
31 if (isset($_GET['search'])) {\r
32         $_GET['words'] = stripslashes($addslashes($_GET['words']));\r
33         $_GET['words'] = str_replace(array('"', '\''), '', $_GET['words']);\r
34         if ($_GET['include'] == 'all') {\r
35                 $checked_include_all = ' checked="checked"';\r
36         } else {\r
37                 $_GET['include'] = 'one';\r
38                 // 'one'\r
39                 $checked_include_one = ' checked="checked"';\r
40         }\r
41 \r
42         if ($_GET['find_in'] == 'this') {\r
43                 $checked_find_in_course = ' checked="checked"';\r
44         } else if ($_GET['find_in'] == 'my') {\r
45                 $checked_find_in_my_courses = ' checked="checked"';\r
46         } else {\r
47                 $_GET['find_in'] = 'all';\r
48                 // 'all'\r
49                 $checked_find_in_all_courses = ' checked="checked"';\r
50         }\r
51 \r
52         if ($_GET['display_as'] == 'pages') {\r
53                 $checked_display_as_pages = ' checked="checked"';\r
54         } else if ($_GET['display_as'] == 'courses') {\r
55                 $checked_display_as_courses = ' checked="checked"';\r
56         } else {\r
57                 $_GET['display_as'] = 'summaries';\r
58                 // 'summaries'\r
59                 $checked_display_as_summaries = ' checked="checked"';\r
60         }\r
61 \r
62         // search within options\r
63         if ($_GET['search_within']=='content'){\r
64                 $checked_sw_content = ' checked="checked"';\r
65         } elseif ($_GET['search_within']=='forums') {\r
66                 $checked_sw_forums = ' checked="checked"';\r
67         } else {\r
68                 $checked_sw_all = ' checked="checked"';\r
69         }\r
70 \r
71 } else {\r
72         // default values:\r
73         $checked_include_all      = ' checked="checked"';\r
74         $checked_sw_all                   = ' checked="checked"';\r
75 \r
76         if ($_SESSION['course_id'] > 0) {\r
77                 $checked_find_in_course   = ' checked="checked"';\r
78                 $checked_display_as_pages = ' checked="checked"';\r
79         } else if ($_SESSION['valid_user']) {\r
80                 $checked_find_in_my_courses = ' checked="checked"';\r
81                 $checked_display_as_courses = ' checked="checked"';\r
82         } else {\r
83                 $checked_find_in_all_courses  = ' checked="checked"';\r
84                 $checked_display_as_summaries = ' checked="checked"';\r
85         }\r
86 }\r
87 if (isset($_GET['search']) && !$_GET['words']) {\r
88         $msg->printErrors('SEARCH_TERM_REQUIRED');\r
89         $_GET = array();\r
90 }\r
91 \r
92 ?>\r
93 \r
94 <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>#search_results" name="form">\r
95 <input type="hidden" name="search" value="1" />\r
96 <div class="input-form">\r
97         <div class="row">\r
98                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="keywords"><?php echo _AT('search_words'); ?></label><br />\r
99                 <input type="text" name="words" size="30" id="keywords" value="<?php echo $_GET['words']; ?>" />\r
100         </div>\r
101 \r
102         <div class="row">\r
103                 <?php echo _AT('search_match'); ?><br />\r
104                 <input type="radio" name="include" value="all" id="all" <?php echo $checked_include_all; ?> /><label for="all"><?php echo _AT('search_all_words'); ?></label><br />\r
105                 <input type="radio" name="include" value="one" id="one" <?php echo $checked_include_one; ?> /><label for="one"><?php echo _AT('search_any_word'); ?></label>\r
106         </div>\r
107         \r
108         <div class="row">\r
109                 <?php echo _AT('find_results_in'); ?><br />\r
110                                 <?php if ($_SESSION['course_id'] > 0) : ?>\r
111                                         <input type="radio" name="find_in" value="this" id="f1" <?php echo $checked_find_in_course; ?> /><label for="f1"><?php echo _AT('this_course_only'); ?></label><br />\r
112                                 <?php endif; ?>\r
113 \r
114                                 <?php if ($_SESSION['valid_user'] && ($_SESSION['course_id'] > -1)) : ?>\r
115                                         <input type="radio" name="find_in" value="my" id="f2" <?php echo $checked_find_in_my_courses; ?> /><label for="f2"><?php echo _AT('my_enrolled_courses'); ?></label><br />\r
116                                 <?php endif; ?>\r
117 \r
118                                 <input type="radio" name="find_in" value="all" id="f3" <?php echo $checked_find_in_all_courses; ?> /><label for="f3"><?php echo _AT('all_available_courses'); ?></label>\r
119         </div>\r
120 \r
121         <div class="row">\r
122                 <?php echo _AT('search_in'); ?><br />\r
123                         <input type="radio" name="search_within" value="all" id="sw_all" <?php echo $checked_sw_all; ?> /><label for="sw_all"><?php echo _AT('all'); ?></label> \r
124                         <input type="radio" name="search_within" value="content" id="sw_content" <?php echo $checked_sw_content; ?> /><label for="sw_content"><?php echo _AT('content'); ?></label>\r
125                         <input type="radio" name="search_within" value="forums" id="sw_forums" <?php echo $checked_sw_forums; ?> /><label for="sw_forums"><?php echo _AT('forums'); ?></label>  \r
126         </div>\r
127 \r
128         <div class="row">\r
129                 <?php echo _AT('display'); ?><br />\r
130                 <input type="radio" name="display_as" value="pages" id="d1" <?php echo $checked_display_as_pages; ?> /><label for="d1"><?php echo _AT('as_individual_content'); ?></label><br />\r
131 \r
132                 <input type="radio" name="display_as" value="courses" id="d2" <?php echo $checked_display_as_courses; ?> /><label for="d2"><?php echo _AT('grouped_by_course'); ?></label><br />\r
133 \r
134                 <input type="radio" name="display_as" value="summaries" id="d3" <?php echo $checked_display_as_summaries; ?> /><label for="d3"><?php echo _AT('course_summaries'); ?></label><br /><br />\r
135         </div>\r
136 \r
137         <div class="row buttons">\r
138                 <input type="submit" name="search" value="<?php echo _AT('search'); ?>" />\r
139         </div>\r
140 </div>\r
141 </form>\r
142 \r
143 <?php\r
144 \r
145 /* search results go down here: */\r
146 if (isset($_GET['search']) && isset($_GET['words'])) {\r
147         $search_results   = array(); // the content search results\r
148         $search_totals    = array(); // total score per course\r
149         $num_found        = 0;       // total results found\r
150         $total_score      = 0;       // total score (temporary per course)\r
151         $results_per_page = 10;      // number of results per page\r
152         $highlight_system_courses = $system_courses;\r
153 \r
154         if ($_GET['include'] == 'all') {\r
155                 $predicate = 'AND';\r
156         } else {\r
157                 $predicate = 'OR';\r
158         }\r
159 \r
160         if (($_GET['find_in'] == 'this') && ($_SESSION['course_id'] > 0)) {\r
161                 if ($_GET['display_as'] == 'pages') {\r
162                         $search_results = get_search_result($_GET['words'], $predicate, $_SESSION['course_id'], $num_found, $total_score);\r
163                 } else { // 'courses' or 'summaries' :\r
164                         $search_results[$_SESSION['course_id']] = get_search_result($_GET['words'], $predicate, $_SESSION['course_id'], $num_found, $total_score);\r
165                         $search_totals[$_SESSION['course_id']]  = $total_score;\r
166                 }\r
167         } else {\r
168                 if ($_GET['find_in'] == 'my') {\r
169                         $my_courses = get_my_courses($_SESSION['member_id']);\r
170                 } else { // $_GET['find_in'] == 'all' (or other). always safe to perform.\r
171                         $my_courses = get_all_courses($_SESSION['member_id']);\r
172                 }\r
173 \r
174                 foreach ($my_courses as $tmp_course_id) {\r
175                         if ($_GET['display_as'] == 'pages') {\r
176                                 // merge all the content results together\r
177                                 $search_results = array_merge($search_results, get_search_result($_GET['words'], $predicate, $tmp_course_id, $num_found, $total_score));\r
178                         } else {\r
179                                 // group by Course\r
180                                 $total_score = 0;\r
181                                 $search_results[$tmp_course_id] = get_search_result($_GET['words'], $predicate, $tmp_course_id, $num_found, $total_score);\r
182                                 if ($total_score) {\r
183                                         $search_totals[$tmp_course_id]  = $total_score;\r
184                                 } // else: no content found in this course.\r
185                         }\r
186                 }\r
187         }\r
188 \r
189         if ($_GET['display_as'] == 'summaries') {\r
190                 $num_found = count($search_totals);\r
191         }\r
192 \r
193         echo '<a name="search_results"></a><h3>'.$num_found.' '._AT('search_results').'</h3>';\r
194 \r
195         if (!$num_found) {\r
196                 $msg->printInfos('NO_SEARCH_RESULTS');\r
197                 require(AT_INCLUDE_PATH.'footer.inc.php');\r
198                 exit;\r
199         } else if (!$num_found && count($search_totals)) {\r
200                 // meaning: no pages were found, just courses:\r
201                 $num_found = count($search_totals);\r
202         }\r
203 \r
204         $num_pages = ceil($num_found / $results_per_page);\r
205                         \r
206         $page = isset($_GET['p']) ? intval($_GET['p']) : 0;\r
207         if (!$page) {\r
208                 $page = 1;\r
209         }\r
210                         \r
211         $count = (($page-1) * $results_per_page) + 1;\r
212 \r
213         $pages_text = '<div class="paging">';\r
214         $pages_text .= '<ul>';\r
215         for ($i=1; $i<=$num_pages; $i++) {\r
216                 $pages_text .= '<li>';\r
217                 if ($i == $page) {\r
218                         $pages_text .= '<a class="current" href="'.$_SERVER['PHP_SELF'].'?search=1'.SEP.'words='.urlencode($_GET['words']).SEP.'include='.$_GET['include'].SEP.'find_in='.$_GET['find_in'].SEP.'search_within='.$_GET['search_within'].SEP.'display_as='.$_GET['display_as'].SEP.'p='.$i.'#search_results"><strong>'.$i.'</strong></a>';\r
219                 } else {\r
220                         $pages_text .= '<a href="'.$_SERVER['PHP_SELF'].'?search=1'.SEP.'words='.urlencode($_GET['words']).SEP.'include='.$_GET['include'].SEP.'find_in='.$_GET['find_in'].SEP.'search_within='.$_GET['search_within'].SEP.'display_as='.$_GET['display_as'].SEP.'p='.$i.'#search_results">'.$i.'</a>';\r
221                 }\r
222                 $pages_text .= '</li>';\r
223         }\r
224         $pages_text .= '</ul>';\r
225         $pages_text .= '</div>';\r
226 \r
227         echo $pages_text;\r
228 \r
229         if ($_GET['display_as'] == 'pages') {\r
230                 uasort($search_results, 'score_cmp');\r
231 \r
232                 $search_results = array_slice($search_results, ($page-1)*$results_per_page, $results_per_page);\r
233 \r
234                 echo '<div class="results">';\r
235                 print_search_pages($search_results);\r
236                 echo '</div>'."\n";\r
237         } else {\r
238                 arsort($search_totals);\r
239                 reset($search_totals);\r
240 \r
241                 $skipped        = 0; // number that have been skipped\r
242                 $printed_so_far = 0; // number printed on this page\r
243 \r
244                 foreach ($search_totals as $tmp_course_id => $score) {\r
245                         $total_here = 0;\r
246                         if ($printed_so_far == $results_per_page) {\r
247                                 break;\r
248                         }\r
249                         \r
250                         $increment_count = false;\r
251                         if (count($search_results[$tmp_course_id]) && ($_GET['display_as'] == 'courses')) {\r
252                                 uasort($search_results[$tmp_course_id], 'score_cmp');\r
253                                 reset($search_results[$tmp_course_id]);\r
254 \r
255                                 $num_available = count($search_results[$tmp_course_id]); // total number available for this course\r
256                 \r
257                                 if ($printed_so_far == $results_per_page) {\r
258                                         break;\r
259                                 }\r
260 \r
261                                 if ($skipped < $count) {\r
262                                         // this course is being truncated\r
263                                         // implies that it's at the start of the page\r
264                                         $start = ($page -1) * $results_per_page - $skipped;\r
265 \r
266                                         $total_here = count($search_results[$tmp_course_id]);\r
267 \r
268                                         $search_results[$tmp_course_id] = array_slice($search_results[$tmp_course_id], $start, $results_per_page - $printed_so_far);\r
269 \r
270                                         $num_printing = count($search_results[$tmp_course_id]);\r
271 \r
272                                         $printed_so_far += $num_printing;\r
273                                         $skipped += ($num_available - $num_printing);\r
274 \r
275                                         if ($num_printing == 0) {\r
276                                                 continue;\r
277                                         }\r
278                                         $increment_count = true;\r
279                                 }\r
280                         } else {\r
281                                 if ($printed_so_far == $results_per_page) {\r
282                                         break;\r
283                                 }\r
284 \r
285                                 $total_here = count($search_results[$tmp_course_id]);\r
286                                 if (($total_here == 0) || ($_GET['display_as'] == 'summaries')) {\r
287                                         if ($skipped < ($page-1) * $results_per_page) {\r
288                                                 $skipped++;\r
289                                                 continue;\r
290                                         }\r
291                                         $printed_so_far ++;\r
292                                         $increment_count = true;\r
293                                 } else {\r
294                                         $printed_so_far += $total_here;\r
295                                 }\r
296                         }\r
297                         echo '<h5 class="search-results"> '._AT('results_from', '<a href="bounce.php?course='.$tmp_course_id.'">'.$highlight_system_courses[$tmp_course_id]['title'] .'</a>').' - '._AT('pages_found', $total_here) . '</h5>';\r
298 \r
299 \r
300                         echo '<p class="search-description">';\r
301                         if ($highlight_system_courses[$tmp_course_id]['description']) {\r
302                                 echo $highlight_system_courses[$tmp_course_id]['description'];\r
303                         } else {\r
304                                 echo '<strong>'._AT('no_description').'</strong>';\r
305                         }\r
306 \r
307                         echo '<br /><small class="search-info">[<strong>'._AT('Access').':</strong> ';\r
308 \r
309                         switch ($highlight_system_courses[$tmp_course_id]['access']){\r
310                                 case 'public':\r
311                                         echo _AT('public');\r
312                                         break;\r
313                                 case 'protected':\r
314                                         echo _AT('protected');\r
315                                         break;\r
316                                 case 'private':\r
317                                         echo _AT('private');\r
318                                         break;\r
319                         }\r
320                         $language =& $languageManager->getLanguage($highlight_system_courses[$tmp_course_id]['primary_language']);\r
321 \r
322                         echo '. <strong>'._AT('primary_language').':</strong> ' . $language->getTranslatedName();\r
323                         \r
324                         echo ']</small>';\r
325 \r
326                         echo '</p>';\r
327 \r
328                         if ($_GET['display_as'] != 'summaries') {\r
329                                 echo '<div class="results">';\r
330                                 print_search_pages($search_results[$tmp_course_id]);\r
331                                 echo '</div>';\r
332                         }                       \r
333                 \r
334                 }\r
335         }\r
336 \r
337         echo $pages_text;       \r
338 }\r
339 \r
340 ?>