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