exercise_report.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 '../gradebook/lib/be.inc.php';
  20. // Setting the tabs
  21. $this_section = SECTION_COURSES;
  22. $htmlHeadXtra[] = api_get_jqgrid_js();
  23. // Access control
  24. api_protect_course_script(true, false, true);
  25. // including additional libraries
  26. require_once 'exercise.class.php';
  27. require_once 'exercise.lib.php';
  28. require_once 'question.class.php';
  29. require_once 'answer.class.php';
  30. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  31. require_once 'hotpotatoes.lib.php';
  32. // need functions of statsutils lib to display previous exercices scores
  33. require_once api_get_path(LIBRARY_PATH) . 'statsUtils.lib.inc.php';
  34. // document path
  35. $documentPath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . "/document";
  36. /* Constants and variables */
  37. $is_allowedToEdit = api_is_allowed_to_edit(null, true) || api_is_drh();
  38. $is_tutor = api_is_allowed_to_edit(true);
  39. $TBL_QUESTIONS = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  40. $TBL_TRACK_EXERCICES = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  41. $TBL_TRACK_ATTEMPT = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  42. $TBL_TRACK_ATTEMPT_RECORDING= Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  43. $TBL_LP_ITEM_VIEW = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  44. $course_id = api_get_course_int_id();
  45. $exercise_id = isset($_REQUEST['exerciseId']) ? intval($_REQUEST['exerciseId']) : null;
  46. $filter_user = isset($_REQUEST['filter_by_user']) ? intval($_REQUEST['filter_by_user']) : 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. if (!empty($exercise_id))
  55. $parameters['exerciseId'] = $exercise_id;
  56. if (!empty($_GET['path'])) {
  57. $parameters['path'] = Security::remove_XSS($_GET['path']);
  58. }
  59. if (!empty($_REQUEST['export_report']) && $_REQUEST['export_report'] == '1') {
  60. if (api_is_platform_admin() || api_is_course_admin() || api_is_course_tutor() || api_is_course_coach()) {
  61. $load_extra_data = false;
  62. if (isset($_REQUEST['extra_data']) && $_REQUEST['extra_data'] == 1) {
  63. $load_extra_data = true;
  64. }
  65. require_once 'exercise_result.class.php';
  66. switch ($_GET['export_format']) {
  67. case 'xls' :
  68. $export = new ExerciseResult();
  69. $export->exportCompleteReportXLS($documentPath, null, $load_extra_data, null, $_GET['exerciseId'], $_GET['hotpotato_name']);
  70. exit;
  71. break;
  72. case 'csv' :
  73. default :
  74. $export = new ExerciseResult();
  75. $export->exportCompleteReportCSV($documentPath, null, $load_extra_data, null, $_GET['exerciseId'], $_GET['hotpotato_name']);
  76. exit;
  77. break;
  78. }
  79. } else {
  80. api_not_allowed(true);
  81. }
  82. }
  83. //Send student email @todo move this code in a class, library
  84. if ($_REQUEST['comments'] == 'update' && ($is_allowedToEdit || $is_tutor) && $_GET['exeid']== strval(intval($_GET['exeid']))) {
  85. $id = intval($_GET['exeid']); //filtered by post-condition
  86. $track_exercise_info = get_exercise_track_exercise_info($id);
  87. if (empty($track_exercise_info)) {
  88. api_not_allowed();
  89. }
  90. $test = $track_exercise_info['title'];
  91. $student_id = $track_exercise_info['exe_user_id'];
  92. $session_id = $track_exercise_info['session_id'];
  93. $lp_id = $track_exercise_info['orig_lp_id'];
  94. //$lp_item_id = $track_exercise_info['orig_lp_item_id'];
  95. $lp_item_view_id = $track_exercise_info['orig_lp_item_view_id'];
  96. $course_info = api_get_course_info();
  97. // Teacher data
  98. $teacher_info = api_get_user_info(api_get_user_id());
  99. $from_name = api_get_person_name($teacher_info['firstname'], $teacher_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  100. $url = api_get_path(WEB_CODE_PATH) . 'exercice/exercise_report.php?' . api_get_cidreq() . '&id_session='.$session_id.'&exerciseId='.$exercise_id;
  101. $my_post_info = array();
  102. $post_content_id = array();
  103. $comments_exist = false;
  104. foreach ($_POST as $key_index => $key_value) {
  105. $my_post_info = explode('_',$key_index);
  106. $post_content_id[]=$my_post_info[1];
  107. if ($my_post_info[0]=='comments') {
  108. $comments_exist=true;
  109. }
  110. }
  111. $loop_in_track=($comments_exist===true) ? (count($_POST)/2) : count($_POST);
  112. $array_content_id_exe=array();
  113. if ($comments_exist===true) {
  114. $array_content_id_exe = array_slice($post_content_id,$loop_in_track);
  115. } else {
  116. $array_content_id_exe = $post_content_id;
  117. }
  118. for ($i=0;$i<$loop_in_track;$i++) {
  119. $my_marks = Database::escape_string($_POST['marks_'.$array_content_id_exe[$i]]);
  120. $contain_comments = Database::escape_string($_POST['comments_'.$array_content_id_exe[$i]]);
  121. if (isset($contain_comments)) {
  122. $my_comments = Database::escape_string($_POST['comments_'.$array_content_id_exe[$i]]);
  123. } else {
  124. $my_comments = '';
  125. }
  126. $my_questionid = intval($array_content_id_exe[$i]);
  127. $sql = "SELECT question from $TBL_QUESTIONS WHERE c_id = $course_id AND id = '$my_questionid'";
  128. $result =Database::query($sql);
  129. Database::result($result,0,"question");
  130. $query = "UPDATE $TBL_TRACK_ATTEMPT SET marks = '$my_marks', teacher_comment = '$my_comments' WHERE question_id = ".$my_questionid." AND exe_id=".$id;
  131. Database::query($query);
  132. //Saving results in the track recording table
  133. $recording_changes = 'INSERT INTO '.$TBL_TRACK_ATTEMPT_RECORDING.' (exe_id, question_id, marks, insert_date, author, teacher_comment)
  134. VALUES ('."'$id','".$my_questionid."','$my_marks','".api_get_utc_datetime()."','".api_get_user_id()."'".',"'.$my_comments.'")';
  135. Database::query($recording_changes);
  136. }
  137. $qry = 'SELECT DISTINCT question_id, marks FROM ' . $TBL_TRACK_ATTEMPT . ' WHERE exe_id = '.$id .' GROUP BY question_id';
  138. $res = Database::query($qry);
  139. $tot = 0;
  140. while ($row = Database :: fetch_array($res, 'ASSOC')) {
  141. $tot += $row['marks'];
  142. }
  143. $totquery = "UPDATE $TBL_TRACK_EXERCICES SET exe_result = '".floatval($tot)."' WHERE exe_id = ".$id;
  144. Database::query($totquery);
  145. if (isset($_POST['send_notification'])) {
  146. //@todo move this somewhere else
  147. $subject = get_lang('ExamSheetVCC');
  148. $message = '<p>'.get_lang('DearStudentEmailIntroduction') . '</p><p>'.get_lang('AttemptVCC');
  149. $message .= '<h3>'.get_lang('CourseName'). '</h3><p>'.Security::remove_XSS($course_info['name']).'';
  150. $message .= '<h3>'.get_lang('Exercise') . '</h3><p>'.Security::remove_XSS($test);
  151. //Only for exercises not in a LP
  152. if ($lp_id == 0) {
  153. $message .= '<p>'.get_lang('ClickLinkToViewComment') . ' <a href="#url#">#url#</a><br />';
  154. }
  155. $message .= '<p>'.get_lang('Regards').'</p>';
  156. $message .= $from_name;
  157. $message = str_replace("#test#", Security::remove_XSS($test), $message);
  158. $message = str_replace("#url#", $url, $message);
  159. MessageManager::send_message_simple($student_id, $subject, $message, api_get_user_id());
  160. }
  161. //Updating LP score here
  162. if (in_array($origin, array ('tracking_course','user_course','correct_exercise_in_lp'))) {
  163. $sql_update_score = "UPDATE $TBL_LP_ITEM_VIEW SET score = '" . floatval($tot) . "' WHERE c_id = ".$course_id." AND id = " .$lp_item_view_id;
  164. Database::query($sql_update_score);
  165. if ($origin == 'tracking_course') {
  166. //Redirect to the course detail in lp
  167. header('location: exercice.php?course=' . Security :: remove_XSS($_GET['course']));
  168. exit;
  169. } else {
  170. //Redirect to the reporting
  171. header('location: ../mySpace/myStudents.php?origin=' . $origin . '&student=' . $student_id . '&details=true&course=' . $course_id.'&session_id='.$session_id);
  172. exit;
  173. }
  174. }
  175. }
  176. if ($is_allowedToEdit && $origin != 'learnpath') {
  177. // the form
  178. if (api_is_platform_admin() || api_is_course_admin() || api_is_course_tutor() || api_is_course_coach()) {
  179. $actions .= '<a href="admin.php?exerciseId='.intval($_GET['exerciseId']).'">' . Display :: return_icon('back.png', get_lang('GoBackToQuestionList'),'',ICON_SIZE_MEDIUM).'</a>';
  180. $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>';
  181. $actions .='<a href="stats.php?' . api_get_cidreq() . '&exerciseId='.$exercise_id.'">'.Display :: return_icon('statistics.png', get_lang('ReportByQuestion'),'', ICON_SIZE_MEDIUM).'</a>';
  182. $actions .= '<a id="export_opener" href="'.api_get_self().'?export_report=1&hotpotato_name='.Security::remove_XSS($_GET['path']).'&exerciseId='.intval($_GET['exerciseId']).'" >'.
  183. Display::return_icon('save.png', get_lang('Export'),'',ICON_SIZE_MEDIUM).'</a>';
  184. }
  185. } else {
  186. $actions .= '<a href="exercice.php">' . Display :: return_icon('back.png', get_lang('GoBackToQuestionList'),'',ICON_SIZE_MEDIUM).'</a>';
  187. }
  188. //Deleting an attempt
  189. if ( ($is_allowedToEdit || $is_tutor || api_is_coach()) && $_GET['delete'] == 'delete' && !empty ($_GET['did']) && $locked == false) {
  190. $exe_id = intval($_GET['did']);
  191. if (!empty($exe_id)) {
  192. $sql = 'DELETE FROM '.$TBL_TRACK_EXERCICES.' WHERE exe_id = '.$exe_id;
  193. Database::query($sql);
  194. $sql = 'DELETE FROM '.$TBL_TRACK_ATTEMPT.' WHERE exe_id = '.$exe_id;
  195. Database::query($sql);
  196. header('Location: exercise_report.php?cidReq=' . Security::remove_XSS($_GET['cidReq']) . '&exerciseId='.$exercise_id);
  197. exit;
  198. }
  199. }
  200. if ($is_allowedToEdit || $is_tutor) {
  201. $nameTools = get_lang('StudentScore');
  202. $interbreadcrumb[] = array("url" => "exercice.php?gradebook=$gradebook","name" => get_lang('Exercices'));
  203. $objExerciseTmp = new Exercise();
  204. if ($objExerciseTmp->read($exercise_id)) {
  205. $interbreadcrumb[] = array("url" => "admin.php?exerciseId=".$exercise_id, "name" => $objExerciseTmp->name);
  206. }
  207. } else {
  208. $interbreadcrumb[] = array("url" => "exercice.php?gradebook=$gradebook","name" => get_lang('Exercices'));
  209. $objExerciseTmp = new Exercise();
  210. if ($objExerciseTmp->read($exercise_id)) {
  211. $nameTools = get_lang('Results').': '.$objExerciseTmp->name;
  212. }
  213. }
  214. Display :: display_header($nameTools);
  215. $actions = Display::div($actions, array('class'=> 'actions'));
  216. $extra = '<script>
  217. $(document).ready(function() {
  218. $( "#dialog:ui-dialog" ).dialog( "destroy" );
  219. $( "#dialog-confirm" ).dialog({
  220. autoOpen: false,
  221. show: "blind",
  222. resizable: false,
  223. height:300,
  224. modal: true
  225. });
  226. $("#export_opener").click(function() {
  227. var targetUrl = $(this).attr("href");
  228. $( "#dialog-confirm" ).dialog({
  229. width:400,
  230. height:300,
  231. buttons: {
  232. "'.addslashes(get_lang('Download')).'": function() {
  233. var export_format = $("input[name=export_format]:checked").val();
  234. var extra_data = $("input[name=load_extra_data]:checked").val();
  235. location.href = targetUrl+"&export_format="+export_format+"&extra_data="+extra_data;
  236. $( this ).dialog( "close" );
  237. },
  238. }
  239. });
  240. $( "#dialog-confirm" ).dialog("open");
  241. return false;
  242. });
  243. });
  244. </script>';
  245. $extra .= '<div id="dialog-confirm" title="'.get_lang("ConfirmYourChoice").'">';
  246. $form = new FormValidator('report', 'post', null, null, array('class' => 'form-vertical'));
  247. $form->addElement('radio', 'export_format', null, get_lang('ExportAsCSV'), 'csv', array('id' => 'export_format_csv_label'));
  248. $form->addElement('radio', 'export_format', null, get_lang('ExportAsXLS'), 'xls', array('id' => 'export_format_xls_label'));
  249. $form->addElement('checkbox', 'load_extra_data', null, get_lang('LoadExtraData'), '0', array('id' => 'export_format_xls_label'));
  250. $form->setDefaults(array('export_format' => 'csv'));
  251. $extra .= $form->return_form();
  252. $extra .= '</div>';
  253. if ($is_allowedToEdit)
  254. echo $extra;
  255. echo $actions;
  256. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_exercise_results&exerciseId='.$exercise_id.'&filter_by_user='.$filter_user;
  257. $action_links = '';
  258. //Generating group list
  259. $group_list = GroupManager::get_group_list();
  260. $group_parameters = array('group_all:'.get_lang('All'),'group_none:'.get_lang('None'));
  261. foreach ($group_list as $group) {
  262. $group_parameters[] = $group['id'].':'.$group['name'];
  263. }
  264. if (!empty($group_parameters)) {
  265. $group_parameters = implode(';', $group_parameters);
  266. }
  267. if ($is_allowedToEdit || $is_tutor) {
  268. //The order is important you need to check the the $column variable in the model.ajax.php file
  269. $columns = array(get_lang('FirstName'),
  270. get_lang('LastName'),
  271. get_lang('LoginName'),
  272. get_lang('Group'),
  273. get_lang('Duration').' ('.get_lang('MinMinute').')',
  274. get_lang('StartDate'),
  275. get_lang('EndDate'),
  276. get_lang('Score'),
  277. get_lang('Status'),
  278. get_lang('ToolLearnpath'),
  279. get_lang('Actions')
  280. );
  281. //Column config
  282. $column_model = array(
  283. array('name'=>'firstname', 'index'=>'firstname', 'width'=>'50', 'align'=>'left', 'search' => 'true'),
  284. array('name'=>'lastname', 'index'=>'lastname', 'width'=>'50', 'align'=>'left', 'formatter'=>'action_formatter', 'search' => 'true'),
  285. array('name'=>'login', 'index'=>'username', 'width'=>'40', 'align'=>'left', 'search' => 'true', 'hidden'=>'true'),
  286. array('name'=>'group_name', 'index'=>'group_id', 'width'=>'40', 'align'=>'left', 'search' => 'true', 'stype'=> 'select',
  287. //for the bottom bar
  288. 'searchoptions' => array(
  289. 'defaultValue' => 'group_all',
  290. 'value' => $group_parameters),
  291. //for the top bar
  292. 'editoptions' => array('value' => $group_parameters)),
  293. array('name'=>'duration', 'index'=>'exe_duration', 'width'=>'30', 'align'=>'left', 'search' => 'true'),
  294. array('name'=>'start_date', 'index'=>'start_date', 'width'=>'60', 'align'=>'left', 'search' => 'true'),
  295. array('name'=>'exe_date', 'index'=>'exe_date', 'width'=>'60', 'align'=>'left', 'search' => 'true'),
  296. array('name'=>'score', 'index'=>'exe_result', 'width'=>'50', 'align'=>'left', 'search' => 'true'),
  297. array('name'=>'status', 'index'=>'revised', 'width'=>'40', 'align'=>'left', 'search' => 'true', 'stype'=>'select',
  298. //for the bottom bar
  299. 'searchoptions' => array(
  300. 'defaultValue' => '',
  301. 'value' => ':'.get_lang('All').';1:'.get_lang('Validated').';0:'.get_lang('NotValidated')),
  302. //for the top bar
  303. 'editoptions' => array('value' => ':'.get_lang('All').';1:'.get_lang('Validated').';0:'.get_lang('NotValidated'))),
  304. array('name'=>'lp', 'index'=>'lp', 'width'=>'60', 'align'=>'left', 'search' => 'false'),
  305. array('name'=>'actions', 'index'=>'actions', 'width'=>'60', 'align'=>'left', 'search' => 'false')
  306. );
  307. $action_links = '
  308. // add username as title in lastname filed - ref 4226
  309. function action_formatter(cellvalue, options, rowObject) {
  310. // rowObject is firstname,lastname,login,... get the third word
  311. var loginx = "'.api_htmlentities(sprintf(get_lang("LoginX"),":::"), ENT_QUOTES).'";
  312. var tabLoginx = loginx.split(/:::/);
  313. // tabLoginx[0] is before and tabLoginx[1] is after :::
  314. // may be empty string but is defined
  315. return "<span title=\""+tabLoginx[0]+rowObject[2]+tabLoginx[1]+"\">"+cellvalue+"</span>";
  316. }';
  317. }
  318. //Autowidth
  319. $extra_params['autowidth'] = 'true';
  320. //height auto
  321. $extra_params['height'] = 'auto';
  322. ?>
  323. <script>
  324. function setSearchSelect(columnName) {
  325. $("#results").jqGrid('setColProp', columnName,
  326. {
  327. searchoptions:{
  328. dataInit:function(el){
  329. $("option[value='1']",el).attr("selected", "selected");
  330. setTimeout(function(){
  331. $(el).trigger('change');
  332. },1000);
  333. }
  334. }
  335. });
  336. }
  337. function exportExcel() {
  338. var mya=new Array();
  339. mya=$("#results").getDataIDs(); // Get All IDs
  340. var data=$("#results").getRowData(mya[0]); // Get First row to get the labels
  341. var colNames=new Array();
  342. var ii=0;
  343. for (var i in data){colNames[ii++]=i;} // capture col names
  344. var html="";
  345. for(i=0;i<mya.length;i++) {
  346. data=$("#results").getRowData(mya[i]); // get each row
  347. for(j=0;j<colNames.length;j++) {
  348. html=html+data[colNames[j]]+","; // output each column as tab delimited
  349. }
  350. html=html+"\n"; // output each row with end of line
  351. }
  352. html = html+"\n"; // end of line at the end
  353. var form = $("#export_report_form");
  354. $("#csvBuffer").attr('value', html);
  355. form.target='_blank';
  356. form.submit();
  357. }
  358. $(function() {
  359. <?php
  360. echo Display::grid_js('results', $url, $columns, $column_model, $extra_params, array(), $action_links, true);
  361. if ($is_allowedToEdit || $is_tutor) { ?>
  362. //setSearchSelect("status");
  363. //
  364. //view:true, del:false, add:false, edit:false, excel:true}
  365. $("#results").jqGrid('navGrid','#results_pager', {view:true, edit:false, add:false, del:false, excel:false},
  366. {height:280, reloadAfterSubmit:false}, // view options
  367. {height:280, reloadAfterSubmit:false}, // edit options
  368. {height:280, reloadAfterSubmit:false}, // add options
  369. {reloadAfterSubmit: false}, // del options
  370. {width:500} // search options
  371. );
  372. /*
  373. // add custom button to export the data to excel
  374. jQuery("#results").jqGrid('navButtonAdd','#results_pager',{
  375. caption:"",
  376. onClickButton : function () {
  377. //exportExcel();
  378. }
  379. });*/
  380. /*
  381. jQuery('#sessions').jqGrid('navButtonAdd','#sessions_pager',{id:'pager_csv',caption:'',title:'Export To CSV',onClickButton : function(e)
  382. {
  383. try {
  384. jQuery("#sessions").jqGrid('excelExport',{tag:'csv', url:'grid.php'});
  385. } catch (e) {
  386. window.location= 'grid.php?oper=csv';
  387. }
  388. },buttonicon:'ui-icon-document'})
  389. */
  390. //Adding search options
  391. var options = {
  392. 'stringResult': true,
  393. 'autosearch' : true,
  394. 'searchOnEnter':false
  395. }
  396. jQuery("#results").jqGrid('filterToolbar',options);
  397. var sgrid = $("#results")[0];
  398. sgrid.triggerToolbar();
  399. <?php } ?>
  400. });
  401. </script>
  402. <form id="export_report_form" method="post" action="exercise_report.php">
  403. <input type="hidden" name="csvBuffer" id="csvBuffer" value="" />
  404. <input type="hidden" name="export_report" id="export_report" value="1" />
  405. <input type="hidden" name="exerciseId" id="exerciseId" value="<?php echo $exercise_id ?>" />
  406. </form>
  407. <?php
  408. echo Display::grid_html('results');
  409. Display :: display_footer();