overview.php 9.9 KB

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