quiz_processor.class.php 5.7 KB

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