overview.php 15 KB

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