quiz_processor.class.php 6.1 KB

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