quiz_processor.class.php 5.6 KB

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