Skip to content

Commit

Permalink
Fix Automatic Time extension #148
Browse files Browse the repository at this point in the history
  • Loading branch information
kylemhall committed May 4, 2020
1 parent 4f799fa commit 616e9a8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
18 changes: 15 additions & 3 deletions lib/Catalyst/Plugin/LibkiSetting.pm
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ Check the time and the user, return the available time if possible.

sub check_login {
my($c,$client,$user) = @_;
my $minutes_until_closing = Libki::Hours::minutes_until_closing( $c,$client->location );
my $minutes_until_closing = Libki::Hours::minutes_until_closing({ c => $c, location => $client->location });
my $timeout = $c->setting('ReservationTimeout') ? $c->setting('ReservationTimeout') : 15 ;
my %result = ('error' => 0, 'detail' => 0,'minutes' => 0, 'reservation' => undef );
my $time_to_reservation = 0;
Expand Down Expand Up @@ -357,7 +357,13 @@ sub check_reservation
my %result = ('error' => 0, 'detail' => 0, 'minutes' => 0,'allotment' => 0, 'end_time' => $parser->parse_datetime($begin_time));
my $datetime = $parser->parse_datetime($begin_time);
my @array;
my $minutes_to_closing = Libki::Hours::minutes_until_closing( $c,$client->location,$parser->parse_datetime($begin_time) );
my $minutes_to_closing = Libki::Hours::minutes_until_closing(
{
c => $c,
location => $client->location,
datetime => $parser->parse_datetime($begin_time),
}
);
my ( $minutes_left, $minutes ) = ( 0, 0);

#1. Check to see if the time has been past
Expand Down Expand Up @@ -493,7 +499,13 @@ sub get_time_list {
my $closehour = 23;
my $closeminute = 59;

my $minutes_to_closing = Libki::Hours::minutes_until_closing( $c,$client->location(),$parser->parse_datetime("$date $openhour:$openminute") );
my $minutes_to_closing = Libki::Hours::minutes_until_closing(
{
c => $c,
location => $client->location(),
datetime => $parser->parse_datetime("$date $openhour:$openminute"),
}
);
if ($minutes_to_closing) {
my $closetime = $opentime + $minutes_to_closing * 60;
$closehour = strftime("%H",localtime($closetime));
Expand Down
2 changes: 1 addition & 1 deletion lib/Libki/Controller/API/Client/v1_0.pm
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ sub index : Path : Args(0) {
}
);

my $minutes_until_closing = Libki::Hours::minutes_until_closing( $c, $client_location );
my $minutes_until_closing = Libki::Hours::minutes_until_closing({ c => $c, location => $client_location });

#TODO: Move this to a unified sub, see TODO below
# Get advanced rule if there is one
Expand Down
7 changes: 6 additions & 1 deletion lib/Libki/Hours.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ the return value will be a negative number of minutes *since* closing.
=cut

sub minutes_until_closing {
my ($c, $location_code, $datetime, $instance) = @_;
my ($params) = @_;

my $c = $params->{c};
my $location_code = $params->{location};
my $datetime = $params->{datetime};
my $instance = $params->{instance};

$instance ||= $c->instance;

Expand Down
34 changes: 22 additions & 12 deletions script/cronjobs/libki.pl
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,17 @@
q{
SELECT users.*,
sessions.*,
clients.location,
AutomaticTimeExtensionAt.value AS 'AutomaticTimeExtensionAt',
AutomaticTimeExtensionLength.value AS 'AutomaticTimeExtensionLength',
AutomaticTimeExtensionUnless.value AS 'AutomaticTimeExtensionUnless',
AutomaticTimeExtensionUseAllotment.value AS 'AutomaticTimeExtensionUseAllotment',
Count(any_reserved.instance) AS 'AnyReservedCount',
Count(this_reserved.client_id) AS 'ThisReservedCount'
FROM sessions
LEFT JOIN clients
ON ( clients.instance = sessions.instance
AND clients.id = sessions.client_id )
LEFT JOIN users
ON ( users.instance = sessions.instance
AND users.id = sessions.user_id )
Expand Down Expand Up @@ -150,7 +154,13 @@
# If we are nearing closing time, we need to only add minutes up to the cloasing time
# TODO We could possibly integrate this into the main query, or at least speed it up with raw SQL
$all_minutes_until_closing->{ $s->{instance} }->{ $s->{location} }
||= Libki::Hours::minutes_until_closing( $c, $s->{location}, $s->{instance} );
||= Libki::Hours::minutes_until_closing(
{
c => $c,
location => $s->{location},
instance => $s->{instance}
}
);

my $minutes_until_closing = $all_minutes_until_closing->{ $s->{instance} }->{ $s->{location} };

Expand All @@ -171,17 +181,17 @@
}
}

$message_rs->create(
{
instance => $s->{instance},
user_id => $s->{id},
content => $c->loc(
"Your session time has been automatically extended by $minutes_to_add_to_session minutes.",
"Your session time has been automatically extended by $minutes_to_add_to_session minutes.",
$minutes_to_add_to_session
),
}
);
$message_rs->create(
{
instance => $s->{instance},
user_id => $s->{id},
content => $c->loc(
"Your session time has been automatically extended by $minutes_to_add_to_session minutes.",
"Your session time has been automatically extended by $minutes_to_add_to_session minutes.",
$minutes_to_add_to_session
),
}
);

## Now we can store the changes
my $minutes_to_add_to_daily_allotment = $s->{AutomaticTimeExtensionUseAllotment} eq 'yes' ? 0 : $minutes_to_add_to_session;
Expand Down

0 comments on commit 616e9a8

Please sign in to comment.