learnpath_processor.class.php 6.3 KB

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