#!/usr/bin/perl
# Copyright 2001-2013 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 = ('Main' => 'Main',
'Eoy' => 'Eoy',
'Reset' => 'Reset',
'Grade' => 'Grade',
'Student' => 'Student',
'New' => 'New',
'Update Record(s)' => 'Update Record(s)',
'Homeroom' => 'Homeroom',
'Record(s) Updated' => 'Record(s) Updated',
'Contact' => 'Contact',
'Error' => 'Error',
'HRm' => 'HRm',
'Gr' => 'Gr',
'Promote' => 'Promote',
'Demote' => 'Demote',
);
my $self = 'resetgrade.pl';
use DBI;
use CGI;
eval require "../../etc/admin.conf";
if ( $@ ) {
print $lex{Error}. ": $@ \n";
die $lex{Error}. ": $@\n";
}
$q = new CGI;
print $q->header( -charset, $charset );
my %arr = $q->Vars;
my $promote = $arr{promote};
my $demote = $arr{demote};
my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;
# Print Page Header
my $title = "$lex{Reset} $lex{Grade}";
print "$doctype\n
\n";
if ( $arr{writeflag} ) {
delete $arr{writeflag};
resetGrades();
}
# Stuff all grades into an array;
my $sth = $dbh->prepare("select distinct grade from student
where grade is not NULL and grade != ''");
$sth->execute;
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my (@grades, %grades );
while ( my $grade = $sth->fetchrow ) {
$grades{$grade} = 1;
}
# Load defaults from metadata also.
$sth = $dbh->prepare("select defaultvalue from meta
where tableid = 'student' and fieldid = 'grade'");
$sth->execute;
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my $gradevalues = $sth->fetchrow;
my @temp = split /\s+/, $gradevalues;
foreach my $val ( @temp ) {
if ($val eq '~') { next; }
$grades{$val} = 1;
}
@grades = sort {$a <=> $b} keys %grades;
# Start Form
print "\n";
#--------------
sub resetGrades {
#--------------
#foreach my $key (keys %arr ) { print "K:$key V:$arr{$key} \n"; }
$sth = $dbh->prepare("select grade from student where studid = ?");
$sth1 = $dbh->prepare("update student set grade = ? where studid = ?");
foreach my $key ( keys %arr ) {
# Get Current Grade
$sth->execute( $key );
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }
my $grade = $sth->fetchrow;
# Update if necessary
if ( $grade ne $arr{$key} ){ # different; do an update
$sth1->execute( $arr{$key}, $key );
#print "K:$key V:$arr{$key} \n";
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }
}
}
if (not $DBI::errstr ) {
print "