exportgradebook.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script.
  5. *
  6. * @package chamilo.gradebook
  7. */
  8. /**
  9. * Prints an HTML page with a table containing the gradebook data.
  10. *
  11. * @param array Array containing the data to be printed in the table
  12. * @param array Table headers
  13. * @param string View to print as a title for the table
  14. * @param string Course name to print as title for the table
  15. *
  16. * @return string
  17. */
  18. function print_table($data_array, $header_names, $view, $coursename)
  19. {
  20. $printdata = '<!DOCTYPE html
  21. PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  22. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  23. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.api_get_language_isocode().'" lang="'.api_get_language_isocode().'">
  24. <head>
  25. <title>'.get_lang('Print').'</title>
  26. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  27. <style type="text/css">
  28. body {
  29. font-size: 12px;
  30. color: #000;
  31. margin: 10px;
  32. padding: 0;
  33. }
  34. a:link {text-decoration: none; font-weight : bold; color : black;}
  35. a:visited {text-decoration: none; font-weight : bold; color : black;}
  36. a:active {text-decoration: none; font-weight : bold; color : black;}
  37. .data_table{
  38. border-collapse: collapse;
  39. width: 100%;
  40. padding: 5px;
  41. border: 1px;
  42. }
  43. .data_table th{
  44. padding: 5px;
  45. vertical-align: top;
  46. border-top: 1px solid black;
  47. border-bottom: 1px solid black;
  48. border-right: 1px solid black;
  49. border-left: 1px solid black;
  50. }
  51. .data_table tr.row_odd{
  52. background-color: #fafafa;
  53. }
  54. .data_table tr.row_even{
  55. background-color: #fff;
  56. }
  57. .data_table td{
  58. padding: 5px;
  59. vertical-align: top;
  60. border-bottom: 1px solid black;
  61. border-right: 1px solid black;
  62. border-left: 1px solid black;
  63. }
  64. </style>
  65. </head>
  66. <body dir="'.api_get_text_direction().'"><div id="main">';
  67. $printdata .= '<h2>'.$view.' : '.$coursename.'</h2>';
  68. //@todo not necessary here
  69. $printdata .= '<table border="1" width="90%" cellspacing="1" cellpadding="1">';
  70. foreach ($header_names as $header) {
  71. $printdata .= '<th>'.$header.'</th>';
  72. }
  73. foreach ($data_array as $data) {
  74. $printdata .= '<tr>';
  75. foreach ($data as $rowdata) {
  76. $printdata .= '<td>'.$rowdata.'</td>';
  77. }
  78. $printdata .= '</tr>';
  79. }
  80. $printdata .= '</table></div></body></html>';
  81. return $printdata;
  82. }
  83. /**
  84. * This function get a content html for export inside a pdf file.
  85. *
  86. * @param array table headers
  87. * @param array table body
  88. * @param array pdf headers
  89. * @param array pdf footers
  90. */
  91. function export_pdf_attendance($headers_table, $data_table, $headers_pdf, $footers_pdf, $title_pdf)
  92. {
  93. $mpdf = new mPDF('UTF-8', 'A4-L', '', '', 15, 10, 35, 20, 4, 2, 'L');
  94. $mpdf->useOnlyCoreFonts = true;
  95. $mpdf->mirrorMargins = 0;
  96. // Use different Odd/Even headers and footers and mirror margins
  97. if (is_array($headers_pdf)) {
  98. // preparing headers pdf
  99. $header = '
  100. <table width="100%" cellspacing="1" cellpadding="1" border="0" class="strong">
  101. <tr>
  102. <td ROWSPAN="3" style="text-align: left;" class="title">
  103. <img src="'.api_get_path(WEB_CSS_PATH).api_get_setting('stylesheets').'/images/header-logo.png">
  104. </td>
  105. <td colspan="3">
  106. <h1>'.$title_pdf.'</h1>
  107. </td>
  108. <tr>
  109. <td></td>
  110. <td><strong>'.$headers_pdf[0][0].'</strong> </td><td> <strong>'.$headers_pdf[0][1].'</strong></td>
  111. <td><strong>'.$headers_pdf[1][0].'</strong> </td><td> <strong>'.$headers_pdf[1][1].'</strong></td>
  112. </tr>
  113. <tr>
  114. <td></td>
  115. <td><strong>'.$headers_pdf[2][0].'</strong> </td><td> <strong>'.$headers_pdf[2][1].'</strong></td>
  116. <td><strong>'.$headers_pdf[3][0].' </strong></td><td> <strong>'.$headers_pdf[3][1].'</strong></td>
  117. </tr>
  118. <tr>
  119. <td></td><td></td>
  120. <td><strong>'.$headers_pdf[4][0].'</strong></td><td> <strong>'.$headers_pdf[4][1].'</strong></td>
  121. <td><strong>'.$headers_pdf[5][0].'</strong> </td><td> <strong>'.$headers_pdf[5][1].'</strong></td>
  122. </tr>
  123. </table>';
  124. }
  125. // preparing footer pdf
  126. $footer = '<table width="100%" cellspacing="2" cellpadding="10" border="0">';
  127. if (is_array($footers_pdf)) {
  128. $footer .= '<tr>';
  129. foreach ($footers_pdf as $foot_pdf) {
  130. $footer .= '<td width="33%" style="text-align: center;">'.$foot_pdf.'</td>';
  131. }
  132. $footer .= '</tr>';
  133. }
  134. $footer .= '</table>';
  135. $footer .= '<div align="right" style="font-weight: bold;">{PAGENO}/{nb}</div>';
  136. // preparing content pdf
  137. $css_file = api_get_path(SYS_CSS_PATH).'themes/'.api_get_setting('stylesheets').'/print.css';
  138. if (file_exists($css_file)) {
  139. $css = @file_get_contents($css_file);
  140. } else {
  141. $css = '';
  142. }
  143. if (count($data_table) > 30) {
  144. $items_per_page = (count($data_table) / 2);
  145. } else {
  146. $items_per_page = count($data_table);
  147. }
  148. $count_pages = ceil(count($data_table) / $items_per_page);
  149. $content_table = '';
  150. for ($x = 0; $x < $count_pages; $x++) {
  151. $content_table .= '<table width="100%" border="1" style="border-collapse:collapse">';
  152. // header table
  153. $content_table .= '<tr>';
  154. $i = 0;
  155. if (is_array($headers_table)) {
  156. foreach ($headers_table as $head_table) {
  157. if (!empty($head_table[0])) {
  158. $width = (!empty($head_table[1]) ? $head_table[1].'%' : '');
  159. $content_table .= '<th width="'.$width.'">'.$head_table[0].'</th>';
  160. $i++;
  161. }
  162. }
  163. }
  164. $content_table .= '</tr>';
  165. // body table
  166. if (is_array($data_table) && count($data_table) > 0) {
  167. $offset = $x * $items_per_page;
  168. $data_table = array_slice($data_table, $offset, count($data_table));
  169. $i = 1;
  170. $item = $offset + 1;
  171. foreach ($data_table as $data) {
  172. $content_table .= '<tr>';
  173. $content_table .= '<td>'.($item < 10 ? '0'.$item : $item).'</td>';
  174. foreach ($data as $key => $content) {
  175. if (isset($content)) {
  176. $key == 1 ? $align = 'align="left"' : $align = 'align="center"';
  177. $content_table .= '<td '.$align.' style="padding:4px;" >'.$content.'</td>';
  178. }
  179. }
  180. $content_table .= '</tr>';
  181. $i++;
  182. $item++;
  183. if ($i > $items_per_page) {
  184. break;
  185. }
  186. }
  187. } else {
  188. $content_table .= '<tr colspan="'.$i.'"><td>'.get_lang('Empty').'</td></tr>';
  189. }
  190. $content_table .= '</table>';
  191. if ($x < ($count_pages - 1)) {
  192. $content_table .= '<pagebreak />';
  193. }
  194. }
  195. $html = $content_table;
  196. // set attributes for pdf
  197. $mpdf->SetHTMLHeader($header);
  198. $mpdf->SetHTMLFooter($footer);
  199. if (!empty($css)) {
  200. $mpdf->WriteHTML($css, 1);
  201. $mpdf->WriteHTML($html, 2);
  202. } else {
  203. $mpdf->WriteHTML($html);
  204. }
  205. $mpdf->Output(api_replace_dangerous_char($title_pdf.'.pdf'), 'D');
  206. exit;
  207. }
  208. /**
  209. * This function get a content html for export inside a pdf file.
  210. *
  211. * @param array table headers
  212. * @param array table body
  213. * @param array pdf headers
  214. * @param array pdf footers
  215. */
  216. function export_pdf_with_html($headers_table, $data_table, $headers_pdf, $footers_pdf, $title_pdf)
  217. {
  218. $headers_in_pdf = '<img src="'.api_get_path(WEB_CSS_PATH).api_get_setting('stylesheets').'/images/header-logo.png">';
  219. if (is_array($headers_pdf)) {
  220. // preparing headers pdf
  221. $header = '<br/><br/>
  222. <table width="100%" cellspacing="1" cellpadding="5" border="0" class="strong">
  223. <tr>
  224. <td width="100%" style="text-align: center;" class="title" colspan="4">
  225. <h1>'.$title_pdf.'</h1></td></tr>';
  226. foreach ($headers_pdf as $header_pdf) {
  227. if (!empty($header_pdf[0]) && !empty($header_pdf[1])) {
  228. $header .= '<tr><td><strong>'.$header_pdf[0].'</strong> </td><td>'.$header_pdf[1].'</td></tr>';
  229. }
  230. }
  231. $header .= '</table><br />';
  232. }
  233. // preparing footer pdf
  234. $footer = '<table width="100%" cellspacing="2" cellpadding="10" border="0">';
  235. if (is_array($footers_pdf)) {
  236. $footer .= '<tr>';
  237. foreach ($footers_pdf as $foot_pdf) {
  238. $footer .= '<td width="33%" style="text-align: center;">'.$foot_pdf.'</td>';
  239. }
  240. $footer .= '</tr>';
  241. }
  242. $footer .= '</table>';
  243. $footer .= '<div align="right" style="font-weight: bold;">{PAGENO}/{nb}</div>';
  244. // preparing content pdf
  245. $css_file = api_get_path(SYS_CSS_PATH).'themes/'.api_get_setting('stylesheets').'/print.css';
  246. if (file_exists($css_file)) {
  247. $css = @file_get_contents($css_file);
  248. } else {
  249. $css = '';
  250. }
  251. $items_per_page = 30;
  252. $count_pages = ceil(count($data_table) / $items_per_page);
  253. for ($x = 0; $x < $count_pages; $x++) {
  254. $content_table .= '<table width="100%" border="1" style="border-collapse:collapse">';
  255. // header table
  256. $content_table .= '<tr>';
  257. $i = 0;
  258. if (is_array($headers_table)) {
  259. foreach ($headers_table as $head_table) {
  260. if (!empty($head_table[0])) {
  261. $width = (!empty($head_table[1]) ? $head_table[1].'%' : '');
  262. $content_table .= '<th width="'.$width.'">'.$head_table[0].'</th>';
  263. $i++;
  264. }
  265. }
  266. }
  267. $content_table .= '</tr>';
  268. // body table
  269. if (is_array($data_table) && count($data_table) > 0) {
  270. $offset = $x * $items_per_page;
  271. $data_table = array_slice($data_table, $offset, count($data_table));
  272. $i = 1;
  273. $item = $offset + 1;
  274. foreach ($data_table as $data) {
  275. $content_table .= '<tr>';
  276. $content_table .= '<td>'.($item < 10 ? '0'.$item : $item).'</td>';
  277. foreach ($data as $key => $content) {
  278. if (isset($content)) {
  279. $key == 1 ? $align = 'align="left"' : $align = 'align="center"';
  280. $content_table .= '<td '.$align.' style="padding:4px;" >'.$content.'</td>';
  281. }
  282. }
  283. $content_table .= '</tr>';
  284. $i++;
  285. $item++;
  286. if ($i > $items_per_page) {
  287. break;
  288. }
  289. }
  290. } else {
  291. $content_table .= '<tr colspan="'.$i.'"><td>'.get_lang('Empty').'</td></tr>';
  292. }
  293. $content_table .= '</table>';
  294. if ($x < ($count_pages - 1)) {
  295. $content_table .= '<pagebreak />';
  296. }
  297. }
  298. $pdf = new PDF();
  299. $pdf->set_custom_footer($footer);
  300. $pdf->set_custom_header($headers_in_pdf);
  301. $pdf->content_to_pdf($header.$content_table, $css, $title_pdf);
  302. exit;
  303. }
  304. /**
  305. * Exports the data as a table on a PDF page.
  306. *
  307. * @param resource The PDF object (ezpdf class) used to generate the file
  308. * @param array The data array
  309. * @param array Table headers
  310. * @param string Format (portrait or landscape)
  311. */
  312. function export_pdf($pdf, $newarray, $header_names, $format)
  313. {
  314. $pdf->selectFont(api_get_path(LIBRARY_PATH).'ezpdf/fonts/Courier.afm');
  315. $pdf->ezSetCmMargins(0, 0, 0, 0);
  316. $pdf->ezSetY(($format == 'portrait') ? '820' : '570');
  317. $pdf->selectFont(api_get_path(LIBRARY_PATH).'ezpdf/fonts/Courier.afm');
  318. if ($format == 'portrait') {
  319. $pdf->line(40, 790, 540, 790);
  320. $pdf->line(40, 40, 540, 40);
  321. } else {
  322. $pdf->line(40, 540, 790, 540);
  323. $pdf->line(40, 40, 790, 40);
  324. }
  325. $pdf->ezSetY(($format == 'portrait') ? '750' : '520');
  326. $pdf->ezTable($newarray, $header_names, '', [
  327. 'showHeadings' => 1,
  328. 'shaded' => 1,
  329. 'showLines' => 1,
  330. 'rowGap' => 3,
  331. 'width' => (($format == 'portrait') ? '500' : '750'),
  332. ]);
  333. $pdf->ezStream();
  334. }