export.lib.inc.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /* See license terms in /license.txt */
  3. /**
  4. * This is the export library for Chamilo.
  5. * Include/require it in your code to use its functionality.
  6. *
  7. * Several functions below are adaptations from functions distributed by www.nexen.net
  8. *
  9. * @package chamilo.library
  10. */
  11. /**
  12. * Code
  13. */
  14. require_once 'document.lib.php';
  15. require_once api_get_path(LIBRARY_PATH).'pdf.lib.php';
  16. /**
  17. *
  18. * @package chamilo.library
  19. */
  20. class Export {
  21. private function __construct() {
  22. }
  23. /**
  24. * Export tabular data to CSV-file
  25. * @param array $data
  26. * @param string $filename
  27. */
  28. public static function export_table_csv ($data, $filename = 'export') {
  29. $file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.csv';
  30. $handle = @fopen($file, 'a+');
  31. if(is_array($data)) {
  32. foreach ($data as $index => $row) {
  33. $line = '';
  34. if(is_array($row)) {
  35. foreach($row as $value) {
  36. $line .= '"'.str_replace('"', '""', $value).'";';
  37. }
  38. }
  39. @fwrite($handle, $line."\n");
  40. }
  41. }
  42. @fclose($handle);
  43. DocumentManager :: file_send_for_download($file, true, $filename.'.csv');
  44. return false;
  45. }
  46. /**
  47. * Export tabular data to XLS-file
  48. * @param array $data
  49. * @param string $filename
  50. */
  51. public static function export_table_xls ($data, $filename = 'export') {
  52. $file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls';
  53. $handle = @fopen($file, 'a+');
  54. foreach ($data as $index => $row) {
  55. @fwrite($handle, implode("\t", $row)."\n");
  56. }
  57. @fclose($handle);
  58. DocumentManager :: file_send_for_download($file, true, $filename.'.xls');
  59. return false;
  60. }
  61. /**
  62. * Export tabular data to XML-file
  63. * @param array Simple array of data to put in XML
  64. * @param string Name of file to be given to the user
  65. * @param string Name of common tag to place each line in
  66. * @param string Name of the root element. A root element should always be given.
  67. * @param string Encoding in which the data is provided
  68. */
  69. public static function export_table_xml ($data, $filename = 'export', $item_tagname = 'item', $wrapper_tagname = null, $encoding = null) {
  70. if (empty($encoding)) {
  71. $encoding = api_get_system_encoding();
  72. }
  73. $file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml';
  74. $handle = fopen($file, 'a+');
  75. fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n");
  76. if (!is_null($wrapper_tagname)) {
  77. fwrite($handle, "\t".'<'.$wrapper_tagname.'>'."\n");
  78. }
  79. foreach ($data as $index => $row) {
  80. fwrite($handle, '<'.$item_tagname.'>'."\n");
  81. foreach ($row as $key => $value) {
  82. fwrite($handle, "\t\t".'<'.$key.'>'.$value.'</'.$key.'>'."\n");
  83. }
  84. fwrite($handle, "\t".'</'.$item_tagname.'>'."\n");
  85. }
  86. if (!is_null($wrapper_tagname)) {
  87. fwrite($handle, '</'.$wrapper_tagname.'>'."\n");
  88. }
  89. fclose($handle);
  90. DocumentManager :: file_send_for_download($file, true, $filename.'.xml');
  91. return false;
  92. }
  93. /**
  94. * Export hierarchical tabular data to XML-file
  95. * @param array Hierarchical array of data to put in XML, each element presenting a 'name' and a 'value' property
  96. * @param string Name of file to be given to the user
  97. * @param string Name of common tag to place each line in
  98. * @param string Name of the root element. A root element should always be given.
  99. * @param string Encoding in which the data is provided
  100. * @return void Prompts the user for a file download
  101. */
  102. public static function export_complex_table_xml ($data, $filename = 'export', $wrapper_tagname, $encoding = 'ISO-8859-1') {
  103. $file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml';
  104. $handle = fopen($file, 'a+');
  105. fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n");
  106. if (!is_null($wrapper_tagname)) {
  107. fwrite($handle, '<'.$wrapper_tagname.'>');
  108. }
  109. $s = self::_export_complex_table_xml_helper($data);
  110. fwrite($handle,$s);
  111. if (!is_null($wrapper_tagname)) {
  112. fwrite($handle, '</'.$wrapper_tagname.'>'."\n");
  113. }
  114. fclose($handle);
  115. DocumentManager :: file_send_for_download($file, true, $filename.'.xml');
  116. return false;
  117. }
  118. /**
  119. * Helper for the hierarchical XML exporter
  120. * @param array Hierarhical array composed of elements of type ('name'=>'xyz','value'=>'...')
  121. * @param int Level of recursivity. Allows the XML to be finely presented
  122. * @return string The XML string to be inserted into the root element
  123. */
  124. public static function _export_complex_table_xml_helper ($data, $level = 1) {
  125. if (count($data)<1) { return '';}
  126. $string = '';
  127. foreach ($data as $index => $row) {
  128. $string .= "\n".str_repeat("\t",$level).'<'.$row['name'].'>';
  129. if (is_array($row['value'])) {
  130. $string .= self::_export_complex_table_xml_helper($row['value'],$level+1)."\n";
  131. $string .= str_repeat("\t",$level).'</'.$row['name'].'>';
  132. } else {
  133. $string .= $row['value'];
  134. $string .= '</'.$row['name'].'>';
  135. }
  136. }
  137. return $string;
  138. }
  139. public static function export_table_pdf($data, $file_name = 'export', $header, $description, $params = array()) {
  140. $headers = $data[0];
  141. unset($data[0]);
  142. $html = '';
  143. $headers_in_pdf = '<img width="150px" src="'.api_get_path(WEB_CSS_PATH).api_get_setting('stylesheets').'/images/header-logo.png">';
  144. $html = '<br/><table width="100%" cellspacing="1" cellpadding="5" border="0">
  145. <tr><td width="100%" style="text-align: center;" class="title" colspan="4"><h1>'.$header.'</h1></td></tr></table><br />';
  146. $html .= $description.'<br />';
  147. $table = new HTML_Table(array('class' => 'data_table', 'repeat_header' => '1'));
  148. $row = 0;
  149. $column = 0;
  150. foreach ($headers as $header) {
  151. $table->setHeaderContents($row, $column, $header);
  152. $column++;
  153. }
  154. $row++;
  155. foreach ($data as &$printable_data_row) {
  156. $column = 0;
  157. foreach ($printable_data_row as &$printable_data_cell) {
  158. $table->setCellContents($row, $column, $printable_data_cell);
  159. //$table->updateCellAttributes($row, $column);
  160. $column++;
  161. }
  162. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  163. $row++;
  164. }
  165. $html .= $table->toHtml();
  166. $html = api_utf8_encode($html);
  167. $css_file = api_get_path(TO_SYS, WEB_CSS_PATH).'/print.css';
  168. $css = file_exists($css_file) ? @file_get_contents($css_file) : '';
  169. $pdf = new PDF('A4', 'P', $params);
  170. $pdf->set_custom_header($headers_in_pdf);
  171. $pdf->content_to_pdf($html, $css, $file_name, api_get_course_id());
  172. exit;
  173. }
  174. }