gradebook_view_result.php 20 KB

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