exportgradebook.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_with_html($headers_table, $data_table, $headers_pdf, $footers_pdf, $title_pdf)
  92. {
  93. $headers_in_pdf = '<img src="'.api_get_path(WEB_CSS_PATH).api_get_setting('stylesheets').'/images/header-logo.png">';
  94. if (is_array($headers_pdf)) {
  95. // preparing headers pdf
  96. $header = '<br/><br/>
  97. <table width="100%" cellspacing="1" cellpadding="5" border="0" class="strong">
  98. <tr>
  99. <td width="100%" style="text-align: center;" class="title" colspan="4">
  100. <h1>'.$title_pdf.'</h1></td></tr>';
  101. foreach ($headers_pdf as $header_pdf) {
  102. if (!empty($header_pdf[0]) && !empty($header_pdf[1])) {
  103. $header .= '<tr><td><strong>'.$header_pdf[0].'</strong> </td><td>'.$header_pdf[1].'</td></tr>';
  104. }
  105. }
  106. $header .= '</table><br />';
  107. }
  108. // preparing footer pdf
  109. $footer = '<table width="100%" cellspacing="2" cellpadding="10" border="0">';
  110. if (is_array($footers_pdf)) {
  111. $footer .= '<tr>';
  112. foreach ($footers_pdf as $foot_pdf) {
  113. $footer .= '<td width="33%" style="text-align: center;">'.$foot_pdf.'</td>';
  114. }
  115. $footer .= '</tr>';
  116. }
  117. $footer .= '</table>';
  118. $footer .= '<div align="right" style="font-weight: bold;">{PAGENO}/{nb}</div>';
  119. // preparing content pdf
  120. $css_file = api_get_path(SYS_CSS_PATH).'themes/'.api_get_setting('stylesheets').'/print.css';
  121. if (file_exists($css_file)) {
  122. $css = @file_get_contents($css_file);
  123. } else {
  124. $css = '';
  125. }
  126. $items_per_page = 30;
  127. $count_pages = ceil(count($data_table) / $items_per_page);
  128. for ($x = 0; $x < $count_pages; $x++) {
  129. $content_table .= '<table width="100%" border="1" style="border-collapse:collapse">';
  130. // header table
  131. $content_table .= '<tr>';
  132. $i = 0;
  133. if (is_array($headers_table)) {
  134. foreach ($headers_table as $head_table) {
  135. if (!empty($head_table[0])) {
  136. $width = (!empty($head_table[1]) ? $head_table[1].'%' : '');
  137. $content_table .= '<th width="'.$width.'">'.$head_table[0].'</th>';
  138. $i++;
  139. }
  140. }
  141. }
  142. $content_table .= '</tr>';
  143. // body table
  144. if (is_array($data_table) && count($data_table) > 0) {
  145. $offset = $x * $items_per_page;
  146. $data_table = array_slice($data_table, $offset, count($data_table));
  147. $i = 1;
  148. $item = $offset + 1;
  149. foreach ($data_table as $data) {
  150. $content_table .= '<tr>';
  151. $content_table .= '<td>'.($item < 10 ? '0'.$item : $item).'</td>';
  152. foreach ($data as $key => $content) {
  153. if (isset($content)) {
  154. $key == 1 ? $align = 'align="left"' : $align = 'align="center"';
  155. $content_table .= '<td '.$align.' style="padding:4px;" >'.$content.'</td>';
  156. }
  157. }
  158. $content_table .= '</tr>';
  159. $i++;
  160. $item++;
  161. if ($i > $items_per_page) {
  162. break;
  163. }
  164. }
  165. } else {
  166. $content_table .= '<tr colspan="'.$i.'"><td>'.get_lang('You left some fields empty.<br>Use the <b>Back</b> button on your browser and try again.<br>If you ignore your training code, see the Training Program').'</td></tr>';
  167. }
  168. $content_table .= '</table>';
  169. if ($x < ($count_pages - 1)) {
  170. $content_table .= '<pagebreak />';
  171. }
  172. }
  173. $pdf = new PDF();
  174. $pdf->set_custom_footer($footer);
  175. $pdf->set_custom_header($headers_in_pdf);
  176. $pdf->content_to_pdf($header.$content_table, $css, $title_pdf);
  177. exit;
  178. }
  179. /**
  180. * Exports the data as a table on a PDF page.
  181. *
  182. * @param resource The PDF object (ezpdf class) used to generate the file
  183. * @param array The data array
  184. * @param array Table headers
  185. * @param string Format (portrait or landscape)
  186. */
  187. function export_pdf($pdf, $newarray, $header_names, $format)
  188. {
  189. $pdf->selectFont(api_get_path(LIBRARY_PATH).'ezpdf/fonts/Courier.afm');
  190. $pdf->ezSetCmMargins(0, 0, 0, 0);
  191. $pdf->ezSetY(($format == 'portrait') ? '820' : '570');
  192. $pdf->selectFont(api_get_path(LIBRARY_PATH).'ezpdf/fonts/Courier.afm');
  193. if ($format == 'portrait') {
  194. $pdf->line(40, 790, 540, 790);
  195. $pdf->line(40, 40, 540, 40);
  196. } else {
  197. $pdf->line(40, 540, 790, 540);
  198. $pdf->line(40, 40, 790, 40);
  199. }
  200. $pdf->ezSetY(($format == 'portrait') ? '750' : '520');
  201. $pdf->ezTable($newarray, $header_names, '', [
  202. 'showHeadings' => 1,
  203. 'shaded' => 1,
  204. 'showLines' => 1,
  205. 'rowGap' => 3,
  206. 'width' => (($format == 'portrait') ? '500' : '750'),
  207. ]);
  208. $pdf->ezStream();
  209. }