[% PROCESS "_dbiconnect.htm" %]
[% PROCESS "_security.htm" %]
[% PROCESS "_instructorsonly.htm" %]
[% # Set the wrapper to use
IF params.print == 1;
mywrapper = "report_print.pdf";
myformat = "_standard_wrapper_user_list_print.htm";
ELSE;
mywrapper = "_standard_wrapper_outer.htm";
myformat = "_standard_wrapper_user_list.htm";
END %]
[% WRAPPER $mywrapper %]
[% title="Staff Rota";
thispage = template.name | remove(".htm");
return.page= "reports.htm";
page_params = "?start_hidden=" _ params.start_hidden
_ "&finish_hidden=" _ params.finish_hidden
_ "&member_type=" _ params.member_type;
link1.page = "rota.htm";
link1.params = page_params;
link1.text = "ROTA";
%]
Staff Rota
[% PERL; %]
my $dbi = $stash->get("DBI"); #we'll need these later
my $dbh = $dbi->{_DBH};
my $CGI = $stash->get("CGI");
my $curdate = $CGI->param('curdate');
my $thisMonday;
my $site_id = $stash->get("site_id");
if ($curdate) { #if curdate, we have a posted date, we need to check to see if anything needs updating
#load the last values from the DB so we know if they cascade from previous, or really need updating
#working/interaction hours first
my $qLastValues = $dbh->prepare("
SELECT
rs.user_id,
LEFT(
GROUP_CONCAT(rs.working_hours ORDER BY rs.date DESC),
LOCATE(',', CONCAT(GROUP_CONCAT(rs.working_hours ORDER BY rs.date DESC), ',')) -1
) as working_hours,
LEFT(
GROUP_CONCAT(rs.interaction_hours ORDER BY rs.date DESC),
LOCATE(',', CONCAT(GROUP_CONCAT(rs.interaction_hours ORDER BY rs.date DESC), ',')) -1
) as interaction_hours
FROM rota_standard rs
WHERE
rs.site_id = ? AND rs.date < '$curdate'
GROUP BY rs.user_id
ORDER BY rs.user_id ASC
"); #">
$qLastValues->execute($site_id);
my %standard_working;
my %standard_interaction;
while (my $r = $qLastValues->fetchrow_hashref()) {
$standard_working{$r->{user_id}} = $r->{working_hours};
$standard_interaction{$r->{user_id}} = $r->{interaction_hours};
}
#target next
my $qLastTarget = $dbh->prepare("
SELECT
rl.user_id,
LEFT(
GROUP_CONCAT(rl.target_interactions ORDER BY rl.date DESC),
LOCATE(',', CONCAT(GROUP_CONCAT(rl.target_interactions ORDER BY rl.date DESC), ',')) -1
) as last_target_interactions
FROM rota rl
WHERE
rl.site_id = ? AND
rl.date < ?
GROUP BY rl.site_id, rl.user_id
"); #">
$qLastTarget->execute($site_id, $curdate);
my %target;
while (my $r = $qLastTarget->fetchrow_hashref()) {
$target{$r->{user_id}} = $r->{last_target_interactions};
}
#put all the posted values in a nicely structured array (if only perl handled forms like php)
my $values;
for (keys %{$CGI->params}) {
if (/${site_id}_(\d+)_(\S+)/) {
$values->{$1}{$2} = $CGI->param($_);
$values->{$1}{$2} = undef
if ($values->{$1}{$2} eq ''); #if nothing was entered, it's null, not ''
}
}
#prepare the queries we will be using to keep the database up to date
my $qData = $dbh->prepare("
INSERT INTO rota (site_id, user_id, date, working_hours, interaction_hours, target_interactions)
VALUES (?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY
UPDATE working_hours = VALUES(working_hours), interaction_hours = VALUES(interaction_hours), target_interactions = VALUES(target_interactions)
");
my $qDataDel = $dbh->prepare("
DELETE FROM rota
WHERE site_id = ? AND user_id = ? AND date = ?
");
my $qStandard = $dbh->prepare("
INSERT INTO rota_standard (site_id, user_id, date, working_hours, interaction_hours)
VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY
UPDATE working_hours = VALUES(working_hours), interaction_hours = VALUES(interaction_hours)
");
my $qStandardDel = $dbh->prepare("
DELETE FROM rota_standard
WHERE site_id = ? AND user_id = ? AND date = ?
");
for my $uid (keys %{$values}) { #loop through previously created datastructure by user id
#if our new value is the same as our old value, mark it as null
$values->{$uid}{'working_hours'} = undef
if ($values->{$uid}{'standard_working_hours'} == $values->{$uid}{'working_hours'});
$values->{$uid}{'interaction_hours'} = undef
if ($values->{$uid}{'standard_interaction_hours'} == $values->{$uid}{'interaction_hours'});
$values->{$uid}{'target_interactions'} = undef
if ($values->{$uid}{'target_interactions'} == $target{$uid});
$values->{$uid}{'standard_working_hours'} = undef
if ($values->{$uid}{'standard_working_hours'} == $standard_working{$uid});
$values->{$uid}{'standard_interaction_hours'} = undef
if ($values->{$uid}{'standard_interaction_hours'} == $standard_interaction{$uid});
#if anything is defined (not null), then we need an update query, otherwise delete
if (defined $values->{$uid}{'working_hours'} || defined $values->{$uid}{'interaction_hours'} || defined $values->{$uid}{'target_interactions'}) {
$qData->execute($site_id, $uid, $curdate, $values->{$uid}{'working_hours'}, $values->{$uid}{'interaction_hours'}, $values->{$uid}{'target_interactions'});
} else {
$qDataDel->execute($site_id, $uid, $curdate);
}
if (defined $values->{$uid}{'standard_working_hours'} || $values->{$uid}{'standard_interaction_hours'}) {
$qStandard->execute($site_id, $uid, $curdate, $values->{$uid}{'standard_working_hours'}, $values->{$uid}{'standard_interaction_hours'});
} else {
$qStandardDel->execute($site_id, $uid, $curdate);
}
}
#if we posted something with the name "prev" we want to move back a week, "next" next week, otherwise, stay the same
if ($CGI->param("prev")) {
my $qmonday = $dbh->prepare("SELECT DATE_FORMAT(DATE_SUB('".$curdate."', INTERVAL 7 DAY), '%Y-%m-%d') as last_monday");
$qmonday->execute();
$thisMonday = ($qmonday->fetchrow_array())[0];
} elsif ($CGI->param("next")) {
my $qmonday = $dbh->prepare("SELECT DATE_FORMAT(DATE_ADD('".$curdate."', INTERVAL 7 DAY), '%Y-%m-%d') as last_monday");
$qmonday->execute();
$thisMonday = ($qmonday->fetchrow_array())[0];
} else {
$thisMonday = $curdate;
}
} else {
#it's our first time, find last Monday
my $wday = (localtime)[6]; #current weekday, 0 = sunday, 6 = saturday
$wday = 7 if ($wday == 0); #we want our weeks to start on monday
--$wday;
my $qmonday = $dbh->prepare("SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL $wday DAY), '%Y-%m-%d') as last_monday");
$qmonday->execute();
$thisMonday = ($qmonday->fetchrow_array())[0];
}
#now we've got $thisMonday and we've updated all we need to, get the last "standard" data for cascade
my $qLastValues = $dbh->prepare("
SELECT
rs.user_id,
LEFT(
GROUP_CONCAT(rs.working_hours ORDER BY rs.date DESC),
LOCATE(',', CONCAT(GROUP_CONCAT(rs.working_hours ORDER BY rs.date DESC), ',')) -1
) as working_hours,
LEFT(
GROUP_CONCAT(rs.interaction_hours ORDER BY rs.date DESC),
LOCATE(',', CONCAT(GROUP_CONCAT(rs.interaction_hours ORDER BY rs.date DESC), ',')) -1
) as interaction_hours
FROM rota_standard rs
WHERE
rs.site_id = ? AND rs.date <= '$thisMonday'
GROUP BY rs.user_id
ORDER BY rs.user_id ASC
"); #"> and again
$qLastValues->execute($site_id);
my %standard_working;
my %standard_interaction;
while (my $r = $qLastValues->fetchrow_hashref()) {
$standard_working{$r->{user_id}} = $r->{working_hours};
$standard_interaction{$r->{user_id}} = $r->{interaction_hours};
}
#get the latest non-standard data
my $qfetch = $dbh->prepare("
SELECT
u.site_id, u.user_id, CONCAT(ud.fname, ' ', ud.sname) as name, ut.name as type,
r.working_hours, r.interaction_hours, r.target_interactions,
r.date,
LEFT(
GROUP_CONCAT(rl.target_interactions ORDER BY rl.date DESC),
LOCATE(',', CONCAT(GROUP_CONCAT(rl.target_interactions ORDER BY rl.date DESC), ',')) -1
) as last_target_interactions
FROM users u
LEFT JOIN user_details ud ON
u.site_id = ud.site_id AND
u.user_id = ud.user_id
LEFT JOIN user_types ut ON
ut.type_id = u.type_id
LEFT JOIN rota r ON
r.site_id = u.site_id AND
r.user_id = u.user_id AND
r.date = ?
LEFT JOIN rota rl ON
rl.site_id = u.site_id AND
rl.user_id = u.user_id AND
rl.date <= ?
WHERE
u.type_id IN(2,3) AND
u.site_id = ? AND
u.user_id NOT IN (10002, 10042)
GROUP BY u.site_id, u.user_id
ORDER BY ud.sname, ud.fname
"); #">
$qfetch->execute($thisMonday, $thisMonday, $site_id);
print "
[% END # Close WRAPPER %]