[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 240 - (download) (as text) (annotate)
Fri Nov 2 23:38:13 2001 UTC (18 years, 1 month ago) by sh002i
File size: 11481 byte(s)
Re-wrapped intro text so that it's less than 80 columns wide.

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9