public function UpdateStatistics() { global $database, $cphp_config; if (!empty($cphp_config->debugmode) || $this->sLastStatisticsUpdate < time() - 60 * 5) { /* Update subscriber count */ if ($result = $database->CachedQuery("SELECT COUNT(*) FROM subscriptions WHERE `CampaignId` = :CampaignId AND `Confirmed` = 1 AND `Active` = 1", array(":CampaignId" => $this->sId))) { $this->uSubscriberCount = $result->data[0]["COUNT(*)"]; } /* Update total monthly donations */ try { $sSubscriptions = Subscription::CreateFromQuery("SELECT * FROM subscriptions WHERE `CampaignId` = :CampaignId AND `Confirmed` = 1 AND `Active` = 1", array(":CampaignId" => $this->sId)); $sTotalDonations = 0; foreach ($sSubscriptions as $sSubscription) { $sTotalDonations += Currency::Convert("usd", $sSubscription->sCurrency, $sSubscription->sAmount); } $this->uMonthlyTotal = $sTotalDonations; } catch (NotFoundException $e) { $this->uMonthlyTotal = 0; } /* Update donation rate */ try { $sDonationsAsked = LogEntry::CreateFromQuery("SELECT * FROM log_entries WHERE `CampaignId` = :CampaignId AND `Type` = :Type AND `Date` > DATE_SUB(NOW(), INTERVAL 1 MONTH)", array(":CampaignId" => $this->sId, ":Type" => LogEntry::DONATION_ASKED)); $have_data = true; } catch (NotFoundException $e) { /* We don't have any data to work from yet. */ $sDonationsAsked = array(); $have_data = false; } if ($have_data) { try { $sDonationsMade = LogEntry::CreateFromQuery("SELECT * FROM log_entries WHERE `CampaignId` = :CampaignId AND `Type` = :Type AND `Date` > DATE_SUB(NOW(), INTERVAL 1 MONTH)", array(":CampaignId" => $this->sId, ":Type" => LogEntry::DONATION_MADE)); $this->uDonationRate = count($sDonationsMade) / count($sDonationsAsked) * 100; $this->uHaveData = true; } catch (NotFoundException $e) { $sDonationsMade = array(); $this->uDonationRate = 0; $this->uHaveData = false; } } else { $sDonationsMade = array(); $this->uDonationRate = 100; $this->uHaveData = false; } /* 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_CONFIRMED), 0)) { $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), 0)) { $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(); } }