C:\$Recycle.Bin\S-1-5-21-3967099092-2644009230-141905953-500\$RAQKNYH\admin\importoldresponses.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/*
* LimeSurvey
* Copyright (C) 2007 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.

* $Id: importoldresponses.php 6730 2009-04-27 13:40:10Z wahrendorff $
*/


//import responses from an old_ survey table into an active survey
include_once("login_check.php");        

if (!isset(
$oldtable)) {$oldtable=returnglobal('oldtable');}
if (!isset(
$surveyid)) {$surveyid=returnglobal('sid');}


if (!
$subaction == "import")
{
    
// show UI for choosing old table

    
$query db_select_tables_like("{$dbprefix}old\_survey\_%");
    
$result db_execute_num($query) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
    
$optionElements '';
    
$queryCheckColumnsActive "SELECT * FROM {$dbprefix}survey_{$surveyid} ";
    
$resultActive db_execute_num($queryCheckColumnsActive) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
    
$countActive $resultActive->FieldCount(); 
    
    while (
$row $result->FetchRow())
    {
        
$queryCheckColumnsOld "SELECT * FROM {$row[0]} ";
        
        
$resultOld db_execute_num($queryCheckColumnsOld) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
        
        if(
$countActive== $resultOld->FieldCount())
        {
            
$optionElements .= "\t\t\t<option value='{$row[0]}'>{$row[0]}</option>\n";
        }
    }

    
//Get the menubar
    
$importoldresponsesoutput browsemenubar($clang->gT("Quick Statistics"));
    
$importoldresponsesoutput .= "<br /><table align='center' class='outlinetable'>
        <tr>
            <th colspan='2'>"
.$clang->gT("Import responses from an old (deactivated) survey table into an active survey")."</th>
        </tr>
        <form method='post'>
        <tr>
         <td align='right'>"
.$clang->gT("Target Survey ID")."</td>
         <td> 
$surveyid<input type='hidden' value='$surveyid' name='sid'></td>
        </tr>
        <tr>
         <td align='right'>
          "
.$clang->gT("Source table").":
         </td>
         <td>
          <select name='oldtable' >
{$optionElements}
          </select>
         </td>
        </tr>
        <tr>
         <td colspan='2' align='center'>
          <input type='submit' value='"
.$clang->gT("Import Responses")."' onclick='return confirm(\"".$clang->gT("Are you sure?","js").")'>&nbsp;
            <input type='hidden' name='subaction' value='import'>
         </td>
        </tr>
        <tr>
            <td colspan='2' align='center'>
            <font style='color:red;font-weight:bold;'>"
.$clang->gT("Warning: You can import all old responses with the same amount of columns as in your active survey. YOU have to make sure, that this responses corresponds to the questions in your active survey.")."</font>
            </td>
        </tr>
        </form>
        </table><br />&nbsp;"
;
}
elseif (isset(
$surveyid) && $surveyid && isset($oldtable))
{
    
/*
     * TODO:
     * - mysql fit machen
     * -- quotes für mysql beachten --> ` 
     * - warnmeldung mehrsprachig
     * - testen
     */
//    if($databasetype=="postgres")
//    {
        
$activetable "{$dbprefix}survey_$surveyid";
        
        
//Fields we don't want to import
        
$dontimportfields = array(
        
'id' //,'otherfield'
        
);
        
        
// fields we can import
        
$importablefields = array();
        if(
$databasetype=="postgres")
        {
            
$query "SELECT column_name as field FROM information_schema.columns WHERE table_name = '{$activetable}' ";
        }
        else
        {
            
$query "SHOW COLUMNS FROM {$activetable}";
        }
        
        
$result db_execute_assoc($query) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());

        while (
$row $result->FetchRow())
        {
            if(
$databasetype=="postgres")
            {
                if (!
in_array($row['field'],$dontimportfields))
                {
                    
$importablefields[] = $row['field'];
                }
            }
            else
            {
            if (!
in_array($row['Field'],$dontimportfields))
                {
                    
$importablefields[] = $row['Field'];
                }
            }
        }
        foreach (
$importablefields as $field => $value)
        {
            
$fields2insert[]=($databasetype=="postgres") ? "\"".$value."\"" "`".$value."`";
//            if($databasetype=="postgres") { $fields2insert[] = "\"".$value."\""; }
        
}
        
        
//fields we can supply //fields in the old database
        
$availablefields = array();

        if(
$databasetype=="postgres")
        {
            
$query "SELECT column_name as field FROM information_schema.columns WHERE table_name = '{$oldtable}' ";
        }
        else
        {
            
$query "SHOW COLUMNS FROM {$oldtable}";
        }
        
$result db_execute_assoc($query) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
        
        while (
$row $result->FetchRow())
        {
            if(
$databasetype=="postgres")
            {
                if (!
in_array($row['field'],$dontimportfields))
                {
                    
$availablefields[] = $row['field'];
                }
            }
            else
            {
                if (!
in_array($row['Field'],$dontimportfields))
                {
                    
$availablefields[] = $row['Field'];
                }
            }

        }
        
        foreach (
$availablefields as $field => $value)
        {
            if(
$databasetype=="postgres")
            {
                
$fields2import[] = "\"".$value."\"";
            }
            else
            {
                
$fields2import[] = '`'.$value.'`'
            }
        }
        
        if(
count($fields2insert)!=count($fields2import))
        { 
// nicht mehr gebraucht, da nur alte Datenbanken mit gleicher Spaltenanzahl angezeigt werden
            
print_r($fields2insert);
            
print_r($fields2import);
            echo 
"Entschuldigung, die Spaltenanzahl stimmt nicht überein. Bitte im Browser \"zurück\" klicken.";
            echo 
"Sorry, a count of columns mismatch prevents the old Responses from importing.";
            exit;
        }
        
$queryOldValues "SELECT ".implode(", ",$fields2import)." "
                        
"FROM {$oldtable} ";
        
$resultOldValues db_execute_assoc($queryOldValues) or safe_die("Error:<br />$queryOldValues<br />".$connect->ErrorMsg());
        
        while (
$row $resultOldValues->FetchRow())
        {
            
$values2import = array();
            foreach(
$row as $fieldName => $fieldValue)
            {
                if( 
$fieldValue=="")
                {
                    
$values2import[] = "NULL";
                }
                else
                {
                    if(!
is_numeric($fieldValue))
                        
$values2import[] = "'".db_quote($fieldValue)."'";
                    else
                        
$values2import[] = "".$fieldValue."";
                }
            }    
    
            
$insertOldValues "INSERT INTO {$activetable} ( ".implode(", ",$fields2insert).") "
                             
"VALUES( ".implode(", ",$values2import)."); ";         
            
$result $connect->Execute($insertOldValues) or safe_die("Error:<br />$insertOldValues<br />".$connect->ErrorMsg());
        }

//    }
//    else
//    {
//        // options (UI not implemented)
//    
//        $dontimportfields = array(
//        'id' //,'otherfield'
//        );
//        $presetfields = array( // quote all strings so we can allow NULL
//        //'4X13X951'=>"'Y'"
//        //'id' => "NULL"
//        );
//        $importidrange = false; //array('first'=>3,'last'=>10);
//    
//        $activetable = "{$dbprefix}survey_$surveyid";
//    
//        // fields we can import
//        $importablefields = array();
//        $query = "SHOW COLUMNS FROM {$activetable}";
//        $result = db_execute_assoc($query) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
//        while ($row = $result->FetchRow())
//        {
//            if (!in_array($row['Field'],$dontimportfields))
//            {
//                $importablefields[] = $row['Field'];
//            }
//        }
//    
//        // fields we can supply
//        $availablefields = array();
//        $query = "SHOW COLUMNS FROM {$oldtable}";
//        $result = db_execute_assoc($query) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
//        while ($row = $result->FetchRow())
//        {
//            $availablefields[] = $row['Field'];
//        }
//        foreach ($presetfields as $field => $value)
//        {
//            if (!in_array($field,$availablefields))
//            {
//                $availablefields[] = $field;
//            }
//        }
//    
//        $fieldstoimport = array_intersect($importablefields,$availablefields);
//    
//        // data sources for each field (field of oldtable or preset value)
//        $sourcefields = array();
//        foreach ($fieldstoimport as $field)
//        {
//            $sourcefields[] = array_key_exists($field,$presetfields)?
//            $presetfields[$field]
//            : ($oldtable.'.`'.$field.'`');
//            $fieldstoimport2[] = '`'.$field.'`'; 
//        }
//    
//        $query = "INSERT INTO {$activetable} (\n\t".join("\t, ",$fieldstoimport2)."\n) "
//        ."SELECT\n\t".join("\t,",$sourcefields)."\n"
//        ."FROM {$oldtable}";
//        if (is_array($importidrange))
//        {
//            $query .= " WHERE {$oldtable}.id >= {$importidrange['first']} "
//            ." AND {$oldtable}.id <= {$importidrange['last']}";
//        }
//    
//        $result = $connect->Execute($query) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
//    }
    
header("Location: $scriptname?action=browse&sid=$surveyid");
}

?>