exercise_report.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Exercise list: This script shows the list of exercises for administrators and students.
  5. *
  6. * @package chamilo.exercise
  7. *
  8. * @author Julio Montoya <gugli100@gmail.com> jqgrid integration
  9. * Modified by hubert.borderiou (question category)
  10. *
  11. * @todo fix excel export
  12. */
  13. require_once __DIR__.'/../inc/global.inc.php';
  14. // Setting the tabs
  15. $this_section = SECTION_COURSES;
  16. $htmlHeadXtra[] = api_get_jqgrid_js();
  17. $filter_user = isset($_REQUEST['filter_by_user']) ? (int) $_REQUEST['filter_by_user'] : null;
  18. $isBossOfStudent = false;
  19. if (api_is_student_boss() && !empty($filter_user)) {
  20. // Check if boss has access to user info.
  21. if (UserManager::userIsBossOfStudent(api_get_user_id(), $filter_user)) {
  22. $isBossOfStudent = true;
  23. } else {
  24. api_not_allowed(true);
  25. }
  26. } else {
  27. api_protect_course_script(true, false, true);
  28. }
  29. $limitTeacherAccess = api_get_configuration_value('limit_exercise_teacher_access');
  30. if ($limitTeacherAccess && !api_is_platform_admin()) {
  31. api_not_allowed(true);
  32. }
  33. // including additional libraries
  34. require_once 'hotpotatoes.lib.php';
  35. $_course = api_get_course_info();
  36. // document path
  37. $documentPath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  38. $origin = api_get_origin();
  39. $is_allowedToEdit = api_is_allowed_to_edit(null, true) ||
  40. api_is_drh() ||
  41. api_is_student_boss() ||
  42. api_is_session_admin();
  43. $is_tutor = api_is_allowed_to_edit(true);
  44. $TBL_TRACK_EXERCISES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  45. $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  46. $TBL_TRACK_ATTEMPT_RECORDING = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  47. $TBL_LP_ITEM_VIEW = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  48. $allowCoachFeedbackExercises = api_get_setting('allow_coach_feedback_exercises') === 'true';
  49. $course_id = api_get_course_int_id();
  50. $exercise_id = isset($_REQUEST['exerciseId']) ? (int) $_REQUEST['exerciseId'] : 0;
  51. $locked = api_resource_is_locked_by_gradebook($exercise_id, LINK_EXERCISE);
  52. $sessionId = api_get_session_id();
  53. if (empty($exercise_id)) {
  54. api_not_allowed(true);
  55. }
  56. $blockPage = true;
  57. if (empty($sessionId)) {
  58. if ($is_allowedToEdit) {
  59. $blockPage = false;
  60. }
  61. } else {
  62. if ($allowCoachFeedbackExercises && api_is_coach($sessionId, $course_id)) {
  63. $blockPage = false;
  64. } else {
  65. if ($is_allowedToEdit) {
  66. $blockPage = false;
  67. }
  68. }
  69. }
  70. if ($blockPage) {
  71. api_not_allowed(true);
  72. }
  73. if (!empty($exercise_id)) {
  74. $parameters['exerciseId'] = $exercise_id;
  75. }
  76. if (!empty($_GET['path'])) {
  77. $parameters['path'] = Security::remove_XSS($_GET['path']);
  78. }
  79. if (!empty($_REQUEST['export_report']) && $_REQUEST['export_report'] == '1') {
  80. if (api_is_platform_admin() || api_is_course_admin() ||
  81. api_is_course_tutor() || api_is_session_general_coach()
  82. ) {
  83. $loadExtraData = false;
  84. if (isset($_REQUEST['extra_data']) && $_REQUEST['extra_data'] == 1) {
  85. $loadExtraData = true;
  86. }
  87. $includeAllUsers = false;
  88. if (isset($_REQUEST['include_all_users']) &&
  89. $_REQUEST['include_all_users'] == 1
  90. ) {
  91. $includeAllUsers = true;
  92. }
  93. $onlyBestAttempts = false;
  94. if (isset($_REQUEST['only_best_attempts']) &&
  95. $_REQUEST['only_best_attempts'] == 1
  96. ) {
  97. $onlyBestAttempts = true;
  98. }
  99. require_once 'exercise_result.class.php';
  100. $export = new ExerciseResult();
  101. $export->setIncludeAllUsers($includeAllUsers);
  102. $export->setOnlyBestAttempts($onlyBestAttempts);
  103. switch ($_GET['export_format']) {
  104. case 'xls':
  105. $export->exportCompleteReportXLS(
  106. $documentPath,
  107. null,
  108. $loadExtraData,
  109. null,
  110. $exercise_id
  111. );
  112. exit;
  113. break;
  114. case 'csv':
  115. default:
  116. $export->exportCompleteReportCSV(
  117. $documentPath,
  118. null,
  119. $loadExtraData,
  120. null,
  121. $exercise_id
  122. );
  123. exit;
  124. break;
  125. }
  126. } else {
  127. api_not_allowed(true);
  128. }
  129. }
  130. $objExerciseTmp = new Exercise();
  131. $exerciseExists = $objExerciseTmp->read($exercise_id);
  132. //Send student email @todo move this code in a class, library
  133. if (isset($_REQUEST['comments']) &&
  134. $_REQUEST['comments'] === 'update' &&
  135. ($is_allowedToEdit || $is_tutor || $allowCoachFeedbackExercises)
  136. ) {
  137. // Filtered by post-condition
  138. $id = (int) $_GET['exeid'];
  139. $track_exercise_info = ExerciseLib::get_exercise_track_exercise_info($id);
  140. if (empty($track_exercise_info)) {
  141. api_not_allowed();
  142. }
  143. $student_id = $track_exercise_info['exe_user_id'];
  144. $session_id = $track_exercise_info['session_id'];
  145. $lp_id = $track_exercise_info['orig_lp_id'];
  146. $lpItemId = $track_exercise_info['orig_lp_item_id'];
  147. $lp_item_view_id = (int) $track_exercise_info['orig_lp_item_view_id'];
  148. $exerciseId = $track_exercise_info['exe_exo_id'];
  149. $exeWeighting = $track_exercise_info['exe_weighting'];
  150. $post_content_id = [];
  151. $comments_exist = false;
  152. foreach ($_POST as $key_index => $key_value) {
  153. $my_post_info = explode('_', $key_index);
  154. $post_content_id[] = isset($my_post_info[1]) ? $my_post_info[1] : null;
  155. if ($my_post_info[0] === 'comments') {
  156. $comments_exist = true;
  157. }
  158. }
  159. $loop_in_track = $comments_exist === true ? (count($_POST) / 2) : count($_POST);
  160. if ($comments_exist === true) {
  161. $array_content_id_exe = array_slice($post_content_id, $loop_in_track);
  162. } else {
  163. $array_content_id_exe = $post_content_id;
  164. }
  165. for ($i = 0; $i < $loop_in_track; $i++) {
  166. $my_marks = isset($_POST['marks_'.$array_content_id_exe[$i]]) ? $_POST['marks_'.$array_content_id_exe[$i]] : '';
  167. $my_comments = '';
  168. if (isset($_POST['comments_'.$array_content_id_exe[$i]])) {
  169. $my_comments = $_POST['comments_'.$array_content_id_exe[$i]];
  170. }
  171. $my_questionid = (int) $array_content_id_exe[$i];
  172. $params = [
  173. 'marks' => $my_marks,
  174. 'teacher_comment' => $my_comments,
  175. ];
  176. Database::update(
  177. $TBL_TRACK_ATTEMPT,
  178. $params,
  179. ['question_id = ? AND exe_id = ?' => [$my_questionid, $id]]
  180. );
  181. $params = [
  182. 'exe_id' => $id,
  183. 'question_id' => $my_questionid,
  184. 'marks' => $my_marks,
  185. 'insert_date' => api_get_utc_datetime(),
  186. 'author' => api_get_user_id(),
  187. 'teacher_comment' => $my_comments,
  188. ];
  189. Database::insert($TBL_TRACK_ATTEMPT_RECORDING, $params);
  190. }
  191. $useEvaluationPlugin = false;
  192. $pluginEvaluation = QuestionOptionsEvaluationPlugin::create();
  193. if ('true' === $pluginEvaluation->get(QuestionOptionsEvaluationPlugin::SETTING_ENABLE)) {
  194. $formula = $pluginEvaluation->getFormulaForExercise($exerciseId);
  195. if (!empty($formula)) {
  196. $useEvaluationPlugin = true;
  197. }
  198. }
  199. if (!$useEvaluationPlugin) {
  200. $qry = 'SELECT DISTINCT question_id, marks
  201. FROM '.$TBL_TRACK_ATTEMPT.' WHERE exe_id = '.$id.'
  202. GROUP BY question_id';
  203. $res = Database::query($qry);
  204. $tot = 0;
  205. while ($row = Database :: fetch_array($res, 'ASSOC')) {
  206. $tot += $row['marks'];
  207. }
  208. } else {
  209. $tot = $pluginEvaluation->getResultWithFormula($id, $formula);
  210. }
  211. $sql = "UPDATE $TBL_TRACK_EXERCISES
  212. SET score = '".floatval($tot)."'
  213. WHERE exe_id = ".$id;
  214. Database::query($sql);
  215. if (isset($_POST['send_notification'])) {
  216. //@todo move this somewhere else
  217. $subject = get_lang('Examsheet viewed/corrected/commented by the trainer');
  218. $message = isset($_POST['notification_content']) ? $_POST['notification_content'] : '';
  219. MessageManager::send_message_simple(
  220. $student_id,
  221. $subject,
  222. $message,
  223. api_get_user_id()
  224. );
  225. if ($allowCoachFeedbackExercises) {
  226. Display::addFlash(
  227. Display::return_message(get_lang('Message Sent'))
  228. );
  229. header('Location: '.api_get_self().'?'.api_get_cidreq().'&exerciseId='.$exerciseId);
  230. exit;
  231. }
  232. }
  233. // Updating LP score here
  234. if (!empty($lp_id) && !empty($lpItemId)) {
  235. $statusCondition = '';
  236. $item = new learnpathItem($lpItemId, api_get_user_id(), api_get_course_int_id());
  237. if ($item) {
  238. $prereqId = $item->get_prereq_string();
  239. $minScore = $item->getPrerequisiteMinScore();
  240. $maxScore = $item->getPrerequisiteMaxScore();
  241. $passed = false;
  242. $lp = new learnpath(api_get_course_id(), $lp_id, $student_id);
  243. $prereqCheck = $lp->prerequisites_match($lpItemId);
  244. if ($prereqCheck) {
  245. $passed = true;
  246. }
  247. if ($passed === false) {
  248. if (!empty($objExerciseTmp->pass_percentage)) {
  249. $passed = ExerciseLib::isSuccessExerciseResult(
  250. $tot,
  251. $exeWeighting,
  252. $objExerciseTmp->pass_percentage
  253. );
  254. } else {
  255. $passed = false;
  256. }
  257. }
  258. if ($passed) {
  259. $statusCondition = ', status = "completed" ';
  260. } else {
  261. $statusCondition = ', status = "failed" ';
  262. }
  263. Display::addFlash(Display::return_message(get_lang('Learning path updated')));
  264. }
  265. $sql = "UPDATE $TBL_LP_ITEM_VIEW
  266. SET score = '".floatval($tot)."'
  267. $statusCondition
  268. WHERE c_id = ".$course_id." AND id = ".$lp_item_view_id;
  269. Database::query($sql);
  270. if (empty($origin)) {
  271. header('Location: '.api_get_path(WEB_CODE_PATH).'exercise/exercise_report.php?exerciseId='.$exercise_id.'&'.api_get_cidreq());
  272. exit;
  273. }
  274. if ($origin === 'tracking_course') {
  275. //Redirect to the course detail in lp
  276. header('Location: '.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?course='.Security::remove_XSS($_GET['course']));
  277. exit;
  278. } else {
  279. // Redirect to the reporting
  280. header(
  281. 'Location: '.api_get_path(WEB_CODE_PATH).'mySpace/myStudents.php?origin='.$origin.'&student='.$student_id.'&details=true&course='.api_get_course_id(
  282. ).'&session_id='.$session_id
  283. );
  284. exit;
  285. }
  286. }
  287. }
  288. $actions = null;
  289. if ($is_allowedToEdit && $origin != 'learnpath') {
  290. // the form
  291. if (api_is_platform_admin() || api_is_course_admin() ||
  292. api_is_course_tutor() || api_is_session_general_coach()
  293. ) {
  294. $actions .= '<a href="exercise.php?'.api_get_cidreq().'">'.
  295. Display::return_icon('back.png', get_lang('Go back to the questions list'), '', ICON_SIZE_MEDIUM).'</a>';
  296. $actions .= '<a href="live_stats.php?'.api_get_cidreq().'&exerciseId='.$exercise_id.'">'.
  297. Display::return_icon('activity_monitor.png', get_lang('Live results'), '', ICON_SIZE_MEDIUM).'</a>';
  298. $actions .= '<a href="stats.php?'.api_get_cidreq().'&exerciseId='.$exercise_id.'">'.
  299. Display::return_icon('statistics.png', get_lang('Report by question'), '', ICON_SIZE_MEDIUM).'</a>';
  300. $actions .= '<a id="export_opener" href="'.api_get_self().'?export_report=1&exerciseId='.$exercise_id.'" >'.
  301. Display::return_icon('save.png', get_lang('Export'), '', ICON_SIZE_MEDIUM).'</a>';
  302. // clean result before a selected date icon
  303. $actions .= Display::url(
  304. Display::return_icon(
  305. 'clean_before_date.png',
  306. get_lang('Clean all results before a selected date'),
  307. '',
  308. ICON_SIZE_MEDIUM
  309. ),
  310. '#',
  311. ['onclick' => 'javascript:display_date_picker()']
  312. );
  313. // clean result before a selected date datepicker popup
  314. $actions .= Display::span(
  315. Display::input(
  316. 'input',
  317. 'datepicker_start',
  318. get_lang('Select a date from the calendar'),
  319. [
  320. 'onmouseover' => 'datepicker_input_mouseover()',
  321. 'id' => 'datepicker_start',
  322. 'onchange' => 'datepicker_input_changed()',
  323. 'readonly' => 'readonly',
  324. ]
  325. ).
  326. Display::button(
  327. 'delete',
  328. get_lang('Delete'),
  329. ['onclick' => 'submit_datepicker()']
  330. ),
  331. ['style' => 'display:none', 'id' => 'datepicker_span']
  332. );
  333. }
  334. } else {
  335. $actions .= '<a href="exercise.php">'.
  336. Display::return_icon(
  337. 'back.png',
  338. get_lang('Go back to the questions list'),
  339. '',
  340. ICON_SIZE_MEDIUM
  341. ).
  342. '</a>';
  343. }
  344. // Deleting an attempt
  345. if (($is_allowedToEdit || $is_tutor || api_is_coach()) &&
  346. isset($_GET['delete']) && $_GET['delete'] === 'delete' &&
  347. !empty($_GET['did']) && $locked == false
  348. ) {
  349. $exe_id = (int) $_GET['did'];
  350. if (!empty($exe_id)) {
  351. $sql = 'DELETE FROM '.$TBL_TRACK_EXERCISES.' WHERE exe_id = '.$exe_id;
  352. Database::query($sql);
  353. $sql = 'DELETE FROM '.$TBL_TRACK_ATTEMPT.' WHERE exe_id = '.$exe_id;
  354. Database::query($sql);
  355. Event::addEvent(
  356. LOG_EXERCISE_ATTEMPT_DELETE,
  357. LOG_EXERCISE_ATTEMPT,
  358. $exe_id,
  359. api_get_utc_datetime()
  360. );
  361. header('Location: exercise_report.php?'.api_get_cidreq().'&exerciseId='.$exercise_id);
  362. exit;
  363. }
  364. }
  365. if ($is_allowedToEdit || $is_tutor) {
  366. $interbreadcrumb[] = [
  367. 'url' => 'exercise.php?'.api_get_cidreq(),
  368. 'name' => get_lang('Tests'),
  369. ];
  370. $nameTools = get_lang('Learner score');
  371. if ($exerciseExists) {
  372. $interbreadcrumb[] = [
  373. 'url' => '#',
  374. 'name' => $objExerciseTmp->selectTitle(true),
  375. ];
  376. }
  377. } else {
  378. $interbreadcrumb[] = [
  379. 'url' => 'exercise.php?'.api_get_cidreq(),
  380. 'name' => get_lang('Tests'),
  381. ];
  382. if ($exerciseExists) {
  383. $nameTools = get_lang('Results and feedback').': '.$objExerciseTmp->selectTitle(true);
  384. }
  385. }
  386. if (($is_allowedToEdit || $is_tutor || api_is_coach()) &&
  387. isset($_GET['a']) && $_GET['a'] === 'close' &&
  388. !empty($_GET['id']) && $locked == false
  389. ) {
  390. // Close the user attempt otherwise left pending
  391. $exe_id = (int) $_GET['id'];
  392. $sql = "UPDATE $TBL_TRACK_EXERCISES SET status = ''
  393. WHERE exe_id = $exe_id AND status = 'incomplete'";
  394. Database::query($sql);
  395. }
  396. Display::display_header($nameTools);
  397. // Clean all results for this test before the selected date
  398. if (($is_allowedToEdit || $is_tutor || api_is_coach()) &&
  399. isset($_GET['delete_before_date']) && $locked == false
  400. ) {
  401. // ask for the date
  402. $check = Security::check_token('get');
  403. if ($check) {
  404. $objExerciseTmp = new Exercise();
  405. if ($objExerciseTmp->read($exercise_id)) {
  406. $count = $objExerciseTmp->cleanResults(
  407. true,
  408. $_GET['delete_before_date'].' 23:59:59'
  409. );
  410. echo Display::return_message(
  411. sprintf(get_lang('XResults and feedbackCleaned'), $count),
  412. 'confirm'
  413. );
  414. }
  415. }
  416. }
  417. // Security token to protect deletion
  418. $token = Security::get_token();
  419. $actions = Display::div($actions, ['class' => 'actions']);
  420. $extra = '<script>
  421. $(function() {
  422. $( "#dialog:ui-dialog" ).dialog( "destroy" );
  423. $( "#dialog-confirm" ).dialog({
  424. autoOpen: false,
  425. show: "blind",
  426. resizable: false,
  427. height:300,
  428. modal: true
  429. });
  430. $("#export_opener").click(function() {
  431. var targetUrl = $(this).attr("href");
  432. $( "#dialog-confirm" ).dialog({
  433. width:400,
  434. height:300,
  435. buttons: {
  436. "'.addslashes(get_lang('Download')).'": function() {
  437. var export_format = $("input[name=export_format]:checked").val();
  438. var extra_data = $("input[name=load_extra_data]:checked").val();
  439. var includeAllUsers = $("input[name=include_all_users]:checked").val();
  440. var attempts = $("input[name=only_best_attempts]:checked").val();
  441. location.href = targetUrl+"&export_format="+export_format+"&extra_data="+extra_data+"&include_all_users="+includeAllUsers+"&only_best_attempts="+attempts;
  442. $( this ).dialog( "close" );
  443. }
  444. }
  445. });
  446. $( "#dialog-confirm" ).dialog("open");
  447. return false;
  448. });
  449. });
  450. </script>';
  451. $extra .= '<div id="dialog-confirm" title="'.get_lang('Please confirm your choice').'">';
  452. $form = new FormValidator(
  453. 'report',
  454. 'post',
  455. null,
  456. null,
  457. ['class' => 'form-vertical']
  458. );
  459. $form->addElement(
  460. 'radio',
  461. 'export_format',
  462. null,
  463. get_lang('CSV export'),
  464. 'csv',
  465. ['id' => 'export_format_csv_label']
  466. );
  467. $form->addElement(
  468. 'radio',
  469. 'export_format',
  470. null,
  471. get_lang('Excel export'),
  472. 'xls',
  473. ['id' => 'export_format_xls_label']
  474. );
  475. $form->addElement(
  476. 'checkbox',
  477. 'load_extra_data',
  478. null,
  479. get_lang('Load extra user fields data (have to be marked as \'Filter\' to appear).'),
  480. '0',
  481. ['id' => 'export_format_xls_label']
  482. );
  483. $form->addElement(
  484. 'checkbox',
  485. 'include_all_users',
  486. null,
  487. get_lang('Include all users'),
  488. '0'
  489. );
  490. $form->addElement(
  491. 'checkbox',
  492. 'only_best_attempts',
  493. null,
  494. get_lang('Only best attempts'),
  495. '0'
  496. );
  497. $form->setDefaults(['export_format' => 'csv']);
  498. $extra .= $form->returnForm();
  499. $extra .= '</div>';
  500. if ($is_allowedToEdit) {
  501. echo $extra;
  502. }
  503. echo $actions;
  504. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_exercise_results&exerciseId='.$exercise_id.'&filter_by_user='.$filter_user.'&'.api_get_cidreq();
  505. $action_links = '';
  506. // Generating group list
  507. $group_list = GroupManager::get_group_list();
  508. $group_parameters = [
  509. 'group_all:'.get_lang('All'),
  510. 'group_none:'.get_lang('none'),
  511. ];
  512. foreach ($group_list as $group) {
  513. $group_parameters[] = $group['id'].':'.$group['name'];
  514. }
  515. if (!empty($group_parameters)) {
  516. $group_parameters = implode(';', $group_parameters);
  517. }
  518. $officialCodeInList = api_get_setting('show_official_code_exercise_result_list');
  519. if ($is_allowedToEdit || $is_tutor) {
  520. // The order is important you need to check the the $column variable in the model.ajax.php file
  521. $columns = [
  522. get_lang('First name'),
  523. get_lang('Last name'),
  524. get_lang('Login'),
  525. get_lang('Group'),
  526. get_lang('Duration').' ('.get_lang('minute').')',
  527. get_lang('Start Date'),
  528. get_lang('End Date'),
  529. get_lang('Score'),
  530. get_lang('IP'),
  531. get_lang('Status'),
  532. get_lang('Learning path'),
  533. get_lang('Detail'),
  534. ];
  535. if ($officialCodeInList === 'true') {
  536. $columns = array_merge([get_lang('Code')], $columns);
  537. }
  538. // Column config
  539. $column_model = [
  540. ['name' => 'firstname', 'index' => 'firstname', 'width' => '50', 'align' => 'left', 'search' => 'true'],
  541. ['name' => 'lastname', 'index' => 'lastname', 'width' => '50', 'align' => 'left', 'formatter' => 'action_formatter', 'search' => 'true'],
  542. [
  543. 'name' => 'login',
  544. 'index' => 'username',
  545. 'width' => '40',
  546. 'align' => 'left',
  547. 'search' => 'true',
  548. 'hidden' => api_get_configuration_value('exercise_attempts_report_show_username') ? 'false' : 'true',
  549. ],
  550. [
  551. 'name' => 'group_name',
  552. 'index' => 'group_id',
  553. 'width' => '40',
  554. 'align' => 'left',
  555. 'search' => 'true',
  556. 'stype' => 'select',
  557. //for the bottom bar
  558. 'searchoptions' => [
  559. 'defaultValue' => 'group_all',
  560. 'value' => $group_parameters,
  561. ],
  562. //for the top bar
  563. 'editoptions' => ['value' => $group_parameters],
  564. ],
  565. ['name' => 'duration', 'index' => 'exe_duration', 'width' => '30', 'align' => 'left', 'search' => 'true'],
  566. ['name' => 'start_date', 'index' => 'start_date', 'width' => '60', 'align' => 'left', 'search' => 'true'],
  567. ['name' => 'exe_date', 'index' => 'exe_date', 'width' => '60', 'align' => 'left', 'search' => 'true'],
  568. ['name' => 'score', 'index' => 'score', 'width' => '50', 'align' => 'center', 'search' => 'true'],
  569. ['name' => 'ip', 'index' => 'user_ip', 'width' => '40', 'align' => 'center', 'search' => 'true'],
  570. [
  571. 'name' => 'status',
  572. 'index' => 'revised',
  573. 'width' => '40',
  574. 'align' => 'left',
  575. 'search' => 'true',
  576. 'stype' => 'select',
  577. //for the bottom bar
  578. 'searchoptions' => [
  579. 'defaultValue' => '',
  580. 'value' => ':'.get_lang('All').';1:'.get_lang('Validated').';0:'.get_lang('Not validated'),
  581. ],
  582. //for the top bar
  583. 'editoptions' => [
  584. 'value' => ':'.get_lang('All').';1:'.get_lang('Validated').';0:'.get_lang(
  585. 'Not validated'
  586. ),
  587. ],
  588. ],
  589. ['name' => 'lp', 'index' => 'orig_lp_id', 'width' => '60', 'align' => 'left', 'search' => 'false'],
  590. ['name' => 'actions', 'index' => 'actions', 'width' => '60', 'align' => 'left', 'search' => 'false', 'sortable' => 'false'],
  591. ];
  592. if ($officialCodeInList === 'true') {
  593. $officialCodeRow = ['name' => 'official_code', 'index' => 'official_code', 'width' => '50', 'align' => 'left', 'search' => 'true'];
  594. $column_model = array_merge([$officialCodeRow], $column_model);
  595. }
  596. $action_links = '
  597. // add username as title in lastname filed - ref 4226
  598. function action_formatter(cellvalue, options, rowObject) {
  599. // rowObject is firstname,lastname,login,... get the third word
  600. var loginx = "'.api_htmlentities(sprintf(get_lang('Login: %s'), ':::'), ENT_QUOTES).'";
  601. var tabLoginx = loginx.split(/:::/);
  602. // tabLoginx[0] is before and tabLoginx[1] is after :::
  603. // may be empty string but is defined
  604. return "<span title=\""+tabLoginx[0]+rowObject[2]+tabLoginx[1]+"\">"+cellvalue+"</span>";
  605. }';
  606. }
  607. $extra_params['autowidth'] = 'true';
  608. $extra_params['height'] = 'auto';
  609. $extra_params['gridComplete'] = "
  610. defaultGroupId = Cookies.get('default_group_".$exercise_id."');
  611. if (typeof defaultGroupId !== 'undefined') {
  612. $('#gs_group_name').val(defaultGroupId);
  613. }
  614. ";
  615. $extra_params['beforeRequest'] = "
  616. var defaultGroupId = $('#gs_group_name').val();
  617. // Load from group menu
  618. if (typeof defaultGroupId !== 'undefined') {
  619. Cookies.set('default_group_".$exercise_id."', defaultGroupId);
  620. } else {
  621. // get from cookies
  622. defaultGroupId = Cookies.get('default_group_".$exercise_id."');
  623. $('#gs_group_name').val(defaultGroupId);
  624. }
  625. if (typeof defaultGroupId !== 'undefined') {
  626. var posted_data = $(\"#results\").jqGrid('getGridParam', 'postData');
  627. var extraFilter = ',{\"field\":\"group_id\",\"op\":\"eq\",\"data\":\"'+ defaultGroupId +'\"}]}';
  628. var filters = posted_data.filters;
  629. var stringObj = new String(filters);
  630. stringObj.replace(']}', extraFilter);
  631. posted_data['group_id_in_toolbar'] = defaultGroupId;
  632. $(this).jqGrid('setGridParam', 'postData', posted_data);
  633. }
  634. ";
  635. $gridJs = Display::grid_js(
  636. 'results',
  637. $url,
  638. $columns,
  639. $column_model,
  640. $extra_params,
  641. [],
  642. $action_links,
  643. true
  644. );
  645. ?>
  646. <script>
  647. function exportExcel()
  648. {
  649. var mya = $("#results").getDataIDs(); // Get All IDs
  650. var data = $("#results").getRowData(mya[0]); // Get First row to get the labels
  651. var colNames = new Array();
  652. var ii = 0;
  653. for (var i in data) {
  654. colNames[ii++] = i;
  655. }
  656. // capture col names
  657. var html = "";
  658. for (i = 0; i < mya.length; i++) {
  659. data = $("#results").getRowData(mya[i]); // get each row
  660. for (j = 0; j < colNames.length; j++) {
  661. html = html + data[colNames[j]] + ","; // output each column as tab delimited
  662. }
  663. html = html + "\n"; // output each row with end of line
  664. }
  665. html = html + "\n"; // end of line at the end
  666. var form = $("#export_report_form");
  667. $("#csvBuffer").attr('value', html);
  668. form.target='_blank';
  669. form.submit();
  670. }
  671. $(function() {
  672. $("#datepicker_start").datepicker({
  673. defaultDate: "",
  674. changeMonth: false,
  675. numberOfMonths: 1
  676. });
  677. <?php
  678. echo $gridJs;
  679. if ($is_allowedToEdit || $is_tutor) {
  680. ?>
  681. $("#results").jqGrid(
  682. 'navGrid',
  683. '#results_pager', {
  684. view:true, edit:false, add:false, del:false, excel:false
  685. },
  686. {height:280, reloadAfterSubmit:false}, // view options
  687. {height:280, reloadAfterSubmit:false}, // edit options
  688. {height:280, reloadAfterSubmit:false}, // add options
  689. {reloadAfterSubmit: false}, // del options
  690. {width:500}, // search options
  691. );
  692. var sgrid = $("#results")[0];
  693. // Update group
  694. var defaultGroupId = Cookies.get('default_group_<?php echo $exercise_id; ?>');
  695. $('#gs_group_name').val(defaultGroupId);
  696. // Adding search options
  697. var options = {
  698. 'stringResult': true,
  699. 'autosearch' : true,
  700. 'searchOnEnter': false,
  701. afterSearch: function () {
  702. $('#gs_group_name').on('change', function() {
  703. var defaultGroupId = $('#gs_group_name').val();
  704. // Save default group id
  705. Cookies.set('default_group_<?php echo $exercise_id; ?>', defaultGroupId);
  706. });
  707. }
  708. }
  709. jQuery("#results").jqGrid('filterToolbar', options);
  710. sgrid.triggerToolbar();
  711. $('#results').on('click', 'a.exercise-recalculate', function (e) {
  712. e.preventDefault();
  713. if (!$(this).data('user') || !$(this).data('exercise') || !$(this).data('id')) {
  714. return;
  715. }
  716. var url = '<?php echo api_get_path(WEB_CODE_PATH); ?>exercise/recalculate.php?<?php echo api_get_cidreq(); ?>';
  717. var recalculateXhr = $.post(url, $(this).data());
  718. $.when(recalculateXhr).done(function (response) {
  719. $('#results').trigger('reloadGrid');
  720. });
  721. });
  722. <?php
  723. }
  724. ?>
  725. });
  726. // datepicker functions
  727. var datapickerInputModified = false;
  728. /**
  729. * return true if the datepicker input has been modified
  730. */
  731. function datepicker_input_changed() {
  732. datapickerInputModified = true;
  733. }
  734. /**
  735. * disply the datepicker calendar on mouse over the input
  736. */
  737. function datepicker_input_mouseover() {
  738. $('#datepicker_start').datepicker( "show" );
  739. }
  740. /**
  741. * display or hide the datepicker input, calendar and button
  742. */
  743. function display_date_picker() {
  744. if (!$('#datepicker_span').is(":visible")) {
  745. $('#datepicker_span').show();
  746. $('#datepicker_start').datepicker( "show" );
  747. } else {
  748. $('#datepicker_start').datepicker( "hide" );
  749. $('#datepicker_span').hide();
  750. }
  751. }
  752. /**
  753. * confirm deletion
  754. */
  755. function submit_datepicker() {
  756. if (datapickerInputModified) {
  757. var dateTypeVar = $('#datepicker_start').datepicker('getDate');
  758. var dateForBDD = $.datepicker.formatDate('yy-mm-dd', dateTypeVar);
  759. // Format the date for confirm box
  760. var dateFormat = $( "#datepicker_start" ).datepicker( "option", "dateFormat" );
  761. var selectedDate = $.datepicker.formatDate(dateFormat, dateTypeVar);
  762. if (confirm("<?php echo convert_double_quote_to_single(get_lang('Are you sure you want to clean results for this test before the selected date ?')).' '; ?>" + selectedDate)) {
  763. self.location.href = "exercise_report.php?<?php echo api_get_cidreq(); ?>&exerciseId=<?php echo $exercise_id; ?>&delete_before_date="+dateForBDD+"&sec_token=<?php echo $token; ?>";
  764. }
  765. }
  766. }
  767. </script>
  768. <form id="export_report_form" method="post" action="exercise_report.php?<?php echo api_get_cidreq(); ?>">
  769. <input type="hidden" name="csvBuffer" id="csvBuffer" value="" />
  770. <input type="hidden" name="export_report" id="export_report" value="1" />
  771. <input type="hidden" name="exerciseId" id="exerciseId" value="<?php echo $exercise_id; ?>" />
  772. </form>
  773. <?php
  774. echo Display::grid_html('results');
  775. Display::display_footer();