lp_ajax_switch_item_toc.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This script contains the server part of the ajax interaction process. The client part is located
  6. * in lp_api.php or other api's.
  7. * This script updated the TOC of the SCORM without updating the SCO's attributes.
  8. *
  9. * @package chamilo.learnpath
  10. *
  11. * @author Yannick Warnier <ywarnier@beeznest.org>
  12. */
  13. // Flag to allow for anonymous user - needs to be set before global.inc.php.
  14. $use_anonymous = true;
  15. require_once __DIR__.'/../inc/global.inc.php';
  16. /**
  17. * Get one item's details.
  18. *
  19. * @param int $lpId LP ID
  20. * @param int $userId user ID
  21. * @param int $viewId View ID
  22. * @param int $currentItem Current item ID
  23. * @param int $nextItem New item ID
  24. *
  25. * @return string JavaScript commands to be executed in scorm_api.php
  26. */
  27. function switch_item_toc($lpId, $userId, $viewId, $currentItem, $nextItem)
  28. {
  29. $debug = 0;
  30. $return = '';
  31. if ($debug > 0) {
  32. error_log('In switch_item_toc('.$lpId.','.$userId.','.$viewId.','.$currentItem.','.$nextItem.')', 0);
  33. }
  34. $myLP = learnpath::getLpFromSession(api_get_course_id(), $lpId, $userId);
  35. $newItemId = 0;
  36. $oldItemId = 0;
  37. switch ($nextItem) {
  38. case 'next':
  39. $myLP->set_current_item($currentItem);
  40. $myLP->next();
  41. $newItemId = $myLP->get_current_item_id();
  42. if ($debug > 1) {
  43. error_log('In {next} - next item is '.$newItemId.'(current: '.$currentItem.')', 0);
  44. }
  45. break;
  46. case 'previous':
  47. $myLP->set_current_item($currentItem);
  48. $myLP->previous();
  49. $newItemId = $myLP->get_current_item_id();
  50. if ($debug > 1) {
  51. error_log('In {previous} - next item is '.$newItemId.'(current: '.$currentItem.')', 0);
  52. }
  53. break;
  54. case 'first':
  55. $myLP->set_current_item($currentItem);
  56. $myLP->first();
  57. $newItemId = $myLP->get_current_item_id();
  58. if ($debug > 1) {
  59. error_log('In {first} - next item is '.$newItemId.'(current: '.$currentItem.')', 0);
  60. }
  61. break;
  62. case 'last':
  63. break;
  64. default:
  65. // Should be filtered to check it's not hacked
  66. if ($nextItem == $currentItem) {
  67. // If we're opening the same item again.
  68. $myLP->items[$currentItem]->restart();
  69. } else {
  70. $oldItemId = $currentItem;
  71. }
  72. $newItemId = $nextItem;
  73. $myLP->set_current_item($newItemId);
  74. if ($debug > 1) {
  75. error_log('In {default} - next item is '.$newItemId.'(current: '.$currentItem.')', 0);
  76. }
  77. break;
  78. }
  79. $myLP->start_current_item(true);
  80. if ($myLP->force_commit) {
  81. $myLP->save_current();
  82. }
  83. if (is_object($myLP->items[$newItemId])) {
  84. $myLPI = $myLP->items[$newItemId];
  85. } else {
  86. if ($debug > 1) {
  87. error_log('In switch_item_details - generating new item object', 0);
  88. }
  89. $myLPI = new learnpathItem($newItemId, $userId);
  90. $myLPI->set_lp_view($viewId);
  91. }
  92. /*
  93. * now get what's needed by the SCORM API:
  94. * -score
  95. * -max
  96. * -min
  97. * -lesson_status
  98. * -session_time
  99. * -suspend_data
  100. */
  101. $lessonStatus = $myLPI->get_status();
  102. $interactionsCount = $myLPI->get_interactions_count();
  103. /**
  104. * Interactions are not returned by switch_item at the moment, but please
  105. * leave commented code to allow for the addition of these in the future.
  106. */
  107. /*
  108. $interactionsString = '';
  109. for ($i = 0; $i < $interactionsCount; $i++) {
  110. $interactionsString .= ",[".$i.",'','','','','','','']";
  111. }
  112. if (!empty($interactionsString)) {
  113. $interactionsString = substr($interactionsString, 1);
  114. }
  115. */
  116. $totalItems = $myLP->getTotalItemsCountWithoutDirs();
  117. $completedItems = $myLP->get_complete_items_count();
  118. $progressMode = $myLP->get_progress_bar_mode();
  119. $progressMode = ($progressMode == '' ? '%' : $progressMode);
  120. $nextItemId = $myLP->get_next_item_id();
  121. $previousItemId = $myLP->get_previous_item_id();
  122. $itemType = $myLPI->get_type();
  123. $lessonMode = $myLPI->get_lesson_mode();
  124. $credit = $myLPI->get_credit();
  125. $launchData = $myLPI->get_launch_data();
  126. $objectivesCount = $myLPI->get_objectives_count();
  127. $coreExit = $myLPI->get_core_exit();
  128. $return .=
  129. "olms.lms_lp_id=".$lpId.";".
  130. "olms.lms_item_id=".$newItemId.";".
  131. "olms.lms_old_item_id=".$oldItemId.";".
  132. "olms.lms_initialized=0;".
  133. "olms.lms_view_id=".$viewId.";".
  134. "olms.lms_user_id=".$userId.";".
  135. "olms.next_item=".$newItemId.";".// This one is very important to replace possible literal strings.
  136. "olms.lms_next_item=".$nextItemId.";".
  137. "olms.lms_previous_item=".$previousItemId.";".
  138. "olms.lms_item_type = '".$itemType."';".
  139. "olms.lms_item_credit = '".$credit."';".
  140. "olms.lms_item_lesson_mode = '".$lessonMode."';".
  141. "olms.lms_item_launch_data = '".$launchData."';".
  142. "olms.lms_item_interactions_count = '".$interactionsCount."';".
  143. "olms.lms_item_objectives_count = '".$objectivesCount."';".
  144. "olms.lms_item_core_exit = '".$coreExit."';".
  145. "olms.asset_timer = 0;";
  146. $return .= "update_toc('unhighlight','".$currentItem."');".
  147. "update_toc('highlight','".$newItemId."');".
  148. "update_toc('$lessonStatus','".$newItemId."');".
  149. "update_progress_bar('$completedItems','$totalItems','$progressMode');";
  150. $myLP->set_error_msg('');
  151. $myLP->prerequisites_match(); // Check the prerequisites are all complete.
  152. if ($debug > 1) {
  153. error_log('prerequisites_match() returned '.htmlentities($myLP->error), 0);
  154. }
  155. Session::write('scorm_item_id', $newItemId);
  156. Session::write('lpobject', serialize($myLP));
  157. Session::write('oLP', $myLP);
  158. return $return;
  159. }
  160. echo switch_item_toc(
  161. $_POST['lid'],
  162. $_POST['uid'],
  163. $_POST['vid'],
  164. $_POST['iid'],
  165. $_POST['next']
  166. );