Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  
   3  /**
   4   * @version   v5.20.16  12-Jan-2020
   5   * @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
   6   * @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
   7   * Released under both BSD license and Lesser GPL library license.
   8   * Whenever there is any discrepancy between the two licenses,
   9   * the BSD license will take precedence.
  10   *
  11   * Code to export recordsets in several formats:
  12   *
  13   * AS VARIABLE
  14   * $s = rs2csv($rs); # comma-separated values
  15   * $s = rs2tab($rs); # tab delimited
  16   *
  17   * TO A FILE
  18   * $f = fopen($path,'w');
  19   * rs2csvfile($rs,$f);
  20   * fclose($f);
  21   *
  22   * TO STDOUT
  23   * rs2csvout($rs);
  24   */
  25  
  26  // returns a recordset as a csv string
  27  function rs2csv(&$rs,$addtitles=true)
  28  {
  29  	 return _adodb_export($rs,',',',',false,$addtitles);
  30  }
  31  
  32  // writes recordset to csv file
  33  function rs2csvfile(&$rs,$fp,$addtitles=true)
  34  {
  35  	 _adodb_export($rs,',',',',$fp,$addtitles);
  36  }
  37  
  38  // write recordset as csv string to stdout
  39  function rs2csvout(&$rs,$addtitles=true)
  40  {
  41  	 $fp = fopen('php://stdout','wb');
  42  	 _adodb_export($rs,',',',',true,$addtitles);
  43  	 fclose($fp);
  44  }
  45  
  46  function rs2tab(&$rs,$addtitles=true)
  47  {
  48  	 return _adodb_export($rs,"\t",',',false,$addtitles);
  49  }
  50  
  51  // to file pointer
  52  function rs2tabfile(&$rs,$fp,$addtitles=true)
  53  {
  54  	 _adodb_export($rs,"\t",',',$fp,$addtitles);
  55  }
  56  
  57  // to stdout
  58  function rs2tabout(&$rs,$addtitles=true)
  59  {
  60  	 $fp = fopen('php://stdout','wb');
  61  	 _adodb_export($rs,"\t",' ',true,$addtitles);
  62  	 if ($fp) fclose($fp);
  63  }
  64  
  65  function _adodb_export(&$rs,$sep,$sepreplace,$fp=false,$addtitles=true,$quote = '"',$escquote = '"',$replaceNewLine = ' ')
  66  {
  67  	 if (!$rs) return '';
  68  	 //----------
  69  	 // CONSTANTS
  70  	 $NEWLINE = "\r\n";
  71  	 $BUFLINES = 100;
  72  	 $escquotequote = $escquote.$quote;
  73  	 $s = '';
  74  
  75  	 if ($addtitles) {
  76  	 	 $fieldTypes = $rs->FieldTypesArray();
  77  	 	 reset($fieldTypes);
  78  	 	 $i = 0;
  79  	 	 $elements = array();
  80  	 	 foreach ($fieldTypes as $o) {
  81  
  82  	 	 	 $v = ($o) ? $o->name : 'Field'.($i++);
  83  	 	 	 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
  84  	 	 	 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
  85  	 	 	 $elements[] = $v;
  86  
  87  	 	 }
  88  	 	 $s .= implode($sep, $elements).$NEWLINE;
  89  	 }
  90  	 $hasNumIndex = isset($rs->fields[0]);
  91  
  92  	 $line = 0;
  93  	 $max = $rs->FieldCount();
  94  
  95  	 while (!$rs->EOF) {
  96  	 	 $elements = array();
  97  	 	 $i = 0;
  98  
  99  	 	 if ($hasNumIndex) {
 100  	 	 	 for ($j=0; $j < $max; $j++) {
 101  	 	 	 	 $v = $rs->fields[$j];
 102  	 	 	 	 if (!is_object($v)) $v = trim($v);
 103  	 	 	 	 else $v = 'Object';
 104  	 	 	 	 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
 105  	 	 	 	 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
 106  
 107  	 	 	 	 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
 108  	 	 	 	 else $elements[] = $v;
 109  	 	 	 }
 110  	 	 } else { // ASSOCIATIVE ARRAY
 111  	 	 	 foreach($rs->fields as $v) {
 112  	 	 	 	 if ($escquote) $v = str_replace($quote,$escquotequote,trim($v));
 113  	 	 	 	 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
 114  
 115  	 	 	 	 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
 116  	 	 	 	 else $elements[] = $v;
 117  	 	 	 }
 118  	 	 }
 119  	 	 $s .= implode($sep, $elements).$NEWLINE;
 120  	 	 $rs->MoveNext();
 121  	 	 $line += 1;
 122  	 	 if ($fp && ($line % $BUFLINES) == 0) {
 123  	 	 	 if ($fp === true) echo $s;
 124  	 	 	 else fwrite($fp,$s);
 125  	 	 	 $s = '';
 126  	 	 }
 127  	 }
 128  
 129  	 if ($fp) {
 130  	 	 if ($fp === true) echo $s;
 131  	 	 else fwrite($fp,$s);
 132  	 	 $s = '';
 133  	 }
 134  
 135  	 return $s;
 136  }