lp_ajax_switch_item_toc.php 6.0 KB

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