Skip to content

Commit

Permalink
Added alma create and deletion of reservations
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed Jul 13, 2011
1 parent 2f71114 commit 4b691ab
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 21 deletions.
2 changes: 2 additions & 0 deletions alma.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*
*/

define('ALMA_AUTH_BLOCKED', '4e5531951f55ab8f6895684999c69c2');

/**
* Implements hook_requirements().
*/
Expand Down
101 changes: 81 additions & 20 deletions alma.reservation.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?php

/**
* @todo
* store reservation list information in session and user status.
*
* @todo
* Where do the user enter the interest periode and preferred branch
* information. (THIS IS NOT IMPLEMENTED YET, SO DEFAULT IS USED)
*/


/**
* Get list of pickup branches.
*/
Expand All @@ -16,25 +26,26 @@ function alma_reservation_pickup_branches($account) {
* Set preferred pickup branch
*/
function alma_reservation_set_preferred_branch($account, $branch) {
// Throw exception if we're not logged in.
if (!ding_user_get_creds()) {
throw new DingProviderAuthException;
}
// Do nothing, ding_reservation will update local user if we don't throw an exception.
// Throws an exception if we are not logged in.
$creds = ding_user_get_creds($account, TRUE);

// @todo Should call alma an update the branch in the library system.
// Ding_reservation will update local user if we don't throw an exception.

/**
* @todo Should call alma an update the branch in the library system.
*/
}

/**
* Get list of reserved items
* Get list of reserved items.
*/
function alma_reservation_list($account) {
$creds = ding_user_get_creds();
$creds = ding_user_get_creds($account, TRUE);
$reservations = alma_client_invoke('get_reservations', $creds['name'], $creds['pass']);
$result = array();

// Create DingProviderReservation objects.
// Create DingProviderReservation objects into to categories base on pickup
// status.
foreach ($reservations as $reservation) {
if (isset($reservation['pickup_number'])) {
$result[$reservation['id']] = new DingProviderReservation($reservation['id'], array(
Expand Down Expand Up @@ -65,42 +76,92 @@ function alma_reservation_set_preferred_branch($account, $branch) {
}

/**
* Create a reservation for a given account
* Create a reservation for a given account.
*/
function alma_reservation_create($account, $id, $branch, $expiry) {
$creds = ding_user_get_creds($account, TRUE);

// Check if the users has this reservation and throw exception.
if (alma_reservation_exists($creds, $id)) {
throw new DingProviderReservationExists();
}

// Build the reservation parameters to send.
$params = array(
'id' => $id,
'valid_from' => date('Y-m-d'),
'valid_to' => alma_reservation_format_date($expiry),
'pickup_branch' => $branch,
);

// Try to make the reservation.
$result = alma_client_invoke('add_reservation', $creds['name'], $creds['pass'], $params);

if ($result === ALMA_AUTH_BLOCKED) {
/**
* @todo return better exception that informs the user about the block
* status.
*/
throw new DingProviderReservationNotAllowed();
}

if ($result) {
return array($id => TRUE);
}
else {
throw new DingProviderReservationNotAvailable();
}
}

/**
* Update order, by defining new expiry date or pickup branch
* Update order, by defining new expiry date or pickup branch.
*/
function alma_reservation_update_order($account, $order_id, $pickup_branch, $expiry_date) {

// TODO:
//
// It's not allowed to change pickup branch for ready to pickup stuff.
//
// throw new DingProviderUserException(t('Error: ' . $booking[$id]));
}

/**
* Delete a reservation for a given account
* Delete a reservation for a given account.
*/
function alma_reservation_delete($account, $id) {

$creds = ding_user_get_creds($account, TRUE);
$reservations = alma_client_invoke('get_reservations', $creds['name'], $creds['pass']);
return alma_client_invoke('remove_reservation', $creds['name'], $creds['pass'], $reservations[$id]);
}

/**
* Return a branch name for a given branch id
* Return a branch name for a given branch id.
*
* @param $branch_id String
* @return String
*/
function alma_reservation_branch_name($branch_id) {
// Throw exception if we're not logged in.
if (!ding_user_get_creds()) {
throw new DingProviderAuthException;
}

// Get library organistation from alma.
$organisation = alma_get_organisation();
if (isset($organisation['branch'][$branch_id])) {
return $organisation['branch'][$branch_id];
}
return NULL;
}

/**
* @return bool
*/
function alma_reservation_exists($creds, $item_id) {
$reservations = alma_client_invoke('get_reservations', $creds['name'], $creds['pass']);
foreach ($reservations as $res) {
if ($res['record_id'] == $item_id) {
return TRUE;
}
}
return FALSE;
}

function alma_reservation_format_date($datetime) {
$timestamp = (!is_int($datetime)) ? strtotime($datetime) : $datetime;
return date('Y-m-d', $timestamp);
}
2 changes: 1 addition & 1 deletion lib/AlmaClient/AlmaClient.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public function add_reservation($borr_card, $pin_code, $reservation) {

// Return error code when patron is blocked.
if ($res_message == 'reservationPatronBlocked') {
return DING_PROVIDER_AUTH_BLOCKED;
return ALMA_AUTH_BLOCKED;
}

// General catchall if status is not okay is to report failure.
Expand Down

0 comments on commit 4b691ab

Please sign in to comment.