gradebook_view_result.php 20 KB

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