#!/usr/bin/perl # Copyright 2001-2008 Leslie Richardson # This file is part of Open Admin for Schools. # Open Admin for Schools is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. my %lex = ('Admin Attendance Entry' => 'Admin Attendance Entry', 'Error' => 'Error', 'Main' => 'Main', 'Attendance' => 'Attendance', 'Blank=All' => 'Blank=All', 'Continue' => 'Continue', 'Attendance Date' => 'Attendance Date', 'Grade' => 'Grade', 'Homeroom' => 'Homeroom', 'Attendance Entry' => 'Attendance Entry', 'Subject' => 'Subject', 'Enter Grade' => 'Enter Grade', 'Max Periods per Day not found for' => 'Max Periods per Day not found for', 'Submit Attendance' => 'Submit Attendance', 'Reason' => 'Reason', 'Student' => 'Student', 'Per' => 'Per', 'Attendance Date' => 'Attendance Date', 'Late' => 'Late', 'Separate Multiple Grades with a space' => 'Separate Multiple Grades with a space', 'Term not found' => 'Term not found', 'Student Not Found' => 'Student Not Found', 'Period(s)' => 'Period(s)', 'Clear Prev Attendance' => 'Clear Prev Attendance', 'Period' => 'Period', 'Reason' => 'Reason', 'Late' => 'Late', 'None' => 'None', ); my $self = 'attaddall.pl'; use DBI; use CGI; my $q = new CGI; print $q->header; my %arr = $q->Vars; eval require "../etc/admin.conf"; if ( $@ ) { print $lex{Error}. " $@
\n"; die $lex{Error}. " $@\n"; } my $dsn = "DBI:$dbtype:dbname=$dbase"; my $dbh = DBI->connect($dsn,$user,$password); # Set Date my @tim = localtime(time); my $year = @tim[5] + 1900; my $month = @tim[4] + 1; my $day = @tim[3]; my $currdate = "$year-$month-$day"; my $currdate1 = "$month[$month] $day, $year"; print "$doctype\n". $lex{'Admin Attendance Entry'}. "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "$chartype\n\n"; print "[ ". $lex{Main}. " |\n"; print " ". $lex{Attendance}. " ] $currdate1\n"; print "

". $lex{'Admin Attendance Entry'}. "

\n"; if ( $arr{grade} ) { doGradeAttendance(); } elsif ( $arr{homeroom} ) { doHomeroomAttendance(); } elsif ( $arr{subject} ) { doSubjectAttendance(); } elsif ( $arr{writeflag} ) { writeRecords(); } else { showStartPage(); } #---------------- sub showStartPage { #---------------- # Find all teachers who do attendance and also record homerooms for them my $sth = $dbh->prepare("select distinct userid, homeroom from staff where doatt = 'Y' order by lastname, firstname"); $sth->execute; if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } while ( my ( $userid, $homeroom ) = $sth->fetchrow ) { if ( $duplicates{$userid} ) { next; } if ( $homeroom ) { push @homeroomteachers, $userid; } push @subjectteachers, $userid; $duplicates{$userid} = 1; # watch for duplicates } # Grade Entry print "
"; print "\n"; print "\n"; print "\n"; print "\n"; # spacer # Get Teacher Name, Homeroom Query my $sth1 = $dbh->prepare("select lastname, firstname, homeroom from staff where userid = ?"); # Homeroom Entry print "\n"; my $count = 0; foreach my $userid ( @homeroomteachers ) { # Get Name and Homeroom $sth1->execute( $userid ); my ( $lastname, $firstname, $homeroom ) = $sth1->fetchrow; if ( $count == 0 ) { print ""; } # Do Form. print "\n"; $count++; if ( $count % 3 == 0 ) { print "\n\n"; } } # Now clean up... my $remaining = 3 - ( $count % 3); for ( 1.. $remaining ) { print ""; } print "\n"; print "\n"; # put in spacer # End of Homeroom Entry # Now Start Subject Attendance Entry $count = 0; print "\n\n\n"; print ""; foreach my $userid ( @subjectteachers ) { # Get Name and Homeroom $sth1->execute( $userid ); my ( $lastname, $firstname, $homeroom ) = $sth1->fetchrow; # Do Form. print "\n"; $count++; if ( $count % 3 == 0 ) { print "\n\n"; } } # Now clean up... my $remaining = 3 - ( $count % 3); for ( 1.. $remaining ) { print ""; } print "\n"; print "
". $lex{Grade}. q{ }. $lex{'Attendance Entry'}. "
"; print "
\n"; print "\n"; print "\n"; print $lex{'Blank=All'}. q{, }. $lex{'Separate Multiple Grades with a space'}; print "
 
". $lex{'Homeroom'}. q{ }. $lex{'Attendance Entry'}; print "
\n"; print "\n"; print "\n"; print "
 
". $lex{Subject}. q{ }. $lex{'Attendance Entry'}. "
\n"; print "\n"; print "\n"; print "
\n\n"; # End of Subject Attendance Entry print "\n"; exit; } #------------------ sub doGradeAttendance { #------------------ # foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } # Grades may be multiple space separated values. my @grades = split /\s/, $arr{grade}; # Start of Form print "
\n"; print "
\n"; print "\n"; # show Date print "
". $lex{'Attendance Date'}; print " \n"; print "\n"; print "
\n"; # Select by grade my $sth = $dbh->prepare("select lastname, firstname, studnum from student where grade = ? order by lastname, firstname"); # Loop through all grades foreach my $grade ( @grades ) { $sth->execute( $grade ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my $maxppd; if ( $g_ppd{$grade} ) { $maxppd = $g_ppd{$grade}; } else { print $lex{'Max Periods per Day not found for'}. q{ }. $lex{Grade}. " $grade\n"; print "\n"; die; } print "

". $lex{Grade}. " $grade

\n"; print "\n"; print "\n"; print "\n"; while (my ($lastname, $firstname, $studnum) = $sth->fetchrow){ print "\n"; for my $p ( 1..$maxppd ) { print "\n"; } print "\n\n"; } print "\n"; print "
\n"; print "
". $lex{Student}. ""; for my $p ( 1..$maxppd ) { print $lex{Per}. " $p"; } print $lex{Reason}. "". $lex{Late}. "
$lastname, $firstname"; print "
\n"; print "
\n"; } # Outer Loop here for each grade. print "\n"; print "
\n"; exit; } #----------------------- sub doHomeroomAttendance { #----------------------- #foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } my $userid = $arr{homeroom}; delete $arr{homeroom}; # Get homeroom for this userid my $sth = $dbh->prepare("select homeroom from staff where userid = ?"); $sth->execute( $userid ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my $homeroom = $sth->fetchrow; # Start of Form print "
\n"; print "\n"; print "\n"; # show Date print "
". $lex{'Attendance Date'}; print " \n"; print "\n"; print "
\n"; # Find largest PPD my $maxppd; my $sth1 = $dbh->prepare("select distinct grade from student where homeroom = ?"); $sth1->execute( $homeroom ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } while ( my $grade = $sth1->fetchrow ) { if ( $g_ppd{$grade} > $maxppd ) { $maxppd = $g_ppd{$grade}; } } if ( not $maxppd ) { print $lex{'Max Periods per Day not found for'}. q{ }. $lex{Homeroom}. " $homeroom\n"; print "\n"; die; } #print "Maxppd: $maxppd
\n"; # Select students from homeroom $sth = $dbh->prepare("select lastname, firstname, studnum from student where homeroom = ? order by lastname, firstname"); $sth->execute( $homeroom ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } print "

". $lex{Homeroom}. " $homeroom

\n"; print "\n"; print "\n"; print "\n"; while (my ($lastname, $firstname, $studnum) = $sth->fetchrow){ print "\n"; for my $p ( 1..$maxppd ) { print "\n"; } print "\n\n"; } print "\n"; print "
\n"; print "
". $lex{Student}. ""; for my $p ( 1..$maxppd ) { print $lex{Per}. " $p"; } print $lex{Reason}. "". $lex{Late}. "
$lastname, $firstname"; print "
\n"; print "
\n"; print "\n"; print "
\n"; exit; } #---------------- sub dateNormalize { #---------------- my $date = shift; my ($year,$month,$day) = split /-/, $date; if ( length( $month ) == 1 ) { $month = '0'. $month; } # prepend 0 if ( length( $day ) == 1 ) { $day = '0'. $day; } # prepend 0 $date = "$year$month$day"; return $date; } #---------------------- sub doSubjectAttendance { #---------------------- #foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } my $colnum = 5; my $userid = $arr{subject}; delete $arr{subject}; # Start of Form print "\n"; print "\n"; # show Date print "
". $lex{'Attendance Date'}; print " \n"; print "\n"; print "
\n"; # Need the Term, to get the right subjects. my $term; my $cdate = dateNormalize( $currdate ); foreach my $t ( sort keys %g_termstart ) { my $start = dateNormalize( $g_termstart{ $t } ); my $end = dateNormalize( $g_termend{ $t } ); if ( $cdate >= $start and $cdate <= $end ) { $term = $t; last; } } if ( not $term ) { print "

". $lex{'Term not found'}. "

\n"; print "\n"; die; } #print "Term: $term
\n"; #require "../lib/libschedule.pl" or die "Cannot open libschedule.pl!\n"; # need $additionalcomments subject name to skip... require "../etc/repcard.conf" or die "Cannot open repcard.conf!\n"; # Get subjects taught this term by this teacher my $tempuser = '%('. $userid. ')'; my $sth= $dbh->prepare("select subjsec, smdesc, description from subject where subject.teacher $sql{like} ? and $term >= subject.startrptperiod and $term <= subject.endrptperiod"); $sth->execute( $tempuser ); if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } my $sth1 = $dbh->prepare("select count(*) from eval where subjcode = ?"); while ( my ( $subjsec, $smdesc, $description ) = $sth->fetchrow ) { if ( $description eq $additionalcomments ) { next; } # skip additional comment subjects # Check for student enrollments $sth1->execute( $subjsec ); if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } my $count = $sth1->fetchrow; if ($count < 1) { next; } # skip if no enrolled students. # Find largest PPD my $maxppd; my $sth1 = $dbh->prepare("select distinct s.grade from student as s, eval as e where e.subjcode = ? and e.studnum = s.studnum and grade != ''"); $sth1->execute( $subjsec ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } while ( my $grade = $sth1->fetchrow ) { if ( $g_ppd{$grade} > $maxppd ) { $maxppd = $g_ppd{$grade}; } } if ( not $maxppd ) { $maxppd = 1; } # fallback to 1 #print "Maxppd: $maxppd
\n"; if ( not $smdesc ) { # use description if not small description. $description =~ s/\s+//g; # strip spaces. $smdesc = substr($description,0, 7); } # Get the Kids first... studnums. $sth1 = $dbh->prepare("select distinct s.studnum, s.lastname, s.firstname from eval as e, student as s where subjcode = ? and e.studnum = s.studnum order by s.lastname, s.firstname"); $sth1->execute( $subjsec ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } # Print Start of Subject Entry Table print "\n"; print "\n"; print "\n"; print "\n"; # Loop Through Students while ( my ( $studnum, $lastname, $firstname ) = $sth1->fetchrow ) { if ( not $lastname ){ # student name not found. $lastname = "". $lex{'Student Not Found'}. ""; } print ""; print "\n"; print "\n"; } print "\n"; print "
$description ($subjsec)
". $lex{Student}. "". $lex{'Period(s)'}; print "". $lex{Reason}. "". $lex{Late}. "
\n"; print "\n"; print "
$lastname, $firstname\n"; for my $p ( 1..$maxppd ){ print $lex{Per}. $p; print " \n"; } print "
\n"; print "\n"; print "
\n\n\n"; } # Outer Loop here for each subject. print "\n"; print "
\n"; exit; } #------------------ sub writeRecords { #------------------ my $mode = $arr{writeflag}; delete $arr{writeflag}; #print "Mode: $mode
\n"; my $date = $arr{date}; delete $arr{date}; print "
". $lex{'Attendance Date'}. " $date
\n"; print "\n"; print "\n"; my $sth1 = $dbh->prepare("select lastname, firstname from student where studnum = ?"); my $sth2 = $dbh->prepare("select description from subject where subjsec = ?"); if ( $mode eq 'subject' ) { foreach my $key ( sort keys %arr ) { if ( $arr{$key} ) { my ( $studnum, $subjsec, $period, $reason, $late ); ( $studnum, $subjsec, $period ) = split /:/, $key; if ( not $period or $period =~ m/\D/ ) { next; } $reason = $arr{ "$studnum:$subjsec:Reason" }; $late = $arr{ "$studnum:$subjsec:Late" }; # Get Student Name and Subject Description $sth1->execute( $studnum ); my ( $lastname, $firstname ) = $sth1->fetchrow; if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } $sth2->execute( $subjsec ); my ( $description ) = $sth2->fetchrow; if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } # Insert the Record my $sth = $dbh->prepare("insert into attend values ( $sql{default}, ? , ? , ? , ? , ? ,? )"); $sth->execute( $studnum, $date , $reason, $period, $subject,$late ); if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } # Show Result print "\n"; } } } else { # grade or homeroom method foreach my $key ( sort keys %arr ) { if ( $arr{$key} ) { my ( $studnum, $subjsec, $period, $reason, $late ); ( $studnum, $period ) = split /:/, $key; if ( not $period or $period =~ m/\D/ ) { next; } $reason = $arr{ "$studnum:Reason" }; $late = $arr{ "$studnum:Late" }; # Get Student Name $sth1->execute( $studnum ); my ( $lastname, $firstname ) = $sth1->fetchrow; if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } # Insert the Record my $sth = $dbh->prepare("insert into attend values ( $sql{default}, ? , ? , ? , ? , $sql{default} ,? )"); $sth->execute( $studnum, $date, $reason, $period, $late ); if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } # Show Result print "\n"; } } } exit; }
". $lex{Student}. "". $lex{Subject}. "". $lex{Period}. ""; print $lex{Reason}. "". $lex{Late}. "
$lastname, $firstname ($studnum)"; print "$description ($subjsec)$period$reason$late
$lastname, $firstname ($studnum)"; print $lex{None}. "$period$reason$late