overview.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. use \ChamiloSession as Session;
  10. require_once '../inc/global.inc.php';
  11. $current_course_tool = TOOL_QUIZ;
  12. // Clear the exercise session just in case
  13. if (isset($_SESSION['objExercise'])) {
  14. Session::erase('objExercise');
  15. }
  16. $this_section = SECTION_COURSES;
  17. // Notice for unauthorized people.
  18. api_protect_course_script(true);
  19. $sessionId = api_get_session_id();
  20. $exercise_id = isset($_REQUEST['exerciseId']) ? intval($_REQUEST['exerciseId']) : 0;
  21. $objExercise = new Exercise();
  22. $result = $objExercise->read($exercise_id);
  23. if (!$result) {
  24. api_not_allowed(true);
  25. }
  26. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  27. $learnpath_id = isset($_REQUEST['learnpath_id']) ? intval($_REQUEST['learnpath_id']) : null;
  28. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? intval($_REQUEST['learnpath_item_id']) : null;
  29. $origin = isset($_REQUEST['origin']) ? Security::remove_XSS($_REQUEST['origin']) : null;
  30. $interbreadcrumb[] = array("url" => "exercise.php?gradebook=$gradebook", "name" => get_lang('Exercises'));
  31. $interbreadcrumb[] = array("url" => "#", "name" => $objExercise->name);
  32. $time_control = false;
  33. $clock_expired_time = ExerciseLib::get_session_time_control_key($objExercise->id, $learnpath_id, $learnpath_item_id);
  34. if ($objExercise->expired_time != 0 && !empty($clock_expired_time)) {
  35. $time_control = true;
  36. }
  37. if ($time_control) {
  38. // Get time left for expiring time
  39. $time_left = api_strtotime($clock_expired_time,'UTC') - time();
  40. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/stylesheet/jquery.epiclock.css');
  41. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/renderers/minute/epiclock.minute.css');
  42. $htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.dateformat.min.js');
  43. $htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.epiclock.min.js');
  44. $htmlHeadXtra[] = api_get_js('epiclock/renderers/minute/epiclock.minute.js');
  45. $htmlHeadXtra[] = $objExercise->show_time_control_js($time_left);
  46. }
  47. if ($origin != 'learnpath') {
  48. Display::display_header();
  49. } else {
  50. $htmlHeadXtra[] = "
  51. <style>
  52. body { background: none;}
  53. </style>
  54. ";
  55. Display::display_reduced_header();
  56. }
  57. $html = '';
  58. $message = '';
  59. $html.= '<div class="exercise">';
  60. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  61. $edit_link = '';
  62. if ($is_allowed_to_edit && $objExercise->sessionId == $sessionId) {
  63. $edit_link = Display::url(
  64. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  65. api_get_path(WEB_CODE_PATH).'exercice/admin.php?'.api_get_cidreq().'&id_session='.api_get_session_id().'&exerciseId='.$objExercise->id
  66. );
  67. }
  68. $iconExercise = Display::return_icon('test-quiz.png', null, array(), ICON_SIZE_MEDIUM);
  69. // Exercise name.
  70. $html .= Display::page_header($iconExercise.$objExercise->name.' '.$edit_link);
  71. //Exercise description
  72. if (!empty($objExercise->description)) {
  73. $html .= Display::div($objExercise->description, array('class'=>'exercise_description'));
  74. }
  75. $extra_params = '';
  76. if (isset($_GET['preview'])) {
  77. $extra_params = '&preview=1';
  78. }
  79. $exercise_stat_info = $objExercise->get_stat_track_exercise_info(
  80. $learnpath_id,
  81. $learnpath_item_id,
  82. 0
  83. );
  84. $attempt_list = null;
  85. if (isset($exercise_stat_info['exe_id'])) {
  86. $attempt_list = Event::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(
  100. $label,
  101. $exercise_url,
  102. array('class' => 'btn btn-success btn-large')
  103. );
  104. //3. Checking visibility of the exercise (overwrites the exercise button)
  105. $visible_return = $objExercise->is_visible(
  106. $learnpath_id,
  107. $learnpath_item_id,
  108. null,
  109. false
  110. );
  111. // Exercise is not visible remove the button
  112. if ($visible_return['value'] == false) {
  113. if ($is_allowed_to_edit) {
  114. $message = Display::return_message(get_lang('ThisItemIsInvisibleForStudentsButYouHaveAccessAsTeacher'), 'warning');
  115. } else {
  116. $message = $visible_return['message'];
  117. $exercise_url_button = null;
  118. }
  119. }
  120. $attempts = Event::getExerciseResultsByUser(
  121. api_get_user_id(),
  122. $objExercise->id,
  123. api_get_course_int_id(),
  124. api_get_session_id(),
  125. $learnpath_id,
  126. $learnpath_item_id,
  127. 'desc'
  128. );
  129. $counter = count($attempts);
  130. $my_attempt_array = array();
  131. $table_content = '';
  132. /* Make a special case for IE, which doesn't seem to be able to handle the
  133. * results popup -> send it to the full results page */
  134. $browser = new Browser();
  135. $current_browser = $browser->getBrowser();
  136. $url_suffix = '';
  137. $btn_class = ' ';
  138. if ($current_browser == 'Internet Explorer') {
  139. $url_suffix = '&show_headers=1';
  140. $btn_class = '';
  141. }
  142. if (!empty($attempts)) {
  143. $i = $counter;
  144. foreach ($attempts as $attempt_result) {
  145. $score = ExerciseLib::show_score(
  146. $attempt_result['exe_result'],
  147. $attempt_result['exe_weighting']
  148. );
  149. $attempt_url = api_get_path(WEB_CODE_PATH) . 'exercice/result.php?';
  150. $attempt_url .= api_get_cidreq() . '&show_headers=1&';
  151. $attempt_url .= http_build_query([
  152. 'id' => $attempt_result['exe_id']
  153. ]);
  154. $attempt_url .= $url_suffix;
  155. $attempt_link = Display::url(
  156. get_lang('Show'),
  157. $attempt_url,
  158. [
  159. 'class' => $btn_class . 'btn btn-default',
  160. 'data-title' => get_lang('Show'),
  161. 'data-size' => 'lg'
  162. ]
  163. );
  164. $teacher_revised = Display::label(get_lang('Validated'), 'success');
  165. if ($attempt_result['attempt_revised'] == 0) {
  166. $teacher_revised = Display::label(get_lang('NotValidated'), 'info');
  167. }
  168. $row = array(
  169. 'count' => $i,
  170. 'date' => api_convert_and_format_date(
  171. $attempt_result['start_date'],
  172. DATE_TIME_FORMAT_LONG
  173. ),
  174. 'userIp' => $attempt_result['user_ip']
  175. );
  176. $attempt_link .= "&nbsp;&nbsp;&nbsp;" . $teacher_revised;
  177. if (in_array(
  178. $objExercise->results_disabled,
  179. array(
  180. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  181. RESULT_DISABLE_SHOW_SCORE_ONLY,
  182. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES
  183. )
  184. )) {
  185. $row['result'] = $score;
  186. }
  187. if (in_array(
  188. $objExercise->results_disabled,
  189. array(
  190. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  191. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES
  192. )
  193. )
  194. || (
  195. $objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY &&
  196. $objExercise->feedback_type == EXERCISE_FEEDBACK_TYPE_END)
  197. ) {
  198. $row['attempt_link'] = $attempt_link;
  199. }
  200. $my_attempt_array[] = $row;
  201. $i--;
  202. }
  203. $table = new HTML_Table(array('class' => 'table table-striped table-hover'));
  204. //Hiding score and answer
  205. switch ($objExercise->results_disabled) {
  206. case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS:
  207. case RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES:
  208. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score'), get_lang('Details'));
  209. break;
  210. case RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS:
  211. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'));
  212. break;
  213. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  214. if ($objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_END) {
  215. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score'));
  216. }
  217. else {
  218. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score'), get_lang('Details'));
  219. }
  220. break;
  221. }
  222. $column = 0;
  223. foreach ($header_names as $item) {
  224. $table->setHeaderContents(0, $column, $item);
  225. $column++;
  226. }
  227. $row = 1;
  228. if (!empty($my_attempt_array)) {
  229. foreach ($my_attempt_array as $data) {
  230. $column = 0;
  231. $table->setCellContents($row, $column, $data);
  232. $table->setRowAttributes($row, null, true);
  233. $column++;
  234. $row++;
  235. }
  236. }
  237. $table_content = $table->toHtml();
  238. }
  239. if ($objExercise->selectAttempts()) {
  240. $attempt_message = get_lang('Attempts').' '.$counter.' / '.$objExercise->selectAttempts();
  241. if ($counter == $objExercise->selectAttempts()) {
  242. $attempt_message = Display::return_message($attempt_message, 'error');
  243. } else {
  244. $attempt_message = Display::return_message($attempt_message, 'info');
  245. }
  246. if ($visible_return['value'] == true) {
  247. $message .= $attempt_message;
  248. }
  249. }
  250. if ($time_control) {
  251. $html.= $objExercise->return_time_left_div();
  252. }
  253. $html .= $message;
  254. if (!empty($exercise_url_button)) {
  255. $html .= Display::div(
  256. Display::div(
  257. $exercise_url_button,
  258. array('class' => 'exercise_overview_options col-md-12')
  259. ),
  260. array('class' => ' row')
  261. );
  262. }
  263. $html .= Display::tag(
  264. 'div',
  265. $table_content,
  266. ['class' => 'table-responsive']
  267. );
  268. $html.= '</div>';
  269. echo $html;
  270. Display::display_footer();