#!/usr/bin/perl
# Copyright 2001-2023 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',
'Error' => 'Error',
'Grade' => 'Grade',
'Continue' => 'Continue',
'Course' => 'Course',
'Export' => 'Export',
'Evaluations' => 'Evaluations',
'Homeroom' => 'Homeroom',
'Course' => 'Course',
'or' => 'or',
'Term' => 'Term',
'Download' => 'Download',
'File' => 'File',
);
use DBI;
use CGI;
my $self = "exporteval.pl";
eval require "../../etc/admin.conf";
if ( $@ ) {
print $lex{Error}. " $@ \n";
die $lex{Error}. " $@\n";
}
my $q = new CGI;
print $q->header( -charset, $charset );
my %arr = $q->Vars;
my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;
# load report card configuration
my $sth = $dbh->prepare("select id, datavalue from conf_system where filename = 'repcard'
order by dataname");
$sth->execute;
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
while ( my ($id, $datavalue) = $sth->fetchrow ) {
eval $datavalue;
if ( $@ ) {
print "$lex{Error}: $@ \n";
die "$lex{Error}: $@\n";
}
}
# print page header.
my $title = "$lex{Export} $lex{Course} $lex{Evaluations}";
print qq{$doctype\n
\n};
# Starting Page
if ( not $arr{page} ){
showStartPage();
} elsif ( $arr{page} == 1 ) {
delete $arr{page};
selectCourses();
} elsif ( $arr{page} == 2 ) {
delete $arr{page};
exportRecords(); # to CSV.
}
#----------------
sub showStartPage {
#----------------
# Find all the grades
my @grades;
my $sth = $dbh->prepare("select distinct grade from student");
$sth->execute;
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
while ( my $grade = $sth->fetchrow ) {
push @grades, $grade;
}
# Find all the homerooms
my @homerooms;
$sth = $dbh->prepare("select distinct homeroom from student");
$sth->execute;
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
while ( my $homeroom = $sth->fetchrow ) {
push @homerooms, $homeroom;
}
# Find the courses and their sections, too.
$sth = $dbh->prepare("select subjsec, description from subject");
$sth->execute;
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
while ( my ( $subjsec, $desc ) = $sth->fetchrow ) {
# Skip Unwanted Subjects
my ($subjcode, $dud) = split('-', $subjsec);
if ( $r_SupressSubject{$subjsec} or $r_SupressSubject{$subjcode} or
$r_AdditionalComments{$subjsec} or $r_AdditionalComments{$subjcode} ) {
next;
}
# if ( $r_SupressSubject{$subjcode} or
# $r_AdditionalComments{$subjcode} ) {
# next;
# }
$subjects{"$desc ($subjsec)"} = $subjsec;
}
# Find the terms
my @terms;
$sth = $dbh->prepare("select distinct term from eval
where term is not NULL and term != '' order by term");
$sth->execute;
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
while ( my $trm = $sth->fetchrow ) {
push @terms, $trm;
}
print qq{\n};
print qq{\n};
exit;
}
#----------------
sub selectCourses {
#----------------
# foreach my $key ( sort keys %arr ) { print qq{
K:$key V:$arr{$key}
\n}; }
# Passed: grade, homeroom, subjcode, term, checked (all/mark), , joinsection (6 values)
my ($checkmark, $checked );
if ( $arr{checked} eq 'all' ) {
$checked = qq{checked="checked"};
} elsif ( $arr{checked} eq 'mark' ) {
$checkmark = qq{checked="checked"};
}
delete $arr{checked};
# my $subjcode = $arr{subjcode};
# delete $arr{subjcode};
my $term = $arr{term};
delete $arr{term};
# print qq{Checkmark:$checkmark Checked:$checked Term:$term \n};
# grade, homeroom, subjcode and joinsection still passed through
# If grade or homeroom, get the students, and then their course enrollments to find courses.
my %courses;
if ( $arr{grade} ) {
my $sth1 = $dbh->prepare("select distinct subjcode from eval where studnum = ?");
# Get Students in this grade
my $sth = $dbh->prepare("select studnum from student where grade = ?");
$sth->execute( $arr{grade} );
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
while ( my $studnum = $sth->fetchrow ) {
# Get student courses.
$sth1->execute( $studnum );
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
while ( my $subjsec = $sth1->fetchrow ) {
# Skip Unwanted Courses
my ($subjcode, $dud) = split('-', $subjsec);
if ( $r_SupressSubject{$subjsec} or $r_SupressSubject{$subjcode} or
$r_AdditionalComments{$subjsec} or $r_AdditionalComments{$subjcode} ) {
next;
}
$courses{$subjsec} = 1;
}
}
} elsif ( $arr{homeroom} ) {
my $sth1 = $dbh->prepare("select distinct subjcode from eval where studnum = ?");
# Get Students in this homeroom
my $sth = $dbh->prepare("select studnum from student where homeroom = ?");
$sth->execute( $arr{homeroom} );
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
while ( my $studnum = $sth->fetchrow ) {
# Get student courses.
$sth1->execute( $studnum );
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
while ( my $subjsec = $sth1->fetchrow ) {
# Skip Unwanted Courses
my ($subjcode, $dud) = split('-', $subjsec);
if ( $r_SupressSubject{$subjsec} or $r_SupressSubject{$subjcode} or
$r_AdditionalComments{$subjsec} or $r_AdditionalComments{$subjcode} ) {
next;
}
$courses{$subjsec} = 1;
}
}
} elsif ( $arr{subjsec} ) {
if ( $arr{joinsection} ) {
# Get all courses with this subject (all sections)
my ($subjcode,$dud) = split('-', $arr{subjsec});
my $sth = $dbh->prepare("select subjsec from subject where subjcode = ?");
$sth->execute( $subjcode );
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
while ( my $subjsec = $sth->fetchrow ) {
$courses{ $subjsec } = 1;
}
} else { # just a single section, as chosen;
$courses{ $arr{subjsec} } = 1;
}
} else {
print qq{