overview.php 11 KB

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