Merge pull request #5248 from solgenomics/topic/batch_update_trials
[sgn.git] / lib / CXGN / People / Roles.pm
blob5db06cd522c42f120dfd7f8eddef306013e7b32d
2 =head1 NAME
4 CXGN::People::Roles - helper class for people's roles
6 =head1 SYNOPSYS
8 my $person_roles = CXGN::Person::Roles->new( { bcs_schema => $schema } );
10 etc.
12 =head1 AUTHOR
14 Nicolas Morales <nm529@cornell.edu>
16 =head1 METHODS
18 =cut
20 package CXGN::People::Roles;
22 use Moose;
23 use Try::Tiny;
24 use SGN::Model::Cvterm;
25 use Data::Dumper;
26 use Text::Unidecode;
27 use List::MoreUtils qw /any /;
29 has 'bcs_schema' => (
30 isa => 'Bio::Chado::Schema',
31 is => 'rw',
34 sub add_sp_role {
35 my $self = shift;
36 my $name = shift;
37 my $dbh = $self->bcs_schema->storage->dbh;
39 my $q="SELECT sp_role_id FROM sgn_people.sp_roles where name=?;";
40 my $sth = $dbh->prepare($q);
41 $sth->execute($name);
42 my $count = $sth->rows;
43 if ($count > 0){
44 print STDERR "A role with that name already exists.\n";
45 return;
47 eval {
48 my $q="INSERT INTO sgn_people.sp_roles (name) VALUES (?);";
49 my $sth = $dbh->prepare($q);
50 $sth->execute($name);
52 if ($@) {
53 return "An error occurred while storing a new role. ($@)";
57 sub update_sp_role {
58 my $self = shift;
59 my $new_name = shift;
60 my $old_name = shift;
61 my $dbh = $self->bcs_schema->storage->dbh;
63 my $q="SELECT sp_role_id FROM sgn_people.sp_roles where name=?;";
64 my $sth = $dbh->prepare($q);
65 $sth->execute($old_name);
66 my $count = $sth->rows;
67 if ($count < 1){
68 print STDERR "No role with that name exists.\n";
69 return;
71 eval {
72 my $q="UPDATE sgn_people.sp_roles SET name = ? WHERE name = ?;";
73 my $sth = $dbh->prepare($q);
74 $sth->execute($new_name,$old_name);
76 if ($@) {
77 return "An error occurred while updating an existing role. ($@)";
81 sub get_breeding_program_roles {
82 my $self = shift;
83 my $ascii_chars = shift;
84 my $dbh = $self->bcs_schema->storage->dbh;
85 my @breeding_program_roles;
86 my $q="SELECT username, sp_person_id, name, censor FROM sgn_people.sp_person
87 JOIN sgn_people.sp_person_roles using(sp_person_id)
88 JOIN sgn_people.sp_roles using(sp_role_id)
89 where disabled IS NULL and sp_person.censor = 0";
90 my $sth = $dbh->prepare($q);
91 $sth->execute();
92 while (my ($username, $sp_person_id, $sp_role, $censor) = $sth->fetchrow_array ) {
93 if ($ascii_chars) {
94 $username = unidecode($username);
96 push(@breeding_program_roles, [$username, $sp_person_id, $sp_role, $censor] );
99 print STDERR Dumper \@breeding_program_roles;
100 return \@breeding_program_roles;
103 sub add_sp_person_role {
104 my $self = shift;
105 my $sp_person_id = shift;
106 my $sp_role_id = shift;
107 my $dbh = $self->bcs_schema->storage->dbh;
108 my $q = "INSERT INTO sgn_people.sp_person_roles (sp_person_id, sp_role_id) VALUES (?,?);";
109 my $sth = $dbh->prepare($q);
110 $sth->execute($sp_person_id, $sp_role_id);
111 return;
114 sub get_sp_persons {
115 my $self = shift;
116 my $dbh = $self->bcs_schema->storage->dbh;
117 my @sp_persons;
118 my $q="SELECT username, sp_person_id FROM sgn_people.sp_person WHERE disabled IS NULL and censor = 0 ORDER BY username ASC;";
119 my $sth = $dbh->prepare($q);
120 $sth->execute();
121 while (my ($username, $sp_person_id) = $sth->fetchrow_array ) {
122 push(@sp_persons, [$username, $sp_person_id] );
124 return \@sp_persons;
127 sub get_sp_roles {
128 my $self = shift;
129 my $dbh = $self->bcs_schema->storage->dbh;
130 my @sp_roles;
131 my $q="SELECT name, sp_role_id FROM sgn_people.sp_roles;";
132 my $sth = $dbh->prepare($q);
133 $sth->execute();
134 while (my ($name, $sp_role_id) = $sth->fetchrow_array ) {
135 push(@sp_roles, [$name, $sp_role_id] );
137 return \@sp_roles;
140 sub check_sp_roles {
141 my $self = shift;
142 my $user_roles = shift;
143 my $program_name = shift;
144 my %check_roles;
145 if (!any { $_ eq "curator" || $_ eq "submitter" } (@$user_roles)){
146 $check_roles{'invalid_role'} = 1;
149 my %has_roles = ();
150 map { $has_roles{$_} = 1; } @$user_roles;
152 if (! ( (exists($has_roles{$program_name}) && exists($has_roles{submitter})) || exists($has_roles{curator}))) {
153 $check_roles{'invalid_program'} = 1;
156 return \%check_roles;