sort totals as first column
[koha-bibliografija] / html.pl
diff --git a/html.pl b/html.pl
index 8744a7c..525e774 100755 (executable)
--- a/html.pl
+++ b/html.pl
@@ -11,8 +11,7 @@ use autodie;
 use locale;
 use Text::Unaccent;
 use Carp qw(confess);
-use Storable;
-
+use utf8;
 
 use lib '/srv/koha_ffzg';
 use C4::Context;
@@ -24,14 +23,27 @@ my $dbh = C4::Context->dbh;
 sub debug {
        my ($title, $data) = @_;
        print "# $title ",dump($data), $/ if $ENV{DEBUG};
-       store $data, "storable/$title";
 }
 
 my $xslfilename = 'compact.xsl';
 
+my $azvo_group_title = {
+'znanstveno nastavni' => qr/(profesor|docent|znanstveni)/i,
+'lektori i predavači' => qr/(lektor|predavač)/i,
+'asistenti i novaci' => qr/(asistent|novak)/i,
+};
+
+my $department_groups = {
+'AAA_humanističke'            => qr/(anglistiku|arheologiju|antropologiju|filozofiju|fonetiku|germanistiku|hungarologiju|indologiju|slavenske|filologiju|komparativnu|kroatistiku|lingvistiku|povijest|romanistiku|talijanistiku)/i,
+'AAB_društvene'                       => qr/(informacijske|pedagogiju|psihologiju|sociologiju)/i,
+};
+
 my $auth_header;
 my $auth_department;
+my $auth_group;
 my @authors;
+my $department_in_sum;
+my $department_in_group;
 
 my $skip;
 
@@ -39,7 +51,8 @@ my $sth_auth = $dbh->prepare(q{
 select
        authid,
        ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name,
-       ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="a"]') as department
+       ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="a"]') as department,
+       ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="i"]') as academic_title
 from auth_header
 });
 
@@ -52,13 +65,44 @@ while( my $row = $sth_auth->fetchrow_hashref ) {
        $auth_header->{ $row->{authid} } = $row->{full_name};
        $row->{department} =~ s/, Filozofski fakultet u Zagrebu.*$//;
        $row->{department} =~ s/^.+\.\s*//;
-#      warn dump( $row );
+       $row->{department} =~ s/\s+$//s;
+       my $group;
+       foreach my $title ( keys %$azvo_group_title ) {
+               if ( $row->{academic_title} =~ $azvo_group_title->{$title} ) {
+                       $group = $title;
+                       last;
+               }
+       }
+       if ( $group ) {
+               $row->{academic_group} = $group;
+               $auth_group->{ $row->{authid} } = $group;
+               $skip->{group_stat}->{$group}++;
+       } else {
+               push @{ $skip->{no_academic_group} }, $row;
+       }
+
+#      warn "# ", dump( $row );
        push @{ $auth_department->{ $row->{department} } }, $row->{authid};
        push @authors, $row;
+       $department_in_sum->{ $row->{department} }++;
+       foreach my $name ( keys %$department_groups ) {
+               my $regex = $department_groups->{$name};
+               if ( $row->{department} =~ $regex ) {
+                       $department_in_group->{ $row->{department} } = $name;
+                       last;
+               }
+       }
+}
+
+debug 'department_in_group' => $department_in_group;
 
+foreach my $department ( keys %$department_in_sum ) {
+       $department_in_sum->{$department} = 0 unless $department =~ m/(centar|croaticum|katedra|odsjek)/i;
 }
 
 debug 'auth_department' => $auth_department;
+debug 'auth_group' => $auth_group;
+debug 'department_in_sum' => $department_in_sum;
 
 
 my $authors;
@@ -153,6 +197,7 @@ while( my $row = $sth_select_authors->fetchrow_hashref ) {
        my $extract = {
                '008' => undef,
                '100' => '9',
+               '680' => 'i',
                '700' => '(9|4)',
                '942' => '(t|r|v)'
        };
@@ -283,7 +328,7 @@ sub html_title {
 }
 
 sub html_end {
-       return qq|</body>\n</html\n|;
+       return qq|</body>\n</html>\n|;
 }
 
 mkdir 'html' unless -d 'html';
@@ -359,6 +404,12 @@ foreach my $department ( sort keys %$auth_department ) {
                push @categories,  keys %{ $authors->{$authid}->{sec} };
                foreach my $category ( sort @categories ) {
                        push @{ $department_category_author->{$department}->{$category} }, $authid;
+                       push @{ $department_category_author->{'AAZ_ukupno'}->{$category} }, $authid if $department_in_sum->{$department};
+                       if ( my $group = $department_in_group->{ $department } ) {
+                               push @{ $department_category_author->{$group}->{$category} }, $authid;
+                       } else {
+                               $skip->{'department_not_in_group'}->{ $department }++;
+                       }
                }
        }
 }
@@ -435,27 +486,31 @@ print $dep_fh qq|</ul>\n|, html_end;
 close($dep_fh);
 rename 'html/departments/index.new', 'html/departments/index.html';
 
-my $azvo_stat_authors;
 my $azvo_stat_biblio;
 
 foreach my $department ( sort keys %$department_category_author ) {
        foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
                foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
+                       my $group = $auth_group->{$authid};
+                       if ( ! $group ) {
+                               push @{ $skip->{no_auth_group} }, $authid;
+                               next;
+                       }
                        foreach my $type ( keys %{ $authors->{$authid} } ) {
                                next unless exists $authors->{$authid}->{$type}->{$category};
-                               $azvo_stat_authors->{ $department }->{ $category }->{ $type } += $#{ $authors->{$authid}->{$type}->{$category} } + 1;
-                               push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } },  @{ $authors->{$authid}->{$type}->{$category} };
+                               push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{$group} },  @{ $authors->{$authid}->{$type}->{$category} };
+                               push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{''} },  @{ $authors->{$authid}->{$type}->{$category} };
                        }
                }
                foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
-                               my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } };
-#                              $azvo_stat_biblio->{ $department }->{ $category }->{ $type } = $#biblios + 1;
-                               $azvo_stat_biblio->{ $department }->{ $category }->{ $type } = [ @biblios ];
+                       foreach my $group ( keys %{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } } ) {
+                               my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } };
+                               $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } = [ @biblios ];
+                       }
                }
        }
 }
 
-debug 'azvo_stat_authors' => $azvo_stat_authors;
 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
 
 my @report_lines;
@@ -463,15 +518,15 @@ my @report_labels;
 
 my $label;
 my $sub_labels;
-open(my $report, '<', 'AZVO.txt');
+open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
 while( <$report> ) {
        chomp;
-       if ( /^(\w+)\t+(.+)/ ) {
+       if ( /^([^\t]+)\t+(.+)/ ) {
                $label = $1;
                push @report_labels, $label;
                my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
                push @report_lines, [ $label, @$type ];
-       } elsif ( /^\t+(\S+):\t+(\d+)(\w*)\t*(.*)$/ ) {
+       } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
                push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
                my $sub_label = $1;
                pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
@@ -481,40 +536,41 @@ while( <$report> ) {
        }
 }
 
-warn "# report_lines = ", dump( @report_lines );
-warn "# sub_labels = ", dump( $sub_labels );
-warn "# report_labels = ", dump( @report_labels );
+debug 'report_lines', \@report_lines;
+debug 'sub_labels', $sub_labels;
+debug 'report_labels', \@report_labels;
 
-my @departments = sort keys %$azvo_stat_biblio;
+my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );
+
+debug 'departments' => \@departments;
 
 my $department2col;
 $department2col->{ $departments[$_] } = $_ foreach ( 0 .. $#departments );
 my $label2row;
 $label2row->{ $report_labels[$_] } = $_ foreach ( 0 .. $#report_labels );
 
-
 my $table;
 
 sub table_count {
        my $label = shift @_;
        my $department = shift @_;
+       my $group = shift @_;
        my @biblionumbers = @_;
        my $unique;
        $unique->{$_}++ foreach @biblionumbers;
-       $table->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar keys %$unique;
+       $table->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar keys %$unique;
 }
 
-foreach my $department ( keys %$azvo_stat_biblio ) {
-       my $dep_id = $#departments;
+foreach my $group ( '', keys %$azvo_group_title ) {
+
+foreach my $department ( @departments ) {
        foreach my $line ( @report_lines ) {
                my $label = $line->[0];
-               warn "# line [$label] = ", dump( $line );
                my @biblionumbers;
                foreach ( 1 .. $#$line ) {
                        my ( $category, $type ) = @{ $line->[ $_ ] };
-                       my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type};
-                       warn "## $_ $category / $type = ", $#$b + 1, " ", dump( $b );
-                       push @biblionumbers, @$b;
+                       my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type}->{$group};
+                       push @biblionumbers, @$b if $b;
                }
                if ( $sub_labels->{$label} ) {
                        my $sub_stats;
@@ -541,33 +597,42 @@ foreach my $department ( keys %$azvo_stat_biblio ) {
                        }
                        foreach my $sub_label ( keys %$sub_stats ) {
                                my $full_label = $label . $sub_label;
-                               table_count $full_label, $department, @{ $sub_stats->{$sub_label} };
+                               table_count $full_label, $department, $group, @{ $sub_stats->{$sub_label} };
                        }
                } else {
-                       table_count $label, $department, @biblionumbers;
+                       table_count $label, $department, $group, @biblionumbers;
                }
        }
 }
 
-warn "# table ", dump( $table );
+} # group
+
+debug 'table', $table;
 
 open(my $fh, '>:encoding(utf-8)', 'html/azvo.new');
 
 print $fh html_title('AZVO tablica');
 
-print $fh "<table border=1>\n";
-print $fh "<tr><th></th>";
-print $fh "<th>$_</th>" foreach @departments;
-print $fh "</tr>\n";
+foreach my $group ( keys %$table ) {
 
-foreach my $row ( 0 .. $#$table ) {
-       print $fh "<tr><th>", $report_labels[$row], "</th>";
-       print $fh "<td>", $table->[ $row ]->[ $_ ] || '', "</td>" foreach 0 .. $#departments;
-       print $fh "</tr>";
-}
+               print $fh "<h1>$group</h1>" if $group;
+
+               print $fh "<table border=1>\n";
+               print $fh "<tr><th></th>";
+               print $fh "<th>$_</th>" foreach @departments;
+               print $fh "</tr>\n";
+
+               foreach my $row ( 0 .. $#{ $table->{$group} } ) {
+                       print $fh "<tr><th>", $report_labels[$row], "</th>\n";
+                       print $fh " <td>", $table->{$group}->[ $row ]->[ $_ ] || '', "</td>\n" foreach 0 .. $#departments;
+                       print $fh "</tr>\n";
+               }
+
+               print $fh "</table>\n";
 
-print $fh "</table>\n", html_end;
+} # group
 
+print $fh html_end;
 close($fh);
 rename 'html/azvo.new', 'html/azvo.html';