exportgradebook.php 12 KB

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