Newer
Older
Digital_Repository / Repositories / statistics / includes / inc.fns.show_detail_eprint.es.php
<?php
/*
	Usage by dates, countries, totals.
*/
/* New */
	function getCountryEprintType($year,$month,$rnge,$type,$id) 
	{
		$year = (int) $year;
		$month = (int) $month;
		$records = array();

		// Generate type enquiry
		if ($type == 'download') {
			$limit2 = ' AND view_type = "download" ';
		} elseif ($type == 'abstract') {
			$limit2 = ' AND view_type = "abstract" ';
		} else {
			$limit2 = '';
		}

		// generate date enquiry
		$date_range = '';
		if($year>=2002 and $year<2100) {
			if($month>0 and $month<=12) {
				$date_range = " AND year(request_date) = $year AND month(request_date) = $month";
			} else {
				$date_range = " AND year(request_date) = $year";
			}
		} 
		if($rnge=='4w') {
			$date_range = " AND request_date >= date_sub(curdate(),INTERVAL 1 MONTH)";
		}
		
		$query = 
			'select count(*) as count,country_name,country_code from view WHERE archiveid = ' . 
			$id .
			$limit2 .
			$date_range .
			' GROUP BY country_name ORDER BY count DESC';
		$result = mysql_query($query);
		if (!$result) {
			sqlconn::_setError(mysql_errno(),mysql_error());
			return mysql_error();
		} else {
			while ($row = mysql_fetch_assoc($result)) {
				$records[] = $row;
			}
			return $records;
		}
		
	}
	
	/*
		Grab the title of an eprint from the repository. Only needed if
		the title isn't already in the stats database, which should only
		occur when an eprint hasn't been downloaded yet.
	*/
	function getEPrintTitle($eprint_id)
	{
		$eprint_title = '';
		$conn_eprints = mysql_connect(
			$GLOBALS["config_vars"]["connections"]["sqlserver_eprints"],
			$GLOBALS["config_vars"]["connections"]["sqluser_eprints"],
			$GLOBALS["config_vars"]["connections"]["sqlpass_eprints"]
		);
		$db_eprints = mysql_select_db(
			$GLOBALS["config_vars"]["connections"]["sqldatabase_eprints"],
			$conn_eprints
		);
		if (!$db_eprints) return $eprint_title;
		$query_eprints = "select title from archive where eprintid = $eprint_id";
		$result_eprints = mysql_query($query_eprints,$conn_eprints);
		$row_eprints = mysql_fetch_assoc($result_eprints);
		$eprint_title = trim($row_eprints["title"]);
		$eprint_title = preg_replace("/\s+/"," ",$eprint_title);
		mysql_close($conn_eprints);
		return $eprint_title;
	}


	// If no id default to front page.
	if(!isset($_REQUEST["id"])) { return "pub_default"; }

	// Get detail given year/month
	$show_date = 'all years';
	if(isset($_REQUEST["year"]) and $_REQUEST["year"]>2002) {
		if(isset($_REQUEST["month"]) and $_REQUEST["month"]>=1 and $_REQUEST["month"]<=12) { 
			$dt = strtotime($_REQUEST["month"]."/1/".$_REQUEST["year"]);
			$show_date = date("F Y",$dt);
		} else {
			$show_date = $_REQUEST["year"];	
			$_REQUEST["month"] = 0;
		}
	} else {
		$_REQUEST["year"] = 0;
		$_REQUEST["month"] = 0;
	}
	if(isset($_REQUEST["range"])) {
		// Work out current date
		if($_REQUEST["range"]=='4w') {
			$show_date = 'past 4 weeks';
		}
	} else {
		$_REQUEST["range"] = '';
	}
	// by country
	$country_abstracts = getCountryEprintType($_REQUEST["year"], $_REQUEST["month"], $_REQUEST["range"], 'abstract', $_REQUEST["id"]);
	$country_downloads = getCountryEprintType($_REQUEST["year"], $_REQUEST["month"], $_REQUEST["range"], 'download', $_REQUEST["id"]);
	
	// NJS 2006-01-18: Calculate total number of countries.
	$total_download_countries = count($country_downloads);
	$total_abstract_countries = count($country_abstracts);
	
	$GLOBALS["db_values"] = merge_countries($country_abstracts, $country_downloads);
	// title
	$title = $sql->getTitle($_REQUEST["id"]);
	// by type
	$type_count = $sql->getAbstractDownload($_REQUEST["year"], $_REQUEST["month"], $_REQUEST["range"], 'download', $_REQUEST["id"]);
	// by date
	$month_downloads = $sql->getCumulativeUsageType($_REQUEST["id"],'download');
	$month_abstracts = $sql->getCumulativeUsageType($_REQUEST["id"],'abstract');
	$month_tally = merge_dates($month_abstracts,$month_downloads);
	
?>