document_processor.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. include_once dirname(__FILE__) . '/../../../global.inc.php';
  3. require_once dirname(__FILE__) . '/search_processor.class.php';
  4. /**
  5. * Process documents before pass it to search listing scripts
  6. */
  7. class document_processor extends search_processor {
  8. function document_processor($rows) {
  9. $this->rows = $rows;
  10. }
  11. public function process() {
  12. $results = array();
  13. foreach ($this->rows as $row_val) {
  14. $search_show_unlinked_results = (api_get_setting('search_show_unlinked_results') == 'true');
  15. $course_visible_for_user = api_is_course_visible_for_user(NULL, $row_val['courseid']);
  16. // can view course?
  17. if ($course_visible_for_user || $search_show_unlinked_results) {
  18. // is visible?
  19. $visibility = api_get_item_visibility(api_get_course_info($row_val['courseid']), TOOL_DOCUMENT, $row_val['xapian_data'][SE_DATA]['doc_id']);
  20. if ($visibility) {
  21. list($thumbnail, $image, $name, $author, $url) = $this->get_information($row_val['courseid'], $row_val['xapian_data'][SE_DATA]['doc_id']);
  22. $result = array(
  23. 'toolid' => TOOL_DOCUMENT,
  24. 'score' => $row_val['score'],
  25. 'url' => $url,
  26. 'thumbnail' => $thumbnail,
  27. 'image' => $image,
  28. 'title' => $name,
  29. 'author' => $author,
  30. );
  31. if ($course_visible_for_user) {
  32. $results[] = $result;
  33. } else { // course not visible for user
  34. if ($search_show_unlinked_results) {
  35. $result['url'] = '';
  36. $results[] = $result;
  37. }
  38. }
  39. }
  40. }
  41. }
  42. // get information to sort
  43. foreach ($results as $key => $row) {
  44. $score[$key] = $row['score'];
  45. }
  46. // Sort results with score descending
  47. array_multisort($score, SORT_DESC, $results);
  48. return $results;
  49. }
  50. /**
  51. * Get document information
  52. */
  53. private function get_information($course_id, $doc_id) {
  54. $doc_table = Database::get_course_table_from_code($course_id, TABLE_DOCUMENT);
  55. $item_property_table = Database::get_course_table_from_code($course_id, TABLE_ITEM_PROPERTY);
  56. $doc_id = Database::escape_string($doc_id);
  57. $sql = "SELECT *
  58. FROM $doc_table
  59. WHERE $doc_table.id = $doc_id
  60. LIMIT 1";
  61. $dk_result = Database::query ($sql);
  62. $sql = "SELECT insert_user_id
  63. FROM $item_property_table
  64. WHERE ref = $doc_id
  65. AND tool = '". TOOL_DOCUMENT ."'
  66. LIMIT 1";
  67. $name = '';
  68. if ($row = Database::fetch_array ($dk_result)) {
  69. $name = $row['title'];
  70. $url = api_get_path(WEB_PATH) . 'courses/%s/document%s';
  71. $url = sprintf($url, api_get_course_path($course_id), $row['path']);
  72. // Get the image path
  73. include_once api_get_path(LIBRARY_PATH). 'fileDisplay.lib.php';
  74. $icon = choose_image(basename($row['path']));
  75. $thumbnail = api_get_path(WEB_CODE_PATH) .'img/'. $icon;
  76. $image = $thumbnail;
  77. //FIXME: use big images
  78. // get author
  79. $author = '';
  80. $item_result = Database::query ($sql);
  81. if ($row = Database::fetch_array ($item_result)) {
  82. $user_data = api_get_user_info($row['insert_user_id']);
  83. $author = api_get_person_name($user_data['firstName'], $user_data['lastName']);
  84. }
  85. }
  86. return array($thumbnail, $image, $name, $author, $url); // FIXME: is it posible to get an author here?
  87. }
  88. }
  89. ?>