/processes/activity_status_count.php

https://github.com/caprenter/IATI-Data-Spotter · PHP · 162 lines · 121 code · 23 blank · 18 comment · 21 complexity · abc9911363f5934debbea99bf547929b MD5 · raw file

  1. <?php
  2. if (in_array($myinputs['group'],array_keys($available_groups))) {
  3. //Include variables for each group. Use group name for the argument
  4. //e.g. php detect_html.php dfid
  5. require_once 'variables/' . $_GET['group'] . '.php';
  6. require_once 'functions/xml_child_exists.php';
  7. require_once 'functions/validator_link.php';
  8. require_once 'functions/bad_files_table.php';
  9. $activity_status_codes = array( "1" => "Pipeline/identification",
  10. "2" => "Implementation",
  11. "3" => "Completion",
  12. "4" => "Post-completion",
  13. "5" => "Cancelled"
  14. );
  15. //echo $activity_status_codes[2];die;
  16. print('<div id="main-content">');
  17. $results = get_count($dir);
  18. //print_r($results);
  19. if (!$results == NULL) {
  20. if(!empty($results["results"])) {
  21. echo "<h4>Count Results</h4>";
  22. echo array_sum($results["results"]) . " activity status elements reported" . "<br/>";
  23. }
  24. if(!empty($results["codes"])) {
  25. echo "<h4>By type</h4>";
  26. sort($results["codes"]);
  27. $transaction_types = array_count_values($results["codes"]);
  28. foreach ($transaction_types as $type => $count) {
  29. echo $type . " (" . $activity_status_codes[$type] . "): " . $count . "<br/>";
  30. }
  31. }
  32. if(!empty($results["bad-codes"])) {
  33. echo "<h4>" . count($results["bad-codes"]) . " mismatched codes</h4>";
  34. print("
  35. <table id='table' class='sortable'>
  36. <thead>
  37. <tr>
  38. <th><h3>Code</h3></th>
  39. <th><h3>Found</h3></th>
  40. <th><h3>Codelist Value</h3></th>
  41. </tr>
  42. </thead>
  43. <tbody>
  44. ");
  45. foreach ($results["bad-codes"] as $bad) {
  46. print('
  47. <tr>
  48. <td>' . $bad[0] . '</td>
  49. <td>' . $bad[1] . '</td>
  50. <td>' . $bad[2] . '</td>
  51. </tr>'
  52. );
  53. }
  54. print("</tbody>
  55. </table>");
  56. //print_r($results["bad-codes"]);
  57. }
  58. if (count($results["zeros"]) >0 ) {
  59. echo "<h4>Zero value transactions</h4>";
  60. echo count($results["zeros"]) . " transactions of 0 value from these files:" . "<br/>";
  61. print("
  62. <table id='table' class='sortable'>
  63. <thead>
  64. <tr>
  65. <th><h3>#</h3></th>
  66. <th><h3>File</h3></th>
  67. <th><h3>Validator</h3></th>
  68. </tr>
  69. </thead>
  70. <tbody>
  71. ");
  72. $files = array_unique($results["zeros"]);
  73. $i=0;
  74. foreach ($files as $file) {
  75. $i++;
  76. print('
  77. <tr>
  78. <td>' . $i . '</td>
  79. <td><a href="' .$url . rawurlencode($file) . '">' . $file . '</a></td>
  80. <td><a href="' . validator_link($url,$file) . '">Validator</a></td>
  81. </tr>'
  82. );
  83. }
  84. print("</tbody>
  85. </table>");
  86. }
  87. } else {
  88. echo "<h4>Counts</h4><p class=\"cross\">Unable to get data</p>";
  89. }
  90. //Print a table of failing files
  91. theme_bad_files($results["bad-files"],$url);
  92. print("</div>");//main content
  93. }
  94. function get_count ($dir) {
  95. global $activity_status_codes;
  96. $bad_files = array();
  97. if ($handle = opendir($dir)) {
  98. /* This is the correct way to loop over the directory. */
  99. while (false !== ($file = readdir($handle))) {
  100. if ($file != "." && $file != "..") { //ignore these system files
  101. if ($xml = simplexml_load_file($dir . $file)) {
  102. if(!xml_child_exists($xml, "//iati-organisation")) {//ignore organisation files
  103. //$count = $xml->count('.//transaction-date'); //php >5.3
  104. //$count = count($xml->{'iati-activity'}->{'transaction'}); //php < 5.3
  105. $result = $xml->xpath("//activity-status");
  106. //echo count($result); die;
  107. //print_r($result); die;
  108. if (count($result)) {
  109. foreach ($result as $status) {
  110. /*if ($value->value == 0 ) {
  111. $zero_transactions[] = $file;
  112. //echo $file;
  113. }*/
  114. $this_code = (string)$status->attributes()->code;
  115. //echo (string)$status; echo $activity_status_codes[$this_code]; die;
  116. //Check the text given matches the code supplied
  117. if ((string)$status != $activity_status_codes[$this_code]) {
  118. //array_push($bad_files,$file);
  119. $bad_codes[] = array($this_code, (string)$status, $activity_status_codes[$this_code]);
  120. } else {
  121. $codes[] = (string)$status->attributes()->code;
  122. }
  123. }
  124. $results[$file] = count($result);
  125. }
  126. }
  127. } else { //simpleXML failed to load a file
  128. array_push($bad_files,$file);
  129. }
  130. }
  131. }
  132. }
  133. $return = array("results" => $results,
  134. "codes" => $codes,
  135. "bad-codes" => $bad_codes,
  136. "bad-files" => $bad_files);
  137. return $return;
  138. }
  139. ?>