aicc_hacp.php 12 KB

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