overview.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Exercise preview.
  6. *
  7. * @package chamilo.exercise
  8. *
  9. * @author Julio Montoya <gugli100@gmail.com>
  10. */
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. $current_course_tool = TOOL_QUIZ;
  13. // Clear the exercise session just in case
  14. Session::erase('objExercise');
  15. Session::erase('calculatedAnswerId');
  16. Session::erase('duration_time_previous');
  17. Session::erase('duration_time');
  18. $this_section = SECTION_COURSES;
  19. // Notice for unauthorized people.
  20. api_protect_course_script(true);
  21. $sessionId = api_get_session_id();
  22. $exercise_id = isset($_REQUEST['exerciseId']) ? intval($_REQUEST['exerciseId']) : 0;
  23. $objExercise = new Exercise();
  24. $result = $objExercise->read($exercise_id);
  25. if (!$result) {
  26. api_not_allowed(true);
  27. }
  28. $learnpath_id = isset($_REQUEST['learnpath_id']) ? intval($_REQUEST['learnpath_id']) : null;
  29. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? intval($_REQUEST['learnpath_item_id']) : null;
  30. $learnpathItemViewId = isset($_REQUEST['learnpath_item_view_id']) ? intval($_REQUEST['learnpath_item_view_id']) : null;
  31. $origin = api_get_origin();
  32. $interbreadcrumb[] = [
  33. "url" => "exercise.php?".api_get_cidreq(),
  34. "name" => get_lang('Exercises'),
  35. ];
  36. $interbreadcrumb[] = ["url" => "#", "name" => $objExercise->selectTitle(true)];
  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. if ($time_control) {
  43. // Get time left for expiring time
  44. $time_left = api_strtotime($clock_expired_time, 'UTC') - time();
  45. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/stylesheet/jquery.epiclock.css');
  46. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/renderers/minute/epiclock.minute.css');
  47. $htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.dateformat.min.js');
  48. $htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.epiclock.min.js');
  49. $htmlHeadXtra[] = api_get_js('epiclock/renderers/minute/epiclock.minute.js');
  50. $htmlHeadXtra[] = $objExercise->showTimeControlJS($time_left);
  51. }
  52. if ($origin != 'learnpath') {
  53. $fluid = false;
  54. //Display::display_header();
  55. } else {
  56. $htmlHeadXtra[] = "
  57. <style>
  58. body { background: none;}
  59. </style>
  60. ";
  61. $fluid = true;
  62. //Display::display_reduced_header();
  63. }
  64. $tpl = new Template('Overview');
  65. $list = [];
  66. $html = '';
  67. $message = '';
  68. $isAllowedToEdit = api_is_allowed_to_edit(null, true);
  69. $list['id'] = $objExercise->id;
  70. $list['session_id'] = $objExercise->sessionId;
  71. $list['edit'] = $isAllowedToEdit;
  72. // Exercise name.
  73. if (api_get_configuration_value('save_titles_as_html')) {
  74. $list['title'] = $objExercise->get_formated_title().PHP_EOL;
  75. } else {
  76. $list['title'] = $objExercise->selectTitle().PHP_EOL;
  77. }
  78. //Exercise description
  79. if (!empty($objExercise->description)) {
  80. $list['description'] = $objExercise->description;
  81. }
  82. $extra_params = '';
  83. if (isset($_GET['preview'])) {
  84. $extra_params = '&preview=1';
  85. }
  86. $exerciseStatus = $objExercise->get_stat_track_exercise_info(
  87. $learnpath_id,
  88. $learnpath_item_id,
  89. 0
  90. );
  91. //1. Check if this is a new attempt or a previous
  92. $label = get_lang('StartTest');
  93. if ($time_control && !empty($clock_expired_time) || isset($exerciseStatus['exe_id'])) {
  94. $label = get_lang('ContinueTest');
  95. }
  96. $msgStatus = null;
  97. if (isset($exerciseStatus['exe_id'])) {
  98. $msgStatus = get_lang('YouTriedToResolveThisExerciseEarlier');
  99. }
  100. // 2. Exercise button
  101. // Notice we not add there the lp_item_view_id because is not already generated
  102. $list['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;
  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 ($isAllowedToEdit) {
  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 = [];
  130. $table_content = null;
  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. $tableContent = null;
  148. if (!empty($attempts)) {
  149. $i = $counter;
  150. foreach ($attempts as $attempt_result) {
  151. $score = ExerciseLib::show_score(
  152. $attempt_result['exe_result'],
  153. $attempt_result['exe_weighting']
  154. );
  155. $attempt_url = api_get_path(WEB_CODE_PATH).'exercise/result.php?';
  156. $attempt_url .= api_get_cidreq().'&show_headers=1&';
  157. $attempt_url .= http_build_query([
  158. 'id' => $attempt_result['exe_id'],
  159. ]);
  160. $attempt_url .= $url_suffix;
  161. $attempt_link = '<div class="row">';
  162. $attempt_link .= '<div class="col-md-6">';
  163. $attempt_link .= Display::url(
  164. get_lang('Show'),
  165. $attempt_url,
  166. [
  167. 'class' => $btn_class.'btn btn-default btn-sm btn-block',
  168. 'data-title' => get_lang('Show'),
  169. 'data-size' => 'lg',
  170. ]
  171. );
  172. $attempt_link .= '</div>';
  173. $attempt_link .= '<div class="col-md-6">';
  174. $teacher_revised = Display::label(get_lang('Validated'), 'success');
  175. if ($attempt_result['attempt_revised'] == 0) {
  176. $teacher_revised = Display::label(get_lang('NotValidated'), 'info');
  177. }
  178. $row = [
  179. 'count' => $i,
  180. 'date' => api_convert_and_format_date(
  181. $attempt_result['start_date'],
  182. DATE_TIME_FORMAT_LONG
  183. ),
  184. 'userIp' => $attempt_result['user_ip'],
  185. ];
  186. $attempt_link .= $teacher_revised;
  187. $attempt_link .= '</div>';
  188. $attempt_link .= '</div>';
  189. if (in_array(
  190. $objExercise->results_disabled,
  191. [
  192. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  193. RESULT_DISABLE_SHOW_SCORE_ONLY,
  194. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
  195. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
  196. ]
  197. )) {
  198. $row['result'] = $score;
  199. }
  200. if (in_array(
  201. $objExercise->results_disabled,
  202. [
  203. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  204. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
  205. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
  206. ]
  207. ) || (
  208. $objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY &&
  209. $objExercise->feedback_type == EXERCISE_FEEDBACK_TYPE_END)
  210. ) {
  211. if ($blockShowAnswers) {
  212. $attempt_link = '';
  213. }
  214. $row['attempt_link'] = $attempt_link;
  215. }
  216. $my_attempt_array[] = $row;
  217. $i--;
  218. }
  219. $header_names = [];
  220. $table = new HTML_Table(['class' => 'table table-striped table-hover']);
  221. // Hiding score and answer
  222. switch ($objExercise->results_disabled) {
  223. case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
  224. if ($blockShowAnswers) {
  225. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score')];
  226. } else {
  227. $header_names = [
  228. get_lang('Attempt'),
  229. get_lang('StartDate'),
  230. get_lang('IP'),
  231. get_lang('Score'),
  232. get_lang('Details'),
  233. ];
  234. }
  235. break;
  236. case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS:
  237. case RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES:
  238. $header_names = [
  239. get_lang('Attempt'),
  240. get_lang('StartDate'),
  241. get_lang('IP'),
  242. get_lang('Score'),
  243. get_lang('Details'),
  244. ];
  245. break;
  246. case RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS:
  247. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP')];
  248. break;
  249. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  250. if ($objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_END) {
  251. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score')];
  252. } else {
  253. $header_names = [
  254. get_lang('Attempt'),
  255. get_lang('StartDate'),
  256. get_lang('IP'),
  257. get_lang('Score'),
  258. get_lang('Details'),
  259. ];
  260. }
  261. break;
  262. }
  263. $column = 0;
  264. foreach ($header_names as $item) {
  265. $table->setHeaderContents(0, $column, $item);
  266. $column++;
  267. }
  268. $row = 1;
  269. if (!empty($my_attempt_array)) {
  270. foreach ($my_attempt_array as $data) {
  271. $column = 0;
  272. $table->setCellContents($row, $column, $data);
  273. $table->setRowAttributes($row, null, true);
  274. $column++;
  275. $row++;
  276. }
  277. }
  278. $tableContent = $table->toHtml();
  279. }
  280. if ($objExercise->selectAttempts() && $visible_return['value'] == true) {
  281. $list['attempts'] = $objExercise->selectAttempts();
  282. }
  283. if ($time_control) {
  284. $tpl->assign('time_control', $objExercise->return_time_left_div());
  285. }
  286. $tpl->assign('fluid', $fluid);
  287. $tpl->assign('data', $list);
  288. $tpl->assign('label', $label);
  289. $tpl->assign('message', $message);
  290. $tpl->assign('count', $counter);
  291. $tpl->assign('status', $msgStatus);
  292. $tpl->assign('table_result', $tableContent);
  293. $layout = $tpl->get_template('exercise/overview.tpl');
  294. $tpl->assign('content', $tpl->fetch($layout));
  295. $tpl->display_one_col_template();