learnpath_processor.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Process learning paths before pass it to search listing scripts.
  5. *
  6. * @package chamilo.include.search
  7. */
  8. class learnpath_processor extends search_processor
  9. {
  10. public $learnpaths = [];
  11. public function __construct($rows)
  12. {
  13. $this->rows = $rows;
  14. // group by learning path
  15. foreach ($rows as $row_id => $row_val) {
  16. $lp_id = $row_val['xapian_data'][SE_DATA]['lp_id'];
  17. $lp_item = $row_val['xapian_data'][SE_DATA]['lp_item'];
  18. $document_id = $row_val['xapian_data'][SE_DATA]['document_id'];
  19. $courseid = $row_val['courseid'];
  20. $item = [
  21. 'courseid' => $courseid,
  22. 'lp_item' => $lp_item,
  23. 'score' => $row_val['score'],
  24. 'row_id' => $row_id,
  25. ];
  26. $this->learnpaths[$courseid][$lp_id][] = $item;
  27. $this->learnpaths[$courseid][$lp_id]['total_score'] += $row_val['score'];
  28. $this->learnpaths[$courseid][$lp_id]['has_document_id'] = is_numeric($document_id);
  29. }
  30. }
  31. /**
  32. * @return array
  33. */
  34. public function process()
  35. {
  36. $results = [];
  37. foreach ($this->learnpaths as $courseid => $learnpaths) {
  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. foreach ($learnpaths as $lp_id => $lp) {
  43. // is visible?
  44. $visibility = api_get_item_visibility(
  45. api_get_course_info($courseid),
  46. TOOL_LEARNPATH,
  47. $lp_id
  48. );
  49. if ($visibility) {
  50. list($thumbnail, $image, $name, $author) = $this->get_information($courseid, $lp_id, $lp['has_document_id']);
  51. $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?cidReq=%s&action=view&lp_id=%s';
  52. $url = sprintf($url, $courseid, $lp_id);
  53. $result = [
  54. 'toolid' => TOOL_LEARNPATH,
  55. 'score' => $lp['total_score'] / (count($lp) - 1), // not count total_score array item
  56. 'url' => $url,
  57. 'thumbnail' => $thumbnail,
  58. 'image' => $image,
  59. 'title' => $name,
  60. 'author' => $author,
  61. ];
  62. if ($course_visible_for_user) {
  63. $results[] = $result;
  64. } else { // course not visible for user
  65. if ($search_show_unlinked_results) {
  66. $result['url'] = '';
  67. $results[] = $result;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. // get information to sort
  75. foreach ($results as $key => $row) {
  76. $score[$key] = $row['score'];
  77. }
  78. // Sort results with score descending
  79. array_multisort($score, SORT_DESC, $results);
  80. return $results;
  81. }
  82. /**
  83. * Get learning path information.
  84. */
  85. private function get_information($course_id, $lp_id, $has_document_id = true)
  86. {
  87. $course_information = api_get_course_info($course_id);
  88. $course_id = $course_information['real_id'];
  89. $course_path = $course_information['path'];
  90. if (!empty($course_information)) {
  91. $lpi_table = Database::get_course_table(TABLE_LP_ITEM);
  92. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  93. $doc_table = Database::get_course_table(TABLE_DOCUMENT);
  94. $lp_id = intval($lp_id);
  95. if ($has_document_id) {
  96. $sql = "SELECT $lpi_table.id, $lp_table.name, $lp_table.author, $doc_table.path
  97. FROM $lp_table, $lpi_table
  98. INNER JOIN $doc_table ON $lpi_table.path = $doc_table.id AND $lpi_table.c_id = $course_id
  99. WHERE $lpi_table.c_id = $course_id AND
  100. $doc_table.c_id = $course_id AND
  101. $lpi_table.lp_id = $lp_id AND
  102. $lpi_table.display_order = 1 AND
  103. $lp_table.id = $lpi_table.lp_id
  104. LIMIT 1";
  105. } else {
  106. $sql = "SELECT $lpi_table.id, $lp_table.name, $lp_table.author
  107. FROM $lp_table, $lpi_table
  108. WHERE
  109. $lpi_table.c_id = $course_id AND
  110. $lp_table.c_id = $course_id AND
  111. $lpi_table.lp_id = $lp_id AND
  112. $lpi_table.display_order = 1 AND
  113. $lp_table.id = $lpi_table.lp_id
  114. LIMIT 1";
  115. }
  116. $dk_result = Database::query($sql);
  117. $path = '';
  118. $name = '';
  119. if ($row = Database::fetch_array($dk_result)) {
  120. // Get the image path
  121. $img_location = api_get_path(WEB_COURSE_PATH).$course_path."/document/";
  122. $thumbnail_path = str_replace('.png.html', '_thumb.png', $row['path']);
  123. $big_img_path = str_replace('.png.html', '.png', $row['path']);
  124. $thumbnail = '';
  125. if (!empty($thumbnail_path)) {
  126. $thumbnail = $img_location.$thumbnail_path;
  127. }
  128. $image = '';
  129. if (!empty($big_img_path)) {
  130. $image = $img_location.$big_img_path;
  131. }
  132. $name = $row['name'];
  133. }
  134. return [$thumbnail, $image, $name, $row['author']];
  135. } else {
  136. return [];
  137. }
  138. }
  139. }