-
-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathaddcourse
More file actions
executable file
·159 lines (116 loc) · 4.28 KB
/
Copy pathaddcourse
File metadata and controls
executable file
·159 lines (116 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env perl
=head1 NAME
addcourse - add a course
=head1 SYNOPSIS
addcourse [options] COURSEID
=head1 DESCRIPTION
Add a course to the courses directory. The required directories will be created.
Optionally, a database can be populated with users. Also, one or more users can
be granted professor privileges.
=head1 OPTIONS
=over
=item B<--users>=I<FILE>
The users listed in the comma-separated text file I<FILE> will be added to the
user list of the new course. The format of this file is the same as user lists
exported from WeBWorK.
=item B<--professors>=I<USERID>[,I<USERID>]...
Each I<USERID>, if it is present in the new course's user list, will be granted
professor privileges (i.e. a permission level of 10). Requires B<--users>.
=item B<--templates-from>=I<COURSEID>
If specified, the contents of the specified course's templates directory are
used to populate the new course's templates directory.
=item I<COURSEID>
The name of the course to create.
=back
=cut
use strict;
use warnings;
use Getopt::Long;
BEGIN {
use Mojo::File qw(curfile);
use Env qw(WEBWORK_ROOT);
$WEBWORK_ROOT = curfile->dirname->dirname;
}
# link to WeBWorK and pg code libraries
use lib "$ENV{WEBWORK_ROOT}/lib";
use WeBWorK::CourseEnvironment;
use WeBWorK::File::Classlist;
use WeBWorK::Utils qw(runtime_use cryptPassword);
use WeBWorK::Utils::CourseManagement qw(addCourse);
use WeBWorK::File::Classlist qw(parse_classlist);
use WeBWorK::DB::Record::User;
use WeBWorK::DB::Record::Password;
use WeBWorK::DB::Record::PermissionLevel;
sub usage_error {
warn "@_\n";
warn "usage: $0 [options] COURSEID\n";
warn "Options:\n";
warn " [--users=FILE [--professors=USERID[,USERID]...] ]\n";
exit;
}
my ($users, $templates_from) = ('', '');
my @professors;
GetOptions(
"users=s" => \$users,
"professors=s" => \@professors,
"templates-from=s" => \$templates_from,
);
my %professors = map { $_ => 1 } map { split /,/ } @professors;
my $courseID = shift;
usage_error('The COURSEID must be provided.') unless $courseID;
my $ce = WeBWorK::CourseEnvironment->new({ courseName => $courseID });
die "Aborting addcourse: Course ID cannot exceed $ce->{maxCourseIdLength} characters."
if length($courseID) > $ce->{maxCourseIdLength};
usage_error("Can't specify --professors without also specifying --users.")
if @professors && !$users;
my @users;
if ($users) {
my @classlist = parse_classlist($users);
for my $record (@classlist) {
my %record = %$record;
my $user_id = $record{user_id};
# Set the default status if the status field is not set.
$record{status} = $ce->{statuses}{Enrolled}{abbrevs}[0] unless $record{status};
# Determine what to use for the password (if anything).
if (!$record{password}) {
if (defined $record{unencrypted_password} && $record{unencrypted_password} =~ /\S/) {
$record{password} = cryptPassword($record{unencrypted_password});
} elsif ($ce->{fallback_password_source}
&& { user_id => 1, first_name => 1, last_name => 1, student_id => 1 }
->{ $ce->{fallback_password_source} }
&& $record{ $ce->{fallback_password_source} }
&& $record{ $ce->{fallback_password_source} } =~ /\S/)
{
$record{password} = cryptPassword($record{ $ce->{fallback_password_source} });
}
}
# Set permission
if (!$record{status} && defined $professors{$user_id}) {
$record{permission} = $ce->{userRoles}{professor};
}
$record{accommodation_time_factor} = 1;
delete $professors{$user_id};
push @users,
[
WeBWorK::DB::Record::User->new(%record),
WeBWorK::DB::Record::Password->new(user_id => $user_id, password => $record{password}),
WeBWorK::DB::Record::PermissionLevel->new(
user_id => $user_id,
permission => defined $professors{$user_id}
? $ce->{userRoles}{professor}
: ($record{permission} // $ce->{default_permission_level})
)
];
}
if (my @ids = keys %professors) {
warn "Warning: The following users were not in the imported list:\n" . join("\n", @ids) . "\n";
}
}
my %optional_arguments;
if ($templates_from) {
$optional_arguments{copyFrom} = $templates_from;
$optional_arguments{copyTemplatesHtml} = 1;
}
eval { addCourse(courseID => $courseID, ce => $ce, users => \@users, %optional_arguments,); };
die "$@\n" if $@;
print qq{Successfully added "$courseID" course.\n};