#! /usr/bin/perl # Copyright 2001-2011 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. $parpage = '/index.html'; my %lex = ('Main' => 'Main', 'Homework' => 'Homework', 'Not Found' => 'Not Found', 'View' => 'View', 'Continue' => 'Continue', 'Grade' => 'Grade', 'Students' => 'Students', 'Error' => 'Error', 'by' => 'by', 'Teacher' => 'Teacher', 'Start Date' => 'Start Date', 'Date' => 'Date', 'Topic' => 'Topic', 'Description' => 'Description', ); my $self = 'viewhomework.pl'; use DBI; use CGI; # Get etc directory location # Not useful #eval require "parbase.conf"; #if ( $@ ) { # print "$lex{Error}: $@
\n"; # die "$lex{Error}: $@\n"; #} # The location this script is run from (ie. what folder, etc.) my $configlocation = $g_EtcPath; # where to load the config file from. my $configlocation = '../etc'; eval require "$configlocation/admin.conf"; if ( $@ ) { print $lex{Error}. ": $@
\n"; die $lex{Error}. ": $@\n"; } my $q = new CGI; print $q->header( -charset, $charset ); my %arr = $q->Vars; # Get Current Date my @tim = localtime(time); my $year = $tim[5] + 1900; my $month = $tim[4] + 1; my $currdate = "$year-$month-$tim[3]"; my $dsn = "DBI:$dbtype:dbname=$dbase"; my $dbh = DBI->connect($dsn,$user,$password); $dbh->{mysql_enable_utf8} = 1; # Get current dir so know what CSS to display; # if (getcwd() =~ /tcgi/){ # we are in tcgi # $css = $tchcss; # $homepage = $tchpage; #} my $title = "$lex{View} $lex{Homework}"; print "$doctype\n$title\n"; print "\n"; print "$chartype\n\n"; print "[ $lex{Main} ]\n"; print "

$title

\n"; if ( not $arr{page} ) { showStartPage(); } elsif ( $arr{page} == 1 ) { delete $arr{page}; showHomework(); } #---------------- sub showStartPage { #---------------- # Read grades 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 = (); while ( my $grade = $sth->fetchrow ) { push @grades, $grade; } @grades = sort {$a <=> $b} @grades; # Read Teachers $sth = $dbh->prepare("select distinct s.teacher, st.lastname, st.firstname from subject s, staff st where s.teacher = st.userid and (s.teacher is not NULL ) and ( s.teacher != '' ) order by st.lastname, st.firstname, st.userid"); $sth->execute; if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } my @userid = (); my %teacher = (); while ( my ( $userid, $lastname, $firstname ) = $sth->fetchrow ) { push @userid, $userid; $teacher{$userid} = "$lastname, $firstname"; } print "
\n"; print "\n"; print "\n"; print "\n"; # Start Date print "\n"; # Select by Teacher print "\n"; print "\n"; print "
\n"; print "
$lex{'Start Date'}\n"; print "\n"; # Select by Grade print "
$lex{View} $lex{by} $lex{Grade}\n"; print "
$lex{View} $lex{by} $lex{Teacher}\n"; print "
\n"; print "
\n"; exit; } #--------------- sub showHomework { #--------------- # foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } if ( $arr{grade} ) { # find homework by grade # Read grades to verify 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 = (); while ( my $grade = $sth->fetchrow ) { $grades{$grade} = 1; } if ( not $grades{ $arr{grade} } ) { # no grade match; crack attempt print "

$lex{Grade} $lex{'Not Found'}:$arr{grade}

\n"; print "\n"; exit; } # Find subjects in this grade. my $sth = $dbh->prepare("select subjsec, description from subject where grade = ?"); $sth->execute( $arr{grade} ); if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } my %subjects = (); while ( my ($subjsec, $description) = $sth->fetchrow ) { $subjects{"$description ($subjsec)"} = $subjsec; } # Prepare to get homework info my $sth1 = $dbh->prepare("select * from plan_homework where subjsec = ? and to_days(duedatetime) >= to_days('$arr{date}') order by id"); # Loop through all subjects in this grade. foreach my $desc ( sort keys %subjects ) { my $subjsec = $subjects{$desc}; my $first = 1; # Get Homework info, if any. $sth1->execute( $subjsec ); if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } while ( my $ref = $sth1->fetchrow_hashref ) { if ( $ref ) { if ( $first ) { print "

$desc

\n"; print "\n"; print "\n"; $first = 0; } print "\n"; } } if ( $first ) { # print "

$lex{Homework} $lex{'Not Found'}: $desc

\n"; } else { print "
$lex{Date}$lex{Topic}$lex{Description}
$ref->{duedatetime}$ref->{topic}$ref->{description}

\n"; } } # loop through all subjects. } # end of homework by Grade if ( $arr{teacher} ) { # find homework by teacher # Read Teachers to verify my $sth = $dbh->prepare("select distinct teacher from subject"); $sth->execute; if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } my %teacher = (); while ( my $userid = $sth->fetchrow ) { $teacher{$userid} = 1; } # Verify teacher if ( not $teacher{ $arr{teacher} } ) { # no teacher match; crack attempt print "

$lex{Teacher} $lex{'Not Found'}:$arr{teacher}

\n"; print "\n"; exit; } # Find subjects of this teacher $sth = $dbh->prepare("select subjsec, description from subject where teacher = ?"); $sth->execute( $arr{teacher} ); if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } my %subjects = (); while ( my ($subjsec, $description) = $sth->fetchrow ) { $subjects{"$description ($subjsec)"} = $subjsec; } # Prepare to get homework info my $sth1 = $dbh->prepare("select * from plan_homework where subjsec = ? and to_days(duedatetime) >= to_days('$arr{date}') order by id"); # Loop through all subjects in this grade. foreach my $desc ( sort keys %subjects ) { my $subjsec = $subjects{$desc}; my $first = 1; # Get Homework info, if any. $sth1->execute( $subjsec ); if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } while ( my $ref = $sth1->fetchrow_hashref ) { if ( $ref ) { if ( $first ) { print "

$desc

\n"; print "\n"; print "\n"; $first = 0; } print "\n"; } } if ( $first ) { # print "

$lex{Homework} $lex{'Not Found'}: $desc

\n"; } else { print "
$lex{Date}$lex{Topic}$lex{Description}
$ref->{duedatetime}$ref->{topic}$ref->{description}

\n"; } } # loop through all subjects. } # end of homework by Teacher print "\n"; } # end of showHomework