#!/usr/local/bin/webwork-perl



## This file is profScoring.pl
## It provides access to utilities for building, correcting and viewing problem sets
##

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

use lib '.'; use webworkInit; # WeBWorKInitLine
use CGI qw(:standard);
use Global;
use Auth;
use strict;

# begin Timing code
use Benchmark;
my $beginTime = new Benchmark;
# end Timing code

&CGI::ReadParse;
my %inputs =%main::in;

# get information from CGI inputs  (see also below for additional information)

	my $Course 			= $inputs{'course'};
	my $User 			= $inputs{'user'};
	my $Session_key 	= $inputs{'key'};

# verify that information has been received
	unless($Course && $User && $Session_key) {
		&wwerror("$0","The script did not receive the proper input data.","","");
		die "The script profLogin.pl did not receive the proper input data.";
		}

# establish environment for this script

	&Global::getCourseEnvironment($inputs{'course'});


	my $cgiURL   				= getWebworkCgiURL;
	my $courseScriptsDirectory 	= getCourseScriptsDirectory;
	my $databaseDirectory 		= getCourseDatabaseDirectory;
	my $htmlURL       			= getCourseHtmlURL;
	my $scriptDirectory   		= getWebworkScriptDirectory;
	my $scoringDirectory        = getCourseScoringDirectory;
	my $dat                     = getDat;

	require "${scriptDirectory}$Global::DBglue_pl";
	require "${scriptDirectory}$Global::classlist_DBglue_pl";
	require "${scriptDirectory}$Global::FILE_pl";
	require "${scriptDirectory}$Global::HTMLglue_pl";

# log access
	&Global::log_info('', query_string);


	my $permissionsFile = &Global::getCoursePermissionsFile($inputs{'course'});
	my $permissions = &get_permissions($inputs{'user'}, $permissionsFile);
	my $keyFile = &Global::getCourseKeyFile($inputs{'course'});

#verify session key
	&verify_key($inputs{'user'}, $inputs{'key'}, $keyFile, $inputs{'course'});

# verify permissions are correct
	if ($permissions != $Global::instructor_permissions ) {
	    print "permissions = $permissions instructor_permissions = $Global::instructor_permissions\n";
	    print &html_NO_PERMISSION;
	    exit(0);
	    }
# get the rest of the information from the submitted form
	# nothing further to get in this case

# get information from the data base.

	# a list of all of the currently created sets:
	# Keys contains the set numbers and the value is a representative psvn
	my %availableSets = &getAllProbSetNumbersHash;
	my @sortedSetNames = &sortSetNamesByDueDate(\%availableSets);

# print HTML text
print &htmlTOP("Scoring Utilities");

# print navigation buttons
print qq!
<A HREF="${cgiURL}profLogin.pl?user=$inputs{'user'}&key=$inputs{'key'}&course=$inputs{'course'}">
<IMG SRC="${Global::upImgUrl}" align="right" BORDER=1 ALT="[Up]"></A><p>
!;

print <<EOF;
<HR><BR>

<h3 align="left">WeBWorK scoring programs for $Course</h3>

From this page, you can score problem sets, download and upload the scoring
spread sheet files, etc. <p>
EOF


## profScoring form -- allows you to score problem sets
print <<EOF;
<P>
<HR NOSHADE>
<H4 ALIGN=LEFT>
 <IMG SRC="$Global::bluesquareImgUrl" BORDER=1  ALT="">
1. Score problem sets in $Course:
</H4>

Select the set(s) you want to score.

		<FORM METHOD = 'POST' ACTION= "${cgiURL}profScoreProbSet.pl">
         <INPUT TYPE='SUBMIT' VALUE='Score problem sets'>

EOF

# enter the available set numbers
    print qq! <SELECT Name='setNo' Size =4 multiple >\n!;
	my ($ind,$label,$fileName, @stat, $date);
	for $ind (@sortedSetNames) {
	    $label ='  Not scored';
	    $fileName = "${scoringDirectory}s${ind}scr.$dat";
	    if (-e $fileName) {
	        @stat = stat($fileName);
	        $date = $stat[9];
	        $date = formatDateAndTime($date);
	        $date =~ s|\s*at.*||;
	        $label = "  Scored $date";
	    }
		print "<OPTION VALUE = \"$ind\">Set $ind $label\n";
	}
    print qq!</SELECT>\n!;
    print qq{
		<BR><INPUT TYPE=CHECKBOX NAME="appendTotals" VALUE=1 checked>Append totals to ${Course}_totals.csv (Recommended)
		<BR>Sort Order:
		<INPUT TYPE=RADIO NAME="sortOrder" VALUE='A' >  by Student Name
		<INPUT TYPE=RADIO NAME="sortOrder" VALUE='S' >  by Student ID  <BR>

		<INPUT TYPE=RADIO NAME="sortOrder" VALUE='AbySec' CHECKED> by Section then by Student Name
		<INPUT TYPE=RADIO NAME="sortOrder" VALUE='SbySec' >  by Section then by Student ID <BR>

		<INPUT TYPE=RADIO NAME="sortOrder" VALUE='AbyRec' > by Recitation then by Student Name
		<INPUT TYPE=RADIO NAME="sortOrder" VALUE='SbyRec' > by Recitation then by Student ID <BR>

	    };

# resume printing the rest of the form

print <<EOF;
         <INPUT TYPE="HIDDEN" NAME="user" VALUE="$inputs{'user'}">
         <INPUT TYPE="HIDDEN" NAME="key" VALUE="$inputs{'key'}">
         <INPUT TYPE="HIDDEN" NAME="course" VALUE="$inputs{'course'}">
         <INPUT TYPE="HIDDEN" NAME="batchMode" VALUE= 0 >
         </FORM>
EOF

print <<EOF;
<P>
<HR NOSHADE>
<H4 ALIGN=LEFT>
 <IMG SRC="$Global::bluesquareImgUrl" BORDER=1  ALT="">
2. Download (or view) $Course scoring files to your personal computer.
</H4>

You can download files for editing or viewing in either cvs (i.e. comma delimited), html, text, or
tab (i.e. tab delimited) format.  <BR>
csv is a supported format for Excel and is the recommended format if you use Excel. <BR> <BR>
The <strong>totals</strong> file (${Course}_totals.csv) is the cummulative semester record of grades.<BR>
The <strong>scr</strong> files (e.g. s2scr.csv) give, for each problem set, the STATUS of individual problems.
The STATUS is a number between 0 and 1 depending on (partial) credit earned.  "STATUS" times "PROB VAL" gives the score for the problem.<BR>
The <strong>ful</strong> files (e.g. s2ful.csv) give, for each problem set, the STATUS plus data on
the number of correct and incorrect attempts for each problem.<BR>
<strong>all</strong> files include backup files, etc.





		<FORM METHOD = "POST" ACTION= "${cgiURL}profDownloadScoringFiles.pl">
		format:
		<INPUT TYPE=RADIO NAME="format" VALUE="csv" CHECKED>csv
		<INPUT TYPE=RADIO NAME="format" VALUE="html" >html
		<INPUT TYPE=RADIO NAME="format" VALUE="text" >text
		<INPUT TYPE=RADIO NAME="format" VALUE="tab" >tab  <BR>
		<INPUT TYPE=CHECKBOX NAME="totalsFile" VALUE=1 checked> totals file
		<INPUT TYPE=CHECKBOX NAME="scrFiles" VALUE=1> scr files
		<INPUT TYPE=CHECKBOX NAME="fulFiles" VALUE=1> ful files
		<INPUT TYPE=CHECKBOX NAME="allFiles" VALUE=1> all files	<BR>

         <INPUT TYPE="SUBMIT" VALUE="Select file(s)">

EOF

# resume printing the rest of the form

print <<EOF;
         <INPUT TYPE="HIDDEN" NAME="user" VALUE="$inputs{'user'}">
         <INPUT TYPE="HIDDEN" NAME="key" VALUE="$inputs{'key'}">
         <INPUT TYPE="HIDDEN" NAME="course" VALUE="$inputs{'course'}">
         <INPUT TYPE="HIDDEN" NAME="download" VALUE= 0 >
         <INPUT TYPE="HIDDEN" NAME="fileName" VALUE= "" >
         </FORM>
EOF

print <<EOF;
<P>
<HR NOSHADE>
<H4 ALIGN=LEFT>
 <IMG SRC="$Global::bluesquareImgUrl" BORDER=1  ALT="">
3. Simple editing of $Course scoring files.
</H4>

Use this for simple editing, e.g. changing a few scores or possibly adding a new column of exam grades.
For more complicated editing, download the file to a preadsheet program such as Excel by option 2 above.


		<FORM METHOD = "POST" ACTION= "${cgiURL}profFormEditScoringFiles.pl">
		<INPUT TYPE=CHECKBOX NAME="totalsFile" VALUE=1 checked> totals file
		<INPUT TYPE=CHECKBOX NAME="scrFiles" VALUE=1> scr files
		<INPUT TYPE=CHECKBOX NAME="fulFiles" VALUE=1> ful files
        <INPUT TYPE="SUBMIT" VALUE="Select file(s)">

EOF



# resume printing the rest of the form

print <<EOF;
         <INPUT TYPE="HIDDEN" NAME="user" VALUE="$inputs{'user'}">
         <INPUT TYPE="HIDDEN" NAME="key" VALUE="$inputs{'key'}">
         <INPUT TYPE="HIDDEN" NAME="course" VALUE="$inputs{'course'}">
         <INPUT TYPE="HIDDEN" NAME="action" VALUE= "" >
         <INPUT TYPE="HIDDEN" NAME="fileName" VALUE= "" >
         <INPUT TYPE="HIDDEN" NAME="readWrite" VALUE= "readOnly" >
         </FORM>
EOF


print <<EOF;
<P>
<HR NOSHADE>
<H4 ALIGN=LEFT>
 <IMG SRC="$Global::bluesquareImgUrl" BORDER=1  ALT="">
4. Upload $Course scoring files from your personal computer.
</H4>

You can upload scoring files from your personal computer in cvs (i.e. comma delimited),
tab (i.e. tab delimited), or html format.  Usually you would do this after
downloading and editing a file in Excel or some other spreadsheet program.  Only valid scoring files
with filenames ending in "_totals.ext", "scr.ext", and "ful.ext" can be uploaded where the extension
"ext" must be "csv", "txt" (for tab delimited), "html" or "htm". You can not upload backup files without
renaming them.
<FORM METHOD = 'POST' enctype='multipart/form-data' ACTION= "${cgiURL}profUploadScoringFile.pl">

		format on personal computer:
		<INPUT TYPE=RADIO NAME="format" VALUE="csv" CHECKED>csv
		<INPUT TYPE=RADIO NAME="format" VALUE="tab" >tab
		<INPUT TYPE=RADIO NAME="format" VALUE="html" >html  <BR>
		<INPUT TYPE=CHECKBOX NAME="overWrite" VALUE=1 unchecked> Overwrite existing file
		<INPUT TYPE=CHECKBOX NAME="backUp" VALUE=1 checked> Backup existing file first	<BR>

File to upload: <input type=file name=upload><br>
<br>
<input type=submit value=UpLoad> the file.

         <INPUT TYPE="HIDDEN" NAME="user" VALUE="$inputs{'user'}">
         <INPUT TYPE="HIDDEN" NAME="key" VALUE="$inputs{'key'}">
         <INPUT TYPE="HIDDEN" NAME="course" VALUE="$inputs{'course'}">
         </FORM>
EOF


print <<EOF;
<P>
<HR NOSHADE>
<H4 ALIGN=LEFT>
 <IMG SRC="$Global::bluesquareImgUrl" BORDER=1  ALT="">
5. Sort $Course scoring files.
</H4>

Use this for sorting scoring files.  Student records can be sorted by
one of six methods: (a) by student name, (b) by student ID, (c) by section then
by student name, (d) by section then by student ID, (e) by recitation then
by student name and (f) by recitation then by student ID.


		<FORM METHOD = "POST" ACTION= "${cgiURL}profSortScoringFiles.pl">
		<INPUT TYPE=CHECKBOX NAME="totalsFile" VALUE=1 checked> totals file
		<INPUT TYPE=CHECKBOX NAME="scrFiles" VALUE=1> scr files
		<INPUT TYPE=CHECKBOX NAME="fulFiles" VALUE=1> ful files <BR>

		Sort Order:
		<INPUT TYPE=RADIO NAME="sortOrder" VALUE='A' >  by Student Name
		<INPUT TYPE=RADIO NAME="sortOrder" VALUE='S' >  by Student ID  <BR>

		<INPUT TYPE=RADIO NAME="sortOrder" VALUE='AbySec' CHECKED> by Section then by Student Name
		<INPUT TYPE=RADIO NAME="sortOrder" VALUE='SbySec' >  by Section then by Student ID <BR>

		<INPUT TYPE=RADIO NAME="sortOrder" VALUE='AbyRec' > by Recitation then by Student Name
		<INPUT TYPE=RADIO NAME="sortOrder" VALUE='SbyRec' > by Recitation then by Student ID <BR>

        <INPUT TYPE="SUBMIT" VALUE="Select file(s)">
        <INPUT TYPE="HIDDEN" NAME="user" VALUE="$inputs{'user'}">
        <INPUT TYPE="HIDDEN" NAME="key" VALUE="$inputs{'key'}">
        <INPUT TYPE="HIDDEN" NAME="course" VALUE="$inputs{'course'}">
        <INPUT TYPE="HIDDEN" NAME="action" VALUE= "" >
        <INPUT TYPE="HIDDEN" NAME="fileName" VALUE= "" >
        </FORM>
EOF




print &htmlBOTTOM("profScoring.pl", \%inputs,'profScoringHelp.html');

# begin Timing code
my $endTime = new Benchmark;
&Global::logTimingInfo($beginTime,$endTime,"profScoring.pl",$inputs{'course'},$inputs{'user'});
# end Timing code
exit;



