document_processor.class.php 4.0 KB

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