exercise.ajax.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. $course_id = api_get_course_int_id();
  12. if ($debug) {
  13. error_log("$action ajax call");
  14. }
  15. $session_id = isset($_REQUEST['session_id']) ? intval($_REQUEST['session_id']) : api_get_session_id();
  16. $course_code = isset($_REQUEST['cidReq']) ? $_REQUEST['cidReq'] : api_get_course_id();
  17. switch ($action) {
  18. case 'get_live_stats':
  19. if (!api_is_allowed_to_edit(null, true)) {
  20. break;
  21. }
  22. // 1. Setting variables needed by jqgrid
  23. $action = $_GET['a'];
  24. $exercise_id = intval($_GET['exercise_id']);
  25. $page = intval($_REQUEST['page']); //page
  26. $limit = intval($_REQUEST['rows']); //quantity of rows
  27. $sidx = $_REQUEST['sidx']; //index to filter
  28. $sord = $_REQUEST['sord']; //asc or desc
  29. if (!in_array($sord, array('asc', 'desc'))) {
  30. $sord = 'desc';
  31. }
  32. // get index row - i.e. user click to sort $sord = $_GET['sord'];
  33. // get the direction
  34. if (!$sidx) {
  35. $sidx = 1;
  36. }
  37. $track_exercise = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  38. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  39. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  40. $minutes = intval($_REQUEST['minutes']);
  41. $now = time() - 60 * $minutes;
  42. $now = api_get_utc_datetime($now);
  43. $where_condition = " orig_lp_id = 0 AND exe_exo_id = $exercise_id AND start_date > '$now' ";
  44. $sql = "SELECT COUNT(DISTINCT exe_id)
  45. FROM $track_exercise
  46. WHERE $where_condition ";
  47. $result = Database::query($sql);
  48. $count = Database::fetch_row($result);
  49. $count = $count[0];
  50. //3. Calculating first, end, etc
  51. $total_pages = 0;
  52. if ($count > 0) {
  53. if (!empty($limit)) {
  54. $total_pages = ceil($count / $limit);
  55. }
  56. }
  57. if ($page > $total_pages) {
  58. $page = $total_pages;
  59. }
  60. $start = $limit * $page - $limit;
  61. if ($start < 0) {
  62. $start = 0;
  63. }
  64. $sql = "SELECT
  65. exe_id,
  66. exe_user_id,
  67. firstname,
  68. lastname,
  69. aa.status,
  70. start_date,
  71. exe_result,
  72. exe_weighting,
  73. exe_result/exe_weighting as score,
  74. exe_duration,
  75. questions_to_check,
  76. orig_lp_id
  77. FROM $user_table u
  78. INNER JOIN (
  79. SELECT
  80. t.exe_id,
  81. t.exe_user_id,
  82. status,
  83. start_date,
  84. exe_result,
  85. exe_weighting,
  86. exe_result/exe_weighting as score,
  87. exe_duration,
  88. questions_to_check,
  89. orig_lp_id
  90. FROM $track_exercise t
  91. LEFT JOIN $track_attempt a
  92. ON (a.exe_id = t.exe_id AND t.exe_user_id = a.user_id)
  93. WHERE t.status = 'incomplete' AND $where_condition
  94. GROUP BY exe_user_id
  95. ) as aa
  96. ON aa.exe_user_id = user_id
  97. ORDER BY $sidx $sord
  98. LIMIT $start, $limit";
  99. $result = Database::query($sql);
  100. $results = array();
  101. while ($row = Database::fetch_array($result, 'ASSOC')) {
  102. $results[] = $row;
  103. }
  104. $oExe = new Exercise();
  105. $oExe->read($exercise_id);
  106. $response = new stdClass();
  107. $response->page = $page;
  108. $response->total = $total_pages;
  109. $response->records = $count;
  110. $i = 0;
  111. if (!empty($results)) {
  112. foreach ($results as $row) {
  113. $sql = "SELECT SUM(count_question_id) as count_question_id
  114. FROM (
  115. SELECT 1 as count_question_id
  116. FROM $track_attempt a
  117. WHERE
  118. user_id = {$row['exe_user_id']} AND
  119. exe_id = {$row['exe_id']}
  120. GROUP by question_id
  121. ) as count_table";
  122. $result_count = Database::query($sql);
  123. $count_questions = Database::fetch_array(
  124. $result_count,
  125. 'ASSOC'
  126. );
  127. $count_questions = $count_questions['count_question_id'];
  128. $row['count_questions'] = $count_questions;
  129. $response->rows[$i]['id'] = $row['exe_id'];
  130. if (!empty($oExe->expired_time)) {
  131. $remaining = strtotime($row['start_date']) + ($oExe->expired_time * 60) - strtotime(api_get_utc_datetime(time()));
  132. $h = floor($remaining / 3600);
  133. $m = floor(($remaining - ($h * 3600)) / 60);
  134. $s = ($remaining - ($h * 3600) - ($m * 60));
  135. $timeInfo = api_format_date(
  136. $row['start_date'],
  137. DATE_TIME_FORMAT_LONG
  138. ).' ['.($h > 0 ? $h.':' : '').sprintf("%02d", $m).':'.sprintf("%02d", $s).']';
  139. } else {
  140. $timeInfo = api_format_date(
  141. $row['start_date'],
  142. DATE_TIME_FORMAT_LONG
  143. );
  144. }
  145. $array = array(
  146. $row['firstname'],
  147. $row['lastname'],
  148. $timeInfo,
  149. $row['count_questions'],
  150. round($row['score'] * 100).'%'
  151. );
  152. $response->rows[$i]['cell'] = $array;
  153. $i++;
  154. }
  155. }
  156. echo json_encode($response);
  157. break;
  158. case 'update_exercise_list_order':
  159. if (api_is_allowed_to_edit(null, true)) {
  160. $new_list = $_REQUEST['exercise_list'];
  161. $table = Database::get_course_table(TABLE_QUIZ_ORDER);
  162. $counter = 1;
  163. //Drop all
  164. $sql = "DELETE FROM $table WHERE session_id = $session_id AND c_id = $course_id";
  165. Database::query($sql);
  166. // Insert all
  167. foreach ($new_list as $new_order_id) {
  168. Database::insert(
  169. $table,
  170. array(
  171. 'exercise_order' => $counter,
  172. 'session_id' => $session_id,
  173. 'exercise_id' => intval($new_order_id),
  174. 'c_id' => $course_id
  175. )
  176. );
  177. $counter++;
  178. }
  179. echo Display::return_message(get_lang('Saved'), 'confirmation');
  180. }
  181. break;
  182. case 'update_question_order':
  183. $course_info = api_get_course_info_by_id($course_id);
  184. $course_id = $course_info['real_id'];
  185. $exercise_id = isset($_REQUEST['exercise_id']) ? $_REQUEST['exercise_id'] : null;
  186. if (empty($exercise_id)) {
  187. return Display::return_message(get_lang('Error'), 'error');
  188. }
  189. if (api_is_allowed_to_edit(null, true)) {
  190. $new_question_list = $_POST['question_id_list'];
  191. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  192. $counter = 1;
  193. foreach ($new_question_list as $new_order_id) {
  194. Database::update(
  195. $TBL_QUESTIONS,
  196. array('question_order' => $counter),
  197. array(
  198. 'question_id = ? AND c_id = ? AND exercice_id = ? ' => array(
  199. intval(
  200. $new_order_id
  201. ),
  202. $course_id,
  203. $exercise_id
  204. )
  205. )
  206. )
  207. ;
  208. $counter++;
  209. }
  210. echo Display::return_message(get_lang('Saved'), 'confirmation');
  211. }
  212. break;
  213. case 'add_question_to_reminder':
  214. /** @var Exercise $objExercise */
  215. $objExercise = Session::read('objExercise');
  216. if (empty($objExercise)) {
  217. echo 0;
  218. exit;
  219. } else {
  220. $objExercise->editQuestionToRemind(
  221. $_REQUEST['exe_id'],
  222. $_REQUEST['question_id'],
  223. $_REQUEST['action']
  224. );
  225. }
  226. break;
  227. case 'save_exercise_by_now':
  228. $course_info = api_get_course_info_by_id($course_id);
  229. $course_id = $course_info['real_id'];
  230. // Use have permissions?
  231. if (api_is_allowed_to_session_edit()) {
  232. // "all" or "simple" strings means that there's one or all questions exercise type
  233. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  234. // Questions choices.
  235. $choice = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : null;
  236. // Hot spot coordinates from all questions.
  237. $hot_spot_coordinates = isset($_REQUEST['hotspot']) ? $_REQUEST['hotspot'] : null;
  238. // There is a reminder?
  239. $remind_list = isset($_REQUEST['remind_list']) && !empty($_REQUEST['remind_list']) ? array_keys($_REQUEST['remind_list']) : null;
  240. // Needed in manage_answer.
  241. $learnpath_id = isset($_REQUEST['learnpath_id']) ? intval($_REQUEST['learnpath_id']) : 0;
  242. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? intval($_REQUEST['learnpath_item_id']) : 0;
  243. // Attempt id.
  244. $exe_id = $_REQUEST['exe_id'];
  245. if ($debug) {
  246. error_log("exe_id = $exe_id");
  247. error_log("type = $type");
  248. error_log("choice = ".print_r($choice, 1)." ");
  249. error_log("hot_spot_coordinates = ".print_r($hot_spot_coordinates, 1));
  250. error_log("remind_list = ".print_r($remind_list, 1));
  251. }
  252. // Exercise information.
  253. /** @var Exercise $objExercise */
  254. $objExercise = Session::read('objExercise');
  255. // Question info.
  256. $question_id = isset($_REQUEST['question_id']) ? intval($_REQUEST['question_id']) : null;
  257. $question_list = Session::read('questionList');
  258. // If exercise or question is not set then exit.
  259. if (empty($question_list) || empty($objExercise)) {
  260. echo 'error';
  261. if ($debug) {
  262. if (empty($question_list)) {
  263. error_log("question_list is empty");
  264. }
  265. if (empty($objExercise)) {
  266. error_log("objExercise is empty");
  267. }
  268. }
  269. exit;
  270. }
  271. // Getting information of the current exercise.
  272. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
  273. $exercise_id = $exercise_stat_info['exe_exo_id'];
  274. $attemptList = array();
  275. // First time here we create an attempt (getting the exe_id).
  276. if (!empty($exercise_stat_info)) {
  277. // We know the user we get the exe_id.
  278. $exe_id = $exercise_stat_info['exe_id'];
  279. $total_score = $exercise_stat_info['exe_result'];
  280. // Getting the list of attempts
  281. $attemptList = Event::getAllExerciseEventByExeId($exe_id);
  282. }
  283. // Updating Reminder algorythm.
  284. if ($objExercise->type == ONE_PER_PAGE) {
  285. $bd_reminder_list = explode(',', $exercise_stat_info['questions_to_check']);
  286. if (empty($remind_list)) {
  287. $remind_list = $bd_reminder_list;
  288. $new_list = array();
  289. foreach ($bd_reminder_list as $item) {
  290. if ($item != $question_id) {
  291. $new_list[] = $item;
  292. }
  293. }
  294. $remind_list = $new_list;
  295. } else {
  296. if (isset($remind_list[0])) {
  297. if (!in_array($remind_list[0], $bd_reminder_list)) {
  298. array_push($bd_reminder_list, $remind_list[0]);
  299. }
  300. $remind_list = $bd_reminder_list;
  301. }
  302. }
  303. }
  304. // No exe id? Can't save answer.
  305. if (empty($exe_id)) {
  306. // Fires an error.
  307. echo 'error';
  308. if ($debug) {
  309. error_log("exe_id is empty");
  310. }
  311. exit;
  312. }
  313. Session::write('exe_id', $exe_id);
  314. // Getting the total weight if the request is simple
  315. $total_weight = 0;
  316. if ($type == 'simple') {
  317. foreach ($question_list as $my_question_id) {
  318. $objQuestionTmp = Question::read($my_question_id, $course_id);
  319. $total_weight += $objQuestionTmp->selectWeighting();
  320. }
  321. }
  322. unset($objQuestionTmp);
  323. // Looping the question list
  324. foreach ($question_list as $my_question_id) {
  325. if ($debug) {
  326. error_log("Saving question_id = $my_question_id ");
  327. }
  328. if ($type == 'simple' && $question_id != $my_question_id) {
  329. continue;
  330. }
  331. $my_choice = isset($choice[$my_question_id]) ? $choice[$my_question_id] : null;
  332. if ($debug) {
  333. error_log("my_choice = ".print_r($my_choice, 1)."");
  334. }
  335. // Creates a temporary Question object
  336. $objQuestionTmp = Question::read($my_question_id, $course_id);
  337. // Getting free choice data.
  338. if (($objQuestionTmp->type == FREE_ANSWER || $objQuestionTmp->type == ORAL_EXPRESSION) &&
  339. $type == 'all'
  340. ) {
  341. $my_choice = isset($_REQUEST['free_choice'][$my_question_id]) && !empty($_REQUEST['free_choice'][$my_question_id])
  342. ? $_REQUEST['free_choice'][$my_question_id]
  343. : null;
  344. }
  345. if ($type == 'all') {
  346. $total_weight += $objQuestionTmp->selectWeighting();
  347. }
  348. // This variable came from exercise_submit_modal.php.
  349. $hotspot_delineation_result = null;
  350. if (isset($_SESSION['hotspot_delineation_result']) &&
  351. isset($_SESSION['hotspot_delineation_result'][$objExercise->selectId()])
  352. ) {
  353. $hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$my_question_id];
  354. }
  355. if ($type == 'simple') {
  356. // Getting old attempt in order to decrees the total score.
  357. $old_result = $objExercise->manage_answer(
  358. $exe_id,
  359. $my_question_id,
  360. null,
  361. 'exercise_show',
  362. array(),
  363. false,
  364. true,
  365. false,
  366. $objExercise->selectPropagateNeg(),
  367. array()
  368. );
  369. // Removing old score.
  370. $total_score = $total_score - $old_result['score'];
  371. }
  372. // Deleting old attempt
  373. if (isset($attemptList) && !empty($attemptList[$my_question_id])) {
  374. if ($debug) {
  375. error_log("delete_attempt exe_id : $exe_id, my_question_id: $my_question_id");
  376. }
  377. Event::delete_attempt(
  378. $exe_id,
  379. api_get_user_id(),
  380. $course_id,
  381. $session_id,
  382. $my_question_id
  383. );
  384. if ($objQuestionTmp->type == HOT_SPOT) {
  385. Event::delete_attempt_hotspot(
  386. $exe_id,
  387. api_get_user_id(),
  388. $course_id,
  389. $session_id,
  390. $my_question_id
  391. );
  392. }
  393. if (isset($attemptList[$my_question_id]) &&
  394. isset($attemptList[$my_question_id]['marks'])
  395. ) {
  396. $total_score -= $attemptList[$my_question_id]['marks'];
  397. }
  398. }
  399. // We're inside *one* question. Go through each possible answer for this question
  400. $result = $objExercise->manage_answer(
  401. $exe_id,
  402. $my_question_id,
  403. $my_choice,
  404. 'exercise_result',
  405. $hot_spot_coordinates,
  406. true,
  407. false,
  408. false,
  409. $objExercise->selectPropagateNeg(),
  410. $hotspot_delineation_result
  411. );
  412. // Adding the new score.
  413. $total_score += $result['score'];
  414. if ($debug) {
  415. error_log("total_score: $total_score ");
  416. error_log("total_weight: $total_weight ");
  417. }
  418. $duration = 0;
  419. $now = time();
  420. if ($type == 'all') {
  421. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
  422. }
  423. $key = ExerciseLib::get_time_control_key(
  424. $exercise_id,
  425. $exercise_stat_info['orig_lp_id'],
  426. $exercise_stat_info['orig_lp_item_id']
  427. );
  428. if (isset($_SESSION['duration_time'][$key]) && !empty($_SESSION['duration_time'][$key])) {
  429. $duration = $now - $_SESSION['duration_time'][$key];
  430. if (!empty($exercise_stat_info['exe_duration'])) {
  431. $duration += $exercise_stat_info['exe_duration'];
  432. }
  433. $duration = intval($duration);
  434. } else {
  435. if (!empty($exercise_stat_info['exe_duration'])) {
  436. $duration = $exercise_stat_info['exe_duration'];
  437. }
  438. }
  439. $_SESSION['duration_time'][$key] = time();
  440. Event::updateEventExercise(
  441. $exe_id,
  442. $objExercise->selectId(),
  443. $total_score,
  444. $total_weight,
  445. $session_id,
  446. $exercise_stat_info['orig_lp_id'],
  447. $exercise_stat_info['orig_lp_item_id'],
  448. $exercise_stat_info['orig_lp_item_view_id'],
  449. $duration,
  450. $question_list,
  451. 'incomplete',
  452. $remind_list
  453. );
  454. // Destruction of the Question object
  455. unset($objQuestionTmp);
  456. if ($debug) {
  457. error_log(" -- end question -- ");
  458. }
  459. }
  460. if ($debug) {
  461. error_log(" ------ end ajax call ------- ");
  462. }
  463. }
  464. if ($objExercise->type == ONE_PER_PAGE) {
  465. echo 'one_per_page';
  466. exit;
  467. }
  468. echo 'ok';
  469. break;
  470. case 'show_question':
  471. $isAllowedToEdit = api_is_allowed_to_edit(null, true, false, false);
  472. if (!$isAllowedToEdit) {
  473. api_not_allowed(true);
  474. exit;
  475. }
  476. $questionId = isset($_GET['question']) ? intval($_GET['question']) : 0;
  477. $exerciseId = isset($_REQUEST['exercise']) ? intval($_REQUEST['exercise']) : 0;
  478. if (!$questionId || !$exerciseId) {
  479. break;
  480. }
  481. $objExercise = new Exercise();
  482. $objExercise->read($exerciseId);
  483. $objQuestion = Question::read($questionId);
  484. $objQuestion->get_question_type_name();
  485. echo '<p class="lead">'.$objQuestion->get_question_type_name().'</p>';
  486. ExerciseLib::showQuestion(
  487. $questionId,
  488. false,
  489. null,
  490. null,
  491. false,
  492. true,
  493. false,
  494. true,
  495. $objExercise->feedback_type,
  496. true
  497. );
  498. break;
  499. default:
  500. echo '';
  501. }
  502. exit;