exercise.ajax.php 12 KB

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