exportgradebook.php 12 KB

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