Parent Directory
|
Revision Log
This commit was manufactured by cvs2svn to create branch 'rel-2-4-patches'.
1 #!/usr/bin/env perl 2 ################################################################################ 3 # WeBWorK Online Homework Delivery System 4 # Copyright © 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/ 5 # $CVSHeader: webwork2/bin/addcourse,v 1.20 2006/12/09 03:29: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 created. 29 Optionally, a database can be populated with users. Also, one or more users can 30 be granted professor privileges. 31 32 =head1 OPTIONS 33 34 =over 35 36 =item B<--db-layout>=I<LAYOUT> 37 38 The specified database layout will be used in place of the default specified in 39 F<global.conf>. 40 41 =item B<--users>=I<FILE> 42 43 The users listed in the comma-separated text file I<FILE> will be added to the 44 user list of the new course. The format of this file is the same as user lists 45 exported from WeBWorK. 46 47 =item B<--professors>=I<USERID>[,I<USERID>]... 48 49 Each I<USERID>, if it is present in the new course's user list, will be granted 50 professor privileges (i.e. a permission level of 10). Requires B<--users>. 51 52 =item B<--templates-from>=I<COURSEID> 53 54 If specified, the contents of the specified course's templates directory are 55 used to populate the new course's templates directory. 56 57 =item I<COURSEID> 58 59 The name of the course to create. 60 61 =back 62 63 =cut 64 65 BEGIN { 66 # hide arguments (there could be passwords there!) 67 $0 = "$0"; 68 } 69 70 use strict; 71 use warnings; 72 use Getopt::Long; 73 74 BEGIN { 75 die "WEBWORK_ROOT not found in environment.\n" 76 unless exists $ENV{WEBWORK_ROOT}; 77 } 78 79 use lib "$ENV{WEBWORK_ROOT}/lib"; 80 use WeBWorK::CourseEnvironment; 81 use WeBWorK::DB; 82 use WeBWorK::File::Classlist; 83 use WeBWorK::Utils qw(runtime_use readFile cryptPassword); 84 use WeBWorK::Utils::CourseManagement qw(addCourse deleteCourse listCourses); 85 86 sub usage { 87 print STDERR "usage: $0 [options] COURSEID\n"; 88 print STDERR "Options:\n"; 89 print STDERR " [--db-layout=LAYOUT]\n"; 90 print STDERR " [--users=FILE [--professors=USERID[,USERID]...] ]\n"; 91 exit; 92 } 93 94 sub usage_error { 95 print STDERR "$0: @_\n"; 96 usage(); 97 } 98 99 my $dbLayout = ""; 100 my $sql_host = ""; 101 my $sql_port = ""; 102 my $sql_user = ""; 103 my $sql_pass = ""; 104 my $sql_db = ""; 105 my $sql_wwhost = ""; 106 my $globalUserID = ""; 107 my $users = ""; 108 my @professors = (); 109 my $templates_from = ""; 110 111 ##### get command-line options ##### 112 113 GetOptions( 114 "db-layout=s" => \$dbLayout, 115 "sql-host=s" => \$sql_host, 116 "sql-port=s" => \$sql_port, 117 "sql-user=s" => \$sql_user, 118 "sql-pass=s" => \$sql_pass, 119 "sql-db=s" => \$sql_db, 120 "sql-wwhost=s" => \$sql_wwhost, 121 "global-user=s" => \$globalUserID, 122 "users=s" => \$users, 123 "professors=s" => \@professors, 124 "templates-from=s" => \$templates_from, 125 ); 126 my %professors = map { $_ => 1 } map { split /,/ } @professors; 127 my $courseID = shift; 128 129 ##### perform sanity checks ##### 130 131 usage_error("must specify COURSEID.") unless $courseID; 132 133 # bring up a minimal course environment 134 my $ce = WeBWorK::CourseEnvironment->new({ 135 webwork_dir => $ENV{WEBWORK_ROOT}, 136 courseName => $courseID 137 }); 138 139 if ($dbLayout) { 140 die "Database layout $dbLayout does not exist in the course environment.", 141 " (It must be defined in global.conf.)\n" 142 unless exists $ce->{dbLayouts}->{$dbLayout}; 143 } else { 144 # use default value 145 $dbLayout = $ce->{dbLayoutName}; 146 } 147 148 usage_error("can't specify --professors without also specifying --users.") 149 if @professors and not $users; 150 151 ##### set up parameters to pass to addCourse() ##### 152 153 my %courseOptions = ( dbLayoutName => $dbLayout ); 154 155 # this is kinda left over from when we had 'gdbm' and 'sql' database layouts 156 # below this line, we would grab values from getopt and put them in this hash 157 # but for now the hash can remain empty 158 my %dbOptions; 159 160 my @users; 161 if ($users) { 162 # this is a hack to create records without bringing up a DB object 163 #my $db = WeBWorK::DB->new($ce->{dbLayouts}->{$dbLayout}); 164 my $userClass = $ce->{dbLayouts}->{$dbLayout}->{user}->{record}; 165 my $passwordClass = $ce->{dbLayouts}->{$dbLayout}->{password}->{record}; 166 my $permissionClass = $ce->{dbLayouts}->{$dbLayout}->{permission}->{record}; 167 168 runtime_use($userClass); 169 runtime_use($passwordClass); 170 runtime_use($permissionClass); 171 172 # Default status is enrolled -- fetch abbreviation for enrolled 173 my $default_status_abbrev = $ce->{statuses}->{Enrolled}->{abbrevs}->[0]; 174 175 # default permission level 176 my $default_permission_level = $ce->{default_permission_level}; 177 178 my @classlist = parse_classlist($users); 179 foreach my $record (@classlist) { 180 my %record = %$record; 181 my $user_id = $record{user_id}; 182 183 # set default status is status field is "empty" 184 $record{status} = $default_status_abbrev 185 unless defined $record{status} and $record{status} ne ""; 186 187 # set password from student ID if password field is "empty" 188 if (not defined $record{password} or $record{password} eq "") { 189 if (defined $record{student_id} and $record{student_id} ne "") { 190 # crypt the student ID and use that 191 $record{password} = cryptPassword($record{student_id}); 192 } else { 193 # an empty password field in the database disables password login 194 $record{password} = ""; 195 } 196 } 197 198 # set default permission level if pedefault_permission_levelrmission level is "empty" 199 if (not defined $record{status} and $record{status} ne "") { 200 if (exists $professors{$user_id}) { 201 $record{permission} = $ce->{userRoles}{professor}; # FIXME hardcoded role name is bad 202 } else { 203 $record{permission} = $default_permission_level; 204 } 205 } 206 207 my $User = $userClass->new(%record); 208 my $PermissionLevel = $permissionClass->new(user_id => $user_id, permission => $record{permission}); 209 my $Password = $passwordClass->new(user_id => $user_id, password => $record{password}); 210 211 if (exists $record{permission}) { 212 $PermissionLevel->permission($record{permission}); 213 delete $professors{$user_id}; 214 } elsif (exists $professors{$user_id}) { 215 $PermissionLevel->permission(10); 216 delete $professors{$user_id}; 217 } 218 219 if (exists $record{password}) { 220 $Password->password($record{password}); 221 } 222 223 push @users, [ $User, $Password, $PermissionLevel ]; 224 } 225 226 if (my @ids = keys %professors) { 227 print STDERR "warning: @ids not in imported user list.\n"; 228 } 229 } 230 231 my %optional_arguments; 232 if ($templates_from ne "") { 233 $optional_arguments{templatesFrom} = $templates_from; 234 } 235 236 ##### call addCourse(), handle errors ##### 237 238 eval { 239 addCourse( 240 courseID => $courseID, 241 ce => $ce, 242 courseOptions => \%courseOptions, 243 dbOptions => \%dbOptions, 244 users => \@users, 245 %optional_arguments, 246 ); 247 }; 248 249 if ($@) { 250 my $error = $@; 251 print STDERR "$error\n"; 252 exit; 253 } 254 255 =head1 AUTHOR 256 257 Written by Sam Hathaway, hathaway at users.sourceforge.net. 258 259 =cut
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |