lp_ajax_save_item.php 20 KB

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