From 5c016fdb138d9d47d6e68309bd3db9fb3be3b838 Mon Sep 17 00:00:00 2001 From: davidc Date: Tue, 29 Nov 2011 16:23:06 +0000 Subject: [PATCH] Budgets - Starts to add budgets to the application. - Simple counts and detection of elements and attributes --- budgets_count.php | 10 ++ budgets_missing_elements.php | 10 ++ processes/budgets_count.php | 118 +++++++++++++++ processes/budgets_missing_elements.php | 197 +++++++++++++++++++++++++ variables/site_vars.php | 9 +- 5 files changed, 343 insertions(+), 1 deletion(-) create mode 100644 budgets_count.php create mode 100644 budgets_missing_elements.php create mode 100644 processes/budgets_count.php create mode 100644 processes/budgets_missing_elements.php diff --git a/budgets_count.php b/budgets_count.php new file mode 100644 index 0000000..c026c4e --- /dev/null +++ b/budgets_count.php @@ -0,0 +1,10 @@ + + + + diff --git a/budgets_missing_elements.php b/budgets_missing_elements.php new file mode 100644 index 0000000..4d0c79f --- /dev/null +++ b/budgets_missing_elements.php @@ -0,0 +1,10 @@ + + + + diff --git a/processes/budgets_count.php b/processes/budgets_count.php new file mode 100644 index 0000000..b066c32 --- /dev/null +++ b/processes/budgets_count.php @@ -0,0 +1,118 @@ +'); + $results = get_count($dir); + //print_r($results); + + if (!$results == NULL) { + if(!empty($results["results"])) { + echo "

Count Results

"; + echo array_sum($results["results"]) . " budgets reported" . "
"; + } + if(!empty($results["codes"])) { + echo "

By type

"; + $budget_types = array_count_values($results["codes"]); + foreach ($budget_types as $type => $count) { + echo $type . ": " . $count . "
"; + } + } + + if (count($results["zeros"]) >0 ) { + echo "

Zero value budgets

"; + echo count($results["zeros"]) . " budgets of 0 value from these files:" . "
"; + print(" + + + + + + + + + + "); + $files = array_unique($results["zeros"]); + $i=0; + foreach ($files as $file) { + $i++; + print(' + + + + + ' + ); + } + print(" +

#

File

Validator

' . $i . '' . $file . 'Validator
"); + } + } else { + echo "

Counts

Unable to get data.

Maybe there are no budgets in this data.

"; + } + + //Print a table of failing files + theme_bad_files($results["bad-files"],$url); + + print("");//main content +} + + + + + + +function get_count ($dir) { + $bad_files = array(); + if ($handle = opendir($dir)) { + /* This is the correct way to loop over the directory. */ + while (false !== ($file = readdir($handle))) { + if ($file != "." && $file != "..") { //ignore these system files + + if ($xml = simplexml_load_file($dir . $file)) { + + if(!xml_child_exists($xml, "//iati-organisation")) {//ignore organisation files + //$count = $xml->count('.//transaction-date'); //php >5.3 + //$count = count($xml->{'iati-activity'}->{'transaction'}); //php < 5.3 + $result = $xml->xpath("//budget"); + //print_r($result); die; + if (count($result)) { + foreach ($result as $value) { + if ($value->value == 0 ) { + $zero_transactions[] = $file; + //echo $file; + } + $codes[] = (string)$value->attributes()->type; + } + $results[$file] = count($result); + } else { + return NULL; + } + } + } else { //simpleXML failed to load a file + array_push($bad_files,$file); + } + + } + } + } + $return = array("results" => $results, + "zeros" =>$zero_transactions, + "codes" => $codes, + "bad-files" => $bad_files); + + return $return; +} + +?> + diff --git a/processes/budgets_missing_elements.php b/processes/budgets_missing_elements.php new file mode 100644 index 0000000..3c11205 --- /dev/null +++ b/processes/budgets_missing_elements.php @@ -0,0 +1,197 @@ +'; + $tick = '1 tick'; + $cross = 'x'; + $tick = '✓'; + + $results = check_budget_elements ($dir); + print('
'); + if($results['start'] || $results['end'] || $results['value'] || $results['value-date'] || $results['fails']) { + print('

Elements

'); + echo count($results['start']) . " budget" . (count($results['start']) == 1 ? '' : 's') . " missing <period-start>"; + echo '
'; + echo count($results['end']). " budget" . (count($results['end']) == 1 ? '' : 's') . " missing <period-end>"; + echo '
'; + echo count($results['value']). " budget" . (count($results['value']) == 1 ? '' : 's') . " missing <value>"; + echo '
'; + echo count($results['value-date']). " budget" . (count($results['value-date']) == 1 ? '' : 's') . " missing @value-date"; + echo '
'; + echo count($results['fails']) . " activit" . (count($results['fails']) == 1 ? 'y' : 'ies') . " experiencing one or more problems"; + } + if (!empty($results['fails'])){ + print('

Table of elements with problems

'); + print(' + + + + + + + + + + + + ' + ); + + foreach ($results['fails'] as $key => $value) { + //print_r($value); die; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + + echo ''; + echo ''; + echo ''; + + } + print(' +

Id

Start

End

Value

@value-date

File

Validator

' . $key . '' . (in_array("start",$value)? $cross:$tick) . '' . (in_array("end",$value)? $cross:$tick) . '' . (in_array("value",$value)? $cross:$tick) . '' . (in_array("value-date",$value)? $cross:$tick) . '' . $url . $value['file'] . 'Validator
'); + } + //die; + if ($results['no-budgets']) { + echo "

Files with no budgets

"; + print(" + + + + + + + + + + "); + $files = array_unique($results['no-budgets']); + $i=0; + foreach ($files as $file) { + $i++; + print(' + + + + + ' + ); + } + print(" +

#

File

Validator

' . $i . '' . $file . 'Validator
"); + } + + print('
'); +} + + +function check_budget_elements ($dir) { + //global $dir; + //$missing= array(); + $files = array(); + //$rows = ''; + $fails = array(); + $fails_value = $fails_value_date = $fails_date = $fails_type = array(); + if ($handle = opendir($dir)) { + /* This is the correct way to loop over the directory. */ + while (false !== ($file = readdir($handle))) { + if ($file != "." && $file != "..") { //ignore these system files + if ($xml = simplexml_load_file($dir . $file)) { + if(!xml_child_exists($xml, "//iati-organisation")) { //ignore org files + if(xml_child_exists($xml, "//budget")) { //ignore org files + foreach ($xml as $activity) { + $id = (string)$activity->{'iati-identifier'}; + foreach ($activity->{'budget'} as $budget) { + if (!xml_child_exists($budget, ".//period-start")) { + array_push ($fails_type, array($id,$file)); + $fails[$id][] = "start"; + $fails[$id]['file'] = $file; + } + if(!xml_child_exists($budget, ".//period-end")) { + array_push ($fails_date, array($id,$file)); + $fails[$id][] = "end"; + $fails[$id]['file'] = $file; + //$fails[$id] = "budget-date"; + } + if(!xml_child_exists($budget, ".//value")) { + array_push ($fails_value, array($id,$file)); + $fails[$id][] = "value"; + $fails[$id]['file'] = $file; + //$fails[$id] = "value"; + } + if (!$budget->xpath(".//value[@value-date]")) { + array_push ($fails_value_date, array($id,$file)); + $fails[$id][] = "value-date"; + $fails[$id]['file'] = $file; + //$fails[$id] = "value-date"; + } + } + } + } else { //no budgets found + $files_with_no_budgets[] = $file; + } + } //end organisation file check + } else { //simpleXML failed to load a file + //echo $file . ' empty'; + } + + }// end if file is not a system file + + } //end while + closedir($handle); + + } + if (!isset($files_with_no_budgets)) { + $files_with_no_budgets = NULL; + } + return array("start" => $fails_type, + "end" => $fails_date, + "value" => $fails_value, + "value-date" => $fails_value_date, + "fails" => $fails, + "no-budgets" => $files_with_no_budgets + ); + +} + + + +function validator_link($url,$file,$id = NULL) { + if ($id !=NULL) { + $link ='http://webapps.kitwallace.me/exist/rest/db/apps/iati/xquery/validate.xq?mode=view&type=activity&id='; + $link .=$id; + $link .= '&source=' . urlencode($url) . urlencode(preg_replace("/ /", "%20", $file)); + } else { + $link ='http://webapps.kitwallace.me/exist/rest/db/apps/iati/xquery/validate.xq?type=activitySet&source='; + $link .= urlencode($url) . urlencode(preg_replace("/ /", "%20", $file)); + $link .= '&mode=download'; + } + return $link; +} + + +?> + + + diff --git a/variables/site_vars.php b/variables/site_vars.php index eb119e9..a080f9e 100644 --- a/variables/site_vars.php +++ b/variables/site_vars.php @@ -50,5 +50,12 @@ "transactions_count" => array("link"=> "transactions_count.php", "title"=>"Count"), "transactions_missing_elements" => array("link"=>"transactions_missing_elements.php","title"=>"Missing Elements"), ); + +$budgets_menu = array(//"transactions" => array("link"=>"transactions.php","title"=>"Overview"), + //"transactions_by_year" => array("link"=>"transactions_by_year.php","title"=>"By Year"), + //"transactions_list" => array("link"=>"transactions_list.php","title"=>"List"), + "budgets_count" => array("link"=> "budgets_count.php", "title"=>"Count"), + "budgets_missing_elements" => array("link"=>"budgets_missing_elements.php","title"=>"Missing Elements"), + ); -$menus = array("overview","validation","activities","elements","codelists","transactions"); +$menus = array("overview","validation","activities","elements","codelists","transactions","budgets");