lp_ajax_save_item.php 12 KB

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