lp_ajax_save_item.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. require_once 'learnpath.class.php';
  19. require_once 'scorm.class.php';
  20. require_once 'aicc.class.php';
  21. require_once 'learnpathItem.class.php';
  22. require_once 'scormItem.class.php';
  23. require_once 'aiccItem.class.php';
  24. /**
  25. * Writes an item's new values into the database and returns the operation result
  26. * @param integer Learnpath ID
  27. * @param integer User ID
  28. * @param integer View ID
  29. * @param integer Item ID
  30. * @param double Current score
  31. * @param double Maximum score
  32. * @param double Minimum score
  33. * @param string Lesson status
  34. * @param string Session time
  35. * @param string Suspend data
  36. * @param string Lesson location
  37. * @param array Interactions array
  38. * @param string Core exit SCORM string
  39. */
  40. 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') {
  41. global $_configuration;
  42. $return = '';
  43. $debug = 0;
  44. if ($debug > 0) {
  45. error_log('lp_ajax_save_item.php : save_item params: ');
  46. error_log("lp_id: $lp_id - user_id: - $user_id - view_id: $view_id - item_id: $item_id");
  47. error_log("score: $score - max:$max - min: $min - status:$status - time:$time - suspend: $suspend - location: $location - core_exit: $core_exit");
  48. }
  49. $mylp = null;
  50. if (isset($_SESSION['lpobject'])) {
  51. $oLP = unserialize($_SESSION['lpobject']);
  52. if (!is_object($oLP)) {
  53. unset($oLP);
  54. $code = api_get_course_id();
  55. $mylp = new learnpath($code, $lp_id, $user_id);
  56. } else {
  57. $mylp = & $oLP;
  58. }
  59. }
  60. if (!is_a($mylp, 'learnpath')) {
  61. return '';
  62. }
  63. $prereq_check = $mylp->prerequisites_match($item_id);
  64. $mylpi = $mylp->items[$item_id];
  65. //This functions sets the $this->db_item_view_id variable needed in get_status() see BT#5069
  66. $mylpi->set_lp_view($view_id);
  67. if ($prereq_check === true) {
  68. if ($debug > 1) { error_log('Prereq are check'); }
  69. // Launch the prerequisites check and set error if needed
  70. if (isset($max) && $max != -1) {
  71. $mylpi->max_score = $max;
  72. $mylpi->set_max_score($max);
  73. if ($debug > 1) { error_log("Setting max_score: $max"); }
  74. }
  75. if (isset($min) && $min != -1 && $min != 'undefined') {
  76. $mylpi->min_score = $min;
  77. if ($debug > 1) { error_log("Setting min_score: $min"); }
  78. }
  79. //set_score function already saves the status
  80. if (isset($score) && $score != -1) {
  81. if ($debug > 1) { error_log('Calling set_score('.$score.')', 0); }
  82. $mylpi->set_score($score);
  83. if ($debug > 1) { error_log('Done calling set_score '.$mylpi->get_score(), 0); }
  84. } else {
  85. if ($debug > 1) { error_log("Score not updated"); }
  86. //Default behaviour
  87. if (isset($status) && $status != '' && $status != 'undefined') {
  88. //Implements scorm 1.2 constraint
  89. //If SCO_MasteryScore does not evaluate to a number, passed/failed status won't be set at all
  90. //Score was not set
  91. //@todo remove the switch
  92. switch ($status) {
  93. case 'failed':
  94. case 'passed':
  95. $mylpi->set_status($status);
  96. //if ($debug > 1) { error_log("Cant't set status to these values only if a score was set"); }
  97. break;
  98. default:
  99. if ($debug > 1) { error_log('Calling set_status('.$status.')', 0); }
  100. $mylpi->set_status($status);
  101. if ($debug > 1) { error_log('Done calling set_status: checking from memory:'.$mylpi->get_status(false), 0); }
  102. break;
  103. }
  104. } else {
  105. if ($debug > 1) { error_log("Status not updated"); }
  106. }
  107. }
  108. // Hack to set status to completed for hotpotatoes if score > 80%.
  109. $my_type = $mylpi->get_type();
  110. if ($my_type == 'hotpotatoes') {
  111. if ((empty($status) || $status == 'undefined' || $status == 'not attempted') && $max > 0) {
  112. if (($score/$max) > 0.8) {
  113. $mystatus = 'completed';
  114. if ($debug > 1) { error_log('Calling set_status('.$mystatus.') for hotpotatoes', 0); }
  115. $mylpi->set_status($mystatus);
  116. if ($debug > 1) { error_log('Done calling set_status for hotpotatoes - now '.$mylpi->get_status(false), 0); }
  117. }
  118. } elseif ($status == 'completed' && $max > 0 && ($score/$max) < 0.8) {
  119. $mystatus = 'failed';
  120. if ($debug > 1) { error_log('Calling set_status('.$mystatus.') for hotpotatoes', 0); }
  121. $mylpi->set_status($mystatus);
  122. if ($debug > 1) { error_log('Done calling set_status for hotpotatoes - now '.$mylpi->get_status(false), 0); }
  123. }
  124. }
  125. if (isset($time) && $time!='' && $time!='undefined') {
  126. // If big integer, then it's a timestamp, otherwise it's normal scorm time.
  127. if ($debug > 1) { error_log('Calling set_time('.$time.') ', 0); }
  128. if ($time == intval(strval($time)) && $time > 1000000) {
  129. if ($debug > 1) { error_log("Time is INT"); }
  130. $real_time = time() - $time;
  131. if ($debug > 1) { error_log('Calling $real_time '.$real_time.' ', 0); }
  132. $mylpi->set_time($real_time, 'int');
  133. } else {
  134. if ($debug > 1) { error_log("Time is in SCORM format"); }
  135. if ($debug > 1) { error_log('Calling $time '.$time.' ', 0); }
  136. $mylpi->set_time($time, 'scorm');
  137. }
  138. //if ($debug > 1) { error_log('Done calling set_time - now '.$mylpi->get_total_time(), 0); }
  139. } else {
  140. $time = $mylpi->get_total_time();
  141. }
  142. if (isset($suspend) && $suspend != '' && $suspend != 'undefined') {
  143. $mylpi->current_data = $suspend; //escapetxt($suspend);
  144. }
  145. if (isset($location) && $location != '' && $location!='undefined') {
  146. $mylpi->set_lesson_location($location);
  147. }
  148. // Deal with interactions provided in arrays in the following format:
  149. // id(0), type(1), time(2), weighting(3), correct_responses(4), student_response(5), result(6), latency(7)
  150. if (is_array($interactions) && count($interactions) > 0) {
  151. foreach ($interactions as $index => $interaction) {
  152. //$mylpi->add_interaction($index,$interactions[$index]);
  153. //fix DT#4444
  154. $clean_interaction = str_replace('@.|@', ',', $interactions[$index]);
  155. $mylpi->add_interaction($index, $clean_interaction);
  156. }
  157. }
  158. if ($core_exit != 'undefined') {
  159. $mylpi->set_core_exit($core_exit);
  160. }
  161. $mylp->save_item($item_id, false);
  162. } else {
  163. return $return;
  164. }
  165. $mystatus_in_db = $mylpi->get_status(true);
  166. error_log("Status in DB: $mystatus_in_db");
  167. if ($mystatus_in_db != 'completed' && $mystatus_in_db != 'passed' && $mystatus_in_db != 'browsed' && $mystatus_in_db != 'failed') {
  168. $mystatus_in_memory = $mylpi->get_status(false);
  169. if ($mystatus_in_memory != $mystatus_in_db) {
  170. $mystatus = $mystatus_in_memory;
  171. } else {
  172. $mystatus = $mystatus_in_db;
  173. }
  174. } else {
  175. $mystatus = $mystatus_in_db;
  176. }
  177. $mytotal = $mylp->get_total_items_count_without_chapters();
  178. $mycomplete = $mylp->get_complete_items_count();
  179. $myprogress_mode = $mylp->get_progress_bar_mode();
  180. $myprogress_mode = $myprogress_mode == '' ? '%' : $myprogress_mode;
  181. if ($debug > 1) { error_log("mystatus: $mystatus", 0); }
  182. if ($debug > 1) { error_log("myprogress_mode: $myprogress_mode", 0); }
  183. if ($debug > 1) { error_log("mytotal: $mytotal", 0); }
  184. if ($debug > 1) { error_log("mycomplete: $mycomplete", 0); }
  185. $_SESSION['lpobject'] = serialize($mylp);
  186. if ($mylpi->get_type() != 'sco') {
  187. // If this object's JS status has not been updated by the SCORM API, update now.
  188. $return .= "olms.lesson_status='".$mystatus."';";
  189. }
  190. $return .= "update_toc('".$mystatus."','".$item_id."');";
  191. $update_list = $mylp->get_update_queue();
  192. foreach ($update_list as $my_upd_id => $my_upd_status) {
  193. 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.
  194. $return .= "update_toc('".$my_upd_status."','".$my_upd_id."');";
  195. }
  196. }
  197. $return .= "update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');";
  198. if ($debug > 0) {
  199. $return .= "logit_lms('Saved data for item ".$item_id.", user ".$user_id." (status=".$mystatus.")',2);";
  200. if ($debug > 1) { error_log('End of save_item()', 0); }
  201. }
  202. if (!isset($_SESSION['login_as'])) {
  203. // If $_SESSION['login_as'] is set, then the user is an admin logged as the user.
  204. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  205. $sql_last_connection = "SELECT login_id, login_date
  206. FROM $tbl_track_login
  207. WHERE login_user_id='".api_get_user_id()."'
  208. ORDER BY login_date DESC LIMIT 0,1";
  209. $q_last_connection = Database::query($sql_last_connection);
  210. if (Database::num_rows($q_last_connection) > 0) {
  211. $current_time = api_get_utc_datetime();
  212. $row = Database::fetch_array($q_last_connection);
  213. $i_id_last_connection = $row['login_id'];
  214. $s_sql_update_logout_date = "UPDATE $tbl_track_login SET logout_date='".$current_time."' WHERE login_id='$i_id_last_connection'";
  215. Database::query($s_sql_update_logout_date);
  216. }
  217. }
  218. if ($mylp->get_type() == 2) {
  219. $return .= "update_stats();";
  220. }
  221. //To be sure progress is updated
  222. $mylp->save_last();
  223. if ($debug > 0) { error_log('lp_ajax_save_item.php : save_item end ----- '); }
  224. return $return;
  225. }
  226. $interactions = array();
  227. if (isset($_REQUEST['interact'])) {
  228. if (is_array($_REQUEST['interact'])) {
  229. foreach ($_REQUEST['interact'] as $idx => $interac) {
  230. $interactions[$idx] = split(',', substr($interac, 1, -1));
  231. if(!isset($interactions[$idx][7])){ // Make sure there are 7 elements.
  232. $interactions[$idx][7] = '';
  233. }
  234. }
  235. }
  236. }
  237. echo save_item(
  238. (!empty($_REQUEST['lid'])?$_REQUEST['lid']:null),
  239. (!empty($_REQUEST['uid'])?$_REQUEST['uid']:null),
  240. (!empty($_REQUEST['vid'])?$_REQUEST['vid']:null),
  241. (!empty($_REQUEST['iid'])?$_REQUEST['iid']:null),
  242. (!empty($_REQUEST['s'])?$_REQUEST['s']:null),
  243. (!empty($_REQUEST['max'])?$_REQUEST['max']:null),
  244. (!empty($_REQUEST['min'])?$_REQUEST['min']:null),
  245. (!empty($_REQUEST['status'])?$_REQUEST['status']:null),
  246. (!empty($_REQUEST['t'])?$_REQUEST['t']:null),
  247. (!empty($_REQUEST['suspend'])?$_REQUEST['suspend']:null),
  248. (!empty($_REQUEST['loc'])?$_REQUEST['loc']:null),
  249. $interactions,
  250. (!empty($_REQUEST['core_exit'])?$_REQUEST['core_exit']:''));