-
Notifications
You must be signed in to change notification settings - Fork 0
/
asu_user.module
60 lines (52 loc) · 1.6 KB
/
asu_user.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* @file
* asu_user.module - ASU User code.
*
* @author
* Dan Poenaru <[email protected]>
*
* Module provides customization functions for the users. Some code is from the
* asu_userpicker made by Michael Samuelson.
*
*/
/**
* API function to get a person/profile record from Elastic.
*
* @param string|int $asurite An ASURITE.
*
* @return array Elastic record for profile.
*/
function _asu_user_get_elastic_profile_record($asurite) {
// Abort.
if (is_null($asurite)) {
return;
}
// Do Elastic query ala https://search.asu.edu/api/v1/webdir-profiles/faculty-staff/filtered?asurite_ids=myasurite
// And populate $record.
$elastic_base_url = \Drupal::config('asu_user.settings')->get('asu_user_elastic_query_url');
$elastic_query_url = $elastic_base_url . '?asurite_ids=' . \Drupal\Component\Utility\Html::escape($asurite);
$client = \Drupal::httpClient();
try {
$response = $client->get($elastic_query_url);
$data = $response->getBody()->getContents();
$elastic_data = \Drupal\Component\Serialization\Json::decode($data);
// Check here that the asurite ID is a 100% match with the record, as
// Elastic does a "contains" match.
$record = NULL;
$elastic_count = $elastic_data['meta']['page']['total_results'];
if ($elastic_count > 0) {
foreach ($elastic_data['results'] as $result) {
if ($result['asurite_id']['raw'] == $asurite) {
$record = $result;
}
}
}
return $record; // Should only be one.
}
catch (Exception $e) {
watchdog_exception('asu_user', $e);
return [];
}
return [];
}