-
Notifications
You must be signed in to change notification settings - Fork 9
DDBEASY-116 - Refactored the availability and reservable logic. #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2ecda51
7acce88
80b859a
a898eaa
1c70723
e030f4f
a70173c
6cf3f53
c766e75
2cee2d6
9c674a2
67321a1
fddf207
dc1fb96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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']; }); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment explaining why this is necessary. The Since the |
||
| if (!$items) { | ||
| return array(); | ||
| } | ||
|
|
@@ -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++; | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this change intentionally part of this pull request? Extracting |
||
| $rows[] = ding_availability_holdings_row($holding); | ||
| } | ||
|
|
||
|
|
@@ -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, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. Also: How about using |
||
| } | ||
|
|
||
| /** | ||
| * @param $holding | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'); | ||
|
|
||
|
|
@@ -121,7 +121,7 @@ | |
| } | ||
| } | ||
|
|
||
| if (!available && !reservable) { | ||
| else { | ||
| element.addClass('not-reservable'); | ||
|
|
||
| // Add class to reserve button | ||
|
|
@@ -130,6 +130,9 @@ | |
| } | ||
| } | ||
| } | ||
| else { | ||
| reserver_btn.addClass('not-reservable'); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why add the 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. |
||
| }); | ||
| } | ||
|
|
||
|
|
@@ -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(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above regarding queue time. |
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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']; | ||
| } | ||
| }); | ||
|
|
||
|
|
@@ -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); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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); | ||
| } | ||
| } | ||
|
|
@@ -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); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well done! Is this none usable in |
||
| } | ||
|
|
||
| update_availability_type(element, class_name); | ||
| update_availability_type(element, status); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above regarding acquisition and queue time. |
||
| <?php print render($holdings); ?> | ||
There was a problem hiding this comment.
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.