From 0c1c0193ce72cee4a7f486a00af1fab46d583bc0 Mon Sep 17 00:00:00 2001 From: Chris Bean Date: Wed, 29 Oct 2014 19:46:15 +0000 Subject: [PATCH 1/2] Friendly Green Button for admins to "Renew Membership" of users whose accounts are "Expiring" or "Expired". --- controllers/AdminController.php | 6 ++++++ model/User.php | 22 ++++++++++++++++++++++ views/admin/editUser.html | 10 +++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/controllers/AdminController.php b/controllers/AdminController.php index ba67358..febbd07 100755 --- a/controllers/AdminController.php +++ b/controllers/AdminController.php @@ -98,6 +98,12 @@ static public function handleEditUser($app, $twig, $username) { $temppassword = $user->resetPassword(); $successmessage = "Password for $username reset to '$temppassword'."; } + + if (isset($_POST['renewmembership'])) { + $user->renewMembership(); + $nextExpiry = date("Y-m-d", strtotime($user->expiry)); + $successmessage = "Membership renewed until $nextExpiry."; + } if (isset($_POST['update'])) { $user->displayname = $_POST["displayname"]; diff --git a/model/User.php b/model/User.php index 4e9ba2c..7b2a2d0 100755 --- a/model/User.php +++ b/model/User.php @@ -212,10 +212,32 @@ public function save() { } } + public function renewMembership() { + $this->expiry = $this->computeExpiry(); + return $this->save(); + } + public function isAdmin() { return $this->isAdmin; } + // Maybe this could be done in the API + public function computeExpiry() { + $now = time(); + $nextYear = date("Y") + 1; + + $nextExpiryString = 'first Friday of October'; + $thresholdString = 'last Friday of May'; + + $nextExpiry = strtotime($nextExpiryString, $now); + $threshold = strtotime($thresholdString, $now); + + if ($now < $threshold) { + return $nextExpiryString; + } else { + return "$nextExpiryString $nextYear"; + } + } } diff --git a/views/admin/editUser.html b/views/admin/editUser.html index 2ef5667..33d4e64 100755 --- a/views/admin/editUser.html +++ b/views/admin/editUser.html @@ -72,6 +72,14 @@ {{ user.status }} + {{ user.status == 'Expired' or user.status == 'Expiring' ? " +
+ +
+ +
+
+ ": null }}
@@ -124,4 +132,4 @@

Are you sure?

-{% endblock %} \ No newline at end of file +{% endblock %} From 97e4cc54c3bcdc78bbaf5168ba2783b7faf0ed13 Mon Sep 17 00:00:00 2001 From: Chris Bean Date: Wed, 29 Oct 2014 23:36:33 +0000 Subject: [PATCH 2/2] computeExpiry more robust. New users have correct expiry date. --- model/User.php | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/model/User.php b/model/User.php index 7b2a2d0..8f1ab4f 100755 --- a/model/User.php +++ b/model/User.php @@ -177,7 +177,7 @@ public function save() { global $conf; if (self::get($this->username) == null) { - //create new user (POST) + //create new user (POST) $details = get_object_vars($this); $user = $_SESSION['username']; @@ -190,6 +190,17 @@ public function save() { if ($response->headers['Status'] != "200 OK") { return false; } else { + + /* There is a bug in the API's use of computeExpiry. The + * subroutine itself works and is passing unit tests but + * somewhere in the user creation process the expiry date is + * getting decremented by one day. So call renewMembership + * here to compensate for that until the bug is fixed. + */ + // TODO: Fix the API's createUser computeExpiry bug + + $this->renewMembership(); + return true; } } else { @@ -214,6 +225,7 @@ public function save() { public function renewMembership() { $this->expiry = $this->computeExpiry(); + $this->paid = "TRUE"; return $this->save(); } @@ -224,19 +236,22 @@ public function isAdmin() { // Maybe this could be done in the API public function computeExpiry() { $now = time(); - $nextYear = date("Y") + 1; + $thisYear = date("Y", $now); + $nextYear = $thisYear + 1; + $newYearsDay = mktime(0, 0, 0, 1, 1, $thisYear); + $hogmanay = mktime(0, 0, 0, 12, 31, $thisYear); + $threshold = strtotime("last Friday of May $thisYear"); + $nextExpiryString = 'first Friday of October'; - $thresholdString = 'last Friday of May'; - - $nextExpiry = strtotime($nextExpiryString, $now); - $threshold = strtotime($thresholdString, $now); - - if ($now < $threshold) { - return $nextExpiryString; + + if ($now >= $newYearsDay and $now < $threshold) { + $nextExpiryString .= " $thisYear"; } else { - return "$nextExpiryString $nextYear"; - } + $nextExpiryString .= " $nextYear"; + } + + return $nextExpiryString; } }