aicc_hacp.php 12 KB

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