Parent Directory
|
Revision Log
Revision 1653 - (view) (download)
| 1 : | sh002i | 1653 | #!/usr/bin/env perl |
| 2 : | |||
| 3 : | ################################################################################ | ||
| 4 : | # WeBWorK mod_perl (c) 1995-2002 WeBWorK Team, Univeristy of Rochester | ||
| 5 : | # $Id: addcourse,v 1.1 2003-12-04 06:03:25 sh002i Exp $ | ||
| 6 : | ################################################################################ | ||
| 7 : | |||
| 8 : | =head1 NAME | ||
| 9 : | |||
| 10 : | addcourse - add a course | ||
| 11 : | |||
| 12 : | =head1 SYNOPSIS | ||
| 13 : | |||
| 14 : | addcourse [B<--users>=I<FILE> [B<--professors>=I<USERID>[,I<USERID>]...] ] | ||
| 15 : | [B<--templates>=I<DIR>] I<COURSEID> | ||
| 16 : | |||
| 17 : | =head1 DESCRIPTION | ||
| 18 : | |||
| 19 : | Add a course to the courses directory. The required directories will be | ||
| 20 : | created. Optionally, a database can be populated with users and the | ||
| 21 : | F<templates> directory can be populated with the contents of another directory. | ||
| 22 : | Also, one or more users can be granted professor privileges. | ||
| 23 : | |||
| 24 : | =head1 OPTIONS | ||
| 25 : | |||
| 26 : | =over | ||
| 27 : | |||
| 28 : | =item B<--users>=I<FILE> | ||
| 29 : | |||
| 30 : | The users listed in the comma-separated text file I<FILE> will be added to the | ||
| 31 : | user list of the new course. | ||
| 32 : | |||
| 33 : | =item B<--professors>=I<USERID>[,I<USERID>]... | ||
| 34 : | |||
| 35 : | Each I<USERID>, if it is present in the new course's user list, will be granted | ||
| 36 : | professor privileges (i.e. a permission level of 10). Requires B<--users>. | ||
| 37 : | |||
| 38 : | =item B<--templates>=I<DIR> | ||
| 39 : | |||
| 40 : | The contents of the directory I<DIR> will be copied to the F<templates> | ||
| 41 : | directory of the new course. | ||
| 42 : | |||
| 43 : | =item I<COURSEID> | ||
| 44 : | |||
| 45 : | The name of the course to create. | ||
| 46 : | |||
| 47 : | =cut | ||
| 48 : | |||
| 49 : | use strict; | ||
| 50 : | use warnings; | ||
| 51 : | use FindBin; | ||
| 52 : | use Getopt::Long; | ||
| 53 : | use lib "$FindBin::Bin/../lib"; | ||
| 54 : | use WeBWorK::CourseEnvironment; | ||
| 55 : | use WeBWorK::DB; | ||
| 56 : | use WeBWorK::Utils qw/readFile/; | ||
| 57 : | |||
| 58 : | sub usage { | ||
| 59 : | print STDERR "$0 [--users=FILE [--professors=USERID[,USERID]...] ]\n"; | ||
| 60 : | print STDERR "[--templates=DIR] COURSEID\n"; | ||
| 61 : | exit; | ||
| 62 : | } | ||
| 63 : | |||
| 64 : | my $users = ""; | ||
| 65 : | my @professors = (); | ||
| 66 : | my $templates = ""; | ||
| 67 : | |||
| 68 : | GetOptions( | ||
| 69 : | "users=s" => \$users, | ||
| 70 : | "professors=s" => \@professors, | ||
| 71 : | "templates=s" => \$templates, | ||
| 72 : | ); | ||
| 73 : | my %professors = map { $_ => 1 } map { split /,/ } @professors; | ||
| 74 : | my $courseID = shift; | ||
| 75 : | |||
| 76 : | #print "users=$users\n"; | ||
| 77 : | #print "professors=@professors\n"; | ||
| 78 : | #print "templates=$templates\n"; | ||
| 79 : | #print "courseID=$courseID\n"; | ||
| 80 : | |||
| 81 : | unless ($ENV{WEBWORK_ROOT}) { | ||
| 82 : | die "WEBWORK_ROOT not found in environment.\n"; | ||
| 83 : | } | ||
| 84 : | |||
| 85 : | unless ($courseID) { | ||
| 86 : | print STDERR "$0: must specify COURSEID.\n"; | ||
| 87 : | usage(); | ||
| 88 : | }; | ||
| 89 : | |||
| 90 : | if (@professors and not $users) { | ||
| 91 : | print STDERR "$0: can't specify --professors without also specifying --users.\n"; | ||
| 92 : | usage(); | ||
| 93 : | } | ||
| 94 : | |||
| 95 : | # bring up a minimal course environment | ||
| 96 : | my $ce = WeBWorK::CourseEnvironment->new($ENV{WEBWORK_ROOT}, "FAKE_URL_ROOT", | ||
| 97 : | "FAKE_PG_ROOT", $courseID); | ||
| 98 : | |||
| 99 : | # collect some data | ||
| 100 : | my $coursesDir = $ce->{webworkDirs}->{courses}; | ||
| 101 : | my $courseDir = "$coursesDir/$courseID"; | ||
| 102 : | |||
| 103 : | # create course directory | ||
| 104 : | #print "mkdir $courseDir\n"; | ||
| 105 : | #mkdir $courseDir or die "Failed to create course directory: $!\n"; | ||
| 106 : | |||
| 107 : | # populate it with some subdirectories | ||
| 108 : | my @subDirs = sort values %{ $ce->{courseDirs} }; | ||
| 109 : | foreach my $subDir (@subDirs) { | ||
| 110 : | print "mkdir $subDir\n"; | ||
| 111 : | mkdir "$subDir" | ||
| 112 : | or die "Failed to create course directory $subDir: $!\n"; | ||
| 113 : | } | ||
| 114 : | |||
| 115 : | if ($users) { | ||
| 116 : | # import users - much of this code is burgled from UserList.pm | ||
| 117 : | |||
| 118 : | my $db = WeBWorK::DB->new($ce); | ||
| 119 : | my @contents = split /\n/, readFile($users); | ||
| 120 : | |||
| 121 : | foreach my $string (@contents) { | ||
| 122 : | $string =~ s/^\s+//; | ||
| 123 : | $string =~ s/\s+$//; | ||
| 124 : | my ( | ||
| 125 : | $student_id, $last_name, $first_name, $status, $comment, | ||
| 126 : | $section, $recitation, $email_address, $user_id | ||
| 127 : | ) = split /\s*,\s*/, $string; | ||
| 128 : | |||
| 129 : | my $User = $db->newUser; | ||
| 130 : | $User->user_id($user_id); | ||
| 131 : | $User->first_name($first_name); | ||
| 132 : | $User->last_name($last_name); | ||
| 133 : | $User->email_address($email_address); | ||
| 134 : | $User->student_id($student_id); | ||
| 135 : | $User->status($status); | ||
| 136 : | $User->section($section); | ||
| 137 : | $User->recitation($recitation); | ||
| 138 : | $User->comment($comment); | ||
| 139 : | |||
| 140 : | my $PermissionLevel = $db->newPermissionLevel; | ||
| 141 : | $PermissionLevel->user_id($user_id); | ||
| 142 : | if (exists $professors{$user_id}) { | ||
| 143 : | $PermissionLevel->permission(10); | ||
| 144 : | } else { | ||
| 145 : | $PermissionLevel->permission(0); | ||
| 146 : | } | ||
| 147 : | |||
| 148 : | my $Password = $db->newPassword; | ||
| 149 : | $Password->user_id($user_id); | ||
| 150 : | $Password->password($student_id); | ||
| 151 : | |||
| 152 : | $db->addUser($User); | ||
| 153 : | $db->addPermissionLevel($PermissionLevel); | ||
| 154 : | $db->addPassword($Password); | ||
| 155 : | |||
| 156 : | if (exists $professors{$user_id}) { | ||
| 157 : | print "add professor $user_id\n"; | ||
| 158 : | delete $professors{$user_id}; | ||
| 159 : | } else { | ||
| 160 : | print "add user $user_id\n"; | ||
| 161 : | } | ||
| 162 : | } | ||
| 163 : | |||
| 164 : | if (my @ids = keys %professors) { | ||
| 165 : | print STDERR "warning: @ids not in imported user list.\n"; | ||
| 166 : | } | ||
| 167 : | } | ||
| 168 : | |||
| 169 : | if ($templates) { | ||
| 170 : | unless (-d "$courseDir/templates") { | ||
| 171 : | warn "$courseDir/templates: not found, creating:\n"; | ||
| 172 : | print "mkdir $courseDir/templates\n"; | ||
| 173 : | mkdir "$courseDir/templates" | ||
| 174 : | or die "Failed to mkdir $courseDir/templates: $!\n"; | ||
| 175 : | } | ||
| 176 : | print "copy $templates/* -> $courseDir/templates\n"; | ||
| 177 : | system "/bin/cp -r $templates/* $courseDir/templates/" | ||
| 178 : | and die "Failed to copy $templates/* to $courseDir/templates: $!\n"; | ||
| 179 : | } | ||
| 180 : | |||
| 181 : | =head1 BUGS | ||
| 182 : | |||
| 183 : | Databases are created using the database layout specified in the global | ||
| 184 : | configuration file (F<global.conf>), which requires users to temporarily modify | ||
| 185 : | their system's configuration in order to create a course with a nonstandard | ||
| 186 : | database layout. | ||
| 187 : | |||
| 188 : | Also, some database drivers are unable to create storage for their data. The | ||
| 189 : | GDBM backend can do this, but the SQL backend cannot (currently). | ||
| 190 : | |||
| 191 : | =head1 AUTHOR | ||
| 192 : | |||
| 193 : | Written by Sam Hathaway, hathaway at users.sourceforge.net. | ||
| 194 : | |||
| 195 : | =cut |
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |