Skip to content
This repository was archived by the owner on May 2, 2018. It is now read-only.

Commit 269b273

Browse files
committed
Merge pull request dingproject#2 from runephilosof/alma_availability
Alma availability regression
2 parents c982866 + 5898133 commit 269b273

File tree

3 files changed

+95
-201
lines changed

3 files changed

+95
-201
lines changed

alma.module

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,18 @@ function alma_client_invoke($method) {
174174
$args = func_get_args();
175175
array_shift($args); // Lose the method.
176176
$client = alma_client();
177+
177178
try {
178179
$result = call_user_func_array(array($client, $method), $args);
179180
}
180181
catch (Exception $e) {
181182
watchdog('alma', '@method error: “@message”', array('@method' => $method, '@message' => $e->getMessage()), WATCHDOG_ERROR);
182183
throw $e;
183184
}
185+
184186
return $result;
185187
}
188+
186189
/**
187190
* Get the complete organisation info from Alma.
188191
*

includes/alma.availability.inc

Lines changed: 91 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -1,234 +1,125 @@
11
<?php
2-
32
/**
43
* Implements provider availability, holdings.
54
*/
6-
75
function alma_availability_holdings($provider_ids) {
86
$ids = join(',', $provider_ids);
9-
7+
8+
$holding_parts = array('branch', 'department', 'location', 'sublocation', 'collection');
109
$details = alma_client_invoke('catalogue_record_detail', $ids);
10+
$org = alma_get_organisation();
1111
$result = array();
12-
1312
if ($details && isset($details['records'])) {
1413
foreach ($details['records'] as $alma_id => $record) {
1514
$holding = array(
1615
'local_id' => $alma_id,
1716
'available' => ($record['available_count'] > 0),
1817
'reservable' => $record['show_reservation_button'],
1918
'show_reservation_button' => $record['show_reservation_button'],
20-
'holdings' => array(),
21-
'holdings_available' => array(),
2219
'reserved_count' => (int) $record['reservation_count'],
23-
'total_count' => _alma_count_total($record['holdings'], $record['media_class'] == 'periodical'),
2420
'deferred_period' => FALSE,
25-
'issues' => array(),
26-
'is_periodical' => ($record['media_class'] == 'periodical'),
21+
22+
'is_periodical' => ($record['media_class'] == 'periodical'),
2723
);
28-
29-
$result[$alma_id] = $holding;
30-
31-
$result[$alma_id]['html'] = _alma_get_holdings($details, $holding['is_periodical']);
32-
}
33-
}
34-
return $result;
35-
36-
}
3724

38-
/**
39-
* @param type $holdings; array containing holding informations
40-
* @param type $is_periodical; Boolean that indicates whether the shown record is a periodical or not
41-
* @return sum of all total_count in $holdings
42-
*/
43-
function _alma_count_total($holdings, $is_periodical) {
44-
$total = 0;
45-
if ($is_periodical) {
46-
foreach ($holdings as $year => $issues) {
47-
foreach ($issues as $issue) {
48-
foreach ($issue as $holding) {
49-
$total += $holding['total_count'];
25+
$total = $total_reservable = 0;
26+
27+
// START periodicals
28+
if( $holding['is_periodical'] ) {
29+
$parts = array();
30+
$holding['holdings'] = array();
31+
foreach( $record['holdings'] as $volume => $issues ) {
32+
foreach($issues as $issue_no => $holds){
33+
$issue = array();
34+
$issue['branches'] = array();
35+
foreach( $holds as $key => $branch_holding) {
36+
37+
if( !in_array( $branch_holding['branch_id'], $issue['branches'] ) ) {
38+
$issue['branches'][] = $branch_holding['branch_id'];
39+
}
40+
41+
$issue['local_id'] = $branch_holding['reservable'];
42+
$issue['reservable'] = (($branch_holding['status'] == 'availableForLoan') &&
43+
((int) $branch_holding['total_count'] - (int) $branch_holding['reference_count']));
44+
$issues_array[$volume][$issue_no] = $issue;
45+
46+
if (in_array($branch_holding['collection_id'], array('karens', 'karens-'))) {
47+
$holding['deferred_period'] = TRUE;
48+
}
49+
50+
$parts = array();
51+
$total += (int) $branch_holding['total_count'];
52+
// Reservable is total items minus reference (which cannot be
53+
// loaned).
54+
$reservable = (int) $branch_holding['total_count'] - (int) $branch_holding['reference_count'];
55+
$total_reservable += $reservable;
56+
foreach ($holding_parts as $part) {
57+
if (!empty($branch_holding[$part . '_id'])) {
58+
$parts[] = $org[$part][$branch_holding[$part . '_id']];
59+
}
60+
}
61+
62+
if (!empty($branch_holding['shelf_mark'])) {
63+
// Shelf mark might have leading >, strip any and replace the rest
64+
// with the proper arrow.
65+
$parts[] = strtr(trim($branch_holding['shelf_mark'], " >\n\t"), array('>' => ''));
66+
}
67+
68+
$parts = array_filter($parts);
69+
70+
if ($parts && $branch_holding['total_count'] > $branch_holding['checked_out_count']) {
71+
$branch_string = join('', $parts);
72+
$holding['holdings_available'][] = $branch_string;
73+
if( !in_array($branch_string,$holding['holdings']) ) {
74+
$holding['holdings'][] = $branch_string;
75+
}
76+
}
5077
}
5178
}
5279
}
53-
}
54-
else {
55-
foreach ($holdings as $holding) {
56-
$total += $holding['total_count'];
80+
if( is_array($holding['holdings']) ) {
81+
asort($holding['holdings']);
5782
}
58-
}
59-
return $total;
60-
}
83+
$holding['issues'] = $issues_array;
84+
} // END periodicals
6185

62-
/**
63-
* @param type $res; returned array from alma_client class
64-
* @param type $is_periodical; Boolean that indicates whether the shown record is a periodical or not
65-
* @return html to be shown. Returns FALSE if no data is received from alma_client class
66-
*/
67-
function _alma_get_holdings($res, $is_periodical = FALSE) {
68-
if(isset($res['records'])) {
69-
if($is_periodical){
70-
return _alma_set_holdings_periodical($res);
71-
}
72-
else {
73-
return _alma_set_holdings($res);
74-
}
75-
}
76-
else {
77-
return FALSE;
78-
}
79-
}
86+
else {
87+
foreach ($record['holdings'] as $branch_holding) {
88+
if (in_array($branch_holding['collection_id'], array('karens', 'karens-'))) {
89+
$holding['deferred_period'] = TRUE;
90+
}
8091

81-
/**
82-
* set holdings for all kinds of material except periodicals
83-
* @param array $res
84-
* @return array $result;
85-
*/
86-
function _alma_set_holdings($res) {
87-
$holdings = array();
88-
foreach ($res['records'] as $alma_id => $records) {
89-
foreach ($records['holdings'] as $holding) {
90-
$holdings[] = $holding;
91-
}
92-
}
93-
94-
$result = _alma_set_table_html($holdings);
95-
return $result;
96-
}
92+
$parts = array();
93+
$total += (int) $branch_holding['total_count'];
94+
// Reservable is total items minus reference (which cannot be
95+
// loaned).
96+
$reservable = (int) $branch_holding['total_count'] - (int) $branch_holding['reference_count'];
97+
$total_reservable += $reservable;
98+
foreach ($holding_parts as $part) {
99+
if (!empty($branch_holding[$part . '_id'])) {
100+
$parts[] = $org[$part][$branch_holding[$part . '_id']];
101+
}
102+
}
97103

98-
/**
99-
* set holdings if material is periodical only
100-
* @param array $res
101-
* @return array $result
102-
*/
103-
function _alma_set_holdings_periodical($res){
104-
$holdings = array();
105-
foreach ($res['records'] as $alma_id => $records) {
106-
foreach ($records['holdings'] as $holding => $issue_year) {
107-
foreach ($issue_year as $key) {
108-
$holdings[] = $key[0];
104+
if (!empty($branch_holding['shelf_mark'])) {
105+
// Shelf mark might have leading >, strip any and replace the rest
106+
// with the proper arrow.
107+
$parts[] = strtr(trim($branch_holding['shelf_mark'], " >\n\t"), array('>' => ''));
109108
}
110-
}
111-
}
112-
113-
$result = _alma_set_table_html($holdings);
114-
return $result;
115-
}
116-
/**
117-
* Make the html-table
118-
* @params $h; holding information for a given material
119-
* @return html-table
120-
*/
121-
function _alma_set_table_html($h) {
122-
// set a classname for styling the table
123-
$variables['attributes']=
124-
array('class'=>array(drupal_html_class('availability_holdings_table')));
125-
// set table header
126-
$variables['header'] =
127-
array('placement'=>t('Placement'), 'copies'=>t('Copies'), 'Home'=>t('At home'),'reservations'=>t('Reservations'));
128-
// set table rows
129-
$variables['rows'] = _alma_set_rows($h);
130-
// theme the table
131-
// @TODO; move this to ding_availability ??
132-
$html = theme('table',$variables );
133-
134-
return $html;
135-
}
136109

137-
/**
138-
* set rows in table for given holdings
139-
* @param $h; holding information for a given material
140-
* @return array;
141-
*/
142-
function _alma_set_rows($h) {
143-
$rows = array();
144-
$org = alma_get_organisation();
145-
146-
$copies_total = 0;
147-
$home_total = 0;
148-
$reservations_total = 0;
149-
foreach ($h as $key => $data) {
150-
$row = array();
151-
$row['placement'] = $org['branch'][$data['branch_id']];
152-
153-
if(!empty($data['department_id'])){
154-
$row['placement'] = $row['placement'] .''. $org['department'][$data['department_id']];
155-
}
156-
157-
if(!empty($data['collection_id'])){
158-
$row['placement'] = $row['placement'] .''. $org['collection'][$data['collection_id']];
159-
}
160-
161-
$row['copies'] = (int) $data['total_count'];
162-
$copies_total += $row['copies'];
163-
$row['home'] = (int) $data['available_count'];
164-
$home_total += $row['home'];
165-
$row['reservations'] = (int) $data['ordered_count'];
166-
$reservations_total += $row['reservations'];
167-
$rows[] = $row;
168-
}
169-
170-
if(count($rows) >= 1){
171-
$rows = _clean_up_rows($rows);
172-
}
173-
//Adding last row - totals
174-
$row = array();
175-
$row['data']['Library'] = t('Total');
176-
$row['data']['Copies'] = $copies_total;
177-
$row['data']['Home'] = $home_total;
178-
$row['data']['Reservations'] = $reservations_total;
179-
$row['class'] = array(drupal_html_class('availability_holdings_last_row'));
180-
$rows[] = $row;
181-
return $rows;
182-
}
110+
$parts = array_filter($parts);
183111

184-
/**
185-
* if the same placement exists several times collect them in one line
186-
* @param array
187-
* @return array;
188-
*/
189-
function _clean_up_rows($_rows) {
190-
$rows = array();
191-
$placements = array();
192-
193-
foreach ($_rows as $row) {
194-
$currkey = $row['placement'];
195-
if(!in_array($currkey, $placements)){
196-
$placements[] = $currkey;
197-
$placementsarr = _get_placements_with_key($_rows, $currkey);
198-
$this_row = _sum_placement($placementsarr);
199-
$rows[] = $this_row;
112+
if ($parts && $branch_holding['total_count'] > $branch_holding['checked_out_count']) {
113+
$holding['holdings'][] = join('', $parts);
114+
}
200115
}
201-
}
202-
return $rows;
203-
}
116+
}
204117

205-
/**
206-
* collect materials with the same placement
207-
* @param array $_rows
208-
* @param String $currkey
209-
* @return array $rows;
210-
*/
211-
function _get_placements_with_key($_rows, $currkey){
212-
$rows = array();
213-
foreach ($_rows as $key) {
214-
if($key['placement'] == $currkey){
215-
$rows[] = $key;
118+
$holding['reservable_count'] = $total_reservable;
119+
$holding['total_count'] = $total;
120+
$result[$alma_id] = $holding;
216121
}
217122
}
218-
return $rows;
219-
}
220-
/**
221-
* sum material for same placement in one row
222-
* @param $placementsarr; array with all instances of the same placement - ie. 'Hovedbiblioteket'
223-
* @return array; $row
224-
*/
225-
function _sum_placement($placementsarr){
226-
$row = $placementsarr[0];
227-
for($i = 1; $i < count($placementsarr);$i++){
228-
$next_row = $placementsarr[$i];
229-
$row['copies'] += $next_row['copies'];
230-
$row['home'] += $next_row['home'];
231-
$row['reservations'] += $next_row['reservations'];
232-
}
233-
return $row;
123+
124+
return $result;
234125
}

lib/AlmaClient/AlmaClient.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ public function catalogue_record_detail($alma_ids) {
656656
'request_status' => $doc->getElementsByTagName('status')->item(0)->getAttribute('value'),
657657
'records' => array(),
658658
);
659+
659660
foreach ($doc->getElementsByTagName('detailCatalogueRecord') as $elem) {
660661
$record = AlmaClient::process_catalogue_record_details($elem);
661662
$data['records'][$record['alma_id']] = $record;
@@ -776,7 +777,6 @@ private static function process_catalogue_record_holdings($elem) {
776777
);
777778
}
778779

779-
780780
return $holdings;
781781
}
782782

0 commit comments

Comments
 (0)