gradebook_view_result.php 20 KB

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