overview.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Exercise preview
  6. *
  7. * @package chamilo.exercise
  8. * @author Julio Montoya <gugli100@gmail.com>
  9. */
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $current_course_tool = TOOL_QUIZ;
  12. // Clear the exercise session just in case
  13. Session::erase('objExercise');
  14. $this_section = SECTION_COURSES;
  15. // Notice for unauthorized people.
  16. api_protect_course_script(true);
  17. $sessionId = api_get_session_id();
  18. $exercise_id = isset($_REQUEST['exerciseId']) ? intval($_REQUEST['exerciseId']) : 0;
  19. $objExercise = new Exercise();
  20. $result = $objExercise->read($exercise_id);
  21. if (!$result) {
  22. api_not_allowed(true);
  23. }
  24. $gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  25. $learnpath_id = isset($_REQUEST['learnpath_id']) ? intval($_REQUEST['learnpath_id']) : null;
  26. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? intval($_REQUEST['learnpath_item_id']) : null;
  27. $learnpathItemViewId = isset($_REQUEST['learnpath_item_view_id']) ? intval($_REQUEST['learnpath_item_view_id']) : null;
  28. $origin = api_get_origin();
  29. $interbreadcrumb[] = array("url" => "exercise.php?gradebook=$gradebook", "name" => get_lang('Exercises'));
  30. $interbreadcrumb[] = array("url" => "#", "name" => $objExercise->selectTitle(true));
  31. $time_control = false;
  32. $clock_expired_time = ExerciseLib::get_session_time_control_key($objExercise->id, $learnpath_id, $learnpath_item_id);
  33. if ($objExercise->expired_time != 0 && !empty($clock_expired_time)) {
  34. $time_control = true;
  35. }
  36. if ($time_control) {
  37. // Get time left for expiring time
  38. $time_left = api_strtotime($clock_expired_time, 'UTC') - time();
  39. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/stylesheet/jquery.epiclock.css');
  40. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/renderers/minute/epiclock.minute.css');
  41. $htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.dateformat.min.js');
  42. $htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.epiclock.min.js');
  43. $htmlHeadXtra[] = api_get_js('epiclock/renderers/minute/epiclock.minute.js');
  44. $htmlHeadXtra[] = $objExercise->show_time_control_js($time_left);
  45. }
  46. if ($origin != 'learnpath') {
  47. Display::display_header();
  48. } else {
  49. $htmlHeadXtra[] = "
  50. <style>
  51. body { background: none;}
  52. </style>
  53. ";
  54. Display::display_reduced_header();
  55. }
  56. $html = '';
  57. $message = '';
  58. $html .= '<div class="exercise">';
  59. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  60. $edit_link = '';
  61. if ($is_allowed_to_edit && $objExercise->sessionId == $sessionId) {
  62. $edit_link = Display::url(
  63. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  64. api_get_path(WEB_CODE_PATH).'exercise/admin.php?'.api_get_cidreq().'&id_session='.api_get_session_id().'&exerciseId='.$objExercise->id
  65. );
  66. }
  67. $iconExercise = Display::return_icon('test-quiz.png', null, array(), ICON_SIZE_MEDIUM);
  68. // Exercise name.
  69. if (api_get_configuration_value('save_titles_as_html')) {
  70. $html .= Display::div(
  71. $objExercise->get_formated_title().PHP_EOL.$edit_link
  72. );
  73. } else {
  74. $html .= Display::page_header(
  75. $iconExercise.PHP_EOL.$objExercise->selectTitle().PHP_EOL.$edit_link
  76. );
  77. }
  78. //Exercise description
  79. if (!empty($objExercise->description)) {
  80. $html .= Display::div($objExercise->description, array('class'=>'exercise_description'));
  81. }
  82. $extra_params = '';
  83. if (isset($_GET['preview'])) {
  84. $extra_params = '&preview=1';
  85. }
  86. $exercise_stat_info = $objExercise->get_stat_track_exercise_info(
  87. $learnpath_id,
  88. $learnpath_item_id,
  89. 0
  90. );
  91. /*$attempt_list = null;
  92. if (isset($exercise_stat_info['exe_id'])) {
  93. $attempt_list = Event::getAllExerciseEventByExeId($exercise_stat_info['exe_id']);
  94. }*/
  95. //1. Check if this is a new attempt or a previous
  96. $label = get_lang('StartTest');
  97. if ($time_control && !empty($clock_expired_time) || isset($exercise_stat_info['exe_id'])) {
  98. $label = get_lang('ContinueTest');
  99. }
  100. if (isset($exercise_stat_info['exe_id'])) {
  101. $message = Display::return_message(get_lang('YouTriedToResolveThisExerciseEarlier'));
  102. }
  103. // 2. Exercise button
  104. // Notice we not add there the lp_item_view_id because is not already generated
  105. $exercise_url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'&origin='.$origin.'&learnpath_id='.$learnpath_id.'&learnpath_item_id='.$learnpath_item_id.'&learnpath_item_view_id='.$learnpathItemViewId.$extra_params;
  106. $exercise_url_button = Display::url(
  107. $label,
  108. $exercise_url,
  109. array('class' => 'btn btn-success btn-large')
  110. );
  111. //3. Checking visibility of the exercise (overwrites the exercise button)
  112. $visible_return = $objExercise->is_visible(
  113. $learnpath_id,
  114. $learnpath_item_id,
  115. null,
  116. false
  117. );
  118. // Exercise is not visible remove the button
  119. if ($visible_return['value'] == false) {
  120. if ($is_allowed_to_edit) {
  121. $message = Display::return_message(get_lang('ThisItemIsInvisibleForStudentsButYouHaveAccessAsTeacher'), 'warning');
  122. } else {
  123. $message = $visible_return['message'];
  124. $exercise_url_button = null;
  125. }
  126. }
  127. $attempts = Event::getExerciseResultsByUser(
  128. api_get_user_id(),
  129. $objExercise->id,
  130. api_get_course_int_id(),
  131. api_get_session_id(),
  132. $learnpath_id,
  133. $learnpath_item_id,
  134. 'desc'
  135. );
  136. $counter = count($attempts);
  137. $my_attempt_array = array();
  138. $table_content = '';
  139. /* Make a special case for IE, which doesn't seem to be able to handle the
  140. * results popup -> send it to the full results page */
  141. $browser = new Browser();
  142. $current_browser = $browser->getBrowser();
  143. $url_suffix = '';
  144. $btn_class = ' ';
  145. if ($current_browser == 'Internet Explorer') {
  146. $url_suffix = '&show_headers=1';
  147. $btn_class = '';
  148. }
  149. $blockShowAnswers = false;
  150. if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  151. if (count($attempts) < $objExercise->attempts) {
  152. $blockShowAnswers = true;
  153. }
  154. }
  155. if (!empty($attempts)) {
  156. $i = $counter;
  157. foreach ($attempts as $attempt_result) {
  158. $score = ExerciseLib::show_score(
  159. $attempt_result['exe_result'],
  160. $attempt_result['exe_weighting']
  161. );
  162. $attempt_url = api_get_path(WEB_CODE_PATH).'exercise/result.php?';
  163. $attempt_url .= api_get_cidreq().'&show_headers=1&';
  164. $attempt_url .= http_build_query([
  165. 'id' => $attempt_result['exe_id']
  166. ]);
  167. $attempt_url .= $url_suffix;
  168. $attempt_link = Display::url(
  169. get_lang('Show'),
  170. $attempt_url,
  171. [
  172. 'class' => $btn_class.'btn btn-default',
  173. 'data-title' => get_lang('Show'),
  174. 'data-size' => 'lg'
  175. ]
  176. );
  177. $teacher_revised = Display::label(get_lang('Validated'), 'success');
  178. if ($attempt_result['attempt_revised'] == 0) {
  179. $teacher_revised = Display::label(get_lang('NotValidated'), 'info');
  180. }
  181. $row = array(
  182. 'count' => $i,
  183. 'date' => api_convert_and_format_date(
  184. $attempt_result['start_date'],
  185. DATE_TIME_FORMAT_LONG
  186. ),
  187. 'userIp' => $attempt_result['user_ip']
  188. );
  189. $attempt_link .= "&nbsp;&nbsp;&nbsp;".$teacher_revised;
  190. if (in_array(
  191. $objExercise->results_disabled,
  192. array(
  193. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  194. RESULT_DISABLE_SHOW_SCORE_ONLY,
  195. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
  196. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT
  197. )
  198. )) {
  199. $row['result'] = $score;
  200. }
  201. if (in_array(
  202. $objExercise->results_disabled,
  203. array(
  204. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  205. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
  206. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT
  207. )
  208. )
  209. || (
  210. $objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY &&
  211. $objExercise->feedback_type == EXERCISE_FEEDBACK_TYPE_END)
  212. ) {
  213. if ($blockShowAnswers) {
  214. $attempt_link = '';
  215. }
  216. $row['attempt_link'] = $attempt_link;
  217. }
  218. $my_attempt_array[] = $row;
  219. $i--;
  220. }
  221. $header_names = [];
  222. $table = new HTML_Table(array('class' => 'table table-striped table-hover'));
  223. // Hiding score and answer
  224. switch ($objExercise->results_disabled) {
  225. case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
  226. if ($blockShowAnswers) {
  227. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score'));
  228. } else {
  229. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score'), get_lang('Details'));
  230. }
  231. break;
  232. case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS:
  233. case RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES:
  234. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score'), get_lang('Details'));
  235. break;
  236. case RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS:
  237. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'));
  238. break;
  239. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  240. if ($objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_END) {
  241. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score'));
  242. } else {
  243. $header_names = array(get_lang('Attempt'), get_lang('StartDate'), get_lang('IP'), get_lang('Score'), get_lang('Details'));
  244. }
  245. break;
  246. }
  247. $column = 0;
  248. foreach ($header_names as $item) {
  249. $table->setHeaderContents(0, $column, $item);
  250. $column++;
  251. }
  252. $row = 1;
  253. if (!empty($my_attempt_array)) {
  254. foreach ($my_attempt_array as $data) {
  255. $column = 0;
  256. $table->setCellContents($row, $column, $data);
  257. $table->setRowAttributes($row, null, true);
  258. $column++;
  259. $row++;
  260. }
  261. }
  262. $table_content = $table->toHtml();
  263. }
  264. if ($objExercise->selectAttempts()) {
  265. $attempt_message = get_lang('Attempts').' '.$counter.' / '.$objExercise->selectAttempts();
  266. if ($counter == $objExercise->selectAttempts()) {
  267. $attempt_message = Display::return_message($attempt_message, 'error');
  268. } else {
  269. $attempt_message = Display::return_message($attempt_message, 'info');
  270. }
  271. if ($visible_return['value'] == true) {
  272. $message .= $attempt_message;
  273. }
  274. }
  275. if ($time_control) {
  276. $html .= $objExercise->return_time_left_div();
  277. }
  278. $html .= $message;
  279. if (!empty($exercise_url_button)) {
  280. $html .= Display::div(
  281. Display::div(
  282. $exercise_url_button,
  283. array('class' => 'exercise_overview_options col-md-12')
  284. ),
  285. array('class' => ' row')
  286. );
  287. }
  288. $html .= Display::tag(
  289. 'div',
  290. $table_content,
  291. ['class' => 'table-responsive']
  292. );
  293. $html .= '</div>';
  294. echo $html;
  295. Display::display_footer();