exercise.ajax.php 18 KB

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