exercise.ajax.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 '../../exercice/exercise.lib.php';
  10. require_once '../global.inc.php';
  11. api_protect_course_script(true);
  12. $action = $_REQUEST['a'];
  13. switch ($action) {
  14. case 'update_question_order':
  15. if (api_is_allowed_to_edit(null, true)) {
  16. $new_question_list = $_POST['question_id_list'];
  17. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  18. $counter = 1;
  19. foreach ($new_question_list as $new_order_id) {
  20. Database::update($TBL_QUESTIONS, array('question_order'=>$counter), array('question_id = ? '=>intval($new_order_id)));
  21. $counter++;
  22. }
  23. Display::display_confirmation_message(get_lang('Saved'));
  24. }
  25. break;
  26. case 'add_question_to_reminder':
  27. $objExercise = $_SESSION['objExercise'];
  28. if (empty($objExercise)) {
  29. echo 0;
  30. exit;
  31. } else {
  32. $objExercise->edit_question_to_remind($_REQUEST['exe_id'], $_REQUEST['question_id'], $_REQUEST['action']);
  33. }
  34. break;
  35. case 'save_exercise_by_now':
  36. //Use have permissions?
  37. if (api_is_allowed_to_session_edit()) {
  38. //"all" or "simple" strings means that there's one or all questions
  39. $type = $_REQUEST['type'];
  40. //Normal questions choices
  41. $choice = $_REQUEST['choice'];
  42. //All Hotspot coordinates from all questions
  43. $hot_spot_coordinates = $_REQUEST['hotspot'];
  44. //There is a reminder?
  45. $remind_list = isset($_REQUEST['remind_list']) && !empty($_REQUEST['remind_list'])? array_keys($_REQUEST['remind_list']) : null;
  46. $exe_id = $_REQUEST['exe_id'];
  47. //Exercise information
  48. $question_id = intval($_REQUEST['question_id']);
  49. $question_list = $_SESSION['questionList'];
  50. $objExercise = $_SESSION['objExercise'];
  51. if (empty($question_list) || empty($objExercise)) {
  52. echo 0;
  53. exit;
  54. }
  55. //Getting information of the current exercise
  56. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
  57. $attempt_list = array();
  58. //First time here we create an attempt (getting the exe_id)
  59. if (empty($exercise_stat_info)) {
  60. /*
  61. //$exe_id = create_event_exercice($objExercise->selectId());
  62. $current_expired_time_key = get_time_control_key($objExercise->id);
  63. if (isset($_SESSION['expired_time'][$current_expired_time_key])) { //Only for exercice of type "One page"
  64. $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
  65. } else {
  66. $expired_date = '0000-00-00 00:00:00';
  67. }
  68. $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
  69. $total_score = $total_weight = 0;
  70. */
  71. } else {
  72. //We know the user we get the exe_id
  73. $exe_id = $exercise_stat_info['exe_id'];
  74. $total_score = $exercise_stat_info['exe_result'];
  75. //Getting the list of attempts
  76. $attempt_list = get_all_exercise_event_by_exe_id($exe_id);
  77. }
  78. //Updating Reminder algorythm
  79. if ($objExercise->type == ONE_PER_PAGE) {
  80. $bd_reminder_list = explode(',', $exercise_stat_info['questions_to_check']);
  81. if (empty($remind_list)) {
  82. $remind_list = $bd_reminder_list;
  83. $new_list = array();
  84. foreach($bd_reminder_list as $item) {
  85. if ($item != $question_id) {
  86. $new_list[] = $item;
  87. }
  88. }
  89. $remind_list = $new_list;
  90. } else {
  91. if (isset($remind_list[0])) {
  92. if (!in_array($remind_list[0], $bd_reminder_list)) {
  93. array_push($bd_reminder_list, $remind_list[0]);
  94. }
  95. $remind_list = $bd_reminder_list;
  96. }
  97. }
  98. }
  99. //No exe id? Can't save answer
  100. if (empty($exe_id)) {
  101. //Fires an error
  102. echo 'error';
  103. exit;
  104. } else {
  105. $_SESSION['exe_id'] = $exe_id;
  106. }
  107. // Getting the total weight if the request is simple
  108. $total_weight = 0;
  109. if ($type == 'simple') {
  110. foreach($question_list as $my_question_id) {
  111. $objQuestionTmp = Question :: read($my_question_id);
  112. $total_weight += $objQuestionTmp->selectWeighting();
  113. }
  114. }
  115. unset($objQuestionTmp);
  116. //Looping the question list
  117. foreach($question_list as $my_question_id) {
  118. if ($type == 'simple' && $question_id != $my_question_id) {
  119. continue;
  120. }
  121. $my_choice = $choice[$my_question_id];
  122. // creates a temporary Question object
  123. $objQuestionTmp = Question::read($my_question_id);
  124. //Getting free choice data
  125. if ($objQuestionTmp->type == FREE_ANSWER && $type == 'all') {
  126. $my_choice = isset($_REQUEST['free_choice'][$my_question_id]) && !empty($_REQUEST['free_choice'][$my_question_id])? $_REQUEST['free_choice'][$my_question_id]: null;
  127. }
  128. if ($type == 'all') {
  129. $total_weight += $objQuestionTmp->selectWeighting();
  130. }
  131. //this variable commes from exercise_submit_modal.php
  132. $hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$my_question_id];
  133. // Deleting old attempt
  134. if (isset($attempt_list) && !empty($attempt_list[$my_question_id])) {
  135. delete_attempt($exe_id, api_get_user_id() , api_get_course_id(), api_get_session_id(), $my_question_id);
  136. if ($objQuestionTmp->type == HOT_SPOT) {
  137. delete_attempt_hotspot($exe_id, api_get_user_id() , api_get_course_id(), $my_question_id);
  138. }
  139. $total_score -= $attempt_list[$my_question_id]['marks'];
  140. }
  141. // We're inside *one* question. Go through each possible answer for this question
  142. $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);
  143. $total_score += $result['score'];
  144. 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);
  145. // Destruction of the Question object
  146. unset($objQuestionTmp);
  147. }
  148. }
  149. if ($objExercise->type == ONE_PER_PAGE) {
  150. echo 'one_per_page';
  151. exit;
  152. }
  153. echo 'ok';
  154. break;
  155. default:
  156. echo '';
  157. }
  158. exit;