fix date input again
fix date input again

--- a/displayCalendar.php
+++ b/displayCalendar.php
@@ -5,9 +5,9 @@
 

 

 if ($_REQUEST['month']) {

-    $monthParts = explode("-", str_replace(Array("'","/","\\",'-'), "",$_REQUEST['month']));

-    $year = $monthParts[1];

-    $month = $monthParts[0];

+    $monthParts = explode("-", str_replace(Array("'","/","\\"), "",$_REQUEST['month']));

+    $year = str_replace('-', '',$monthParts[1]);

+    $month = str_replace('-', '',$monthParts[0]);

     $monthName = date("F Y",  mktime(0,0,0,$month,1,$year));

             

     include_header($monthName);

@@ -81,7 +81,7 @@
         setlocale(LC_MONETARY, 'en_US');

         $value = number_format(doubleval($row["val"]), 2);

         $month_name = date('F', mktime(0, 0, 0, $row[1]));

-        echo ('<tr><td><b><a href="?month=$row[1]-$row[0]">'.$month_name.' '.$row[0].'</a></b></td><td>$'.$value.'</td><td>('                .$row['count'].' contracts)</td></tr>');

+        echo ('<tr><td><b><a href="?month='.$row[1].'-'.$row[0].'">'.$month_name.' '.$row[0].'</a></b></td><td>$'.$value.'</td><td>('                .$row['count'].' contracts)</td></tr>');

     }

     echo "</table>";

 }


--- a/heuristics/heuristics.inc.php
+++ b/heuristics/heuristics.inc.php
@@ -1,43 +1,47 @@
 <?php
+
 include_once ("../lib/common.inc.php");
 $heuristics = Array();
 //each heuristic adds self to description array
 include ("dateHeuristics.php");
 include ("historyHeuristics.php");
+
 //include ("metadataHeuristics.php");
 //include ("valueHeuristics.php");
-function runHeuristic($heuristicName, $cn)
-{
+function runHeuristic($heuristicName, $cn) {
     global $conn;
-	// check  if already ran
-	$query = "select count(*) from heuristic_results where heuristic_name = '$heuristicName' and \"CNID\" = '{$cn['CNID']}'";
-	    $result = $conn->query($query);
-             databaseError($conn->errorInfo());
+    // check  if already ran
+    $query = "select count(*) from heuristic_results where heuristic_name = '$heuristicName' and \"CNID\" = '{$cn['CNID']}'";
+    $result = $conn->query($query);
+    databaseError($conn->errorInfo());
     $r = $result->fetch(PDO::FETCH_BOTH);
-	if ($r[0] == 0) {
-		// if not, run now
-		$hresults = call_user_func($heuristicName, $cn);
-		if (!isset($hresults["heuristic_value"]) || !isset($hresults["raw_value"]) || !isset($hresults["mean"]) || !isset($hresults["stddev"])) {
-			print_r($hresults);
-			die("Missing field in heurtistic $heuristicName result");
-		}
-		$query = "insert into heuristic_results values('$heuristicName',
+    if ($r[0] == 0) {
+        // if not, run now
+        $hresults = call_user_func($heuristicName, $cn);
+        if (!isset($hresults["heuristic_value"]) || !isset($hresults["raw_value"]) || !isset($hresults["mean"]) || !isset($hresults["stddev"])) {
+            print_r($hresults);
+            die("Missing field in heurtistic $heuristicName result");
+        }
+        $query = "insert into heuristic_results values('$heuristicName',
     '{$hresults["heuristic_value"]}',
      '{$hresults["raw_value"]}',
       '{$hresults["mean"]}',
        '{$hresults["stddev"]}',
            '{$cn["CNID"]}',
            NOW(),
-           '{$cn["publishDate"]}',
-           '{$cn["agencyABN"]}',
-           '{$cn["supplierID"]}'
+           '{$cn["publishDate"]}',"
+           //."'{$cn["agencyABN"]}',
+           ."0,'{$cn["supplierID"]}'
     )";
-		// save value and cn data via sql
-		$result = $conn->query($query);
-                $errors = $conn->errorInfo();
-		if ($errors[2] == "") echo "Saved $heuristicName for {$cn["CNID"]} <br>\n";
-		elseif (strpos($errors[2] , "Duplicate entry") === false) echo $hresults . " failed insert.<br>" . print_r($errors,true) . " <br>  $query <br><br>\n";
-	}
+        // save value and cn data via sql
+        $result = $conn->query($query);
+        $errors = $conn->errorInfo();
+        if ($errors[2] == "")
+            echo "Saved $heuristicName for {$cn["CNID"]} <br>\n";
+        elseif (strpos($errors[2], "Duplicate entry") === false)
+            echo $hresults . " failed insert.<br>" . print_r($errors, true) . " <br>  $query <br><br>\n";
+    }
 }
+
 ?>
 

--- a/heuristics/historyHeuristics.php
+++ b/heuristics/historyHeuristics.php
@@ -20,9 +20,9 @@
 $agencyTransactions = Array();
 function getAgencyTransactions($agencyName)
 {
-	global $agencyTransactions;
+	global $agencyTransactions,$conn;
 	if (!$agencyTransactions[$agencyName]) {
-		$query = 'select count(*) from contractnotice where agencyName = "' . $agencyName . '"';
+		$query = 'select count(*) from contractnotice where "agencyName" = \'' . $agencyName . '"\'';
     $result = $conn->query($query);
     $r = $result->fetch(PDO::FETCH_BOTH);
 		$agencyTransactions[$agencyName] = $r[0];
@@ -49,9 +49,9 @@
 }
 function getStatsAgencyTransactions()
 {
-	global $averageAgencyTransactions, $stddevAgencyTransactions;
-	$query = "select avg(count), STDDEV(count) from (select count(*) as count
-                from contractnotice group by agencyName) as a;";
+	global $averageAgencyTransactions, $stddevAgencyTransactions,$conn;
+	$query = 'select avg(count), STDDEV(count) from (select count(*) as count
+                from contractnotice group by "agencyName") as a;';
     $result = $conn->query($query);
     $r = $result->fetch(PDO::FETCH_BOTH);
 	$averageAgencyTransactions = $r[0];
@@ -78,10 +78,10 @@
 $supplierTransactions = Array();
 function getSupplierTransactions($supplierName, $supplierABN)
 {
-	global $supplierTransactions;
+	global $supplierTransactions,$conn;
 	if ($supplierABN != 0 && $supplierABN != "") {
 		if (!$supplierTransactions[$supplierABN]) {
-			$query = 'select count(*) from contractnotice where supplierABN = "' . $supplierABN . '"';
+			$query = "select count(*) from contractnotice where \"supplierABN\" = '" . $supplierABN . "'";
 			    $result = $conn->query($query);
     $r = $result->fetch(PDO::FETCH_BOTH);
 			$supplierTransactions[$supplierABN] = $r[0];
@@ -89,7 +89,8 @@
 		return $supplierTransactions[$supplierABN];
 	}
 	if (!$supplierTransactions[$supplierName]) {
-		$query = 'select count(*) from contractnotice where supplierName = "' . $supplierName . '"';
+		$query = "select count(*) from contractnotice where \"supplierName\" = '" . $supplierName . "'";
+                
     $result = $conn->query($query);
     $r = $result->fetch(PDO::FETCH_BOTH);
 		$supplierTransactions[$supplierName] = $r[0];
@@ -116,8 +117,10 @@
 }
 function getStatsSupplierTransactions()
 {
-	global $averageSupplierTransactions, $stddevSupplierTransactions;
-	$query = 'select avg(count), stddev(count) from (select IF(supplierABN != "",supplierABN,supplierName) as supplierID, count(*) as count from contractnotice group by supplierID) as a;';
+	global $averageSupplierTransactions, $stddevSupplierTransactions,$conn;
+	$query = 'select avg(count), stddev(count) from (
+            select (case when "supplierABN" != 0 THEN "supplierABN"::text ELSE "supplierName" END) as "supplierID",
+            count(*) as count from contractnotice group by "supplierID") as a;';
     $result = $conn->query($query);
     $r = $result->fetch(PDO::FETCH_BOTH);
 	$averageSupplierTransactions = $r[0];

--- a/heuristics/runHeuristics.php
+++ b/heuristics/runHeuristics.php
@@ -5,10 +5,11 @@
 auth();
 include_once("heuristics.inc.php");
 
-$query = 'SELECT *, agency.abn as "agencyABN", case when "supplierABN" != 0 then "supplierABN"::text else "supplierName" end as "supplierID"
-FROM contractnotice JOIN agency ON contractnotice."agencyName"=agency."agencyName"
+// agency table missing JOIN agency ON contractnotice."agencyName"
+$query = 'SELECT *, case when "supplierABN" != 0 then "supplierABN"::text else "supplierName" end as "supplierID"
+FROM contractnotice 
 WHERE  DATE("importDate") = (select * from (SELECT DATE("importDate") 
-FROM contractnotice ORDER BY "importDate" DESC limit 1) alias) limit 10';
+FROM contractnotice ORDER BY "importDate" DESC limit 1) alias) limit 100';
     $query = $conn->prepare($query);
     $query->execute();
     databaseError($conn->errorInfo());

--- a/heuristics/valueHeuristics.php
+++ b/heuristics/valueHeuristics.php
@@ -1,58 +1,61 @@
-<?php 
-      /*  - large contract value
-          - standard dev from mean/median
-          - percent of total contracts for supplier/agency*/
-   $heuristics["VALUE_LARGE_CONTRACT_OVERALL"] = Array(
-	"description" => "unusual value for time of year");
-function METADATA_DUPLICATED_DESCRIPTION($cn)
-{
-    	$averageContractPeriod = getAverageContractPeriod();
-	$diff = strtotime($cn['contractStart']) - strtotime($cn['publishDate']);
-	$days = intval($diff / 24);
-	return ($days > 45 ? 1 : 0);
+<?php
+
+/*  - large contract value
+  - standard dev from mean/median
+  - percent of total contracts for supplier/agency */
+$heuristics["VALUE_LARGE_CONTRACT_OVERALL"] = Array(
+    "description" => "unusual value for time of year");
+
+function VALUE_LARGE_CONTRACT_OVERALL($cn) {
+    $averageContractPeriod = getAverageContractPeriod();
+    $diff = strtotime($cn['contractStart']) - strtotime($cn['publishDate']);
+    $days = intval($diff / 24);
+    return ($days > 45 ? 1 : 0);
 }
 
-      /*  - peculiar value
-        - Just under 80k, amplified if other contracts with same supplier are just under
-      */
-         $heuristics["VALUE_NEAR_THRESHOLD"] = Array(
-	"description" => "unusual value for time of year");
-      /*
-    - unusual variation amount - absolute value; large reductions as well as large increases
-      */
-           $heuristics["VALUE_LARGE_VARIATION"] = Array(
-	"description" => "unusual value for time of year");
-function METADATA_DUPLICATED_DESCRIPTION($cn)
-{
-    	$averageContractPeriod = getAverageContractPeriod();
-	$diff = strtotime($cn['contractStart']) - strtotime($cn['publishDate']);
-	$days = intval($diff / 24);
-	return ($days > 45 ? 1 : 0);
+/*  - peculiar value
+  - Just under 80k, amplified if other contracts with same supplier are just under
+ */
+$heuristics["VALUE_NEAR_THRESHOLD"] = Array(
+    "description" => "unusual value for time of year");
+/*
+  - unusual variation amount - absolute value; large reductions as well as large increases
+ */
+$heuristics["VALUE_LARGE_VARIATION"] = Array(
+    "description" => "unusual value for time of year");
+
+function VALUE_LARGE_VARIATION($cn) {
+    $averageContractPeriod = getAverageContractPeriod();
+    $diff = strtotime($cn['contractStart']) - strtotime($cn['publishDate']);
+    $days = intval($diff / 24);
+    return ($days > 45 ? 1 : 0);
 }
 
 /*   - unusual value for time of year
-        - compare to all other records in last 2 weeks
-        - ie. many large contracts in june so takes more to standout*/
+  - compare to all other records in last 2 weeks
+  - ie. many large contracts in june so takes more to standout */
 $heuristics["VALUE_HIGH_FOR_MONTH"] = Array(
-	"description" => "unusual value for time of year"
+    "description" => "unusual value for time of year"
 );
-function VALUE_HIGH_FOR_MONTH($cn, $monthAsInt)
-{
-	$averageContractPeriod = getAverageContractPeriod();
-	$diff = strtotime($cn['contractStart']) - strtotime($cn['publishDate']);
-	$days = intval($diff / 24);
-	return ($days > 45 ? 1 : 0);
+
+function VALUE_HIGH_FOR_MONTH($cn, $monthAsInt) {
+    $averageContractPeriod = getAverageContractPeriod();
+    $diff = strtotime($cn['contractStart']) - strtotime($cn['publishDate']);
+    $days = intval($diff / 24);
+    return ($days > 45 ? 1 : 0);
 }
+
 $monthlyValueAverage = Array();
-function getAgencyTransactions($agencyName)
-{
-	global $agencyTransactions;
-	if (!$agencyTransactions[$agencyName]) {
-		$query = 'select count(*) from contractnotice where agencyName = "' . $agencyName . '"';
-    $result = $conn->query($query);
-    $r = $result->fetch(PDO::FETCH_BOTH);
-		$agencyTransactions[$agencyName] = $r[0];
-	}
-	return $agencyTransactions[$agencyName];
+
+function getAgencyTransactions($agencyName) {
+    global $agencyTransactions;
+    if (!$agencyTransactions[$agencyName]) {
+        $query = 'select count(*) from contractnotice where agencyName = "' . $agencyName . '"';
+        $result = $conn->query($query);
+        $r = $result->fetch(PDO::FETCH_BOTH);
+        $agencyTransactions[$agencyName] = $r[0];
+    }
+    return $agencyTransactions[$agencyName];
 }
+
 ?>

--- a/heuristics/viewHeuristicsDistribution.php
+++ b/heuristics/viewHeuristicsDistribution.php
@@ -6,7 +6,7 @@
 
 // spread of values
 select floor(sum) as val,count(*) from (SELECT sum(heuristic_value) 
-as sum FROM heuristic_results group by CNID) as a group by val*/
+as sum FROM heuristic_results group by "CNID") as a group by val*/
 
 $series = Array();
 
@@ -17,4 +17,17 @@
     $series[$r["heuristic_name"]][$r["val"]] = $r[2];
 }
 
+ $labels = Array();
+ foreach ($series as $value) {
+    $labels = $labels+array_keys($value);
+ }
+ $labels =  Array(0,1,2,3,4,5);
+foreach ($series as $seriesName => $seriesEntry) {
+    $data;
+ foreach ($labels as $label) {
+    $data[$label] = ($seriesEntry[$label] ? $seriesEntry[$label] : 0);
+ }
+}
+print_r($labels);
+print_r($data);
 ?>

--- a/lib/common.inc.php
+++ b/lib/common.inc.php
@@ -11,7 +11,7 @@
 }

 

 define('ROOT', pathinfo(__FILE__, PATHINFO_DIRNAME));

-if (strstr($_SERVER['PHP_SELF'], "labs/") || strstr($_SERVER['PHP_SELF'], "admin/")) {

+if (strstr($_SERVER['PHP_SELF'], "labs/") || strstr($_SERVER['PHP_SELF'], "admin/") || strstr($_SERVER['PHP_SELF'], "heuristics/")) {

     $basePath = "../";

 }

 require $basePath."lib/amon-php/amon.php";


--- a/sitemap.xml.php
+++ b/sitemap.xml.php
@@ -1,14 +1,68 @@
 <?php
 include ('lib/common.inc.php');
-$last_updated = date('Y-m-d',@filemtime('cbrfeed.zip'));
+$last_updated['page'] = date('Y-m-d',@filemtime('index.php'));
+$sections = Array(
+    "agency",
+    "category",
+    "supplier",
+    "page"
+);
 header("Content-Type: text/xml");
 echo "<?xml version='1.0' encoding='UTF-8'?>";
-  echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
+$result = $conn->query('SELECT title, to_char("lastUpdated",\'YYYY-MM-DD\') as "lastUpdated" from datasets');
+foreach ($result->fetchAll() as $row) {
+    if ($row['title'] == "Contract Notices") {
+        $last_updated['agency'] = $row['lastUpdated'];
+        $last_updated['supplier'] = $row['lastUpdated'];
+        $last_updated['category'] = $row['lastUpdated'];
+    }
+}
+if (isset($_REQUEST['section']) == false) {
+    echo '<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd"
+         xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
+    foreach ($sections as $section) {
+        echo "<sitemap>
+      <loc>" . local_url() . "sitemap.xml.php?section=$section</loc>
+      <lastmod>" . $last_updated[$section] . '</lastmod></sitemap>';
+    }
+    echo '</sitemapindex>';
+} else {
+   echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
+    if ($_REQUEST['section'] == "page") {
       echo " <url><loc>".local_url()."index.php</loc><priority>1.0</priority></url>\n";
 foreach (scandir("./") as $file) {
       if (strpos($file,".php") !== false && $file != "index.php" && $file != "sitemap.xml.php") echo " <url><loc>".local_url()."$file</loc><priority>0.3</priority></url>\n";
 }
+    }
+        if ($_REQUEST['section'] == "agency") {
+       $result = $conn->query('SELECT DISTINCT "agencyName" from contractnotice');
+       foreach ($result->fetchAll() as $row) {
+            echo " <url><loc>" . local_url() . "displayAgency.php?agency=" . urlencode($row['agencyName']) . "</loc>
+      <lastmod>" . $last_updated['agency'] . "</lastmod></url>\n";
+        }
+    }
+    if ($_REQUEST['section'] == "supplier") {
+       $result = $conn->query('SELECT DISTINCT on ("supplierID") (
+ case when "supplierABN" != 0 THEN "supplierABN"::text ELSE "supplierName" END) as "supplierID", "supplierABN", "supplierName"  from contractnotice');
+       
+       foreach ($result->fetchAll() as $row) {
+           
+           $supplier = urlencode(stripslashes($row['supplierABN'] . '-' . $row['supplierName']));
+            echo " <url><loc>" . local_url() . "displaySupplier.php?supplier=$supplier</loc>
+      <lastmod>" . $last_updated['supplier'] . "</lastmod></url>\n";
+        }
+    }
+    if ($_REQUEST['section'] == "category") {
+       $result = $conn->query('SELECT distinct category FROM contractnotice;');
+       foreach ($result->fetchAll() as $row) {
+           
+            echo " <url><loc>" . local_url() . "displayCategory.php?category=".urlencode($row['category'])."</loc>
+      <lastmod>" . $last_updated['category'] . "</lastmod></url>\n";
+           
+        }
+    }
   echo '</urlset>';
-
+}
 ?>