Parent Directory
|
Revision Log
UPDATE YOUR CONFIG FILES! (global.conf.dist, database.conf.dist) Made changes to support the storage of multiple database layouts in a single course environment. Database layouts are now stored in a hash named %dbLayouts in database.conf, and the default layout is aliased to *dbLayout (see comments in database.conf.dist and global.conf.dist) sql.conf and gdbm.conf are no longer used. Support the selection of a specific database layout when instantiating WeBWorK::DB. WeBWorK::DB now takes a reference to a database layout rather than an entire course environment. All calls to WeBWorK::DB::new were updated to pass the correct argument.
1 #!/usr/bin/env perl 2 ################################################################################ 3 # WeBWorK Online Homework Delivery System 4 # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/ 5 # $CVSHeader: webwork-modperl/bin/addcourse,v 1.4 2003/12/27 21:41:56 sh002i Exp $ 6 # 7 # This program is free software; you can redistribute it and/or modify it under 8 # the terms of either: (a) the GNU General Public License as published by the 9 # Free Software Foundation; either version 2, or (at your option) any later 10 # version, or (b) the "Artistic License" which comes with this package. 11 # 12 # This program is distributed in the hope that it will be useful, but WITHOUT 13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 14 # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the 15 # Artistic License for more details. 16 ################################################################################ 17 18 =head1 NAME 19 20 addcourse - add a course 21 22 =head1 SYNOPSIS 23 24 addcourse [options] COURSEID 25 26 =head1 DESCRIPTION 27 28 Add a course to the courses directory. The required directories will be 29 created. Optionally, a database can be populated with users and the 30 F<templates> directory can be populated with the contents of another directory. 31 Also, one or more users can be granted professor privileges. 32 33 =head1 OPTIONS 34 35 =over 36 37 =item B<--templates>=I<DIR> 38 39 The contents of the directory I<DIR> will be copied to the F<templates> 40 directory of the new course. 41 42 =item B<--db-layout>=I<LAYOUT> 43 44 The specified database layout will be used in place of the default specified in 45 F<global.conf>. 46 47 =item B<--users>=I<FILE> 48 49 The users listed in the comma-separated text file I<FILE> will be added to the 50 user list of the new course. The format of this file is the same as user lists 51 exported from WeBWorK. 52 53 =item B<--professors>=I<USERID>[,I<USERID>]... 54 55 Each I<USERID>, if it is present in the new course's user list, will be granted 56 professor privileges (i.e. a permission level of 10). Requires B<--users>. 57 58 =item I<COURSEID> 59 60 The name of the course to create. 61 62 =back 63 64 =cut 65 66 use strict; 67 use warnings; 68 use Getopt::Long; 69 70 BEGIN { 71 die "WEBWORK_ROOT not found in environment.\n" 72 unless exists $ENV{WEBWORK_ROOT}; 73 } 74 75 use lib "$ENV{WEBWORK_ROOT}/lib"; 76 use WeBWorK::CourseEnvironment; 77 use WeBWorK::DB; 78 use WeBWorK::Utils qw(readFile cryptPassword); 79 80 sub usage { 81 print STDERR "$0 [options] COURSEID\n"; 82 print STDERR " [--templates=DIR] [--db-layout=LAYOUT]\n"; 83 print STDERR " [--users=FILE [--professors=USERID[,USERID]...] ]\n"; 84 exit; 85 } 86 87 my $templates = ""; 88 my $dbLayout = ""; 89 my $users = ""; 90 my @professors = (); 91 92 GetOptions( 93 "templates=s" => \$templates, 94 "db-layout=s" => \$dbLayout, 95 "users=s" => \$users, 96 "professors=s" => \@professors, 97 ); 98 my %professors = map { $_ => 1 } map { split /,/ } @professors; 99 my $courseID = shift; 100 101 unless ($courseID) { 102 print STDERR "$0: must specify COURSEID.\n"; 103 usage(); 104 }; 105 106 if (@professors and not $users) { 107 print STDERR "$0: can't specify --professors without also specifying --users.\n"; 108 usage(); 109 } 110 111 # bring up a minimal course environment 112 my $ce = WeBWorK::CourseEnvironment->new($ENV{WEBWORK_ROOT}, "FAKE_URL_ROOT", 113 "FAKE_PG_ROOT", $courseID); 114 115 if ($dbLayout) { 116 die "Database layout $dbLayout does not exist in the course environment.", 117 " (It must be defined in global.conf.)\n" 118 unless exists $ce->{dbLayouts}->{$dbLayout}; 119 } 120 121 # collect some data 122 my $coursesDir = $ce->{webworkDirs}->{courses}; 123 my $courseDir = "$coursesDir/$courseID"; 124 125 # make sure the course doesn't already exist 126 if (-e $courseDir) { 127 die "$courseID: course exists\n"; 128 } 129 130 # create required directories 131 my @subDirs = sort values %{ $ce->{courseDirs} }; 132 foreach my $subDir (@subDirs) { 133 print "mkdir $subDir\n"; 134 mkdir "$subDir" 135 or die "Failed to create course directory $subDir: $!\n"; 136 } 137 138 if ($templates) { 139 unless (-d "$courseDir/templates") { 140 warn "$courseDir/templates: not found, creating:\n"; 141 print "mkdir $courseDir/templates\n"; 142 mkdir "$courseDir/templates" 143 or die "Failed to mkdir $courseDir/templates: $!\n"; 144 } 145 print "copy $templates/* -> $courseDir/templates\n"; 146 system "/bin/cp -r $templates/* $courseDir/templates/" 147 and die "Failed to copy $templates/* to $courseDir/templates: $!\n"; 148 } 149 150 if ($users) { 151 # import users - much of this code is burgled from UserList.pm 152 153 my $db; 154 if ($dbLayout) { 155 # use the specified layout 156 $db = WeBWorK::DB->new($ce->{dbLayouts}->{$dbLayout}); 157 } else { 158 # use the default layout 159 $db = WeBWorK::DB->new($ce->{dbLayout}); 160 } 161 162 my @contents = split /\n/, readFile($users); 163 164 foreach my $string (@contents) { 165 $string =~ s/^\s+//; 166 $string =~ s/\s+$//; 167 my ( 168 $student_id, $last_name, $first_name, $status, $comment, 169 $section, $recitation, $email_address, $user_id 170 ) = split /\s*,\s*/, $string; 171 172 my $User = $db->newUser; 173 $User->user_id($user_id); 174 $User->first_name($first_name); 175 $User->last_name($last_name); 176 $User->email_address($email_address); 177 $User->student_id($student_id); 178 $User->status($status); 179 $User->section($section); 180 $User->recitation($recitation); 181 $User->comment($comment); 182 183 my $PermissionLevel = $db->newPermissionLevel; 184 $PermissionLevel->user_id($user_id); 185 if (exists $professors{$user_id}) { 186 $PermissionLevel->permission(10); 187 } else { 188 $PermissionLevel->permission(0); 189 } 190 191 my $Password = $db->newPassword; 192 $Password->user_id($user_id); 193 $Password->password(cryptPassword($student_id)); 194 195 $db->addUser($User); 196 $db->addPermissionLevel($PermissionLevel); 197 $db->addPassword($Password); 198 199 if (exists $professors{$user_id}) { 200 print "add professor $user_id\n"; 201 delete $professors{$user_id}; 202 } else { 203 print "add user $user_id\n"; 204 } 205 } 206 207 if (my @ids = keys %professors) { 208 print STDERR "warning: @ids not in imported user list.\n"; 209 } 210 } 211 212 =head1 BUGS 213 214 Databases are created using the database layout specified in the global 215 configuration file (F<global.conf>), which requires users to temporarily modify 216 their system's configuration in order to create a course with a nonstandard 217 database layout. 218 219 Also, some database drivers are unable to create storage for their data. The 220 GDBM backend can do this, but the SQL backend cannot (currently). 221 222 =head1 AUTHOR 223 224 Written by Sam Hathaway, hathaway at users.sourceforge.net. 225 226 =cut
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |