#!/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. # 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 = ('Enrol Students by Grade' => 'Enrol Students by Grade', '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', 'Error' => 'Error', 'Checked' => 'Checked', 'Please Select Subjects to Add' => 'Please Select Subjects to Add', 'Teacher' => 'Teacher', ); use DBI; use CGI; eval require "../../etc/admin.conf"; if ( $@ ) { print $lex{Error}. " $@
\n"; die $lex{Error}. " $@\n"; } 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{'Enrol Students by Grade'}. " \n$chartype\n\n"; print "[ ". $lex{Main}. " |\n"; print "". $lex{'Report Card'}. " ]\n"; if ( not $arr{page} ) { showStartPage(); } if ( $arr{page} == 2 ) { delete $arr{page}; selectSubjects(); } if ( $arr{page} == 3 ) { delete $arr{page}; selectStudents(); } if ( $arr{page} == 4 ) { delete $arr{page}; writeRecord(); } #----------------- sub selectStudents { # select students in each of the subjects #----------------- #foreach my $key (keys %arr) { print "K:$key V:$arr{$key}
\n"; } my $grade = $arr{grade}; my $checked = $arr{checked}; delete $arr{grade}; delete $arr{checked}; # passed %arr containing all subjects of interest, now # 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; } my @studarray; while (my ($lastname, $firstname, $initial, $studnum) = $sth->fetchrow ) { push @studarray, "$lastname, $firstname $initial:$studnum"; } $sth = $dbh->prepare("select description, teacher from subject where subjsec = ?"); print "

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

\n"; print "

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

\n"; print "
\n"; print "\n"; print "
\n"; foreach my $subjsec ( sort keys %arr ){ #loop through subjects; $sth->execute( $subjsec ); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } my ( $description, $teachid ) = $sth->fetchrow; 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"; exit; } #---------------- sub showStartPage { #---------------- print "
\n"; print "
\n"; print "\n"; print $lex{'Please enter a Grade'}. "
\n"; print $lex{Checked}. "?
\n"; print "\n"; print "
\n"; exit; } #----------------- sub selectSubjects { #----------------- #foreach my $key (keys %arr) { print "K:$key V:$arr{$key}
\n"; } my $grade = $arr{grade}; my $checked = $arr{checked}; # Find all subjects in this grade... 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; } # Start of Subject Selection. print "

". $lex{'Please Select Subjects to Add'}. "

\n"; print "
\n"; print "\n"; print "\n"; print "\n"; print '\n"; while ( my ($description, $teacher, $subjsec, $startrp, $endrp) = $sth->fetchrow ) { if ( not $startrp or not $endrp ){ # missing start/end term print "No reporting period for: $description ($subjsec)
\n"; next; } print "\n"; print "\n"; } print "\n"; print "
'. $lex{'Subjects'}. "". $lex{Teacher}. "
$description ($subjsec)$teacher
"; print $lex{Checked}. "? \n"; print "
\n"; print "\n"; exit; } # end of selectSubjects #-------------- 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; # Not needed; no insert $description = $dbh->quote( $description ); $teacher = $dbh->quote( $teacher ); # 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', $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default}, $sql{default} )"); $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'}; print " $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; } }