lp_ajax_save_item.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. * Writes an item's new values into the database and returns the operation result
  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. * @param array Interactions array
  29. * @param string Core exit SCORM string
  30. */
  31. 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') {
  32. global $_configuration;
  33. $debug = 0;
  34. $return = '';
  35. if ($debug > 0) { error_log('In 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); }
  36. //$objResponse = new xajaxResponse();
  37. require_once 'learnpath.class.php';
  38. require_once 'scorm.class.php';
  39. require_once 'aicc.class.php';
  40. require_once 'learnpathItem.class.php';
  41. require_once 'scormItem.class.php';
  42. require_once 'aiccItem.class.php';
  43. $mylp = '';
  44. if (isset($_SESSION['lpobject'])) {
  45. if ($debug > 1) { error_log('////$_SESSION[lpobject] is set', 0); }
  46. $oLP =& unserialize($_SESSION['lpobject']);
  47. if (!is_object($oLP)) {
  48. if ($debug > 2) { error_log(print_r($oLP,true), 0); }
  49. if ($debug > 2) { error_log('////Building new lp', 0); }
  50. unset($oLP);
  51. $code = api_get_course_id();
  52. $mylp = & new learnpath($code, $lp_id, $user_id);
  53. } else {
  54. if ($debug > 2) { error_log('////Reusing session lp', 0); }
  55. $mylp = & $oLP;
  56. }
  57. }
  58. //$objResponse->addAlert(api_get_path(REL_CODE_PATH).'newscorm/learnpathItem.class.php');
  59. if (!is_a($mylp, 'learnpath')) { return ''; }
  60. $prereq_check = $mylp->prerequisites_match($item_id);
  61. if ($prereq_check === true) { // Launch the prerequisites check and set error if needed.
  62. $mylpi =& $mylp->items[$item_id];
  63. //$mylpi->set_lp_view($view_id);
  64. if (isset($max) && $max != -1) {
  65. $mylpi->max_score = $max;
  66. $mylpi->set_max_score($max);
  67. }
  68. if (isset($min) && $min != -1 && $min != 'undefined') {
  69. $mylpi->min_score = $min;
  70. }
  71. if (isset($score) && $score != -1) {
  72. if ($debug > 1) { error_log('Calling set_score('.$score.') from xajax', 0); }
  73. $mylpi->set_score($score);
  74. if ($debug > 1) { error_log('Done calling set_score from xajax - now '.$mylpi->get_score(), 0); }
  75. }
  76. if (isset($status) && $status != '' && $status != 'undefined') {
  77. if ($debug > 1) { error_log('Calling set_status('.$status.') from xajax', 0); }
  78. $mylpi->set_status($status);
  79. if ($debug > 1) { error_log('Done calling set_status from xajax - now '.$mylpi->get_status(false), 0); }
  80. }
  81. // Hack to set status to completed for hotpotatoes if score > 80%.
  82. if ($mylpi->get_type() == 'hotpotatoes') {
  83. if ((empty($status) || $status == 'undefined' || $status == 'not attempted') && $max > 0) {
  84. if (($score/$max) > 0.8) {
  85. $mystatus = 'completed';
  86. if ($debug > 1) { error_log('Calling set_status('.$mystatus.') from xajax for hotpotatoes', 0); }
  87. $mylpi->set_status($mystatus);
  88. if ($debug > 1) { error_log('Done calling set_status from xajax for hotpotatoes - now '.$mylpi->get_status(false), 0); }
  89. }
  90. } elseif ($status == 'completed' && $max > 0 && ($score/$max) < 0.8) {
  91. $mystatus = 'failed';
  92. if ($debug > 1) { error_log('Calling set_status('.$mystatus.') from xajax for hotpotatoes', 0); }
  93. $mylpi->set_status($mystatus);
  94. if ($debug > 1) { error_log('Done calling set_status from xajax for hotpotatoes - now '.$mylpi->get_status(false), 0); }
  95. }
  96. }
  97. if (isset($time) && $time!='' && $time!='undefined') {
  98. // If big integer, then it's a timestamp, otherwise it's normal scorm time.
  99. if ($debug > 1) { error_log('Calling set_time('.$time.') from xajax', 0); }
  100. if ($time == intval(strval($time)) && $time > 1000000) {
  101. $real_time = time() - $time;
  102. //$real_time += $mylpi->get_total_time();
  103. $mylpi->set_time($real_time, 'int');
  104. } else {
  105. $mylpi->set_time($time);
  106. }
  107. if ($debug > 1) { error_log('Done calling set_time from xajax - now '.$mylpi->get_total_time(), 0); }
  108. } else {
  109. $time = $mylpi->get_total_time();
  110. }
  111. if (isset($suspend) && $suspend != '' && $suspend != 'undefined') {
  112. $mylpi->current_data = $suspend; //escapetxt($suspend);
  113. }
  114. if (isset($location) && $location != '' && $location!='undefined') {
  115. $mylpi->set_lesson_location($location);
  116. }
  117. // Deal with interactions provided in arrays in the following format:
  118. // id(0), type(1), time(2), weighting(3), correct_responses(4), student_response(5), result(6), latency(7)
  119. if (is_array($interactions) && count($interactions) > 0) {
  120. foreach ($interactions as $index => $interaction) {
  121. //$mylpi->add_interaction($index,$interactions[$index]);
  122. //fix DT#4444
  123. $clean_interaction = str_replace('@.|@', ',', $interactions[$index]);
  124. $mylpi->add_interaction($index, $clean_interaction);
  125. }
  126. }
  127. if ($core_exit != 'undefined') {
  128. $mylpi->set_core_exit($core_exit);
  129. }
  130. $mylp->save_item($item_id, false);
  131. } else {
  132. //return $objResponse;
  133. return $return;
  134. }
  135. $mystatus_in_db = $mylpi->get_status(true);
  136. if ($mystatus_in_db != 'completed' && $mystatus_in_db != 'passed' && $mystatus_in_db != 'browsed') {
  137. $mystatus_in_memory = $mylpi->get_status(false);
  138. if ($mystatus_in_memory != $mystatus_in_db) {
  139. $mystatus = $mystatus_in_memory;
  140. } else {
  141. $mystatus = $mystatus_in_db;
  142. }
  143. } else {
  144. $mystatus = $mystatus_in_db;
  145. }
  146. $mytotal = $mylp->get_total_items_count_without_chapters();
  147. $mycomplete = $mylp->get_complete_items_count();
  148. $myprogress_mode = $mylp->get_progress_bar_mode();
  149. $myprogress_mode = ($myprogress_mode == '' ? '%' : $myprogress_mode);
  150. //$mylpi->write_to_db();
  151. $_SESSION['lpobject'] = serialize($mylp);
  152. if ($mylpi->get_type() != 'sco') {
  153. // If this object's JS status has not been updated by the SCORM API, update now.
  154. //$objResponse->addScript("lesson_status='".$mystatus."';");
  155. $return .= "olms.lesson_status='".$mystatus."';";
  156. }
  157. //$objResponse->addScript("update_toc('".$mystatus."','".$item_id."');");
  158. $return .= "update_toc('".$mystatus."','".$item_id."');";
  159. $update_list = $mylp->get_update_queue();
  160. foreach ($update_list as $my_upd_id => $my_upd_status) {
  161. 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.
  162. //$objResponse->addScript("update_toc('".$my_upd_status."','".$my_upd_id."');");
  163. $return .= "update_toc('".$my_upd_status."','".$my_upd_id."');";
  164. }
  165. }
  166. //$objResponse->addScript("update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');");
  167. $return .= "update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');";
  168. if ($debug > 0) {
  169. //$objResponse->addScript("logit_lms('Saved data for item ".$item_id.", user ".$user_id." (status=".$mystatus.")',2);");
  170. $return .= "logit_lms('Saved data for item ".$item_id.", user ".$user_id." (status=".$mystatus.")',2);";
  171. if ($debug > 1) { error_log('End of save_item()', 0); }
  172. }
  173. if (!isset($_SESSION['login_as'])) {
  174. // If $_SESSION['login_as'] is set, then the user is an admin logged as the user.
  175. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  176. $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";
  177. $q_last_connection = Database::query($sql_last_connection);
  178. if (Database::num_rows($q_last_connection) > 0) {
  179. $current_time = date('Y-m-d H:i:s');
  180. $row = Database::fetch_array($q_last_connection);
  181. $i_id_last_connection = $row['login_id'];
  182. $s_sql_update_logout_date = "UPDATE $tbl_track_login SET logout_date='".$current_time."' WHERE login_id='$i_id_last_connection'";
  183. Database::query($s_sql_update_logout_date);
  184. }
  185. }
  186. if ($mylp->get_type() == 2) {
  187. $return .= "update_stats();";
  188. }
  189. return $return;
  190. //return $objResponse;
  191. }
  192. $interactions = array();
  193. if (isset($_REQUEST['interact'])) {
  194. if (is_array($_REQUEST['interact'])) {
  195. foreach ($_REQUEST['interact'] as $idx => $interac) {
  196. $interactions[$idx] = split(',', substr($interac, 1, -1));
  197. if(!isset($interactions[$idx][7])){ // Make sure there are 7 elements.
  198. $interactions[$idx][7] = '';
  199. }
  200. }
  201. }
  202. }
  203. echo save_item(
  204. $_REQUEST['lid'],
  205. $_REQUEST['uid'],
  206. $_REQUEST['vid'],
  207. $_REQUEST['iid'],
  208. $_REQUEST['s'],
  209. $_REQUEST['max'],
  210. $_REQUEST['min'],
  211. $_REQUEST['status'],
  212. $_REQUEST['t'],
  213. $_REQUEST['suspend'],
  214. $_REQUEST['loc'],
  215. $interactions,
  216. $_REQUEST['core_exit']);