#!/usr/bin/perl



## This file is profDeleteProSetPage.pl
##

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

use lib '/ww/webwork/development/'; # mainWeBWorKDirectory;
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.");
}

# establish environment for this script

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


my $cgiURL   				= getWebworkCgiURL;
my $courseScriptsDirectory 	= getCourseScriptsDirectory;
my $databaseDirectory 		= getCourseDatabaseDirectory;
my $scriptDirectory   		= getWebworkScriptDirectory;


require "${scriptDirectory}$Global::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'});
my $defaultClasslistFile = getCourseClasslistFile($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);
	    }
####################################Page 1####################################
unless ($inputs{'pDPS1'}) {
    print &htmlTOP("Delete Problem Sets");
my $Course 			= $inputs{'course'};
print <<EOF;
    <H3 align="center">Delete Problem Sets Page for $Course</H3>

    On this page, you may delete one or more problem sets (and associated .sco files) from the
	database, leaving nothing but the uncompiled set definition file(s) and problem template
	files themselves in/under the templates directory. <b> Be very careful.</b> Whereas a
	deleted set can be rebuilt with the exact same problems for all students, the current
	information on scoring and status in the webwork-database and the .sco files <b>can not</b> be
	recovered.  If you want to save the current scoring information for a set to be
	deleted, score the set before deleting it (goto the scoring page) and/or move the .sco files out
	of the DATA directory. <b> Be very careful and 
	think before you click.</b> 
    <p><HR>
    Select problem set(s) to delete:
EOF

    print "<form action=\"${cgiURL}profDeleteProbSet.pl\">\n";
#    %setNumberHash=&getAllProbSetNumbersHash;
#    &printProbSets("setNo",\%setNumberHash);
	my %availableSets = &getAllProbSetNumbersHash;
	my @sortedSetNames = &sortSetNamesByDueDate(\%availableSets);
	
	# enter the available set numbers
	print qq! <SELECT Name = 'setNo' Size =4 multiple>\n!;
	my $ind;
	for $ind (@sortedSetNames) {
		print "<OPTION VALUE = \"$ind\">Set $ind\n";
		}
	print qq!</SELECT><BR>\n!;
# resume printing the rest of the forms

print <<EOF;
    <INPUT TYPE=RADIO NAME='outputFormat' VALUE='no_students' CHECKED> Don't display actions for individual students<BR>
    <INPUT TYPE=RADIO NAME='outputFormat' VALUE='all_students'> Display actions for all students<BR>
    <INPUT TYPE=HIDDEN NAME='pDPS1' VALUE=1>
EOF
    print &sessionKeyInputs(\%inputs);
print <<EOF;    
    <BR><INPUT TYPE=SUBMIT VALUE='Delete Problem Sets'</FORM>
    <INPUT TYPE=RADIO NAME='confirm' VALUE= 0 CHECKED> Off 
    <INPUT TYPE=RADIO NAME='confirm' VALUE= 1> Delete enabled<BR>        
EOF
    exit;
}

####################################Page 2####################################

if ($inputs{'confirm'}) {
    print &htmlTOP("DONE Deleting Your Problem Set(s)");

    my $query = $main::in{CGI};
    my @setNumbers = $query -> param('setNo' );
    my $setNumber;
    my $outputFormat = $inputs{'outputFormat'};

    foreach $setNumber (@setNumbers) {

        my @PINs=&getAllProbSetKeysForSet($setNumber);
        my @a=<${databaseDirectory}S${setNumber}*>;

        print "Deleting problems in set $setNumber<BR>\n";
        print "Problem set keys are: <BR>" if ($outputFormat eq 'all_students');
        my $PIN;
        my $PINcount = 0;
        foreach $PIN (@PINs) {
           &attachProbSetRecord($PIN);
            if (&deleteProbSetRecord($PIN)) {
                $PINcount++;
                print ( $PIN, " deleted<BR>\n" ) if ($outputFormat eq 'all_students');
            }
        }
        print "<BR> A total of $PINcount records have been deleted<BR><BR>\n";
        print "deleting .sco files: ${databaseDirectory}S$setNumber-*.sco<BR>";
        system ("rm ${databaseDirectory}S${setNumber}-*.sco");
    
        my $l2hDir = getCoursel2hDirectory();
        print "\ndeleting Latex2html tmp files and the directory:<BR>\n";
        print "     ${l2hDir}set$setNumber<BR>";
        system ("rm -rf ${l2hDir}set$setNumber");
        print '<HR>';
    }
    print "<H1><B>DONE.</B></H1>";
    &sendBack;
}
else {
    print &htmlTOP("NO Set(s) were deleted");
    print "No sets were deleted since Delete was not enabled. If you want to delete sets, go back and select
      &quot;Delete enabled&quot; before deleing the sets.<BR>";  
    print "<H1><B>NO SETS DELETED.</B></H1>";    
    &sendBack;
}    
      
# begin Timing code
my $endTime = new Benchmark;
&Global::logTimingInfo($beginTime,$endTime,"profDeleteProbSetPage.pl",$inputs{'course'},$inputs{'user'});
# end Timing code

exit;

#####################################END######################################

sub sendBack {
print <<EOF;
     <A HREF="${cgiURL}profLogin.pl?user=$inputs{'user'}&key=$inputs{'key'}&course=$inputs{'course'}">
          <IMG SRC="$Global::upImgUrl" BORDER=1></A>
EOF
}
