overview.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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']) ? (int) $_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->getFeedbackType() == 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. if (!empty($objExercise->getResultAccess())) {
  266. if (!$objExercise->hasResultsAccess($attempt_result)) {
  267. $attempt_link = '';
  268. }
  269. }
  270. $row['attempt_link'] = $attempt_link;
  271. }
  272. $my_attempt_array[] = $row;
  273. $i--;
  274. }
  275. $header_names = [];
  276. $table = new HTML_Table(['class' => 'table table-striped table-hover']);
  277. // Hiding score and answer
  278. switch ($objExercise->results_disabled) {
  279. case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
  280. if ($blockShowAnswers) {
  281. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Details')];
  282. } else {
  283. $header_names = [
  284. get_lang('Attempt'),
  285. get_lang('StartDate'),
  286. get_lang('IP'),
  287. get_lang('Score'),
  288. get_lang('Details'),
  289. ];
  290. }
  291. break;
  292. case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
  293. if ($blockShowAnswers) {
  294. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score')];
  295. } else {
  296. $header_names = [
  297. get_lang('Attempt'),
  298. get_lang('StartDate'),
  299. get_lang('IP'),
  300. get_lang('Score'),
  301. get_lang('Details'),
  302. ];
  303. }
  304. break;
  305. case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS:
  306. case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING:
  307. case RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES:
  308. case RESULT_DISABLE_RANKING:
  309. $header_names = [
  310. get_lang('Attempt'),
  311. get_lang('StartDate'),
  312. get_lang('IP'),
  313. get_lang('Score'),
  314. get_lang('Details'),
  315. ];
  316. break;
  317. case RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS:
  318. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP')];
  319. break;
  320. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  321. if ($objExercise->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_END) {
  322. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score')];
  323. } else {
  324. $header_names = [
  325. get_lang('Attempt'),
  326. get_lang('StartDate'),
  327. get_lang('IP'),
  328. get_lang('Score'),
  329. get_lang('Details'),
  330. ];
  331. }
  332. break;
  333. }
  334. $column = 0;
  335. foreach ($header_names as $item) {
  336. $table->setHeaderContents(0, $column, $item);
  337. $column++;
  338. }
  339. $row = 1;
  340. if (!empty($my_attempt_array)) {
  341. foreach ($my_attempt_array as $data) {
  342. $column = 0;
  343. $table->setCellContents($row, $column, $data);
  344. $table->setRowAttributes($row, null, true);
  345. $column++;
  346. $row++;
  347. }
  348. }
  349. $table_content = $table->toHtml();
  350. }
  351. $selectAttempts = $objExercise->selectAttempts();
  352. if ($selectAttempts) {
  353. $attempt_message = get_lang('Attempts').' '.$counter.' / '.$selectAttempts;
  354. if ($counter == $selectAttempts) {
  355. $attempt_message = Display::return_message($attempt_message, 'error');
  356. } else {
  357. $attempt_message = Display::return_message($attempt_message, 'info');
  358. }
  359. if ($visible_return['value'] == true) {
  360. $message .= $attempt_message;
  361. }
  362. }
  363. if ($time_control) {
  364. $html .= $objExercise->returnTimeLeftDiv();
  365. }
  366. // Ofaj
  367. //$html .= $message;
  368. $disable = api_get_configuration_value('exercises_disable_new_attempts');
  369. if ($disable && empty($exercise_stat_info)) {
  370. $exercise_url_button = Display::return_message(get_lang('NewExerciseAttemptDisabled'));
  371. }
  372. $isLimitReached = ExerciseLib::isQuestionsLimitPerDayReached(
  373. api_get_user_id(),
  374. count($objExercise->get_validated_question_list()),
  375. api_get_course_int_id(),
  376. api_get_session_id()
  377. );
  378. if (!empty($exercise_url_button) && !$isLimitReached) {
  379. $html .= Display::div(
  380. Display::div(
  381. $exercise_url_button,
  382. ['class' => 'exercise_overview_options']
  383. ),
  384. ['class' => 'options']
  385. );
  386. }
  387. // ofaj
  388. if ($counter == $selectAttempts) {
  389. $html .= '<div class="alert alert-error" role="alert">';
  390. $html .= get_lang('Attempts')." $counter / $selectAttempts ";
  391. $html .= '</div>';
  392. } else {
  393. $html .= '<div class="attempts">
  394. <ul class="attempts-status">';
  395. $path = api_get_path(WEB_CSS_PATH).api_get_visual_theme();
  396. if ($counter != 0) {
  397. for ($i = 0; $i <= $counter -1; $i++) {
  398. $html .= '<li>'.Display::return_icon('attempt-check.png').'</li>';
  399. }
  400. }
  401. if ($counter != $selectAttempts) {
  402. for ($i = 0; $i <= $selectAttempts - 1 - $counter ; $i++) {
  403. $html .= '<li>'.Display::return_icon('attempt-nocheck.png').'</li>';
  404. }
  405. }
  406. $html .= '</ul>
  407. <div class="details">';
  408. $html .= get_lang('Attempts')." $counter / $selectAttempts ";
  409. $html .= '</div></div>';
  410. }
  411. if ($isLimitReached) {
  412. $maxQuestionsAnswered = (int) api_get_course_setting('quiz_question_limit_per_day');
  413. $html .= Display::return_message(
  414. sprintf(get_lang('QuizQuestionsLimitPerDayXReached'), $maxQuestionsAnswered),
  415. 'warning',
  416. false
  417. );
  418. }
  419. $html .= Display::tag(
  420. 'div',
  421. $table_content,
  422. ['class' => 'table-responsive']
  423. );
  424. $html .= '</div>';
  425. if ($certificateBlock) {
  426. $html .= PHP_EOL.$certificateBlock;
  427. }
  428. echo $html;
  429. Display::display_footer();