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

Commit

Permalink
Merge pull request dingproject#2 from runephilosof/alma_availability
Browse files Browse the repository at this point in the history
Alma availability regression
  • Loading branch information
cableman committed Nov 13, 2012
2 parents c982866 + 5898133 commit 269b273
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 201 deletions.
3 changes: 3 additions & 0 deletions alma.module
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,18 @@ function alma_client_invoke($method) {
$args = func_get_args();
array_shift($args); // Lose the method.
$client = alma_client();

try {
$result = call_user_func_array(array($client, $method), $args);
}
catch (Exception $e) {
watchdog('alma', '@method error: “@message”', array('@method' => $method, '@message' => $e->getMessage()), WATCHDOG_ERROR);
throw $e;
}

return $result;
}

/**
* Get the complete organisation info from Alma.
*
Expand Down
291 changes: 91 additions & 200 deletions includes/alma.availability.inc
Original file line number Diff line number Diff line change
@@ -1,234 +1,125 @@
<?php

/**
* Implements provider availability, holdings.
*/

function alma_availability_holdings($provider_ids) {
$ids = join(',', $provider_ids);


$holding_parts = array('branch', 'department', 'location', 'sublocation', 'collection');
$details = alma_client_invoke('catalogue_record_detail', $ids);
$org = alma_get_organisation();
$result = array();

if ($details && isset($details['records'])) {
foreach ($details['records'] as $alma_id => $record) {
$holding = array(
'local_id' => $alma_id,
'available' => ($record['available_count'] > 0),
'reservable' => $record['show_reservation_button'],
'show_reservation_button' => $record['show_reservation_button'],
'holdings' => array(),
'holdings_available' => array(),
'reserved_count' => (int) $record['reservation_count'],
'total_count' => _alma_count_total($record['holdings'], $record['media_class'] == 'periodical'),
'deferred_period' => FALSE,
'issues' => array(),
'is_periodical' => ($record['media_class'] == 'periodical'),

'is_periodical' => ($record['media_class'] == 'periodical'),
);

$result[$alma_id] = $holding;

$result[$alma_id]['html'] = _alma_get_holdings($details, $holding['is_periodical']);
}
}
return $result;

}

/**
* @param type $holdings; array containing holding informations
* @param type $is_periodical; Boolean that indicates whether the shown record is a periodical or not
* @return sum of all total_count in $holdings
*/
function _alma_count_total($holdings, $is_periodical) {
$total = 0;
if ($is_periodical) {
foreach ($holdings as $year => $issues) {
foreach ($issues as $issue) {
foreach ($issue as $holding) {
$total += $holding['total_count'];
$total = $total_reservable = 0;

// START periodicals
if( $holding['is_periodical'] ) {
$parts = array();
$holding['holdings'] = array();
foreach( $record['holdings'] as $volume => $issues ) {
foreach($issues as $issue_no => $holds){
$issue = array();
$issue['branches'] = array();
foreach( $holds as $key => $branch_holding) {

if( !in_array( $branch_holding['branch_id'], $issue['branches'] ) ) {
$issue['branches'][] = $branch_holding['branch_id'];
}

$issue['local_id'] = $branch_holding['reservable'];
$issue['reservable'] = (($branch_holding['status'] == 'availableForLoan') &&
((int) $branch_holding['total_count'] - (int) $branch_holding['reference_count']));
$issues_array[$volume][$issue_no] = $issue;

if (in_array($branch_holding['collection_id'], array('karens', 'karens-'))) {
$holding['deferred_period'] = TRUE;
}

$parts = array();
$total += (int) $branch_holding['total_count'];
// Reservable is total items minus reference (which cannot be
// loaned).
$reservable = (int) $branch_holding['total_count'] - (int) $branch_holding['reference_count'];
$total_reservable += $reservable;
foreach ($holding_parts as $part) {
if (!empty($branch_holding[$part . '_id'])) {
$parts[] = $org[$part][$branch_holding[$part . '_id']];
}
}

if (!empty($branch_holding['shelf_mark'])) {
// Shelf mark might have leading >, strip any and replace the rest
// with the proper arrow.
$parts[] = strtr(trim($branch_holding['shelf_mark'], " >\n\t"), array('>' => ''));
}

$parts = array_filter($parts);

if ($parts && $branch_holding['total_count'] > $branch_holding['checked_out_count']) {
$branch_string = join('', $parts);
$holding['holdings_available'][] = $branch_string;
if( !in_array($branch_string,$holding['holdings']) ) {
$holding['holdings'][] = $branch_string;
}
}
}
}
}
}
else {
foreach ($holdings as $holding) {
$total += $holding['total_count'];
if( is_array($holding['holdings']) ) {
asort($holding['holdings']);
}
}
return $total;
}
$holding['issues'] = $issues_array;
} // END periodicals

/**
* @param type $res; returned array from alma_client class
* @param type $is_periodical; Boolean that indicates whether the shown record is a periodical or not
* @return html to be shown. Returns FALSE if no data is received from alma_client class
*/
function _alma_get_holdings($res, $is_periodical = FALSE) {
if(isset($res['records'])) {
if($is_periodical){
return _alma_set_holdings_periodical($res);
}
else {
return _alma_set_holdings($res);
}
}
else {
return FALSE;
}
}
else {
foreach ($record['holdings'] as $branch_holding) {
if (in_array($branch_holding['collection_id'], array('karens', 'karens-'))) {
$holding['deferred_period'] = TRUE;
}

/**
* set holdings for all kinds of material except periodicals
* @param array $res
* @return array $result;
*/
function _alma_set_holdings($res) {
$holdings = array();
foreach ($res['records'] as $alma_id => $records) {
foreach ($records['holdings'] as $holding) {
$holdings[] = $holding;
}
}

$result = _alma_set_table_html($holdings);
return $result;
}
$parts = array();
$total += (int) $branch_holding['total_count'];
// Reservable is total items minus reference (which cannot be
// loaned).
$reservable = (int) $branch_holding['total_count'] - (int) $branch_holding['reference_count'];
$total_reservable += $reservable;
foreach ($holding_parts as $part) {
if (!empty($branch_holding[$part . '_id'])) {
$parts[] = $org[$part][$branch_holding[$part . '_id']];
}
}

/**
* set holdings if material is periodical only
* @param array $res
* @return array $result
*/
function _alma_set_holdings_periodical($res){
$holdings = array();
foreach ($res['records'] as $alma_id => $records) {
foreach ($records['holdings'] as $holding => $issue_year) {
foreach ($issue_year as $key) {
$holdings[] = $key[0];
if (!empty($branch_holding['shelf_mark'])) {
// Shelf mark might have leading >, strip any and replace the rest
// with the proper arrow.
$parts[] = strtr(trim($branch_holding['shelf_mark'], " >\n\t"), array('>' => ''));
}
}
}

$result = _alma_set_table_html($holdings);
return $result;
}
/**
* Make the html-table
* @params $h; holding information for a given material
* @return html-table
*/
function _alma_set_table_html($h) {
// set a classname for styling the table
$variables['attributes']=
array('class'=>array(drupal_html_class('availability_holdings_table')));
// set table header
$variables['header'] =
array('placement'=>t('Placement'), 'copies'=>t('Copies'), 'Home'=>t('At home'),'reservations'=>t('Reservations'));
// set table rows
$variables['rows'] = _alma_set_rows($h);
// theme the table
// @TODO; move this to ding_availability ??
$html = theme('table',$variables );

return $html;
}

/**
* set rows in table for given holdings
* @param $h; holding information for a given material
* @return array;
*/
function _alma_set_rows($h) {
$rows = array();
$org = alma_get_organisation();

$copies_total = 0;
$home_total = 0;
$reservations_total = 0;
foreach ($h as $key => $data) {
$row = array();
$row['placement'] = $org['branch'][$data['branch_id']];

if(!empty($data['department_id'])){
$row['placement'] = $row['placement'] .''. $org['department'][$data['department_id']];
}

if(!empty($data['collection_id'])){
$row['placement'] = $row['placement'] .''. $org['collection'][$data['collection_id']];
}

$row['copies'] = (int) $data['total_count'];
$copies_total += $row['copies'];
$row['home'] = (int) $data['available_count'];
$home_total += $row['home'];
$row['reservations'] = (int) $data['ordered_count'];
$reservations_total += $row['reservations'];
$rows[] = $row;
}

if(count($rows) >= 1){
$rows = _clean_up_rows($rows);
}
//Adding last row - totals
$row = array();
$row['data']['Library'] = t('Total');
$row['data']['Copies'] = $copies_total;
$row['data']['Home'] = $home_total;
$row['data']['Reservations'] = $reservations_total;
$row['class'] = array(drupal_html_class('availability_holdings_last_row'));
$rows[] = $row;
return $rows;
}
$parts = array_filter($parts);

/**
* if the same placement exists several times collect them in one line
* @param array
* @return array;
*/
function _clean_up_rows($_rows) {
$rows = array();
$placements = array();

foreach ($_rows as $row) {
$currkey = $row['placement'];
if(!in_array($currkey, $placements)){
$placements[] = $currkey;
$placementsarr = _get_placements_with_key($_rows, $currkey);
$this_row = _sum_placement($placementsarr);
$rows[] = $this_row;
if ($parts && $branch_holding['total_count'] > $branch_holding['checked_out_count']) {
$holding['holdings'][] = join('', $parts);
}
}
}
return $rows;
}
}

/**
* collect materials with the same placement
* @param array $_rows
* @param String $currkey
* @return array $rows;
*/
function _get_placements_with_key($_rows, $currkey){
$rows = array();
foreach ($_rows as $key) {
if($key['placement'] == $currkey){
$rows[] = $key;
$holding['reservable_count'] = $total_reservable;
$holding['total_count'] = $total;
$result[$alma_id] = $holding;
}
}
return $rows;
}
/**
* sum material for same placement in one row
* @param $placementsarr; array with all instances of the same placement - ie. 'Hovedbiblioteket'
* @return array; $row
*/
function _sum_placement($placementsarr){
$row = $placementsarr[0];
for($i = 1; $i < count($placementsarr);$i++){
$next_row = $placementsarr[$i];
$row['copies'] += $next_row['copies'];
$row['home'] += $next_row['home'];
$row['reservations'] += $next_row['reservations'];
}
return $row;

return $result;
}
2 changes: 1 addition & 1 deletion lib/AlmaClient/AlmaClient.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ public function catalogue_record_detail($alma_ids) {
'request_status' => $doc->getElementsByTagName('status')->item(0)->getAttribute('value'),
'records' => array(),
);

foreach ($doc->getElementsByTagName('detailCatalogueRecord') as $elem) {
$record = AlmaClient::process_catalogue_record_details($elem);
$data['records'][$record['alma_id']] = $record;
Expand Down Expand Up @@ -776,7 +777,6 @@ private static function process_catalogue_record_holdings($elem) {
);
}


return $holdings;
}

Expand Down

0 comments on commit 269b273

Please sign in to comment.