overview.php 11 KB

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