Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 17 additions & 53 deletions modules/statistics/php/charts.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -554,25 +554,6 @@ class Charts extends \NDB_Page

$conditions = $this->_buildQueryConditions($request, false);

$recruitmentData = [];
$recruitmentStartDate = $DB->pselectOne(
"SELECT MIN(Date_registered) FROM candidate",
[]
);
$recruitmentEndDate = $DB->pselectOne(
"SELECT MAX(Date_registered) FROM candidate",
[]
);

if ($recruitmentStartDate !== null
&& $recruitmentEndDate !== null
) {
$recruitmentData['labels'] = $this->_createSiteLineChartLabels(
new \DateTimeImmutable($recruitmentStartDate),
new \DateTimeImmutable($recruitmentEndDate)
);
}

$user = \NDB_Factory::singleton()->user();
$list_of_sites = $user->getStudySites();

Expand All @@ -597,10 +578,12 @@ class Charts extends \NDB_Page
);

$recruitmentdata = [];
$labels = [];
foreach ($recruitment_summary as $row) {
$siteId = $row['SiteID'];
$year = $row['Year'];
$month = $row['Month'];
$labels["{$month}-{$year}"] = true;
if (!isset($recruitmentdata[$siteId])) {
$recruitmentdata[$siteId] = [
$year => [$month => $row['Count']]
Expand All @@ -613,6 +596,7 @@ class Charts extends \NDB_Page
}

}
$recruitmentData['labels'] = array_keys($labels);
foreach ($list_of_sites as $siteID => $siteName) {
if (!isset($recruitmentData['labels'])) {
continue;
Expand Down Expand Up @@ -755,15 +739,23 @@ class Charts extends \NDB_Page
}

if (($queryParams['dateRegisteredStart'] ?? "undefined") != 'undefined') {
$candJoin = "JOIN candidate c ON c.ID=s.CandidateID";
$paramName = 'dateStart' . (++$paramCounter);
$projectQuery .= " AND c.Date_registered >= :$paramName";
$paramName = 'dateStart' . (++$paramCounter);
if ($scansbymonth === true) {
$projectQuery .= " AND DATE(pf.Value) >= :$paramName";
} else {
$candJoin = "JOIN candidate c ON c.ID=s.CandidateID";
$projectQuery .= " AND c.Date_registered >= :$paramName";
}
$params[$paramName] = $queryParams['dateRegisteredStart'];
}
if (($queryParams['dateRegisteredEnd'] ?? "undefined") != 'undefined') {
$candJoin = "JOIN candidate c ON c.ID=s.CandidateID";
$paramName = 'dateEnd' . (++$paramCounter);
$projectQuery .= " AND c.Date_registered <= :$paramName";
$paramName = 'dateEnd' . (++$paramCounter);
if ($scansbymonth === true) {
$projectQuery .= " AND DATE(pf.Value) <= :$paramName";
} else {
$candJoin = "JOIN candidate c ON c.ID=s.CandidateID";
$projectQuery .= " AND c.Date_registered <= :$paramName";
}
$params[$paramName] = $queryParams['dateRegisteredEnd'];
}
if (is_numeric($queryParams['candidateAgeMin'] ?? null)) {
Expand Down Expand Up @@ -800,34 +792,6 @@ class Charts extends \NDB_Page
];
}

/**
* Helper to generate labels for every month between startDate and endDate.
*
* @param \DateTimeImmutable $startDate The start date for the labels.
* @param \DateTimeImmutable $endDate The end date for the labels.
*
* @return array
*/
private function _createSiteLineChartLabels(
\DateTimeImmutable $startDate,
\DateTimeImmutable $endDate
) : array {
$month = date_interval_create_from_date_string('1 month');

// Since we're only concerned with months, act as if $startDate
// is always the first of the month so that the last month doesn't
// get truncated.
$betweenDate = new \DateTimeImmutable($startDate->format('Y-m-01'));

$labels = [];

while ($betweenDate <= $endDate) {
$labels[] = $betweenDate->format('n-Y');
$betweenDate = $betweenDate->add($month);
}
return $labels;
}

/**
* Helper to generate the data for the site recruitment line for $siteID.
*
Expand Down
Loading