[LON-CAPA-cvs] cvs: loncom /automation batchcreatecourse.pm

raeburn raeburn@source.lon-capa.org
Sat, 05 Sep 2009 20:20:17 -0000


This is a MIME encoded message

--raeburn1252182017
Content-Type: text/plain

raeburn		Sat Sep  5 20:20:17 2009 EDT

  Modified files:              
    /loncom/automation	batchcreatecourse.pm 
  Log:
  - Batch course creation can now create custom roles in a course.
  - Check requested roles are legitimate.
  - Check requested custom role was defined by course owner.
  - Ignore sections in batch request if defined for a CC role. 
  
  
--raeburn1252182017
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20090905202017.txt"

Index: loncom/automation/batchcreatecourse.pm
diff -u loncom/automation/batchcreatecourse.pm:1.30 loncom/automation/batchcreatecourse.pm:1.31
--- loncom/automation/batchcreatecourse.pm:1.30	Sat Aug 15 00:25:34 2009
+++ loncom/automation/batchcreatecourse.pm	Sat Sep  5 20:20:17 2009
@@ -1,5 +1,5 @@
 #
-# $Id: batchcreatecourse.pm,v 1.30 2009/08/15 00:25:34 raeburn Exp $
+# $Id: batchcreatecourse.pm,v 1.31 2009/09/05 20:20:17 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -530,6 +530,10 @@
 #
 # Process other reqested users
 #
+    my @courseroles = qw(st ep ta in cc);
+    if (&owner_is_dc($owner_uname,$owner_domain,$crsudom)) {
+        push(@courseroles,'ad');
+    }
     my $stulogmsg = '';
     foreach my $userkey (sort(keys(%{$details->{'users'}}))) {
         my $url = '/'.$crsudom.'/'.$crsunum;
@@ -537,8 +541,35 @@
         if (@{$details->{'users'}{$userkey}{'roles'}} > 0) {
             my ($username,$userdom) = split/:/,$userkey;
             if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
-                my $firstrole = $details->{'users'}{$userkey}{'roles'}[0];
-                my $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
+                my @reqroles = @{$details->{'users'}{$userkey}{'roles'}};
+                my @badroles;
+                my $firstrole = shift(@reqroles);
+                while (@reqroles > 0) { 
+                    if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
+                        if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
+                            last;
+                        } else {
+                            push(@badroles,$firstrole);
+                            $firstrole = shift(@reqroles);
+                        }
+                    } elsif (grep(/^\Q$firstrole\E$/,@courseroles)) {
+                        last;
+                    } else {
+                        push(@badroles,$firstrole);
+                        $firstrole = shift(@reqroles);
+                    }
+                }
+                if (@badroles > 0) {
+                    if (@badroles > 1) {
+                        $$output .= &mt('The following requested roles are unavailable:').' '.join(', ',@badroles);
+                    } else {
+                        $$output .= &mt('The following requested role: [_1] is unavailable.',$badroles[0]); 
+                    }
+                }
+                my $firstsec;
+                unless (($firstrole eq 'cc') || ($firstrole eq ''))  {
+                    $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
+                }
                 my $userargs = {
                     'auth' => $details->{'users'}{$userkey}{'authtype'},
                     'authparam' => $details->{'users'}{$userkey}{'autharg'},
@@ -564,7 +595,7 @@
                 $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo,$context);
 # now add other roles and other sections.
                 if ($outcome eq 'ok') {
-                    if (($firstrole ne 'st') && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
+                    if ((($firstrole ne 'st') && ($firstrole ne 'cc') && ($firstrole ne '')) && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
                         for (my $i=1; $i<@{$details->{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
                             my $curr_role = $firstrole;
                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
@@ -574,12 +605,15 @@
                             if ($usec ne '') {
                                 $url .= '/'.$usec;
                             }
-                            $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
+                            if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
+                                $$output .= &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
+                            } else {
+                                $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
+                            }
                         }
                     }
-                    if (@{$details->{'users'}{$userkey}{'roles'}} > 1) {
-                        for (my $j=1; $j<@{$details->{'users'}{$userkey}{'roles'}}; $j++) {
-                            my $curr_role = $details->{'users'}{$userkey}{'roles'}[$j];
+                    if (@reqroles > 0) {
+                        foreach my $curr_role (@reqroles) {
                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
                             if ($curr_role eq 'st') {
@@ -589,14 +623,32 @@
                                     $url .= '/'.$usec;
                                 }
                                 $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
-                            } else {
+                            } elsif ($curr_role eq 'cc') {
+                                $url = '/'.$crsudom.'/'.$crsunum;
+                                my $usec = '';
+                                $$output .=
+                                    &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
+                            } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) || 
+                                     ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
                                 foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
                                     $url = '/'.$crsudom.'/'.$crsunum;
                                     if ($usec ne '') {
                                         $url .= '/'.$usec;
                                     }
-                                    $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
+                                    if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
+                                        if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
+                                            $$output .= 
+                                                &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
+                                        } else {
+                                            $$output = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
+                                        }
+                                    } else {
+                                        $$output .= 
+                                            &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
+                                    }
                                 }
+                            } else {
+                                $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
                             }
                         }
                     }
@@ -612,20 +664,44 @@
                             $url .= '/'.$usec;
                         }
                         $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
-                    } else {
+                    } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
+                                     ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
                         if (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
                             foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
                                 $url = '/'.$crsudom.'/'.$crsunum;
                                 if ($usec ne '') {
                                     $url .= '/'.$usec;
                                 }
-                                my $stdresult = &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
+                                my $stdresult;
+                                if ($curr_role =~ m{/^cr/($match_domain)/($match_username)/([^/]+)$}) {
+                                    if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
+                                        $stdresult = 
+                                            &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
+                                    } else {
+                                        $stdresult = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
+                                    }
+                                } else {
+                                    $stdresult = 
+                                        &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
+                                }
                                 $$output .= $stdresult;
                             }
                         } else {
                             $url = '/'.$crsudom.'/'.$crsunum;
-                            $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'',$context);
+                            if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
+                                if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
+                                    $$output .= 
+                                        &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
+                                } else {
+                                    $$output .= &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
+                                }
+                            } else {
+                                $$output .= 
+                                    &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'',$context);
+                            }
                         }
+                    } else {
+                        $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
                     }
                 }
             }
@@ -642,6 +718,17 @@
     return $courseid;
 }
 
+sub owner_is_dc {
+    my ($owner_uname,$owner_dom,$cdom) = @_;
+    my $is_dc = 0;
+    my %roles = &Apache::lonnet::get_my_roles($owner_uname,$owner_dom,'userroles',
+                    ['active'],['dc'],[$cdom]);
+    if ($roles{$owner_uname.':'.$owner_dom.':dc'}) {
+        $is_dc = 1;
+    }
+    return $is_dc;
+}
+
 #########################################################
 #
 # process_date()

--raeburn1252182017--