exercise_report.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 Olivier Brouckaert, original author
  7. * @author Denes Nagy, HotPotatoes integration
  8. * @author Wolfgang Schneider, code/html cleanup
  9. * @author Julio Montoya <gugli100@gmail.com>, lots of cleanup + several improvements
  10. * Modified by hubert.borderiou (question category)
  11. */
  12. /**
  13. * Code
  14. */
  15. // name of the language file that needs to be included
  16. $language_file = array('exercice','tracking');
  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_jquery_ui_js();
  23. // Access control
  24. api_protect_course_script(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) . 'document.lib.php';
  34. require_once api_get_path(LIBRARY_PATH) . 'mail.lib.inc.php';
  35. require_once api_get_path(LIBRARY_PATH)."groupmanager.lib.php"; // for group filtering
  36. // need functions of statsutils lib to display previous exercices scores
  37. require_once api_get_path(LIBRARY_PATH) . 'statsUtils.lib.inc.php';
  38. // document path
  39. $documentPath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . "/document";
  40. /* Constants and variables */
  41. $is_allowedToEdit = api_is_allowed_to_edit(null,true);
  42. $is_tutor = api_is_allowed_to_edit(true);
  43. $is_tutor_course = api_is_course_tutor();
  44. $TBL_QUESTIONS = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  45. $TBL_TRACK_EXERCICES = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  46. $TBL_TRACK_ATTEMPT = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  47. $TBL_TRACK_ATTEMPT_RECORDING= Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  48. $TBL_LP_ITEM_VIEW = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  49. $TBL_LP_ITEM = Database :: get_course_table(TABLE_LP_ITEM);
  50. $course_id = api_get_course_int_id();
  51. $exercise_id = isset($_REQUEST['exerciseId']) ? intval($_REQUEST['exerciseId']) : null;
  52. if (empty($exercise_id)) {
  53. api_not_allowed();
  54. }
  55. if (!empty($exercise_id))
  56. $parameters['exerciseId'] = $exercise_id;
  57. if (!empty($_GET['path'])) {
  58. $parameters['path'] = Security::remove_XSS($_GET['path']);
  59. }
  60. // filter display by student group
  61. // if $_GET['filterByGroup'] = -1 => do not filter
  62. // else, filter by group_id (0 for no group)
  63. $filterByGroup = -1;
  64. if (isset($_GET['filterByGroup']) && is_numeric($_GET['filterByGroup'])) {
  65. $filterByGroup = Security::remove_XSS($_GET['filterByGroup']);
  66. api_session_register('filterByGroup');
  67. } else if (isset($_SESSION['filterByGroup'])) {
  68. $filterByGroup = $_SESSION['filterByGroup'];
  69. }
  70. if (!empty ($_GET['extra_data'])) {
  71. switch ($_GET['extra_data']) {
  72. case 'on' :
  73. $_SESSION['export_user_fields'] = true;
  74. break;
  75. default :
  76. $_SESSION['export_user_fields'] = false;
  77. break;
  78. }
  79. }
  80. if (!empty($_GET['export_report']) && $_GET['export_report'] == '1') {
  81. if (api_is_platform_admin() || api_is_course_admin() || api_is_course_tutor() || api_is_course_coach()) {
  82. $user_id = null;
  83. if (empty($_SESSION['export_user_fields']))
  84. $_SESSION['export_user_fields'] = false;
  85. if (!$is_allowedToEdit and !$is_tutor) {
  86. $user_id = api_get_user_id();
  87. }
  88. require_once 'exercise_result.class.php';
  89. switch ($_GET['export_format']) {
  90. case 'xls' :
  91. $export = new ExerciseResult();
  92. $export->exportCompleteReportXLS($documentPath, $user_id, $_SESSION['export_user_fields'], $_GET['export_filter'], $_GET['exerciseId'], $_GET['hotpotato_name']);
  93. exit;
  94. break;
  95. case 'csv' :
  96. default :
  97. $export = new ExerciseResult();
  98. $export->exportCompleteReportCSV($documentPath, $user_id, $_SESSION['export_user_fields'], $_GET['export_filter'], $_GET['exerciseId'], $_GET['hotpotato_name']);
  99. exit;
  100. break;
  101. }
  102. } else {
  103. api_not_allowed(true);
  104. }
  105. }
  106. //Send student email @todo move this code in a class, library
  107. if ($_REQUEST['comments'] == 'update' && ($is_allowedToEdit || $is_tutor) && $_GET['exeid']== strval(intval($_GET['exeid']))) {
  108. $id = intval($_GET['exeid']); //filtered by post-condition
  109. $track_exercise_info = get_exercise_track_exercise_info($id);
  110. if (empty($track_exercise_info)) {
  111. api_not_allowed();
  112. }
  113. $test = $track_exercise_info['title'];
  114. $student_id = $track_exercise_info['exe_user_id'];
  115. $session_id = $track_exercise_info['session_id'];
  116. $lp_id = $track_exercise_info['orig_lp_id'];
  117. $lp_item_id = $track_exercise_info['orig_lp_item_id'];
  118. $lp_item_view_id = $track_exercise_info['orig_lp_item_view_id'];
  119. // Teacher data
  120. $teacher_info = api_get_user_info(api_get_user_id());
  121. $user_info = api_get_user_info($student_id);
  122. $student_email = $user_info['mail'];
  123. $from = $teacher_info['mail'];
  124. $from_name = api_get_person_name($teacher_info['firstname'], $teacher_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  125. $url = api_get_path(WEB_CODE_PATH) . 'exercice/exercise_report.php?' . api_get_cidreq() . '&id_session='.$session_id.'&exerciseId='.$exercise_id;
  126. $my_post_info = array();
  127. $post_content_id = array();
  128. $comments_exist = false;
  129. foreach ($_POST as $key_index=>$key_value) {
  130. $my_post_info = explode('_',$key_index);
  131. $post_content_id[]=$my_post_info[1];
  132. if ($my_post_info[0]=='comments') {
  133. $comments_exist=true;
  134. }
  135. }
  136. $loop_in_track=($comments_exist===true) ? (count($_POST)/2) : count($_POST);
  137. $array_content_id_exe=array();
  138. if ($comments_exist===true) {
  139. $array_content_id_exe = array_slice($post_content_id,$loop_in_track);
  140. } else {
  141. $array_content_id_exe = $post_content_id;
  142. }
  143. for ($i=0;$i<$loop_in_track;$i++) {
  144. $my_marks = Database::escape_string($_POST['marks_'.$array_content_id_exe[$i]]);
  145. $contain_comments = Database::escape_string($_POST['comments_'.$array_content_id_exe[$i]]);
  146. if (isset($contain_comments)) {
  147. $my_comments = Database::escape_string($_POST['comments_'.$array_content_id_exe[$i]]);
  148. } else {
  149. $my_comments = '';
  150. }
  151. $my_questionid = intval($array_content_id_exe[$i]);
  152. $sql = "SELECT question from $TBL_QUESTIONS WHERE c_id = $course_id AND id = '$my_questionid'";
  153. $result =Database::query($sql);
  154. $ques_name = Database::result($result,0,"question");
  155. $query = "UPDATE $TBL_TRACK_ATTEMPT SET marks = '$my_marks',teacher_comment = '$my_comments' WHERE question_id = ".$my_questionid." AND exe_id=".$id;
  156. Database::query($query);
  157. //Saving results in the track recording table
  158. $recording_changes = 'INSERT INTO '.$TBL_TRACK_ATTEMPT_RECORDING.' (exe_id, question_id, marks, insert_date, author, teacher_comment)
  159. VALUES ('."'$id','".$my_questionid."','$my_marks','".api_get_utc_datetime()."','".api_get_user_id()."'".',"'.$my_comments.'")';
  160. Database::query($recording_changes);
  161. }
  162. $qry = 'SELECT DISTINCT question_id, marks FROM ' . $TBL_TRACK_ATTEMPT . ' WHERE exe_id = '.$id .' GROUP BY question_id';
  163. $res = Database::query($qry);
  164. $tot = 0;
  165. while ($row = Database :: fetch_array($res, 'ASSOC')) {
  166. $tot += $row['marks'];
  167. }
  168. $totquery = "UPDATE $TBL_TRACK_EXERCICES SET exe_result = '".floatval($tot)."' WHERE exe_id = ".$id;
  169. Database::query($totquery);
  170. //@todo move this somewhere else
  171. $subject = get_lang('ExamSheetVCC');
  172. $message = '<p>'.get_lang('DearStudentEmailIntroduction') . '</p><p>'.get_lang('AttemptVCC');
  173. $message .= '<h3>'.get_lang('CourseName'). '</h3><p>'.Security::remove_XSS($course_info['name']).'';
  174. $message .= '<h3>'.get_lang('Exercise') . '</h3><p>'.Security::remove_XSS($test);
  175. //Only for exercises not in a LP
  176. if ($lp_id == 0) {
  177. $message .= '<p>'.get_lang('ClickLinkToViewComment') . ' <a href="#url#">#url#</a><br />';
  178. }
  179. $message .= '<p>'.get_lang('Regards') . ' </p>';
  180. $message .= $from_name;
  181. $message = str_replace("#test#", Security::remove_XSS($test), $message);
  182. $message = str_replace("#url#", $url, $message);
  183. @api_mail_html($student_email, $student_email, $subject, $message, $from_name, $from, array('charset'=>api_get_system_encoding()));
  184. //Updating LP score here
  185. if (in_array($origin, array ('tracking_course','user_course','correct_exercise_in_lp'))) {
  186. $sql_update_score = "UPDATE $TBL_LP_ITEM_VIEW SET score = '" . floatval($tot) . "' WHERE id = " .$lp_item_view_id;
  187. Database::query($sql_update_score);
  188. if ($origin == 'tracking_course') {
  189. //Redirect to the course detail in lp
  190. header('location: exercice.php?course=' . Security :: remove_XSS($_GET['course']));
  191. exit;
  192. } else {
  193. //Redirect to the reporting
  194. header('location: ../mySpace/myStudents.php?origin=' . $origin . '&student=' . $student_id . '&details=true&course=' . $course_id.'&session_id='.$session_id);
  195. exit;
  196. }
  197. }
  198. }
  199. if ($is_allowedToEdit && $origin != 'learnpath') {
  200. // the form
  201. if (api_is_platform_admin() || api_is_course_admin() || api_is_course_tutor() || api_is_course_coach()) {
  202. if ($_SESSION['export_user_fields']) {
  203. $alt = get_lang('ExportWithUserFields');
  204. $extra_user_fields = '<input type="hidden" name="export_user_fields" value="export_user_fields">';
  205. } else {
  206. $alt = get_lang('ExportWithoutUserFields');
  207. $extra_user_fields = '<input type="hidden" name="export_user_fields" value="do_not_export_user_fields">';
  208. }
  209. $actions .= '<a href="admin.php?exerciseId='.intval($_GET['exerciseId']).'">' . Display :: return_icon('back.png', get_lang('GoBackToQuestionList'),'','32').'</a>';
  210. if ($_GET['filter'] == '1' or !isset ($_GET['filter']) or $_GET['filter'] == 0 ) {
  211. $filter = 1;
  212. } else {
  213. $filter = 2;
  214. }
  215. $actions .= '<a id="export_opener" href="'.api_get_self().'?export_report=1&export_filter='.$filter.'&hotpotato_name='.Security::remove_XSS($_GET['path']).'&exerciseId='.intval($_GET['exerciseId']).'" >'.
  216. Display::return_icon('save.png', get_lang('Export'),'',32).'</a>';
  217. }
  218. } else {
  219. $actions .= '<a href="exercice.php">' . Display :: return_icon('back.png', get_lang('GoBackToQuestionList'),'','32').'</a>';
  220. }
  221. //Deleting an attempt
  222. if ($_GET['delete'] == 'delete' && ($is_allowedToEdit || api_is_coach()) && !empty ($_GET['did']) && $_GET['did'] == strval(intval($_GET['did']))) {
  223. $sql = 'DELETE FROM ' . $TBL_TRACK_EXERCICES . ' WHERE exe_id = ' . intval($_GET['did']); //_GET[did] filtered by entry condition
  224. Database::query($sql);
  225. $sql = 'DELETE FROM ' . $TBL_TRACK_ATTEMPT . ' WHERE exe_id = ' . intval($_GET['did']); //_GET[did] filtered by entry condition
  226. Database::query($sql);
  227. $filter=Security::remove_XSS($_GET['filter']);
  228. header('Location: exercise_report.php?cidReq=' . Security::remove_XSS($_GET['cidReq']) . '&filter=' . $filter . '&exerciseId='.$exercise_id.'&filter_by_user='.$_GET['filter_by_user']);
  229. exit;
  230. }
  231. if (api_is_allowed_to_edit(null,true)) {
  232. if (!$_GET['filter']) {
  233. $filter_by_not_revised = true;
  234. $filter = 1;
  235. } else {
  236. $filter=Security::remove_XSS($_GET['filter']);
  237. }
  238. $filter = (int)$_GET['filter'];
  239. switch ($filter) {
  240. case 1 :
  241. $filter_by_not_revised = true;
  242. break;
  243. case 2 :
  244. $filter_by_revised = true;
  245. break;
  246. default :
  247. null;
  248. }
  249. //Report by question
  250. $actions .= Display::url(Display::return_icon('statistics_admin.gif', get_lang("ReportByQuestion")), 'stats.php?exerciseId='.$exercise_id);
  251. //Live results
  252. $actions .='<a href="live_stats.php?' . api_get_cidreq() . '&exerciseId='.$exercise_id.'">'.Display :: return_icon('activity_monitor.png', get_lang('LiveResults'),'',32).'</a>';
  253. if (!empty($_GET['exerciseId']) && empty($_GET['filter_by_user'])) {
  254. if ($_GET['filter'] == '1' or !isset ($_GET['filter']) or $_GET['filter'] == 0 ) {
  255. $view_result = '<a href="' . api_get_self() . '?cidReq=' . api_get_course_id() . '&filter=2&id_session='.intval($_GET['id_session']).'&exerciseId='.intval($_GET['exerciseId']).'&gradebook='.$gradebook.'" >'.Display :: return_icon('exercice_check.png', get_lang('ShowCorrectedOnly'),'','32').'</a>';
  256. } else {
  257. $view_result = '<a href="' .api_get_self() . '?cidReq=' . api_get_course_id() . '&filter=1&id_session='.intval($_GET['id_session']).'&exerciseId='.intval($_GET['exerciseId']).'&gradebook='.$gradebook.'" >'.Display :: return_icon('exercice_uncheck.png', get_lang('ShowUnCorrectedOnly'),'','32').'</a>';
  258. }
  259. $actions .= $view_result;
  260. // filter by student group menu
  261. $actions .= "<script type='text/javascript'>";
  262. $actions .= " function doFilterByGroup() {";
  263. $actions .= " var IdGroup = document.getElementById('groupFilter').value;";
  264. $actions .= " var goToUrl = \"".api_get_self()."?".api_get_cidreq()."&filter=$filter&gradebook=$gradebook&exerciseId=$exercise_id;$quiz_results_per_page&filterByGroup=\"+IdGroup;";
  265. $actions .= " self.location.href=goToUrl;";
  266. $actions .= " }";
  267. $actions .= " </script>";
  268. $actions .= "&nbsp;&nbsp;";
  269. $actions .= Display::return_icon('group.gif', get_lang("FilterByGroup"));
  270. $actions .= displayGroupMenu("groupFilter", $filterByGroup, "doFilterByGroup()")."&nbsp;";
  271. }
  272. }
  273. $parameters=array('cidReq'=>Security::remove_XSS($_GET['cidReq']),'filter' => Security::remove_XSS($_GET['filter']),'gradebook' =>Security::remove_XSS($_GET['gradebook']));
  274. $table = new SortableTable('quiz_results', 'get_count_exam_results', 'get_exam_results_data', 1, 10);
  275. $table->set_additional_parameters($parameters);
  276. if ($is_allowedToEdit || $is_tutor) {
  277. if (api_is_western_name_order()) {
  278. $table->set_header(0, get_lang('FirstName'));
  279. $table->set_header(1, get_lang('LastName'));
  280. } else {
  281. $table->set_header(0, get_lang('LastName'));
  282. $table->set_header(1, get_lang('FirstName'));
  283. }
  284. $table->set_header(2, get_lang('LoginName'));
  285. $table->set_header(3, get_lang('Group'),false);
  286. $table->set_header(4, get_lang('Exercice'),false);
  287. $table->set_header(5, get_lang('Duration'),false);
  288. $table->set_header(6, get_lang('Date'));
  289. $table->set_header(7, get_lang('Score'),false);
  290. $table->set_header(8, get_lang('Status'), false);
  291. $table->set_header(9, get_lang('Actions'), false);
  292. } else {
  293. $table->set_header(0, get_lang('Exercice'));
  294. $table->set_header(1, get_lang('Duration'),false);
  295. $table->set_header(2, get_lang('Date'));
  296. $table->set_header(3, get_lang('Score'),false);
  297. $table->set_header(4, get_lang('Result'), false);
  298. }
  299. $content = $table->return_table();
  300. if ($is_allowedToEdit || $is_tutor) {
  301. $nameTools = get_lang('StudentScore');
  302. $interbreadcrumb[] = array("url" => "exercice.php?gradebook=$gradebook","name" => get_lang('Exercices'));
  303. $objExerciseTmp = new Exercise();
  304. if ($objExerciseTmp->read($exercise_id)) {
  305. $interbreadcrumb[] = array("url" => "admin.php?exerciseId=".$exercise_id, "name" => $objExerciseTmp->name);
  306. }
  307. } else {
  308. $nameTools = get_lang('YourScore');
  309. $interbreadcrumb[] = array ("url" => "exercice.php?gradebook=$gradebook","name" => get_lang('Exercices'));
  310. }
  311. Display :: display_header($nameTools);
  312. $actions = Display::div($actions, array('class'=> 'actions'));
  313. $extra = '<script type="text/javascript">
  314. $(document).ready(function() {
  315. $( "#dialog:ui-dialog" ).dialog( "destroy" );
  316. $( "#dialog-confirm" ).dialog({
  317. autoOpen: false,
  318. show: "blind",
  319. resizable: false,
  320. height:250,
  321. modal: true
  322. });
  323. $("#export_opener").click(function() {
  324. var targetUrl = $(this).attr("href");
  325. $( "#dialog-confirm" ).dialog({
  326. width:350,
  327. height:220,
  328. buttons: {
  329. "'.addslashes(get_lang('Download')).'": function() {
  330. var export_format = $("input[name=export_format]:checked").val();
  331. var extra_data = $("input[name=load_extra_data]:checked").val();
  332. location.href = targetUrl+"&export_format="+export_format+"&extra_data="+extra_data;
  333. $( this ).dialog( "close" );
  334. },
  335. }
  336. });
  337. $( "#dialog-confirm" ).dialog("open");
  338. return false;
  339. });
  340. });
  341. </script>';
  342. $extra .= '<div id="dialog-confirm" title="'.get_lang("ConfirmYourChoice").'">';
  343. $extra .= Display::tag('p', Display::input('radio', 'export_format', 'csv', array('checked'=>'1', 'id'=>'export_format_csv_label')). Display::tag('label', get_lang('ExportAsCSV'), array('for'=>'export_format_csv_label')));
  344. $extra .= Display::tag('p', Display::input('radio', 'export_format', 'xls', array('id'=>'export_format_xls_label')). Display::tag('label', get_lang('ExportAsXLS'), array('for'=>'export_format_xls_label')));
  345. $extra .= Display::tag('p', Display::input('checkbox', 'load_extra_data', '0',array('id'=>'load_extra_data_id')). Display::tag('label', get_lang('LoadExtraData'), array('for'=>'load_extra_data_id')));
  346. $extra .= '</div>';
  347. if ($is_allowedToEdit)
  348. echo $extra;
  349. echo $actions;
  350. echo $content;
  351. /*
  352. $tpl = new Template($nameTools);
  353. $tpl->assign('content', $content);
  354. $tpl->display_one_col_template();
  355. */
  356. Display :: display_footer();