From 38f908e0a94243599a9690f4d8523ac4f05d53de Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Tue, 26 Feb 2013 04:43:11 +0100 Subject: [PATCH] Calculate and display actual campaign statistics --- public_html/classes/campaign.php | 50 +++++++++++++++----- public_html/classes/logentry.php | 1 + public_html/modules/campaign/dashboard.php | 32 +++++++++++-- public_html/templates/campaign/dashboard.tpl | 16 +++---- 4 files changed, 76 insertions(+), 23 deletions(-) diff --git a/public_html/classes/campaign.php b/public_html/classes/campaign.php index f88f030..c5d2172 100644 --- a/public_html/classes/campaign.php +++ b/public_html/classes/campaign.php @@ -21,26 +21,30 @@ class Campaign extends CPHPDatabaseRecordClass public $prototype = array( 'string' => array( - 'Name' => "Name", - 'UrlName' => "UrlName" + 'Name' => "Name", + 'UrlName' => "UrlName" ), 'numeric' => array( - 'OwnerId' => "OwnerId", - 'DonationRate' => "DonationRate", - 'SubscriberCount' => "SubscriberCount", - 'MonthlyTotal' => "TotalMonthlyDonations", - 'MonthlyProjection' => "ProjectedMonthlyDonations" + 'OwnerId' => "OwnerId", + 'DonationRate' => "DonationRate", + 'SubscriberCount' => "SubscriberCount", + 'MonthlyTotal' => "TotalMonthlyDonations", + 'MonthlyProjection' => "ProjectedMonthlyDonations", + 'PastMonthDonations' => "PastMonthDonations", + 'PastMonthNonDonations' => "PastMonthNonDonations", + 'PastMonthSubscriptions' => "PastMonthSubscriptions", + 'PastMonthUnsubscriptions' => "PastMonthUnsubscriptions" ), 'boolean' => array( - 'AllowOneTime' => "AllowOneTime", - 'HaveData' => "HaveData" + 'AllowOneTime' => "AllowOneTime", + 'HaveData' => "HaveData" ), 'timestamp' => array( - 'LastStatisticsUpdate' => "LastStatisticsUpdate", - 'CreationDate' => "CreationDate" + 'LastStatisticsUpdate' => "LastStatisticsUpdate", + 'CreationDate' => "CreationDate" ), 'user' => array( - 'Owner' => "OwnerId" + 'Owner' => "OwnerId" ) ); @@ -131,6 +135,7 @@ class Campaign extends CPHPDatabaseRecordClass catch (NotFoundException $e) { /* We don't have any data to work from yet. */ + $sDonationsAsked = array(); $have_data = false; } @@ -146,6 +151,7 @@ class Campaign extends CPHPDatabaseRecordClass } catch (NotFoundException $e) { + $sDonationsMade = array(); $this->uDonationRate = 0; $this->uHaveData = false; } @@ -159,6 +165,26 @@ class Campaign extends CPHPDatabaseRecordClass /* Update projected monthly donations */ $this->uMonthlyProjection = $this->uMonthlyTotal * ($this->uDonationRate / 100); + /* Update past-month subscription count */ + if($result = $database->CachedQuery("SELECT COUNT(*) FROM log_entries WHERE `CampaignId` = :CampaignId AND `Type` = :Type AND `Date` > DATE_SUB(NOW(), INTERVAL 1 MONTH)", + array(":CampaignId" => $this->sId, ":Type" => LogEntry::SUBSCRIPTION))) + { + $this->uPastMonthSubscriptions = $result->data[0]["COUNT(*)"]; + } + + /* Update past-month unsubscription count */ + if($result = $database->CachedQuery("SELECT COUNT(*) FROM log_entries WHERE `CampaignId` = :CampaignId AND `Type` = :Type AND `Date` > DATE_SUB(NOW(), INTERVAL 1 MONTH)", + array(":CampaignId" => $this->sId, ":Type" => LogEntry::UNSUBSCRIPTION))) + { + $this->uPastMonthUnsubscriptions = $result->data[0]["COUNT(*)"]; + } + + /* Update past month donation count */ + $this->uPastMonthDonations = count($sDonationsMade); + + /* Update past month non-donation count */ + $this->uPastMonthNonDonations = count($sDonationsAsked) - count($sDonationsMade); + $this->uLastStatisticsUpdate = time(); $this->InsertIntoDatabase(); } diff --git a/public_html/classes/logentry.php b/public_html/classes/logentry.php index 4b4e9ce..b31dc57 100644 --- a/public_html/classes/logentry.php +++ b/public_html/classes/logentry.php @@ -41,4 +41,5 @@ class LogEntry extends CPHPDatabaseRecordClass const SUBSCRIPTION = 2; const DONATION_ASKED = 3; const DONATION_MADE = 4; + const UNSUBSCRIPTION = 5; } diff --git a/public_html/modules/campaign/dashboard.php b/public_html/modules/campaign/dashboard.php index 66c2617..13026b6 100644 --- a/public_html/modules/campaign/dashboard.php +++ b/public_html/modules/campaign/dashboard.php @@ -46,10 +46,36 @@ catch (NotFoundException $e) /* No payment methods...? */ } +$sEventTotal = $sCampaign->sPastMonthSubscriptions + $sCampaign->sPastMonthUnsubscriptions + $sCampaign->sPastMonthDonations + $sCampaign->sPastMonthNonDonations; + +if($sEventTotal !== 0) +{ + $sSubscriptionPercentage = ($sCampaign->sPastMonthSubscriptions / $sEventTotal) * 100; + $sUnsubscriptionPercentage = ($sCampaign->sPastMonthUnsubscriptions / $sEventTotal) * 100; + $sDonationPercentage = ($sCampaign->sPastMonthDonations / $sEventTotal) * 100; + $sNonDonationPercentage = ($sCampaign->sPastMonthNonDonations / $sEventTotal) * 100; +} +else +{ + /* We obviously can't divide by zero - and nothing happened anyway. */ + $sSubscriptionPercentage = 0; + $sUnsubscriptionPercentage = 0; + $sDonationPercentage = 0; + $sNonDonationPercentage = 0; +} + $sPageTitle = "Dashboard for {$sCampaign->sName}"; $sPageContents = NewTemplater::Render("campaign/dashboard", $locale->strings, array( - "name" => $sCampaign->sName, - "urlname" => $sCampaign->sUrlName, - "payment-methods" => $sPaymentMethods + "name" => $sCampaign->sName, + "urlname" => $sCampaign->sUrlName, + "payment-methods" => $sPaymentMethods, + "subscriptions-amount" => $sCampaign->sPastMonthSubscriptions, + "subscriptions-percentage" => $sSubscriptionPercentage, + "unsubscriptions-amount" => $sCampaign->sPastMonthUnsubscriptions, + "unsubscriptions-percentage" => $sUnsubscriptionPercentage, + "donations-amount" => $sCampaign->sPastMonthDonations, + "donations-percentage" => $sDonationPercentage, + "nondonations-amount" => $sCampaign->sPastMonthNonDonations, + "nondonations-percentage" => $sNonDonationPercentage )); diff --git a/public_html/templates/campaign/dashboard.tpl b/public_html/templates/campaign/dashboard.tpl index 1ecad1c..b2eec0e 100644 --- a/public_html/templates/campaign/dashboard.tpl +++ b/public_html/templates/campaign/dashboard.tpl @@ -17,31 +17,31 @@

Past month

-
-
-
-
+
+
+
+
-
23 new subscriptions
+
{%?subscriptions-amount} new subscriptions
-
49 donations
+
{%?donations-amount} donations
-
16 non-donations
+
{%?nondonations-amount} non-donations
-
12 unsubscriptions
+
{%?unsubscriptions-amount} unsubscriptions