<?php

session_start();

require 'configuration.php';

require_once 'includes/common.functions.php';

require_once ('classes/database.php');

require_once ('classes/functions.php');

$sqlObj = new mysqlClass;

$funcObj = new funcClass;

$table = "##campaigns";

$curFile = basename(__FILE__);



$id 	= intval($_GET['id']); // ID of active campaign



$query = "SELECT COUNT(ID) AS COUNT, ACTION, DATE_FORMAT(DATE_TIME, '%Y-%m-%d') AS DATE_TIME, DATE_FORMAT(DATE_TIME, '%e %b') AS DATE 

FROM smlr_report WHERE CAMPAIGN_ID = '{$id}' GROUP BY ACTION, DATE_TIME ORDER BY DATE_TIME ASC";



$activityReportResultSet	= array();

$activityReportResultSet 	= $funcObj->CustomQuery($query);



//print_r($activityReportResultSet);



$activityReportReportData = "";



$views = 0;

$unsubs = 0;

$tracks = 0;

$date = "";

$iCount = 1;

$popRows = 1;



foreach ($activityReportResultSet as $row)

{

	

	$query 				= "SELECT COUNT(ID) AS COUNT FROM ##report WHERE CAMPAIGN_ID = '{$id}' AND ACTION='view' AND DATE(DATE_TIME)='$row->DATE_TIME'";

	$totalViews			= $funcObj->CustomQueryCount($query);

	

	$query 				= "SELECT COUNT(ID) AS COUNT FROM ##report WHERE CAMPAIGN_ID = '{$id}' AND ACTION='track' AND DATE(DATE_TIME)='$row->DATE_TIME'";

	$totalClicks		= $funcObj->CustomQueryCount($query);

	

	$query 				= "SELECT COUNT(DISTINCT(EMAIL)) AS COUNT FROM ##report WHERE CAMPAIGN_ID = '{$id}' AND ACTION='unsubscribe' AND DATE(DATE_TIME)='$row->DATE_TIME'";

	$totalUnsubs		= $funcObj->CustomQueryCount($query);

	

	if ($date == "")

	{

		$date = $row->DATE;

	}

	

	// if the date is same

	if ($date == $row->DATE)

	{

		if ($row->ACTION == 'track')

			$tracks = $totalClicks;

		else if ($row->ACTION == 'unsubscribe')		

			$unsubs = $totalUnsubs;

		else if ($row->ACTION == 'view')			

			$views = $totalViews;

		$date = $row->DATE;	

	}

	else

	{

		$activityReportReportData .= $date . ';' . $views . ';' . $tracks . ';' . $unsubs . "\n";

		

		if ($popRows == 15)

			break;

			 

		$popRows++;

		

		$views = 0;

		$unsubs = 0;

		$tracks = 0;

		$date = "";



		// assign a new date

		$date = $row->DATE;

		

		if ($row->ACTION == 'track')

			$tracks = $row->COUNT;

		else if ($row->ACTION == 'unsubscribe')		

			$unsubs = $row->COUNT;

		else if ($row->ACTION == 'view')			

			$views = $row->COUNT;

		

		

	}

	if ($iCount == count($activityReportResultSet))

		$activityReportReportData .= $date . ';' . $views . ';' . $tracks . ';' . $unsubs . "\n";

		

	$iCount++;

}

echo $activityReportReportData;

?>