upload fieldbook from manage phenotyping
[sgn.git] / cgi-bin / phenome / assign_owner.pl
blob8c8202505dfbc2ce4a879ea9ac30d859caf97fe6
1 use Modern::Perl;
2 use warnings;
4 use CXGN::Scrap::AjaxPage;
5 use CXGN::Login;
6 use CXGN::People::Person;
7 use CXGN::Feed;
8 use CatalystX::GlobalContext '$c';
10 my $dbh = $c->dbc->dbh;
12 my ( $login_person_id, $login_user_type ) =
13 CXGN::Login->new($dbh)->verify_session();
15 if ( $login_user_type eq 'curator' ) {
16 my $doc = CXGN::Scrap::AjaxPage->new();
17 $doc->send_http_header();
18 my ( $user_info, $object_type, $object_id, $sp_person_id ) =
19 $doc->get_encoded_arguments( "user_info", "object_type", "object_id",
20 "sp_person_id" );
22 #query for retreiving user information
23 if ($user_info) {
24 my $user_query = $dbh->prepare(
25 "SELECT sp_person_id, first_name, last_name, user_type FROM sgn_people.sp_person
26 WHERE (first_name ilike '%$user_info%' OR last_name ilike '%$user_info%')
27 ORDER BY last_name
30 $user_query->execute();
31 my ( $sp_person_id, $first_name, $last_name, $user_type ) =
32 $user_query->fetchrow_array();
33 my $users;
34 my @roles = ();
35 while ($first_name) {
36 my $person = CXGN::People::Person->new( $dbh, $sp_person_id );
37 @roles = $person->get_roles;
38 my $role_string = join ',', @roles;
39 $users .= "$sp_person_id*$last_name, $first_name [$role_string]|";
40 ( $sp_person_id, $first_name, $last_name, $user_type ) =
41 $user_query->fetchrow_array();
43 print "$users";
46 #setting the new object owner. Only curators can do this.
47 #if the user has a 'user' account it will be updated to a 'submitter' first
48 #executed when javascript sends object_type and object_id , and sp_person_id args
49 if ( $object_type && $object_id ) {
50 my $new_owner = CXGN::People::Person->new( $dbh, $sp_person_id );
52 eval {
54 #if the new owner is not a submitter, assign that role
55 if ( !$new_owner->has_role('submitter') ) {
56 $new_owner->add_role('submitter');
58 my $query;
59 if ( $object_type eq 'locus' ) {
60 $query = $dbh->prepare(
61 "INSERT INTO phenome.locus_owner (sp_person_id, locus_id, granted_by)
62 VALUES (?,?,?)"
64 $query->execute( $sp_person_id, $object_id, $login_person_id );
66 #if the current owner of the locus is a logged-in SGN curator, do an obsolete..
67 if ( $login_user_type eq 'curator' ) {
68 my $remove_curator_query =
69 "UPDATE phenome.locus_owner SET obsolete='t', modified_date= now()
70 WHERE locus_id=? AND sp_person_id IN (SELECT sp_person_id FROM sgn_people.sp_person_roles WHERE sp_role_id = (SELECT sp_role_id FROM sgn_people.sp_roles WHERE name = 'curator') )";
71 my $remove_curator_sth =
72 $dbh->prepare($remove_curator_query);
73 $remove_curator_sth->execute($object_id);
77 elsif ( $object_type eq 'individual' ) {
78 $query = $dbh->prepare(
79 "UPDATE phenome.individual SET sp_person_id= ?
80 WHERE indvidual_id= ?"
82 $query->execute( $sp_person_id, $object_id );
85 elsif ( $object_type eq 'stock' ) {
86 my $stock =
87 $c->dbic_schema( 'Bio::Chado::Schema', 'sgn_chado' )
88 ->resultset("Stock::Stock")
89 ->find( { stock_id => $object_id } );
90 $stock->create_stockprops(
91 { 'sp_person_id' => $sp_person_id },
93 'cv_name' => 'local',
94 autocreate => 1,
95 'allow_duplicate_values' => 1
100 else { exit(); }
103 if ($@) {
104 my $message = "assigning $object_type owner failed! $@";
105 warn $message;
106 return $message;
108 else {
109 my $subject =
110 "[New $object_type owner assigned] $object_type $object_id";
111 my $person = CXGN::People::Person->new( $dbh, $login_person_id );
112 my $user =
113 $person->get_first_name() . " " . $person->get_last_name();
114 my $user_link =
115 qq |solgenomics.net/solpeople/personal-info.pl?sp_person_id=$login_person_id|;
117 my %links = (
118 'locus' => qq |solgenomics.net/locus/$object_id/view/|,
119 'stock' => qq |solgenomics.net/stock/$object_id/view/|,
121 my $object_link = $links{$object_type};
122 my $owner = CXGN::People::Person->new( $dbh, $sp_person_id );
123 my $owner_name =
124 $owner->get_first_name() . " " . $owner->get_last_name();
125 my $owner_link =
126 qq |solgenomics.net/solpeople/personal-info.pl?sp_person_id=$sp_person_id|;
127 my $fdbk_body =
128 "curator $user ($user_link) has assigned a new owner ($owner_name, $owner_link) for $object_type $object_link \n ";
129 CXGN::Contact::send_email( $subject, $fdbk_body,
130 'sgn-db-curation@sgn.cornell.edu' );
131 CXGN::Feed::update_feed( $subject, $fdbk_body );