overview.php 9.8 KB

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