/
OrcidSync.pm
105 lines (93 loc) · 3.83 KB
/
OrcidSync.pm
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package EPrints::Plugin::Event::OrcidSync;
our @ISA = qw( EPrints::Plugin::Event );
use strict;
use utf8;
use EPrints;
use EPrints::Plugin::Event;
use EPrints::ORCID::AdvanceUtils;
use JSON;
use Storable qw(dclone);
#add institution to employment list - requires /activities/update scope
sub update_employment
{
my ( $self, $user ) = @_;
#get user object for relevant details - check it exists and has appropriate fields
my $repo = $self->{repository};
unless (defined( $repo ) && defined( $user ))
{
$repo->log( "Repository or User object not defined" );
return EPrints::Const::HTTP_INTERNAL_SERVER_ERROR;
}
unless( $user->exists_and_set( "orcid" ) && $user->exists_and_set( "orcid_access_token" ) )
{
$repo->log( "Orcid id or authorisation code not set for user ". $user->get_value( "userid" ) );
return EPrints::Const::HTTP_INTERNAL_SERVER_ERROR;
}
my $user_type = $user->get_value( "usertype" );
my $affiliation = "employment";
if( defined $user_type && grep( /^$user_type$/, @{$repo->config( "orcid_support_advance", "education_user_types" )} ) )
{
$affiliation = "education";
}
#get details for the communication from config - check they exist
my $organisation = $repo->config( "plugins" )->{"Event::OrcidSync"}->{"params"}->{"affiliation"};
unless ( defined( $organisation ))
{
$repo->log( "Organization not defined correctly in configuration" );
return EPrints::Const::HTTP_INTERNAL_SERVER_ERROR;
}
#check if we have already created a profile on the ORCID record to determine whether to create or update the profile
#NOTE: Assuming we are only maintaining one affiliation record as we have no historical detail over role / deparmental changes
my $response = EPrints::ORCID::AdvanceUtils::read_orcid_record( $repo, $user, "/$affiliation" ."s" );
if( $response->is_success )
{
my $json = new JSON;
my $json_text = $json->utf8->decode($response->content);
my $institution = $repo->config( "plugins" )->{"Event::OrcidSync"}->{"params"}->{"affiliation"}; #get our institution from the config
# Add department
if ( $user->is_set( "dept" ) )
{
my $institution_temp = dclone $institution;
$institution_temp->{ "department-name" } = $user->get_value( "dept" );
$institution = \%{$institution_temp};
}
#check this institution isn't already in the orcid.org list of employments
my $add_institution = 1;
foreach my $employment ( @{$json_text->{"$affiliation-summary"}} )
{
my $orgid1 = $institution->{'organization'}->{'disambiguated-organization'}->{'disambiguated-organization-identifier'};
my $orgid2 = $employment->{'organization'}->{'disambiguated-organization'}->{'disambiguated-organization-identifier'};
if( $orgid1 eq $orgid2 )
{
$add_institution = 0;
}
}
#add the insitution if we still need to
if( $add_institution )
{
my $result = EPrints::ORCID::AdvanceUtils::write_orcid_record( $repo, $user, "POST", "/$affiliation", $institution );
if( $result->is_success )
{
return EPrints::Const::HTTP_OK;
}
else
{
#problem with the communication
$repo->log( "Response from ORCID:".$response->code." ".$response->content );
return EPrints::Const::HTTP_INTERNAL_SERVER_ERROR;
}
}
else
{
#no need to add institution
$repo->log( "[Event::OrcidSync::update_employment]Institution present in ORCID record. \n" );
return EPrints::Const::HTTP_OK;
}
}
else
{
#problem with the communication
$repo->log( "Response from ORCID:".$response->code." ".$response->content );
return EPrints::Const::HTTP_INTERNAL_SERVER_ERROR;
}
}