lp_ajax_save_item.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This script contains the server part of the AJAX interaction process.
  6. * The client part is located * in lp_api.php or other api's.
  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. require_once __DIR__.'/../inc/global.inc.php';
  13. /**
  14. * Writes an item's new values into the database and returns the operation result
  15. * @param int $lp_id Learnpath ID
  16. * @param int $user_id User ID
  17. * @param int $view_id View ID
  18. * @param int $item_id Item ID
  19. * @param float $score Current score
  20. * @param float $max Maximum score
  21. * @param float $min Minimum score
  22. * @param string $status Lesson status
  23. * @param int $time Session time
  24. * @param string $suspend Suspend data
  25. * @param string $location Lesson location
  26. * @param array $interactions Interactions array
  27. * @param string $core_exit Core exit SCORM string
  28. * @param int $sessionId Session ID
  29. * @param int $courseId Course ID
  30. * @param int $lmsFinish Whether the call was issued from SCORM's LMSFinish()
  31. * @param int $userNavigatesAway Whether the user is moving to another item
  32. * @param int $statusSignalReceived Whether the SCO called SetValue(lesson_status)
  33. * @return bool|null|string The resulting JS string
  34. */
  35. function save_item(
  36. $lp_id,
  37. $user_id,
  38. $view_id,
  39. $item_id,
  40. $score = -1.0,
  41. $max = -1.0,
  42. $min = -1.0,
  43. $status = '',
  44. $time = 0,
  45. $suspend = '',
  46. $location = '',
  47. $interactions = array(),
  48. $core_exit = 'none',
  49. $sessionId = null,
  50. $courseId = null,
  51. $lmsFinish = 0,
  52. $userNavigatesAway = 0,
  53. $statusSignalReceived = 0
  54. ) {
  55. //global $debug;
  56. $debug = 0;
  57. $return = null;
  58. if ($debug > 0) {
  59. error_log('lp_ajax_save_item.php : save_item() params: ');
  60. error_log("item_id: $item_id");
  61. error_log("lp_id: $lp_id - user_id: - $user_id - view_id: $view_id - item_id: $item_id");
  62. error_log("score: $score - max:$max - min: $min - status:$status");
  63. error_log("time:$time - suspend: $suspend - location: $location - core_exit: $core_exit");
  64. error_log("finish: $lmsFinish - navigatesAway: $userNavigatesAway");
  65. }
  66. $myLP = learnpath::getLpFromSession(api_get_course_id(), $lp_id, $user_id);
  67. if (!is_a($myLP, 'learnpath')) {
  68. if ($debug) {
  69. error_log("mylp variable is not an learnpath object");
  70. }
  71. return null;
  72. }
  73. $prerequisitesCheck = $myLP->prerequisites_match($item_id);
  74. /** @var learnpathItem $myLPI */
  75. $myLPI = $myLP->items[$item_id];
  76. if (empty($myLPI)) {
  77. if ($debug > 0) {
  78. error_log("item #$item_id not found in the items array: ".print_r($myLP->items, 1));
  79. }
  80. return false;
  81. }
  82. // This functions sets the $this->db_item_view_id variable needed in get_status() see BT#5069
  83. $myLPI->set_lp_view($view_id);
  84. // Launch the prerequisites check and set error if needed
  85. if ($prerequisitesCheck !== true) {
  86. // If prerequisites were not matched, don't update any item info
  87. if ($debug) {
  88. error_log("prereq_check: ".intval($prerequisitesCheck));
  89. }
  90. return $return;
  91. } else {
  92. if ($debug > 1) {
  93. error_log('Prerequisites are OK');
  94. }
  95. if (isset($max) && $max != -1) {
  96. $myLPI->max_score = $max;
  97. $myLPI->set_max_score($max);
  98. if ($debug > 1) {
  99. error_log("Setting max_score: $max");
  100. }
  101. }
  102. if (isset($min) && $min != -1 && $min != 'undefined') {
  103. $myLPI->min_score = $min;
  104. if ($debug > 1) {
  105. error_log("Setting min_score: $min");
  106. }
  107. }
  108. // set_score function used to save the status, but this is not the case anymore
  109. if (isset($score) && $score != -1) {
  110. if ($debug > 1) {
  111. error_log('Calling set_score('.$score.')', 0);
  112. error_log('set_score changes the status to failed/passed if mastery score is provided', 0);
  113. }
  114. $myLPI->set_score($score);
  115. if ($debug > 1) {
  116. error_log('Done calling set_score '.$myLPI->get_score(), 0);
  117. }
  118. } else {
  119. if ($debug > 1) {
  120. error_log("Score not updated");
  121. }
  122. }
  123. $statusIsSet = false;
  124. // Default behaviour.
  125. if (isset($status) && $status != '' && $status != 'undefined') {
  126. if ($debug > 1) {
  127. error_log('Calling set_status('.$status.')', 0);
  128. }
  129. $myLPI->set_status($status);
  130. $statusIsSet = true;
  131. if ($debug > 1) {
  132. error_log('Done calling set_status: checking from memory: '.$myLPI->get_status(false), 0);
  133. }
  134. } else {
  135. if ($debug > 1) {
  136. error_log("Status not updated");
  137. }
  138. }
  139. $my_type = $myLPI->get_type();
  140. // Set status to completed for hotpotatoes if score > 80%.
  141. if ($my_type == 'hotpotatoes') {
  142. if ((empty($status) || $status == 'undefined' || $status == 'not attempted') && $max > 0) {
  143. if (($score / $max) > 0.8) {
  144. $myStatus = 'completed';
  145. if ($debug > 1) {
  146. error_log('Calling set_status('.$myStatus.') for hotpotatoes', 0);
  147. }
  148. $myLPI->set_status($myStatus);
  149. $statusIsSet = true;
  150. if ($debug > 1) {
  151. error_log('Done calling set_status for hotpotatoes - now '.$myLPI->get_status(false), 0);
  152. }
  153. }
  154. } elseif ($status == 'completed' && $max > 0 && ($score / $max) < 0.8) {
  155. $myStatus = 'failed';
  156. if ($debug > 1) {
  157. error_log('Calling set_status('.$myStatus.') for hotpotatoes', 0);
  158. }
  159. $myLPI->set_status($myStatus);
  160. $statusIsSet = true;
  161. if ($debug > 1) {
  162. error_log('Done calling set_status for hotpotatoes - now '.$myLPI->get_status(false), 0);
  163. }
  164. }
  165. } elseif ($my_type == 'sco') {
  166. /*
  167. * This is a specific implementation for SCORM 1.2, matching page 26 of SCORM 1.2's RTE
  168. * "Normally the SCO determines its own status and passes it to the LMS.
  169. * 1) If cmi.core.credit is set to "credit" and there is a mastery
  170. * score in the manifest (adlcp:masteryscore), the LMS can change
  171. * the status to either passed or failed depending on the
  172. * student's score compared to the mastery score.
  173. * 2) If there is no mastery score in the manifest
  174. * (adlcp:masteryscore), the LMS cannot override SCO
  175. * determined status.
  176. * 3) If the student is taking the SCO for no-credit, there is no
  177. * change to the lesson_status, with one exception. If the
  178. * lesson_mode is "browse", the lesson_status may change to
  179. * "browsed" even if the cmi.core.credit is set to no-credit.
  180. * "
  181. * Additionally, the LMS behaviour should be:
  182. * If a SCO sets the cmi.core.lesson_status then there is no problem.
  183. * However, the SCORM does not force the SCO to set the cmi.core.lesson_status.
  184. * There is some additional requirements that must be adhered to
  185. * successfully handle these cases:
  186. * Upon initial launch
  187. * the LMS should set the cmi.core.lesson_status to "not attempted".
  188. * Upon receiving the LMSFinish() call or the user navigates away,
  189. * the LMS should set the cmi.core.lesson_status for the SCO to "completed".
  190. * After setting the cmi.core.lesson_status to "completed",
  191. * the LMS should now check to see if a Mastery Score has been
  192. * specified in the cmi.student_data.mastery_score, if supported,
  193. * or the manifest that the SCO is a member of.
  194. * If a Mastery Score is provided and the SCO did set the
  195. * cmi.core.score.raw, the LMS shall compare the cmi.core.score.raw
  196. * to the Mastery Score and set the cmi.core.lesson_status to
  197. * either "passed" or "failed". If no Mastery Score is provided,
  198. * the LMS will leave the cmi.core.lesson_status as "completed"
  199. */
  200. $masteryScore = $myLPI->get_mastery_score();
  201. if ($masteryScore == -1 || empty($masteryScore)) {
  202. $masteryScore = false;
  203. }
  204. $credit = $myLPI->get_credit();
  205. /**
  206. * 1) If cmi.core.credit is set to "credit" and there is a mastery
  207. * score in the manifest (adlcp:masteryscore), the LMS can change
  208. * the status to either passed or failed depending on the
  209. * student's score compared to the mastery score.
  210. */
  211. if ($credit == 'credit' &&
  212. $masteryScore &&
  213. (isset($score) && $score != -1) &&
  214. !$statusIsSet && !$statusSignalReceived
  215. ) {
  216. if ($score >= $masteryScore) {
  217. $myLPI->set_status('passed');
  218. } else {
  219. $myLPI->set_status('failed');
  220. }
  221. $statusIsSet = true;
  222. }
  223. /**
  224. * 2) If there is no mastery score in the manifest
  225. * (adlcp:masteryscore), the LMS cannot override SCO
  226. * determined status.
  227. */
  228. if (!$statusIsSet && !$masteryScore && !$statusSignalReceived) {
  229. if (!empty($status)) {
  230. $myLPI->set_status($status);
  231. $statusIsSet = true;
  232. }
  233. //if no status was set directly, we keep the previous one
  234. }
  235. /**
  236. * 3) If the student is taking the SCO for no-credit, there is no
  237. * change to the lesson_status, with one exception. If the
  238. * lesson_mode is "browse", the lesson_status may change to
  239. * "browsed" even if the cmi.core.credit is set to no-credit.
  240. */
  241. if (!$statusIsSet && $credit == 'no-credit' && !$statusSignalReceived) {
  242. $mode = $myLPI->get_lesson_mode();
  243. if ($mode == 'browse' && $status == 'browsed') {
  244. $myLPI->set_status($status);
  245. $statusIsSet = true;
  246. }
  247. //if no status was set directly, we keep the previous one
  248. }
  249. /**
  250. * If a SCO sets the cmi.core.lesson_status then there is no problem.
  251. * However, the SCORM does not force the SCO to set the
  252. * cmi.core.lesson_status. There is some additional requirements
  253. * that must be adhered to successfully handle these cases:
  254. */
  255. if (!$statusIsSet && empty($status) && !$statusSignalReceived) {
  256. /**
  257. * Upon initial launch the LMS should set the
  258. * cmi.core.lesson_status to "not attempted".
  259. */
  260. // this case should be handled by LMSInitialize() and xajax_switch_item()
  261. /**
  262. * Upon receiving the LMSFinish() call or the user navigates
  263. * away, the LMS should set the cmi.core.lesson_status for the
  264. * SCO to "completed".
  265. */
  266. if ($lmsFinish || $userNavigatesAway) {
  267. $myStatus = 'completed';
  268. /**
  269. * After setting the cmi.core.lesson_status to "completed",
  270. * the LMS should now check to see if a Mastery Score has been
  271. * specified in the cmi.student_data.mastery_score, if supported,
  272. * or the manifest that the SCO is a member of.
  273. * If a Mastery Score is provided and the SCO did set the
  274. * cmi.core.score.raw, the LMS shall compare the cmi.core.score.raw
  275. * to the Mastery Score and set the cmi.core.lesson_status to
  276. * either "passed" or "failed". If no Mastery Score is provided,
  277. * the LMS will leave the cmi.core.lesson_status as "completed”
  278. */
  279. if ($masteryScore && (isset($score) && $score != -1)) {
  280. if ($score >= $masteryScore) {
  281. $myStatus = 'passed';
  282. } else {
  283. $myStatus = 'failed';
  284. }
  285. }
  286. $myLPI->set_status($myStatus);
  287. $statusIsSet = true;
  288. }
  289. }
  290. // End of type=='sco'
  291. }
  292. // If no previous condition changed the SCO status, proceed with a
  293. // generic behaviour
  294. if (!$statusIsSet && !$statusSignalReceived) {
  295. // Default behaviour
  296. if (isset($status) && $status != '' && $status != 'undefined') {
  297. if ($debug > 1) {
  298. error_log('Calling set_status('.$status.')', 0);
  299. }
  300. $myLPI->set_status($status);
  301. if ($debug > 1) {
  302. error_log('Done calling set_status: checking from memory: '.$myLPI->get_status(false), 0);
  303. }
  304. } else {
  305. if ($debug > 1) {
  306. error_log("Status not updated");
  307. }
  308. }
  309. }
  310. if (isset($time) && $time != '' && $time != 'undefined') {
  311. // If big integer, then it's a timestamp, otherwise it's normal scorm time.
  312. if ($debug > 1) {
  313. error_log('Calling set_time('.$time.') ', 0);
  314. }
  315. if ($time == intval(strval($time)) && $time > 1000000) {
  316. if ($debug > 1) {
  317. error_log("Time is INT");
  318. }
  319. $real_time = time() - $time;
  320. if ($debug > 1) {
  321. error_log('Calling $real_time '.$real_time.' ', 0);
  322. }
  323. $myLPI->set_time($real_time, 'int');
  324. } else {
  325. if ($debug > 1) {
  326. error_log("Time is in SCORM format");
  327. }
  328. if ($debug > 1) {
  329. error_log('Calling $time '.$time.' ', 0);
  330. }
  331. $myLPI->set_time($time, 'scorm');
  332. }
  333. }
  334. if (isset($suspend) && $suspend != '' && $suspend != 'undefined') {
  335. $myLPI->current_data = $suspend;
  336. }
  337. if (isset($location) && $location != '' && $location != 'undefined') {
  338. $myLPI->set_lesson_location($location);
  339. }
  340. // Deal with interactions provided in arrays in the following format:
  341. // id(0), type(1), time(2), weighting(3), correct_responses(4), student_response(5), result(6), latency(7)
  342. if (is_array($interactions) && count($interactions) > 0) {
  343. foreach ($interactions as $index => $interaction) {
  344. //$mylpi->add_interaction($index,$interactions[$index]);
  345. //fix DT#4444
  346. $clean_interaction = str_replace('@.|@', ',', $interactions[$index]);
  347. $myLPI->add_interaction($index, $clean_interaction);
  348. }
  349. }
  350. if ($core_exit != 'undefined') {
  351. $myLPI->set_core_exit($core_exit);
  352. }
  353. $myLP->save_item($item_id, false);
  354. }
  355. $myStatusInDB = $myLPI->get_status(true);
  356. if ($debug) {
  357. error_log("Status in DB: $myStatusInDB");
  358. }
  359. if ($myStatusInDB != 'completed' &&
  360. $myStatusInDB != 'passed' &&
  361. $myStatusInDB != 'browsed' &&
  362. $myStatusInDB != 'failed'
  363. ) {
  364. $myStatusInMemory = $myLPI->get_status(false);
  365. if ($myStatusInMemory != $myStatusInDB) {
  366. $myStatus = $myStatusInMemory;
  367. } else {
  368. $myStatus = $myStatusInDB;
  369. }
  370. } else {
  371. $myStatus = $myStatusInDB;
  372. }
  373. $myTotal = $myLP->getTotalItemsCountWithoutDirs();
  374. $myComplete = $myLP->get_complete_items_count();
  375. $myProgressMode = $myLP->get_progress_bar_mode();
  376. $myProgressMode = $myProgressMode == '' ? '%' : $myProgressMode;
  377. if ($debug > 1) {
  378. error_log("mystatus: $myStatus", 0);
  379. error_log("myprogress_mode: $myProgressMode", 0);
  380. error_log("progress: $myComplete / $myTotal", 0);
  381. }
  382. if ($myLPI->get_type() != 'sco') {
  383. // If this object's JS status has not been updated by the SCORM API, update now.
  384. $return .= "olms.lesson_status='".$myStatus."';";
  385. }
  386. $return .= "update_toc('".$myStatus."','".$item_id."');";
  387. $update_list = $myLP->get_update_queue();
  388. foreach ($update_list as $my_upd_id => $my_upd_status) {
  389. if ($my_upd_id != $item_id) {
  390. /* Only update the status from other items (i.e. parents and brothers),
  391. do not update current as we just did it already. */
  392. $return .= "update_toc('".$my_upd_status."','".$my_upd_id."');";
  393. }
  394. }
  395. $return .= "update_progress_bar('$myComplete', '$myTotal', '$myProgressMode');";
  396. if (!isset($_SESSION['login_as'])) {
  397. // If $_SESSION['login_as'] is set, then the user is an admin logged as the user.
  398. $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  399. $sql = "SELECT login_id, login_date
  400. FROM $tbl_track_login
  401. WHERE login_user_id= ".api_get_user_id()."
  402. ORDER BY login_date DESC
  403. LIMIT 0,1";
  404. $q_last_connection = Database::query($sql);
  405. if (Database::num_rows($q_last_connection) > 0) {
  406. $current_time = api_get_utc_datetime();
  407. $row = Database::fetch_array($q_last_connection);
  408. $i_id_last_connection = $row['login_id'];
  409. $sql = "UPDATE $tbl_track_login
  410. SET logout_date='".$current_time."'
  411. WHERE login_id = $i_id_last_connection";
  412. Database::query($sql);
  413. }
  414. }
  415. if ($myLP->get_type() == 2) {
  416. $return .= "update_stats();";
  417. }
  418. // To be sure progress is updated.
  419. $myLP->save_last();
  420. Session::write('lpobject', serialize($myLP));
  421. if ($debug > 0) {
  422. error_log('---------------- lp_ajax_save_item.php : save_item end ----- ');
  423. }
  424. return $return;
  425. }
  426. $interactions = array();
  427. if (isset($_REQUEST['interact'])) {
  428. if (is_array($_REQUEST['interact'])) {
  429. foreach ($_REQUEST['interact'] as $idx => $interac) {
  430. $interactions[$idx] = preg_split('/,/', substr($interac, 1, -1));
  431. if (!isset($interactions[$idx][7])) { // Make sure there are 7 elements.
  432. $interactions[$idx][7] = '';
  433. }
  434. }
  435. }
  436. }
  437. echo save_item(
  438. (!empty($_REQUEST['lid']) ? $_REQUEST['lid'] : null),
  439. (!empty($_REQUEST['uid']) ? $_REQUEST['uid'] : null),
  440. (!empty($_REQUEST['vid']) ? $_REQUEST['vid'] : null),
  441. (!empty($_REQUEST['iid']) ? $_REQUEST['iid'] : null),
  442. (!empty($_REQUEST['s']) ? $_REQUEST['s'] : null),
  443. (!empty($_REQUEST['max']) ? $_REQUEST['max'] : null),
  444. (!empty($_REQUEST['min']) ? $_REQUEST['min'] : null),
  445. (!empty($_REQUEST['status']) ? $_REQUEST['status'] : null),
  446. (!empty($_REQUEST['t']) ? $_REQUEST['t'] : null),
  447. (!empty($_REQUEST['suspend']) ? $_REQUEST['suspend'] : null),
  448. (!empty($_REQUEST['loc']) ? $_REQUEST['loc'] : null),
  449. $interactions,
  450. (!empty($_REQUEST['core_exit']) ? $_REQUEST['core_exit'] : ''),
  451. (!empty($_REQUEST['session_id']) ? $_REQUEST['session_id'] : ''),
  452. (!empty($_REQUEST['course_id']) ? $_REQUEST['course_id'] : ''),
  453. (empty($_REQUEST['finish']) ? 0 : 1),
  454. (empty($_REQUEST['userNavigatesAway']) ? 0 : 1),
  455. (empty($_REQUEST['statusSignalReceived']) ? 0 : 1)
  456. );