gradebook_view_result.php 21 KB

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