learnpath_processor.class.php 5.2 KB

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