Calculate and display actual campaign statistics

master
Sven Slootweg 11 years ago
parent f7182d1de3
commit 38f908e0a9

@ -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();
}

@ -41,4 +41,5 @@ class LogEntry extends CPHPDatabaseRecordClass
const SUBSCRIPTION = 2;
const DONATION_ASKED = 3;
const DONATION_MADE = 4;
const UNSUBSCRIPTION = 5;
}

@ -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
));

@ -17,31 +17,31 @@
<h3>Past month</h3>
<div class="bar-graph">
<div class="area unsubscribed" style="width: 12%;"></div>
<div class="area not-donated" style="width: 16%;"></div>
<div class="area donated" style="width: 49%;"></div>
<div class="area subscribed" style="width: 23%;"></div>
<div class="area unsubscribed" style="width: {%?unsubscriptions-percentage}%;"></div>
<div class="area not-donated" style="width: {%?nondonations-percentage}%;"></div>
<div class="area donated" style="width: {%?donations-percentage}%;"></div>
<div class="area subscribed" style="width: {%?subscriptions-percentage}%;"></div>
</div>
<div class="graph-legend">
<div class="item">
<div class="box subscribed"></div>
<div class="description">23 new subscriptions</div>
<div class="description">{%?subscriptions-amount} new subscriptions</div>
<div class="clear"></div>
</div>
<div class="item">
<div class="box donated"></div>
<div class="description">49 donations</div>
<div class="description">{%?donations-amount} donations</div>
<div class="clear"></div>
</div>
<div class="item">
<div class="box not-donated"></div>
<div class="description">16 non-donations</div>
<div class="description">{%?nondonations-amount} non-donations</div>
<div class="clear"></div>
</div>
<div class="item">
<div class="box unsubscribed"></div>
<div class="description">12 unsubscriptions</div>
<div class="description">{%?unsubscriptions-amount} unsubscriptions</div>
<div class="clear"></div>
</div>
</div>

Loading…
Cancel
Save