exercise.ajax.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. if (WhispeakAuthPlugin::questionRequireAuthentify($question_id)) {
  401. if ($objExercise->type == ONE_PER_PAGE) {
  402. echo 'one_per_page';
  403. break;
  404. }
  405. echo 'ok';
  406. break;
  407. } else {
  408. ChamiloSession::erase(WhispeakAuthPlugin::SESSION_QUIZ_QUESTION);
  409. }
  410. // Getting information of the current exercise.
  411. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exeId);
  412. $exercise_id = $exercise_stat_info['exe_exo_id'];
  413. $attemptList = [];
  414. // First time here we create an attempt (getting the exe_id).
  415. if (!empty($exercise_stat_info)) {
  416. // We know the user we get the exe_id.
  417. $exeId = $exercise_stat_info['exe_id'];
  418. $total_score = $exercise_stat_info['exe_result'];
  419. // Getting the list of attempts
  420. $attemptList = Event::getAllExerciseEventByExeId($exeId);
  421. }
  422. // Updating Reminder algorithm.
  423. if ($objExercise->type == ONE_PER_PAGE) {
  424. $bd_reminder_list = explode(',', $exercise_stat_info['questions_to_check']);
  425. if (empty($remind_list)) {
  426. $remind_list = $bd_reminder_list;
  427. $new_list = [];
  428. foreach ($bd_reminder_list as $item) {
  429. if ($item != $question_id) {
  430. $new_list[] = $item;
  431. }
  432. }
  433. $remind_list = $new_list;
  434. } else {
  435. if (isset($remind_list[0])) {
  436. if (!in_array($remind_list[0], $bd_reminder_list)) {
  437. array_push($bd_reminder_list, $remind_list[0]);
  438. }
  439. $remind_list = $bd_reminder_list;
  440. }
  441. }
  442. }
  443. // No exe id? Can't save answer.
  444. if (empty($exeId)) {
  445. // Fires an error.
  446. echo 'error';
  447. if ($debug) {
  448. error_log('exe_id is empty');
  449. }
  450. exit;
  451. }
  452. Session::write('exe_id', $exeId);
  453. // Getting the total weight if the request is simple
  454. $total_weight = 0;
  455. if ($type == 'simple') {
  456. foreach ($question_list as $my_question_id) {
  457. $objQuestionTmp = Question::read($my_question_id, $objExercise->course);
  458. $total_weight += $objQuestionTmp->selectWeighting();
  459. }
  460. }
  461. unset($objQuestionTmp);
  462. // Looping the question list
  463. foreach ($question_list as $my_question_id) {
  464. if ($debug) {
  465. error_log("Saving question_id = $my_question_id ");
  466. }
  467. if ($type == 'simple' && $question_id != $my_question_id) {
  468. continue;
  469. }
  470. $my_choice = isset($choice[$my_question_id]) ? $choice[$my_question_id] : null;
  471. if ($debug) {
  472. error_log("my_choice = ".print_r($my_choice, 1)."");
  473. }
  474. // Creates a temporary Question object
  475. $objQuestionTmp = Question::read($my_question_id, $objExercise->course);
  476. $myChoiceDegreeCertainty = null;
  477. if ($objQuestionTmp->type === MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  478. if (isset($choiceDegreeCertainty[$my_question_id])) {
  479. $myChoiceDegreeCertainty = $choiceDegreeCertainty[$my_question_id];
  480. }
  481. }
  482. // Getting free choice data.
  483. if (in_array($objQuestionTmp->type, [FREE_ANSWER, ORAL_EXPRESSION]) && $type == 'all') {
  484. $my_choice = isset($_REQUEST['free_choice'][$my_question_id]) && !empty($_REQUEST['free_choice'][$my_question_id])
  485. ? $_REQUEST['free_choice'][$my_question_id]
  486. : null;
  487. }
  488. if ($type == 'all') {
  489. $total_weight += $objQuestionTmp->selectWeighting();
  490. }
  491. // This variable came from exercise_submit_modal.php.
  492. $hotspot_delineation_result = null;
  493. if (isset($_SESSION['hotspot_delineation_result']) &&
  494. isset($_SESSION['hotspot_delineation_result'][$objExercise->selectId()])
  495. ) {
  496. $hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$my_question_id];
  497. }
  498. if ($type == 'simple') {
  499. // Getting old attempt in order to decrees the total score.
  500. $old_result = $objExercise->manage_answer(
  501. $exeId,
  502. $my_question_id,
  503. null,
  504. 'exercise_show',
  505. [],
  506. false,
  507. true,
  508. false,
  509. $objExercise->selectPropagateNeg(),
  510. []
  511. );
  512. // Removing old score.
  513. $total_score = $total_score - $old_result['score'];
  514. }
  515. // Deleting old attempt
  516. if (isset($attemptList) && !empty($attemptList[$my_question_id])) {
  517. if ($debug) {
  518. error_log("delete_attempt exe_id : $exeId, my_question_id: $my_question_id");
  519. }
  520. Event::delete_attempt(
  521. $exeId,
  522. api_get_user_id(),
  523. $course_id,
  524. $session_id,
  525. $my_question_id
  526. );
  527. if ($objQuestionTmp->type === HOT_SPOT) {
  528. Event::delete_attempt_hotspot(
  529. $exeId,
  530. api_get_user_id(),
  531. $course_id,
  532. $session_id,
  533. $my_question_id
  534. );
  535. }
  536. if (isset($attemptList[$my_question_id]) &&
  537. isset($attemptList[$my_question_id]['marks'])
  538. ) {
  539. $total_score -= $attemptList[$my_question_id]['marks'];
  540. }
  541. }
  542. // We're inside *one* question. Go through each possible answer for this question
  543. if ($objQuestionTmp->type === MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  544. $myChoiceTmp = [];
  545. $myChoiceTmp['choice'] = $my_choice;
  546. $myChoiceTmp['choiceDegreeCertainty'] = $myChoiceDegreeCertainty;
  547. $result = $objExercise->manage_answer(
  548. $exeId,
  549. $my_question_id,
  550. $myChoiceTmp,
  551. 'exercise_result',
  552. $hot_spot_coordinates,
  553. true,
  554. false,
  555. false,
  556. $objExercise->selectPropagateNeg(),
  557. $hotspot_delineation_result
  558. );
  559. } else {
  560. $result = $objExercise->manage_answer(
  561. $exeId,
  562. $my_question_id,
  563. $my_choice,
  564. 'exercise_result',
  565. $hot_spot_coordinates,
  566. true,
  567. false,
  568. false,
  569. $objExercise->selectPropagateNeg(),
  570. $hotspot_delineation_result
  571. );
  572. }
  573. // Adding the new score.
  574. $total_score += $result['score'];
  575. if ($debug) {
  576. error_log("total_score: $total_score ");
  577. error_log("total_weight: $total_weight ");
  578. }
  579. $duration = 0;
  580. $now = time();
  581. if ($type == 'all') {
  582. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exeId);
  583. }
  584. $key = ExerciseLib::get_time_control_key(
  585. $exercise_id,
  586. $exercise_stat_info['orig_lp_id'],
  587. $exercise_stat_info['orig_lp_item_id']
  588. );
  589. $durationTime = Session::read('duration_time');
  590. if (isset($durationTime[$key]) && !empty($durationTime[$key])) {
  591. if ($debug) {
  592. error_log('Session time :'.$durationTime[$key]);
  593. }
  594. $duration = $now - $durationTime[$key];
  595. if (!empty($exercise_stat_info['exe_duration'])) {
  596. $duration += $exercise_stat_info['exe_duration'];
  597. }
  598. $duration = (int) $duration;
  599. } else {
  600. if (!empty($exercise_stat_info['exe_duration'])) {
  601. $duration = $exercise_stat_info['exe_duration'];
  602. }
  603. }
  604. if ($debug) {
  605. error_log('duration to save in DB:'.$duration);
  606. }
  607. Session::write('duration_time', [$key => $now]);
  608. Event::updateEventExercise(
  609. $exeId,
  610. $objExercise->selectId(),
  611. $total_score,
  612. $total_weight,
  613. $session_id,
  614. $exercise_stat_info['orig_lp_id'],
  615. $exercise_stat_info['orig_lp_item_id'],
  616. $exercise_stat_info['orig_lp_item_view_id'],
  617. $duration,
  618. $question_list,
  619. 'incomplete',
  620. $remind_list
  621. );
  622. // Destruction of the Question object
  623. unset($objQuestionTmp);
  624. if ($debug) {
  625. error_log("---------- end question ------------");
  626. }
  627. }
  628. }
  629. if ($type == 'all') {
  630. echo 'ok';
  631. exit;
  632. }
  633. if ($objExercise->type == ONE_PER_PAGE) {
  634. if ($debug) {
  635. error_log("result: one_per_page");
  636. error_log(" ------ end ajax call ------- ");
  637. }
  638. echo 'one_per_page';
  639. exit;
  640. }
  641. if ($debug) {
  642. error_log("result: ok");
  643. error_log(" ------ end ajax call ------- ");
  644. }
  645. echo 'ok';
  646. break;
  647. case 'show_question':
  648. $isAllowedToEdit = api_is_allowed_to_edit(null, true, false, false);
  649. if (!$isAllowedToEdit) {
  650. api_not_allowed(true);
  651. exit;
  652. }
  653. $questionId = isset($_GET['question']) ? (int) $_GET['question'] : 0;
  654. $exerciseId = isset($_REQUEST['exercise']) ? (int) $_REQUEST['exercise'] : 0;
  655. if (!$questionId || !$exerciseId) {
  656. break;
  657. }
  658. $objExercise = new Exercise();
  659. $objExercise->read($exerciseId);
  660. $objQuestion = Question::read($questionId);
  661. $id = '';
  662. if (api_get_configuration_value('show_question_id')) {
  663. $id = '<h4>#'.$objQuestion->course['code'].'-'.$objQuestion->iid.'</h4>';
  664. }
  665. echo $id;
  666. echo '<p class="lead">'.$objQuestion->get_question_type_name().'</p>';
  667. if ($objQuestion->type === FILL_IN_BLANKS) {
  668. echo '<script>
  669. $(function() {
  670. $(".selectpicker").selectpicker({});
  671. });
  672. </script>';
  673. }
  674. // Allows render MathJax elements in a ajax call
  675. if (api_get_setting('include_asciimathml_script') === 'true') {
  676. echo '<script> MathJax.Hub.Queue(["Typeset",MathJax.Hub]);</script>';
  677. }
  678. ExerciseLib::showQuestion(
  679. $objExercise,
  680. $questionId,
  681. false,
  682. null,
  683. null,
  684. false,
  685. true,
  686. false,
  687. true,
  688. true
  689. );
  690. break;
  691. case 'get_quiz_embeddable':
  692. $exercises = ExerciseLib::get_all_exercises_for_course_id(
  693. api_get_course_info(),
  694. api_get_session_id(),
  695. api_get_course_int_id(),
  696. false
  697. );
  698. $exercises = array_filter(
  699. $exercises,
  700. function (array $exercise) {
  701. return ExerciseLib::isQuizEmbeddable($exercise);
  702. }
  703. );
  704. $result = [];
  705. $codePath = api_get_path(WEB_CODE_PATH);
  706. foreach ($exercises as $exercise) {
  707. $title = Security::remove_XSS(api_html_entity_decode($exercise['title']));
  708. $result[] = [
  709. 'id' => $exercise['iid'],
  710. 'title' => strip_tags($title),
  711. ];
  712. }
  713. header('Content-Type: application/json');
  714. echo json_encode($result);
  715. break;
  716. default:
  717. echo '';
  718. }
  719. exit;