gradebook_view_result.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script
  5. * @package chamilo.gradebook
  6. */
  7. $language_file[] = 'gradebook';
  8. require_once '../inc/global.inc.php';
  9. require_once 'lib/be.inc.php';
  10. require_once 'lib/gradebook_functions.inc.php';
  11. require_once 'lib/fe/displaygradebook.php';
  12. require_once 'lib/fe/evalform.class.php';
  13. require_once 'lib/fe/dataform.class.php';
  14. require_once api_get_path(LIBRARY_PATH) . 'fileManage.lib.php';
  15. require_once api_get_path(LIBRARY_PATH) . 'export.lib.inc.php';
  16. require_once api_get_path(LIBRARY_PATH) . 'import.lib.php';
  17. require_once 'lib/results_data_generator.class.php';
  18. require_once 'lib/fe/resulttable.class.php';
  19. require_once 'lib/fe/exportgradebook.php';
  20. require_once 'lib/scoredisplay.class.php';
  21. require_once api_get_path(LIBRARY_PATH) . 'ezpdf/class.ezpdf.php';
  22. require_once api_get_path(SYS_CODE_PATH) . 'gradebook/lib/gradebook_functions.inc.php';
  23. api_block_anonymous_users();
  24. $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
  25. api_get_user_id(),
  26. api_get_course_info()
  27. );
  28. if (!$isDrhOfCourse) {
  29. block_students();
  30. }
  31. $interbreadcrumb[] = array(
  32. 'url' => $_SESSION['gradebook_dest'],
  33. 'name' => get_lang('Gradebook'
  34. ));
  35. //load the evaluation & category
  36. $select_eval = Security::remove_XSS($_GET['selecteval']);
  37. if (empty($select_eval)) {
  38. api_not_allowed();
  39. }
  40. $displayscore = Scoredisplay :: instance();
  41. $eval = Evaluation :: load($select_eval);
  42. $overwritescore = 0;
  43. if ($eval[0]->get_category_id() < 0) {
  44. // if category id is negative, then the evaluation's origin is a link
  45. $link = LinkFactory :: get_evaluation_link($eval[0]->get_id());
  46. $currentcat = Category :: load($link->get_category_id());
  47. } else
  48. $currentcat = Category :: load($eval[0]->get_category_id());
  49. //load the result with the evaluation id
  50. if (isset($_GET['delete_mark'])) {
  51. $result = Result :: load($_GET['delete_mark']);
  52. if (!empty($result[0])) {
  53. $result[0]->delete();
  54. }
  55. }
  56. if (isset($_GET['selecteval'])) {
  57. $allresults = Result :: load(null, null, $select_eval);
  58. $iscourse = $currentcat[0]->get_course_code() == null ? 1 : 0;
  59. }
  60. if (isset($_GET['editres'])) {
  61. $edit_res_xml = Security::remove_XSS($_GET['editres']);
  62. $select_eval_edit = Security::remove_XSS($_GET['selecteval']);
  63. $resultedit = Result :: load($edit_res_xml);
  64. $edit_res_form = new EvalForm(
  65. EvalForm :: TYPE_RESULT_EDIT,
  66. $eval[0],
  67. $resultedit[0],
  68. 'edit_result_form',
  69. null,
  70. api_get_self() . '?editres=' . $resultedit[0]->get_id(
  71. ) . '&selecteval=' . $select_eval_edit
  72. );
  73. if ($edit_res_form->validate()) {
  74. $values = $edit_res_form->exportValues();
  75. $result = new Result();
  76. $resultlog = new Result();
  77. $resultlog->add_result__log($values['hid_user_id'], $select_eval_edit);
  78. $result->set_id($edit_res_xml);
  79. $result->set_user_id($values['hid_user_id']);
  80. $result->set_evaluation_id($select_eval_edit);
  81. $row_value = isset($values['score']) ? (float) $values['score'] : 0;
  82. if ((!empty($row_value)) || ($row_value == 0)) {
  83. $result->set_score(floatval(number_format($row_value, api_get_setting('gradebook_number_decimals'))));
  84. }
  85. $result->save();
  86. unset($result);
  87. header('Location: gradebook_view_result.php?selecteval=' . $select_eval_edit . '&editresmessage=');
  88. exit;
  89. }
  90. }
  91. if (isset($_GET['import'])) {
  92. $interbreadcrumb[] = array('url' => 'gradebook_view_result.php?selecteval=' . Security::remove_XSS($_GET['selecteval']), 'name' => get_lang('ViewResult'));
  93. $import_result_form = new DataForm(
  94. DataForm :: TYPE_IMPORT,
  95. 'import_result_form',
  96. null,
  97. api_get_self() . '?import=&selecteval=' . Security::remove_XSS($_GET['selecteval']),
  98. '_blank',
  99. ''
  100. );
  101. if (!$import_result_form->validate()) {
  102. Display :: display_header(get_lang('Import'));
  103. }
  104. $eval[0]->check_lock_permissions();
  105. if ($_POST['formSent']) {
  106. if (!empty($_FILES['import_file']['name'])) {
  107. $values = $import_result_form->exportValues();
  108. $file_type = $_POST['file_type'];
  109. $file_name = $_FILES['import_file']['tmp_name'];
  110. if ($file_type == 'csv') {
  111. $results = Import :: csv_to_array($file_name);
  112. } else {
  113. $results = parse_xml_data($file_name);
  114. }
  115. $nr_results_added = 0;
  116. foreach ($results as $index => $importedresult) {
  117. //check username & score
  118. $importedresult['user_id'] = UserManager::get_user_id_from_username($importedresult['username']);
  119. $added = '0';
  120. foreach ($allresults as $allresult) {
  121. if (($importedresult['user_id'] == $allresult->get_user_id())) {
  122. if ($importedresult['score'] != $allresult->get_score()) {
  123. if (!isset($values['overwrite'])) {
  124. header('Location: gradebook_view_result.php?selecteval=' . Security::remove_XSS($_GET['selecteval']) . '&import_score_error=' . $importedresult['user_id']);
  125. exit;
  126. break;
  127. } else {
  128. overwritescore($allresult->get_id(), $importedresult['score'], $eval[0]->get_max());
  129. $overwritescore++;
  130. $added = '1';
  131. }
  132. } else {
  133. $added = '1';
  134. }
  135. }
  136. }
  137. if ($importedresult['user_id'] == null) {
  138. header('Location: gradebook_view_result.php?selecteval=' . Security::remove_XSS($_GET['selecteval']) . '&incorrectdata=');
  139. exit;
  140. }
  141. $userinfo = get_user_info_from_id($importedresult['user_id']);
  142. if ($userinfo['lastname'] != $importedresult['lastname'] ||
  143. $userinfo['firstname'] != $importedresult['firstname'] ||
  144. $userinfo['official_code'] != $importedresult['official_code']
  145. ) {
  146. if (!isset($values['ignoreerrors'])) {
  147. header('Location: gradebook_view_result.php?selecteval=' . Security::remove_XSS($_GET['selecteval']) . '&import_user_error=' . $importedresult['user_id']);
  148. exit;
  149. }
  150. }
  151. if ($added != '1') {
  152. if ($importedresult['score'] > $eval[0]->get_max()) {
  153. header('Location: gradebook_view_result.php?selecteval=' . Security::remove_XSS($_GET['selecteval']) . '&overwritemax=');
  154. exit;
  155. }
  156. $result = new Result();
  157. $result->set_user_id($importedresult['user_id']);
  158. if (!empty($importedresult['score'])) {
  159. $result->set_score(floatval(number_format($importedresult['score'], api_get_setting('gradebook_number_decimals'))));
  160. }
  161. if (!empty($importedresult['date'])) {
  162. $result->set_date(api_get_utc_datetime($importedresult['date']));
  163. } else {
  164. $result->set_date(api_get_utc_datetime());
  165. }
  166. $result->set_evaluation_id($_GET['selecteval']);
  167. $result->add();
  168. $nr_results_added++;
  169. }
  170. }
  171. } else {
  172. header('Location: ' . api_get_self() . '?import=&selecteval=' . Security::remove_XSS($_GET['selecteval']) . '&importnofile=');
  173. exit;
  174. }
  175. if ($overwritescore != 0) {
  176. header('Location: ' . api_get_self() . '?selecteval=' . Security::remove_XSS($_GET['selecteval']) . '&importoverwritescore=' . $overwritescore);
  177. exit;
  178. }
  179. if ($nr_results_added == 0) {
  180. header('Location: ' . api_get_self() . '?selecteval=' . Security::remove_XSS($_GET['selecteval']) . '&nothingadded=');
  181. exit;
  182. }
  183. header('Location: ' . api_get_self() . '?selecteval=' . Security::remove_XSS($_GET['selecteval']) . '&importok=');
  184. exit;
  185. }
  186. }
  187. if (isset($_GET['export'])) {
  188. $interbreadcrumb[] = array('url' => 'gradebook_view_result.php?selecteval=' . Security::remove_XSS($_GET['selecteval']), 'name' => get_lang('ViewResult'));
  189. $locked_status = $eval[0]->get_locked();
  190. $export_result_form = new DataForm(
  191. DataForm :: TYPE_EXPORT,
  192. 'export_result_form',
  193. null,
  194. api_get_self() . '?export=&selecteval=' . $_GET['selecteval'],
  195. '_blank',
  196. $locked_status
  197. );
  198. if (!$export_result_form->validate()) {
  199. Display :: display_header(get_lang('Export'));
  200. }
  201. if ($export_result_form->validate()) {
  202. $export = $export_result_form->exportValues();
  203. $file_type = $export['file_type'];
  204. $filename = 'export_results_' . gmdate('Y-m-d_H-i-s');
  205. $results = Result :: load(null, null, Security::remove_XSS($_GET['selecteval']));
  206. $data = array(); //when file type is csv, add a header to the output file
  207. if ($file_type == 'csv') {
  208. $alldata[] = array(
  209. 'username',
  210. 'official_code',
  211. 'lastname',
  212. 'firstname',
  213. 'score',
  214. 'date'
  215. );
  216. }
  217. // export results to pdf file
  218. if ($file_type == 'pdf') {
  219. $number_decimals = api_get_setting('gradebook_number_decimals');
  220. $datagen = new ResultsDataGenerator($eval[0], $allresults);
  221. // set headers pdf
  222. !empty($_user['official_code']) ? $officialcode = $_user['official_code'] . ' - ' : '';
  223. $h1 = array(get_lang('Teacher'), $officialcode . $_user['firstName'] . ', ' . $_user['lastName']);
  224. $h2 = array(get_lang('Score'), $eval[0]->get_max());
  225. $h3 = array(get_lang('Course'), $_course['name']);
  226. $h4 = array(get_lang('Weight'), $eval[0]->get_weight());
  227. $h5 = array(get_lang('Session'), api_get_session_name(api_get_session_id()));
  228. $date = date('d-m-Y H:i:s', time());
  229. $h6 = array(get_lang('DateTime'), api_convert_and_format_date($date, "%d/%m/%Y %H:%M"));
  230. $header_pdf = array($h1, $h2, $h3, $h4, $h5, $h6);
  231. // set footer pdf
  232. $f1 = '<hr />' . get_lang('Drh');
  233. $f2 = '<hr />' . get_lang('Teacher');
  234. $f3 = '<hr />' . get_lang('Date');
  235. $footer_pdf = array($f1, $f2, $f3);
  236. // set title pdf
  237. $title_pdf = $eval[0]->get_name();
  238. // set headers data table
  239. $head_ape_name = '';
  240. if (api_is_western_name_order()) {
  241. $head_ape_name = get_lang('FirstName') . ', ' . get_lang('LastName');
  242. } else {
  243. $head_ape_name = get_lang('LastName') . ', ' . get_lang('FirstName');
  244. }
  245. $head_table = array(
  246. array('#', 3),
  247. array(get_lang('Code'), 12),
  248. array($head_ape_name, 40),
  249. array(get_lang('Score'), 12)
  250. );
  251. if ($number_decimals == null) {
  252. $head_table[] = array(get_lang('Letters'), 15);
  253. }
  254. $head_display_score = '';
  255. $scoredisplay = ScoreDisplay :: instance();
  256. $customdisplays = $scoredisplay->get_custom_score_display_settings();
  257. if (!empty($customdisplays) && $scoredisplay->is_custom()) {
  258. $head_display_score = get_lang('Display');
  259. $head_table[] = array($head_display_score, 15);
  260. }
  261. // get data table
  262. if (api_sort_by_first_name()) {
  263. $data_array = $datagen->get_data(ResultsDataGenerator :: RDG_SORT_FIRSTNAME, 0, null, false, true);
  264. } else {
  265. $data_array = $datagen->get_data(ResultsDataGenerator :: RDG_SORT_LASTNAME, 0, null, false, true);
  266. }
  267. $data_table = array();
  268. foreach ($data_array as $data) {
  269. $result = array();
  270. $user_info = api_get_user_info($data['id']);
  271. $result[] = $user_info['username'];
  272. if (api_is_western_name_order()) {
  273. $result[] = $user_info['firstname'] . ', ' . $user_info['lastname'];
  274. } else {
  275. $result[] = $user_info['lastname'] . ', ' . $user_info['firstname'];
  276. }
  277. if ($number_decimals == null) {
  278. if (empty($data['scoreletter']) && !is_numeric($data['score'])) {
  279. $result[] = get_lang('DidNotTakeTheExam');
  280. } else {
  281. $result[] = api_strtoupper(get_lang('Literal' . $data['scoreletter']));
  282. }
  283. } else {
  284. if (empty($data['score']) && !is_numeric($data['score'])) {
  285. $result[] = get_lang('DidNotTakeTheExamAcronym');
  286. } else {
  287. $result[] = $data['score'];
  288. }
  289. }
  290. if ($scoredisplay->is_custom()) {
  291. $result[] = $data['display'];
  292. }
  293. $data_table[] = $result;
  294. }
  295. export_pdf_with_html(
  296. $head_table,
  297. $data_table,
  298. $header_pdf,
  299. $footer_pdf,
  300. $title_pdf
  301. );
  302. }
  303. // export results to xml or csv file
  304. foreach ($results as $result) {
  305. $userinfo = get_user_info_from_id($result->get_user_id());
  306. $data['username'] = $userinfo['username']; //$result->get_user_id();
  307. $data['official_code'] = $userinfo['official_code'];
  308. $data['lastname'] = $userinfo['lastname'];
  309. $data['firstname'] = $userinfo['firstname'];
  310. $data['score'] = $result->get_score();
  311. $data['date'] = api_format_date($result->get_date(), "%d/%m/%Y %R");
  312. $alldata[] = $data;
  313. }
  314. switch ($file_type) {
  315. case 'xml' :
  316. Export :: export_table_xml($alldata, $filename, 'Result', 'XMLResults');
  317. exit;
  318. break;
  319. case 'csv' :
  320. Export :: export_table_csv($alldata, $filename);
  321. exit;
  322. break;
  323. }
  324. }
  325. }
  326. if (isset($_GET['resultdelete'])) {
  327. $result = Result :: load($_GET['resultdelete']);
  328. $result[0]->delete();
  329. header('Location: gradebook_view_result.php?deleteresult=&selecteval=' . Security::remove_XSS($_GET['selecteval']));
  330. exit;
  331. }
  332. if (isset($_POST['action'])) {
  333. $number_of_selected_items = count($_POST['id']);
  334. if ($number_of_selected_items == '0') {
  335. Display :: display_warning_message(get_lang('NoItemsSelected'), false);
  336. } else {
  337. switch ($_POST['action']) {
  338. case 'delete' :
  339. $number_of_deleted_results = 0;
  340. foreach ($_POST['id'] as $indexstr) {
  341. $result = Result :: load($indexstr);
  342. $result[0]->delete();
  343. $number_of_deleted_results++;
  344. }
  345. header('Location: gradebook_view_result.php?massdelete=&selecteval=' . Security::remove_XSS($_GET['selecteval']));
  346. exit;
  347. break;
  348. }
  349. }
  350. } // TODO - what if selecteval not set ?
  351. $addparams = array('selecteval' => $eval[0]->get_id());
  352. if (isset($_GET['print'])) {
  353. $datagen = new ResultsDataGenerator($eval[0], $allresults);
  354. if (api_sort_by_first_name()) {
  355. $data_array = $datagen->get_data(ResultsDataGenerator :: RDG_SORT_FIRSTNAME, 0, null, true);
  356. } else {
  357. $data_array = $datagen->get_data(ResultsDataGenerator :: RDG_SORT_LASTNAME, 0, null, true);
  358. }
  359. if ($displayscore->is_custom()) {
  360. if (api_is_western_name_order()) {
  361. $header_names = array(get_lang('FirstName'), get_lang('LastName'), get_lang('Score'), get_lang('Display'));
  362. } else {
  363. $header_names = array(get_lang('LastName'), get_lang('FirstName'), get_lang('Score'), get_lang('Display'));
  364. }
  365. } else {
  366. if (api_is_western_name_order()) {
  367. $header_names = array(get_lang('FirstName'), get_lang('LastName'), get_lang('Score'));
  368. } else {
  369. $header_names = array(get_lang('LastName'), get_lang('FirstName'), get_lang('Score'));
  370. }
  371. }
  372. $newarray = array();
  373. foreach ($data_array as $data) {
  374. $newarray[] = array_slice($data, 3);
  375. }
  376. echo print_table($newarray, $header_names, get_lang('ViewResult'), $eval[0]->get_name());
  377. exit;
  378. } else {
  379. $resulttable = new ResultTable($eval[0], $allresults, $iscourse, $addparams);
  380. }
  381. $htmlHeadXtra[] = '<script type="text/javascript">
  382. function confirmationuser() {
  383. if (confirm("' . get_lang('DeleteUser') . '?"))
  384. {return true;}
  385. else
  386. {return false;}
  387. }
  388. function confirmationall () {
  389. if (confirm("' . get_lang('DeleteAll') . '?"))
  390. {return true;}
  391. else
  392. {return false;}
  393. }
  394. </script>';
  395. if (isset($_GET['deleteall'])) {
  396. $eval[0]->delete_results();
  397. header('Location: gradebook_view_result.php?allresdeleted=&selecteval=' . Security::remove_XSS($_GET['selecteval']));
  398. exit;
  399. }
  400. if ((!isset($_GET['export'])) && (!isset($_GET['import']))) {
  401. if (!isset($_GET['selectcat'])) {
  402. $interbreadcrumb[] = array(
  403. 'url' => $_SESSION['gradebook_dest'] . '?selectcat=' . $currentcat[0]->get_id(),
  404. 'name' => get_lang('Details')
  405. );
  406. }
  407. $interbreadcrumb[] = array('url' => 'gradebook_view_result.php' . '?selecteval=' . Security::remove_XSS($_GET['selecteval']), 'name' => get_lang('ViewResult'));
  408. Display :: display_header('');
  409. }
  410. if (isset($_GET['addresultnostudents'])) {
  411. Display :: display_warning_message(get_lang('AddResultNoStudents'), false);
  412. }
  413. if (isset($_GET['editresmessage'])) {
  414. Display :: display_confirmation_message(get_lang('ResultEdited'), false);
  415. }
  416. if (isset($_GET['addresult'])) {
  417. Display :: display_confirmation_message(get_lang('ResultAdded'), false);
  418. }
  419. if (isset($_GET['adduser'])) {
  420. Display :: display_confirmation_message(get_lang('UserAdded'), false);
  421. }
  422. if (isset($_GET['deleteresult'])) {
  423. Display :: display_confirmation_message(get_lang('ResultDeleted'), false);
  424. }
  425. if (isset($_GET['editallresults'])) {
  426. Display :: display_confirmation_message(get_lang('AllResultsEdited'), false);
  427. }
  428. if (isset($_GET['importok'])) {
  429. Display :: display_confirmation_message(get_lang('FileUploadComplete'), false);
  430. }
  431. if (isset($_GET['importnofile'])) {
  432. Display :: display_warning_message(get_lang('ImportNoFile'), false);
  433. }
  434. if (isset($_GET['incorrectdata'])) {
  435. Display :: display_warning_message(get_lang('IncorrectData'), false);
  436. }
  437. if (isset($_GET['nothingadded'])) {
  438. Display :: display_warning_message(get_lang('ProblemUploadingFile'), false);
  439. }
  440. if (isset($_GET['massdelete'])) {
  441. Display :: display_confirmation_message(get_lang('ResultsDeleted'), false);
  442. }
  443. if (isset($_GET['nouser'])) {
  444. Display :: display_warning_message(get_lang('NoUser'), false);
  445. }
  446. if (isset($_GET['overwritemax'])) {
  447. Display :: display_warning_message(get_lang('OverWriteMax'), false);
  448. }
  449. if (isset($_GET['importoverwritescore'])) {
  450. Display :: display_confirmation_message(get_lang('ImportOverWriteScore') . ' ' . $_GET['importoverwritescore']);
  451. }
  452. if (isset($_GET['import_user_error'])) {
  453. $userinfo = get_user_info_from_id($_GET['import_user_error']);
  454. Display :: display_warning_message(get_lang('UserInfoDoesNotMatch') . ' ' . api_get_person_name($userinfo['firstname'], $userinfo['lastname']));
  455. }
  456. if (isset($_GET['allresdeleted'])) {
  457. Display :: display_confirmation_message(get_lang('AllResultDeleted'));
  458. }
  459. if (isset($_GET['import_score_error'])) {
  460. $userinfo = get_user_info_from_id($_GET['import_score_error']);
  461. Display :: display_warning_message(get_lang('ScoreDoesNotMatch') . ' ' . api_get_person_name($userinfo['firstname'], $userinfo['lastname']));
  462. }
  463. if ($file_type == null) { //show the result header
  464. if (isset($export_result_form) && !(isset($edit_res_form))) {
  465. echo $export_result_form->display();
  466. DisplayGradebook :: display_header_result($eval[0], $currentcat[0]->get_id(), 1);
  467. } else {
  468. if (isset($import_result_form)) {
  469. echo $import_result_form->display();
  470. }
  471. if (isset($edit_res_form)) {
  472. echo $edit_res_form->toHtml();
  473. }
  474. DisplayGradebook :: display_header_result($eval[0], $currentcat[0]->get_id(), 1);
  475. }
  476. // Letter-based scores are built from lib/results_data_generator.class.php::get_score_display()
  477. $resulttable->display();
  478. Display :: display_footer();
  479. }