lp_comm.server.php 16 KB

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