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