learnpath_processor.class.php 6.2 KB

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