From c3ac8bc2f8fae0423fdc68cc2326f2dcc0e14725 Mon Sep 17 00:00:00 2001
From: Michael Birkner <michael.birkner@akwien.at>
Date: Wed, 27 Mar 2019 11:19:37 +0100
Subject: [PATCH] Using request-options API for requestable indicator. Jira
 VUFIND-1299.

---
 config/vufind/Alma.ini                       |  32 ------
 module/VuFind/src/VuFind/ILS/Driver/Alma.php | 104 ++++++-------------
 2 files changed, 32 insertions(+), 104 deletions(-)

diff --git a/config/vufind/Alma.ini b/config/vufind/Alma.ini
index e6dcfde3a26..8d3ec03df13 100644
--- a/config/vufind/Alma.ini
+++ b/config/vufind/Alma.ini
@@ -75,38 +75,6 @@ expiryDate =
 purgeDate =
 
 
-[FulfillmentUnits]
-; Specify the association of fulfillment units and its locations. Take the codes from:
-; Alma Configuration -> Fulfillment -> Fulfillment Units -> [Choose Fulfillment Unit] -> Fulfillment Unit Locations.
-; Tip: Export the list from Alma as Excel and use the CONCATENATE() formula to generate this list.
-; Format: FULFILLMENT_UNIT_CODE[] = LOCATION_CODE
-; Example:
-;	STACKS[] = stack1
-;	STACKS[] = stack2
-;	STACKS[] = stack3
-;	LIMITED[] = periodicalroom
-;	LIMITED[] = musicrefs
-;	SHORTLOAN[] = office1
-;	SHORTLOAN[] = office2
-
-[Requestable]
-; Specify for which combination of user group and fulfillment unit (see above) the request link
-; should be displayed (who is allowed to request what). Define every combination of fulfillment unit
-; and user group and assign "N" for "No, not requestable for this user group" or "Y" for "Yes, is
-; requestable for this user group". You will find the user group codes here:
-; Alma Configuration -> User Management -> User Groups
-; Format: FULFILLMENT_UNIT_CODE[USER_GROUP_CODE] = N
-; Example:
-;	STACKS[STAFF] = Y
-;	STACKS[STUDENT] = Y
-;	STACKS[GUEST] = Y
-;	LIMITED[STAFF] = Y
-;	LIMITED[STUDENT] = N
-;	LIMITED[GUEST] = N
-;	SHORTLOAN[STAFF] = Y
-;	SHORTLOAN[STUDENT] = Y
-;	SHORTLOAN[GUEST] = N
-
 [Webhook]
 ; The webhook secret. This must be the same value that was added to the Alma webhook configuration as a secret.
 secret = YOUR_WEBHOOK_SECRET_FROM_ALMA
diff --git a/module/VuFind/src/VuFind/ILS/Driver/Alma.php b/module/VuFind/src/VuFind/ILS/Driver/Alma.php
index 0ad494817de..acefab8489a 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/Alma.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/Alma.php
@@ -241,26 +241,14 @@ class Alma extends AbstractBase implements \VuFindHttp\HttpServiceAwareInterface
      */
     public function getHolding($id, array $patron = null)
     {
-        // Get config data:
-        $fulfillementUnits = $this->config['FulfillmentUnits'] ?? null;
-        $requestableConfig = $this->config['Requestable'] ?? null;
-
         $results = [];
         $copyCount = 0;
+        $username = $patron['cat_username'] ?? null;
         $bibPath = '/bibs/' . urlencode($id) . '/holdings';
         if ($holdings = $this->makeRequest($bibPath)) {
             foreach ($holdings->holding as $holding) {
                 $holdingId = (string)$holding->holding_id;
                 $locationCode = (string)$holding->location;
-                $addLink = false;
-                if ($fulfillementUnits != null && $requestableConfig != null) {
-                    $addLink = $this->requestsAllowed(
-                        $fulfillementUnits,
-                        $locationCode,
-                        $requestableConfig,
-                        $patron
-                    );
-                }
 
                 $itemPath = $bibPath . '/' . urlencode($holdingId) . '/items';
                 if ($currentItems = $this->makeRequest($itemPath)) {
@@ -299,6 +287,36 @@ class Alma extends AbstractBase implements \VuFindHttp\HttpServiceAwareInterface
                             $duedate = $this->parseDate((string)$loan->due_date);
                         }
 
+                        // Calculate request options if a user is logged-in
+                        if ($username) {
+
+                            // Call the request-options API for the logged-in user
+                            $requestOptionsPath = '/bibs/' . urlencode($id)
+                                . '/holdings/' . urlencode($holdingId) . '/items/'
+                                . urlencode($itemId) . '/request-options?user_id='
+                                . urlencode($username);
+
+                            // Make the API request
+                            $requestOptions = $this->makeRequest(
+                                $requestOptionsPath
+                            );
+
+                            // Get all possible request types from the API answer
+                            $requestTypes = $requestOptions->xpath(
+                                '/request_options/request_option//type'
+                            );
+
+                            // Add all allowed request types to an array
+                            $requestTypesArr = [];
+                            foreach ($requestTypes as $requestType) {
+                                $requestTypesArr[] = (string)$requestType;
+                            }
+
+                            // If HOLD is an allowed request type, add the link for
+                            // placing a hold
+                            $addLink = in_array('HOLD', $requestTypesArr);
+                        }
+
                         $results[] = [
                             'id' => $id,
                             'source' => 'Solr',
@@ -317,7 +335,7 @@ class Alma extends AbstractBase implements \VuFindHttp\HttpServiceAwareInterface
                             'item_notes' => $itemNotes,
                             'item_id' => $itemId,
                             'holding_id' => $holdingId,
-                            'addLink' => $addLink,
+                            'addLink' => $addLink ?? false,
                                // For Alma title-level hold requests
                             'description' => $description
                         ];
@@ -329,64 +347,6 @@ class Alma extends AbstractBase implements \VuFindHttp\HttpServiceAwareInterface
         return $results;
     }
 
-    /**
-     * Check if the user is allowed to place requests for an Alma fulfillment
-     * unit in general. We check for blocks on the patron account that could
-     * block a request in getRequestBlocks().
-     *
-     * @param array  $fulfillementUnits An array of fulfillment units and associated
-     *                                  locations from Alma.ini (see section
-     *                                  [FulfillmentUnits])
-     * @param string $locationCode      The location code of the holding to be
-     *                                  checked
-     * @param array  $requestableConfig An array of fulfillment units and associated
-     *                                  patron groups and their request policy from
-     *                                  Alma.ini (see section [Requestable])
-     * @param array  $patron            An array with the patron details (username
-     *                                  and password)
-     *
-     * @return boolean                  true if the the patron is allowed to place
-     *                                  requests on holdings of this fulfillment
-     *                                  unit, false otherwise.
-     * @author Michael Birkner
-     */
-    protected function requestsAllowed(
-        $fulfillementUnits,
-        $locationCode,
-        $requestableConfig,
-        $patron
-    ) {
-        $requestsAllowed = false;
-
-        // Get user group code
-        $cacheId = 'alma|user|' . $patron['cat_username'] . '|group_code';
-        $userGroupCode = $this->getCachedData($cacheId);
-        if ($userGroupCode === null) {
-            $profile = $this->getMyProfile($patron);
-            $userGroupCode = (string)$profile['group_code'];
-        }
-
-        // Get the fulfillment unit of the location.
-        $locationFulfillmentUnit = $this->getFulfillmentUnitByLocation(
-            $locationCode,
-            $fulfillementUnits
-        );
-
-        // Check if the group of the currently logged in user is allowed to place
-        // requests on items belonging to current fulfillment unit
-        if (($locationFulfillmentUnit != null && !empty($locationFulfillmentUnit))
-            && ($userGroupCode != null && !empty($userGroupCode))
-        ) {
-            $requestsAllowed = false;
-            if ($requestableConfig[$locationFulfillmentUnit][$userGroupCode] == 'Y'
-            ) {
-                $requestsAllowed = true;
-            }
-        }
-
-        return $requestsAllowed;
-    }
-
     /**
      * Check for request blocks.
      *
-- 
GitLab