[system] / trunk / webwork / system / scripts / import_classlist-database.pl Repository:
ViewVC logotype

View of /trunk/webwork/system/scripts/import_classlist-database.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

    1 #!/usr/bin/perl -w
    2 
    3 ####################################################################
    4 # Copyright @ 1995-1999 University of Rochester
    5 # All Rights Reserved
    6 ####################################################################
    7 
    8 use lib '/ww/webwork/development/'; # mainWeBWorKDirectory;
    9 
   10 use Global;
   11 use Auth;
   12 use strict;
   13 use GDBM_File;
   14 
   15 if (@ARGV != 2) {
   16   print "\nSyntax is import_classlist-database.pl courseID textCL_Database\n";
   17   print "      (e.g. import_classlist-database.pl  demoCourse textDataBase)\n\n";
   18   exit(0);
   19 }
   20 
   21 sub getAns {
   22   my $ans = <STDIN>;
   23   $ans =~ s/^\s*//;
   24   $ans = substr($ans, 0, 1);
   25   return lc($ans);
   26 }
   27 print qq{
   28 
   29 This command line script will create a classlist database from a classlist file.
   30 It should only be used if no classlist database exists which means that you can not
   31 log into the course.  Usually this only occurs when setting up the demoCourse for
   32 a new WeBWorK system.  The most standard procedure for creating other new courses
   33 is to copy an existing course (e.g. the demoCourse) and in the process you copy the
   34 classlist database also.
   35 
   36 If you want to update (or even replace) an existing claslist database with data
   37 from a classlist file, go to the classlist page from the Professor page and use
   38 the web based "Import the classlist database from a classlist file". That procedure
   39 gives you much more control and updates all information.  This command line script
   40 will totally replace the classlist database (if it exists) with data from the
   41 classlist file but will not update any information in the webwork database (it will
   42 however attempt to prevent you from making major errors).
   43 
   44 Do you wish to continue with the command line script \? (y or n)
   45 };
   46 
   47 exit(0) if &getAns ne 'y';
   48 
   49 
   50 
   51 
   52 
   53 my $course = $ARGV[0];
   54 my $textCL_Database = $ARGV[1];
   55 
   56 # establish environment for this script
   57 
   58 &Global::getCourseEnvironment($course);
   59 
   60 # Directory paths
   61 
   62 my $databaseDirectory = getCourseDatabaseDirectory();
   63 my $scriptDirectory = getWebworkScriptDirectory();
   64 my $passwordFile = &Global::getCoursePasswordFile($course);
   65 my $permissionsFile = &Global::getCoursePermissionsFile($course);
   66 
   67 my $DELIM = $Global::delim;
   68 # File names
   69 
   70 require "${scriptDirectory}$Global::classlist_DBglue_pl";
   71 #  require "${scriptDirectory}$Global::HTMLglue_pl";
   72 require "${scriptDirectory}$Global::FILE_pl";
   73 
   74 my $msg = buildClasslistDB($course,$textCL_Database);
   75 print $msg;
   76 
   77 $msg = initial_passwords();
   78 print $msg;
   79 
   80 
   81 exit;  ## end of main script
   82 
   83 
   84 sub buildClasslistDB { ## builds the classlist DB and returns a message
   85 
   86   my ($classID,$classlistFilename) = @_;
   87   my $scriptDirectory   = $Global::scriptDirectory;
   88 
   89   &Global::getCourseEnvironment("$classID");
   90   my $databaseDirectory = $Global::databaseDirectory;
   91   my $templateDirectory = $Global::templateDirectory;
   92   my $CL_Database = $Global::CL_Database;
   93   my $path_to_CL_DB = "${databaseDirectory}$CL_Database";
   94 
   95   require "${scriptDirectory}$Global::DBglue_pl";
   96   require "${scriptDirectory}$Global::classlist_DBglue_pl";
   97   require "${scriptDirectory}$Global::FILE_pl";
   98 
   99   #get data from class list.
  100   my $fileName="${templateDirectory}$classlistFilename";  ## e.g. fileName=m161.lst
  101 
  102   my $message = "\nGetting class list from $fileName\n";
  103   checkClasslistFile($Global::noOfFieldsInClasslist,$fileName);
  104   open(FILE, "$fileName") || wwerror($0, "Can't open $fileName");
  105   my @classList=<FILE>;
  106   close(FILE);
  107 
  108   ###################################
  109   #  Before building the database we pause to check that the database file exists.
  110   #  If so, we back it up
  111   # (if not we create it).
  112   ###################################
  113   if ( -e "$path_to_CL_DB" ) {
  114 
  115     my $CL_status = get_CL_database_status();
  116     wwerror("Classlist Database is unlocked", "You must lock the classlist database (or move it)
  117 before you can import a new claslist database.") unless $CL_status eq 'locked';
  118 
  119     $message .= "Backing up current class list data base: $path_to_CL_DB\n\n";
  120     &backup($path_to_CL_DB);
  121   }
  122 
  123   $message .= "Creating new data base $path_to_CL_DB .\n";
  124   create_db("$path_to_CL_DB", $Global::webwork_database_permission);
  125   if ( -e "$path_to_CL_DB" ) {
  126     chmod($Global::webwork_database_permission,"$path_to_CL_DB") ||
  127              wwerror($0, "Can't do chmod($Global::webwork_database_permission,$path_to_CL_DB)");
  128     chown(-1,$Global::numericalGroupID,"$path_to_CL_DB")  ||
  129              wwerror($0,"Can't do chown(-1,$Global::numericalGroupID,$path_to_CL_DB)");
  130     $message .=   "New classlist data base created\n";
  131   }
  132   else {
  133       wwerror($0,"New classlist data base $path_to_CL_DB could not be created.");
  134   }
  135 
  136   my $WW_DB_exists = 0;
  137   $WW_DB_exists = 1 if ( -e "${databaseDirectory}$Global::database" );
  138 
  139   my %loginName_StudentID_Hash_from_WW_DB;
  140   my %studentID_LoginName_Hash_from_WW_DB;
  141 
  142   if ($WW_DB_exists) {
  143     %loginName_StudentID_Hash_from_WW_DB =%{getLoginName_StudentID_Hash_from_WW_DB()};
  144     %studentID_LoginName_Hash_from_WW_DB = reverse %loginName_StudentID_Hash_from_WW_DB;
  145   }
  146   foreach (@classList)     {                     ## read through classlist and create
  147                            ## class list database
  148     unless ($_ =~ /\S/)  {next;}                   ## skip blank lines
  149     chomp;
  150     my @classListRecord=&getRecord($_);
  151     my ($studentID, $lastName, $firstName, $status, $comment,  $section, $recitation, $email_address, $login_name)
  152         =  @classListRecord;
  153 
  154     if (($WW_DB_exists) and (defined $loginName_StudentID_Hash_from_WW_DB{$login_name})
  155       and ($loginName_StudentID_Hash_from_WW_DB{$login_name} ne $studentID)) {
  156       $message .= "\n      $firstName $lastName, $login_name, $studentID HAS NOT BEEN ENTERED
  157       IN THE CLASSLIST DATABASE  because of a conflict with entries in the WeBWorK problem set database.
  158       Enter this information again from the Add Student(s) Page to get a more detailed error message
  159       and instructions on how to correct the problem.";
  160       next;
  161     }
  162 
  163     if (($WW_DB_exists) and (defined $studentID_LoginName_Hash_from_WW_DB{$studentID})
  164       and ($studentID_LoginName_Hash_from_WW_DB{$studentID} ne $login_name)) {
  165             $message .= "\n$firstName $lastName, $login_name, $studentID HAS NOT BEEN ENTERED IN THE CLASSLIST DATABASE
  166      because of a conflict with entries in the WeBWorK problem set database.
  167      Enter this information again from the Add Student(s) Page to get a more detailed error message
  168      and instructions on how to correct the problem.\n";
  169       next;
  170     }
  171 
  172 
  173     &CL_putStudentID           ($studentID, $login_name);
  174     &CL_putStudentLastName     ($lastName, $login_name);
  175     &CL_putStudentFirstName    ($firstName, $login_name);
  176     &CL_putStudentStatus       ($status, $login_name);
  177     &CL_putComment             ($comment, $login_name);
  178     &CL_putClassSection        ($section,$login_name);
  179     &CL_putClassRecitation     ($recitation,$login_name);
  180     &CL_putStudentEmailAddress ($email_address, $login_name);
  181 
  182     &saveCLRecord($login_name);
  183   }
  184 unlock_CL_database();
  185 $message;
  186 }
  187 
  188 sub backup  {
  189   ## takes as a parameter the full path name
  190   ## makes upto two backups of the file with _bak1, or _bak2
  191   ## appended to filename where _bak1 is the most recent backup
  192 
  193   my $fileName =$_[0];
  194 
  195   if (-e "${fileName}_bak1") {
  196     rename("${fileName}_bak1","${fileName}_bak2") or
  197       &wwerror("$0","can't rename ${fileName}_bak1");
  198   }
  199 
  200   if (-e "${fileName}") {
  201     rename("${fileName}","${fileName}_bak1") or
  202       &wwerror("$0","can't rename ${fileName}");
  203   }
  204 }
  205 
  206 
  207 sub initial_passwords {
  208   my %studentsinclass=();
  209   my @classListRecord=();
  210   my $msg ='';
  211 
  212   # Check that the files exist:
  213   #    The permissions file must exist and have both read and write privilages.
  214   #    The permissions file must exist and have both read and write privilages.
  215 
  216 
  217   unless ( -r "${databaseDirectory}$Global::CL_Database" ) {
  218     wwerror ($0, "Can't read the class list database file ${databaseDirectory}$Global::CL_Database");
  219     }
  220 
  221   unless ( -e $permissionsFile) {
  222     &create_db($permissionsFile,$Global::standard_tie_permission);
  223     $msg .= "Permissions file does not exist.\n Creating permissions file:\n   $permissionsFile\n";
  224     }
  225 
  226   unless ( -e $passwordFile) {
  227     &create_db($passwordFile,$Global::standard_tie_permission);
  228     $msg .= "Password file does not exist.\n Creating password file:\n   $passwordFile\n";
  229     }
  230 
  231   unless ( -r $passwordFile and -w $passwordFile) {
  232     wwerror ($0, "Permissions set incorrectly on $passwordFile or its directory.
  233        Cannot access file to both read and write.");
  234     }
  235 
  236   unless ( -r $permissionsFile and -w $permissionsFile) {
  237     wwerror ($0, "Permissions set incorrectly on $permissionsFile or its directory.
  238        Cannot access file to both read and write.");
  239     }
  240 
  241 
  242 
  243 
  244 
  245   my $login_name;
  246 
  247   $msg .= "\nLoading classlist database: ${databaseDirectory}$Global::CL_Database\n\n";
  248   my @classList = @{getAllLoginNames()};
  249 
  250   $msg .= "\n Modifying the password file :\n   $passwordFile\n\n";
  251 
  252   foreach $login_name (@classList)   {                  ## read through classlist database and create
  253                              ## problems for all active students
  254                              ## except if problems already exist for
  255     attachCLRecord($login_name);
  256                            ## student
  257 
  258     my $status    = CL_getStudentStatus($login_name);
  259     my $studentID   = CL_getStudentID($login_name);
  260 
  261     $studentsinclass{$login_name}++ unless(&dropStatus($status));
  262 
  263     if(&dropStatus($status)) {
  264       $msg .= "      $login_name not added because status is $status\n";
  265     }
  266     elsif (&get_password($login_name, $passwordFile)) {
  267       $msg .= "   $login_name not added because password already exists\n";
  268     }
  269     else {
  270       &new_password($login_name, $studentID, $passwordFile);
  271       &put_permissions(0,$login_name,$permissionsFile);
  272       $msg .= "added: $login_name, $studentID\n";
  273     }
  274   }
  275 
  276   my @pwStudents = &get_keys_from_db($passwordFile);
  277   my ($ans,$student);
  278 
  279 
  280   $msg .= "\n The following login's (if any) in the password and permissions databases are either\n";
  281   $msg .= "    (1) not listed  in the class list database file \n";
  282   $msg .= "      ${databaseDirectory}$Global::CL_Database or\n";
  283   $msg .= "    (2) have DROP status in the class list database file.\n";
  284   $msg .= "They will all be removed from the password and permissions databases.\n\n";
  285 
  286   foreach $student (@pwStudents) {
  287     next if defined($studentsinclass{$student});
  288 
  289     &delete_password($student,$passwordFile);
  290     &delete_permissions($student,$permissionsFile);
  291     $msg .= "         $student removed from password and permissions databases\n";
  292   }
  293 
  294   ## if the owner of the password file is running this script (e.g. when the password file is first created)
  295   ## set the permissions correctly
  296 
  297   open (PASSWORDFILE, "$passwordFile") or wwerror($0, "Can't open $passwordFile");
  298   my @stat = stat PASSWORDFILE;
  299   close PASSWORDFILE;
  300 
  301   if ($< == $stat[4]) {
  302 
  303     chmod($Global::password_permission, $passwordFile) or
  304                  wwerror($0, "Can't do chmod($Global::password_permission, $passwordFile)");
  305     chown(-1,$Global::numericalGroupID,$passwordFile)  or
  306                  wwerror($0, "Can't do chown(-1,$Global::numericalGroupID,$passwordFile)");
  307   }
  308 
  309   open (PERMISSIONSFILE, "$permissionsFile") or wwerror($0, "Can't open $permissionsFile");
  310   @stat = stat PERMISSIONSFILE;
  311   close PERMISSIONSFILE;
  312 
  313   if ($< == $stat[4]) {
  314 
  315     chmod($Global::permissions_permission, $permissionsFile) or
  316                 wwerror($0, "Can't do chmod($Global::permissions_permission, $permissionsFile)");
  317     chown(-1,$Global::numericalGroupID,$permissionsFile)  or
  318                 wwerror($0, "Can't do chown(-1,$Global::numericalGroupID,$permissionsFile)");
  319   }
  320 $msg;
  321 }

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9