lp_ajax_switch_item_toc.php 6.0 KB

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