aicc_hacp.php 10 KB

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