Skip to content

Commit d176bd6

Browse files
committed
- Added example 13;
- Added faster fetching of members and units from DB table instead of Profile data.
1 parent bc927d0 commit d176bd6

File tree

4 files changed

+84
-9
lines changed

4 files changed

+84
-9
lines changed

queries/example1/example1.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@
2525
throw new Exception($response['error']);
2626
}
2727

28-
$unitType = 'Faculty';
29-
$units = $client->queryUnits($unitType, 'en');
30-
$unitName = $units[1]['name'] ?? false;
28+
$units = $client->queryUnits(['sortBy' => 'memberCount']);
29+
$unit = array_pop($units); // last unit
3130

32-
if (!$unitName) {
33-
throw new Exception("Cannot find a '$unitType' unit name");
31+
if (!$unit) {
32+
throw new Exception("Cannot find a unit");
3433
}
3534

35+
$unitId = $unit['contentId'];
36+
$unitName = $unit['unitName'];
37+
3638
// Get authorized API client
3739
$filter = ['unit' => $unitName, 'title' => 'Professor'];
3840

queries/example13.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/**
4+
* Request a list of members from the DB table Members.
5+
* By not specifying "resources", the API interprets that the desired
6+
* response is simply a list of content-types items.
7+
*/
8+
9+
require_once __DIR__ . '/../src/UniwebClient.php';
10+
11+
use Proximify\Uniweb\API\UniwebClient;
12+
13+
$client = new UniwebClient(UniwebClient::loadCredentials());
14+
15+
$units = $client->queryUnits(['sortBy' => 'memberCount']);
16+
$unit = array_pop($units); // last unit
17+
18+
if (!$unit) {
19+
throw new Exception("Cannot find a unit");
20+
}
21+
22+
$unitId = $unit['contentId'];
23+
24+
25+
// If "recourses" is not given, the query will fetch from a DB table
26+
// of the requested content type. That is the fastest type of query.
27+
// Profile resources are comparatively slower.
28+
$request = [
29+
'action' => 'read',
30+
'contentType' => 'members',
31+
'filter' => [
32+
'units' => [$unitId]
33+
]
34+
];
35+
36+
$response = $client->sendRequest($request);
37+
38+
$unitName = $unit['unitName'];
39+
$client->printResponse($response, "List of members in unit '$unitName':");

src/UniwebClient.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,40 @@ public function getAccessToken()
410410
/**
411411
* Query units from the server.
412412
*
413+
* @param array $options Includes: 'lang', 'filter', and 'sortBy'.
414+
* @return array
415+
*/
416+
public function queryUnits(array $options = []): array
417+
{
418+
$request = [
419+
'contentType' => 'units',
420+
'lang' => $options['lang'] ?? 'en',
421+
'filter' => $options['filter'] ?? null
422+
];
423+
424+
$units = $this->read($request);
425+
426+
$sortBy = $options['sortBy'] ?? false;
427+
428+
if ($sortBy) {
429+
usort($units, function ($u1, $u2) use ($sortBy) {
430+
$a = $u1[$sortBy];
431+
$b = $u2[$sortBy];
432+
return ($a == $b) ? 0 : ($a < $b ? -1 : 1);
433+
});
434+
}
435+
436+
return $units;
437+
}
438+
439+
/**
440+
* Query unit profiles from the server.
441+
*
413442
* @param string|null $unitType Filter response by type.
414443
* @param string|null $lang Localize to selected language code ('en, 'fr).
415444
* @return array
416445
*/
417-
public function queryUnits(string $unitType = null, string $lang = null): array
446+
public function queryUnitProfiles(string $unitType = null, string $lang = null): array
418447
{
419448
$request = [
420449
'contentType' => 'units',
@@ -483,9 +512,9 @@ public static function assertValidRequest($request)
483512
self::throwError('Invalid request parameters');
484513
}
485514

486-
if (empty($request['resources'])) {
487-
self::throwError('Empty "resources" property in request');
488-
}
515+
// if (empty($request['resources'])) {
516+
// self::throwError('Empty "resources" property in request');
517+
// }
489518
}
490519

491520
/**

www/home.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ <h2>Example API queries</h2>
8383
</td>
8484
</tr>
8585

86+
<tr>
87+
<td class="tg-2fdn"><a href="?example13">Get the members of a unit</a></td>
88+
<td class="tg-2fdn">Choose one 'University' unit and request all members in it.</td>
89+
</tr>
90+
8691
<tr>
8792
<td class="tg-2fdn"><a href="?example3">Fetch CV data</a></td>
8893
<td class="tg-2fdn">Request information about sections, fields

0 commit comments

Comments
 (0)