lp_ajax_save_item.php 20 KB

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