lp_comm.server.php 18 KB

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