gradebook_view_result.php 21 KB

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