exercise.ajax.php 17 KB

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