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