forked from caprenter/IATI-Transparency_Indicator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinal_Results_Sheet.php
More file actions
189 lines (174 loc) · 6.17 KB
/
Copy pathFinal_Results_Sheet.php
File metadata and controls
189 lines (174 loc) · 6.17 KB
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
<?php
//Thanks: http://www.developerfusion.com/code/2058/determine-execution-time-in-php/
//This helps us monitor how long the script takes to run.
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
?>
<?php
/* Take lots of CSV files and create two master final sheets
*
*
*/
//Get a mapping of publishers and their real names
if (($handle = fopen("helpers/publisher_mapping.csv", "r")) !== FALSE) {
while (($pub_data = fgetcsv($handle, 1000, ";")) !== FALSE) {
if (isset($pub_data[3]) && $pub_data[3] !=NULL) {
$use_this_name = $pub_data[3];
} else {
$use_this_name = trim($pub_data[1],",");
}
$publishers[$pub_data[0]] = array("name"=>$use_this_name,"group"=>$pub_data[2]);
$groups[$pub_data[0]] = $pub_data[2];
}
fclose($handle);
}
//$new_file = array();
//Do Timeliness_Transactions_1.1.php
$dir_to_csvs = "annual_report/";
$dir_to_csvs = "";
$master_files = array("Other.csv","Signatory.csv");
$files = array( $dir_to_csvs . "Timeliness_Transactions_1.1.csv",
$dir_to_csvs . "Timeliness_Files_1.2.csv",
$dir_to_csvs . "Activity_planning_3.csv",
$dir_to_csvs . "Alignement_with_financial_year_Transactions_4.1.csv",
$dir_to_csvs . "Alignement_with_financial_year_Budgets_4.2.csv"
);
foreach ($master_files as $master_file) {
foreach ($files as $file) {
switch ($file) {
case $dir_to_csvs . "Timeliness_Transactions_1.1.csv":
$column = "Assessment";
$row = 4; //human readable row number
break;
case $dir_to_csvs . "Timeliness_Files_1.2.csv":
$column = "Assessment";
$row = 5; //human readable row number
break;
case $dir_to_csvs . "Activity_planning_3.csv":
$column = "%";
$row = 6; //human readable row number
break;
case $dir_to_csvs . "Alignement_with_financial_year_Transactions_4.1.csv":
$column = "Assessment";
$row = 7; //human readable row number
break;
case $dir_to_csvs . "Alignement_with_financial_year_Budgets_4.2.csv":
$column = "Assessment";
$row = 8; //human readable row number
break;
default:
break;
}
add_data_to_master($file,$master_file,$row,$column);
unset($row);
unset($column);
}
}
function add_data_to_master($file,$master_file,$row,$column) {
//global $new_file;
$new_file = array();
global $publishers;
global $dir_to_csvs;
//Open the Master file
if (($handle2 = fopen("csv/" . $master_file, "r")) !== FALSE) {
//Grab and store the first line
$row1 = fgetcsv($handle2, 0, ',','"'); // read and store the first line
//print_r($row1);die;
$new_file[] = $row1; //shove it in the array we will use to print results
//Get to the row we want to add values in
$i = 1;
while (($data2 = fgetcsv($handle2, 2000, ",")) !== FALSE) {
$i++;
if ($i == $row) {
//We're at our row
//For each provider column we need to add the correct data..
//from the file containing the results
if (($handle = fopen("csv/" . $file, "r")) !== FALSE) {
//echo $file; die;
$results_row1 = fgetcsv($handle, 0, ',','"'); // read and store the first line
//Timeliness_Transactions_1.1.csv - has column headings on row2
if ($file == $dir_to_csvs . "Timeliness_Transactions_1.1.csv" ) {
$results_row1 = fgetcsv($handle, 0, ',','"'); // read and store the first line
}
//print_r($results_row1);die;
while (($data = fgetcsv($handle, 2000, ",")) !== FALSE) {
$provider = $data[0]; //get the provider id
//echo $provider; die;
if (isset($publishers[$provider]["name"])) {
$provider = $publishers[$provider]["name"];
}
if ($provider == NULL || strstr($provider,"This page")) {
continue;
}
//echo $provider; die;
//Is this provider in the master file?
foreach ($row1 as $key=>$column_name) { //$row1 contains provider names as column headers
if ($column_name == $provider) { //Yes we have the provider, now add data to this column
$column_number = $key; //so now we know which column to store the data in
//buut what data?
//grab it from
//echo $column_number; die;
foreach ($results_row1 as $key2=>$value) { //e.g. $row1[0] = projectid
if (trim($value) == $column) { //we've found the column we need
//Store it in the file
/*if (!isset ($data[$key2])) {
echo $key2 . PHP_EOL;
echo $value . PHP_EOL;
echo $column . PHP_EOL;
echo $file;
die;
}*/
$store = $data[$key2];
//echo $store; die;
//echo $column_number; die;
$data2[$column_number] = $store;
}
}
}
}
//echo $provider; die;
//Get the data that needs to go in the master sheet
} //end while $data
} //end if handle //$data2[$column_number] = $store;
} //end if $ ==$row
$new_file[] = $data2;
} //end while $data2
fclose($handle);
fclose($handle2);
}
$fp = fopen("csv/" . $master_file, 'w');
foreach ($new_file as $fields) {
fputcsv($fp, $fields,",");
}
fclose($fp);
//die;
}
?>
<?php
//Thanks: http://www.developerfusion.com/code/2058/determine-execution-time-in-php/
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo PHP_EOL . "This page was created in ".$totaltime." seconds" . PHP_EOL;
/* JSON
* {
* results {
* total => x
*
* }
* good {
* name {
files {
* a
* b
* c
}
}
}
* bad{}
* */
?>