lp_ajax_save_item.php 12 KB

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