#!/usr/bin/perl # Copyright 2001-2007 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. # Description: Grade based subject enrollment. List all the students # for all the subjects in that grade. Deselect those that you don't # have in those subjects. my $self = 'enroladdgr.pl'; my %lex = ('Add Subject Enrollments' => 'Add Subject Enrollments', 'Main' => 'Main', 'Report Card' => 'Report Card', 'Please enter a Grade' => 'Please enter a Grade', 'Missing starting/ending reporting periods' => 'Missing starting/ending reporting periods', 'for the following subjects' => 'for the following subjects', 'Subjects' => 'Subjects', 'Subject Enrollment' => 'Subject Enrollment', 'Students' => 'Students', 'Continue' => 'Continue', 'Terms' => 'Terms', 'Your records are now stored' => 'Your records are now stored', 'Grade' => 'Grade', 'Record exists for Term' => 'Record exists for Term', 'for' => 'for', 'Your records are now stored' => 'Your records are now stored', 'There was an error storing your data' => 'There was an error storing your data', 'Please contact' => 'Please contact', 'Please record the following error' => 'Please record the following error', ); use DBI; use CGI; require "../../etc/admin.conf" or die "Cannot open admin.conf!"; my $q = new CGI; print $q->header; my %arr = $q->Vars; my $grade = $arr{grade}; my $checked = $arr{checked}; my $dsn = "DBI:$dbtype:dbname=$dbase"; my $dbh = DBI->connect($dsn,$user,$password); # Print Page Header print "$doctype\n". $lex{'Add Subject Enrollments'}. " \n$chartype\n\n"; print "[ ". $lex{Main}. " |\n"; print "". $lex{'Report Card'}. " ]\n"; if ($arr{flag}) { delete $arr{flag}; writeRecord(); } if (not $grade){ print "

". $lex{'Please enter a Grade'}. "!

\n"; print "\n"; die; } # Put all subjects of this grade into a subject array my $sth = $dbh->prepare("select description, teacher, subjsec, startrptperiod, endrptperiod from subject where grade = ? order by description"); $sth->execute($grade); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } my $periodflag; while (my ($description, $teacher, $subjsec, $startrp, $endrp) = $sth->fetchrow) { $description =~ s/://g; # strip any colons. $subjsec =~ s/://g; $teacher =~ s/://g; if (not $startrp or not $endrp){ # missing start/end term $periodflag = 1; push @errorarray,"$description ($teacher) $subjsec"; } push @subjarray, "$description:$teacher:$subjsec"; } if ($periodflag){ # missing reporting periods; crash and burn. print "

". $lex{'Missing starting/ending reporting periods'}. "
\n"; print $lex{'for the following subjects'}. ":

\n"; print "\n"; print '\n"; foreach my $badsubject (@errorarray){ print "\n"; } print "
'. $lex{'Subjects'}. "
$badsubject
\n"; die; } # Put all students of that grade into a student array my $sth = $dbh->prepare("select lastname, firstname, initial, studnum from student where grade = ? order by lastname, firstname"); $sth->execute($grade); my $studrows = $sth->rows; if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } while (my ($lastname, $firstname, $initial, $studnum) = $sth->fetchrow ) { push @studarray, "$lastname, $firstname $initial:$studnum"; } print "

". $lex{'Add Subject Enrollments'}. "

\n"; print "

". $lex{Grade}. " $grade  ". $lex{'Students'}. " $studrows

\n"; print "
\n"; print "\n"; print "
\n"; foreach my $subject (@subjarray){ #loop through subjects; my ($description,$teachid,$subjsec) = split /:/,$subject; print "
\n"; print "\n"; print "\n"; foreach $student (@studarray) { ($name,$studnum) = split /:/,$student; print ""; print "\n"; } print "
$description ($subjsec)
$name ($studnum)
\n\n"; } print "

\n"; print "
\n"; #-------------- sub writeRecord { #-------------- #foreach my $key (keys %arr) { print "K:$key V:$arr{$key}
\n"; } print "\n\n"; print '\n"; my $sth2 = $dbh->prepare("select lastname, firstname from student where studnum = ?"); foreach my $key (keys %arr) { # $key contains studnum:subjsec my ($studnum,$subjsec) = split(/:/,$key); # Read the subject my $sth1 = $dbh->prepare("select description, teacher, startrptperiod, endrptperiod from subject where subjsec = ?"); $sth1->execute( $subjsec ); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } my ($description, $teacher, $startterm, $endterm) = $sth1->fetchrow; # Check for the existence of the same record(s), # one for each term for each student in each subject. my ($term, %terms); $sth1 = $dbh->prepare("select term from eval where subjcode = ? and studnum = ? order by term"); $sth1->execute( $subjsec, $studnum ); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr;} # Get Student Name $sth2->execute($studnum); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } my ($lastname, $firstname) = $sth2->fetchrow; if (not $lastname) { $lastname = ''. $lex{'Not Found'}. ''; } # Put existing term records in the 'terms' hash; check for dups. while (my $term = $sth1->fetchrow){ if ($terms{$term}) { print $lex{'Record exists for Term'}. ": $term ". $lex{for}; print " $description ($subjsec) for $lastname, $firstname ($studnum).
\n"; next; } else { # add to hash. $terms{$term} = 1; } } # print line for this record. print ""; print "\n\n"; # end of printed row # Write a record into evaljrl table. writeEvalJrl($subjsec,$studnum); } # Next Student/Subject combo. if (not $DBI::errstr ) { print "
'. $lex{Students}. ''. $lex{Subjects}; print ''. $lex{Terms}. "
$lastname, $firstname ($studnum)$description ($subjsec) "; # Loop through and check where term is missing... for ($i = $startterm;$i <=$endterm; $i++){ if (not $terms{$i}){ # fill in holes! $sth = $dbh->prepare("insert into eval values ( $sql{default},'$subjsec','$studnum','$teacher','$i', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)"); $sth->execute; if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } print " $i "; } else { print '('. $lex{Skip}. " $i) "; } } # Loop for Terms print "
\n"; print "

". $lex{'Your records are now stored'}. ".

\n"; } else { print "

". $lex{'There was an error storing your data'}. ".\n"; print $lex{'Please contact'}. " $adminname $adminemail\n"; print $lex{'Please record the following error'}. ": $DBI::errstr

\n"; } print "

[ ". $lex{'Report Card'}. " ]\n"; print "

\n"; exit; } # End of writeRecord #------------ sub writeEvalJrl { # write subj enroll recs into eval journal (evaljrl table) #------------ # Passed subject-section number (unique for a subject), and student number. my $subjsec = shift; my $studnum = shift; # Get Student Information my $sth = $dbh->prepare("select lastname, firstname, initial, birthdate, provnum from studentall where studnum = '$studnum'"); $sth->execute; if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } my ($lastname, $firstname, $initial, $birthdate, $provnum) = $sth->fetchrow; $lastname = $dbh->quote($lastname); $firstname = $dbh->quote($firstname); $initial = $dbh->quote($initial); # Add a single record for this student in this subject to evaljrl table. $sth = $dbh->prepare("insert into evaljrl values ( $sql{default},'$subjsec','$studnum', now(),'enrol', 'Y',$lastname, $firstname , $initial ,'$birthdate','$provnum')"); $sth->execute; if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } }