2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
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 /************************************************************************/
15 // NOTE! please see include/html/search.inc.php NOTE!
17 function score_cmp($a, $b) {
18 if ($a['score'] == $b['score']) {
21 return ($a['score'] > $b['score']) ? -1 : 1;
25 function get_search_result($words, $predicate, $course_id, &$num_found, &$total_score){
26 global $_pages, $moduleFactory;
28 $search_results = array();
29 $content_search_results = array();
30 $forums_search_results = array();
33 if (isset($_GET['search_within'])){
34 if ($_GET['search_within'] == 'content'){
35 $content_search_results = get_content_search_result($words, $predicate, $course_id, $total_score, $course_score);
36 $search_results = $content_search_results;
37 } elseif ($_GET['search_within'] == 'forums'){
38 $forums_search_results = get_forums_search_result($words, $predicate, $course_id, $total_score, $course_score);
39 // get all enabled modules
40 $modules = $moduleFactory->getModules(AT_MODULE_STATUS_ENABLED, 0, TRUE);
41 // if forum has been disabled, don't search in it.
42 if (isset($_SESSION['course_id']) && $_SESSION['course_id']==0){
43 $search_results = $forums_search_results;
44 } elseif (isset($modules['_standard/forums'])){
45 $search_results = $forums_search_results;
48 $content_search_results = get_content_search_result($words, $predicate, $course_id, $total_score, $course_score);
49 $forums_search_results = get_forums_search_result($words, $predicate, $course_id, $total_score, $course_score);
50 $search_results = array_merge($content_search_results, $forums_search_results);
53 if ((count($search_results) == 0) && $course_score && ($_GET['display_as'] != 'pages')) {
56 $num_found += count($search_results);
58 return $search_results;
61 function get_content_search_result($words, $predicate, $course_id, &$total_score, &$course_score) {
62 global $addslashes, $db, $highlight_system_courses, $strlen, $substr, $strtolower;
64 $search_results = array();
65 $lower_words = array();
67 $predicate = " $predicate "; // either 'AND' or 'OR'
69 $words = trim($words);
70 $words = explode(' ',$words);
71 $words = array_values(array_diff(array_unique($words), array('')));
72 $num_words = count($words);
76 for ($i=0; $i<$num_words; $i++) {
77 $lower_words[$i] = $strtolower($words[$i]);
80 $words_sql .= $predicate;
82 $words[$i] = $addslashes($words[$i]);
83 $words_sql .= ' (C.title LIKE "%'.$words[$i].'%" OR C.text LIKE "%'.$words[$i].'%" OR C.keywords LIKE "%'.$words[$i].'%")';
85 /* search through the course title and description keeping track of its total */
86 $course_score += 15 * substr_count($strtolower($highlight_system_courses[$course_id]['title']), $lower_words[$i]);
87 $course_score += 12 * substr_count($strtolower($highlight_system_courses[$course_id]['description']), $lower_words[$i]);
89 $highlight_system_courses[$course_id]['title'] = highlight($highlight_system_courses[$course_id]['title'], $words[$i]);
90 $highlight_system_courses[$course_id]['description'] = highlight($highlight_system_courses[$course_id]['description'], $words[$i]);
96 $sql = 'SELECT C.last_modified, C.course_id, C.content_id, C.title, C.text, C.keywords FROM '.TABLE_PREFIX.'content AS C WHERE C.course_id='.$course_id;
97 $sql .= ' AND ('.$words_sql.') LIMIT 200';
99 $result = mysql_query($sql, $db);
100 while($row = mysql_fetch_assoc($result)) {
103 $row['title'] = strip_tags($row['title']);
104 $row['text'] = strip_tags($row['text']);
106 $lower_title = $strtolower($row['title']);
107 $lower_text = $strtolower($row['text']);
108 $lower_keywords = $strtolower($row['keywords']);
110 if ($strlen($row['text']) > 270) {
111 $row['text'] = $substr($row['text'], 0, 268).'...';
114 for ($i=0; $i<$num_words; $i++) {
115 $score += 8 * substr_count($lower_keywords, $lower_words[$i]); /* keywords are weighed more */
116 $score += 4 * substr_count($lower_title, $lower_words[$i]); /* titles are weighed more */
117 $score += 1 * substr_count($lower_text, $lower_words[$i]);
119 $row['title'] = highlight($row['title'], $words[$i]);
120 $row['text'] = highlight($row['text'], $words[$i]);
121 $row['keywords'] = highlight($row['keywords'], $words[$i]);
125 $score += $course_score;
127 $row['score'] = $score;
128 $search_results[] = $row;
130 $total_score += $score;
133 if ($total_score == 0) {
134 $total_score = $course_score;
137 return $search_results;
141 * Get forum search results
142 * @author Harris Wong
144 function get_forums_search_result($words, $predicate, $course_id, &$total_score, &$course_score) {
145 global $addslashes, $db, $highlight_system_courses, $strlen, $substr, $strtolower;
147 $search_results = array();
148 $lower_words = array();
150 $predicate = " $predicate "; // either 'AND' or 'OR'
152 $words = trim($words);
153 $words = explode(' ',$words);
154 $words = array_values(array_diff(array_unique($words), array('')));
155 $num_words = count($words);
159 for ($i=0; $i<$num_words; $i++) {
160 $lower_words[$i] = $strtolower($words[$i]);
163 $words_sql .= $predicate;
165 $words[$i] = $addslashes($words[$i]);
166 $words_sql .= ' (course_group_forums.title LIKE "%'.$words[$i].'%" OR T.subject LIKE "%'.$words[$i].'%" OR T.body LIKE "%'.$words[$i].'%")';
168 /* search through the course title and description keeping track of its total */
169 $course_score += 15 * substr_count($strtolower($highlight_system_courses[$course_id]['title']), $lower_words[$i]);
170 $course_score += 12 * substr_count($strtolower($highlight_system_courses[$course_id]['description']), $lower_words[$i]);
178 // Wants to get course forums + "my" group forums
179 //if the search is performed outside of a course, do not search in any group forums
180 // UNION on course_forums and group_forums
181 // TODO: Simplify the query.
182 ((isset($_SESSION['is_admin']) && $_SESSION['is_admin'] > 0) ? $is_admin_string = '1 OR ' : $is_admin_string = '');
183 (isset($_SESSION['member_id']) ? $member_id = $_SESSION['member_id'] : $member_id = 0);
184 $sql = 'SELECT course_group_forums.title AS forum_title, course_group_forums.course_id, T.* FROM '.TABLE_PREFIX.'forums_threads T RIGHT JOIN ';
187 $sql .= '( SELECT forum_id, course_id, title, description, num_topics, num_posts, last_post, mins_to_edit FROM '.TABLE_PREFIX.'forums_courses ';
188 $sql .= ' NATURAL JOIN '.TABLE_PREFIX.'forums WHERE course_id='.$course_id;
193 $sql .= ' SELECT forum_id, course_id, title, description, num_topics, num_posts, last_post, mins_to_edit FROM '.TABLE_PREFIX.'forums_groups NATURAL JOIN ';
194 $sql .= ' (SELECT forum_id, num_topics, num_posts, last_post, mins_to_edit FROM '.TABLE_PREFIX.'forums) AS f NATURAL JOIN ';
195 $sql .= ' '.TABLE_PREFIX.'groups_members NATURAL JOIN ';
196 $sql .= ' (SELECT g.*, gt.course_id FROM '.TABLE_PREFIX.'groups g INNER JOIN '.TABLE_PREFIX.'groups_types gt USING (type_id) WHERE ';
197 $sql .= ' course_id='.$course_id.') AS group_course WHERE '.$is_admin_string .'member_id='.$member_id;
198 $sql .= ') AS course_group_forums ';
200 $sql .= 'USING (forum_id) ';
201 $sql .= 'WHERE ' . $words_sql;
203 $result = mysql_query($sql, $db);
204 while($row = mysql_fetch_assoc($result)) {
207 $row['forum_title'] = strip_tags($row['forum_title']);
208 $row['subject'] = strip_tags($row['subject']);
209 $row['body'] = strip_tags($row['body']);
211 $lower_forum_title = $strtolower($row['forum_title']);
212 $lower_subject = $strtolower($row['subject']);
213 $lower_body = $strtolower($row['body']);
214 $num_posts = intval($row['num_comments']);
216 if ($strlen($row['body']) > 270) {
217 $row['body'] = $substr($row['body'], 0, 268).'...';
220 for ($i=0; $i<$num_words; $i++) {
221 $score += 8 * substr_count($lower_forum_title, $lower_words[$i]); /* forum's title are weighed more */
222 $score += 4 * substr_count($lower_subject, $lower_words[$i]); /* thread subject are weighed more */
223 $score += 2 * substr_count($lower_body, $lower_words[$i]);
224 $score += 1 * $num_posts;
226 $row['forum_title'] = highlight($row['forum_title'], $words[$i]);
227 $row['subject'] = highlight($row['subject'], $words[$i]);
228 $row['body'] = highlight($row['body'], $words[$i]);
232 $score += $course_score;
234 $row['score'] = $score;
235 $search_results[] = $row;
237 $total_score += $score;
240 return $search_results;
245 // My Courses - All courses you're enrolled in (including hidden)
246 function get_my_courses($member_id) {
251 $sql = "SELECT course_id FROM ".TABLE_PREFIX."course_enrollment WHERE member_id=$member_id AND (approved='y' OR approved='a')";
252 $result = mysql_query($sql, $db);
253 while ($row = mysql_fetch_assoc($result)) {
254 $list[] = $row['course_id']; // list contains all the Course IDs
261 // All courses (display hidden too if you're enrolled in it)
262 function get_all_courses($member_id) {
263 global $system_courses, $db;
267 $num_courses = count($system_courses);
269 // add all the courses that are not hidden,then find the hidden courses that you're enrolled in and then add that to array
270 foreach ($system_courses as $course_id => $course_info) {
271 if (!$course_info['hide']) {
272 $list[] = $course_id;
276 // if there aren't any hidden courses:
277 if (count($system_courses) == count($list)) {
281 if ($_SESSION['valid_user']) {
282 $my_courses = implode(',', get_my_courses($member_id));
283 $sql = "SELECT course_id FROM ".TABLE_PREFIX."courses WHERE hide=1 AND course_id IN (0, $my_courses)";
284 $result = mysql_query($sql, $db);
285 while ($row = mysql_fetch_assoc($result)) {
286 $list[] = $row['course_id'];
292 function print_search_pages($result) {
295 foreach ($result as $items) {
296 uasort($result, 'score_cmp');
298 echo '<h5>' . $count . '. ';
300 if(isset($items['forum_title'])){
302 if ($_SESSION['course_id'] != $items['course_id']) {
303 echo '<a href="bounce.php?course='.$items['course_id'].SEP.'p='.urlencode('forum/view.php?fid='.$items['forum_id'].SEP.'pid='.$items['post_id'].SEP.'words='.$_GET['words']).'">'.$items['forum_title'].' - '.$items['subject'].'</a> ';
305 echo '<a href="'.url_rewrite('mods/_standard/forums/forum/view.php?fid='.$items['forum_id'].SEP.'pid='.$items['post_id'].SEP.'words='.$_GET['words']).'">'.$items['forum_title'].' - '.$items['subject'].'</a> ';
309 echo '<p><small>'.$items['body'];
312 if ($_SESSION['course_id'] != $items['course_id']) {
313 echo '<a href="bounce.php?course='.$items['course_id'].SEP.'p='.urlencode('content.php?cid='.$items['content_id'].SEP.'words='.$_GET['words']).'">'.$items['title'].'</a> ';
315 echo '<a href="'.url_rewrite('content.php?cid='.$items['content_id'].SEP.'words='.$_GET['words']).'">'.$items['title'].'</a> ';
319 echo '<p><small>'.$items['text'];
322 echo '<br /><small class="search-info">[<strong>'._AT('keywords').':</strong> ';
323 if (isset($items['keywords'])) {
324 echo $items['keywords'];
326 echo '<strong>'._AT('none').'</strong>';
328 echo '. <strong>'._AT('author').':</strong> ';
329 if (isset($items['member_id'])) {
330 echo AT_print(get_display_name($items['member_id']), 'members.login');
332 echo '<strong>'._AT('none').'</strong>';
334 echo '. <strong>'._AT('updated').':</strong> ';
335 echo AT_date(_AT('inbox_date_format'), (isset($items['last_modified']) && $items['last_modified']!='')?$items['last_modified']:$items['last_comment'], AT_DATE_MYSQL_DATETIME);
339 echo '</small></p>'."\n";