lp_comm.server.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script contains the server part of the xajax interaction process. The client part is located
  5. * in lp_api.php or other api's.
  6. * This is a first attempt at using xajax and AJAX in general, so the code might be a bit unsettling.
  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. * Backup an item's values into the javascript API as "old" values (so we still have them at hand)
  15. * @param integer Learnpath ID
  16. * @param integer User ID
  17. * @param integer View ID
  18. * @param integer Item ID
  19. * @param double Current score
  20. * @param double Maximum score
  21. * @param double Minimum score
  22. * @param string Lesson status
  23. * @param string Session time
  24. * @param string Suspend data
  25. * @param string Lesson location
  26. */
  27. function backup_item_details($lp_id, $user_id, $view_id, $item_id, $score = -1, $max = -1, $min = -1, $status = '', $time = '', $suspend = '', $location = '') {
  28. $objResponse = new xajaxResponse();
  29. $objResponse->addScript(
  30. "old_score=".$score.";" .
  31. "old_max=".$max.";" .
  32. "old_min=".$min.";" .
  33. "old_lesson_status='".$status."';" .
  34. "old_session_time='".$time."';" .
  35. "lms_old_item_id='".$item_id."';" .
  36. "old_suspend_data='".$suspend."';" .
  37. "old_lesson_location='".$location."';");
  38. //$objResponse->addAlert('data for item '.$item_id.', user '.$user_id.' backed up');
  39. return $objResponse;
  40. }
  41. /**
  42. * Writes an item's new values into the database and returns the operation result
  43. * @param integer Learnpath ID
  44. * @param integer User ID
  45. * @param integer View ID
  46. * @param integer Item ID
  47. * @param double Current score
  48. * @param double Maximum score
  49. * @param double Minimum score
  50. * @param string Lesson status
  51. * @param string Session time
  52. * @param string Suspend data
  53. * @param string Lesson location
  54. * @param string Core exit SCORM string
  55. */
  56. function save_item($lp_id, $user_id, $view_id, $item_id, $score = -1, $max = -1, $min = -1, $status = '', $time = 0, $suspend = '', $location = '', $interactions = array(), $core_exit = 'none') {
  57. global $_configuration;
  58. $debug = 0;
  59. if ($debug > 0) { error_log('In xajax_save_item('.$lp_id.','.$user_id.','.$view_id.','.$item_id.','.$score.','.$max.','.$min.',"'.$status.'",'.$time.',"'.$suspend.'","'.$location.'","'.(count($interactions)>0?$interactions[0]:'').'","'.$core_exit.'")', 0); }
  60. $objResponse = new xajaxResponse();
  61. $mylp = learnpath::getLpFromSession(api_get_course_id(), $lp_id, $user_id);
  62. $prereq_check = $mylp->prerequisites_match($item_id);
  63. if ($prereq_check === true) { // Launch the prerequisites check and set error if needed.
  64. $mylpi =& $mylp->items[$item_id];
  65. //$mylpi->set_lp_view($view_id);
  66. if ($max != -1) {
  67. $mylpi->max_score = $max;
  68. }
  69. if ($min != -1) {
  70. $mylpi->min_score = $min;
  71. }
  72. if ($score != -1) {
  73. $mylpi->set_score($score);
  74. }
  75. if ($status != '') {
  76. if ($debug > 1) { error_log('Calling set_status('.$status.') from xajax', 0); }
  77. $mylpi->set_status($status);
  78. if ($debug > 1) { error_log('Done calling set_status from xajax', 0); }
  79. }
  80. if ($time != '') {
  81. // If big integer, then it's a timestamp, otherwise it's normal scorm time.
  82. if ($time == intval(strval($time)) && $time > 1000000) {
  83. $real_time = time() - $time;
  84. //$real_time += $mylpi->get_total_time();
  85. $mylpi->set_time($real_time, 'int');
  86. } else {
  87. $mylpi->set_time($time);
  88. }
  89. }
  90. if ($suspend != '') {
  91. $mylpi->current_data = $suspend; //escapetxt($suspend);
  92. }
  93. if ($location != '') {
  94. $mylpi->set_lesson_location($location);
  95. }
  96. // Deal with interactions provided in arrays in the following format
  97. // id(0), type(1), time(2), weighting(3), correct_responses(4), student_response(5), result(6), latency(7)
  98. if (is_array($interactions) && count($interactions) > 0) {
  99. foreach ($interactions as $index => $interaction) {
  100. $mylpi->add_interaction($index, $interactions[$index]);
  101. }
  102. }
  103. $mylpi->set_core_exit($core_exit);
  104. $mylp->save_item($item_id, false);
  105. } else {
  106. return $objResponse;
  107. }
  108. $mystatus = $mylpi->get_status(false);
  109. $mytotal = $mylp->get_total_items_count_without_chapters();
  110. $mycomplete = $mylp->get_complete_items_count();
  111. $myprogress_mode = $mylp->get_progress_bar_mode();
  112. $myprogress_mode = ($myprogress_mode == '' ? '%' : $myprogress_mode);
  113. //$mylpi->write_to_db();
  114. $_SESSION['lpobject'] = serialize($mylp);
  115. if ($mylpi->get_type()!='sco'){
  116. // If this object's JS status has not been updated by the SCORM API, update now.
  117. $objResponse->addScript("lesson_status='".$mystatus."';");
  118. }
  119. $objResponse->addScript("update_toc('".$mystatus."','".$item_id."');");
  120. $update_list = $mylp->get_update_queue();
  121. foreach ($update_list as $my_upd_id => $my_upd_status) {
  122. if ($my_upd_id != $item_id) { // Only update the status from other items (i.e. parents and brothers), do not update current as we just did it already.
  123. $objResponse->addScript("update_toc('".$my_upd_status."','".$my_upd_id."');");
  124. }
  125. }
  126. $objResponse->addScript("update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');");
  127. if ($debug > 0) {
  128. $objResponse->addScript("logit_lms('Saved data for item ".$item_id.", user ".$user_id." (status=".$mystatus.")',2)");
  129. if ($debug > 1) { error_log('End of xajax_save_item()', 0); }
  130. }
  131. if (!isset($_SESSION['login_as'])) {
  132. // If $_SESSION['login_as'] is set, then the user is an admin logged as the user.
  133. $tbl_track_login = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  134. $sql_last_connection = "SELECT login_id, login_date FROM $tbl_track_login
  135. WHERE login_user_id='".api_get_user_id()."' ORDER BY login_date DESC LIMIT 0,1";
  136. $q_last_connection = Database::query($sql_last_connection);
  137. if (Database::num_rows($q_last_connection) > 0) {
  138. $row = Database::fetch_array($q_last_connection);
  139. $i_id_last_connection = $row['login_id'];
  140. $now = api_get_utc_datetime();
  141. $s_sql_update_logout_date = "UPDATE $tbl_track_login SET logout_date = '$now' WHERE login_id='$i_id_last_connection'";
  142. Database::query($s_sql_update_logout_date);
  143. }
  144. }
  145. return $objResponse;
  146. }
  147. /**
  148. * Writes an item's new values into the database and returns the operation result
  149. * @param integer Learnpath ID
  150. * @param integer User ID
  151. * @param integer View ID
  152. * @param integer Item ID
  153. * @param array Objectives array
  154. */
  155. function save_objectives($lp_id, $user_id, $view_id, $item_id, $objectives = array()) {
  156. global $_configuration;
  157. $debug = 0;
  158. if ($debug > 0) { error_log('In xajax_save_objectives('.$lp_id.','.$user_id.','.$view_id.','.$item_id.',"'.(count($objectives) > 0 ? count($objectives) : '').'")', 0); }
  159. $objResponse = new xajaxResponse();
  160. $mylp = learnpath::getLpFromSession(api_get_course_id(), $lp_id, $user_id);
  161. $mylpi =& $mylp->items[$item_id];
  162. if (is_array($objectives) && count($objectives)>0){
  163. foreach($objectives as $index=>$objective){
  164. //error_log(__FILE__.' '.__LINE__.' '.$objectives[$index][0], 0);
  165. $mylpi->add_objective($index,$objectives[$index]);
  166. }
  167. $mylpi->write_objectives_to_db();
  168. }
  169. return $objResponse;
  170. }
  171. /**
  172. * Get one item's details
  173. * @param integer LP ID
  174. * @param integer user ID
  175. * @param integer View ID
  176. * @param integer Current item ID
  177. * @param integer New item ID
  178. */
  179. function switch_item_details($lp_id, $user_id, $view_id, $current_item, $next_item) {
  180. global $charset;
  181. $debug = 0;
  182. if ($debug > 0) { error_log('In xajax_switch_item_details('.$lp_id.','.$user_id.','.$view_id.','.$current_item.','.$next_item.')', 0); }
  183. $objResponse = new xajaxResponse();
  184. /*$item_id may be one of:
  185. * -'next'
  186. * -'previous'
  187. * -'first'
  188. * -'last'
  189. * - a real item ID
  190. */
  191. $mylp = learnpath::getLpFromSession(api_get_course_id(), $lp_id, $user_id);
  192. $new_item_id = 0;
  193. switch ($next_item) {
  194. case 'next':
  195. $mylp->set_current_item($current_item);
  196. $mylp->next();
  197. $new_item_id = $mylp->get_current_item_id();
  198. if ($debug > 1) { error_log('In {next} - next item is '.$new_item_id.'(current: '.$current_item.')', 0); }
  199. break;
  200. case 'previous':
  201. $mylp->set_current_item($current_item);
  202. $mylp->previous();
  203. $new_item_id = $mylp->get_current_item_id();
  204. if ($debug > 1) { error_log('In {previous} - next item is '.$new_item_id.'(current: '.$current_item.')', 0); }
  205. break;
  206. case 'first':
  207. $mylp->set_current_item($current_item);
  208. $mylp->first();
  209. $new_item_id = $mylp->get_current_item_id();
  210. if ($debug > 1) { error_log('In {first} - next item is '.$new_item_id.'(current: '.$current_item.')', 0); }
  211. break;
  212. case 'last':
  213. break;
  214. default:
  215. // Should be filtered to check it's not hacked.
  216. if($next_item == $current_item){
  217. // If we're opening the same item again.
  218. $mylp->items[$current_item]->restart();
  219. }
  220. $new_item_id = $next_item;
  221. $mylp->set_current_item($new_item_id);
  222. if ($debug > 1) { error_log('In {default} - next item is '.$new_item_id.'(current: '.$current_item.')', 0); }
  223. break;
  224. }
  225. $mylp->start_current_item(true);
  226. if ($mylp->force_commit){
  227. $mylp->save_current();
  228. }
  229. //$objResponse->addAlert(api_get_path(REL_CODE_PATH).'newscorm/learnpathItem.class.php');
  230. if (is_object($mylp->items[$new_item_id])) {
  231. $mylpi = $mylp->items[$new_item_id];
  232. } else {
  233. if ($debug > 1) { error_log('In switch_item_details - generating new item object', 0); }
  234. $mylpi = new learnpathItem($new_item_id, $user_id);
  235. $mylpi->set_lp_view($view_id);
  236. }
  237. /*
  238. * now get what's needed by the SCORM API:
  239. * -score
  240. * -max
  241. * -min
  242. * -lesson_status
  243. * -session_time
  244. * -suspend_data
  245. */
  246. $myscore = $mylpi->get_score();
  247. $mymax = $mylpi->get_max();
  248. $mymin = $mylpi->get_min();
  249. $mylesson_status = $mylpi->get_status();
  250. $mylesson_location = $mylpi->get_lesson_location();
  251. $mytotal_time = $mylpi->get_scorm_time('js');
  252. $mymastery_score = $mylpi->get_mastery_score();
  253. $mymax_time_allowed = $mylpi->get_max_time_allowed();
  254. $mylaunch_data = $mylpi->get_launch_data();
  255. /*
  256. if ($mylpi->get_type() == 'asset') {
  257. // Temporary measure to save completion of an asset. Later on, Chamilo should trigger something on unload, maybe... (even though that would mean the last item cannot be completed)
  258. $mylesson_status = 'completed';
  259. $mylpi->set_status('completed');
  260. $mylpi->save();
  261. }
  262. */
  263. $mysession_time = $mylpi->get_total_time();
  264. $mysuspend_data = $mylpi->get_suspend_data();
  265. $mylesson_location = $mylpi->get_lesson_location();
  266. $objResponse->addScript(
  267. "score=".$myscore.";" .
  268. "max=".$mymax.";" .
  269. "min=".$mymin.";" .
  270. "lesson_status='".$mylesson_status."';" .
  271. "lesson_location='".$mylesson_location."';" .
  272. "session_time='".$mysession_time."';" .
  273. "suspend_data='".$mysuspend_data."';" .
  274. "lesson_location='".$mylesson_location."';" .
  275. "total_time = '".$mytotal_time."';" .
  276. "mastery_score = '".$mymastery_score."';" .
  277. "max_time_allowed = '".$mymax_time_allowed."';" .
  278. "launch_data = '".$mylaunch_data."';" .
  279. "interactions = new Array();" .
  280. "item_objectives = new Array();" .
  281. "G_lastError = 0;" .
  282. "G_LastErrorMessage = 'No error';");
  283. /*
  284. * and re-initialise the rest
  285. * -saved_lesson_status = 'not attempted'
  286. * -lms_lp_id
  287. * -lms_item_id
  288. * -lms_old_item_id
  289. * -lms_new_item_id
  290. * -lms_been_synchronized
  291. * -lms_initialized
  292. * -lms_total_lessons
  293. * -lms_complete_lessons
  294. * -lms_progress_bar_mode
  295. * -lms_view_id
  296. * -lms_user_id
  297. */
  298. $mytotal = $mylp->get_total_items_count_without_chapters();
  299. $mycomplete = $mylp->get_complete_items_count();
  300. $myprogress_mode = $mylp->get_progress_bar_mode();
  301. $myprogress_mode = ($myprogress_mode == '' ? '%' : $myprogress_mode);
  302. $mynext = $mylp->get_next_item_id();
  303. $myprevious = $mylp->get_previous_item_id();
  304. $myitemtype = $mylpi->get_type();
  305. $mylesson_mode = $mylpi->get_lesson_mode();
  306. $mycredit = $mylpi->get_credit();
  307. $mylaunch_data = $mylpi->get_launch_data();
  308. $myinteractions_count = $mylpi->get_interactions_count();
  309. $myobjectives_count = $mylpi->get_objectives_count();
  310. $mycore_exit = $mylpi->get_core_exit();
  311. $objResponse->addScript(
  312. "saved_lesson_status='not attempted';" .
  313. "lms_lp_id=".$lp_id.";" .
  314. "lms_item_id=".$new_item_id.";" .
  315. "lms_old_item_id=0;" .
  316. "lms_been_synchronized=0;" .
  317. "lms_initialized=0;" .
  318. "lms_total_lessons=".$mytotal.";" .
  319. "lms_complete_lessons=".$mycomplete.";" .
  320. "lms_progress_bar_mod='".$myprogress_mode."';" .
  321. "lms_view_id=".$view_id.";" .
  322. "lms_user_id=".$user_id.";" .
  323. "next_item=".$new_item_id.";" . // This one is very important to replace possible literal strings.
  324. "lms_next_item=".$mynext.";" .
  325. "lms_previous_item=".$myprevious.";" .
  326. "lms_item_type = '".$myitemtype."';" .
  327. "lms_item_credit = '".$mycredit."';" .
  328. "lms_item_lesson_mode = '".$mylesson_mode."';" .
  329. "lms_item_launch_data = '".$mylaunch_data."';" .
  330. "lms_item_interactions_count = '".$myinteractions_count."';" .
  331. "lms_item_objectives_count = '".$myinteractions_count."';" .
  332. "lms_item_core_exit = '".$mycore_exit."';" .
  333. "asset_timer = 0;"
  334. );
  335. $objResponse->addScript("update_toc('unhighlight','".$current_item."');");
  336. $objResponse->addScript("update_toc('highlight','".$new_item_id."');");
  337. $objResponse->addScript("update_toc('$mylesson_status','".$new_item_id."');");
  338. $objResponse->addScript("update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');");
  339. $mylp->set_error_msg('');
  340. $mylp->prerequisites_match(); // Check the prerequisites are all complete.
  341. if ($debug > 1) { error_log('Prereq_match() returned '.api_htmlentities($mylp->error, ENT_QUOTES, $charset), 0); }
  342. $objResponse->addScript("update_message_frame('".str_replace("'", "\'", api_htmlentities($mylp->error, ENT_QUOTES, $charset))."');");
  343. $_SESSION['scorm_item_id'] = $new_item_id; // Save the new item ID for the exercise tool to use.
  344. $_SESSION['lpobject'] = serialize($mylp);
  345. return $objResponse;
  346. }
  347. /**
  348. * Start a timer and hand it back to the JS by assigning the current time (of start) to
  349. * var asset_timer
  350. */
  351. function start_timer() {
  352. $objResponse = new xajaxResponse();
  353. $time = time();
  354. $objResponse->addScript("asset_timer='$time';asset_timer_total=0;");
  355. return $objResponse;
  356. }
  357. require 'lp_comm.common.php';
  358. $xajax->processRequests();