Skip to content

Commit

Permalink
Allow adding multiple initial users when adding a new course.
Browse files Browse the repository at this point in the history
When adding a new course, there is now an 'Add Another Instructor' button
which can be used to add more than one new user to the newly created course.
Each time this button is hit, the form expands allowing input for a new user.
It is possible to set the permission level of each of these users and to keep
the form balanced, the student ID can now also be included.

This also updates assigning the initial user to sets and achievements. The
intention was to assign new instructors to these, so any user (either copied
from the admin course or added initially) whose permission is less than admin
will be assigned to the sets and achievements.
  • Loading branch information
somiaj committed Nov 13, 2024
1 parent 973fdd0 commit 84d0a3b
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 132 deletions.
161 changes: 91 additions & 70 deletions lib/WeBWorK/ContentGenerator/CourseAdmin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,18 @@ sub pre_header_initialize ($c) {
}

sub add_course_form ($c) {
$c->param('number_of_additional_users', ($c->param('number_of_additional_users') // 0) + 1)
if $c->param('add_another_instructor');

return $c->include('ContentGenerator/CourseAdmin/add_course_form');
}

sub add_course_validate ($c) {
my $ce = $c->ce;

my $add_courseID = trim_spaces($c->param('new_courseID')) || '';
my $add_initial_userID = trim_spaces($c->param('add_initial_userID')) || '';
my $add_initial_password = trim_spaces($c->param('add_initial_password')) || '';
my $add_initial_confirmPassword = trim_spaces($c->param('add_initial_confirmPassword')) || '';
my $add_dbLayout = trim_spaces($c->param('add_dbLayout')) || '';
my $add_courseID = trim_spaces($c->param('new_courseID')) || '';
my $number_of_additional_users = $c->param('number_of_additional_users') || 0;
my $add_dbLayout = trim_spaces($c->param('add_dbLayout')) || '';

my @errors;

Expand All @@ -257,11 +258,24 @@ sub add_course_validate ($c) {
push @errors, $c->maketext('Course ID cannot exceed [_1] characters.', $ce->{maxCourseIdLength});
}

if ($add_initial_userID ne ''
&& $add_initial_password ne ''
&& $add_initial_password ne $add_initial_confirmPassword)
{
push @errors, $c->maketext('The password and password confirmation for the instructor must match.');
for (1 .. $number_of_additional_users) {
my $userID = trim_spaces($c->param("add_initial_userID_$_")) || '';
my $password = trim_spaces($c->param("add_initial_password_$_")) || '';
my $confirmPassword = trim_spaces($c->param("add_initial_confirmPassword_$_")) || '';

if ($userID ne '') {
unless ($userID =~ /^[\w-.,]*$/) {
push @errors,
$c->maketext(
'User ID number [_1] may only contain letters, numbers, hyphens, periods, commas, '
. 'and underscores.',
$_ + 1
);
}
if ($password ne '' && $password ne $confirmPassword) {
push @errors, $c->maketext('Pasword number [_1] and its password confirmation must match.', $_ + 1);
}
}
}

if ($add_dbLayout eq '') {
Expand All @@ -283,17 +297,10 @@ sub do_add_course ($c) {
my $db = $c->db;
my $authz = $c->authz;

my $add_courseID = trim_spaces($c->param('new_courseID')) // '';
my $add_courseTitle = ($c->param('add_courseTitle') // '') =~ s/^\s*|\s*$//gr;
my $add_courseInstitution = ($c->param('add_courseInstitution') // '') =~ s/^\s*|\s\*$//gr;

my $add_initial_userID = trim_spaces($c->param('add_initial_userID')) // '';
my $add_initial_password = trim_spaces($c->param('add_initial_password')) // '';
my $add_initial_confirmPassword = trim_spaces($c->param('add_initial_confirmPassword')) // '';
my $add_initial_firstName = trim_spaces($c->param('add_initial_firstName')) // '';
my $add_initial_lastName = trim_spaces($c->param('add_initial_lastName')) // '';
my $add_initial_email = trim_spaces($c->param('add_initial_email')) // '';
my $add_initial_user = $c->param('add_initial_user') // 0;
my $add_courseID = trim_spaces($c->param('new_courseID')) // '';
my $add_courseTitle = ($c->param('add_courseTitle') // '') =~ s/^\s*|\s*$//gr;
my $add_courseInstitution = ($c->param('add_courseInstitution') // '') =~ s/^\s*|\s\*$//gr;
my $number_of_additional_users = $c->param('number_of_additional_users') || 0;

my $copy_from_course = trim_spaces($c->param('copy_from_course')) // '';

Expand All @@ -314,12 +321,18 @@ sub do_add_course ($c) {
));
next;
}
if ($userID eq $add_initial_userID) {
$c->addbadmessage($c->maketext(
'User "[_1]" will not be copied from the [_2] course as it is the initial instructor.', $userID,
$ce->{admin_course_id}
));
next;
for (1 .. $number_of_additional_users) {
my $add_initial_userID = trim_spaces($c->param("add_initial_userID_$_")) // '';

if ($userID eq $add_initial_userID) {
$c->addbadmessage($c->maketext(
'User "[_1]" will not be copied from the [_2] course as it is the same as additional user '
. 'number [_3].',
$userID,
$ce->{admin_course_id}
));
next;
}
}

my $PermissionLevel = $db->getPermissionLevel($userID);
Expand All @@ -330,43 +343,56 @@ sub do_add_course ($c) {
push @users, [ $User, $Password, $PermissionLevel ];
}

# add initial instructor if desired
if ($add_initial_userID =~ /\S/) {
my $User = $db->newUser(
user_id => $add_initial_userID,
first_name => $add_initial_firstName,
last_name => $add_initial_lastName,
email_address => $add_initial_email,
status => 'O',
);
my $Password = $db->newPassword(
user_id => $add_initial_userID,
password => $add_initial_password ? cryptPassword($add_initial_password) : '',
);
my $PermissionLevel = $db->newPermissionLevel(
user_id => $add_initial_userID,
permission => '10',
);
push @users, [ $User, $Password, $PermissionLevel ];

# Add initial user to admin course if asked.
if ($add_initial_user) {
if ($db->existsUser($add_initial_userID)) {
$c->addbadmessage($c->maketext(
'User "[_1]" will not be added to the [_2] course as it already exists.', $add_initial_userID,
$ce->{admin_course_id}
));
} else {
$User->status('D'); # By default don't allow user to login.
$db->addUser($User);
$db->addPassword($Password);
$db->addPermissionLevel($PermissionLevel);
$User->status('O');
# add additional instructors if desired
for (1 .. $number_of_additional_users) {
my $userID = trim_spaces($c->param("add_initial_userID_$_")) // '';
my $password = trim_spaces($c->param("add_initial_password_$_")) // '';
my $confirmPassword = trim_spaces($c->param("add_initial_confirmPassword_$_")) // '';
my $firstName = trim_spaces($c->param("add_initial_firstName_$_")) // '';
my $lastName = trim_spaces($c->param("add_initial_lastName_$_")) // '';
my $email = trim_spaces($c->param("add_initial_email_$_")) // '';
my $studentID = trim_spaces($c->param("add_initial_studentID_$_")) // '';
my $permissionLevel = trim_spaces($c->param("add_initial_permission_$_"));
my $add_user = $c->param("add_initial_user_$_") // 0;

if ($userID =~ /\S/) {
my $User = $db->newUser(
user_id => $userID,
first_name => $firstName,
last_name => $lastName,
email_address => $email,
student_id => $studentID,
status => 'O',
);
my $Password = $db->newPassword(
user_id => $userID,
password => $password ? cryptPassword($password) : '',
);
my $PermissionLevel = $db->newPermissionLevel(
user_id => $userID,
permission => $permissionLevel,
);
push @users, [ $User, $Password, $PermissionLevel ];

# Add initial user to admin course if asked.
if ($add_user) {
if ($db->existsUser($userID)) {
$c->addbadmessage($c->maketext(
'User "[_1]" will not be added to the [_2] course as it already exists.', $userID,
$ce->{admin_course_id}
));
} else {
$User->status('D'); # By default don't allow user to login.
$db->addUser($User);
$db->addPassword($Password);
$db->addPermissionLevel($PermissionLevel);
$User->status('O');
}
}
}
}

push @{ $courseOptions{PRINT_FILE_NAMES_FOR} }, map { $_->[0]->user_id } @users;
push @{ $courseOptions{PRINT_FILE_NAMES_FOR} }, map { $_->[0]->user_id } grep { $_->[2]->permission >= 10 } @users;

# Include any optional arguments, including a template course and the course title and course institution.
my %optional_arguments;
Expand All @@ -386,11 +412,10 @@ sub do_add_course ($c) {

eval {
addCourse(
courseID => $add_courseID,
ce => $ce2,
courseOptions => \%courseOptions,
users => \@users,
initial_userID => $add_initial_userID,
courseID => $add_courseID,
ce => $ce2,
courseOptions => \%courseOptions,
users => \@users,
%optional_arguments,
);
};
Expand Down Expand Up @@ -419,11 +444,7 @@ sub do_add_course ($c) {
"\tAdded",
(defined $add_courseInstitution ? $add_courseInstitution : '(no institution specified)'),
(defined $add_courseTitle ? $add_courseTitle : '(no title specified)'),
$add_courseID,
$add_initial_firstName,
$add_initial_lastName,
$add_initial_email,
)
$add_courseID)
);
push(
@$output,
Expand Down
10 changes: 5 additions & 5 deletions lib/WeBWorK/Utils/CourseManagement.pm
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ boolean options:
=cut

sub addCourse {
my (%options) = (initial_userID => '', @_);
my (%options) = @_;

for my $key (keys(%options)) {
my $value = '####UNDEF###';
Expand All @@ -217,7 +217,7 @@ sub addCourse {

debug \@users;

my ($initialUser) = grep { $_->[0]{user_id} eq $options{initial_userID} } @users;
my @initialUsers = grep { $_->[2]->permission < $ce->{userRoles}{admin} } @users;

# get the database layout out of the options hash
my $dbLayoutName = $courseOptions{dbLayoutName};
Expand Down Expand Up @@ -407,7 +407,7 @@ sub addCourse {
assignSetsToUsers($db, $ce, \@user_sets, [$user_id]);
}
}
assignSetsToUsers($db, $ce, \@set_ids, [ $initialUser->[0]{user_id} ]) if $initialUser;
assignSetsToUsers($db, $ce, \@set_ids, [ map { $_->[0]{user_id} } @initialUsers ]) if @initialUsers;
}

# add achievements
Expand All @@ -416,9 +416,9 @@ sub addCourse {
for my $achievement_id (@achievement_ids) {
eval { $db->addAchievement($db0->getAchievement($achievement_id)) };
warn $@ if $@;
if ($initialUser) {
for (@initialUsers) {
my $userAchievement = $db->newUserAchievement();
$userAchievement->user_id($initialUser->[0]{user_id});
$userAchievement->user_id($_->[0]{user_id});
$userAchievement->achievement_id($achievement_id);
$db->addUserAchievement($userAchievement);
}
Expand Down
Loading

0 comments on commit 84d0a3b

Please sign in to comment.