#!/usr/bin/perl
# Copyright 2001-2020 Leslie Richardson
use DBI;
use CGI;
use Number::Format qw(:all);
my $self = 'emailStudent.pl';
# my $sssvmail = "SummerSchoolfortheSoloVoice\@gmail.com";
eval require "../etc/admin.conf.root";
if ( $@ ) {
print $lex{Error}. ": $@
\n";
die $lex{Error}. ": $@\n";
}
my $dbtype = 'mysql';
my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;
# Load configuration values of interest
my $sth = $dbh->prepare("select datavalue from conf_system where dataname = ?");
foreach my $dv ( qw( doctype tchcss tchpage )) {
$sth->execute($dv);
my $datavalue = $sth->fetchrow;
eval $datavalue;
if ( $@ ) {
print "Error: $@
\n";
die "Error: $@\n";
}
}
my $q = new CGI;
print $q->header( -charset, $charset );
my %arr = $q->Vars;
# Page Header.
my $title = "Email Students/Parents";
print "$doctype\n
$title\n";
print qq{\n};
print qq{[ Main ]\n};
print qq{$title
\n};
if ( not $arr{page} ) {
showStartPage();
} elsif ( $arr{page} == 1 ) {
delete $arr{page};
selectStudents();
} elsif ( $arr{page} == 2 ) {
delete $arr{page};
showReport();
}
#-------------
sub showReport {
#-------------
# foreach my $key (keys %arr ) { print "K:$key V:$arr{$key}
\n"; }
# find who to email for each student.
my @emailwho;
foreach my $key ( keys %arr ) {
if ( $key eq 'emailstudent' ) {
push @emailwho, 'email';
delete $arr{$key};
} elsif ( $key eq 'emailpar1' ) {
push @emailwho, 'par1_email';
delete $arr{$key};
} elsif ( $key eq 'emailpar2' ) {
push @emailwho, 'par2_email';
delete $arr{$key};
}
}
if ( not @emailwho ) {
print qq{No Email Recipients Selected!
\n};
print qq{\n};
exit;
}
# foreach my $key (keys %arr ) { print "K:$key V:$arr{$key}
\n"; }
# Get teacher's email address.
my $userid = $ENV{'REMOTE_USER'};
my $sth = $dbh->prepare("select email from staff where userid = ?");
$sth->execute($userid);
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
my $tchEmail = $sth->fetchrow;
my $sth = $dbh->prepare("select * from studentall where studnum = ?");
my %email; # list of all emails to send to.
foreach my $studnum ( sort {$a <=> $b} keys %arr ) {
# Get the student record
$sth->execute($studnum);
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
my $ref = $sth->fetchrow_hashref;
my %r = %$ref;
foreach my $field ( @emailwho ) {
$email{ $r{$field} } = 1;
print "Field:$field Email:$r{$field}
\n";
}
}
my $em = join(',',keys %email);
print qq{[ Send Email ]\n};
# Example: print qq{Email Confirmation | };
print qq{