2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto */
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 /************************************************************************/
16 var $quote_search = array('""', '\\\n', '\\\r');
17 var $quote_replace = array('"', '\n', '\r');
20 function CSVImport() { }
23 // returns the primary_key, or false if there is none, or null if more than 1
24 function getPrimaryFieldName($table_name) {
29 $sql = "SELECT * FROM ".TABLE_PREFIX.$table_name .' WHERE 0';
30 $result = mysql_query($sql, $db);
31 $num_fields = mysql_num_fields($result);
32 for ($i= 0; $i<$num_fields; $i++) {
33 $flags = explode(' ', mysql_field_flags($result, $i));
34 if (in_array('primary_key', $flags)) {
35 if ($field == false) {
36 $field = mysql_field_name($result, $i);
38 // there is more than one primary_key
48 // given a query result returns an array of field types.
49 // possible field types are int, string, datetime, or blob...
50 function detectFieldTypes($table_name) {
53 $field_types = array();
55 $sql = "SELECT * FROM ".TABLE_PREFIX.$table_name .' WHERE 0';
56 $result = @mysql_query($sql, $db);
60 $num_fields = mysql_num_fields($result);
62 for ($i=0; $i< $num_fields; $i++) {
63 $field_types[] = mysql_field_type($result, $i);
69 function translateWhitespace($input) {
70 $input = addslashes($input);
71 $input = str_replace($this->quote_search, $this->quote_replace, $input);
77 function import($tableName, $path, $course_id, $version) {
81 $fn_name = $tableName.'_convert';
84 $lock_sql = 'LOCK TABLES ' . TABLE_PREFIX . $tableName. ', ' . TABLE_PREFIX . 'courses WRITE';
85 $result = mysql_query($lock_sql, $db);
87 // get the field types
88 $field_types = $this->detectFieldTypes($tableName);
93 // get the name of the primary field
94 $primary_key_field_name = $this->getPrimaryFieldName($tableName);
95 // read the rows into an array
96 $fp = @fopen($path . $tableName . '.csv', 'rb');
99 // get the name of the primary ID field and the next index
101 if ($primary_key_field_name) {
102 // get the next primary ID
103 $sql = 'SELECT MAX(' . $primary_key_field_name . ') AS next_id FROM ' . TABLE_PREFIX . $tableName;
104 $result = mysql_query($sql, $db);
105 $next_id = mysql_fetch_assoc($result);
106 $next_id = $next_id['next_id']+1;
110 while ($row = @fgetcsv($fp, 70000)) {
111 if (count($row) && (trim($row[0]) == '')) {
115 if (function_exists($fn_name)) {
116 $row = $fn_name($row, $course_id, $table_id_map, $version);
125 $table_id_map[$tableName][$row[0]] = $next_id;
126 if ($primary_key_field_name != NULL) {
130 $sql = 'REPLACE INTO '.TABLE_PREFIX.$tableName.' VALUES (';
132 foreach($row as $id => $field) {
133 if (($field_types[$id] != 'int') && ($field_types[$id] != 'real')) {
134 $field = $this->translateWhitespace($field);
135 } else if ($field_types[$id] == 'int') {
136 $field = intval($field);
138 $sql .= "'" . $field."',";
140 $sql = substr($sql, 0, -1);
143 $result = mysql_query($sql, $db);
152 $lock_sql = 'UNLOCK TABLES';
153 $result = mysql_query($lock_sql, $db);