1 package OpenILS::Utils::Penalty;
2 use strict; use warnings;
5 use OpenSRF::EX qw(:try);
6 use OpenSRF::Utils::Cache;
7 use OpenSRF::Utils qw/:datetime/;
8 use OpenILS::Application::AppUtils;
9 use OpenSRF::Utils::Logger qw(:logger);
10 use OpenILS::Utils::CStoreEditor qw/:funcs/;
11 use OpenILS::Utils::Fieldmapper;
12 use OpenILS::Const qw/:const/;
13 my $U = "OpenILS::Application::AppUtils";
16 # calculate and update the well-known penalties
17 sub calculate_penalties {
18 my($class, $e, $user_id, $context_org) = @_;
20 my $penalties = $e->json_query({from => ['actor.calculate_system_penalties',$user_id, $context_org]});
22 for my $pen_obj (@$penalties) {
24 next if grep { # leave duplicate penalties in place
25 $_->{org_unit} == $pen_obj->{org_unit} and
26 $_->{standing_penalty} == $pen_obj->{standing_penalty} and
27 ($_->{id} || '') ne ($pen_obj->{id} || '') } @$penalties;
29 if(defined $pen_obj->{id}) {
30 $e->delete_actor_user_standing_penalty($pen_obj->{id})
31 or return $e->die_event;
34 my $newp = Fieldmapper::actor::user_standing_penalty->new;
35 $newp->$_($pen_obj->{$_}) for keys %$pen_obj;
36 $e->create_actor_user_standing_penalty($newp)
37 or return $e->die_event;
44 # any penalties whose block_list has an item from @fatal_mask will be sorted
45 # into the fatal_penalties set. Others will be sorted into the info_penalties set
46 sub retrieve_penalties {
47 my($class, $e, $user_id, $context_org, @fatal_mask) = @_;
49 my $penalties = $e->search_actor_user_standing_penalty([
50 {usr => $user_id, org_unit => $U->get_org_ancestors($context_org)},
51 {flesh => 1, flesh_fields => {ausp => ['standing_penalty']}}
55 for my $p (@$penalties) {
57 if($p->standing_penalty->block_list) {
58 for my $m (@fatal_mask) {
59 if($p->standing_penalty->block_list =~ /$m/) {
60 push(@fatal, $p->name);
66 push(@info, $p->name) unless $pushed;
69 return {fatal_penalties => \@fatal, info_penalties => \@info};