lp_comm.server.php 18 KB

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