overview.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Exercise preview
  5. *
  6. * @package chamilo.exercise
  7. * @author Julio Montoya <gugli100@gmail.com>
  8. */
  9. /**
  10. * Code
  11. */
  12. use \ChamiloSession as Session;
  13. $language_file = 'exercice';
  14. require_once 'exercise.class.php';
  15. $current_course_tool = TOOL_QUIZ;
  16. // Clear the exercise session just in case
  17. $objExercise = Session::read('objExercise');
  18. if (isset($objExercise)) {
  19. Session::erase('objExercise');
  20. }
  21. $this_section = SECTION_COURSES;
  22. // Notice for unauthorized people.
  23. api_protect_course_script(true);
  24. $exercise_id = isset($_REQUEST['exerciseId']) ? intval($_REQUEST['exerciseId']) : 0;
  25. $objExercise = new Exercise();
  26. $result = $objExercise->read($exercise_id);
  27. if (!$result) {
  28. api_not_allowed(true);
  29. }
  30. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  31. $learnpath_id = isset($_REQUEST['learnpath_id']) ? intval($_REQUEST['learnpath_id']) : null;
  32. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? intval($_REQUEST['learnpath_item_id']) : null;
  33. $learnpath_item_view_id = isset($_REQUEST['learnpath_item_view_id']) ? intval($_REQUEST['learnpath_item_view_id']) : null;
  34. $origin = isset($_REQUEST['origin']) ? Security::remove_XSS($_REQUEST['origin']) : null;
  35. $interbreadcrumb[] = array ("url" => "exercice.php?gradebook=$gradebook", "name" => get_lang('Exercices'));
  36. $interbreadcrumb[] = array ("url" => "#", "name" => $objExercise->getCutTitle());
  37. $time_control = false;
  38. $clock_expired_time = ExerciseLib::get_session_time_control_key($objExercise->id, $learnpath_id, $learnpath_item_id);
  39. if ($objExercise->expired_time != 0 && !empty($clock_expired_time)) {
  40. $time_control = true;
  41. }
  42. $urlMainExercise = api_get_path(WEB_CODE_PATH).'exercice/';
  43. $exercise_stat_info = $objExercise->getStatTrackExerciseInfo($learnpath_id, $learnpath_item_id, 0);
  44. if ($time_control) {
  45. // Get time left for expiring time
  46. $time_left = api_strtotime($clock_expired_time, 'UTC') - time();
  47. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/stylesheet/jquery.epiclock.css');
  48. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/renderers/minute/epiclock.minute.css');
  49. $htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.dateformat.min.js');
  50. $htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.epiclock.min.js');
  51. $htmlHeadXtra[] = api_get_js('epiclock/renderers/minute/epiclock.minute.js');
  52. $htmlHeadXtra[] = $objExercise->show_time_control_js($time_left);
  53. $htmlHeadXtra[] = '<script>
  54. function final_submit() {
  55. var lp_data = $.param({"learnpath_id": '.intval($learnpath_id).', "learnpath_item_id" : '.intval($learnpath_item_id).', "learnpath_item_view_id": '.intval($learnpath_item_view_id).'});
  56. var url = "'.$urlMainExercise.'exercise_result.php?'.api_get_cidreq().'&origin='.$origin.'&exe_id='.$exercise_stat_info['exe_id'].'&";
  57. window.location = url ;
  58. }
  59. </script>';
  60. }
  61. if ($origin != 'learnpath') {
  62. Display::display_header();
  63. } else {
  64. Display::display_reduced_header();
  65. }
  66. $html = '';
  67. $message = '';
  68. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  69. $edit_link = '';
  70. if ($is_allowed_to_edit) {
  71. $url = api_get_path(WEB_CODE_PATH).'exercice/admin.php?'.api_get_cidreq().'&id_session='.api_get_session_id().'&exerciseId='.$objExercise->id;
  72. $edit_link = Display::url(Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL), $url);
  73. }
  74. //Exercise name
  75. $html .= Display::page_subheader($objExercise->name.' '.$edit_link);
  76. //Exercise description
  77. if (!empty($objExercise->description)) {
  78. $html .= Display::div($objExercise->description, array('class'=>'exercise_description'));
  79. }
  80. $extra_params = '';
  81. if (isset($_GET['preview'])) {
  82. $extra_params = '&preview=1';
  83. }
  84. $attempt_list = null;
  85. if (isset($exercise_stat_info['exe_id'])) {
  86. $attempt_list = getAllExerciseEventByExeId($exercise_stat_info['exe_id']);
  87. }
  88. //1. Check if this is a new attempt or a previous
  89. $label = get_lang('StartTest');
  90. if ($time_control && !empty($clock_expired_time) || !empty($attempt_list)) {
  91. $label = get_lang('ContinueTest');
  92. }
  93. if (!empty($attempt_list)) {
  94. $message = Display::return_message(get_lang('YouTriedToResolveThisExerciseEarlier'));
  95. }
  96. //2. Exercise button
  97. //Notice we not add there the lp_item_view_id because is not already generated
  98. $exercise_url = api_get_path(WEB_CODE_PATH).'exercice/exercise_submit.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'&origin='.$origin.'&learnpath_id='.$learnpath_id.'&learnpath_item_id='.$learnpath_item_id.$extra_params;
  99. $exercise_url_button = Display::url($label, $exercise_url, array('class'=>'btn btn-primary btn-large'));
  100. //3. Checking visibility of the exercise (overwrites the exercise button)
  101. $visible_return = $objExercise->is_visible($learnpath_id, $learnpath_item_id, null, false);
  102. //Exercise is not visible remove the button
  103. if ($visible_return['value'] == false) {
  104. if ($is_allowed_to_edit) {
  105. $message = Display::return_message(get_lang('ThisItemIsInvisibleForStudentsButYouHaveAccessAsTeacher'), 'warning');
  106. } else {
  107. $message = $visible_return['message'];
  108. $exercise_url_button = null;
  109. }
  110. }
  111. $attempts = getExerciseResultsByUser(
  112. api_get_user_id(),
  113. $objExercise->id,
  114. api_get_course_int_id(),
  115. api_get_session_id(),
  116. $learnpath_id,
  117. $learnpath_item_id,
  118. 'desc'
  119. );
  120. $counter = count($attempts);
  121. $my_attempt_array = array();
  122. $table_content = '';
  123. /* Make a special case for IE, which doesn't seem to be able to handle the
  124. * results popup -> send it to the full results page */
  125. $browser = new Browser();
  126. $current_browser = $browser->getBrowser();
  127. $url_suffix = '';
  128. $btn_class = 'ajax ';
  129. if ($current_browser == 'Internet Explorer') {
  130. $url_suffix = '&amp;show_headers=1';
  131. $btn_class = '';
  132. }
  133. if (!empty($attempts)) {
  134. $i = $counter;
  135. foreach ($attempts as $attempt_result) {
  136. $score = ExerciseLib::show_score($attempt_result['exe_result'], $attempt_result['exe_weighting']);
  137. $attempt_url = api_get_path(WEB_CODE_PATH).'exercice/result.php?'.api_get_cidreq().'&amp;id='.$attempt_result['exe_id'].'&amp;id_session='.api_get_session_id().'&amp;height=500&amp;width=950'.$url_suffix;
  138. $attempt_link = Display::url(get_lang('Show'), $attempt_url, array('class'=>$btn_class.'btn'));
  139. $teacher_revised = Display::label(get_lang('Validated'), 'success');
  140. if ($attempt_result['attempt_revised'] == 0) {
  141. $teacher_revised = Display::label(get_lang('NotValidated'), 'info');
  142. }
  143. $row = array(
  144. 'count' => $i,
  145. 'date' => api_convert_and_format_date($attempt_result['start_date'], DATE_TIME_FORMAT_LONG)
  146. );
  147. $attempt_link .= "&nbsp;&nbsp;&nbsp;".$teacher_revised;
  148. if (in_array(
  149. $objExercise->results_disabled,
  150. array(
  151. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  152. RESULT_DISABLE_SHOW_SCORE_ONLY,
  153. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES
  154. )
  155. )) {
  156. $row['result'] = $score;
  157. }
  158. if (in_array(
  159. $objExercise->results_disabled,
  160. array(
  161. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  162. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES
  163. )
  164. )
  165. || ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY && $objExercise->feedback_type == EXERCISE_FEEDBACK_TYPE_END)) {
  166. $row['attempt_link'] = $attempt_link;
  167. }
  168. $my_attempt_array[] = $row;
  169. $i--;
  170. }
  171. $table = new HTML_Table(array('class' => 'data_table'));
  172. //Hiding score and answer
  173. switch ($objExercise->results_disabled) {
  174. case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS:
  175. case RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES:
  176. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('Score'), get_lang('Details'));
  177. break;
  178. case RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS:
  179. $header_names = array(get_lang('Attempt'), get_lang('StartDate'));
  180. break;
  181. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  182. if ($objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_END) {
  183. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('Score'));
  184. } else {
  185. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('Score'), get_lang('Details'));
  186. }
  187. break;
  188. }
  189. $column = 0;
  190. foreach ($header_names as $item) {
  191. $table->setHeaderContents(0, $column, $item);
  192. $column++;
  193. }
  194. $row = 1;
  195. if (!empty($my_attempt_array)) {
  196. foreach ($my_attempt_array as $data) {
  197. $column = 0;
  198. $table->setCellContents($row, $column, $data);
  199. $class = 'class="row_odd"';
  200. if ($row % 2) {
  201. $class = 'class="row_even"';
  202. }
  203. $table->setRowAttributes($row, $class, true);
  204. $column++;
  205. $row++;
  206. }
  207. }
  208. $table_content = $table->toHtml();
  209. }
  210. if ($objExercise->selectAttempts()) {
  211. $attempt_message = get_lang('Attempts').' '.$counter.' / '.$objExercise->selectAttempts();
  212. if ($counter == $objExercise->selectAttempts()) {
  213. $attempt_message = Display::return_message($attempt_message, 'error');
  214. } else {
  215. $attempt_message = Display::return_message($attempt_message, 'info');
  216. }
  217. if ($visible_return['value'] == true) {
  218. $message .= $attempt_message;
  219. }
  220. }
  221. if ($time_control) {
  222. $html.= $objExercise->returnTimeLeftDiv();
  223. }
  224. $html .= $message;
  225. if (!empty($exercise_url_button)) {
  226. $html .= Display::div(Display::div($exercise_url_button, array('class'=>'exercise_overview_options span12')), array('class'=>' row'));
  227. }
  228. $html .= $table_content;
  229. echo $html;
  230. if ($origin != 'learnpath') {
  231. Display::display_footer();
  232. }