overview.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Exercise preview
  6. *
  7. * @package chamilo.exercise
  8. * @author Julio Montoya <gugli100@gmail.com>
  9. */
  10. $current_course_tool = TOOL_QUIZ;
  11. // Clear the exercise session just in case
  12. $exerciseFromSession = Session::read('objExercise');
  13. if (isset($exerciseFromSession)) {
  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. $learnpath_id = isset($_REQUEST['learnpath_id']) ? intval($_REQUEST['learnpath_id']) : null;
  27. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? intval($_REQUEST['learnpath_item_id']) : null;
  28. $learnpathItemViewId = isset($_REQUEST['learnpath_item_view_id']) ? intval($_REQUEST['learnpath_item_view_id']) : null;
  29. $origin = api_get_origin();
  30. $interbreadcrumb[] = array(
  31. "url" => "exercise.php?".api_get_cidreq(),
  32. "name" => get_lang('Exercises'),
  33. );
  34. $interbreadcrumb[] = array("url" => "#", "name" => $objExercise->name);
  35. $time_control = false;
  36. $clock_expired_time = ExerciseLib::get_session_time_control_key(
  37. $objExercise->id,
  38. $learnpath_id,
  39. $learnpath_item_id
  40. );
  41. if ($objExercise->expired_time != 0 && !empty($clock_expired_time)) {
  42. $time_control = true;
  43. }
  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('js/epiclock/stylesheet/jquery.epiclock.css');
  48. $htmlHeadXtra[] = api_get_css(
  49. 'js/epiclock/renderers/minute/epiclock.minute.css'
  50. );
  51. $htmlHeadXtra[] = api_get_js(
  52. 'js/epiclock/javascript/jquery.dateformat.min.js'
  53. );
  54. $htmlHeadXtra[] = api_get_js(
  55. 'js/epiclock/javascript/jquery.epiclock.min.js'
  56. );
  57. $htmlHeadXtra[] = api_get_js(
  58. 'js/epiclock/renderers/minute/epiclock.minute.js'
  59. );
  60. $htmlHeadXtra[] = $objExercise->show_time_control_js($time_left);
  61. }
  62. if ($origin != 'learnpath') {
  63. Display::display_header();
  64. } else {
  65. $htmlHeadXtra[] = "
  66. <style>
  67. body { background: none;}
  68. </style>
  69. ";
  70. Display::display_reduced_header();
  71. }
  72. $html = '';
  73. $message = '';
  74. $html.= '<div class="exercise">';
  75. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  76. $edit_link = '';
  77. if ($is_allowed_to_edit && $objExercise->sessionId == $sessionId) {
  78. $edit_link = Display::url(
  79. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  80. api_get_path(WEB_CODE_PATH).'exercise/admin.php?'.api_get_cidreq().'&id_session='.api_get_session_id().'&exerciseId='.$objExercise->id
  81. );
  82. }
  83. $iconExercise = Display::return_icon('test-quiz.png', null, array(), ICON_SIZE_MEDIUM);
  84. // Exercise name.
  85. $html .= Display::page_header($iconExercise.$objExercise->name.' '.$edit_link);
  86. //Exercise description
  87. if (!empty($objExercise->description)) {
  88. $html .= Display::div($objExercise->description, array('class'=>'exercise_description'));
  89. }
  90. $extra_params = '';
  91. if (isset($_GET['preview'])) {
  92. $extra_params = '&preview=1';
  93. }
  94. $exercise_stat_info = $objExercise->get_stat_track_exercise_info(
  95. $learnpath_id,
  96. $learnpath_item_id,
  97. 0
  98. );
  99. /*$attempt_list = null;
  100. if (isset($exercise_stat_info['exe_id'])) {
  101. $attempt_list = Event::getAllExerciseEventByExeId($exercise_stat_info['exe_id']);
  102. }*/
  103. //1. Check if this is a new attempt or a previous
  104. $label = get_lang('StartTest');
  105. if ($time_control && !empty($clock_expired_time) || isset($exercise_stat_info['exe_id'])) {
  106. $label = get_lang('ContinueTest');
  107. }
  108. if (isset($exercise_stat_info['exe_id'])) {
  109. $message = Display::return_message(get_lang('YouTriedToResolveThisExerciseEarlier'));
  110. }
  111. // 2. Exercise button
  112. // Notice we not add there the lp_item_view_id because is not already generated
  113. $exercise_url = api_get_path(WEB_CODE_PATH) . 'exercise/exercise_submit.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'&origin='.$origin.'&learnpath_id='.$learnpath_id.'&learnpath_item_id='.$learnpath_item_id.'&learnpath_item_view_id='.$learnpathItemViewId.$extra_params;
  114. $exercise_url_button = Display::url(
  115. $label,
  116. $exercise_url,
  117. array('class' => 'btn btn-success btn-large')
  118. );
  119. //3. Checking visibility of the exercise (overwrites the exercise button)
  120. $visible_return = $objExercise->is_visible(
  121. $learnpath_id,
  122. $learnpath_item_id,
  123. null,
  124. false
  125. );
  126. // Exercise is not visible remove the button
  127. if ($visible_return['value'] == false) {
  128. if ($is_allowed_to_edit) {
  129. $message = Display::return_message(get_lang('ThisItemIsInvisibleForStudentsButYouHaveAccessAsTeacher'), 'warning');
  130. } else {
  131. $message = $visible_return['message'];
  132. $exercise_url_button = null;
  133. }
  134. }
  135. $attempts = Event::getExerciseResultsByUser(
  136. api_get_user_id(),
  137. $objExercise->id,
  138. api_get_course_int_id(),
  139. api_get_session_id(),
  140. $learnpath_id,
  141. $learnpath_item_id,
  142. 'desc'
  143. );
  144. $counter = count($attempts);
  145. $my_attempt_array = array();
  146. $table_content = '';
  147. /* Make a special case for IE, which doesn't seem to be able to handle the
  148. * results popup -> send it to the full results page */
  149. $browser = new Browser();
  150. $current_browser = $browser->getBrowser();
  151. $url_suffix = '';
  152. $btn_class = ' ';
  153. if ($current_browser == 'Internet Explorer') {
  154. $url_suffix = '&show_headers=1';
  155. $btn_class = '';
  156. }
  157. $blockShowAnswers = false;
  158. if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  159. if (count($attempts) < $objExercise->attempts) {
  160. $blockShowAnswers = true;
  161. }
  162. }
  163. if (!empty($attempts)) {
  164. $i = $counter;
  165. foreach ($attempts as $attempt_result) {
  166. $score = ExerciseLib::show_score(
  167. $attempt_result['exe_result'],
  168. $attempt_result['exe_weighting']
  169. );
  170. $attempt_url = api_get_path(WEB_CODE_PATH) . 'exercise/result.php?';
  171. $attempt_url .= api_get_cidreq() . '&show_headers=1&';
  172. $attempt_url .= http_build_query([
  173. 'id' => $attempt_result['exe_id']
  174. ]);
  175. $attempt_url .= $url_suffix;
  176. $attempt_link = Display::url(
  177. get_lang('Show'),
  178. $attempt_url,
  179. [
  180. 'class' => $btn_class . 'btn btn-default',
  181. 'data-title' => get_lang('Show'),
  182. 'data-size' => 'lg'
  183. ]
  184. );
  185. $teacher_revised = Display::label(get_lang('Validated'), 'success');
  186. if ($attempt_result['attempt_revised'] == 0) {
  187. $teacher_revised = Display::label(get_lang('NotValidated'), 'info');
  188. }
  189. $row = array(
  190. 'count' => $i,
  191. 'date' => api_convert_and_format_date(
  192. $attempt_result['start_date'],
  193. DATE_TIME_FORMAT_LONG
  194. ),
  195. 'userIp' => $attempt_result['user_ip']
  196. );
  197. $attempt_link .= "&nbsp;&nbsp;&nbsp;" . $teacher_revised;
  198. if (in_array(
  199. $objExercise->results_disabled,
  200. array(
  201. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  202. RESULT_DISABLE_SHOW_SCORE_ONLY,
  203. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
  204. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT
  205. )
  206. )) {
  207. $row['result'] = $score;
  208. }
  209. if (in_array(
  210. $objExercise->results_disabled,
  211. array(
  212. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  213. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
  214. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT
  215. )
  216. )
  217. || (
  218. $objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY &&
  219. $objExercise->feedback_type == EXERCISE_FEEDBACK_TYPE_END)
  220. ) {
  221. if ($blockShowAnswers) {
  222. $attempt_link = '';
  223. }
  224. $row['attempt_link'] = $attempt_link;
  225. }
  226. $my_attempt_array[] = $row;
  227. $i--;
  228. }
  229. $header_names = [];
  230. $table = new HTML_Table(array('class' => 'table table-striped table-hover'));
  231. // Hiding score and answer
  232. switch ($objExercise->results_disabled) {
  233. case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
  234. if ($blockShowAnswers) {
  235. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score'));
  236. } else {
  237. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score'), get_lang('Details'));
  238. }
  239. break;
  240. case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS:
  241. case RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES:
  242. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score'), get_lang('Details'));
  243. break;
  244. case RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS:
  245. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'));
  246. break;
  247. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  248. if ($objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_END) {
  249. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score'));
  250. }
  251. else {
  252. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score'), get_lang('Details'));
  253. }
  254. break;
  255. }
  256. $column = 0;
  257. foreach ($header_names as $item) {
  258. $table->setHeaderContents(0, $column, $item);
  259. $column++;
  260. }
  261. $row = 1;
  262. if (!empty($my_attempt_array)) {
  263. foreach ($my_attempt_array as $data) {
  264. $column = 0;
  265. $table->setCellContents($row, $column, $data);
  266. $table->setRowAttributes($row, null, true);
  267. $column++;
  268. $row++;
  269. }
  270. }
  271. $table_content = $table->toHtml();
  272. }
  273. if ($objExercise->selectAttempts()) {
  274. $attempt_message = get_lang('Attempts').' '.$counter.' / '.$objExercise->selectAttempts();
  275. if ($counter == $objExercise->selectAttempts()) {
  276. $attempt_message = Display::return_message($attempt_message, 'error');
  277. } else {
  278. $attempt_message = Display::return_message($attempt_message, 'info');
  279. }
  280. if ($visible_return['value'] == true) {
  281. $message .= $attempt_message;
  282. }
  283. }
  284. if ($time_control) {
  285. $html.= $objExercise->return_time_left_div();
  286. }
  287. $html .= $message;
  288. if (!empty($exercise_url_button)) {
  289. $html .= Display::div(
  290. Display::div(
  291. $exercise_url_button,
  292. array('class' => 'exercise_overview_options col-md-12')
  293. ),
  294. array('class' => ' row')
  295. );
  296. }
  297. $html .= Display::tag(
  298. 'div',
  299. $table_content,
  300. ['class' => 'table-responsive']
  301. );
  302. $html.= '</div>';
  303. echo $html;
  304. Display::display_footer();