Skip to content
Closed
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
2 changes: 1 addition & 1 deletion ding_availability.field.inc
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ function ding_availability_field_formatter_view($entity_type, $entity, $field, $
'link' => array(
// The online url can't be used with l(), has it will encode
// it.
'#markup' => '<a class="search-result--availability-link availability available" href="/' . $url['path'] . '">' . check_plain($type) . '</a>',
'#markup' => '<a id="' . $id . '" class="search-result--availability-link availability available" href="/' . $url['path'] . '">' . check_plain($type) . '</a>',
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion ding_availability.make
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ projects[virtual_field][version] = "1.2"
projects[ding_provider][type] = "module"
projects[ding_provider][download][type] = "git"
projects[ding_provider][download][url] = "git@github.com:ding2/ding_provider.git"
projects[ding_provider][download][branch] = "master"
projects[ding_provider][download][tag] = "v2.0.0"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should not be a part of the pull request.


54 changes: 54 additions & 0 deletions ding_availability.module
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ function ding_availability_items($provider_ids) {
*/
function ding_availability_holdings($provider_ids) {
$items = ding_provider_invoke('availability', 'holdings', $provider_ids);
$items = array_filter($items, function($v) { return $v['availability_information']; });
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment explaining why this is necessary. The availability_information entry does not seem to be used further down the line.

Since the array_filter callback expects a boolean I would appreciate wrapping $v['availability_information']; in empty(). This should also prevent warnings if the entry is missing.

if (!$items) {
return array();
}
Expand Down Expand Up @@ -184,7 +185,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++;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change intentionally part of this pull request?

Extracting closest_loan and in_acquisition does not seem to be related to issue 618. I would also think that supporting this would require changes to the provider implementations.

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

Expand All @@ -198,13 +217,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,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

));
}
}

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;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

Also: How about using format_interval for this?

}

/**
* @param $holding
*
Expand Down
24 changes: 17 additions & 7 deletions js/ding_availability.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@
var available = false;
var reservable = false;
var is_internet = false;
var element = $('#' + id);
element.removeClass('pending').addClass('processed');

$.each(entity_ids, function(index, entity_id) {
// Reserve button
var reserver_btn = element.parents('.ting-object:first').find('a[id$=' + entity_id + '].reserve-button');

if (Drupal.DADB[entity_id]) {
available = available || Drupal.DADB[entity_id]['available'];
reservable = reservable || Drupal.DADB[entity_id]['reservable'];
is_internet = is_internet || Drupal.DADB[entity_id]['is_internet'];

var element = $('#' + id);
element.removeClass('pending').addClass('processed');

// Reserve button
var reserver_btn = element.parents('.ting-object:first').find('.reserve-button');

if (available) {
element.addClass('available');

Expand All @@ -121,7 +121,7 @@
}
}

if (!available && !reservable) {
else {
element.addClass('not-reservable');

// Add class to reserve button
Expand All @@ -130,6 +130,9 @@
}
}
}
else {
reserver_btn.addClass('not-reservable');
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add the not-reservable class to an element which we do not know the status for?

If we are to add a class I would expect it that we should add it to the element as well as in the other branches of the code.

});
}

Expand All @@ -148,6 +151,13 @@
if (Drupal.DADB[entity_id] && (Drupal.DADB[entity_id]['holdings'])) {
// Show status for material.
$('#' + id).html(Drupal.DADB[entity_id].html);
// Don't show queue time if item not reservable.
if (Drupal.DADB[entity_id].reservable === false) {
$('#' + id + ' span.in-queue').hide();
}
}
else {
$('div.group-holdings-available').parent().parent().remove();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above regarding queue time.

}
}

Expand Down
111 changes: 59 additions & 52 deletions js/ding_availability_labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,17 @@
*/
function update_availability(id, entity_ids) {
// Default the status to not available and not reservable.
var available = false;
var reservable = false;
var status = {
available: false,
reservable: false
};

// Loop over the entity ids and if one has available or reservable
// true save that value.
$.each(entity_ids, function(index, entity_id) {
if (Drupal.DADB[entity_id]) {
available = available || Drupal.DADB[entity_id]['available'];
reservable = reservable || Drupal.DADB[entity_id]['reservable'];
status.available = status.available || Drupal.DADB[entity_id]['available'];
status.reservable = status.reservable || Drupal.DADB[entity_id]['reservable'];
}
});

Expand All @@ -91,20 +93,9 @@

// Get hold of the reserve button (it hidden as default, so we may need
// to show it).
var reserver_btn = element.parents('.ting-object:first').find('[id^=ding-reservation-reserve-form]');
var reserver_btn = element.parents('.ting-object:first').find('.reserve-button');

if (available) {
update_availability_elements(element, reserver_btn, 'available');
}
else if (reservable) {
update_availability_elements(element, reserver_btn, 'reservable');
}
else if (!available && !reservable) {
update_availability_elements(element, reserver_btn, 'not-reservable');
}
else {
update_availability_elements(element, reserver_btn, 'unavailable');
}
update_availability_elements(element, reserver_btn, status);
}

/**
Expand All @@ -113,38 +104,38 @@
*
* @param element
* The target element (material that should be moved).
* @param type
* The type of availability the element has.
* @param status
* Structure with available and reservable state.
*/
function update_availability_type(element, type) {
// Select type, if any or create the type.
function update_availability_type(element, status) {
var groups_wrapper = element.closest('.search-result--availability');
var group = $('.js-' + type, groups_wrapper);
if (group.length !== 1) {
// Create group.
if (type === 'available') {
// Add as first line.
group = $('<p class="js-' + type + '">' + Drupal.t('Available') + ': </p>');
groups_wrapper.prepend(group);
}
else if (type === 'unavailable') {
// Add last.
group = $('<p class="js-' + type + '">' + Drupal.t('Unavailable') + ': </p>');
group.insertBefore('.js-pending', groups_wrapper);
var reservable = status['reservable'];
var available = status['available'];

var group = null;
if ($('.js-online', groups_wrapper).length !== 0) {
group = $('.js-online', groups_wrapper);
}
else if (available) {
group = $('.js-available', groups_wrapper);

if (group.length === 0) {
group = $('<p class="js-available">' + Drupal.t('Available') + ': </p>');
groups_wrapper.append(group);
}
else if (type === 'reservable') {
// First or after available.
group = $('<p class="js-' + type + '">' + Drupal.t('Reservable') + ': </p>');
if ($('.js-available', groups_wrapper).length) {
group.insertAfter($('.js-available', groups_wrapper));
}
else {
groups_wrapper.prepend(group);
}
}
else if (reservable) {
group = $('.js-reservable', groups_wrapper);
if (group.length === 0) {
group = $('<p class="js-reservable">' + Drupal.t('Reservable') + ': </p>');
groups_wrapper.append(group);
}
else {
// Add last to not-reservable.
group = $('<p class="js-not-reservable">' + Drupal.t('Not reservable') + ': </p>');
}
else {
group = $('.js-unavailable', groups_wrapper);

if (group.length === 0) {
group = $('<p class="js-unavailable">' + Drupal.t('Not available') + ': </p>');
groups_wrapper.append(group);
}
}
Expand Down Expand Up @@ -182,16 +173,32 @@
* jQuery availability element to add the class to.
* @param btn
* Reservation button to add the class to.
* @param class_name
* The class to add to the elements.
* @param status
* Structure with available and reservable state.
*/
function update_availability_elements(element, btn, class_name) {
element.addClass(class_name);
if (btn.length) {
btn.addClass(class_name);
function update_availability_elements(element, btn, status) {
var class_name = null;

for (var i in status) {
if (status[i] === true) {
class_name = i;
}
else {
if (i === 'available') {
class_name = 'un' + i;
}
else if (i === 'reservable') {
class_name = 'not-' + i;
}
}

element.addClass(class_name);
if (btn.length) {
btn.addClass(class_name);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done!

Is this none usable in ding_availability.js? It seems as if it addresses the exact same things.

}

update_availability_type(element, class_name);
update_availability_type(element, status);
}
}
};
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 . "<span class='in-queue'>" . $reserved_text . "</span>"; ?></span>
</p>
<p>
<?php print $closest_loan; ?>
</p>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above regarding acquisition and queue time.

<?php print render($holdings); ?>