lp_ajax_save_item.php 20 KB

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