overview.php 11 KB

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