document_processor.class.php 4.2 KB

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