PageRenderTime 29ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/include/lib/search.inc.php

https://github.com/harriswong/ATutor
PHP | 344 lines | 239 code | 71 blank | 34 comment | 50 complexity | da400bad673b4899ec053cf3f2935de9 MD5 | raw file
  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. // NOTE! please see include/html/search.inc.php NOTE!
  15. function score_cmp($a, $b) {
  16. if ($a['score'] == $b['score']) {
  17. return 0;
  18. }
  19. return ($a['score'] > $b['score']) ? -1 : 1;
  20. }
  21. function get_search_result($words, $predicate, $course_id, &$num_found, &$total_score){
  22. global $_pages, $moduleFactory;
  23. $search_results = array();
  24. $content_search_results = array();
  25. $forums_search_results = array();
  26. $course_score = 0;
  27. if (isset($_GET['search_within'])){
  28. if ($_GET['search_within'] == 'content'){
  29. $content_search_results = get_content_search_result($words, $predicate, $course_id, $total_score, $course_score);
  30. $search_results = $content_search_results;
  31. } elseif ($_GET['search_within'] == 'forums'){
  32. $forums_search_results = get_forums_search_result($words, $predicate, $course_id, $total_score, $course_score);
  33. // get all enabled modules
  34. $modules = $moduleFactory->getModules(AT_MODULE_STATUS_ENABLED, 0, TRUE);
  35. // if forum has been disabled, don't search in it.
  36. if (isset($_SESSION['course_id']) && $_SESSION['course_id']==0){
  37. $search_results = $forums_search_results;
  38. } elseif (isset($modules['_standard/forums'])){
  39. $search_results = $forums_search_results;
  40. }
  41. } else {
  42. $content_search_results = get_content_search_result($words, $predicate, $course_id, $total_score, $course_score);
  43. $forums_search_results = get_forums_search_result($words, $predicate, $course_id, $total_score, $course_score);
  44. $search_results = array_merge($content_search_results, $forums_search_results);
  45. }
  46. if ((count($search_results) == 0) && $course_score && ($_GET['display_as'] != 'pages')) {
  47. $num_found++;
  48. }
  49. $num_found += count($search_results);
  50. }
  51. return $search_results;
  52. }
  53. function get_content_search_result($words, $predicate, $course_id, &$total_score, &$course_score) {
  54. global $addslashes, $db, $highlight_system_courses, $strlen, $substr, $strtolower;
  55. $search_results = array();
  56. $lower_words = array();
  57. $predicate = " $predicate "; // either 'AND' or 'OR'
  58. $words = trim($words);
  59. $words = explode(' ',$words);
  60. $words = array_values(array_diff(array_unique($words), array('')));
  61. $num_words = count($words);
  62. $course_score = 0;
  63. $words_sql = '';
  64. for ($i=0; $i<$num_words; $i++) {
  65. $lower_words[$i] = $strtolower($words[$i]);
  66. if ($words_sql) {
  67. $words_sql .= $predicate;
  68. }
  69. $words[$i] = $addslashes($words[$i]);
  70. $words_sql .= ' (C.title LIKE "%'.$words[$i].'%" OR C.text LIKE "%'.$words[$i].'%" OR C.keywords LIKE "%'.$words[$i].'%")';
  71. /* search through the course title and description keeping track of its total */
  72. $course_score += 15 * substr_count($strtolower($highlight_system_courses[$course_id]['title']), $lower_words[$i]);
  73. $course_score += 12 * substr_count($strtolower($highlight_system_courses[$course_id]['description']), $lower_words[$i]);
  74. $highlight_system_courses[$course_id]['title'] = highlight($highlight_system_courses[$course_id]['title'], $words[$i]);
  75. $highlight_system_courses[$course_id]['description'] = highlight($highlight_system_courses[$course_id]['description'], $words[$i]);
  76. }
  77. if (!$words_sql) {
  78. return;
  79. }
  80. $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;
  81. $sql .= ' AND ('.$words_sql.') LIMIT 200';
  82. $result = mysql_query($sql, $db);
  83. while($row = mysql_fetch_assoc($result)) {
  84. $score = 0;
  85. $row['title'] = strip_tags($row['title']);
  86. $row['text'] = strip_tags($row['text']);
  87. $lower_title = $strtolower($row['title']);
  88. $lower_text = $strtolower($row['text']);
  89. $lower_keywords = $strtolower($row['keywords']);
  90. if ($strlen($row['text']) > 270) {
  91. $row['text'] = $substr($row['text'], 0, 268).'...';
  92. }
  93. for ($i=0; $i<$num_words; $i++) {
  94. $score += 8 * substr_count($lower_keywords, $lower_words[$i]); /* keywords are weighed more */
  95. $score += 4 * substr_count($lower_title, $lower_words[$i]); /* titles are weighed more */
  96. $score += 1 * substr_count($lower_text, $lower_words[$i]);
  97. $row['title'] = highlight($row['title'], $words[$i]);
  98. $row['text'] = highlight($row['text'], $words[$i]);
  99. $row['keywords'] = highlight($row['keywords'], $words[$i]);
  100. }
  101. if ($score != 0) {
  102. $score += $course_score;
  103. }
  104. $row['score'] = $score;
  105. $search_results[] = $row;
  106. $total_score += $score;
  107. }
  108. if ($total_score == 0) {
  109. $total_score = $course_score;
  110. }
  111. return $search_results;
  112. }
  113. /*
  114. * Get forum search results
  115. * @author Harris Wong
  116. */
  117. function get_forums_search_result($words, $predicate, $course_id, &$total_score, &$course_score) {
  118. global $addslashes, $db, $highlight_system_courses, $strlen, $substr, $strtolower;
  119. $search_results = array();
  120. $lower_words = array();
  121. $predicate = " $predicate "; // either 'AND' or 'OR'
  122. $words = trim($words);
  123. $words = explode(' ',$words);
  124. $words = array_values(array_diff(array_unique($words), array('')));
  125. $num_words = count($words);
  126. $course_score = 0;
  127. $words_sql = '';
  128. for ($i=0; $i<$num_words; $i++) {
  129. $lower_words[$i] = $strtolower($words[$i]);
  130. if ($words_sql) {
  131. $words_sql .= $predicate;
  132. }
  133. $words[$i] = $addslashes($words[$i]);
  134. $words_sql .= ' (course_group_forums.title LIKE "%'.$words[$i].'%" OR T.subject LIKE "%'.$words[$i].'%" OR T.body LIKE "%'.$words[$i].'%")';
  135. /* search through the course title and description keeping track of its total */
  136. $course_score += 15 * substr_count($strtolower($highlight_system_courses[$course_id]['title']), $lower_words[$i]);
  137. $course_score += 12 * substr_count($strtolower($highlight_system_courses[$course_id]['description']), $lower_words[$i]);
  138. }
  139. if (!$words_sql) {
  140. return;
  141. }
  142. //forums sql
  143. // Wants to get course forums + "my" group forums
  144. //if the search is performed outside of a course, do not search in any group forums
  145. // UNION on course_forums and group_forums
  146. // TODO: Simplify the query.
  147. ((isset($_SESSION['is_admin']) && $_SESSION['is_admin'] > 0) ? $is_admin_string = '1 OR ' : $is_admin_string = '');
  148. (isset($_SESSION['member_id']) ? $member_id = $_SESSION['member_id'] : $member_id = 0);
  149. $sql = 'SELECT course_group_forums.title AS forum_title, course_group_forums.course_id, T.* FROM '.TABLE_PREFIX.'forums_threads T RIGHT JOIN ';
  150. //course forums
  151. $sql .= '( SELECT forum_id, course_id, title, description, num_topics, num_posts, last_post, mins_to_edit FROM '.TABLE_PREFIX.'forums_courses ';
  152. $sql .= ' NATURAL JOIN '.TABLE_PREFIX.'forums WHERE course_id='.$course_id;
  153. $sql .= ' UNION ';
  154. //group forums
  155. $sql .= ' SELECT forum_id, course_id, title, description, num_topics, num_posts, last_post, mins_to_edit FROM '.TABLE_PREFIX.'forums_groups NATURAL JOIN ';
  156. $sql .= ' (SELECT forum_id, num_topics, num_posts, last_post, mins_to_edit FROM '.TABLE_PREFIX.'forums) AS f NATURAL JOIN ';
  157. $sql .= ' '.TABLE_PREFIX.'groups_members NATURAL JOIN ';
  158. $sql .= ' (SELECT g.*, gt.course_id FROM '.TABLE_PREFIX.'groups g INNER JOIN '.TABLE_PREFIX.'groups_types gt USING (type_id) WHERE ';
  159. $sql .= ' course_id='.$course_id.') AS group_course WHERE '.$is_admin_string .'member_id='.$member_id;
  160. $sql .= ') AS course_group_forums ';
  161. $sql .= 'USING (forum_id) ';
  162. $sql .= 'WHERE ' . $words_sql;
  163. $result = mysql_query($sql, $db);
  164. while($row = mysql_fetch_assoc($result)) {
  165. $score = 0;
  166. $row['forum_title'] = strip_tags($row['forum_title']);
  167. $row['subject'] = strip_tags($row['subject']);
  168. $row['body'] = strip_tags($row['body']);
  169. $lower_forum_title = $strtolower($row['forum_title']);
  170. $lower_subject = $strtolower($row['subject']);
  171. $lower_body = $strtolower($row['body']);
  172. $num_posts = intval($row['num_comments']);
  173. if ($strlen($row['body']) > 270) {
  174. $row['body'] = $substr($row['body'], 0, 268).'...';
  175. }
  176. for ($i=0; $i<$num_words; $i++) {
  177. $score += 8 * substr_count($lower_forum_title, $lower_words[$i]); /* forum's title are weighed more */
  178. $score += 4 * substr_count($lower_subject, $lower_words[$i]); /* thread subject are weighed more */
  179. $score += 2 * substr_count($lower_body, $lower_words[$i]);
  180. $score += 1 * $num_posts;
  181. $row['forum_title'] = highlight($row['forum_title'], $words[$i]);
  182. $row['subject'] = highlight($row['subject'], $words[$i]);
  183. $row['body'] = highlight($row['body'], $words[$i]);
  184. }
  185. if ($score != 0) {
  186. $score += $course_score;
  187. }
  188. $row['score'] = $score;
  189. $search_results[] = $row;
  190. $total_score += $score;
  191. }
  192. return $search_results;
  193. }
  194. // My Courses - All courses you're enrolled in (including hidden)
  195. function get_my_courses($member_id) {
  196. global $db;
  197. $list = array();
  198. $sql = "SELECT course_id FROM ".TABLE_PREFIX."course_enrollment WHERE member_id=$member_id AND (approved='y' OR approved='a')";
  199. $result = mysql_query($sql, $db);
  200. while ($row = mysql_fetch_assoc($result)) {
  201. $list[] = $row['course_id']; // list contains all the Course IDs
  202. }
  203. return $list;
  204. }
  205. // All courses (display hidden too if you're enrolled in it)
  206. function get_all_courses($member_id) {
  207. global $system_courses, $db;
  208. $list = array();
  209. $num_courses = count($system_courses);
  210. // add all the courses that are not hidden,then find the hidden courses that you're enrolled in and then add that to array
  211. foreach ($system_courses as $course_id => $course_info) {
  212. if (!$course_info['hide']) {
  213. $list[] = $course_id;
  214. }
  215. }
  216. // if there aren't any hidden courses:
  217. if (count($system_courses) == count($list)) {
  218. return $list;
  219. }
  220. if ($_SESSION['valid_user']) {
  221. $my_courses = implode(',', get_my_courses($member_id));
  222. $sql = "SELECT course_id FROM ".TABLE_PREFIX."courses WHERE hide=1 AND course_id IN (0, $my_courses)";
  223. $result = mysql_query($sql, $db);
  224. while ($row = mysql_fetch_assoc($result)) {
  225. $list[] = $row['course_id'];
  226. }
  227. }
  228. return $list;
  229. }
  230. function print_search_pages($result) {
  231. global $count;
  232. foreach ($result as $items) {
  233. uasort($result, 'score_cmp');
  234. echo '<h5>' . $count . '. ';
  235. if(isset($items['forum_title'])){
  236. //Forum
  237. if ($_SESSION['course_id'] != $items['course_id']) {
  238. 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> ';
  239. } else {
  240. 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> ';
  241. }
  242. echo '</h5>'."\n";
  243. echo '<p><small>'.$items['body'];
  244. } else {
  245. //Content
  246. if ($_SESSION['course_id'] != $items['course_id']) {
  247. 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> ';
  248. } else {
  249. echo '<a href="'.url_rewrite('content.php?cid='.$items['content_id'].SEP.'words='.$_GET['words']).'">'.$items['title'].'</a> ';
  250. }
  251. echo '</h5>'."\n";
  252. echo '<p><small>'.$items['text'];
  253. }
  254. echo '<br /><small class="search-info">[<strong>'._AT('keywords').':</strong> ';
  255. if (isset($items['keywords'])) {
  256. echo $items['keywords'];
  257. } else {
  258. echo '<strong>'._AT('none').'</strong>';
  259. }
  260. echo '. <strong>'._AT('author').':</strong> ';
  261. if (isset($items['member_id'])) {
  262. echo AT_print(get_display_name($items['member_id']), 'members.login');
  263. } else {
  264. echo '<strong>'._AT('none').'</strong>';
  265. }
  266. echo '. <strong>'._AT('updated').':</strong> ';
  267. echo AT_date(_AT('inbox_date_format'), (isset($items['last_modified']) && $items['last_modified']!='')?$items['last_modified']:$items['last_comment'], AT_DATE_MYSQL_DATETIME);
  268. echo ']</small>';
  269. echo '</small></p>'."\n";
  270. $count++;
  271. }
  272. }
  273. ?>