overview.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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('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. $certificateBlock = '';
  179. if (!empty($attempts)) {
  180. $i = $counter;
  181. foreach ($attempts as $attempt_result) {
  182. if (empty($certificateBlock)) {
  183. $certificateBlock = ExerciseLib::generateAndShowCertificateBlock(
  184. $attempt_result['exe_result'],
  185. $attempt_result['exe_weighting'],
  186. $objExercise,
  187. $attempt_result['exe_user_id'],
  188. $courseCode,
  189. $sessionId
  190. );
  191. }
  192. $score = ExerciseLib::show_score($attempt_result['exe_result'], $attempt_result['exe_weighting']);
  193. $attempt_url = api_get_path(WEB_CODE_PATH).'exercise/result.php?';
  194. $attempt_url .= api_get_cidreq().'&show_headers=1&';
  195. $attempt_url .= http_build_query(['id' => $attempt_result['exe_id']]);
  196. $attempt_url .= $url_suffix;
  197. $attempt_link = Display::url(
  198. get_lang('Show'),
  199. $attempt_url,
  200. [
  201. 'class' => $btn_class.'btn btn-default',
  202. 'data-title' => get_lang('Show'),
  203. 'data-size' => 'lg',
  204. ]
  205. );
  206. $teacher_revised = Display::label(get_lang('Validated'), 'success');
  207. if ($attempt_result['attempt_revised'] == 0) {
  208. $teacher_revised = Display::label(get_lang('NotValidated'), 'info');
  209. }
  210. $row = [
  211. 'count' => $i,
  212. 'date' => api_convert_and_format_date(
  213. $attempt_result['start_date'],
  214. DATE_TIME_FORMAT_LONG
  215. ),
  216. 'userIp' => $attempt_result['user_ip'],
  217. ];
  218. $attempt_link .= PHP_EOL.$teacher_revised;
  219. if (in_array(
  220. $objExercise->results_disabled,
  221. [
  222. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  223. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  224. RESULT_DISABLE_SHOW_SCORE_ONLY,
  225. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
  226. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
  227. RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
  228. RESULT_DISABLE_RANKING,
  229. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  230. ]
  231. )) {
  232. $row['result'] = $score;
  233. }
  234. if (in_array(
  235. $objExercise->results_disabled,
  236. [
  237. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  238. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  239. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
  240. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
  241. RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
  242. RESULT_DISABLE_RANKING,
  243. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  244. ]
  245. ) || (
  246. $objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY &&
  247. $objExercise->getFeedbackType() == EXERCISE_FEEDBACK_TYPE_END
  248. )
  249. ) {
  250. if ($blockShowAnswers &&
  251. $objExercise->results_disabled != RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK
  252. ) {
  253. $attempt_link = '';
  254. }
  255. if ($blockShowAnswers == true &&
  256. $objExercise->results_disabled == RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK
  257. ) {
  258. if (isset($row['result'])) {
  259. unset($row['result']);
  260. }
  261. }
  262. if (!empty($objExercise->getResultAccess())) {
  263. if (!$objExercise->hasResultsAccess($attempt_result)) {
  264. $attempt_link = '';
  265. }
  266. }
  267. $row['attempt_link'] = $attempt_link;
  268. }
  269. $my_attempt_array[] = $row;
  270. $i--;
  271. }
  272. $header_names = [];
  273. $table = new HTML_Table(['class' => 'table table-striped table-hover']);
  274. // Hiding score and answer
  275. switch ($objExercise->results_disabled) {
  276. case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
  277. if ($blockShowAnswers) {
  278. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Details')];
  279. } else {
  280. $header_names = [
  281. get_lang('Attempt'),
  282. get_lang('StartDate'),
  283. get_lang('IP'),
  284. get_lang('Score'),
  285. get_lang('Details'),
  286. ];
  287. }
  288. break;
  289. case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
  290. if ($blockShowAnswers) {
  291. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score')];
  292. } else {
  293. $header_names = [
  294. get_lang('Attempt'),
  295. get_lang('StartDate'),
  296. get_lang('IP'),
  297. get_lang('Score'),
  298. get_lang('Details'),
  299. ];
  300. }
  301. break;
  302. case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS:
  303. case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING:
  304. case RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES:
  305. case RESULT_DISABLE_RANKING:
  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. break;
  314. case RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS:
  315. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP')];
  316. break;
  317. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  318. if ($objExercise->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_END) {
  319. $header_names = [get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score')];
  320. } else {
  321. $header_names = [
  322. get_lang('Attempt'),
  323. get_lang('StartDate'),
  324. get_lang('IP'),
  325. get_lang('Score'),
  326. get_lang('Details'),
  327. ];
  328. }
  329. break;
  330. }
  331. $column = 0;
  332. foreach ($header_names as $item) {
  333. $table->setHeaderContents(0, $column, $item);
  334. $column++;
  335. }
  336. $row = 1;
  337. if (!empty($my_attempt_array)) {
  338. foreach ($my_attempt_array as $data) {
  339. $column = 0;
  340. $table->setCellContents($row, $column, $data);
  341. $table->setRowAttributes($row, null, true);
  342. $column++;
  343. $row++;
  344. }
  345. }
  346. $table_content = $table->toHtml();
  347. }
  348. $selectAttempts = $objExercise->selectAttempts();
  349. if ($selectAttempts) {
  350. $attempt_message = get_lang('Attempts').' '.$counter.' / '.$selectAttempts;
  351. if ($counter == $selectAttempts) {
  352. $attempt_message = Display::return_message($attempt_message, 'error');
  353. } else {
  354. $attempt_message = Display::return_message($attempt_message, 'info');
  355. }
  356. if ($visible_return['value'] == true) {
  357. $message .= $attempt_message;
  358. }
  359. }
  360. if ($time_control) {
  361. $html .= $objExercise->returnTimeLeftDiv();
  362. }
  363. $html .= $message;
  364. $disable = api_get_configuration_value('exercises_disable_new_attempts');
  365. if ($disable && empty($exercise_stat_info)) {
  366. $exercise_url_button = Display::return_message(get_lang('NewExerciseAttemptDisabled'));
  367. }
  368. $isLimitReached = ExerciseLib::isQuestionsLimitPerDayReached(
  369. api_get_user_id(),
  370. count($objExercise->get_validated_question_list()),
  371. api_get_course_int_id(),
  372. api_get_session_id()
  373. );
  374. if (!empty($exercise_url_button) && !$isLimitReached) {
  375. $html .= Display::div(
  376. Display::div(
  377. $exercise_url_button,
  378. ['class' => 'exercise_overview_options']
  379. ),
  380. ['class' => 'options']
  381. );
  382. }
  383. if ($isLimitReached) {
  384. $maxQuestionsAnswered = (int) api_get_course_setting('quiz_question_limit_per_day');
  385. $html .= Display::return_message(
  386. sprintf(get_lang('QuizQuestionsLimitPerDayXReached'), $maxQuestionsAnswered),
  387. 'warning',
  388. false
  389. );
  390. }
  391. $html .= Display::tag(
  392. 'div',
  393. $table_content,
  394. ['class' => 'table-responsive']
  395. );
  396. $html .= '</div>';
  397. if ($certificateBlock) {
  398. $html .= PHP_EOL.$certificateBlock;
  399. }
  400. echo $html;
  401. Display::display_footer();