overview.php 14 KB

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