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