overview.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. $js = '<script>'.api_get_language_translate_html().'</script>';
  20. $htmlHeadXtra[] = $js;
  21. // Notice for unauthorized people.
  22. api_protect_course_script(true);
  23. $sessionId = api_get_session_id();
  24. $courseCode = api_get_course_id();
  25. $exercise_id = isset($_REQUEST['exerciseId']) ? intval($_REQUEST['exerciseId']) : 0;
  26. $objExercise = new Exercise();
  27. $result = $objExercise->read($exercise_id, true);
  28. if (!$result) {
  29. api_not_allowed(true);
  30. }
  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. $learnpathItemViewId = isset($_REQUEST['learnpath_item_view_id']) ? intval($_REQUEST['learnpath_item_view_id']) : null;
  34. $origin = api_get_origin();
  35. $logInfo = [
  36. 'tool' => TOOL_QUIZ,
  37. 'tool_id' => $exercise_id,
  38. 'tool_id_detail' => 0,
  39. 'action' => isset($_REQUEST['learnpath_id']) ? 'learnpath_id' : '',
  40. 'action_details' => isset($_REQUEST['learnpath_id']) ? (int) $_REQUEST['learnpath_id'] : '',
  41. ];
  42. Event::registerLog($logInfo);
  43. $interbreadcrumb[] = [
  44. 'url' => 'exercise.php?'.api_get_cidreq(),
  45. 'name' => get_lang('Exercises'),
  46. ];
  47. $interbreadcrumb[] = ['url' => '#', 'name' => $objExercise->selectTitle(true)];
  48. $time_control = false;
  49. $clock_expired_time = ExerciseLib::get_session_time_control_key($objExercise->id, $learnpath_id, $learnpath_item_id);
  50. if ($objExercise->expired_time != 0 && !empty($clock_expired_time)) {
  51. $time_control = true;
  52. }
  53. if ($time_control) {
  54. // Get time left for expiring time
  55. $time_left = api_strtotime($clock_expired_time, 'UTC') - time();
  56. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/stylesheet/jquery.epiclock.css');
  57. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/renderers/minute/epiclock.minute.css');
  58. $htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.dateformat.min.js');
  59. $htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.epiclock.min.js');
  60. $htmlHeadXtra[] = api_get_js('epiclock/renderers/minute/epiclock.minute.js');
  61. $htmlHeadXtra[] = $objExercise->showTimeControlJS($time_left);
  62. }
  63. if (!in_array($origin, ['learnpath', 'embeddable'])) {
  64. Display::display_header();
  65. } else {
  66. $htmlHeadXtra[] = "
  67. <style>
  68. body { background: none;}
  69. </style>
  70. ";
  71. Display::display_reduced_header();
  72. }
  73. $html = '';
  74. $message = '';
  75. $html .= '<div class="exercise-overview">';
  76. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  77. $editLink = '';
  78. if ($is_allowed_to_edit) {
  79. if ($objExercise->sessionId == $sessionId) {
  80. $editLink = Display::url(
  81. Display::return_icon('edit.png', get_lang('Edit'), [], ICON_SIZE_SMALL),
  82. api_get_path(WEB_CODE_PATH).'exercise/admin.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id
  83. );
  84. }
  85. $editLink .= Display::url(
  86. Display::return_icon('test_results.png', get_lang('Results'), [], ICON_SIZE_SMALL),
  87. api_get_path(WEB_CODE_PATH).'exercise/exercise_report.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id,
  88. ['title' => get_lang('Results')]
  89. );
  90. }
  91. $iconExercise = Display::return_icon('test-quiz.png', null, [], ICON_SIZE_MEDIUM);
  92. // Exercise name.
  93. if (api_get_configuration_value('save_titles_as_html')) {
  94. $html .= Display::div(
  95. $objExercise->get_formated_title().PHP_EOL.$editLink
  96. );
  97. } else {
  98. $html .= Display::page_header(
  99. $iconExercise.PHP_EOL.$objExercise->selectTitle().PHP_EOL.$editLink
  100. );
  101. }
  102. // Exercise description
  103. if (!empty($objExercise->description)) {
  104. $html .= Display::div($objExercise->description, ['class' => 'exercise_description']);
  105. }
  106. $extra_params = '';
  107. if (isset($_GET['preview'])) {
  108. $extra_params = '&preview=1';
  109. }
  110. $exercise_stat_info = $objExercise->get_stat_track_exercise_info(
  111. $learnpath_id,
  112. $learnpath_item_id,
  113. 0
  114. );
  115. //1. Check if this is a new attempt or a previous
  116. $label = get_lang('StartTest');
  117. if ($time_control && !empty($clock_expired_time) || isset($exercise_stat_info['exe_id'])) {
  118. $label = get_lang('ContinueTest');
  119. }
  120. if (isset($exercise_stat_info['exe_id'])) {
  121. $message = Display::return_message(get_lang('YouTriedToResolveThisExerciseEarlier'));
  122. }
  123. // 2. Exercise button
  124. // Notice we not add there the lp_item_view_id because is not already generated
  125. $exercise_url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'&learnpath_id='.$learnpath_id.'&learnpath_item_id='.$learnpath_item_id.'&learnpath_item_view_id='.$learnpathItemViewId.$extra_params;
  126. $exercise_url_button = Display::url(
  127. $label,
  128. $exercise_url,
  129. ['class' => 'btn btn-success btn-large']
  130. );
  131. //3. Checking visibility of the exercise (overwrites the exercise button)
  132. $visible_return = $objExercise->is_visible(
  133. $learnpath_id,
  134. $learnpath_item_id,
  135. null,
  136. true
  137. );
  138. // Exercise is not visible remove the button
  139. if ($visible_return['value'] == false) {
  140. if ($is_allowed_to_edit) {
  141. $message = Display::return_message(get_lang('ThisItemIsInvisibleForStudentsButYouHaveAccessAsTeacher'), 'warning');
  142. } else {
  143. $message = $visible_return['message'];
  144. $exercise_url_button = null;
  145. }
  146. }
  147. $attempts = Event::getExerciseResultsByUser(
  148. api_get_user_id(),
  149. $objExercise->id,
  150. api_get_course_int_id(),
  151. api_get_session_id(),
  152. $learnpath_id,
  153. $learnpath_item_id,
  154. 'desc'
  155. );
  156. $counter = count($attempts);
  157. $my_attempt_array = [];
  158. $table_content = '';
  159. /* Make a special case for IE, which doesn't seem to be able to handle the
  160. * results popup -> send it to the full results page */
  161. $browser = new Browser();
  162. $current_browser = $browser->getBrowser();
  163. $url_suffix = '';
  164. $btn_class = ' ';
  165. if ($current_browser == 'Internet Explorer') {
  166. $url_suffix = '&show_headers=1';
  167. $btn_class = '';
  168. }
  169. $blockShowAnswers = false;
  170. if (in_array(
  171. $objExercise->results_disabled,
  172. [
  173. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
  174. RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
  175. ])
  176. ) {
  177. if (count($attempts) < $objExercise->attempts) {
  178. $blockShowAnswers = true;
  179. }
  180. }
  181. $certificateBlock = '';
  182. if (!empty($attempts)) {
  183. $i = $counter;
  184. foreach ($attempts as $attempt_result) {
  185. if (empty($certificateBlock)) {
  186. $certificateBlock = ExerciseLib::generateAndShowCertificateBlock(
  187. $attempt_result['exe_result'],
  188. $attempt_result['exe_weighting'],
  189. $objExercise,
  190. $attempt_result['exe_user_id'],
  191. $courseCode,
  192. $sessionId
  193. );
  194. }
  195. $score = ExerciseLib::show_score($attempt_result['exe_result'], $attempt_result['exe_weighting']);
  196. $attempt_url = api_get_path(WEB_CODE_PATH).'exercise/result.php?';
  197. $attempt_url .= api_get_cidreq().'&show_headers=1&';
  198. $attempt_url .= http_build_query(['id' => $attempt_result['exe_id']]);
  199. $attempt_url .= $url_suffix;
  200. $attempt_link = Display::url(
  201. get_lang('Show'),
  202. $attempt_url,
  203. [
  204. 'class' => $btn_class.'btn btn-default',
  205. 'data-title' => get_lang('Show'),
  206. 'data-size' => 'lg',
  207. ]
  208. );
  209. $teacher_revised = Display::label(get_lang('Validated'), 'success');
  210. if ($attempt_result['attempt_revised'] == 0) {
  211. $teacher_revised = Display::label(get_lang('NotValidated'), 'info');
  212. }
  213. $row = [
  214. 'count' => $i,
  215. 'date' => api_convert_and_format_date(
  216. $attempt_result['start_date'],
  217. DATE_TIME_FORMAT_LONG
  218. ),
  219. 'userIp' => $attempt_result['user_ip'],
  220. ];
  221. $attempt_link .= PHP_EOL.$teacher_revised;
  222. if (in_array(
  223. $objExercise->results_disabled,
  224. [
  225. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  226. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  227. RESULT_DISABLE_SHOW_SCORE_ONLY,
  228. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
  229. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
  230. RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
  231. RESULT_DISABLE_RANKING,
  232. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  233. ]
  234. )) {
  235. $row['result'] = $score;
  236. }
  237. if (in_array(
  238. $objExercise->results_disabled,
  239. [
  240. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  241. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  242. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
  243. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
  244. RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
  245. RESULT_DISABLE_RANKING,
  246. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  247. ]
  248. ) || (
  249. $objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY &&
  250. $objExercise->feedback_type == EXERCISE_FEEDBACK_TYPE_END
  251. )
  252. ) {
  253. if ($blockShowAnswers &&
  254. $objExercise->results_disabled != RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK
  255. ) {
  256. $attempt_link = '';
  257. }
  258. if ($blockShowAnswers == true &&
  259. $objExercise->results_disabled == RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK
  260. ) {
  261. if (isset($row['result'])) {
  262. unset($row['result']);
  263. }
  264. }
  265. $row['attempt_link'] = $attempt_link;
  266. }
  267. $my_attempt_array[] = $row;
  268. $i--;
  269. }
  270. $header_names = [];
  271. $table = new HTML_Table(['class' => 'table table-striped table-hover']);
  272. // Hiding score and answer
  273. switch ($objExercise->results_disabled) {
  274. case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
  275. if ($blockShowAnswers) {
  276. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Details')];
  277. } else {
  278. $header_names = [
  279. get_lang('Attempt'),
  280. get_lang('StartDate'),
  281. get_lang('IP'),
  282. get_lang('Score'),
  283. get_lang('Details'),
  284. ];
  285. }
  286. break;
  287. case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
  288. if ($blockShowAnswers) {
  289. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score')];
  290. } else {
  291. $header_names = [
  292. get_lang('Attempt'),
  293. get_lang('StartDate'),
  294. get_lang('IP'),
  295. get_lang('Score'),
  296. get_lang('Details'),
  297. ];
  298. }
  299. break;
  300. case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS:
  301. case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING:
  302. case RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES:
  303. case RESULT_DISABLE_RANKING:
  304. $header_names = [
  305. get_lang('Attempt'),
  306. get_lang('StartDate'),
  307. get_lang('IP'),
  308. get_lang('Score'),
  309. get_lang('Details'),
  310. ];
  311. break;
  312. case RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS:
  313. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP')];
  314. break;
  315. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  316. if ($objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_END) {
  317. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score')];
  318. } else {
  319. $header_names = [
  320. get_lang('Attempt'),
  321. get_lang('StartDate'),
  322. get_lang('IP'),
  323. get_lang('Score'),
  324. get_lang('Details'),
  325. ];
  326. }
  327. break;
  328. }
  329. $column = 0;
  330. foreach ($header_names as $item) {
  331. $table->setHeaderContents(0, $column, $item);
  332. $column++;
  333. }
  334. $row = 1;
  335. if (!empty($my_attempt_array)) {
  336. foreach ($my_attempt_array as $data) {
  337. $column = 0;
  338. $table->setCellContents($row, $column, $data);
  339. $table->setRowAttributes($row, null, true);
  340. $column++;
  341. $row++;
  342. }
  343. }
  344. $table_content = $table->toHtml();
  345. }
  346. if ($objExercise->selectAttempts()) {
  347. $attempt_message = get_lang('Attempts').' '.$counter.' / '.$objExercise->selectAttempts();
  348. if ($counter == $objExercise->selectAttempts()) {
  349. $attempt_message = Display::return_message($attempt_message, 'error');
  350. } else {
  351. $attempt_message = Display::return_message($attempt_message, 'info');
  352. }
  353. if ($visible_return['value'] == true) {
  354. $message .= $attempt_message;
  355. }
  356. }
  357. if ($time_control) {
  358. $html .= $objExercise->return_time_left_div();
  359. }
  360. $html .= $message;
  361. $disable = api_get_configuration_value('exercises_disable_new_attempts');
  362. if ($disable && empty($exercise_stat_info)) {
  363. $exercise_url_button = Display::return_message(get_lang('NewExerciseAttemptDisabled'));
  364. }
  365. $isLimitReached = ExerciseLib::isQuestionsLimitPerDayReached(
  366. api_get_user_id(),
  367. count($objExercise->get_validated_question_list()),
  368. api_get_course_int_id(),
  369. api_get_session_id()
  370. );
  371. if (!empty($exercise_url_button) && !$isLimitReached) {
  372. $html .= Display::div(
  373. Display::div(
  374. $exercise_url_button,
  375. ['class' => 'exercise_overview_options']
  376. ),
  377. ['class' => 'options']
  378. );
  379. }
  380. if ($isLimitReached) {
  381. $maxQuestionsAnswered = (int) api_get_course_setting('quiz_question_limit_per_day');
  382. $html .= Display::return_message(
  383. sprintf(get_lang('QuizQuestionsLimitPerDayXReached'), $maxQuestionsAnswered),
  384. 'warning',
  385. false
  386. );
  387. }
  388. $html .= Display::tag(
  389. 'div',
  390. $table_content,
  391. ['class' => 'table-responsive']
  392. );
  393. $html .= '</div>';
  394. if ($certificateBlock) {
  395. $html .= PHP_EOL.$certificateBlock;
  396. }
  397. echo $html;
  398. Display::display_footer();