lp_ajax_save_item.php 11 KB

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