gradebook_view_result.php 21 KB

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