From d176bd6ab835ce746818fac0f909ba9fe68ef656 Mon Sep 17 00:00:00 2001 From: Diego Macrini Date: Fri, 23 Oct 2020 12:31:09 -0400 Subject: [PATCH] - Added example 13; - Added faster fetching of members and units from DB table instead of Profile data. --- queries/example1/example1.php | 12 ++++++----- queries/example13.php | 39 +++++++++++++++++++++++++++++++++++ src/UniwebClient.php | 37 +++++++++++++++++++++++++++++---- www/home.html | 5 +++++ 4 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 queries/example13.php diff --git a/queries/example1/example1.php b/queries/example1/example1.php index a8830d2..c92b61f 100644 --- a/queries/example1/example1.php +++ b/queries/example1/example1.php @@ -25,14 +25,16 @@ throw new Exception($response['error']); } -$unitType = 'Faculty'; -$units = $client->queryUnits($unitType, 'en'); -$unitName = $units[1]['name'] ?? false; +$units = $client->queryUnits(['sortBy' => 'memberCount']); +$unit = array_pop($units); // last unit -if (!$unitName) { - throw new Exception("Cannot find a '$unitType' unit name"); +if (!$unit) { + throw new Exception("Cannot find a unit"); } +$unitId = $unit['contentId']; +$unitName = $unit['unitName']; + // Get authorized API client $filter = ['unit' => $unitName, 'title' => 'Professor']; diff --git a/queries/example13.php b/queries/example13.php new file mode 100644 index 0000000..86e6eef --- /dev/null +++ b/queries/example13.php @@ -0,0 +1,39 @@ +queryUnits(['sortBy' => 'memberCount']); +$unit = array_pop($units); // last unit + +if (!$unit) { + throw new Exception("Cannot find a unit"); +} + +$unitId = $unit['contentId']; + + +// If "recourses" is not given, the query will fetch from a DB table +// of the requested content type. That is the fastest type of query. +// Profile resources are comparatively slower. +$request = [ + 'action' => 'read', + 'contentType' => 'members', + 'filter' => [ + 'units' => [$unitId] + ] +]; + +$response = $client->sendRequest($request); + +$unitName = $unit['unitName']; +$client->printResponse($response, "List of members in unit '$unitName':"); diff --git a/src/UniwebClient.php b/src/UniwebClient.php index b470fc0..50a259f 100644 --- a/src/UniwebClient.php +++ b/src/UniwebClient.php @@ -410,11 +410,40 @@ public function getAccessToken() /** * Query units from the server. * + * @param array $options Includes: 'lang', 'filter', and 'sortBy'. + * @return array + */ + public function queryUnits(array $options = []): array + { + $request = [ + 'contentType' => 'units', + 'lang' => $options['lang'] ?? 'en', + 'filter' => $options['filter'] ?? null + ]; + + $units = $this->read($request); + + $sortBy = $options['sortBy'] ?? false; + + if ($sortBy) { + usort($units, function ($u1, $u2) use ($sortBy) { + $a = $u1[$sortBy]; + $b = $u2[$sortBy]; + return ($a == $b) ? 0 : ($a < $b ? -1 : 1); + }); + } + + return $units; + } + + /** + * Query unit profiles from the server. + * * @param string|null $unitType Filter response by type. * @param string|null $lang Localize to selected language code ('en, 'fr). * @return array */ - public function queryUnits(string $unitType = null, string $lang = null): array + public function queryUnitProfiles(string $unitType = null, string $lang = null): array { $request = [ 'contentType' => 'units', @@ -483,9 +512,9 @@ public static function assertValidRequest($request) self::throwError('Invalid request parameters'); } - if (empty($request['resources'])) { - self::throwError('Empty "resources" property in request'); - } + // if (empty($request['resources'])) { + // self::throwError('Empty "resources" property in request'); + // } } /** diff --git a/www/home.html b/www/home.html index a9525cd..180ed87 100644 --- a/www/home.html +++ b/www/home.html @@ -83,6 +83,11 @@

Example API queries

+ + Get the members of a unit + Choose one 'University' unit and request all members in it. + + Fetch CV data Request information about sections, fields