#! /usr/bin/perl
# Copyright 2001-2021 Leslie Richardson
# This file is part of Open Admin for Schools.
# Query Function for StudentPersonal Objects
# Passed Values: none.
use DBI;
use CGI;
use XML::Writer;
use XML::Writer::String;
use Data::UUID;
use HTTP::Request::Common qw(POST);
use HTTP::Headers;
use LWP::UserAgent;
use XML::LibXML;
my $self = 'syncenrol.pl';
eval require "../../etc/admin.conf";
if ( $@ ) {
print qq{Error $@ \n};
die qq{Error $@\n};
}
eval require "./slxmllibNew.pl";
if ( $@ ) {
print qq{Error $@ \n};
die qq{Error $@\n};
}
my $q = new CGI;
my %arr = $q->Vars;
print $q->header( -charset, $charset);
my $debug;
if ( $arr{debug} ){ $debug = 1; }
delete $arr{debug};
my $dsn = "DBI:$dbtype:dbname=$dbase";
$dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;
# Setup Date and Times
my @tim = localtime(time);
my $year = $tim[5] + 1900;
$tim[4]++;
for (0..4){if (length($tim[$_]) == 1){ $tim[$_] = '0'.$tim[$_];}}
my $currdate = "$year-$tim[4]-$tim[3]";
my $currtime = "$tim[2]:$tim[1]:$tim[0]";
my $currlongdate = "$month[$tim[4]] $tim[3], $year";
# Page Header
print qq{$doctype\n
SDS Query: School Enrollment
$chartype\n[ Main |
Export ]
Query School Enrollment
$currlongdate
\n};
# This shows the records to update....(and select)
if ( $arr{page} ) { # Activate/Create Transfer records
delete $arr{page};
activateTransfer();
}
# Find all local kids and push studnum into hash, display any blank provnum
my $sth = $dbh->prepare("select studnum, provnum, lastname, firstname
from student order by provnum");
$sth->execute;
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr;}
my $studcount = $sth->rows;
# Note any missing provincial numbers...
my $foundmissing = 0;
my %localStudent = ();
my %masterList = ();
while ( my ( $studnum,$provnum,$lastname, $firstname ) = $sth->fetchrow ) {
if ( not $provnum ) {
if ($foundmissing == 0){ # print the table heading, once.
print qq{
\n};
}
$foundmissing = 1;
print qq{
Missing Provincial Number for: };
print qq{$firstname $lastname ($studnum)
\n};
push @noprovnum, "$lastname:$firstname:$studnum"; # used to NOT withdraw them.
next;
}
$localStudent{$provnum} = 1;
$masterList{$provnum} = 1;
}
if ( $foundmissing ) {
print qq{
\n};
}
# Create a new user agent
my $ua = LWP::UserAgent->new();
$ua->agent("OpenAdmin");
my $count++;
mkQueryString( $count ); # Generate $output string
# DEBUG Data Errors
if ( $debug ){
print qq{
\n};
}
# Create the https post request
my $req = POST $url, [ XML=>$output->value ];
$req->content_type('application/xml;charset="utf-8"');
$req->authorization_basic($sds_userid, $sds_password);
# Issue the request and receive a response
my $res = $ua->request($req);
# Check the status of the response
if ( $res->is_success ) {
# For Debugging Data Errors
if ($debug){
print qq{
\n};
}
my $response = $res->content;
# Parse the response.
my $parser = XML::LibXML->new();
eval {$doc = $parser->parse_string($response)};
if ($@){
print qq{
Sask Ed Error:\n $@\n$response
\n};
print qq{\n}; die;
}
$doc->setEncoding('UTF-8');
my $root = $doc->getDocumentElement;
$root->setNamespace($xmlns,'sl',1);
my $mastermsgid = $root->findvalue('//sl:SL_MsgId');
my $status = $root->findvalue('//sl:SL_Status/sl:SL_StatusCode');
if ( $status eq 'Errors' or $status eq 'Invalid' ){
# Print out error and add to errorlog.
prErr($mastermsgid,"$lastname, $firstname ($studnum)");
} elsif ( $status eq 'Successful' ){
my $ref = parseSPLite( $root ); # main thing... get their list of kids.
@slprovnums = @$ref;
} else { # print warnings... ($status eq 'Warnings')
print qq{There were warnings...};
}
} else { # Transfer Error!
my $err = $res->status_line;
print qq{
Transfer Error: $err
};
exit;
}
# Now create a master list from both lists.
my %saskStudent = ();
my $skcount = $#slprovnums + 1;
foreach my $pn ( @slprovnums ) {
$masterList{$pn} = 1;
$saskStudent{$pn} = 1;
}
#other hash is: $localStudent{$provnum} = 1;
foreach my $pn ( keys %masterList ) { # go through all students (on both lists)
# Delete them if we have a match (they're registered in both systems)
if ( $saskStudent{$pn} == $localStudent{$pn} and $localStudent{$pn} == 1 ) {
delete $saskStudent{$pn};
delete $localStudent{$pn};
}
}
# Display Enrollments - Sask Ed and Local
print qq{