remove old readme
[atutor.git] / docs / include / classes / CSVExport.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12 // $Id$
13
14 class CSVExport {
15 //      var $quote_search  = array('"',  "\n", "\r", "\x00");
16 //      var $quote_replace = array('""', '\n', '\r', '\0');
17         var $quote_search  = array('"', "\n", "\r", "\x00");
18         var $quote_replace = array('""', '\n', '\r', '\0');
19
20         // constructor
21         function CSVExport() { }
22
23         // public
24         function export($sql, $course_id) {
25                 global $db;
26                 $sql = str_replace('?', $course_id , $sql);
27
28                 $content = '';
29
30                 $result = mysql_query($sql, $db);
31
32                 $field_types = $this->detectFieldTypes($result);
33                 if (!$field_types) {
34                         return FALSE;
35                 }
36                 $num_fields = count($field_types);
37
38                 while ($row = mysql_fetch_row($result)) {
39                         for ($i=0; $i < $num_fields; $i++) {
40                                 if ($types[$i] == 'int' || $types[$i] == 'real') {
41                                         $content .= $row[$i] . ',';
42                                 } else {
43                                         $content .= $this->quote($row[$i]) . ',';
44                                 }
45                         }
46                         $content = substr($content, 0, -1);
47                         $content .= "\n";
48                 }
49                 
50                 @mysql_free_result($result);
51
52                 return $content;
53         }
54
55         // public
56         // given a query result returns an array of field types.
57         // possible field types are int, string, datetime, or blob...
58         function detectFieldTypes(&$result) {
59                 $field_types = array();
60                 $num_fields = @mysql_num_fields($result);
61
62                 if (!$num_fields) {
63                         return array();
64                 }
65
66                 for ($i=0; $i< $num_fields; $i++) {
67                         $field_types[] = mysql_field_type($result, $i);
68                 }
69                 return $field_types;
70         }
71
72         // private
73         // quote $line so that it's safe to save as a CSV field
74         function quote($line) {
75                 return '"'.str_replace($this->quote_search, $this->quote_replace, $line).'"';
76         }
77 }
78
79 ?>