exercise_report.php 18 KB

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