overview.php 10 KB

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