link_processor.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 links before pass it to search listing scripts
  14. * @package chamilo.include.search
  15. */
  16. class link_processor extends search_processor {
  17. public $links = array();
  18. function link_processor($rows) {
  19. $this->rows = $rows;
  20. // group all links together
  21. foreach ($rows as $row_id => $row_val) {
  22. $link_id = $row_val['xapian_data'][SE_DATA]['link_id'];
  23. $courseid = $row_val['courseid'];
  24. $item = array(
  25. 'courseid' => $courseid,
  26. 'score' => $row_val['score'],
  27. 'link_id' => $link_id,
  28. 'row_id' => $row_id,
  29. );
  30. $this->links[$courseid]['links'][] = $item;
  31. $this->links[$courseid]['total_score'] += $row_val['score'];
  32. }
  33. }
  34. public function process() {
  35. $results = array();
  36. foreach ($this->links as $courseid => $one_course_links) {
  37. $course_info = api_get_course_info($courseid);
  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, $courseid);
  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($courseid, $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. $course_information = api_get_course_info($course_id);
  89. $course_id = $course_information['real_id'];
  90. $course_id_alpha = $course_information['id'];
  91. if (!empty($course_information)) {
  92. $item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  93. $link_id = intval($link_id);
  94. $sql = "SELECT insert_user_id FROM $item_property_table
  95. WHERE ref = $link_id AND tool = '" . TOOL_LINK . "' AND c_id = $course_id
  96. LIMIT 1";
  97. $name = get_lang('Links');
  98. $url = api_get_path(WEB_PATH) . 'main/link/link.php?cidReq=%s';
  99. $url = sprintf($url, $course_id_alpha);
  100. // Get the image path
  101. $thumbnail = api_get_path(WEB_CODE_PATH) . 'img/link.gif';
  102. $image = $thumbnail; //FIXME: use big images
  103. // get author
  104. $author = '';
  105. $item_result = Database::query($sql);
  106. if ($row = Database::fetch_array($item_result)) {
  107. $user_data = api_get_user_info($row['insert_user_id']);
  108. $author = api_get_person_name($user_data['firstName'], $user_data['lastName']);
  109. }
  110. return array($thumbnail, $image, $name, $author, $url);
  111. } else {
  112. return array();
  113. }
  114. }
  115. }