Skip to content
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

Add user statistics #3

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 77 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ If the package doesn't fit your needs, you might take a look at the alternative
* [Item by barcode](#item-by-barcode)
* [Electronic portfolios and collections](#electronic-portfolios-and-collections)
* [Digital representation and files](#digital-representation-and-files)
* [Users, loans, fees and requests](#users-loans-fees-and-requests)
* [Users, loans, fees, requests and statistics](#users-loans-fees-requests-and-statistics)
* [Search](#search)
* [Loans](#loans)
* [Fees](#fees)
* [Requests](#requests)
* [Statistics](#statistics)
* [Analytics reports](#analytics-reports)
* [Column names](#column-names)
* [Filters](#filters)
Expand All @@ -43,6 +44,8 @@ If the package doesn't fit your needs, you might take a look at the alternative
* [Listing jobs](#listing-jobs)
* [Retrieving information about a specific job](#retrieving-information-about-a-specific-job)
* [Submitting a job](#submitting-a-job)
* [Code Tables](#code-tables)
* [Getting a single code table](#getting-a-codetable)
* [Automatic retries on errors](#automatic-retries-on-errors)
* [Laravel integration](#laravel-integration)
* [Customizing the HTTP client stack](#customizing-the-http-client-stack)
Expand Down Expand Up @@ -327,7 +330,7 @@ foreach ($bib->representations as $rep) {
}
```

## Users, loans, fees and requests
## Users, loans, fees, requests and statistics

**Note**: Editing is not fully implemented.

Expand Down Expand Up @@ -427,6 +430,64 @@ foreach ($user->requests as $request) {

Requests can also be retrieved from a `Bib` object or an `Item` object.

### Statistics

**Note** Not fully implemented.

Example:
```php
foreach ($user->Statistics as $statistic) {
echo json_endcode($statistic, JSON_PRETTY_PRINT);
}
```
Get Statistics:
```php
$statistics = $user->Statistics->allStatistics();
```
Get a Statistic:
```php
$statistic = $user->Statistics->get('CAMPUS','M');
```
Add a Statistic:
```php
# Create the object:
$StatisticObject = (object) [
'statistic_category' => (object) [
'value' => "M",
'desc' => "Main Campus",
],
'category_type' => (object) [
'value' => "CAMPUS",
'desc' => "Campus",
],
'statistic_note' => "New Note",
'segment_type' => "External",
];

# Add the Statistic object:
$user->Statitics->addStatistic($StatisticObject);

# Save the User:
try {
$return = $user->save();
} catch (Exception $e) {
echo 'Exception: ', $e->getMessage(), "\n";
}
```
Remove a Statistic:
```php
# Remove the Statistic:
$user->Statistics->removeStatistic('CAMPUS','M');

# Save the User:
try {
$return = $user->save();
} catch (Exception $e) {
echo 'Exception: ', $e->getMessage(), "\n";
}
```


## Analytics reports

To retrieve the results from a single report:
Expand Down Expand Up @@ -578,6 +639,20 @@ $job = $alma->jobs['M43'];
$instance = $alma->jobs['M43']->submit();
```

## Code Tables

### Getting a Code Table

To fetch a code table

```php
$ct = $alma->codetables->get('systemJobStatus');
echo "Rows for ".$ct->sub_system->value."'s ".$ct->name."\n";
foreach ($ct->row as $row) {
echo "code: ".$row->code.", description: ".$row->description."\n";
}
```

## Automatic retries on errors

If the client receives a 429 (rate limiting) response from Alma, it will sleep for a short time (0.5 seconds by default)
Expand Down
47 changes: 47 additions & 0 deletions spec/Conf/CodeTableSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace spec\Scriptotek\Alma\CodeTable;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Scriptotek\Alma\Conf\CodeTable;
use Scriptotek\Alma\Conf\CodeTables;
use Scriptotek\Alma\Client as AlmaClient;
use Scriptotek\Alma\Exception\ResourceNotFound;
use spec\Scriptotek\Alma\SpecHelper;

class CodeTableSpec extends ObjectBehavior
{
public function let(AlmaClient $client)
{
$this->beConstructedWith($client, 'systemJobStatus');
}

protected function expectRequest($client)
{
$client->getXML('/conf/code-table/systemJobStatus')
->shouldBeCalled()
->willReturn(SpecHelper::getDummyData('codetable_response.json'));
}

public function it_is_lazy(AlmaClient $client)
{
SpecHelper::expectNoRequests($client);
$this->shouldHaveType(CodeTable::class);
}

public function it_fetches_record_data_when_needed(AlmaClient $client)
{
$this->expectRequest($client);

$this->name->('systemJobStatus');
$this->subSystem->value->shouldBe('INFRA');
}

public function it_can_exist(AlmaClient $client)
{
$this->expectRequest($client);

$this->exists()->shouldBe(true);
}
}
40 changes: 40 additions & 0 deletions spec/Conf/CodeTablesSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace spec\Scriptotek\Alma\CodeTables;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Scriptotek\Alma\Conf\CodeTable;
use Scriptotek\Alma\Client as AlmaClient;
use spec\Scriptotek\Alma\SpecHelper;

class CodeTablesSpec extends ObjectBehavior
{
public function let(AlmaClient $client)
{
$this->beConstructedWith($client);
}

public function it_provides_a_lazy_interface_to_codetable_objects(AlmaClient $client)
{
SpecHelper::expectNoRequests($client);

$ctid = 'myCodeTable'; // str_random();
$bib = $this->get($ctid);

$bib->shouldHaveType(CodeTable::class);
$bib->code->shouldBe($ctid);
}

public function it_provides_a_lazy_array_interface_to_codetable_objects(AlmaClient $client)
{
SpecHelper::expectNoRequests($client);

$ctid = 'myCodeTable'; // str_random();
$ct = $this[$ctid];

$ct->shouldHaveType(CodeTable::class);
$ct->code->shouldBe($ctid);
}

}
109 changes: 109 additions & 0 deletions spec/data/codetable_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"name": "systemJobStatus",
"description": "System Job Status",
"sub_system": {
"value": "INFRA",
"desc": "Infra"
},
"patron_facing": true,
"language": {
"value": "en",
"desc": "English"
},
"scope": {
"institution_id": {
"value": "01MY_INST",
"desc": "My Institution"
},
"library_id": {
"value": "",
"desc": ""
}
},
"row": [
{
"code": "QUEUED",
"description": "Queued",
"default": false,
"enabled": true
},
{
"code": "PENDING",
"description": "Pending",
"default": false,
"enabled": true
},
{
"code": "INITIALIZING",
"description": "Initializing",
"default": false,
"enabled": true
},
{
"code": "RUNNING",
"description": "Running",
"default": false,
"enabled": true
},
{
"code": "MANUAL_HANDLING_REQUIRED",
"description": "Manual Handling Required",
"default": false,
"enabled": true
},
{
"code": "FINALIZING",
"description": "Finalizing",
"default": false,
"enabled": true
},
{
"code": "COMPLETED_SUCCESS",
"description": "Completed Successfully",
"default": false,
"enabled": true
},
{
"code": "COMPLETED_NO_BULKS",
"description": "Completed with no Bulks",
"default": false,
"enabled": true
},
{
"code": "COMPLETED_FAILED",
"description": "Completed with Errors",
"default": false,
"enabled": true
},
{
"code": "FAILED",
"description": "Failed",
"default": false,
"enabled": true
},
{
"code": "COMPLETED_WARNING",
"description": "Completed with Warnings",
"default": false,
"enabled": true
},
{
"code": "USER_ABORTED",
"description": "Aborted by User",
"default": false,
"enabled": true
},
{
"code": "SYSTEM_ABORTED",
"description": "Aborted by System",
"default": false,
"enabled": true
},
{
"code": "SKIPPED",
"description": "Skipped",
"default": false,
"enabled": true
}
]
}
7 changes: 7 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Scriptotek\Alma\Conf\Jobs;
use Scriptotek\Alma\Conf\Libraries;
use Scriptotek\Alma\Conf\Library;
use Scriptotek\Alma\Conf\CodeTables;
use Scriptotek\Alma\Exception\ClientException as AlmaClientException;
use Scriptotek\Alma\Exception\InvalidApiKey;
use Scriptotek\Alma\Exception\MaxNumberOfAttemptsExhausted;
Expand Down Expand Up @@ -103,6 +104,11 @@ class Client
*/
public $jobs;

/**
* @var CodeTables
*/
public $codetables;

/**
* @var TaskLists
*/
Expand Down Expand Up @@ -152,6 +158,7 @@ public function __construct(
$this->conf = new Conf($this);
$this->libraries = $this->conf->libraries; // shortcut
$this->jobs = $this->conf->jobs; // shortcut
$this->codetables = $this->conf->codetables; // shortcut

$this->taskLists = new TaskLists($this);

Expand Down
Loading