Skip to content

Commit 7a96808

Browse files
refactor: Filter campaigns for users - add age range
Alter filter to display campaigns for which the user meets the search criteria and add the ability to filter the returned campaigns by age range.
1 parent 7fdd385 commit 7a96808

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

code/web/Drivers/Koha.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ private function loadPatronInfoFromDB($patronId, $password, $suppliedUsernameOrB
11071107
global $logger;
11081108

11091109
/** @noinspection SqlResolve */
1110-
$sql = "SELECT borrowernumber, cardnumber, surname, firstname, streetnumber, streettype, address, address2, city, state, zipcode, country, email, phone, mobile, categorycode, dateexpiry, password, userid, branchcode, opacnote, privacy from borrowers where borrowernumber = '" . mysqli_escape_string($this->dbConnection, $patronId) . "';";
1110+
$sql = "SELECT borrowernumber, cardnumber, surname, firstname, streetnumber, streettype, address, address2, city, state, zipcode, country, email, phone, mobile, categorycode, dateexpiry, password, userid, branchcode, opacnote, privacy, dateofbirth from borrowers where borrowernumber = '" . mysqli_escape_string($this->dbConnection, $patronId) . "';";
11111111

11121112
$userExistsInDB = false;
11131113
$lookupUserResult = mysqli_query($this->dbConnection, $sql, MYSQLI_USE_RESULT);
@@ -1229,6 +1229,7 @@ private function loadPatronInfoFromDB($patronId, $password, $suppliedUsernameOrB
12291229
$user->ils_password = $password;
12301230
$user->email = $userFromDb['email'];
12311231
$user->patronType = $userFromDb['categorycode'];
1232+
$user->dateOfBirth = $userFromDb['dateofbirth'];
12321233

12331234
$user->_address1 = trim($userFromDb['streetnumber'] . ' ' . $userFromDb['address']);
12341235
$user->_address2 = $userFromDb['address2'];

code/web/sys/Account/User.php

+19
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class User extends DataObject {
5252
public $userCookiePreferenceLocalAnalytics;
5353
public $holdInfoLastLoaded;
5454
public $checkoutInfoLastLoaded;
55+
public $dateOfBirth;
5556

5657
public $onboardAppNotifications;
5758
public $shouldAskBrightness;
@@ -3068,6 +3069,24 @@ public function getPTypeObj(): ?PType {
30683069
return $this->_pTypeObj;
30693070
}
30703071

3072+
/**
3073+
* Get the user's age based on their date of birth.
3074+
*
3075+
* @return int|null The user's age or null if date of birth is not set.
3076+
*/
3077+
public function getAge(): ?int {
3078+
if (empty($this->dateOfBirth)) {
3079+
return null;
3080+
}
3081+
3082+
$dob = new DateTime($this->dateOfBirth);
3083+
$today = new DateTime();
3084+
3085+
$age = $dob->diff($today)->y;
3086+
3087+
return $age;
3088+
}
3089+
30713090
public function updatePatronInfo($canUpdateContactInfo, $fromMasquerade = false) {
30723091
$result = $this->getCatalogDriver()->updatePatronInfo($this, $canUpdateContactInfo, $fromMasquerade);
30733092
$this->clearCache();

code/web/sys/Community/Campaign.php

+28-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Campaign extends DataObject {
2020
public $unenrollmentCounter;
2121
public $currentEnrollments;
2222
public $campaignReward;
23+
public $userAgeRange;
2324

2425
/** @var AvailableMilestones[] */
2526
private $_availableMilestones;
@@ -111,6 +112,15 @@ public static function getObjectStructure($context = ''): array {
111112
'values' => $libraryList,
112113
'hideInLists' => false,
113114
],
115+
'userAgeRange' => [
116+
'property' => 'userAgeRange',
117+
'type' => 'text',
118+
'label' => 'User Age Range',
119+
'description' => 'Define the age range for this campaign e.g. "14-18", "14+", "All Ages")',
120+
'default' => 'All Ages',
121+
'maxLength' => 255,
122+
'hideInLists' => false,
123+
],
114124
];
115125
}
116126

@@ -349,10 +359,24 @@ public function find($fetchFirst = false, $requireOneMatchToReturn = true): bool
349359
if (!UserAccount::isLoggedIn() || UserAccount::getActiveUserObj()->isAspenAdminUser())
350360
return parent::find($fetchFirst, $requireOneMatchToReturn);
351361

352-
$this->joinAdd(new CampaignPatronTypeAccess(), 'INNER', 'ce_campaign_patron_type_access', 'id', 'campaignId');
353-
$this->whereAdd("ce_campaign_patron_type_access.patronTypeId = '" . UserAccount::getActiveUserObj()->getPTypeObj()->id . "'");
354-
$this->joinAdd(new CampaignLibraryAccess(), 'INNER', 'ce_campaign_library_access', 'id', 'campaignId');
355-
$this->whereAdd("ce_campaign_library_access.libraryId = '" . UserAccount::getActiveUserObj()->getHomeLibrary()->libraryId . "'");
362+
$this->joinAdd(new CampaignPatronTypeAccess(), 'LEFT', 'ce_campaign_patron_type_access', 'id', 'campaignId');
363+
$this->whereAdd("ce_campaign_patron_type_access.patronTypeId = '" . UserAccount::getActiveUserObj()->getPTypeObj()->id . "' OR ce_campaign_patron_type_access.patronTypeId IS NULL");
364+
$this->joinAdd(new CampaignLibraryAccess(), 'LEFT', 'ce_campaign_library_access', 'id', 'campaignId');
365+
$this->whereAdd("ce_campaign_library_access.libraryId = '" . UserAccount::getActiveUserObj()->getHomeLibrary()->libraryId . "' OR NOT EXISTS (SELECT 1 FROM ce_campaign_library_access WHERE ce_campaign_library_access.campaignId = ce_campaign.id)");
366+
$userAge = (int)UserAccount::getActiveUserObj()->getAge();
367+
$ageCondition = "(
368+
userAgeRange IS NULL OR
369+
userAgeRange = '' OR
370+
userAgeRange = 'All Ages' OR
371+
(userAgeRange LIKE 'Under %' AND $userAge < CAST(SUBSTRING_INDEX(userAgeRange, ' ', -1) AS UNSIGNED)) OR
372+
(userAgeRange LIKE 'Over %' AND $userAge > CAST(SUBSTRING_INDEX(userAgeRange, ' ', -1) AS UNSIGNED)) OR
373+
(userAgeRange LIKE '%+' AND $userAge >= CAST(LEFT(userAgeRange, LOCATE('+', userAgeRange) -1) AS UNSIGNED)) OR
374+
(userAgeRange LIKE '%-%' AND $userAge BETWEEN
375+
CAST(LEFT(userAgeRange, LOCATE('-', userAgeRange) -1) AS UNSIGNED) AND
376+
CAST(SUBSTRING_INDEX(userAgeRange, '-', -1) AS UNSIGNED)
377+
)
378+
)";
379+
$this->whereAdd($ageCondition);
356380
return parent::find($fetchFirst, $requireOneMatchToReturn);
357381
}
358382

code/web/sys/DBMaintenance/community_engagement_updates.php

+14
Original file line numberDiff line numberDiff line change
@@ -296,5 +296,19 @@ function getCommunityEngagementUpdates() {
296296
"INSERT INTO role_permissions(roleId, permissionId) VALUES ((SELECT roleId from roles where name='opacAdmin'), (SELECT id from permissions where name='Administer Community Module'))"
297297
],
298298
],
299+
'add_date_of_birth_to_user' => [
300+
'title' => 'Add Date Of Birth To User',
301+
'description' => 'Add the date of birth to the user object',
302+
'sql' => [
303+
"ALTER TABLE user ADD COLUMN dateOfBirth DATE"
304+
],
305+
],
306+
'add_user_age_range_to_campaign' => [
307+
'title' => 'Add User Age Range To Campaign',
308+
'description' => 'Add an ange range to the campaign',
309+
'sql' => [
310+
"ALTER TABLE ce_campaign ADD COLUMN userAgeRange VARCHAR(255) DEFAULT 'All Ages'"
311+
],
312+
],
299313
];
300314
}

0 commit comments

Comments
 (0)