Skip to content
Open
Show file tree
Hide file tree
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
53 changes: 53 additions & 0 deletions ding_availability.module
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,25 @@ function ding_availability_holdings($provider_ids) {
);

$rows = array();
$copies = 0;
$closest_loan = NULL;
$in_acquisition = 0;
foreach ($item['holdings'] as $holding) {
// This item is considered to be waited for.
// Next, the number of holdings is compared to reserved ones.
// Also keep the next loan date.
if (empty($item['available']) && !empty($holding['available_from'])) {
$next_loan_time = strtotime($holding['available_from']);
$copies++;
if (empty($closest_loan) || $closest_loan < $next_loan_time) {
$closest_loan = $next_loan_time;
}
}

if ($holding['status'] == 'inAcquisition') {
$in_acquisition++;
}

$rows[] = ding_availability_holdings_row($holding);
}

Expand All @@ -198,13 +216,48 @@ function ding_availability_holdings($provider_ids) {
),
'total_count' => $item['total_count'],
'reserved_count' => $item['reserved_count'],
'closest_loan' => (count($item['holdings']) == $copies) ? ding_availability_holding_format_time($closest_loan) : NULL,
'in_acquisition' => $in_acquisition,
));
}
}

return $items;
}

/**
* Generate a human-readable amount of wait time.
*
* @param int $date
* Loan date timestamp.
* @return string
* Sane display of time to wait (months/weeks/days).
*/
function ding_availability_holding_format_time($date) {
$days = ceil(($date - time()) / 86400);

$plurals = array('day' => t('days'), 'week' => t('weeks'), 'month' => t('months'));
if ($days > 35) {
$days = ceil($days / 30);
$label = 'month';
}
elseif ($days > 6) {
$days = ceil($days / 7);
$label = 'week';
}
else {
$label = 'day';
}

if ($days > 1) {
$label = $plurals[$label];
}

$text = t('The waiting time is currently: <strong>@days @label</strong>', array('@days' => $days, '@label' => $label));

return $text;
}

/**
* @param $holding
*
Expand Down
17 changes: 14 additions & 3 deletions templates/ding-holdings.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@
* - $reserved_count: Amount of reservations.
*/

$total_text = format_plural($total_count, 'We have 1 copy.', 'We have @count copies.', array('@count' => $total_count));
$reserved_text = format_plural($reserved_count, 'There is 1 user in queue to loan the material.', 'There are @count users in queue to loan the material.');
$total_text = format_plural($total_count, t('We have 1 copy'), t('We have @count copies'), array('@count' => $total_count));
$reserved_text = format_plural($reserved_count, t('There is 1 user in queue to loan the material'), t('There are @count users in queue to loan the material'));
$acquisition_text = format_plural($in_acquisition, t('(1 material under way)'), t('(@count materials under way)'), array('@count' => $in_acquisition));

if ($in_acquisition > 0) {
$total_text .= ' ' . $acquisition_text;
}

?>
<p><?php print "$total_text $reserved_text"; ?></p>
<p>
<span><?php print "$total_text. $reserved_text."; ?></span>
</p>
<p>
<?php print $closest_loan; ?>
</p>
<?php print render($holdings); ?>