#!/usr/local/bin/perl

####################################################################
# Copyright @ 1995-1998 University of Rochester
# All Rights Reserved
####################################################################


use lib '.'; use webworkInit; # WeBWorKInitLine
use Global;
use TimeLocal;
use strict;

# This file is  alternateScoreProbSet.pl

# Call with command arguments of the form:  className set5.def 

if (@ARGV != 2)  {
    print "\nSyntax is alternateScoreProbSet.pl courseID setDefinitionFile.ext \n";
    print "      (e.g. alternateScoreProbSet.pl MTH140A set7.def ) \n\n";
    exit(0);
}

 
# CAUTION:  FOR THIS SCRIPT TO WORK CORRECTLTY,  ALL STUDENTS
#           MUST HAVE BEEN ASSIGNED THE SAME NUMBER OF PROBLREMS 


my $classID = $ARGV[0];

my $scriptDirectory   = &getWebworkScriptDirectory();
&getCourseEnvironment("$classID");
my $databaseDirectory = &getCourseDatabaseDirectory();
my $templateDirectory = &getCourseTemplateDirectory();
my $scoringDirectory  = &getCourseScoringDirectory(); 

my $DAT = &getDat();

require "${scriptDirectory}$Global::DBglue_pl";
require "${scriptDirectory}$Global::FILE_pl";
require "${scriptDirectory}$Global::SCRtools_pl";


my $fileName="${templateDirectory}$ARGV[1]";

print STDERR "Getting set definition from file:$fileName \n";

my ($setNumber,$setHeaderFileName,$probHeaderFileName,$dueDate,$openDate,$answerDate,$problemListref,$problemValueListref,$problemAttemptLimitListref)
  = &readSetDef($fileName);
my @problemList = @$problemListref;

$dueDate = &unformatDateAndTime($dueDate);	##just in case someone uses the old format
$dueDate = &formatDateAndTime($dueDate);	##dueDate is now in the form 4/15/96 at 2:00 AM 

my $dueTime=$dueDate;                            
$dueDate=~s|^\s*||;
$dueDate=~s|\s*at.*$||;
$dueTime=~s|^.*at\s*||;
$dueTime=~s|\s*$||;	

print "$dueDate\n";
print "$dueTime\n";

&get_raw_scores('scofiles', $setNumber, $dueDate, $dueTime,$problemListref,$problemValueListref);

my $fullTotalsFileName = "${scoringDirectory}${classID}totals.${DAT}";
my $fullScrFileName = "${scoringDirectory}s${setNumber}scr.${DAT}";
my $noOfProbs = @problemList;

print "\n Number of Probs is $noOfProbs\n"; 

print "\n\nDo you want to total the scores in the score file for this set,\n";
print "$fullScrFileName, \n";
print "and append these totals to the cummulative totals file,\n";
print "$fullTotalsFileName, \n\n";
print "We recommend that you do\? (Y or N)\n";

my $ans;

$ans=<STDIN>;
chomp($ans);
unless (($ans eq "Y") || ($ans eq "y")) {exit 0;}

my $totalsFileName = "${classID}totals";
my $scrFileName = "s${setNumber}scr";

&total_score($scrFileName,$setNumber);    

unless (-e "$fullTotalsFileName")
	{
    my $gid = $Global::numericalGroupID;
    $gid = $Global::numericalGroupID;    ## hack to remove warning message about $Global::numericalGroupID used only once	    
	&createFile("$fullTotalsFileName", $Global::scoring_files_permission, $gid);
	}
	
&append_score_db($totalsFileName,$scrFileName);

&delete_columns ($totalsFileName, -$noOfProbs-1, -2, 0);


