forked from caprenter/IATI-Transparency_Indicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timeliness_Transactions_1.1.php
232 lines (215 loc) · 8.73 KB
/
Timeliness_Transactions_1.1.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
<?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
//Output
echo ",Activities,,,,,,,,,Transactions,,,,,,". PHP_EOL;
//echo "Provider,No. Activities,No. w/Transactions,No. Act. with Trans. in last 30 days,1,2,3,6,12,future,other,1,2,3,6,12,future,other,Sum of array values,Transact Dates,Transacts". PHP_EOL;
echo "Provider,No. Activities,No. w/Transactions,1,2,3,6,12,future,other,1,2,3,6,12,future,other,Sum of array values,Transact Dates,Transacts,Assessment". PHP_EOL;
error_reporting(0);
//$dir = '../raw_IATI_xml_data/'; //contains dirs with each providers data in it - named by group name on registry
include ('settings.php'); //sets $dir
$dirs = scandir($dir); //all the folders in our directory holding the data
unset($dirs[0]); // unset the . value
unset($dirs[1]); //unset the .. value
//$dirs = array("globalgiving");
$months = array();
$now = time();
$transaction_count = 0;
$transaction_date_count = 0;
foreach ($dirs as $corpus) {
$path_to_corpus = $dir . $corpus . "/";
//if ($corpus == "globalgiving") {
if ($handle = opendir($path_to_corpus) ) {
//echo PHP_EOL . "Provider: " . $corpus . PHP_EOL;
$transaction_count = 0;
$transaction_date_count = 0;
$activities = 0;
$activities_with_transactions = 0;
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") { //ignore these system files
//echo $file . PHP_EOL;
//load the xml
if ($xml = simplexml_load_file($path_to_corpus . $file)) {;
//print_r($xml); //debug
foreach ($xml as $activity) {
//CHECK: Reporting Org is included in iati-identifier string
//$reporting_org_ref = (string)$activity->{'reporting-org'}->attributes()->ref;
//echo $reporting_org_ref;
$iati_identifier = (string)$activity->{'iati-identifier'};
$activities++;
$transactions = $activity->transaction;
//if (count($transactions) == 0) {
// echo $id;
//die;
//}
if (isset($transactions) && count($transactions) > 0) { //something not quite right here
$activities_with_transactions++;
//echo "yes";
//Loop through each of the elements
foreach ($transactions as $transaction) {
$transaction_count++; //echo $transaction_count .PHP_EOL;
$transaction_date = (string)$transaction->{'transaction-date'}->attributes()->{'iso-date'};
$time = strtotime($transaction_date);
if ($time) {
$transaction_date_count++;
if (($now - $time) < 0 ) {
$months[$corpus]["future"]++;
$months[$corpus]["id"]["future"][] = $iati_identifier;
continue;
} elseif (($now - $time) < 60*60*24*30 ) {
$months[$corpus][1]++;
$months[$corpus]["id"][1][] = $iati_identifier;
continue;
} elseif ($now - $time < 60*60*24*30*2 ) {
$months[$corpus][2]++;
$months[$corpus]["id"][2][] = $iati_identifier;
continue;
} elseif ($now - $time < 60*60*24*30*3 ) {
$months[$corpus][3]++;
$months[$corpus]["id"][3][] = $iati_identifier;
continue;
} elseif ($now - $time < 60*60*24*30*6 ) {
$months[$corpus][6]++;
$months[$corpus]["id"][6][] = $iati_identifier;
continue;
} elseif ($now - $time < 60*60*24*365) {
$months[$corpus][12]++;
$months[$corpus]["id"][12][] = $iati_identifier;
continue;
} else {
$months[$corpus]["other"]++;
$months[$corpus]["id"]["other"][] = $iati_identifier;
}
unset($transaction_date);
//$month = date("m",$time);
//$months[$month]++;
}
}
}
}
} else { //simpleXML failed to load a file
//array_push($bad_files,$file);
}
}// end if file is not a system file
} //end while
closedir($handle);
//} //if corpus -===
}
$month_numbers = array(1,2,3,6,12,"future","other");
foreach ($month_numbers as $number) {
if (isset($months[$corpus]["id"][$number])) {
$activities_last_x_days = count(array_unique($months[$corpus]["id"][$number]));
$months[$corpus]["id"][$number] = $activities_last_x_days;
unset($activities_last_x_days);
}
//unset($activities_last_30days);
}
format_transaction_results($corpus,$activities,$activities_with_transactions,$transaction_date_count,$transaction_count,$months[$corpus]);
/*
print_r ($months[$corpus]);
echo PHP_EOL;
echo "Activities: " . $activities . PHP_EOL;
echo "Activities with Transactions: " . $activities_with_transactions . PHP_EOL;
echo "Transaction Dates: " . $transaction_date_count . PHP_EOL;
echo "Transactions: " . $transaction_count . PHP_EOL;
echo "Sum of array values: " . array_sum($months[$corpus]) . PHP_EOL;
//echo "Activities within last 30 days: " . count(array_unique($months[$corpus]["id"][1])) . PHP_EOL;
//make the script faster by agregating the id data
//$activities_last_30days = count(array_unique($months[$corpus]["id"][1]));
//$months[$corpus]["id"][1] = $activities_last_30days;
echo "Activities within transaction(s) (1 or more) in last 30 days: " . $months[$corpus]["id"][1] . PHP_EOL;
*/
}
//print_r($months);
function format_transaction_results ($corpus,$activities,$activities_with_transactions,$transaction_date_count,$transaction_count,$month_data) {
$monthly_count = 0;
$month_numbers = array(1,2,3,6,12,"future","other");
echo $corpus; echo ",";
echo $activities; echo ",";
echo $activities_with_transactions; echo ",";
foreach ($month_numbers as $number) {
if (isset($month_data["id"][$number])) {
echo $month_data["id"][$number];
}
echo ",";
}
foreach ($month_numbers as $number) {
if (isset($month_data[$number])) {
echo $month_data[$number];
}
echo ",";
}
echo array_sum($month_data); echo ",";
echo $transaction_date_count; echo ",";
echo $transaction_count; echo ",";
//Assessment
if ( isset($month_data[1]) ) {
$monthly_count++;
}
if ( isset($month_data[2]) ) {
$monthly_count++;
}
if ( isset($month_data[3]) ) {
$monthly_count++;
}
if ($monthly_count > 1) {
$assessment = "Monthly";
}
if ($monthly_count == 1) {
$assessment = "Quarterly";
}
if (!isset($assessment)) {
if (isset($month_data[6])) {
$assessment = "Six-monthly";
}
}
if (!isset($assessment)) {
if (isset($month_data[12])) {
$assessment = "Annually";
}
}
if (!isset($assessment)) {
if (isset($month_data["other"])) {
$assessment = "Beyond one year";
}
}
if (!isset($assessment)) {
$assessment = "Can not calculate";
}
echo $assessment; echo ",";
echo PHP_EOL;
}
?>
<?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{}
* */
?>