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 my $pen = Fieldmapper::actor::user_standing_penalty->new;
30 $pen->$_($pen_obj->{$_}) for keys %$pen_obj;
32 if(defined $pen_obj->{id}) {
33 $e->delete_actor_user_standing_penalty($pen) or return $e->die_event;
36 $e->create_actor_user_standing_penalty($pen) or return $e->die_event;
43 # any penalties whose block_list has an item from @fatal_mask will be sorted
44 # into the fatal_penalties set. Others will be sorted into the info_penalties set
45 sub retrieve_penalties {
46 my($class, $e, $user_id, $context_org, @fatal_mask) = @_;
48 my $penalties = $e->search_actor_user_standing_penalty([
49 {usr => $user_id, org_unit => $U->get_org_ancestors($context_org)},
50 {flesh => 1, flesh_fields => {ausp => ['standing_penalty']}}
54 for my $p (@$penalties) {
56 if($p->standing_penalty->block_list) {
57 for my $m (@fatal_mask) {
58 if($p->standing_penalty->block_list =~ /$m/) {
59 push(@fatal, $p->standing_penalty->name);
65 push(@info, $p->standing_penalty->name) unless $pushed;
68 return {fatal_penalties => \@fatal, info_penalties => \@info};