exportgradebook.php 11 KB

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