exercise.ajax.php 19 KB

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