exercise.ajax.php 20 KB

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