[system] / trunk / webwork / system / scripts / proceduresForBuildProbSetDB.pl Repository:
ViewVC logotype

View of /trunk/webwork/system/scripts/proceduresForBuildProbSetDB.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (download) (as text) (annotate)
Thu Jun 14 17:08:51 2001 UTC (18 years, 7 months ago) by sam
File size: 13871 byte(s)
initial import

    1 #!/usr/bin/perl
    2 
    3 ## $Id$
    4 
    5 
    6 ####################################################################
    7 # Copyright @ 1995-1998 University of Rochester
    8 # All Rights Reserved
    9 ####################################################################
   10 
   11 
   12 
   13 # This file is proceduresForBuildProbSetDB.pl
   14 # Call with command arguments of the form:  set5.def  classl.gp
   15 # This file loads the database from the class list and the set definition
   16 ###################################################################
   17 
   18 use strict;
   19 
   20 #$method ='readFromLogFile';  ## or 'createNewPSVNs'
   21 #$method ='createNewPSVNs';
   22 #$logFileName = "sampleCourse_set:3_bak3.psvnlog";
   23 
   24 
   25 #########################
   26 
   27 sub buildProbSetDB  ## builds the problem set and returns a message
   28   {
   29   my ($classID,$setDefFilename,$method,$logFileName,$outputFormat) = @_;
   30   my $scriptDirectory   = $Global::scriptDirectory;
   31   &Global::getCourseEnvironment("$classID");
   32   my $databaseDirectory = $Global::databaseDirectory;
   33   my $templateDirectory = $Global::templateDirectory;
   34   my $logsDirectory = getCourseLogsDirectory();
   35 
   36   require "${scriptDirectory}$Global::DBglue_pl";
   37   require "${scriptDirectory}$Global::classlist_DBglue_pl";
   38   require "${scriptDirectory}$Global::FILE_pl";
   39 
   40 
   41        # Get data from set definition
   42 
   43   my $fileName="${templateDirectory}$setDefFilename";  ## e.g. $fileName=set5.def
   44   my $PIN;
   45   my $message = '';
   46   $message .= "\nGetting set definition from file: $fileName\n";
   47    # print  "Getting set definition from file: $fileName\n";
   48 
   49   my ($setNumber,$setHeaderFileName,$probHeaderFileName,$dueDate,$openDate,$answerDate,$problemListref,$problemValueListref,$problemAttemptLimitListref)
   50     = &readSetDef($fileName);
   51   my @problemList = @$problemListref;
   52   my @problemValueList = @$problemValueListref;
   53   my @problemAttemptLimitList = @$problemAttemptLimitListref;
   54   $message .= "  set Name or Number: $setNumber\n  openDate: $openDate\n  dueDate: $dueDate\n  answerDate: $answerDate\n\n";
   55 
   56 
   57   ## put dates into standard perl format, i.e. seconds since 1980
   58 
   59   $openDate = &unformatDateAndTime($openDate);
   60   $dueDate = &unformatDateAndTime($dueDate);
   61   $answerDate = &unformatDateAndTime($answerDate);
   62 
   63 
   64   #get data from class list.
   65   #  $fileName="${templateDirectory}$classlistFilename";  ## e.g. fileName=m161.lst
   66   #  print "Getting class list from $fileName\n";
   67 # checkClasslistFile($Global::noOfFieldsInClasslist,$fileName);
   68  #   open(FILE, "$fileName") || wwerror($0, "Can't open $fileName");
   69  #   my @classList=<FILE>;
   70  #   close(FILE);
   71   ###################################
   72   #  Before building the database we pause to check that the database file exists
   73   # (if not we create it).
   74   ###################################
   75   if ( -e "${databaseDirectory}$Global::database" ) {
   76 
   77   my @data = stat("${databaseDirectory}$Global::database");
   78   ## This statement is a hack.  When using Berkeley DB 1.85 on a FreeBSD system
   79   ## sometimes this test returns true even when the database file does not exist.
   80   ## We have never seen this happen with the stat statement. Very strange indeed.
   81 
   82  #   $message .= "data base file statistics: @data\n\n";
   83 
   84   $message .= "Loading WeBWorK database: ${databaseDirectory}$Global::database\n";
   85   }
   86   else {
   87      # print "Data base ${databaseDirectory}$Global::database does not exist.\n\n";
   88      $message .=   "Data base ${databaseDirectory}$Global::database does not exist.\n\n";
   89  #       print "Create new database?(y or n)";
   90  #       my $tempinput = <STDIN>;
   91     my $tempinput = 'y';       ## always create a new database
   92     if ($tempinput =~ /y|Y/) {
   93      #     print "Creating new data base ${databaseDirectory}$Global::database.\n";
   94       $message .= "Creating new data base ${databaseDirectory}$Global::database.\n";
   95       create_db("${databaseDirectory}$Global::database", $Global::webwork_database_permission);
   96       if ( -e "${databaseDirectory}$Global::database" ) {
   97       chmod($Global::webwork_database_permission,"${databaseDirectory}$Global::database") ||
   98                wwerror($0, "Can't do chmod($Global::webwork_database_permission,${databaseDirectory}$Global::database)");
   99       chown(-1,$Global::numericalGroupID,"${databaseDirectory}$Global::database")  ||
  100                wwerror($0,"Can't do chown(-1,$Global::numericalGroupID,${databaseDirectory}$Global::database)");
  101        # print "New data base created\n";
  102       $message .=   "New data base created\n";
  103       }
  104       else {
  105        #   print "New data base could not be created\n";
  106         $message .=   "New data base could not be created\n";
  107       }
  108     }
  109     else {
  110 
  111       print "Abort buildProbSetDB.pl\n\n";
  112       exit;
  113       }
  114     }
  115 
  116   ## Create a logfile in which we will save the following data for each new psvn created
  117   ## $login_name $PIN seedForProb1 seedForProb2 seedForProb3 ...
  118 
  119   ##don't overwrite existing backups
  120   my $i=1;
  121   while(-e "${logsDirectory}${classID}_set:${setNumber}_bak${i}.psvnlog") {$i++;}
  122   my $fullLogFileName ="${logsDirectory}${classID}_set:${setNumber}_bak${i}.psvnlog";
  123   &createFile($fullLogFileName, 0660, $Global::numericalGroupID);
  124   open(LOGFILE,">$fullLogFileName")  or  wwerror($0, "Can't open $fullLogFileName");
  125 
  126   ## two hashes which are used in 'readFromLogFile' mode
  127   my (%psvnsHash,%seedsHash,@seedsArrayFromLogFile);
  128 
  129   if ($method eq 'readFromLogFile') {
  130     my (@lineArray, $loginID, $seedString);
  131     $logFileName = "${logsDirectory}${logFileName}";
  132      # print "Getting psvn's and problem seeds from $logFileName\n\n";
  133     $message .= "Getting psvn's and problem seeds from $logFileName\n\n";
  134     open(BAKLOGFILE,"$logFileName")  or wwerror($0, "Can't open $logFileName");
  135     while (<BAKLOGFILE>)
  136       {
  137       @lineArray = split;
  138       $loginID= shift(@lineArray);
  139       $psvnsHash{$loginID} = shift(@lineArray);
  140       $seedString = join ':', @lineArray;
  141       $seedsHash{$loginID} = $seedString;
  142     }
  143   close(BAKLOGFILE);
  144   }
  145 
  146 
  147   ## get a hash of all loginID's for set if set is already defined
  148 
  149   my %loginHashForSet =();
  150   if (&probSetExists($setNumber)) {%loginHashForSet = &getLoginHashForSet($setNumber);}
  151   my $new_student_count = 0;
  152   my $existing_student_count = 0;
  153 
  154   my $login_name;
  155 
  156   $message .= "Loading classlist database: ${databaseDirectory}$Global::CL_Database\n\n";
  157   my @classList = @{getAllLoginNamesSortedByName()};
  158 
  159   foreach $login_name (@classList)   {                  ## read through classlist database and create
  160                            ## problems for all active students
  161                            ## except if problems already exist for
  162     attachCLRecord($login_name);
  163                          ## student
  164     my $lastName    = CL_getStudentLastName($login_name);
  165     my $firstName   = CL_getStudentFirstName($login_name);
  166     my $status      = CL_getStudentStatus($login_name);
  167     my $comment     = CL_getComment($login_name);
  168     my $section     = CL_getClassSection($login_name);
  169     my $recitation    = CL_getClassRecitation($login_name);
  170     my $email_address = CL_getStudentEmailAddress($login_name);
  171     my $studentID   = CL_getStudentID($login_name);
  172 
  173 
  174 
  175     unless (&dropStatus($status))                   ## skip students who have dropped the course
  176       {
  177       if (defined $loginHashForSet{$login_name}) {
  178         $existing_student_count++;
  179         if (((defined $outputFormat) and ($outputFormat eq 'all_students')) or ($method eq 'readFromLogFile')){
  180           $message .= "   Problems for $firstName $lastName (login $login_name) already exist.  \n";
  181           $message .= "      Retaining probsetkey $loginHashForSet{$login_name} and data.  \n";
  182         }
  183       }
  184       elsif (($method eq 'readFromLogFile') and !(defined($psvnsHash{$login_name})))
  185         {
  186       #      print "$firstName $lastName (login $login_name) is not in the logfile $logFileName.  \n";
  187         $message .= "   $firstName $lastName (login $login_name) is not in the logfile $logFileName.  \n";
  188       #      print "      Problems for $firstName $lastName (login $login_name) not added.\n";
  189         $message .= "      Problems for $firstName $lastName (login $login_name) not added.\n";
  190         }
  191       elsif (($method eq 'readFromLogFile') and (&attachProbSetRecord($psvnsHash{$login_name}) ))
  192         {
  193         $PIN = $psvnsHash{$login_name};
  194         my $oldFN = &getStudentFirstName($PIN);
  195         my $oldLN =  &getStudentLastName($PIN);
  196         my $oldLogin = &getStudentLogin($PIN);
  197 #                print "ERROR, ERROR, ERROR\n";
  198 #                print "         The psvn $PIN already exists. It is assigned to $oldFN $oldLN  ($oldLogin).\n";
  199 #                print "         A new problem set must have been created after the psvn $PIN for \n";
  200 #                print "         $firstName $lastName (login $login_name) was deleted.\n";
  201 #        print "         Problems for $firstName $lastName (login $login_name) have not been added.\n";
  202 #                print "         After restoring all other psvn's for this set from the psvnlog(s),\n";
  203 #                print "         run buildProbSetDB.pl.  This will create a new psvn for  $login_name.\n";
  204 #                print "         Then use the dataMonger to restore the original problem seeds for\n";
  205 #                print "         $login_name.  The seeds for  $login_name  are listed in order in the file\n";
  206 #                print "         $logFileName, on the line begining: $login_name $PIN .\n";
  207         $message .=
  208         "ERROR, ERROR, ERROR\n
  209      The psvn $PIN already exists. It is assigned to $oldFN $oldLN  ($oldLogin).\n
  210      A new problem set must have been created after the psvn $PIN for \n
  211      $firstName $lastName (login $login_name) was deleted.\n
  212      Problems for $firstName $lastName (login $login_name) have not been added.\n
  213      After restoring all other psvn's for this set from the psvnlog(s), use \n
  214      Build problem sets from the Professor's page to build this set again.  This \n
  215      will create a new psvn for  $login_name.\n
  216      Then use Examine or modify data from the Professor's page to restore the original \n
  217      problem seeds for $login_name.  The seeds for  $login_name  are listed in order in \n
  218      the file $logFileName, on the line begining: $login_name $PIN .\n";
  219 
  220         }
  221       else
  222         {
  223         if ($method eq 'readFromLogFile')
  224           {
  225           $PIN = $psvnsHash{$login_name};
  226           }
  227         else
  228           {
  229           # Create a new unique pin number;
  230           my $min_psnv = 10**($Global::psvn_digits - 1);
  231           my $big_psvn = 10**$Global::psvn_digits - $min_psnv - 1;
  232           $PIN = int (  rand($big_psvn)+$min_psnv ); ##eg rand(8999)+1000
  233           $i=0;
  234           # Try for up to 200 times to create a pin number which hasn't been used.
  235           while ( &attachProbSetRecord($PIN)   ) {
  236              # print "psvn Number $PIN already taken\n" if $i>0 ;
  237             $message .= "psvn Number $PIN already taken\n" if $i>0 ;
  238             $PIN = int (  rand($big_psvn)+$min_psnv );
  239             ++$i;
  240             if ($i>200) { wwerror($0, 'Tried 200 times and could not find an unused psvn number.
  241               You have run out of psvn numbers.  In Global.pm, increase the variable \$psvn_digits')};
  242             }#end while loop
  243           } ##end of if ($method eq 'readFromLogFile')
  244         #       Store the record of pin numbers in @pinNumbersArray
  245         #       This can be used to delete pin numbers in case of a failure.
  246 
  247         #       push(@pinNumbersArray, $PIN);
  248 
  249         #       Create a random security number
  250         #               $securityNumber=int rand(99999);
  251         #               print "security number is $securityNumber\n";
  252 
  253         #       Install the necessary variables into pinRecord
  254 
  255         &putStudentLastName     ($lastName, $PIN);
  256         &putStudentFirstName    ($firstName, $PIN);
  257         &putStudentID           ($studentID, $PIN);
  258         &putStudentLogin        ($login_name,$PIN);
  259         &putStudentStatus       ($status,$PIN);
  260         &putClassSection        ($section,$PIN);
  261         &putClassRecitation     ($recitation,$PIN);
  262         &putSetNumber           ($setNumber, $PIN);
  263         &putSetHeaderFileName   ($setHeaderFileName, $PIN);
  264         &putProbHeaderFileName   ($probHeaderFileName, $PIN);
  265         &putDueDate             ($dueDate, $PIN);
  266         &putOpenDate            ($openDate, $PIN);
  267         &putAnswerDate          ($answerDate, $PIN);
  268         &putStudentEmailAddress ($email_address, $PIN);
  269 
  270         my @seedList =();
  271         my ($probNumber,$probSeed);
  272         #       Generate the problems
  273         if ($method eq 'readFromLogFile')   {@seedsArrayFromLogFile = split /:/,$seedsHash{$login_name};}
  274         for($i=0; $i<@problemList; ++$i) {
  275           $probNumber=$i+1;
  276           &putProblemFileName     ($problemList[$i],$probNumber, $PIN);
  277 
  278           if ($method eq 'readFromLogFile')   {$probSeed = shift @seedsArrayFromLogFile;}
  279           else {$probSeed = int( rand(5000) );}
  280 
  281           push (@seedList,$probSeed);  ## put seed in array to be saved later in log file
  282           &putProblemSeed         ($probSeed,$probNumber, $PIN);
  283           &putProblemValue        ($problemValueList[$i],$probNumber, $PIN);
  284           &putProblemMaxNumOfIncorrectAttemps       ($problemAttemptLimitList[$i],$probNumber, $PIN);
  285           &putProblemAttempted(0,$probNumber, $PIN);
  286           &putProblemStatus(0,$probNumber, $PIN);
  287           &putProblemNumOfCorrectAns       (0,$probNumber, $PIN);
  288           &putProblemNumOfIncorrectAns     (0,$probNumber, $PIN);
  289         }
  290 
  291 
  292         if (&detachProbSetRecord($PIN) )  {
  293          # print "probSetKey $PIN for $firstName $lastName (login $login_name) inserted succesfully\n";
  294 
  295         if (
  296           ((defined $outputFormat) and ($outputFormat ne 'no_students')) or
  297           ($method eq 'readFromLogFile')
  298         )  {
  299           $message .= "probSetKey $PIN for $firstName $lastName (login $login_name) inserted succesfully\n";
  300         }
  301           $new_student_count++;
  302           #         print "$login_name $PIN @seedList \n";
  303           print LOGFILE "$login_name $PIN @seedList \n";
  304           }
  305         else {
  306          # print "Couldn't insert probSetKey $PIN\n";
  307           $message .= "Couldn't insert probSetKey $PIN\n";
  308           }#endif
  309         } # end if else
  310       }#end unless
  311     }#endforeachLoop
  312    # print "\n";
  313   close(LOGFILE);
  314    # print "DONE\n";
  315   $message .= "\nData entered for $new_student_count new student(s) in set $setNumber.\n";
  316   $message .= "Data left unchanged for $existing_student_count existing student(s) in set $setNumber.\n";
  317   $message .= "\nFINISHED BUILDING PROBLEM SET\n\n";
  318   $message;
  319 } ### end of sub buildProbSetDB
  320 
  321 1;
  322 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9