learnpath_processor.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if ($search_show_unlinked_results) {
  44. if (!$course_visible_for_user) {
  45. $url = '';
  46. }
  47. $results[] = array(
  48. 'toolid' => TOOL_LEARNPATH,
  49. 'score' => $lp['total_score']/(count($lp)-1), // not count total_score array item
  50. 'url' => $url,
  51. 'thumbnail' => $thumbnail,
  52. 'image' => $image,
  53. 'title' => $name,
  54. 'author' => $author,
  55. );
  56. }
  57. }
  58. }
  59. }
  60. }
  61. // get information to sort
  62. foreach ($results as $key => $row) {
  63. $score[$key] = $row['score'];
  64. }
  65. // Sort results with score descending
  66. array_multisort($score, SORT_DESC, $results);
  67. return $results;
  68. }
  69. /**
  70. * Get learning path information
  71. */
  72. private function get_information($course_id, $lp_id, $has_document_id=TRUE) {
  73. $lpi_table = Database::get_course_table_from_code($course_id, TABLE_LP_ITEM);
  74. $lp_table = Database::get_course_table_from_code($course_id, TABLE_LP_MAIN);
  75. $doc_table = Database::get_course_table_from_code($course_id, TABLE_DOCUMENT);
  76. if ($has_document_id) {
  77. $sql = "SELECT $lpi_table.id, $lp_table.name, $lp_table.author, $doc_table.path
  78. FROM $lp_table, $lpi_table
  79. INNER JOIN $doc_table ON $lpi_table.path = $doc_table.id
  80. WHERE $lpi_table.lp_id = $lp_id
  81. AND $lpi_table.display_order = 1
  82. AND $lp_table.id = $lpi_table.lp_id
  83. LIMIT 1";
  84. }
  85. else {
  86. $sql = "SELECT $lpi_table.id, $lp_table.name, $lp_table.author
  87. FROM $lp_table, $lpi_table
  88. WHERE $lpi_table.lp_id = $lp_id
  89. AND $lpi_table.display_order = 1
  90. AND $lp_table.id = $lpi_table.lp_id
  91. LIMIT 1";
  92. }
  93. $dk_result = api_sql_query ($sql);
  94. $path = '';
  95. $name = '';
  96. if ($row = Database::fetch_array ($dk_result)) {
  97. // Get the image path
  98. $img_location = api_get_path(WEB_COURSE_PATH).api_get_course_path($course_id)."/document/";
  99. $thumbnail_path = str_replace ('.png.html', '_thumb.png', $row['path']);
  100. $big_img_path = str_replace ('.png.html', '.png', $row['path']);
  101. $thumbnail = '';
  102. if (!empty($thumbnail_path)) {
  103. $thumbnail = $img_location . $thumbnail_path;
  104. }
  105. $image = '';
  106. if (!empty($big_img_path)) {
  107. $image = $img_location . $big_img_path;
  108. }
  109. $name = $row['name'];
  110. }
  111. return array($thumbnail, $image, $name, $row['author']);
  112. }
  113. }
  114. ?>