<?php

#include("./onslib.php");
$connection = connect("read");
#$stid = $_stid;
/*This file provides functions for reporting statistical aspects of 
the data in the database*/


function alias_stat($name, $stid, $connection){
 
/*this function counts and displays the aliases in use*/

echo "<hr><h3>Surname Spelling Variants</h3>";

$query = "SELECT count(*) FROM ind WHERE stid = '$stid' AND inds_surname = '$name'";
/*echo $query; */
$result = mysql_db_query ($dbname, $query, $connection)
or die ("Invalid query");
$array = mysql_fetch_array($result);
echo "<p>There are ",$array[0]," persons in the database with the surname $name or one of its variants</p>";

$query = "SELECT distinct ind_surname FROM ind WHERE stid = '$stid' AND inds_surname = '$name' ORDER BY ind_surname ";
/*echo $query;*/ 
$result = mysql_db_query ($dbname, $query, $connection)
or die ("Invalid query");
$num = mysql_num_rows($result);

echo "<p>There are $num spelling variants for $name currently in use in this study:</p>";

echo "<center><table border=\"1\" width=\"100%\">
    <tr>
      <td width=\"33%\">Surname Spelling</td>
      <td width=\"33%\">Number of Individuals</td>
      <td width=\"34%\">Number of Trees</td></center></em>
    </tr>";
while ($array = mysql_fetch_array($result)){
		$nam = $array[0];
		$query1 = "SELECT count(*) FROM ind WHERE stid = '$stid' AND ind_surname = '$nam'";
		/*echo $query1;*/ 
		$result1 = mysql_db_query ($dbname, $query1, $connection)
		or die ("Invalid query");
		$numinds = mysql_fetch_array($result1);
		$numind = $numinds[0];
		$query2 = "SELECT count(*) FROM ind WHERE ind_surname = '$nam' AND stid = '$stid' AND fam_id =0";
		/*echo $query2;*/ 
		$result2 = mysql_db_query ($dbname, $query2, $connection)
		or die ("Invalid query");
		$numfams = mysql_fetch_array($result2);
		$numfam = $numfams[0];
		echo "<tr>
      <td width='33%' align='right'>$nam</td>
      <td width='33%'>$numind</td>
      <td width='34%'>$numfam</td></tr>";
		}
echo "</table></center></p><hr>";

return ;
}

function rec_count( $name, $stid, $connection){
 
/*this function reports record counts and maximum id values*/

echo "<h3>Database Table Statistics for this Study</h3>";

echo "<p>The following table gives the numbers of records of some of the tables together with the maximum value used
by this study (other studies share the same table so maximum values might be larger than record counts)</p>";
/*get individual table data*/

$query = "SELECT count(*) FROM ind WHERE stid = \"$stid\"";
/*echo $query; */
$result = mysql_db_query ($dbname, $query, $connection)
or die ("Invalid query");
$array = mysql_fetch_array($result);
$ind_cnt = $array[0];

$query = "SELECT max(ind_id) FROM ind WHERE stid = \"$stid\"";
/*echo $query; */
$result = mysql_db_query ($dbname, $query, $connection)
or die ("Invalid query");
$array = mysql_fetch_array($result);
$ind_max = $array[0];

$query = "SELECT count(*) FROM fam WHERE stid = \"$stid\"";
/*echo $query; */
$result = mysql_db_query ($dbname, $query, $connection)
or die ("Invalid query");
$array = mysql_fetch_array($result);
$fam_cnt = $array[0];

$query = "SELECT max(fam_id) FROM fam WHERE stid = \"$stid\"";
/*echo $query; */
$result = mysql_db_query ($dbname, $query, $connection)
or die ("Invalid query");
$array = mysql_fetch_array($result);
$fam_max = $array[0];

$query = "SELECT count(*) FROM events WHERE stid = \"$stid\"";
/*echo $query; */
$result = mysql_db_query ($dbname, $query, $connection)
or die ("Invalid query");
$array = mysql_fetch_array($result);
$evt_cnt = $array[0];

$query = "SELECT max(event_id) FROM events WHERE stid = \"$stid\"";
/*echo $query; */
$result = mysql_db_query ($dbname, $query, $connection)
or die ("Invalid query");
$array = mysql_fetch_array($result);
$evt_max = $array[0];

$query = "SELECT count(*) FROM contrib WHERE stid = \"$stid\"";
/*echo $query; */
$result = mysql_db_query ($dbname, $query, $connection)
or die ("Invalid query");
$array = mysql_fetch_array($result);
$con_cnt = $array[0];

$query = "SELECT max(cont_id) FROM contrib WHERE stid = \"$stid\"";
/*echo $query; */
$result = mysql_db_query ($dbname, $query, $connection)
or die ("Invalid query");
$array = mysql_fetch_array($result);
$con_max = $array[0];

$query = "SELECT count(*) FROM dat_file WHERE stid = \"$stid\"";
/*echo $query; */
$result = mysql_db_query ($dbname, $query, $connection)
or die ("Invalid query");
$array = mysql_fetch_array($result);
$fil_cnt = $array[0];

$query = "SELECT max(df_id) FROM dat_file WHERE stid = \"$stid\"";
/*echo $query; */
$result = mysql_db_query ($dbname, $query, $connection)
or die ("Invalid query");
$array = mysql_fetch_array($result);
$fil_max = $array[0];

/*output section */
echo "<table border=\"1\" width=\"100%\">
    <tr><em><center>
      <td width=\"33%\" >Table</td>
      <td width=\"33%\">Count</td>
      <td width=\"34%\">Maximum id Value</td></center></em>
    </tr>
    <tr>
      <td width=\"33%\" align=\"right\">Individuals</td>
      <td width=\"33%\">$ind_cnt</td>
      <td width=\"34%\">$ind_max</td>
    </tr>
    <tr>
      <td width=\"33%\" align=\"right\">Families</td>
      <td width=\"33%\">$fam_cnt</td>
      <td width=\"34%\">$fam_max</td>
    </tr>
    <tr>
      <td width=\"33%\" align=\"right\">Events </td>
      <td width=\"33%\">$evt_cnt</td>
      <td width=\"34%\">$evt_max</td>
    </tr>
    <tr>
      <td width=\"33%\" align=\"right\">Contributors</td>
      <td width=\"33%\">$con_cnt</td>
      <td width=\"34%\">$con_max</td>
    </tr>
    <tr>
      <td width=\"33%\" align=\"right\">Files Included</td>
      <td width=\"33%\">$fil_cnt</td>
      <td width=\"34\">$fil_max</td>
    </tr>
  </table>";


return ;
}

function filcontr($name, $stid, $connection){
 
/*This function provides a table of source files and contributers for the build*/

echo "The following table shows the names of the source files and the contributors 
of those files, for the database used in this study.";

echo "<hr><h3>Contributed Files included in this Study</h3>";
echo "<table border=\"1\" width=\"100%\">
    <tr><em><center>
      <td width=\"10%\" >File</td>
      <td width=\"15%\">Contributor</td>
		<td width=\"10%\" >Individuals</td>
		<td width=\"10%\" >Families</td>
		<td width=\"55%\">Description</td></center></em>
    </tr>";
	
	 $nfiles = 0;
	 $famtot = 0;
	 $indtot = 0;

	 $query = "SELECT DISTINCT filename FROM buildlog WHERE stid = \"$stid\" ORDER BY filename";
	 /*echo $query; */
	 $result = mysql_db_query ($dbname, $query, $connection)
	 or die ("Invalid filename query");

	 while ($array = mysql_fetch_array($result)){
	 $file = $array["filename"];
	 $nfiles = $nfiles + 1;

	 $query = "SELECT * FROM dat_file WHERE df_filname = \"$file\" and stid = \"$stid\"";
	 /*echo $query;*/
	 $result1 = mysql_db_query ($dbname, $query, $connection)
	 or die ("Invalid contributor query");
	 $array1= mysql_fetch_array($result1);
	 $desc = $array1["df_desc"];
	 $contid = $array1["df_cont_id"];

	 $query = "SELECT * FROM contrib WHERE cont_id = \"$contid\"";
	/*echo $query;*/
	 $result2 = mysql_db_query ($dbname, $query, $connection)
	  	or die ("Invalid contrib query");
	 $array2 = mysql_fetch_array($result2);
	 $name = $array2["cont_fname"]." ".$array2["cont_sname"];


	 $query = "SELECT count(*) FROM ind WHERE cont_fil = \"$file\" and stid = \"$stid\"";
	 /*echo $query; */
	 $result9 = mysql_db_query ($dbname, $query, $connection)
	 or die ("Invalid query");
	 $incnt = mysql_fetch_array($result9);
	 $indcnt1 = $incnt[0];
	 $indtot = $indtot + $indcnt1;

	 $query = "SELECT count(*) FROM fam WHERE cont_fil = \"$file\" and stid = \"$stid\"";
	 /*echo $query; */
	 $result0 = mysql_db_query ($dbname, $query, $connection)
	 or die ("Invalid query");
	 $famcnt = mysql_fetch_array($result0);
	 $famcnt1 = $famcnt[0];
	 $famtot = $famtot + $famcnt1;
	 echo "<tr><center>

      <td width=\"10%\" >$file</td>
      <td width=\"15%\">$name</td>
		<td width=\"10%\" >$indcnt1</td>
		<td width=\"10%\" >$famcnt1</td>
		<td width=\"55%\">$desc</td></center>
    </tr>";
	}
echo "<tr><center>

      <td width=\"10%\" >totals</td>
      <td width=\"15%\">$nfiles</td>
		<td width=\"10%\" >$indtot</td>
		<td width=\"10%\" >$famtot</td>
		<td width=\"55%\"></td></center>
    </tr>";

echo " </table>";

return ;
}

function onlcontr($stname, $stid, $connection){
$query = "Select distinct cont_id from ind where cont_fil='console' and stid ='$stid'";
#echo "$query<br>";
$result = mysql_db_query ($dbname, $query, $connection)
    or die ("Invalid  contributor query");
echo "<hr><h3>On-line Data Entry Included in this Study</h3>";
echo "<table border = \"1\"><tr>
		<td>Contributor</td>
		<td>Individuals</td>
		<td>Families</td></tr>";
$totind = 0;
$totfam = 0;
while($onldat = mysql_fetch_array($result)){
    $indcount = 0;
	$famcount = 0;
	$contid = $onldat[cont_id];
	$contdat = getcontdat($contid, $connection);
	$where ="where cont_fil='console' and cont_id ='$contid' and stid='$stid'";
    $indcount = getcount('ind', $where, $connection);
    $famcount = getcount('fam', $where, $connection);
    echo "<tr>
		<td>$contdat[cont_fname] $contdat[cont_sname]</td>
		<td>$indcount</td>
		<td>$famcount</td></tr>";
	$totind = $totind + $indcount;
	$totfam = $totfam + $famcount;
    }
echo "<tr>
		<td>Totals</td>
		<td>$totind</td>
		<td>$totfam</td></tr></table>";
}

function auditfile($stid, $connection){

/* this is the function that reads an input gedcom file produces a count of 
the individuals and families included in that file
paramters:
*/

$path = getpath($stid, $connection);

$query = "SELECT * FROM dat_file WHERE stid = \"$stid\" ORDER BY df_filname ";
/*echo $query;*/ 
$result = mysql_db_query ($dbname, $query, $connection)
or die ("Invalid dat_file query");
headz("Input File Data");
echo "<table border = \"1\"><tr>
		<td>File Name </td>
		<td>Individuals</td>
		<td>Child Individuals</td>
		<td>Families</td></tr>";
$totind = 0;
$totfam = 0;
while($array = mysql_fetch_array($result)){

	$indcount = 0;
	$famcount = 0;
	$childcount = 0;
	$dat_file = $array["df_filname"];
	$dat1_file = $path."/".$dat_file;
	/*echo "file to open for audit is ", $dat_file,"<br>";*/
	$fd = fopen($dat1_file, "r");
	/*echo "fd is ",$fd,"<br>";*/
	/*$fam_offset = getmax("fam_id", "fam", $connection);
	$ind_offset = getmax("ind_id", "ind", $connection);*/
	/*echo "offsets", $ind_offset," ", $fam_offset," ", $event_offset;*/
	if ($fd != 0){
	while ($buffer = fgets($fd, 1024)){
		$lin = chop($buffer);
		$lin = ereg_replace("FAMILY\_CHILD", "FAMC", $lin);
		/*echo $lin, "<br>";*/
		/* extract the data */
		if ((substr($lin, 2, 1))=="@"){
			$ndpos = strrpos ($lin, "@");
			if ((substr($lin, ($ndpos + 2), 4))=="INDI"){
				$indcount ++;
				}
			if ((substr($lin, ($ndpos + 2), 3))=="FAM"){
				$famcount ++;
				}
			}
		if ((substr($lin, 2, 4))=="FAMC"){
			$childcount ++;
			}
		}
		fclose($fd);
		$totfam = $totfam + $famcount;
		$totind = $totind + $indcount;
		$totchild = $totchild + $childcount;
		/*echo $indcount," ",$famcount,"<br>";*/
		echo "<tr>
		<td>$dat_file</td>
		<td>$indcount</td>
		<td>$childcount</td>
		<td>$famcount</td></tr>";
		}
		}
	echo "<tr>
		<td>totals</td>
		<td>$totind</td>
		<td>$totchild</td>
		<td>$totfam</td></tr></table>";
}
/*processing starts here*/

#include("./header.php");
/*echo "mode is",$mode,"<br>";*/
$stid = $stdat[stid];
$color = $stdat[st_color];
$name = $stdat[st_name];
 switch ($mode) {
 case "":
	$number = treestart($stdat, $connection);
	alias_stat ($stdat[st_name], $stid, $connection);
	rec_count ($stdat[st_name], $stid, $connection);
	filcontr($stdat[st_name], $stid, $connection);
	onlcontr($stdat[st_name], $stid, $connection);
	break;
case "A":
	auditfile($stid, $connection);
	break;
	}
newcrite('1','',$update);
?>

