exercise.ajax.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Responses to AJAX calls.
  6. */
  7. require_once __DIR__.'/../global.inc.php';
  8. $debug = false;
  9. api_protect_course_script(true);
  10. $action = $_REQUEST['a'];
  11. if ($debug) {
  12. error_log('-----------------------------------------------------');
  13. error_log("$action ajax call");
  14. error_log('-----------------------------------------------------');
  15. }
  16. $course_id = api_get_course_int_id();
  17. $session_id = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : api_get_session_id();
  18. $course_code = isset($_REQUEST['cidReq']) ? $_REQUEST['cidReq'] : api_get_course_id();
  19. switch ($action) {
  20. case 'update_duration':
  21. $exeId = isset($_REQUEST['exe_id']) ? $_REQUEST['exe_id'] : 0;
  22. if (Session::read('login_as')) {
  23. if ($debug) {
  24. error_log("User is 'login as' don't update duration time.");
  25. }
  26. exit;
  27. }
  28. if (empty($exeId)) {
  29. if ($debug) {
  30. error_log('Exe id not provided.');
  31. }
  32. exit;
  33. }
  34. /** @var Exercise $exerciseInSession */
  35. $exerciseInSession = Session::read('objExercise');
  36. if (empty($exerciseInSession)) {
  37. if ($debug) {
  38. error_log('Exercise obj not provided.');
  39. }
  40. exit;
  41. }
  42. // If exercise was updated x seconds before, then don't updated duration.
  43. $onlyUpdateValue = 10;
  44. $em = Database::getManager();
  45. /** @var \Chamilo\CoreBundle\Entity\TrackEExercises $attempt */
  46. $attempt = $em->getRepository('ChamiloCoreBundle:TrackEExercises')->find($exeId);
  47. if (empty($attempt)) {
  48. if ($debug) {
  49. error_log("Attempt #$exeId doesn't exists.");
  50. }
  51. exit;
  52. }
  53. $nowObject = api_get_utc_datetime(null, false, true);
  54. $now = $nowObject->getTimestamp();
  55. $exerciseId = $attempt->getExeExoId();
  56. $userId = $attempt->getExeUserId();
  57. $currentUserId = api_get_user_id();
  58. if ($userId != $currentUserId) {
  59. if ($debug) {
  60. error_log("User $currentUserId trying to change time for user $userId");
  61. }
  62. exit;
  63. }
  64. if ($exerciseInSession->id != $exerciseId) {
  65. if ($debug) {
  66. error_log("Cannot update, exercise are different.");
  67. }
  68. exit;
  69. }
  70. if ($attempt->getStatus() != 'incomplete') {
  71. if ($debug) {
  72. error_log('Cannot update exercise is already completed.');
  73. }
  74. exit;
  75. }
  76. // Check if we are dealing with the same exercise.
  77. $timeWithOutUpdate = $now - $attempt->getExeDate()->getTimestamp();
  78. if ($timeWithOutUpdate > $onlyUpdateValue) {
  79. $key = ExerciseLib::get_time_control_key(
  80. $exerciseId,
  81. $attempt->getOrigLpId(),
  82. $attempt->getOrigLpItemId()
  83. );
  84. $durationFromObject = $attempt->getExeDuration();
  85. $previousTime = Session::read('duration_time_previous');
  86. if (isset($previousTime[$key]) &&
  87. !empty($previousTime[$key])
  88. ) {
  89. $sessionTime = $previousTime[$key];
  90. $duration = $sessionTime = $now - $sessionTime;
  91. if (!empty($durationFromObject)) {
  92. $duration += $durationFromObject;
  93. }
  94. $duration = (int) $duration;
  95. if (!empty($duration)) {
  96. if ($debug) {
  97. error_log("Exe_id: #".$exeId);
  98. error_log("Key: $key");
  99. error_log("Exercise to update: #$exerciseId of user: #$userId");
  100. error_log("Duration time found in DB before update: $durationFromObject");
  101. error_log("Current spent time $sessionTime before an update");
  102. error_log("Accumulate duration to save in DB: $duration");
  103. error_log("End date (UTC) before update: ".$attempt->getExeDate()->format('Y-m-d H:i:s'));
  104. error_log("End date (UTC) to be save in DB: ".$nowObject->format('Y-m-d H:i:s'));
  105. }
  106. $attempt
  107. ->setExeDuration($duration)
  108. ->setExeDate($nowObject);
  109. $em->merge($attempt);
  110. $em->flush();
  111. }
  112. } else {
  113. if ($debug) {
  114. error_log("Nothing to update, 'duration_time_previous' session not set");
  115. error_log("Key: $key");
  116. }
  117. }
  118. } else {
  119. if ($debug) {
  120. error_log("Can't update, time was already updated $timeWithOutUpdate seconds ago");
  121. }
  122. }
  123. break;
  124. case 'get_live_stats':
  125. if (!api_is_allowed_to_edit(null, true)) {
  126. break;
  127. }
  128. // 1. Setting variables needed by jqgrid
  129. $action = $_GET['a'];
  130. $exercise_id = (int) $_GET['exercise_id'];
  131. $page = (int) $_REQUEST['page']; //page
  132. $limit = (int) $_REQUEST['rows']; //quantity of rows
  133. $sidx = $_REQUEST['sidx']; //index to filter
  134. $sord = $_REQUEST['sord']; //asc or desc
  135. if (!in_array($sord, ['asc', 'desc'])) {
  136. $sord = 'desc';
  137. }
  138. // get index row - i.e. user click to sort $sord = $_GET['sord'];
  139. // get the direction
  140. if (!$sidx) {
  141. $sidx = 1;
  142. }
  143. $track_exercise = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  144. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  145. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  146. $minutes = (int) $_REQUEST['minutes'];
  147. $now = time() - 60 * $minutes;
  148. $now = api_get_utc_datetime($now);
  149. $where_condition = " orig_lp_id = 0 AND exe_exo_id = $exercise_id AND start_date > '$now' ";
  150. $sql = "SELECT COUNT(DISTINCT exe_id)
  151. FROM $track_exercise
  152. WHERE $where_condition ";
  153. $result = Database::query($sql);
  154. $count = Database::fetch_row($result);
  155. $count = $count[0];
  156. //3. Calculating first, end, etc
  157. $total_pages = 0;
  158. if ($count > 0) {
  159. if (!empty($limit)) {
  160. $total_pages = ceil($count / $limit);
  161. }
  162. }
  163. if ($page > $total_pages) {
  164. $page = $total_pages;
  165. }
  166. $start = $limit * $page - $limit;
  167. if ($start < 0) {
  168. $start = 0;
  169. }
  170. $sql = "SELECT
  171. exe_id,
  172. exe_user_id,
  173. firstname,
  174. lastname,
  175. aa.status,
  176. start_date,
  177. exe_result,
  178. exe_weighting,
  179. exe_result/exe_weighting as score,
  180. exe_duration,
  181. questions_to_check,
  182. orig_lp_id
  183. FROM $user_table u
  184. INNER JOIN (
  185. SELECT
  186. t.exe_id,
  187. t.exe_user_id,
  188. status,
  189. start_date,
  190. exe_result,
  191. exe_weighting,
  192. exe_result/exe_weighting as score,
  193. exe_duration,
  194. questions_to_check,
  195. orig_lp_id
  196. FROM $track_exercise t
  197. LEFT JOIN $track_attempt a
  198. ON (a.exe_id = t.exe_id AND t.exe_user_id = a.user_id)
  199. WHERE t.status = 'incomplete' AND $where_condition
  200. GROUP BY exe_user_id
  201. ) as aa
  202. ON aa.exe_user_id = user_id
  203. ORDER BY $sidx $sord
  204. LIMIT $start, $limit";
  205. $result = Database::query($sql);
  206. $results = [];
  207. while ($row = Database::fetch_array($result, 'ASSOC')) {
  208. $results[] = $row;
  209. }
  210. $oExe = new Exercise();
  211. $oExe->read($exercise_id);
  212. $response = new stdClass();
  213. $response->page = $page;
  214. $response->total = $total_pages;
  215. $response->records = $count;
  216. $i = 0;
  217. if (!empty($results)) {
  218. foreach ($results as $row) {
  219. $sql = "SELECT SUM(count_question_id) as count_question_id
  220. FROM (
  221. SELECT 1 as count_question_id
  222. FROM $track_attempt a
  223. WHERE
  224. user_id = {$row['exe_user_id']} AND
  225. exe_id = {$row['exe_id']}
  226. GROUP by question_id
  227. ) as count_table";
  228. $result_count = Database::query($sql);
  229. $count_questions = Database::fetch_array(
  230. $result_count,
  231. 'ASSOC'
  232. );
  233. $count_questions = $count_questions['count_question_id'];
  234. $row['count_questions'] = $count_questions;
  235. $response->rows[$i]['id'] = $row['exe_id'];
  236. if (!empty($oExe->expired_time)) {
  237. $remaining = strtotime($row['start_date']) +
  238. ($oExe->expired_time * 60) -
  239. strtotime(api_get_utc_datetime(time()));
  240. $h = floor($remaining / 3600);
  241. $m = floor(($remaining - ($h * 3600)) / 60);
  242. $s = ($remaining - ($h * 3600) - ($m * 60));
  243. $timeInfo = api_convert_and_format_date(
  244. $row['start_date'],
  245. DATE_TIME_FORMAT_LONG
  246. ).' ['.($h > 0 ? $h.':' : '').sprintf("%02d", $m).':'.sprintf("%02d", $s).']';
  247. } else {
  248. $timeInfo = api_convert_and_format_date(
  249. $row['start_date'],
  250. DATE_TIME_FORMAT_LONG
  251. );
  252. }
  253. $array = [
  254. $row['firstname'],
  255. $row['lastname'],
  256. $timeInfo,
  257. $row['count_questions'],
  258. round($row['score'] * 100).'%',
  259. ];
  260. $response->rows[$i]['cell'] = $array;
  261. $i++;
  262. }
  263. }
  264. echo json_encode($response);
  265. break;
  266. case 'update_exercise_list_order':
  267. if (api_is_allowed_to_edit(null, true)) {
  268. $new_list = $_REQUEST['exercise_list'];
  269. $table = Database::get_course_table(TABLE_QUIZ_ORDER);
  270. $counter = 1;
  271. //Drop all
  272. $sql = "DELETE FROM $table WHERE session_id = $session_id AND c_id = $course_id";
  273. Database::query($sql);
  274. // Insert all
  275. foreach ($new_list as $new_order_id) {
  276. Database::insert(
  277. $table,
  278. [
  279. 'exercise_order' => $counter,
  280. 'session_id' => $session_id,
  281. 'exercise_id' => (int) $new_order_id,
  282. 'c_id' => $course_id,
  283. ]
  284. );
  285. $counter++;
  286. }
  287. echo Display::return_message(get_lang('Saved'), 'confirmation');
  288. }
  289. break;
  290. case 'update_question_order':
  291. $course_info = api_get_course_info_by_id($course_id);
  292. $course_id = $course_info['real_id'];
  293. $exercise_id = isset($_REQUEST['exercise_id']) ? (int) $_REQUEST['exercise_id'] : null;
  294. if (empty($exercise_id)) {
  295. return Display::return_message(get_lang('Error'), 'error');
  296. }
  297. if (api_is_allowed_to_edit(null, true)) {
  298. $new_question_list = $_POST['question_id_list'];
  299. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  300. $counter = 1;
  301. foreach ($new_question_list as $new_order_id) {
  302. Database::update(
  303. $TBL_QUESTIONS,
  304. ['question_order' => $counter],
  305. [
  306. 'question_id = ? AND c_id = ? AND exercice_id = ? ' => [
  307. (int) $new_order_id,
  308. $course_id,
  309. $exercise_id,
  310. ],
  311. ]
  312. )
  313. ;
  314. $counter++;
  315. }
  316. echo Display::return_message(get_lang('Saved'), 'confirmation');
  317. }
  318. break;
  319. case 'add_question_to_reminder':
  320. /** @var Exercise $objExercise */
  321. $objExercise = Session::read('objExercise');
  322. $exeId = isset($_REQUEST['exe_id']) ? $_REQUEST['exe_id'] : 0;
  323. if (empty($objExercise) || empty($exeId)) {
  324. echo 0;
  325. exit;
  326. } else {
  327. $option = isset($_GET['option']) ? $_GET['option'] : '';
  328. switch ($option) {
  329. case 'add_all':
  330. $questionListInSession = Session::read('questionList');
  331. $objExercise->addAllQuestionToRemind(
  332. $exeId,
  333. $questionListInSession
  334. );
  335. break;
  336. case 'remove_all':
  337. $objExercise->removeAllQuestionToRemind(
  338. $exeId
  339. );
  340. break;
  341. default:
  342. $objExercise->editQuestionToRemind(
  343. $exeId,
  344. $_REQUEST['question_id'],
  345. $_REQUEST['action']
  346. );
  347. break;
  348. }
  349. }
  350. break;
  351. case 'save_exercise_by_now':
  352. $course_info = api_get_course_info_by_id($course_id);
  353. $course_id = $course_info['real_id'];
  354. // Use have permissions?
  355. if (api_is_allowed_to_session_edit()) {
  356. // "all" or "simple" strings means that there's one or all questions exercise type
  357. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  358. // Questions choices.
  359. $choice = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : null;
  360. // certainty degree choice
  361. $choiceDegreeCertainty = isset($_REQUEST['choiceDegreeCertainty'])
  362. ? $_REQUEST['choiceDegreeCertainty'] : null;
  363. // Hot spot coordinates from all questions.
  364. $hot_spot_coordinates = isset($_REQUEST['hotspot']) ? $_REQUEST['hotspot'] : null;
  365. // There is a reminder?
  366. $remind_list = isset($_REQUEST['remind_list']) && !empty($_REQUEST['remind_list'])
  367. ? array_keys($_REQUEST['remind_list']) : null;
  368. // Needed in manage_answer.
  369. $learnpath_id = isset($_REQUEST['learnpath_id']) ? (int) $_REQUEST['learnpath_id'] : 0;
  370. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? (int) $_REQUEST['learnpath_item_id'] : 0;
  371. // Attempt id.
  372. $exeId = isset($_REQUEST['exe_id']) ? (int) $_REQUEST['exe_id'] : 0;
  373. if ($debug) {
  374. error_log("exe_id = $exeId");
  375. error_log("type = $type");
  376. error_log("choice = ".print_r($choice, 1)." ");
  377. error_log("hot_spot_coordinates = ".print_r($hot_spot_coordinates, 1));
  378. error_log("remind_list = ".print_r($remind_list, 1));
  379. error_log("--------------------------------");
  380. }
  381. // Exercise information.
  382. /** @var Exercise $objExercise */
  383. $objExercise = Session::read('objExercise');
  384. // Question info.
  385. $question_id = isset($_REQUEST['question_id']) ? (int) $_REQUEST['question_id'] : null;
  386. $question_list = Session::read('questionList');
  387. // If exercise or question is not set then exit.
  388. if (empty($question_list) || empty($objExercise)) {
  389. echo 'error';
  390. if ($debug) {
  391. if (empty($question_list)) {
  392. error_log("question_list is empty");
  393. }
  394. if (empty($objExercise)) {
  395. error_log("objExercise is empty");
  396. }
  397. }
  398. exit;
  399. }
  400. // Getting information of the current exercise.
  401. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exeId);
  402. $exercise_id = $exercise_stat_info['exe_exo_id'];
  403. $attemptList = [];
  404. // First time here we create an attempt (getting the exe_id).
  405. if (!empty($exercise_stat_info)) {
  406. // We know the user we get the exe_id.
  407. $exeId = $exercise_stat_info['exe_id'];
  408. $total_score = $exercise_stat_info['exe_result'];
  409. // Getting the list of attempts
  410. $attemptList = Event::getAllExerciseEventByExeId($exeId);
  411. }
  412. // Updating Reminder algorithm.
  413. if ($objExercise->type == ONE_PER_PAGE) {
  414. $bd_reminder_list = explode(',', $exercise_stat_info['questions_to_check']);
  415. if (empty($remind_list)) {
  416. $remind_list = $bd_reminder_list;
  417. $new_list = [];
  418. foreach ($bd_reminder_list as $item) {
  419. if ($item != $question_id) {
  420. $new_list[] = $item;
  421. }
  422. }
  423. $remind_list = $new_list;
  424. } else {
  425. if (isset($remind_list[0])) {
  426. if (!in_array($remind_list[0], $bd_reminder_list)) {
  427. array_push($bd_reminder_list, $remind_list[0]);
  428. }
  429. $remind_list = $bd_reminder_list;
  430. }
  431. }
  432. }
  433. // No exe id? Can't save answer.
  434. if (empty($exeId)) {
  435. // Fires an error.
  436. echo 'error';
  437. if ($debug) {
  438. error_log('exe_id is empty');
  439. }
  440. exit;
  441. }
  442. Session::write('exe_id', $exeId);
  443. // Getting the total weight if the request is simple
  444. $total_weight = 0;
  445. if ($type == 'simple') {
  446. foreach ($question_list as $my_question_id) {
  447. $objQuestionTmp = Question::read($my_question_id, $objExercise->course);
  448. $total_weight += $objQuestionTmp->selectWeighting();
  449. }
  450. }
  451. unset($objQuestionTmp);
  452. // Looping the question list
  453. foreach ($question_list as $my_question_id) {
  454. if ($debug) {
  455. error_log("Saving question_id = $my_question_id ");
  456. }
  457. if ($type == 'simple' && $question_id != $my_question_id) {
  458. continue;
  459. }
  460. $my_choice = isset($choice[$my_question_id]) ? $choice[$my_question_id] : null;
  461. if ($debug) {
  462. error_log("my_choice = ".print_r($my_choice, 1)."");
  463. }
  464. // Creates a temporary Question object
  465. $objQuestionTmp = Question::read($my_question_id, $objExercise->course);
  466. $myChoiceDegreeCertainty = null;
  467. if ($objQuestionTmp->type === MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  468. if (isset($choiceDegreeCertainty[$my_question_id])) {
  469. $myChoiceDegreeCertainty = $choiceDegreeCertainty[$my_question_id];
  470. }
  471. }
  472. // Getting free choice data.
  473. if (in_array($objQuestionTmp->type, [FREE_ANSWER, ORAL_EXPRESSION]) && $type == 'all') {
  474. $my_choice = isset($_REQUEST['free_choice'][$my_question_id]) && !empty($_REQUEST['free_choice'][$my_question_id])
  475. ? $_REQUEST['free_choice'][$my_question_id]
  476. : null;
  477. }
  478. if ($type == 'all') {
  479. $total_weight += $objQuestionTmp->selectWeighting();
  480. }
  481. // This variable came from exercise_submit_modal.php.
  482. $hotspot_delineation_result = null;
  483. if (isset($_SESSION['hotspot_delineation_result']) &&
  484. isset($_SESSION['hotspot_delineation_result'][$objExercise->selectId()])
  485. ) {
  486. $hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$my_question_id];
  487. }
  488. if ($type === 'simple') {
  489. // Getting old attempt in order to decrees the total score.
  490. $old_result = $objExercise->manage_answer(
  491. $exeId,
  492. $my_question_id,
  493. null,
  494. 'exercise_show',
  495. [],
  496. false,
  497. true,
  498. false,
  499. $objExercise->selectPropagateNeg()
  500. );
  501. // Removing old score.
  502. $total_score = $total_score - $old_result['score'];
  503. }
  504. // Deleting old attempt
  505. if (isset($attemptList) && !empty($attemptList[$my_question_id])) {
  506. if ($debug) {
  507. error_log("delete_attempt exe_id : $exeId, my_question_id: $my_question_id");
  508. }
  509. Event::delete_attempt(
  510. $exeId,
  511. api_get_user_id(),
  512. $course_id,
  513. $session_id,
  514. $my_question_id
  515. );
  516. if ($objQuestionTmp->type === HOT_SPOT) {
  517. Event::delete_attempt_hotspot(
  518. $exeId,
  519. api_get_user_id(),
  520. $course_id,
  521. $session_id,
  522. $my_question_id
  523. );
  524. }
  525. if (isset($attemptList[$my_question_id]) &&
  526. isset($attemptList[$my_question_id]['marks'])
  527. ) {
  528. $total_score -= $attemptList[$my_question_id]['marks'];
  529. }
  530. }
  531. // We're inside *one* question. Go through each possible answer for this question
  532. if ($objQuestionTmp->type === MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  533. $myChoiceTmp = [];
  534. $myChoiceTmp['choice'] = $my_choice;
  535. $myChoiceTmp['choiceDegreeCertainty'] = $myChoiceDegreeCertainty;
  536. $result = $objExercise->manage_answer(
  537. $exeId,
  538. $my_question_id,
  539. $myChoiceTmp,
  540. 'exercise_result',
  541. $hot_spot_coordinates,
  542. true,
  543. false,
  544. false,
  545. $objExercise->selectPropagateNeg(),
  546. $hotspot_delineation_result
  547. );
  548. } else {
  549. $result = $objExercise->manage_answer(
  550. $exeId,
  551. $my_question_id,
  552. $my_choice,
  553. 'exercise_result',
  554. $hot_spot_coordinates,
  555. true,
  556. false,
  557. false,
  558. $objExercise->selectPropagateNeg(),
  559. $hotspot_delineation_result
  560. );
  561. }
  562. // Adding the new score.
  563. $total_score += $result['score'];
  564. if ($debug) {
  565. error_log("total_score: $total_score ");
  566. error_log("total_weight: $total_weight ");
  567. }
  568. $duration = 0;
  569. $now = time();
  570. if ($type == 'all') {
  571. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exeId);
  572. }
  573. $key = ExerciseLib::get_time_control_key(
  574. $exercise_id,
  575. $exercise_stat_info['orig_lp_id'],
  576. $exercise_stat_info['orig_lp_item_id']
  577. );
  578. $durationTime = Session::read('duration_time');
  579. if (isset($durationTime[$key]) && !empty($durationTime[$key])) {
  580. if ($debug) {
  581. error_log('Session time :'.$durationTime[$key]);
  582. }
  583. $duration = $now - $durationTime[$key];
  584. if (!empty($exercise_stat_info['exe_duration'])) {
  585. $duration += $exercise_stat_info['exe_duration'];
  586. }
  587. $duration = (int) $duration;
  588. } else {
  589. if (!empty($exercise_stat_info['exe_duration'])) {
  590. $duration = $exercise_stat_info['exe_duration'];
  591. }
  592. }
  593. if ($debug) {
  594. error_log('duration to save in DB:'.$duration);
  595. }
  596. Session::write('duration_time', [$key => $now]);
  597. Event::updateEventExercise(
  598. $exeId,
  599. $objExercise->selectId(),
  600. $total_score,
  601. $total_weight,
  602. $session_id,
  603. $exercise_stat_info['orig_lp_id'],
  604. $exercise_stat_info['orig_lp_item_id'],
  605. $exercise_stat_info['orig_lp_item_view_id'],
  606. $duration,
  607. $question_list,
  608. 'incomplete',
  609. $remind_list
  610. );
  611. // Destruction of the Question object
  612. unset($objQuestionTmp);
  613. if ($debug) {
  614. error_log("---------- end question ------------");
  615. }
  616. }
  617. }
  618. if ($type == 'all') {
  619. echo 'ok';
  620. exit;
  621. }
  622. if ($objExercise->type == ONE_PER_PAGE) {
  623. if ($debug) {
  624. error_log("result: one_per_page");
  625. error_log(" ------ end ajax call ------- ");
  626. }
  627. echo 'one_per_page';
  628. exit;
  629. }
  630. if ($debug) {
  631. error_log("result: ok");
  632. error_log(" ------ end ajax call ------- ");
  633. }
  634. echo 'ok';
  635. break;
  636. case 'show_question':
  637. $isAllowedToEdit = api_is_allowed_to_edit(null, true, false, false);
  638. if (!$isAllowedToEdit) {
  639. api_not_allowed(true);
  640. exit;
  641. }
  642. $questionId = isset($_GET['question']) ? (int) $_GET['question'] : 0;
  643. $exerciseId = isset($_REQUEST['exercise']) ? (int) $_REQUEST['exercise'] : 0;
  644. if (!$questionId || !$exerciseId) {
  645. break;
  646. }
  647. $objExercise = new Exercise();
  648. $objExercise->read($exerciseId);
  649. $objQuestion = Question::read($questionId);
  650. $id = '';
  651. if (api_get_configuration_value('show_question_id')) {
  652. $id = '<h4>#'.$objQuestion->course['code'].'-'.$objQuestion->iid.'</h4>';
  653. }
  654. echo $id;
  655. echo '<p class="lead">'.$objQuestion->get_question_type_name().'</p>';
  656. if ($objQuestion->type === FILL_IN_BLANKS) {
  657. echo '<script>
  658. $(function() {
  659. $(".selectpicker").selectpicker({});
  660. });
  661. </script>';
  662. }
  663. // Allows render MathJax elements in a ajax call
  664. if (api_get_setting('include_asciimathml_script') === 'true') {
  665. echo '<script> MathJax.Hub.Queue(["Typeset",MathJax.Hub]);</script>';
  666. }
  667. ExerciseLib::showQuestion(
  668. $objExercise,
  669. $questionId,
  670. false,
  671. null,
  672. null,
  673. false,
  674. true,
  675. false,
  676. true,
  677. true
  678. );
  679. break;
  680. case 'get_quiz_embeddable':
  681. $exercises = ExerciseLib::get_all_exercises_for_course_id(
  682. api_get_course_info(),
  683. api_get_session_id(),
  684. api_get_course_int_id(),
  685. false
  686. );
  687. $exercises = array_filter(
  688. $exercises,
  689. function (array $exercise) {
  690. return ExerciseLib::isQuizEmbeddable($exercise);
  691. }
  692. );
  693. $result = [];
  694. $codePath = api_get_path(WEB_CODE_PATH);
  695. foreach ($exercises as $exercise) {
  696. $title = Security::remove_XSS(api_html_entity_decode($exercise['title']));
  697. $result[] = [
  698. 'id' => $exercise['iid'],
  699. 'title' => strip_tags($title),
  700. ];
  701. }
  702. header('Content-Type: application/json');
  703. echo json_encode($result);
  704. break;
  705. default:
  706. echo '';
  707. }
  708. exit;