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. /**
  8. * Code
  9. */
  10. include_once dirname(__FILE__) . '/../../../global.inc.php';
  11. require_once dirname(__FILE__) . '/search_processor.class.php';
  12. /**
  13. * Process documents before pass it to search listing scripts
  14. * @package chamilo.include.search
  15. */
  16. class document_processor extends search_processor {
  17. function document_processor($rows) {
  18. $this->rows = $rows;
  19. }
  20. public function process() {
  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. $course_information = api_get_course_info($course_id);
  64. $course_id = $course_information['real_id'];
  65. $course_path = $course_information['path'];
  66. if (!empty($course_information)) {
  67. $item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  68. $doc_table = Database::get_course_table(TABLE_DOCUMENT);
  69. $doc_id = intval($doc_id);
  70. $sql = "SELECT * FROM $doc_table
  71. WHERE $doc_table.id = $doc_id AND c_id = $course_id
  72. LIMIT 1";
  73. $dk_result = Database::query($sql);
  74. $sql = "SELECT insert_user_id FROM $item_property_table
  75. WHERE ref = $doc_id AND tool = '" . TOOL_DOCUMENT . "' AND c_id = $course_id
  76. LIMIT 1";
  77. $name = '';
  78. if ($row = Database::fetch_array($dk_result)) {
  79. $name = $row['title'];
  80. $url = api_get_path(WEB_COURSE_PATH).'%s/document%s';
  81. $url = sprintf($url, $course_path, $row['path']);
  82. // Get the image path
  83. $icon = choose_image(basename($row['path']));
  84. $thumbnail = api_get_path(WEB_CODE_PATH) . 'img/' . $icon;
  85. $image = $thumbnail;
  86. //FIXME: use big images
  87. // get author
  88. $author = '';
  89. $item_result = Database::query($sql);
  90. if ($row = Database::fetch_array($item_result)) {
  91. $user_data = api_get_user_info($row['insert_user_id']);
  92. $author = api_get_person_name($user_data['firstName'], $user_data['lastName']);
  93. }
  94. }
  95. return array($thumbnail, $image, $name, $author, $url); // FIXME: is it posible to get an author here?
  96. } else {
  97. return array();
  98. }
  99. }
  100. }