-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasu_user.module
More file actions
61 lines (55 loc) · 1.7 KB
/
asu_user.module
File metadata and controls
61 lines (55 loc) · 1.7 KB
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
60
61
<?php
use Drupal\Component\Utility\Html;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\DeprecationHelper;
use Drupal\Core\Utility\Error;
/**
* @file
* asu_user.module - ASU User code.
*
* @author
* Dan Poenaru <dan.poenaru@deptagency.com>
*
* 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=' . Html::escape($asurite);
$client = \Drupal::httpClient();
try {
$response = $client->get($elastic_query_url);
$data = $response->getBody()->getContents();
$elastic_data = 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) {
Error::logException(\Drupal::logger('asu_user'), $e);
}
return [];
}