exercise_report.php 21 KB

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