exercise.ajax.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. switch ($action) {
  16. case 'get_live_stats':
  17. if (!api_is_allowed_to_edit(null, true)) {
  18. break;
  19. }
  20. // 1. Setting variables needed by jqgrid
  21. $action = $_GET['a'];
  22. $exercise_id = intval($_GET['exercise_id']);
  23. $page = intval($_REQUEST['page']); //page
  24. $limit = intval($_REQUEST['rows']); //quantity of rows
  25. $sidx = $_REQUEST['sidx']; //index to filter
  26. $sord = $_REQUEST['sord']; //asc or desc
  27. if (!in_array($sord, array('asc','desc'))) {
  28. $sord = 'desc';
  29. }
  30. // get index row - i.e. user click to sort $sord = $_GET['sord'];
  31. // get the direction
  32. if (!$sidx) $sidx = 1;
  33. $track_exercise = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  34. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  35. $track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  36. $minutes = intval($_REQUEST['minutes']);
  37. $now = time() - 60*$minutes; //1 hour
  38. $now = api_get_utc_datetime($now);
  39. $where_condition = " orig_lp_id = 0 AND exe_exo_id = $exercise_id AND start_date > '$now' ";
  40. $sql = "SELECT COUNT(DISTINCT exe_id) FROM $track_exercise WHERE $where_condition ";
  41. $result = Database::query($sql);
  42. $count = Database::fetch_row($result);
  43. $count = $count[0];
  44. //3. Calculating first, end, etc
  45. $total_pages = 0;
  46. if ($count > 0) {
  47. if (!empty($limit)) {
  48. $total_pages = ceil($count/$limit);
  49. }
  50. }
  51. if ($page > $total_pages) {
  52. $page = $total_pages;
  53. }
  54. $start = $limit * $page - $limit;
  55. if ($start < 0 ) {
  56. $start = 0;
  57. }
  58. $sql = "SELECT count_questions, exe_user_id, firstname, lastname, aa.status, start_date, exe_result, exe_weighting, exe_result/exe_weighting as score, exe_duration, questions_to_check, orig_lp_id
  59. FROM $user_table u INNER JOIN (
  60. SELECT t.exe_user_id, count(question_id) as count_questions, status,
  61. start_date, exe_result, exe_weighting, exe_result/exe_weighting as score, exe_duration, questions_to_check, orig_lp_id
  62. FROM $track_exercise t LEFT JOIN $track_attempt a ON (a.exe_id = t.exe_id AND t.exe_user_id = a.user_id )
  63. WHERE t.status = 'incomplete' AND
  64. $where_condition
  65. GROUP BY exe_user_id
  66. ) as aa
  67. ON aa.exe_user_id = user_id
  68. ORDER BY $sidx $sord LIMIT $start, $limit";
  69. //echo $sql;
  70. $result = Database::query($sql);
  71. $results = array();
  72. while ($row = Database::fetch_array($result,'ASSOC')){
  73. $results[] = $row;
  74. }
  75. $response = new stdClass();
  76. $response->page = $page;
  77. $response->total = $total_pages;
  78. $response->records = $count;
  79. $i=0;
  80. if (!empty($results)) {
  81. foreach($results as $row) {
  82. //$user_info = api_get_user_info($row['exe_user_id']);
  83. //print_r($row);
  84. $response->rows[$i]['id'] = $row['exe_id'];
  85. $array = array( $row['firstname'],
  86. $row['lastname'],
  87. api_format_date($row['start_date'], DATE_TIME_FORMAT_LONG),
  88. $row['count_questions'],
  89. round($row['score']*100).'%'
  90. );
  91. $response->rows[$i]['cell'] = $array;
  92. $i++;
  93. }
  94. }
  95. echo json_encode($response);
  96. break;
  97. case 'update_question_order':
  98. if (api_is_allowed_to_edit(null, true)) {
  99. $new_question_list = $_POST['question_id_list'];
  100. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  101. $counter = 1;
  102. foreach ($new_question_list as $new_order_id) {
  103. Database::update($TBL_QUESTIONS, array('question_order'=>$counter), array('question_id = ? AND c_id = ? '=>array(intval($new_order_id), $course_id)));
  104. $counter++;
  105. }
  106. Display::display_confirmation_message(get_lang('Saved'));
  107. }
  108. break;
  109. case 'add_question_to_reminder':
  110. $objExercise = $_SESSION['objExercise'];
  111. if (empty($objExercise)) {
  112. echo 0;
  113. exit;
  114. } else {
  115. $objExercise->edit_question_to_remind($_REQUEST['exe_id'], $_REQUEST['question_id'], $_REQUEST['action']);
  116. }
  117. break;
  118. case 'save_exercise_by_now':
  119. //Use have permissions?
  120. if (api_is_allowed_to_session_edit()) {
  121. //"all" or "simple" strings means that there's one or all questions
  122. $type = $_REQUEST['type'];
  123. //Normal questions choices
  124. $choice = $_REQUEST['choice'];
  125. //All Hotspot coordinates from all questions
  126. $hot_spot_coordinates = $_REQUEST['hotspot'];
  127. //There is a reminder?
  128. $remind_list = isset($_REQUEST['remind_list']) && !empty($_REQUEST['remind_list'])? array_keys($_REQUEST['remind_list']) : null;
  129. $exe_id = $_REQUEST['exe_id'];
  130. if ($debug) error_log("exe_id = $exe_id ");
  131. if ($debug) error_log("type = $type ");
  132. if ($debug) error_log("choice = ".print_r($choice, 1)." ");
  133. if ($debug) error_log("hot_spot_coordinates = $hot_spot_coordinates ");
  134. if ($debug) error_log("remind_list = $remind_list ");
  135. //Exercise information
  136. $question_id = intval($_REQUEST['question_id']);
  137. $question_list = $_SESSION['questionList'];
  138. $objExercise = $_SESSION['objExercise'];
  139. if (empty($question_list) || empty($objExercise)) {
  140. echo 0;
  141. exit;
  142. }
  143. //Getting information of the current exercise
  144. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
  145. $attempt_list = array();
  146. //First time here we create an attempt (getting the exe_id)
  147. if (empty($exercise_stat_info)) {
  148. /*
  149. //$exe_id = create_event_exercice($objExercise->selectId());
  150. $current_expired_time_key = get_time_control_key($objExercise->id);
  151. if (isset($_SESSION['expired_time'][$current_expired_time_key])) { //Only for exercice of type "One page"
  152. $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
  153. } else {
  154. $expired_date = '0000-00-00 00:00:00';
  155. }
  156. $exe_id = $objExercise->save_stat_track_exercise_info($expired_date, $safe_lp_id, $safe_lp_item_id, $safe_lp_item_view_id, $question_list, 0); //total weight 0 by now
  157. $total_score = $total_weight = 0;
  158. */
  159. } else {
  160. //We know the user we get the exe_id
  161. $exe_id = $exercise_stat_info['exe_id'];
  162. $total_score = $exercise_stat_info['exe_result'];
  163. //Getting the list of attempts
  164. $attempt_list = get_all_exercise_event_by_exe_id($exe_id);
  165. }
  166. //Updating Reminder algorythm
  167. if ($objExercise->type == ONE_PER_PAGE) {
  168. $bd_reminder_list = explode(',', $exercise_stat_info['questions_to_check']);
  169. if (empty($remind_list)) {
  170. $remind_list = $bd_reminder_list;
  171. $new_list = array();
  172. foreach($bd_reminder_list as $item) {
  173. if ($item != $question_id) {
  174. $new_list[] = $item;
  175. }
  176. }
  177. $remind_list = $new_list;
  178. } else {
  179. if (isset($remind_list[0])) {
  180. if (!in_array($remind_list[0], $bd_reminder_list)) {
  181. array_push($bd_reminder_list, $remind_list[0]);
  182. }
  183. $remind_list = $bd_reminder_list;
  184. }
  185. }
  186. }
  187. //No exe id? Can't save answer
  188. if (empty($exe_id)) {
  189. //Fires an error
  190. echo 'error';
  191. exit;
  192. } else {
  193. $_SESSION['exe_id'] = $exe_id;
  194. }
  195. // Getting the total weight if the request is simple
  196. $total_weight = 0;
  197. if ($type == 'simple') {
  198. foreach($question_list as $my_question_id) {
  199. $objQuestionTmp = Question :: read($my_question_id);
  200. $total_weight += $objQuestionTmp->selectWeighting();
  201. }
  202. }
  203. unset($objQuestionTmp);
  204. //Looping the question list
  205. foreach ($question_list as $my_question_id) {
  206. if ($debug) error_log("Saving question_id = $my_question_id ");
  207. if ($type == 'simple' && $question_id != $my_question_id) {
  208. continue;
  209. }
  210. $my_choice = $choice[$my_question_id];
  211. if ($debug) error_log("my_choice = ".print_r($my_choice, 1)."");
  212. // creates a temporary Question object
  213. $objQuestionTmp = Question::read($my_question_id);
  214. //Getting free choice data
  215. if ($objQuestionTmp->type == FREE_ANSWER && $type == 'all') {
  216. $my_choice = isset($_REQUEST['free_choice'][$my_question_id]) && !empty($_REQUEST['free_choice'][$my_question_id])? $_REQUEST['free_choice'][$my_question_id]: null;
  217. }
  218. if ($type == 'all') {
  219. $total_weight += $objQuestionTmp->selectWeighting();
  220. }
  221. //this variable commes from exercise_submit_modal.php
  222. $hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$my_question_id];
  223. // Deleting old attempt
  224. if (isset($attempt_list) && !empty($attempt_list[$my_question_id])) {
  225. if ($debug) error_log("delete_attempt exe_id : $exe_id, my_question_id: $my_question_id");
  226. delete_attempt($exe_id, api_get_user_id() , api_get_course_id(), api_get_session_id(), $my_question_id);
  227. if ($objQuestionTmp->type == HOT_SPOT) {
  228. delete_attempt_hotspot($exe_id, api_get_user_id() , api_get_course_id(), $my_question_id);
  229. }
  230. $total_score -= $attempt_list[$my_question_id]['marks'];
  231. }
  232. // We're inside *one* question. Go through each possible answer for this question
  233. $result = $objExercise->manage_answer($exe_id, $my_question_id, $my_choice,'exercise_result', $hot_spot_coordinates, true, false, $show_results, $objExercise->selectPropagateNeg(), $hotspot_delineation_result, true);
  234. $total_score += $result['score'];
  235. if ($debug) error_log("total_score: $total_score ");
  236. if ($debug) error_log("total_weight: $total_weight ");
  237. update_event_exercice($exe_id, $objExercise->selectId(), $total_score, $total_weight, api_get_session_id(), $exercise_stat_info['orig_lp_id'], $exercise_stat_info['orig_lp_item_id'], $exercise_stat_info['orig_lp_item_view_id'], $exercise_stat_info['exe_duration'], $question_list, 'incomplete', $remind_list);
  238. // Destruction of the Question object
  239. unset($objQuestionTmp);
  240. if ($debug) error_log(" -- end question -- ");
  241. }
  242. if ($debug) error_log(" ------ end ajax call ------- ");
  243. }
  244. if ($objExercise->type == ONE_PER_PAGE) {
  245. echo 'one_per_page';
  246. exit;
  247. }
  248. echo 'ok';
  249. break;
  250. default:
  251. echo '';
  252. }
  253. exit;