1 | <?php |
||
2 | /* For licensing terms, see /license.txt */ |
||
3 | |||
4 | use Chamilo\CourseBundle\Entity\CQuiz; |
||
5 | |||
6 | /** |
||
7 | * Process exercises before pass it to search listing scripts. |
||
8 | * |
||
9 | * @package chamilo.include.search |
||
10 | */ |
||
11 | class quiz_processor extends search_processor |
||
12 | { |
||
13 | public $exercices = []; |
||
14 | |||
15 | public function __construct($rows) |
||
16 | { |
||
17 | $this->rows = $rows; |
||
18 | // group by exercise |
||
19 | foreach ($rows as $row_id => $row_val) { |
||
20 | $courseid = $row_val['courseid']; |
||
21 | $se_data = $row_val['xapian_data'][SE_DATA]; |
||
22 | switch ($row_val['xapian_data'][SE_DATA]['type']) { |
||
23 | case SE_DOCTYPE_EXERCISE_EXERCISE: |
||
24 | $exercise_id = $se_data['exercise_id']; |
||
25 | $question = null; |
||
26 | $item = [ |
||
27 | 'courseid' => $courseid, |
||
28 | 'question' => $question, |
||
29 | 'total_score' => $row_val['score'], |
||
30 | 'row_id' => $row_id, |
||
31 | ]; |
||
32 | $this->exercises[$courseid][$exercise_id] = $item; |
||
33 | $this->exercises[$courseid][$exercise_id]['total_score'] += $row_val['score']; |
||
34 | break; |
||
35 | case SE_DOCTYPE_EXERCISE_QUESTION: |
||
36 | if (is_array($se_data['exercise_ids'])) { |
||
37 | foreach ($se_data['exercise_ids'] as $exercise_id) { |
||
38 | $question = $se_data['question_id']; |
||
39 | $item = [ |
||
40 | 'courseid' => $courseid, |
||
41 | 'question' => $question, |
||
42 | 'total_score' => $row_val['score'], |
||
43 | 'row_id' => $row_id, |
||
44 | ]; |
||
45 | $this->exercises[$courseid][$exercise_id] = $item; |
||
46 | $this->exercises[$courseid][$exercise_id]['total_score'] += $row_val['score']; |
||
47 | } |
||
48 | } |
||
49 | break; |
||
50 | } |
||
51 | } |
||
52 | } |
||
53 | |||
54 | public function process() |
||
55 | { |
||
56 | $results = []; |
||
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 = [ |
||
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 | |||
91 | // get information to sort |
||
92 | foreach ($results as $key => $row) { |
||
93 | $score[$key] = $row['total_score']; |
||
94 | } |
||
95 | // Sort results with score descending |
||
96 | array_multisort($score, SORT_DESC, $results); |
||
97 | |||
98 | return $results; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get learning path information. |
||
103 | */ |
||
104 | private function get_information($courseCode, $exercise_id) |
||
105 | { |
||
106 | $course_information = api_get_course_info($courseCode); |
||
107 | $course_id = $course_information['real_id']; |
||
108 | |||
109 | $em = Database::getManager(); |
||
110 | |||
111 | if (!empty($course_information)) { |
||
112 | $exercise_id = intval($exercise_id); |
||
113 | $dk_result = $em->find(CQuiz::class, $exercise_id); |
||
114 | |||
115 | $name = ''; |
||
116 | if ($dk_result) { |
||
117 | // Get the image path |
||
118 | $thumbnail = Display::returnIconPath('quiz.png'); |
||
119 | $image = $thumbnail; //FIXME: use big images |
||
120 | $name = $dk_result->getTitle(); |
||
121 | // get author |
||
122 | $author = ''; |
||
123 | $item_result = $em |
||
124 | ->getRepository('ChamiloCourseBundle:CItemProperty') |
||
125 | ->findOneBy([ |
||
126 | 'ref' => $exercise_id, |
||
127 | 'tool' => TOOL_QUIZ, |
||
128 | 'course' => $course_id, |
||
129 | ]); |
||
130 | |||
131 | if ($item_result) { |
||
132 | $author = UserManager::formatUserFullName($item_result->getInsertUser()); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | return [$thumbnail, $image, $name, $author]; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
137 | } else { |
||
138 | return []; |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 |