#!perl BEGIN { chdir('..'); } our ($conf, $dir, $lock_file, $booked_file, $static_file, $cancelled_file, $holidays_file, $info_file, $static_info_file); our ($booked, $static, $cancelled, $booked_x, $static_x, $cancelled_x, $holidays, $info, $info_x, $static_info, $static_info_x); our ($program, $submit_i, $file_i, $password_i, @month, @weekday); our (%index, @dates, @resources, $resource, $date_first, $date_last, $from, $to); our (%by_subj, %by_yr, %by_yr_subj); use strict; use warnings; use IO::File; use CGI ':standard'; use CGI::Carp 'fatalsToBrowser'; require "lib_pl"; require "lib_bookings_pl"; use Date; use File::Copy; use Data::Dumper; # debug hdr("Resource Usage Stats"); load_calendar_data(); $conf = read_data("bookings.conf"); $resource = param("resource") || "Library : Library"; param("resource", $resource); my $resource_changed = $resource ne param("resourceold"); build_index(); scan_dates(); scan_resources(); my $dropdown = dropdown({selected=>$resource}, "resource", \@resources); print qq{

resource $dropdown  from   to   $submit_i

}; count_by_subj_yr_and_both(); my $td = ""; print qq{$td\n}; print h3("by year level and subject"); print_stats(\%by_yr_subj); print qq{$td\n}; print h3(qq{by subject}); print_stats(\%by_subj); print qq{$td\n}; print h3(qq{by year level}); print_stats(\%by_yr); print qq{
\n}; print qq{
}; print hidden("resourceold", $resource); ftr(); sub print_stats { my ($hash) = @_; print "
\n";
	for my $k (sort keys(%$hash)) {
		my $v = $hash->{$k};
		print "$v\t$k\n";
	}
	print "
\n"; } sub scan_resources { my %res; for (@$info) { my ($date, $period, $res, $key, $value) = @$_; $res{$res} = 1; } @resources = sort keys %res; } sub build_index { for (@$info) { my ($date, $period, $res, $key, $value) = @$_; if ($res eq $resource) { $index{$date}{$period}{$key} = $value; } } } sub scan_dates { @dates = sort keys %index; ($date_first, $date_last) = @dates[0,-1]; $from = param("from") || $date_first; $to = param("to") || $date_last; if ($resource_changed || param("reset")) { $from = $date_first; $to = $date_last; } for ($from, $to) { $_ =~ tr/0-9/./cs; s/^\.*//; s/\.*$//; my ($y, $m, $d) = split /\./, $_; $_ = sprintf("%04d.%02d.%02d", $y, $m, $d); } param("from", $from); param("to", $to); } #sub get_all_resources { # for my $category (@{$conf->{resources}{__order}}) { # for my $res (@{$conf->{resources}{$category}{__order}}) { # push @resources, "$category : $res"; # } # } #} sub count_by_subj_yr_and_both { while (my ($date, $x) = each(%index)) { unless ($date ge $from and $date le $to) { next } while (my ($period, $y) = each(%$x)) { my $subj = $y->{subj} || ""; my $yr = $y->{yr} || ""; $yr = " $yr" while length($yr) < 2; $by_subj{$subj} ++; $by_yr{$yr} ++; $by_yr_subj{"$yr $subj"} ++; } } }