quiz_processor.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Process exercises before pass it to search listing scripts.
  5. *
  6. * @package chamilo.include.search
  7. */
  8. class quiz_processor extends search_processor
  9. {
  10. public $exercices = [];
  11. public function __construct($rows)
  12. {
  13. $this->rows = $rows;
  14. // group by exercise
  15. foreach ($rows as $row_id => $row_val) {
  16. $courseid = $row_val['courseid'];
  17. $se_data = $row_val['xapian_data'][SE_DATA];
  18. switch ($row_val['xapian_data'][SE_DATA]['type']) {
  19. case SE_DOCTYPE_EXERCISE_EXERCISE:
  20. $exercise_id = $se_data['exercise_id'];
  21. $question = null;
  22. $item = [
  23. 'courseid' => $courseid,
  24. 'question' => $question,
  25. 'total_score' => $row_val['score'],
  26. 'row_id' => $row_id,
  27. ];
  28. $this->exercises[$courseid][$exercise_id] = $item;
  29. $this->exercises[$courseid][$exercise_id]['total_score'] += $row_val['score'];
  30. break;
  31. case SE_DOCTYPE_EXERCISE_QUESTION:
  32. if (is_array($se_data['exercise_ids'])) {
  33. foreach ($se_data['exercise_ids'] as $exercise_id) {
  34. $question = $se_data['question_id'];
  35. $item = [
  36. 'courseid' => $courseid,
  37. 'question' => $question,
  38. 'total_score' => $row_val['score'],
  39. 'row_id' => $row_id,
  40. ];
  41. $this->exercises[$courseid][$exercise_id] = $item;
  42. $this->exercises[$courseid][$exercise_id]['total_score'] += $row_val['score'];
  43. }
  44. }
  45. break;
  46. }
  47. }
  48. }
  49. public function process()
  50. {
  51. $results = [];
  52. foreach ($this->exercises as $courseid => $exercises) {
  53. $search_show_unlinked_results = (api_get_setting('search_show_unlinked_results') == 'true');
  54. $course_visible_for_user = api_is_course_visible_for_user(null, $courseid);
  55. // can view course?
  56. if ($course_visible_for_user || $search_show_unlinked_results) {
  57. foreach ($exercises as $exercise_id => $exercise) {
  58. // is visible?
  59. $visibility = api_get_item_visibility(api_get_course_info($courseid), TOOL_QUIZ, $exercise_id);
  60. if ($visibility) {
  61. list($thumbnail, $image, $name, $author) = $this->get_information($courseid, $exercise_id);
  62. $url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php?cidReq=%s&exerciseId=%s';
  63. $url = sprintf($url, $courseid, $exercise_id);
  64. $result = [
  65. 'toolid' => TOOL_QUIZ,
  66. 'total_score' => $exercise['total_score'] / (count($exercise) - 1), // not count total_score array item
  67. 'url' => $url,
  68. 'thumbnail' => $thumbnail,
  69. 'image' => $image,
  70. 'title' => $name,
  71. 'author' => $author,
  72. ];
  73. if ($course_visible_for_user) {
  74. $results[] = $result;
  75. } else { // course not visible for user
  76. if ($search_show_unlinked_results) {
  77. $result['url'] = '';
  78. $results[] = $result;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. // get information to sort
  86. foreach ($results as $key => $row) {
  87. $score[$key] = $row['total_score'];
  88. }
  89. // Sort results with score descending
  90. array_multisort($score, SORT_DESC, $results);
  91. return $results;
  92. }
  93. /**
  94. * Get learning path information.
  95. */
  96. private function get_information($courseCode, $exercise_id)
  97. {
  98. $course_information = api_get_course_info($courseCode);
  99. $course_id = $course_information['real_id'];
  100. $em = Database::getManager();
  101. if (!empty($course_information)) {
  102. $exercise_id = intval($exercise_id);
  103. $dk_result = $em
  104. ->getRepository('ChamiloCourseBundle:CQuiz')
  105. ->findOneBy([
  106. 'id' => $exercise_id,
  107. 'cId' => $course_id,
  108. ]);
  109. $name = '';
  110. if ($dk_result) {
  111. // Get the image path
  112. $thumbnail = Display::returnIconPath('quiz.png');
  113. $image = $thumbnail; //FIXME: use big images
  114. $name = $dk_result->getTitle();
  115. // get author
  116. $author = '';
  117. $item_result = $em
  118. ->getRepository('ChamiloCourseBundle:CItemProperty')
  119. ->findOneBy([
  120. 'ref' => $exercise_id,
  121. 'tool' => TOOL_QUIZ,
  122. 'course' => $course_id,
  123. ]);
  124. if ($item_result) {
  125. $author = UserManager::formatUserFullName($item_result->getInsertUser());
  126. }
  127. }
  128. return [$thumbnail, $image, $name, $author];
  129. } else {
  130. return [];
  131. }
  132. }
  133. }