link_processor.class.php 4.9 KB

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