diff --git a/ding_availability.module b/ding_availability.module index 9d655d7..ec1b0bb 100644 --- a/ding_availability.module +++ b/ding_availability.module @@ -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); } @@ -198,6 +216,8 @@ 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, )); } } @@ -205,6 +225,39 @@ function ding_availability_holdings($provider_ids) { 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: @days @label', array('@days' => $days, '@label' => $label)); + + return $text; +} + /** * @param $holding * diff --git a/templates/ding-holdings.tpl.php b/templates/ding-holdings.tpl.php index 0efbb9a..b5ca876 100644 --- a/templates/ding-holdings.tpl.php +++ b/templates/ding-holdings.tpl.php @@ -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; +} + ?> -
++ +
++ +