exercise_report.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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()) &&
  124. isset($_GET['delete']) && $_GET['delete'] == 'delete' && !empty ($_GET['did']) && $locked == false
  125. ) {
  126. $exe_id = intval($_GET['did']);
  127. if (!empty($exe_id)) {
  128. $sql = 'DELETE FROM '.$TBL_TRACK_EXERCICES.' WHERE exe_id = '.$exe_id;
  129. Database::query($sql);
  130. $sql = 'DELETE FROM '.$TBL_TRACK_ATTEMPT.' WHERE exe_id = '.$exe_id;
  131. Database::query($sql);
  132. header('Location: exercise_report.php?'.api_get_cidreq().'&exerciseId='.$exercise_id);
  133. exit;
  134. }
  135. }
  136. if ($is_allowedToEdit || $is_tutor) {
  137. $nameTools = get_lang('StudentScore');
  138. $interbreadcrumb[] = array("url" => "exercice.php?gradebook=$gradebook", "name" => get_lang('Exercices'));
  139. $objExerciseTmp = new Exercise();
  140. if ($objExerciseTmp->read($exercise_id)) {
  141. $interbreadcrumb[] = array("url" => "admin.php?exerciseId=".$exercise_id, "name" => $objExerciseTmp->name);
  142. }
  143. } else {
  144. $interbreadcrumb[] = array("url" => "exercice.php?gradebook=$gradebook", "name" => get_lang('Exercices'));
  145. $objExerciseTmp = new Exercise();
  146. if ($objExerciseTmp->read($exercise_id)) {
  147. $nameTools = get_lang('Results').': '.$objExerciseTmp->name;
  148. }
  149. }
  150. Display :: display_header($nameTools);
  151. $actions = Display::div($actions, array('class' => 'actions'));
  152. $extra = '<script>
  153. $(document).ready(function() {
  154. $( "#dialog:ui-dialog" ).dialog( "destroy" );
  155. $( "#dialog-confirm" ).dialog({
  156. autoOpen: false,
  157. show: "blind",
  158. resizable: false,
  159. height:300,
  160. modal: true
  161. });
  162. $("#export_opener").click(function() {
  163. var targetUrl = $(this).attr("href");
  164. $( "#dialog-confirm" ).dialog({
  165. width:400,
  166. height:300,
  167. buttons: {
  168. "'.addslashes(get_lang('Download')).'": function() {
  169. var export_format = $("input[name=export_format]:checked").val();
  170. var extra_data = $("input[name=load_extra_data]:checked").val();
  171. location.href = targetUrl+"&export_format="+export_format+"&extra_data="+extra_data;
  172. $( this ).dialog( "close" );
  173. },
  174. }
  175. });
  176. $( "#dialog-confirm" ).dialog("open");
  177. return false;
  178. });
  179. });
  180. </script>';
  181. $extra .= '<div id="dialog-confirm" title="'.get_lang("ConfirmYourChoice").'">';
  182. $form = new FormValidator('report', 'post', null, null, array('class' => 'form-vertical'));
  183. $form->addElement(
  184. 'radio',
  185. 'export_format',
  186. null,
  187. get_lang('ExportAsCSV'),
  188. 'csv',
  189. array('id' => 'export_format_csv_label')
  190. );
  191. $form->addElement(
  192. 'radio',
  193. 'export_format',
  194. null,
  195. get_lang('ExportAsXLS'),
  196. 'xls',
  197. array('id' => 'export_format_xls_label')
  198. );
  199. $form->addElement(
  200. 'checkbox',
  201. 'load_extra_data',
  202. null,
  203. get_lang('LoadExtraData'),
  204. '0',
  205. array('id' => 'export_format_xls_label')
  206. );
  207. $form->setDefaults(array('export_format' => 'csv'));
  208. $extra .= $form->return_form();
  209. $extra .= '</div>';
  210. if ($is_allowedToEdit) {
  211. echo $extra;
  212. }
  213. echo $actions;
  214. $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();
  215. $action_links = '';
  216. // Generating group list
  217. $group_list = GroupManager::get_group_list();
  218. $group_parameters = array('group_all:'.get_lang('All'), 'group_none:'.get_lang('None'));
  219. foreach ($group_list as $group) {
  220. $group_parameters[] = $group['id'].':'.$group['name'];
  221. }
  222. if (!empty($group_parameters)) {
  223. $group_parameters = implode(';', $group_parameters);
  224. }
  225. if ($is_allowedToEdit || $is_tutor) {
  226. //The order is important you need to check the the $column variable in the model.ajax.php file
  227. $columns = array(
  228. get_lang('FirstName'),
  229. get_lang('LastName'),
  230. get_lang('LoginName'),
  231. get_lang('Group'),
  232. get_lang('Duration').' ('.get_lang('MinMinute').')',
  233. get_lang('StartDate'),
  234. get_lang('EndDate'),
  235. get_lang('Score'),
  236. get_lang('Status'),
  237. get_lang('ToolLearnpath'),
  238. get_lang('Actions')
  239. );
  240. //Column config
  241. $column_model = array(
  242. array('name' => 'firstname', 'index' => 'firstname', 'width' => '50', 'align' => 'left', 'search' => 'true'),
  243. array(
  244. 'name' => 'lastname',
  245. 'index' => 'lastname',
  246. 'width' => '50',
  247. 'align' => 'left',
  248. 'formatter' => 'action_formatter',
  249. 'search' => 'true'
  250. ),
  251. array(
  252. 'name' => 'login',
  253. 'index' => 'username',
  254. 'width' => '40',
  255. 'align' => 'left',
  256. 'search' => 'true',
  257. 'hidden' => 'true'
  258. ),
  259. array(
  260. 'name' => 'group_name',
  261. 'index' => 'group_id',
  262. 'width' => '40',
  263. 'align' => 'left',
  264. 'search' => 'true',
  265. 'stype' => 'select',
  266. //for the bottom bar
  267. 'searchoptions' => array(
  268. 'defaultValue' => 'group_all',
  269. 'value' => $group_parameters
  270. ),
  271. //for the top bar
  272. 'editoptions' => array('value' => $group_parameters)
  273. ),
  274. array('name' => 'duration', 'index' => 'exe_duration', 'width' => '30', 'align' => 'left', 'search' => 'true'),
  275. array('name' => 'start_date', 'index' => 'start_date', 'width' => '60', 'align' => 'left', 'search' => 'true'),
  276. array('name' => 'exe_date', 'index' => 'exe_date', 'width' => '60', 'align' => 'left', 'search' => 'true'),
  277. array('name' => 'score', 'index' => 'exe_result', 'width' => '50', 'align' => 'left', 'search' => 'true'),
  278. array(
  279. 'name' => 'status',
  280. 'index' => 'revised',
  281. 'width' => '40',
  282. 'align' => 'left',
  283. 'search' => 'true',
  284. 'stype' => 'select',
  285. //for the bottom bar
  286. 'searchoptions' => array(
  287. 'defaultValue' => '',
  288. 'value' => ':'.get_lang('All').';1:'.get_lang('Validated').';0:'.get_lang('NotValidated')
  289. ),
  290. //for the top bar
  291. 'editoptions' => array(
  292. 'value' => ':'.get_lang('All').';1:'.get_lang('Validated').';0:'.get_lang(
  293. 'NotValidated'
  294. )
  295. )
  296. ),
  297. array('name' => 'lp', 'index' => 'lp', 'width' => '60', 'align' => 'left', 'search' => 'false'),
  298. array('name' => 'actions', 'index' => 'actions', 'width' => '60', 'align' => 'left', 'search' => 'false')
  299. );
  300. $action_links = '
  301. // add username as title in lastname filed - ref 4226
  302. function action_formatter(cellvalue, options, rowObject) {
  303. // rowObject is firstname,lastname,login,... get the third word
  304. var loginx = "'.api_htmlentities(sprintf(get_lang("LoginX"), ":::"), ENT_QUOTES).'";
  305. var tabLoginx = loginx.split(/:::/);
  306. // tabLoginx[0] is before and tabLoginx[1] is after :::
  307. // may be empty string but is defined
  308. return "<span title=\""+tabLoginx[0]+rowObject[2]+tabLoginx[1]+"\">"+cellvalue+"</span>";
  309. }';
  310. }
  311. //Autowidth
  312. $extra_params['autowidth'] = 'true';
  313. //height auto
  314. $extra_params['height'] = 'auto';
  315. ?>
  316. <script>
  317. function setSearchSelect(columnName) {
  318. $("#results").jqGrid('setColProp', columnName, {
  319. searchoptions:{
  320. dataInit:function (el) {
  321. $("option[value='1']", el).attr("selected", "selected");
  322. setTimeout(function () {
  323. $(el).trigger('change');
  324. }, 1000);
  325. }
  326. }
  327. });
  328. }
  329. function exportExcel() {
  330. var mya = new Array();
  331. mya = $("#results").getDataIDs(); // Get All IDs
  332. var data = $("#results").getRowData(mya[0]); // Get First row to get the labels
  333. var colNames = new Array();
  334. var ii = 0;
  335. for (var i in data) {
  336. colNames[ii++] = i;
  337. } // capture col names
  338. var html = "";
  339. for (i = 0; i < mya.length; i++) {
  340. data = $("#results").getRowData(mya[i]); // get each row
  341. for (j = 0; j < colNames.length; j++) {
  342. html = html + data[colNames[j]] + ","; // output each column as tab delimited
  343. }
  344. html = html + "\n"; // output each row with end of line
  345. }
  346. html = html + "\n"; // end of line at the end
  347. var form = $("#export_report_form");
  348. $("#csvBuffer").attr('value', html);
  349. form.target = '_blank';
  350. form.submit();
  351. }
  352. $(function () {
  353. <?php
  354. echo Display::grid_js('results', $url, $columns, $column_model, $extra_params, array(), $action_links, true);
  355. if ($is_allowedToEdit || $is_tutor) {
  356. ?>
  357. $("#results").jqGrid(
  358. 'navGrid',
  359. '#results_pager',
  360. { view:true, edit:false, add:false, del:false, excel:false },
  361. { height:280, reloadAfterSubmit:false }, // view options
  362. { height:280, reloadAfterSubmit:false }, // edit options
  363. { height:280, reloadAfterSubmit:false }, // add options
  364. { reloadAfterSubmit:false }, // del options
  365. { width:500 } // search options
  366. );
  367. //Adding search options
  368. var options = {
  369. 'stringResult': true,
  370. 'autosearch' : true,
  371. 'searchOnEnter':false
  372. }
  373. jQuery("#results").jqGrid('filterToolbar', options);
  374. var sgrid = $("#results")[0];
  375. sgrid.triggerToolbar();
  376. <?php } ?>
  377. });
  378. </script>
  379. <form id="export_report_form" method="post" action="exercise_report.php">
  380. <input type="hidden" name="csvBuffer" id="csvBuffer" value=""/>
  381. <input type="hidden" name="export_report" id="export_report" value="1"/>
  382. <input type="hidden" name="exerciseId" id="exerciseId" value="<?php echo $exercise_id ?>"/>
  383. </form>
  384. <?php
  385. echo Display::grid_html('results');
  386. Display :: display_footer();