exercise_report.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. * @package chamilo.exercise
  6. * @author Julio Montoya <gugli100@gmail.com> jqgrid integration
  7. * Modified by hubert.borderiou (question category)
  8. *
  9. * @todo fix excel export
  10. *
  11. */
  12. /**
  13. * Code
  14. */
  15. // name of the language file that needs to be included
  16. $language_file = array('exercice');
  17. // including the global library
  18. //require_once '../inc/global.inc.php';
  19. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php';
  20. $urlMainExercise = api_get_path(WEB_CODE_PATH).'exercice/';
  21. // Setting the tabs
  22. $this_section = SECTION_COURSES;
  23. $htmlHeadXtra[] = api_get_jqgrid_js();
  24. // Access control
  25. api_protect_course_script(true, false, true);
  26. // including additional libraries
  27. require_once 'exercise.class.php';
  28. require_once 'question.class.php';
  29. require_once 'answer.class.php';
  30. require_once 'hotpotatoes.lib.php';
  31. // need functions of statsutils lib to display previous exercices scores
  32. require_once api_get_path(LIBRARY_PATH).'statsUtils.lib.inc.php';
  33. // document path
  34. $documentPath = api_get_path(SYS_COURSE_PATH).$_course['path']."/document";
  35. /* Constants and variables */
  36. $is_allowedToEdit = api_is_allowed_to_edit(null, true) || api_is_drh();
  37. $is_tutor = api_is_allowed_to_edit(true);
  38. $TBL_QUESTIONS = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  39. $TBL_TRACK_EXERCICES = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  40. $TBL_TRACK_ATTEMPT = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  41. $TBL_TRACK_ATTEMPT_RECORDING = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  42. $TBL_LP_ITEM_VIEW = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  43. $course_id = api_get_course_int_id();
  44. $exercise_id = isset($_REQUEST['exerciseId']) ? intval($_REQUEST['exerciseId']) : null;
  45. $filter_user = isset($_REQUEST['filter_by_user']) ? intval($_REQUEST['filter_by_user']) : null;
  46. $gradebook = isset($_REQUEST['gradebook']) ? Security::remove_XSS($_REQUEST['gradebook']) : null;
  47. $locked = api_resource_is_locked_by_gradebook($exercise_id, LINK_EXERCISE);
  48. if (empty($exercise_id)) {
  49. api_not_allowed(true);
  50. }
  51. if (!$is_allowedToEdit) {
  52. api_not_allowed(true);
  53. }
  54. // @todo check if the $parameters is used
  55. if (!empty($exercise_id)) {
  56. $parameters['exerciseId'] = $exercise_id;
  57. }
  58. if (!empty($_GET['path'])) {
  59. $parameters['path'] = Security::remove_XSS($_GET['path']);
  60. }
  61. if (!empty($_REQUEST['export_report']) && $_REQUEST['export_report'] == '1') {
  62. if (api_is_platform_admin() || api_is_course_admin() || api_is_course_tutor() || api_is_course_coach()) {
  63. $load_extra_data = false;
  64. if (isset($_REQUEST['extra_data']) && $_REQUEST['extra_data'] == 1) {
  65. $load_extra_data = true;
  66. }
  67. require_once 'exercise_result.class.php';
  68. switch ($_GET['export_format']) {
  69. case 'xls':
  70. $export = new ExerciseResult();
  71. $export->exportCompleteReportXLS(
  72. $documentPath,
  73. null,
  74. $load_extra_data,
  75. null,
  76. $_GET['exerciseId'],
  77. $_GET['hotpotato_name']
  78. );
  79. exit;
  80. break;
  81. case 'csv':
  82. default:
  83. $export = new ExerciseResult();
  84. $export->exportCompleteReportCSV(
  85. $documentPath,
  86. null,
  87. $load_extra_data,
  88. null,
  89. $_GET['exerciseId'],
  90. $_GET['hotpotato_name']
  91. );
  92. exit;
  93. break;
  94. }
  95. } else {
  96. api_not_allowed(true);
  97. }
  98. }
  99. $actions = null;
  100. if (isset($origin) && $origin == 'learnpath') {
  101. $actions .= '<a href="exercice.php">'.Display :: return_icon(
  102. 'back.png',
  103. get_lang('GoBackToQuestionList'),
  104. '',
  105. ICON_SIZE_MEDIUM
  106. ).'</a>';
  107. } else {
  108. if ($is_allowedToEdit) {
  109. // the form
  110. if (api_is_platform_admin() || api_is_course_admin() || api_is_course_tutor() || api_is_course_coach()) {
  111. // @todo check if $path is used
  112. $path = isset($_GET['path']) ? Security::remove_XSS($_GET['path']) : null;
  113. $actions .= '<a href="admin.php?exerciseId='.intval($_GET['exerciseId']).'">'.Display :: return_icon('back.png',get_lang('GoBackToQuestionList'),'',ICON_SIZE_MEDIUM).'</a>';
  114. $actions .= '<a href="live_stats.php?'.api_get_cidreq().'&exerciseId='.$exercise_id.'">'.Display :: return_icon('activity_monitor.png',get_lang('LiveResults'), '', ICON_SIZE_MEDIUM ).'</a>';
  115. $actions .= '<a href="stats.php?'.api_get_cidreq().'&exerciseId='.$exercise_id.'">'.Display :: return_icon('statistics.png',get_lang('ReportByQuestion'), '', ICON_SIZE_MEDIUM).'</a>';
  116. $actions .= '<a id="export_opener" href="'.api_get_self().'?export_report=1&hotpotato_name='.$path.'&exerciseId='.intval($_GET['exerciseId']).'" >'.
  117. Display::return_icon('save.png', get_lang('Export'), '', ICON_SIZE_MEDIUM).'</a>';
  118. $actions .= '<a href="recalculate_scores.php?'.api_get_cidreq().'&exerciseId='.$exercise_id.'">'.Display :: return_icon('history.png',get_lang('RecalculateResults'), '', ICON_SIZE_MEDIUM).'</a>';
  119. }
  120. }
  121. }
  122. //Deleting an attempt
  123. if (($is_allowedToEdit || $is_tutor || api_is_coach()) && isset($_GET['delete']) && $_GET['delete'] == 'delete' && !empty ($_GET['did']) && $locked == false
  124. ) {
  125. $exe_id = intval($_GET['did']);
  126. if (!empty($exe_id)) {
  127. $sql = 'DELETE FROM '.$TBL_TRACK_EXERCICES.' WHERE exe_id = '.$exe_id;
  128. Database::query($sql);
  129. $sql = 'DELETE FROM '.$TBL_TRACK_ATTEMPT.' WHERE exe_id = '.$exe_id;
  130. Database::query($sql);
  131. header(
  132. 'Location: exercise_report.php?cidReq='.Security::remove_XSS($_GET['cidReq']).'&exerciseId='.$exercise_id
  133. );
  134. exit;
  135. }
  136. }
  137. if ($is_allowedToEdit || $is_tutor) {
  138. $nameTools = get_lang('StudentScore');
  139. $interbreadcrumb[] = array("url" => "exercice.php?gradebook=$gradebook", "name" => get_lang('Exercices'));
  140. $objExerciseTmp = new Exercise();
  141. if ($objExerciseTmp->read($exercise_id)) {
  142. $interbreadcrumb[] = array("url" => "admin.php?exerciseId=".$exercise_id, "name" => $objExerciseTmp->name);
  143. }
  144. } else {
  145. $interbreadcrumb[] = array("url" => "exercice.php?gradebook=$gradebook", "name" => get_lang('Exercices'));
  146. $objExerciseTmp = new Exercise();
  147. if ($objExerciseTmp->read($exercise_id)) {
  148. $nameTools = get_lang('Results').': '.$objExerciseTmp->name;
  149. }
  150. }
  151. Display :: display_header($nameTools);
  152. $actions = Display::div($actions, array('class' => 'actions'));
  153. $extra = '<script>
  154. $(document).ready(function() {
  155. $( "#dialog:ui-dialog" ).dialog( "destroy" );
  156. $( "#dialog-confirm" ).dialog({
  157. autoOpen: false,
  158. show: "blind",
  159. resizable: false,
  160. height:300,
  161. modal: true
  162. });
  163. $("#export_opener").click(function() {
  164. var targetUrl = $(this).attr("href");
  165. $( "#dialog-confirm" ).dialog({
  166. width:400,
  167. height:300,
  168. buttons: {
  169. "'.addslashes(get_lang('Download')).'": function() {
  170. var export_format = $("input[name=export_format]:checked").val();
  171. var extra_data = $("input[name=load_extra_data]:checked").val();
  172. location.href = targetUrl+"&export_format="+export_format+"&extra_data="+extra_data;
  173. $( this ).dialog( "close" );
  174. },
  175. }
  176. });
  177. $( "#dialog-confirm" ).dialog("open");
  178. return false;
  179. });
  180. });
  181. </script>';
  182. $extra .= '<div id="dialog-confirm" title="'.get_lang("ConfirmYourChoice").'">';
  183. $form = new FormValidator('report', 'post', null, null, array('class' => 'form-vertical'));
  184. $form->addElement(
  185. 'radio',
  186. 'export_format',
  187. null,
  188. get_lang('ExportAsCSV'),
  189. 'csv',
  190. array('id' => 'export_format_csv_label')
  191. );
  192. $form->addElement(
  193. 'radio',
  194. 'export_format',
  195. null,
  196. get_lang('ExportAsXLS'),
  197. 'xls',
  198. array('id' => 'export_format_xls_label')
  199. );
  200. $form->addElement(
  201. 'checkbox',
  202. 'load_extra_data',
  203. null,
  204. get_lang('LoadExtraData'),
  205. '0',
  206. array('id' => 'export_format_xls_label')
  207. );
  208. $form->setDefaults(array('export_format' => 'csv'));
  209. $extra .= $form->return_form();
  210. $extra .= '</div>';
  211. if ($is_allowedToEdit) {
  212. echo $extra;
  213. }
  214. echo $actions;
  215. $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();
  216. $action_links = '';
  217. //Generating group list
  218. $group_list = GroupManager::get_group_list();
  219. $group_parameters = array('group_all:'.get_lang('All'), 'group_none:'.get_lang('None'));
  220. foreach ($group_list as $group) {
  221. $group_parameters[] = $group['id'].':'.$group['name'];
  222. }
  223. if (!empty($group_parameters)) {
  224. $group_parameters = implode(';', $group_parameters);
  225. }
  226. if ($is_allowedToEdit || $is_tutor) {
  227. //The order is important you need to check the the $column variable in the model.ajax.php file
  228. $columns = array(
  229. get_lang('FirstName'),
  230. get_lang('LastName'),
  231. get_lang('LoginName'),
  232. get_lang('Group'),
  233. get_lang('Duration').' ('.get_lang('MinMinute').')',
  234. get_lang('StartDate'),
  235. get_lang('EndDate'),
  236. get_lang('Score'),
  237. get_lang('Status'),
  238. get_lang('ToolLearnpath'),
  239. get_lang('Actions')
  240. );
  241. //Column config
  242. $column_model = array(
  243. array('name' => 'firstname', 'index' => 'firstname', 'width' => '50', 'align' => 'left', 'search' => 'true'),
  244. array(
  245. 'name' => 'lastname',
  246. 'index' => 'lastname',
  247. 'width' => '50',
  248. 'align' => 'left',
  249. 'formatter' => 'action_formatter',
  250. 'search' => 'true'
  251. ),
  252. array(
  253. 'name' => 'login',
  254. 'index' => 'username',
  255. 'width' => '40',
  256. 'align' => 'left',
  257. 'search' => 'true',
  258. 'hidden' => 'true'
  259. ),
  260. array(
  261. 'name' => 'group_name',
  262. 'index' => 'group_id',
  263. 'width' => '40',
  264. 'align' => 'left',
  265. 'search' => 'true',
  266. 'stype' => 'select',
  267. //for the bottom bar
  268. 'searchoptions' => array(
  269. 'defaultValue' => 'group_all',
  270. 'value' => $group_parameters
  271. ),
  272. //for the top bar
  273. 'editoptions' => array('value' => $group_parameters)
  274. ),
  275. array('name' => 'duration', 'index' => 'exe_duration', 'width' => '30', 'align' => 'left', 'search' => 'true'),
  276. array('name' => 'start_date', 'index' => 'start_date', 'width' => '60', 'align' => 'left', 'search' => 'true'),
  277. array('name' => 'exe_date', 'index' => 'exe_date', 'width' => '60', 'align' => 'left', 'search' => 'true'),
  278. array('name' => 'score', 'index' => 'exe_result', 'width' => '50', 'align' => 'left', 'search' => 'true'),
  279. array(
  280. 'name' => 'status',
  281. 'index' => 'revised',
  282. 'width' => '40',
  283. 'align' => 'left',
  284. 'search' => 'true',
  285. 'stype' => 'select',
  286. //for the bottom bar
  287. 'searchoptions' => array(
  288. 'defaultValue' => '',
  289. 'value' => ':'.get_lang('All').';1:'.get_lang('Validated').';0:'.get_lang('NotValidated')
  290. ),
  291. //for the top bar
  292. 'editoptions' => array(
  293. 'value' => ':'.get_lang('All').';1:'.get_lang('Validated').';0:'.get_lang(
  294. 'NotValidated'
  295. )
  296. )
  297. ),
  298. array('name' => 'lp', 'index' => 'lp', 'width' => '60', 'align' => 'left', 'search' => 'false'),
  299. array('name' => 'actions', 'index' => 'actions', 'width' => '60', 'align' => 'left', 'search' => 'false')
  300. );
  301. $action_links = '
  302. // add username as title in lastname filed - ref 4226
  303. function action_formatter(cellvalue, options, rowObject) {
  304. // rowObject is firstname,lastname,login,... get the third word
  305. var loginx = "'.api_htmlentities(sprintf(get_lang("LoginX"), ":::"), ENT_QUOTES).'";
  306. var tabLoginx = loginx.split(/:::/);
  307. // tabLoginx[0] is before and tabLoginx[1] is after :::
  308. // may be empty string but is defined
  309. return "<span title=\""+tabLoginx[0]+rowObject[2]+tabLoginx[1]+"\">"+cellvalue+"</span>";
  310. }';
  311. }
  312. //Autowidth
  313. $extra_params['autowidth'] = 'true';
  314. //height auto
  315. $extra_params['height'] = 'auto';
  316. ?>
  317. <script>
  318. function setSearchSelect(columnName) {
  319. $("#results").jqGrid('setColProp', columnName, {
  320. searchoptions:{
  321. dataInit:function (el) {
  322. $("option[value='1']", el).attr("selected", "selected");
  323. setTimeout(function () {
  324. $(el).trigger('change');
  325. }, 1000);
  326. }
  327. }
  328. });
  329. }
  330. function exportExcel() {
  331. var mya = new Array();
  332. mya = $("#results").getDataIDs(); // Get All IDs
  333. var data = $("#results").getRowData(mya[0]); // Get First row to get the labels
  334. var colNames = new Array();
  335. var ii = 0;
  336. for (var i in data) {
  337. colNames[ii++] = i;
  338. } // capture col names
  339. var html = "";
  340. for (i = 0; i < mya.length; i++) {
  341. data = $("#results").getRowData(mya[i]); // get each row
  342. for (j = 0; j < colNames.length; j++) {
  343. html = html + data[colNames[j]] + ","; // output each column as tab delimited
  344. }
  345. html = html + "\n"; // output each row with end of line
  346. }
  347. html = html + "\n"; // end of line at the end
  348. var form = $("#export_report_form");
  349. $("#csvBuffer").attr('value', html);
  350. form.target = '_blank';
  351. form.submit();
  352. }
  353. $(function () {
  354. <?php
  355. echo Display::grid_js('results', $url, $columns, $column_model, $extra_params, array(), $action_links, true);
  356. if ($is_allowedToEdit || $is_tutor) {
  357. ?>
  358. $("#results").jqGrid(
  359. 'navGrid',
  360. '#results_pager',
  361. { view:true, edit:false, add:false, del:false, excel:false },
  362. { height:280, reloadAfterSubmit:false }, // view options
  363. { height:280, reloadAfterSubmit:false }, // edit options
  364. { height:280, reloadAfterSubmit:false }, // add options
  365. { reloadAfterSubmit:false }, // del options
  366. { width:500 } // search options
  367. );
  368. //Adding search options
  369. var options = {
  370. 'stringResult': true,
  371. 'autosearch' : true,
  372. 'searchOnEnter':false
  373. }
  374. jQuery("#results").jqGrid('filterToolbar', options);
  375. var sgrid = $("#results")[0];
  376. sgrid.triggerToolbar();
  377. <?php } ?>
  378. });
  379. </script>
  380. <form id="export_report_form" method="post" action="exercise_report.php">
  381. <input type="hidden" name="csvBuffer" id="csvBuffer" value=""/>
  382. <input type="hidden" name="export_report" id="export_report" value="1"/>
  383. <input type="hidden" name="exerciseId" id="exerciseId" value="<?php echo $exercise_id ?>"/>
  384. </form>
  385. <?php
  386. echo Display::grid_html('results');
  387. Display :: display_footer();