aicc_hacp.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. // Treat the incoming request:
  153. $aicc = new aicc();
  154. $msg_array = $aicc->parse_ini_string_quotes_safe($hacp_aicc_data, ['core_lesson', 'core_vendor']);
  155. foreach ($msg_array as $key => $dummy) {
  156. switch (strtolower($key)) {
  157. case 'core':
  158. foreach ($msg_array[$key] as $subkey => $value) {
  159. switch (strtolower($subkey)) {
  160. case 'lesson_location':
  161. $oItem->set_lesson_location($value);
  162. break;
  163. case 'lesson_status':
  164. // Sometimes values are sent abbreviated
  165. switch ($value) {
  166. case 'C':
  167. $value = 'completed';
  168. break;
  169. case 'I':
  170. $value = 'incomplete';
  171. break;
  172. case 'N':
  173. case 'NA':
  174. $value = 'not attempted';
  175. break;
  176. case 'P':
  177. $value = 'passed';
  178. break;
  179. case 'B':
  180. $value = 'browsed';
  181. break;
  182. default:
  183. break;
  184. }
  185. $oItem->set_status($value);
  186. break;
  187. case 'score':
  188. $oItem->set_score($value);
  189. break;
  190. case 'time':
  191. if (strpos($value, ':') !== false) {
  192. $oItem->set_time($value, 'scorm');
  193. } else {
  194. $oItem->set_time($value);
  195. }
  196. break;
  197. }
  198. }
  199. break;
  200. case 'core_lesson':
  201. $oItem->current_data = $msg_array[$key];
  202. break;
  203. case 'comments':
  204. break;
  205. case 'objectives_status':
  206. break;
  207. case 'student_data':
  208. break;
  209. case 'student_preferences':
  210. break;
  211. }
  212. }
  213. $error_code = 0;
  214. $error_text = $errors[$error_code];
  215. $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf;
  216. $oItem->save(false);
  217. break;
  218. case 'putcomments':
  219. $error_code = 0;
  220. $error_text = $errors[$error_code];
  221. $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf;
  222. break;
  223. case 'putobjectives':
  224. $error_code = 0;
  225. $error_text = $errors[$error_code];
  226. $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf;
  227. break;
  228. case 'putpath':
  229. $error_code = 0;
  230. $error_text = $errors[$error_code];
  231. $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf;
  232. break;
  233. case 'putinteractions':
  234. $error_code = 0;
  235. $error_text = $errors[$error_code];
  236. $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf;
  237. break;
  238. case 'putperformance':
  239. $error_code = 0;
  240. $error_text = $errors[$error_code];
  241. $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf;
  242. break;
  243. case 'exitau':
  244. $error_code = 0;
  245. $error_text = $errors[$error_code];
  246. $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf.$s_ad.$crlf;
  247. break;
  248. default:
  249. $error_code = 1;
  250. $error_text = $errors[1];
  251. $result = $s_ec.$error_code.$crlf.$s_et.$error_text.$crlf;
  252. }
  253. }
  254. Session::write('lpobject', serialize($oLP));
  255. Session::write('oLP', $oLP);
  256. session_write_close();
  257. // Content type must be text/plain.
  258. header('Content-type: text/plain');
  259. echo $result;