overview.php 14 KB

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