link_processor.class.php 4.4 KB

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