link_processor.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Process links before pass it to search listing scripts.
  5. *
  6. * @package chamilo.include.search
  7. */
  8. class link_processor extends search_processor
  9. {
  10. public $links = [];
  11. public function __construct($rows)
  12. {
  13. $this->rows = $rows;
  14. // group all links together
  15. foreach ($rows as $row_id => $row_val) {
  16. $link_id = $row_val['xapian_data'][SE_DATA]['link_id'];
  17. $courseid = $row_val['courseid'];
  18. $item = [
  19. 'courseid' => $courseid,
  20. 'score' => $row_val['score'],
  21. 'link_id' => $link_id,
  22. 'row_id' => $row_id,
  23. ];
  24. $this->links[$courseid]['links'][] = $item;
  25. $this->links[$courseid]['total_score'] += $row_val['score'];
  26. }
  27. }
  28. public function process()
  29. {
  30. $results = [];
  31. foreach ($this->links as $courseCode => $one_course_links) {
  32. $course_info = api_get_course_info($courseCode);
  33. $search_show_unlinked_results = (api_get_setting('search_show_unlinked_results') == 'true');
  34. $course_visible_for_user = api_is_course_visible_for_user(null, $courseCode);
  35. // can view course?
  36. if ($course_visible_for_user || $search_show_unlinked_results) {
  37. $result = null;
  38. foreach ($one_course_links['links'] as $one_link) {
  39. // is visible?
  40. $visibility = api_get_item_visibility($course_info, TOOL_LINK, $one_link['link_id']);
  41. if ($visibility) {
  42. // if one is visible let show the result for a course
  43. // also asume all data of this item like the data of the whole group of links(Ex. author)
  44. list($thumbnail, $image, $name, $author, $url) = $this->get_information($courseCode, $one_link['link_id']);
  45. $result_tmp = [
  46. 'toolid' => TOOL_LINK,
  47. 'score' => $one_course_links['total_score'] / (count($one_course_links) - 1), // not count total_score array item
  48. 'url' => $url,
  49. 'thumbnail' => $thumbnail,
  50. 'image' => $image,
  51. 'title' => $name,
  52. 'author' => $author,
  53. ];
  54. if ($course_visible_for_user) {
  55. $result = $result_tmp;
  56. } else { // course not visible for user
  57. if ($search_show_unlinked_results) {
  58. $result_tmp['url'] = '';
  59. $result = $result_tmp;
  60. }
  61. }
  62. break;
  63. }
  64. }
  65. if (!is_null($result)) {
  66. // if there is at least one link item found show link to course Links tool page
  67. $results[] = $result;
  68. }
  69. }
  70. }
  71. // get information to sort
  72. foreach ($results as $key => $row) {
  73. $score[$key] = $row['score'];
  74. }
  75. // Sort results with score descending
  76. array_multisort($score, SORT_DESC, $results);
  77. return $results;
  78. }
  79. /**
  80. * Get document information.
  81. */
  82. private function get_information($course_id, $link_id)
  83. {
  84. $course_information = api_get_course_info($course_id);
  85. $course_id = $course_information['real_id'];
  86. $course_id_alpha = $course_information['id'];
  87. if (!empty($course_information)) {
  88. $item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  89. $link_id = intval($link_id);
  90. $sql = "SELECT insert_user_id FROM $item_property_table
  91. WHERE ref = $link_id AND tool = '".TOOL_LINK."' AND c_id = $course_id
  92. LIMIT 1";
  93. $name = get_lang('Links');
  94. $url = api_get_path(WEB_PATH).'main/link/link.php?cidReq=%s';
  95. $url = sprintf($url, $course_id_alpha);
  96. // Get the image path
  97. $thumbnail = Display::returnIconPath('link.png');
  98. $image = $thumbnail; //FIXME: use big images
  99. // get author
  100. $author = '';
  101. $item_result = Database::query($sql);
  102. if ($row = Database::fetch_array($item_result)) {
  103. $user_data = api_get_user_info($row['insert_user_id']);
  104. $author = api_get_person_name($user_data['firstName'], $user_data['lastName']);
  105. }
  106. return [$thumbnail, $image, $name, $author, $url];
  107. } else {
  108. return [];
  109. }
  110. }
  111. }