Skip to content

Commit

Permalink
[Candidate] Add getAge function (aces#5945)
Browse files Browse the repository at this point in the history
Calculates the candidate's age today using php's DateTime obj diff() function, rather than the custom function used in the Utility class.
  • Loading branch information
zaliqarosli authored and AlexandraLivadas committed Jun 29, 2021
1 parent 9ca4eb6 commit b592b7b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
51 changes: 51 additions & 0 deletions php/libraries/Candidate.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,57 @@ class Candidate implements \LORIS\StudyEntities\AccessibleResource
return intval($result);
}

/**
* Returns DateInterval between candidate's date of birth and a given date,
* or today if no date given.
*
* @param ?DateTime $refDate The reference date from which to calculate the age,
* or null if not provided
*
* @return DateInterval Between DoB and $refDate
*/
public function getAge(?DateTime $refDate = null): DateInterval
{
if (!$refDate) {
$refDate = new DateTime();
}

$dob = new DateTime($this->candidateInfo["DoB"]);

return $dob->diff($refDate);
}

/**
* Returns the candidate's calculated age today in int years
*
* @return int The candidate's age today as years
*/
public function getAgeInYears(): int
{
return (int)$this->getAge()->format('%y');
}

/**
* Returns the candidate's calculated age today in total number of months
*
* @return int The candidate's age today in months
*/
public function getAgeInMonths(): int
{
return (int)$this->getAge()->format('%m') +
12 * $this->getAgeInYears();
}

/**
* Returns the candidate's calculated age today in total number of days
*
* @return int The candidate's age today in days
*/
public function getAgeInDays(): int
{
return (int)$this->getAge()->days;
}

/**
* Returns list of consents and their respective statuses for this candidate
*
Expand Down
56 changes: 56 additions & 0 deletions test/unittests/CandidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,62 @@ public function testGetNextVisitNoReturnsOneWhenNextVisitDoesNotExist()
$this->_candidate->select($this->_candidateInfo['CandID']);
$this->assertEquals(1, $this->_candidate->getNextVisitNo());
}
/**
* Test getAge returns correct DateTime Interval $y, $m, $d properties
*
* @covers Candidate::getAge()
* @return void
*/
public function testGetAgeReturnsCorrectDateTimeInterval()
{
$this->_setUpTestDoublesForSelectCandidate();
$this->_candidate->select($this->_candidateInfo['CandID']);

$referenceDate = new DateTime('2020-02-25');
$this->assertEquals(12, $this->_candidate->getAge($referenceDate)->y);
$this->assertEquals(11, $this->_candidate->getAge($referenceDate)->m);
$this->assertEquals(23, $this->_candidate->getAge($referenceDate)->d);
}
/**
* Test getAgeInYears returns age as int years
*
* @covers Candidate::getAgeInYears()
* @return void
*/
public function testGetAgeInYearsReturnsIntYears()
{
$this->_setUpTestDoublesForSelectCandidate();
$this->_candidate->select($this->_candidateInfo['CandID']);

$this->assertEquals($this->_candidate->getAge()->format('%y'), $this->_candidate->getAgeInYears());
}
/**
* Test getAgeInMonths returns age in months
*
* @covers Candidate::getAgeInMonths()
* @return void
*/
public function testGetAgeInMonthsReturnsMonths()
{
$this->_setUpTestDoublesForSelectCandidate();
$this->_candidate->select($this->_candidateInfo['CandID']);

$expectedAge = intval($this->_candidate->getAge()->format('%m')) + 12 * intval($this->_candidate->getAge()->format('%y'));
$this->assertEquals($expectedAge, $this->_candidate->getAgeInMonths());
}
/**
* Test getAgeInDays returns age in days
*
* @covers Candidate::getAgeInDays()
* @return void
*/
public function testGetAgeInDaysReturnsDays()
{
$this->_setUpTestDoublesForSelectCandidate();
$this->_candidate->select($this->_candidateInfo['CandID']);

$this->assertEquals($this->_candidate->getAge()->days, $this->_candidate->getAgeInDays());
}
/**
* Test getSessionID returns session ID for a given existing visit
*
Expand Down

0 comments on commit b592b7b

Please sign in to comment.