Newer
Older
Digital_Repository / Repositories / statistics / includes / inc.fns.merge.es.php
  1. <?php
  2. /* -----------------------------------------------------------------------------------
  3. This module contains two merge functions, one for date and one for country arrays
  4. plus ancillary functions.
  5. (c) Copyright 2004 Arthur Sale, University of Tasmania
  6. ----------------------------------------------------------------------------------- */
  7.  
  8. /* ------------------------------------------------------------------------------
  9. This function computes an integer datestamp to a month resolution.
  10. (c) Copyright 2004 Arthur Sale, University of Tasmania
  11. Parameters:
  12. An array and an index into it
  13. Returns:
  14. Result: the datestamp
  15. ------------------------------------------------------------------------------ */
  16. function datestamp(&$x, $j)
  17. // reference parameter for efficiency, not changed
  18. // invalid dates return zero, ie earlier than any possible date.
  19. {
  20. if ($j < count($x)) {
  21. // $mo is in {0..11}
  22. $mo = (int) strpos('JanFebMarAprMayJunJulAugSepOctNovDec', $x[$j]["month"])/3;
  23. return (int) $x[$j]["year"]*12 + $mo;
  24. } else {
  25. return 0;
  26. }
  27. }
  28.  
  29. /* ------------------------------------------------------------------------------
  30. This function merges two date arrays, to produce a single consolidated array.
  31. Note that either of the two arrays might be missing a view for a month or more.
  32. Precondition: both arrays are ordered in descending order of date (latest first)
  33. as is the result array.
  34. (c) Copyright 2004 Arthur Sale, University of Tasmania
  35. Parameters:
  36. Two date-ordered arrays: abstracts and downloads
  37. Returns:
  38. Result: merged array also ordered by date
  39. NJS 2006-01-18: Added count of distinct countries.
  40. ------------------------------------------------------------------------------ */
  41. function merge_dates(&$aa, &$ad)
  42. // reference parameters for efficiency, not changed
  43. {
  44. $merged = array();
  45. // start of all arrays
  46. $i = 0;
  47. $indexa = 0;
  48. $indexd = 0;
  49. // set up the rest of the loop invariant
  50. $datea = datestamp($aa,0);
  51. $dated = datestamp($ad,0);
  52. // $datei is initialized to the most recent date for which an access is recorded
  53. $datei = max($datea, $dated);
  54. while (($datea + $dated) != 0) {
  55. // Loop invariant: All elements prior to $indexa in $aa and $indexd in $ad
  56. // have been transferred to $merged (ie all dates *after* $datei)
  57. // Progress: $date1 is decremented by 1 (ie one month *earlier*)
  58. // Termination: No more elements, signalled by both datestamps == 0.
  59. if (($datea == $datei) and ($dated == $datei)) {
  60. // Both are valid and equal to the current date being displayed, the majority case
  61. $merged[$i] = array(
  62. "downloads" => $ad[$indexd]["count"],
  63. "dcountries" => $ad[$indexd]["countries"],
  64. "abstracts" => $aa[$indexa]["count"],
  65. "acountries" => $aa[$indexa]["countries"],
  66. "month" => $ad[$indexd]["month"], // same value as aa
  67. "year" => $ad[$indexd]["year"] // same value as aa
  68. );
  69. $indexa++; $indexd++;
  70. $datea = datestamp($aa,$indexa);
  71. $dated = datestamp($ad,$indexd);
  72. } elseif ($datea == $datei) {
  73. // aa (but not ad) is equal to the current date being displayed
  74. $merged[$i] = array(
  75. "downloads" => 0,
  76. "dcountries" => 0,
  77. "abstracts" => $aa[$indexa]["count"],
  78. "acountries" => $aa[$indexa]["countries"],
  79. "month" => $aa[$indexa]["month"],
  80. "year" => $aa[$indexa]["year"]
  81. );
  82. $indexa++;
  83. $datea = datestamp($aa,$indexa);
  84. } elseif ($dated == $datei) {
  85. // ad (but not aa) is equal to the current date being displayed
  86. $merged[$i] = array(
  87. "downloads" => $ad[$indexd]["count"],
  88. "dcountries" => $ad[$indexd]["countries"],
  89. "abstracts" => 0,
  90. "acountries" => 0,
  91. "month" => $ad[$indexd]["month"],
  92. "year" => $ad[$indexd]["year"]
  93. );
  94. $indexd++;
  95. $dated = datestamp($ad,$indexd);
  96. } else {
  97. // Neither aa nor ad are equal to the current date being displayed
  98. // So generate an empty record
  99. $monthname = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  100. $merged[$i] = array(
  101. "downloads" => 0,
  102. "dcountries" => 0,
  103. "abstracts" => 0,
  104. "acountries" => 0,
  105. "month" => $monthname[$datei % 12],
  106. // NJS 2006-06-14 BUG FIX: added () around the expression
  107. // to ensure that the (int) is applied to the whole thing.
  108. // We were getting fractional years in the output :(
  109. "year" => (int) ( $datei / 12 )
  110. );
  111. }
  112. // Progress: one more entry in $merged, and date earlier by one month
  113. $i++;
  114. $datei--;
  115. }
  116. return $merged;
  117. }
  118.  
  119. /* ------------------------------------------------------------------------------
  120. This function merges two country arrays, to produce a single consolidated array.
  121. Note that either of the two arrays might be missing a country in the other.
  122. (c) Copyright 2004 Arthur Sale, University of Tasmania
  123. Precondition: both arrays are ordered in descending order of views.
  124. Postcondition: result array contains records for every country in $aa and $ad,
  125. and is ordered in descending order of download views.
  126. ------------------------------------------------------------------------------ */
  127. function merge_countries($aa, &$ad)
  128. // reference parameter $ad for efficiency, not changed,
  129. // however the $aa value parameter is altered and is not prapogated back
  130. {
  131. $merged = array();
  132. // Copy acrosss the download array, adding counts from the abstract array as needed.
  133. for ($i=0; $i<count($ad); $i++) {
  134. $merged[$i] = array(
  135. "country_code" => $ad[$i]["country_code"],
  136. "country_name" => $ad[$i]["country_name"],
  137. "country_downloads" => $ad[$i]["count"],
  138. "country_abstracts" => 0
  139. );
  140. $c_code = $merged[$i]["country_code"];
  141. for ($j=0; $j<count($aa); $j++) {
  142. if ($c_code == $aa[$j]["country_code"]) {
  143. // matching country in abstracts
  144. $merged[$i]["country_abstracts"] = $aa[$j]["count"];
  145. // render this entry dead in future with reserved country code
  146. $aa[$j]["country_code"] = '==';
  147. // and get out of the loop
  148. break;
  149. }
  150. } // for on $j
  151. } // for on $i
  152. // Copy what is left of the abstract array
  153. $i = count($merged);
  154. for ($j=0; $j<count($aa); $j++) {
  155. if ($aa[$j]["country_code"] != '==') {
  156. // country with only abstract views, so copy
  157. $merged[$i] = array(
  158. "country_code" => $aa[$j]["country_code"],
  159. "country_name" => $aa[$j]["country_name"],
  160. "country_downloads" => 0,
  161. "country_abstracts" => $aa[$j]["count"]
  162. );
  163. // and increment $i
  164. $i++;
  165. }
  166. } // for on $j
  167. return $merged;
  168. }
  169. ?>