1 | <?php |
||
2 | /* For licensing terms, see /license.txt */ |
||
3 | |||
4 | use ChamiloSession as Session; |
||
5 | |||
6 | /** |
||
7 | * API event handler functions for AICC / CMIv4 in HACP communication mode. |
||
8 | * |
||
9 | * @author Denes Nagy <[email protected]> |
||
10 | * @author Yannick Warnier <[email protected]> |
||
11 | * |
||
12 | * @version v 1.0 |
||
13 | * |
||
14 | * @package chamilo.learnpath |
||
15 | * |
||
16 | * @license GNU/GPL |
||
17 | */ |
||
18 | |||
19 | /** |
||
20 | * This script is divided into three sections. |
||
21 | * The first section (below) is the initialisation part. |
||
22 | * The second section is the AICC object part |
||
23 | * The third section defines the event handlers for Chamilo's internal messaging |
||
24 | * and frames refresh. |
||
25 | * |
||
26 | * This script implements the HACP messaging for AICC. The API messaging is |
||
27 | * made by another set of scripts. |
||
28 | * |
||
29 | * Rules for HACP processing of one AU |
||
30 | * Rule #1 The first HACP message issued must be a GetParam |
||
31 | * Rule #2 The last HACP message issued must be an ExitAU |
||
32 | * Rule #3 At least one PutParam message must be issued prior to an ExitAU message |
||
33 | * Rule #4 No HACP messages can be issued after a successfully issued ExitAU message |
||
34 | * |
||
35 | * Only suspend_data and core.lesson_location should be sent updated to a late GetParam |
||
36 | * request. All other params should be as when the AU was launched. |
||
37 | */ |
||
38 | |||
39 | /* INIT SECTION */ |
||
40 | |||
41 | $debug = 0; |
||
42 | |||
43 | // Flag to allow for anonymous user - needs to be set before global.inc.php. |
||
44 | $use_anonymous = true; |
||
45 | |||
46 | // Use session ID as provided by the request. |
||
47 | if (!empty($_REQUEST['aicc_sid'])) { |
||
48 | session_id($_REQUEST['aicc_sid']); |
||
49 | if ($debug > 1) { |
||
50 | error_log('New LP - '.__FILE__.','.__LINE__.' - reusing session ID '.$_REQUEST['aicc_sid']); |
||
51 | } |
||
52 | } elseif (!empty($_REQUEST['session_id'])) { |
||
53 | session_id($_REQUEST['session_id']); |
||
54 | if ($debug > 1) { |
||
55 | error_log('New LP - '.__FILE__.','.__LINE__.' - reusing session ID '.$_REQUEST['session_id']); |
||
56 | } |
||
57 | } |
||
58 | //Load common libraries using a compatibility script to bridge between 1.6 and 1.8. |
||
59 | require_once __DIR__.'/../inc/global.inc.php'; |
||
60 | if ($debug > 2) { |
||
61 | error_log('New LP - '.__FILE__.','.__LINE__.' - Current session ID: '.session_id()); |
||
62 | } |
||
63 | |||
64 | // Is this needed? This is probabaly done in the header file. |
||
65 | $file = Session::read('file'); |
||
66 | /** @var learnpath $oLP */ |
||
67 | $oLP = UnserializeApi::unserialize( |
||
68 | 'not_allowed_classes', |
||
69 | Session::read('lpobject') |
||
70 | ); |
||
71 | $oItem = &$oLP->items[$oLP->current]; |
||
72 | if (!is_object($oItem)) { |
||
73 | error_log('New LP - aicc_hacp - Could not load oItem item', 0); |
||
74 | exit; |
||
75 | } |
||
76 | $autocomplete_when_80pct = 0; |
||
77 | |||
78 | $result = [ |
||
79 | 'core' => [], |
||
80 | 'core_lesson' => [], |
||
81 | 'core_vendor' => [], |
||
82 | 'evaluation' => [], |
||
83 | 'student_data' => [], |
||
84 | ]; |
||
85 | $convert_enc = ['%25', '%0D', '%0A', '%09', '%20', '%2D', '%2F', '%3B', '%3F', '%7B', '%7D', '%7C', '%5C', '%5E', '%7E', '%5B', '%5D', '%60', '%23', '%3E', '%3C', '%22']; |
||
86 | $convert_dec = ['%', "\r", "\n", "\t", ' ', '-', '/', ';', '?', '{', '}', '|', '\\', '^', '~', '[', ']', '`', '#', '>', '<', '"']; |
||
87 | $crlf = "\r\n"; |
||
88 | //$tab = "\t"; |
||
89 | $tab = ""; |
||
90 | $s_ec = 'error='; //string for error code |
||
91 | $s_et = 'error_text='; //string for error text |
||
92 | $s_ad = 'aicc_data='; //string for aicc_data |
||
93 | |||
94 | $errors = [0 => 'Successful', 1 => 'Invalid Command', 2 => 'Invalid AU password', 3 => 'Invalid Session ID']; |
||
95 | |||
96 | $error_code = 0; |
||
97 | $error_text = ''; |
||
98 | $aicc_data = ''; |
||
99 | $result = ''; |
||
100 | // Get REQUEST |
||
101 | if (!empty($_REQUEST['command'])) { |
||
102 | //error_log('In '.__FILE__.', '.__LINE__.' - request is '.$_REQUEST['command'], 0); |
||
103 | switch (strtolower($_REQUEST['command'])) { |
||
104 | case 'getparam': |
||
105 | // Request for all available data to be printed out in the answer. |
||
106 | if (!empty($_REQUEST['version'])) { |
||
107 | $hacp_version = Database::escape_string($_REQUEST['version']); |
||
108 | } |
||
109 | if (!empty($_REQUEST['session_id'])) { |
||
110 | $hacp_session_id = Database::escape_string($_REQUEST['session_id']); |
||
111 | } |
||
112 | $error_code = 0; |
||
113 | $error_text = $errors[$error_code]; |
||
114 | //$result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf; |
||
115 | $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad; |
||
116 | $result .= '[Core]'.$crlf; |
||
117 | $result .= $tab.'Student_ID='.$_user['user_id'].$crlf; |
||
118 | $result .= $tab.'Student_Name='.api_get_person_name($_user['firstName'], $_user['lastName']).$crlf; |
||
119 | $result .= $tab.'Lesson_Location='.$oItem->get_lesson_location().$crlf; |
||
120 | $result .= $tab.'Credit='.$oItem->get_credit().$crlf; |
||
121 | $result .= $tab.'Lesson_Status='.$oItem->get_status().$crlf; |
||
122 | $result .= $tab.'Score='.$oItem->get_score().$crlf; |
||
123 | $result .= $tab.'Time='.$oItem->get_scorm_time('js').$crlf; |
||
124 | $result .= $tab.'Lesson_Mode='.$oItem->get_lesson_mode().$crlf; |
||
125 | $result .= '[Core_Lesson]'.$crlf; |
||
126 | $result .= $oItem->get_suspend_data().$crlf; |
||
127 | $result .= '[Core_Vendor]'.$crlf; |
||
128 | $result .= $oItem->get_launch_data.$crlf; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
129 | $result .= '[Comments]'.$crlf; |
||
130 | $result .= $crlf; |
||
131 | $result .= '[Evaluation]'.$crlf; |
||
132 | $result .= $tab.'Course_ID={'.api_get_course_id().'}'.$crlf; |
||
133 | //$result .= '[Objectives_Status]'.$crlf; |
||
134 | $result .= '[Student_Data]'.$crlf; |
||
135 | $result .= $tab.'Mastery_Score='.$oItem->masteryscore.$crlf; |
||
136 | //$result .= '[Student_Demographics]'.$crlf; |
||
137 | //$result .= '[Student_Preferences]'.$crlf; |
||
138 | |||
139 | //error_log('Returning message: '.$result,0); |
||
140 | //$result = str_replace($convert_dec, $convert_enc, $result); |
||
141 | //error_log('Returning message (encoded): '.$result,0); |
||
142 | break; |
||
143 | case 'putparam': |
||
144 | $hacp_version = ''; |
||
145 | $hacp_session_id = ''; |
||
146 | $hacp_aicc_data = ''; |
||
147 | foreach ($_REQUEST as $name => $value) { |
||
148 | //escape the value as described in the AICC documentation p170 |
||
149 | switch (strtolower($name)) { |
||
150 | case 'version': |
||
151 | $hacp_version = $value; |
||
152 | break; |
||
153 | case 'session_id': |
||
154 | $hacp_session_id = $value; |
||
155 | break; |
||
156 | case 'aicc_data': |
||
157 | //error_log('In '.__FILE__.', '.__LINE__.' - aicc data before translation is '.$value, 0); |
||
158 | $value = str_replace('+', ' ', $value); |
||
159 | $value = str_replace($convert_enc, $convert_dec, $value); |
||
160 | $hacp_aicc_data = $value; |
||
161 | break; |
||
162 | } |
||
163 | } |
||
164 | // Treat the incoming request: |
||
165 | $aicc = new aicc(); |
||
166 | $msg_array = $aicc->parse_ini_string_quotes_safe($hacp_aicc_data, ['core_lesson', 'core_vendor']); |
||
167 | foreach ($msg_array as $key => $dummy) { |
||
168 | switch (strtolower($key)) { |
||
169 | case 'core': |
||
170 | foreach ($msg_array[$key] as $subkey => $value) { |
||
171 | switch (strtolower($subkey)) { |
||
172 | case 'lesson_location': |
||
173 | $oItem->set_lesson_location($value); |
||
174 | break; |
||
175 | case 'lesson_status': |
||
176 | // Sometimes values are sent abbreviated |
||
177 | switch ($value) { |
||
178 | case 'C': |
||
179 | $value = 'completed'; |
||
180 | break; |
||
181 | case 'I': |
||
182 | $value = 'incomplete'; |
||
183 | break; |
||
184 | case 'N': |
||
185 | case 'NA': |
||
186 | $value = 'not attempted'; |
||
187 | break; |
||
188 | case 'P': |
||
189 | $value = 'passed'; |
||
190 | break; |
||
191 | case 'B': |
||
192 | $value = 'browsed'; |
||
193 | break; |
||
194 | default: |
||
195 | break; |
||
196 | } |
||
197 | $oItem->set_status($value); |
||
198 | break; |
||
199 | case 'score': |
||
200 | $oItem->set_score($value); |
||
201 | break; |
||
202 | case 'time': |
||
203 | if (strpos($value, ':') !== false) { |
||
204 | $oItem->set_time($value, 'scorm'); |
||
205 | } else { |
||
206 | $oItem->set_time($value); |
||
207 | } |
||
208 | break; |
||
209 | } |
||
210 | } |
||
211 | break; |
||
212 | case 'core_lesson': |
||
213 | $oItem->current_data = $msg_array[$key]; |
||
214 | break; |
||
215 | case 'comments': |
||
216 | break; |
||
217 | case 'objectives_status': |
||
218 | break; |
||
219 | case 'student_data': |
||
220 | break; |
||
221 | case 'student_preferences': |
||
222 | break; |
||
223 | } |
||
224 | } |
||
225 | |||
226 | $error_code = 0; |
||
227 | $error_text = $errors[$error_code]; |
||
228 | $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf; |
||
229 | $oItem->save(false); |
||
230 | break; |
||
231 | case 'putcomments': |
||
232 | $error_code = 0; |
||
233 | $error_text = $errors[$error_code]; |
||
234 | $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf; |
||
235 | break; |
||
236 | case 'putobjectives': |
||
237 | $error_code = 0; |
||
238 | $error_text = $errors[$error_code]; |
||
239 | $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf; |
||
240 | break; |
||
241 | case 'putpath': |
||
242 | $error_code = 0; |
||
243 | $error_text = $errors[$error_code]; |
||
244 | $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf; |
||
245 | break; |
||
246 | case 'putinteractions': |
||
247 | $error_code = 0; |
||
248 | $error_text = $errors[$error_code]; |
||
249 | $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf; |
||
250 | break; |
||
251 | case 'putperformance': |
||
252 | $error_code = 0; |
||
253 | $error_text = $errors[$error_code]; |
||
254 | $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf; |
||
255 | break; |
||
256 | case 'exitau': |
||
257 | $error_code = 0; |
||
258 | $error_text = $errors[$error_code]; |
||
259 | $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf; |
||
260 | break; |
||
261 | default: |
||
262 | $error_code = 1; |
||
263 | $error_text = $errors[1]; |
||
264 | $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf; |
||
265 | } |
||
266 | } |
||
267 | |||
268 | Session::write('lpobject', serialize($oLP)); |
||
269 | Session::write('oLP', $oLP); |
||
270 | session_write_close(); |
||
271 | // Content type must be text/plain. |
||
272 | header('Content-type: text/plain'); |
||
273 | echo $result; |
||
274 |