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

raeburn raeburn@source.lon-capa.org
Sat, 15 Aug 2009 00:05:55 -0000


This is a MIME encoded message

--raeburn1250294755
Content-Type: text/plain

raeburn		Sat Aug 15 00:05:55 2009 EDT

  Modified files:              
    /loncom/automation	batchcreatecourse.pm 
  Log:
  - Simplify data structure in &build_course(). 
    - Pass the (inner) hash reference of course details instead of a reference to the (outer) hash of a hash.   
  
  
--raeburn1250294755
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20090815000555.txt"

Index: loncom/automation/batchcreatecourse.pm
diff -u loncom/automation/batchcreatecourse.pm:1.28 loncom/automation/batchcreatecourse.pm:1.29
--- loncom/automation/batchcreatecourse.pm:1.28	Sat Aug  8 19:55:15 2009
+++ loncom/automation/batchcreatecourse.pm	Sat Aug 15 00:05:54 2009
@@ -1,5 +1,5 @@
 #
-# $Id: batchcreatecourse.pm,v 1.28 2009/08/08 19:55:15 raeburn Exp $
+# $Id: batchcreatecourse.pm,v 1.29 2009/08/15 00:05:54 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -219,8 +219,9 @@
             my %details = ();
             if (-e $newcoursedir.'/'.$request) {
                 &parse_coursereqs($newcoursedir.'/'.$request, \%details);
-                foreach my $num (sort keys %details) {
-                    my $courseid = &build_course($dom,$num,$context,\%details,\%longroles,\$logmsg,\$newusermsg,\$addresult,\%enrollcount,\$output,\$keysmsg);
+                foreach my $num (sort(keys(%details))) {
+                    my $reqdetails = $details{$num};
+                    my $courseid = &build_course($dom,$num,$context,$reqdetails,\%longroles,\$logmsg,\$newusermsg,\$addresult,\%enrollcount,\$output,\$keysmsg);
                     if ($courseid =~m{^/$match_domain/$match_courseid}) {
                         $$courseids{$courseid} = $details{$num}{'class'};
                     }
@@ -393,9 +394,10 @@
 #########################################################
 
 sub build_course {
-    my ($cdom,$num,$context,$details,$longroles,$logmsg,$newusermsg,$addresult,$enrollcount,$output,$keysmsg,$udom,$uname,$cnum) = @_;
-    my $owner_uname = $$details{$num}{'owner'};
-    my $owner_domain = $$details{$num}{'domain'};
+    my ($cdom,$num,$context,$details,$longroles,$logmsg,$newusermsg,$addresult,$enrollcount,$output,$keysmsg,$udom,$uname,$cnum,$category) = @_;
+    return unless (ref($details) eq 'HASH');
+    my $owner_uname = $details->{'owner'};
+    my $owner_domain = $details->{'domain'};
     my $owner = $owner_uname.':'.$owner_domain;
     my $sectionstr = '';
     my $xliststr = '';
@@ -408,46 +410,46 @@
     } else {
         $linefeed = "<br />\n";
     }
-    if ($$details{$num}{'accessend'} eq '') {
+    if ($details->{'accessend'} eq '') {
         $noenddate = 1;
     }
-    my $reshome = $$details{$num}{'reshome'};
+    my $reshome = $details->{'reshome'};
     if ($reshome eq '') {
         $reshome = '/res/'.$cdom;
     }
-    my $firstres =  $$details{$num}{'firstres'};
+    my $firstres =  $details->{'firstres'};
     if ($firstres eq '') {
         $firstres = 'syl';
     }
-    foreach my $secid (sort keys %{$$details{$num}{'sections'}}) {
-        $sectionstr .= $$details{$num}{'sections'}{$secid}{'inst'}.':'.$$details{$num}{'sections'}{$secid}{'loncapa'}.',';
+    foreach my $secid (sort(keys(%{$details->{'sections'}}))) {
+        $sectionstr .= $details->{'sections'}{$secid}{'inst'}.':'.$details->{'sections'}{$secid}{'loncapa'}.',';
     }
     $sectionstr =~ s/,$//;
 
-    foreach my $xlist (sort keys %{$$details{$num}{'crosslists'}}) {
-        $xliststr .= $$details{$num}{'crosslists'}{$xlist}{'inst'}.':'.$$details{$num}{'crosslists'}{$xlist}{'loncapa'}.',';
+    foreach my $xlist (sort(keys(%{$details->{'crosslists'}}))) {
+        $xliststr .= $details->{'crosslists'}{$xlist}{'inst'}.':'.$details->{'crosslists'}{$xlist}{'loncapa'}.',';
     }
     $xliststr =~ s/,$//;
 
     my %courseinfo = (
-                      inst_code => $$details{$num}{'coursecode'},
-                      description => $$details{$num}{'title'}
+                      inst_code => $details->{'coursecode'},
+                      description => $details->{'title'}
                      ); 
-    if (&Apache::lonnet::homeserver($$details{$num}{'owner'},$$details{$num}{'domain'}) eq 'no_host') { # Add user if no account
-        my $ownerargs = {'auth' => $$details{$num}{'ownerauthtype'},
-                    'authparam' => $$details{$num}{'ownerauthparam'},
-                    'emailenc' => $$details{$num}{'emailenc'},
-                    'udom' => $$details{$num}{'domain'},
-                    'uname' => $$details{$num}{'owner'},
-                    'pid' => $$details{$num}{'users'}{$owner}{'studentID'},
-                    'first' => $$details{$num}{'users'}{$owner}{'firstname'},
-                    'middle' => $$details{$num}{'users'}{$owner}{'middlename'},
-                    'last' => $$details{$num}{'users'}{$owner}{'lastname'},
-                    'gene' => $$details{$num}{'users'}{$owner}{'generation'},
+    if (&Apache::lonnet::homeserver($details->{'owner'},$details->{'domain'}) eq 'no_host') { # Add user if no account
+        my $ownerargs = {'auth' => $details->{'ownerauthtype'},
+                    'authparam' => $details->{'ownerauthparam'},
+                    'emailenc' => $details->{'emailenc'},
+                    'udom' => $details->{'domain'},
+                    'uname' => $details->{'owner'},
+                    'pid' => $details->{'users'}{$owner}{'studentID'},
+                    'first' => $details->{'users'}{$owner}{'firstname'},
+                    'middle' => $details->{'users'}{$owner}{'middlename'},
+                    'last' => $details->{'users'}{$owner}{'lastname'},
+                    'gene' => $details->{'users'}{$owner}{'generation'},
                     'usec' => '',
                     'end' => '',
                     'start' => '',
-                    'emailaddr' => $$details{$num}{'users'}{$owner}{'email'},
+                    'emailaddr' => $details->{'users'}{$owner}{'email'},
                     'cid' => '',
                     'context' => 'createowner',
                     'linefeed' => $linefeed,
@@ -459,54 +461,54 @@
     }
 
     if ($outcome eq 'ok') {
-        if ($$details{$num}{'datemode'} !~ /^(preserve|shift|delete)$/) {
-            $$details{$num}{'datemode'} = 'shift';
-            $$details{$num}{'dateshift'} = 365;
+        if ($details->{'datemode'} !~ /^(preserve|shift|delete)$/) {
+            $details->{'datemode'} = 'shift';
+            $details->{'dateshift'} = 365;
         }
         my $courseargs = {
-               ccuname => $$details{$num}{'owner'},
-               ccdomain => $$details{$num}{'domain'},
-               cdescr => $$details{$num}{'title'},
-               crstype => $$details{$num}{'crstype'},
-               curl => $$details{$num}{'topmap'},
+               ccuname => $details->{'owner'},
+               ccdomain => $details->{'domain'},
+               cdescr => $details->{'title'},
+               crstype => $details->{'crstype'},
+               curl => $details->{'topmap'},
                course_domain => $cdom,
-               course_home =>  $$details{$num}{'coursehome'},
-               nonstandard => $$details{$num}{'nonstandard'},
-               crscode => $$details{$num}{'coursecode'},
-               crsquota => $$details{$num}{'crsquota'},
-               clonecourse => $$details{$num}{'clonecrs'},
-               clonedomain => $$details{$num}{'clonedom'},
-               datemode => $$details{$num}{'datemode'},
-               dateshift => $$details{$num}{'dateshift'},
-               crsid => $$details{$num}{'optional_id'},
-               curruser => $$details{$num}{'owner'},
+               course_home =>  $details->{'coursehome'},
+               nonstandard => $details->{'nonstandard'},
+               crscode => $details->{'coursecode'},
+               crsquota => $details->{'crsquota'},
+               clonecourse => $details->{'clonecrs'},
+               clonedomain => $details->{'clonedom'},
+               datemode => $details->{'datemode'},
+               dateshift => $details->{'dateshift'},
+               crsid => $details->{'optional_id'},
+               curruser => $details->{'owner'},
                crssections => $sectionstr,
                crsxlist => $xliststr,
-               autoadds => $$details{$num}{'adds'},
-               autodrops => $$details{$num}{'drops'},
-               notify => $$details{$num}{'notify_owner'},
-               notify_dc => $$details{$num}{'notify_dc'},
+               autoadds => $details->{'adds'},
+               autodrops => $details->{'drops'},
+               notify => $details->{'notify_owner'},
+               notify_dc => $details->{'notify_dc'},
                no_end_date => $noenddate,
-               showphotos => $$details{$num}{'showphotos'},
-               authtype => $$details{$num}{'authtype'},
-               autharg => $$details{$num}{'authparam'},
-               enrollstart => $$details{$num}{'enrollstart'},
-               enrollend => $$details{$num}{'enrollend'},
-               startaccess => $$details{$num}{'accessstart'},
-               endaccess => $$details{$num}{'accessend'},
-               setpolicy => $$details{$num}{'setpolicy'},
-               setcontent => $$details{$num}{'setcontent'},
+               showphotos => $details->{'showphotos'},
+               authtype => $details->{'authtype'},
+               autharg => $details->{'authparam'},
+               enrollstart => $details->{'enrollstart'},
+               enrollend => $details->{'enrollend'},
+               startaccess => $details->{'accessstart'},
+               endaccess => $details->{'accessend'},
+               setpolicy => $details->{'setpolicy'},
+               setcontent => $details->{'setcontent'},
                reshome => $reshome,
-               setkeys => $$details{$num}{'setkeys'},
-               keyauth => $$details{$num}{'keyauth'},
-               disresdis => $$details{$num}{'disresdis'},
-               disablechat => $$details{$num}{'disablechat'},
-               openall => $$details{$num}{'openall'},
+               setkeys => $details->{'setkeys'},
+               keyauth => $details->{'keyauth'},
+               disresdis => $details->{'disresdis'},
+               disablechat => $details->{'disablechat'},
+               openall => $details->{'openall'},
                firstres => $firstres
                };
         my %host_servers = &Apache::lonnet::get_servers($cdom,'library');
-        if (! exists($host_servers{$$details{$num}{'coursehome'}})) {
-            $$logmsg .= &mt('Invalid home server for course').': '.$$details{$num}{'coursehome'};
+        if (! exists($host_servers{$details->{'coursehome'}})) {
+            $$logmsg .= &mt('Invalid home server for course').': '.$details->{'coursehome'};
             return;
         }
         my ($success, $msg) = &Apache::loncommon::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,$udom,$uname,$context,$cnum);
@@ -529,44 +531,45 @@
 # Process other reqested users
 #
     my $stulogmsg = '';
-    foreach my $userkey (sort keys %{$$details{$num}{'users'}}) {
+    foreach my $userkey (sort(keys(%{$details->{'users'}}))) {
         my $url = '/'.$crsudom.'/'.$crsunum;
-        if (@{$$details{$num}{'users'}{$userkey}{'roles'}} > 0) {
+        next if (ref($details->{'users'}{$userkey}{'roles'}) ne 'ARRAY');   
+        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{$num}{'users'}{$userkey}{'roles'}[0];
-                my $firstsec = $$details{$num}{'users'}{$userkey}{$firstrole}{'usec'}[0];
+                my $firstrole = $details->{'users'}{$userkey}{'roles'}[0];
+                my $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
                 my $userargs = {
-                    'auth' => $$details{$num}{'users'}{$userkey}{'authtype'},
-                    'authparam' => $$details{$num}{'users'}{$userkey}{'autharg'},
-                    'emailenc' => $$details{$num}{'users'}{$userkey}{'emailenc'},
+                    'auth' => $details->{'users'}{$userkey}{'authtype'},
+                    'authparam' => $details->{'users'}{$userkey}{'autharg'},
+                    'emailenc' => $details->{'users'}{$userkey}{'emailenc'},
                     'udom' => $userdom,
                     'uname' => $username,
-                    'pid' => $$details{$num}{'users'}{$userkey}{'studentID'},
-                    'first' => $$details{$num}{'users'}{$userkey}{'firstname'},
-                    'middle' => $$details{$num}{'users'}{$userkey}{'middlename'},
-                    'last' => $$details{$num}{'users'}{$userkey}{'lastname'},
-                    'gene' => $$details{$num}{'users'}{$userkey}{'generation'},
+                    'pid' => $details->{'users'}{$userkey}{'studentID'},
+                    'first' => $details->{'users'}{$userkey}{'firstname'},
+                    'middle' => $details->{'users'}{$userkey}{'middlename'},
+                    'last' => $details->{'users'}{$userkey}{'lastname'},
+                    'gene' => $details->{'users'}{$userkey}{'generation'},
                     'usec' => $firstsec,
-                    'end' => $$details{$num}{'users'}{$userkey}{'end'},
-                    'start' => $$details{$num}{'users'}{$userkey}{'start'},
-                    'emailaddr' => $$details{$num}{'users'}{$userkey}{'emailaddr'},
+                    'end' => $details->{'users'}{$userkey}{'end'},
+                    'start' => $details->{'users'}{$userkey}{'start'},
+                    'emailaddr' => $details->{'users'}{$userkey}{'emailaddr'},
                     'cid' => $courseid,
                     'crs' => $crsunum,
                     'cdom' => $crsudom,
                     'context' => 'createcourse',
                     'linefeed' => $linefeed,
-                    'role' => $$details{$num}{'users'}{$userkey}{'roles'}[0], 
+                    'role' => $details->{'users'}{$userkey}{'roles'}[0], 
                    };
                 $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{$num}{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
-                        for (my $i=1; $i<@{$$details{$num}{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
+                    if (($firstrole ne 'st') && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
+                        for (my $i=1; $i<@{$details->{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
                             my $curr_role = $firstrole;
-                            my $start = $$details{$num}{'users'}{$userkey}{$curr_role}{'start'};
-                            my $end = $$details{$num}{'users'}{$userkey}{$curr_role}{'end'};
-                            my $usec = $$details{$num}{'users'}{$userkey}{$firstrole}{'usec'}[$i];
+                            my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
+                            my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
+                            my $usec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[$i];
                             $url = '/'.$crsudom.'/'.$crsunum;
                             if ($usec ne '') {
                                 $url .= '/'.$usec;
@@ -574,20 +577,20 @@
                             $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                         }
                     }
-                    if (@{$$details{$num}{'users'}{$userkey}{'roles'}} > 1) {
-                        for (my $j=1; $j<@{$$details{$num}{'users'}{$userkey}{'roles'}}; $j++) {
-                            my $curr_role = $$details{$num}{'users'}{$userkey}{'roles'}[$j];
-                            my $start = $$details{$num}{'users'}{$userkey}{$curr_role}{'start'};
-                            my $end = $$details{$num}{'users'}{$userkey}{$curr_role}{'end'};
+                    if (@{$details->{'users'}{$userkey}{'roles'}} > 1) {
+                        for (my $j=1; $j<@{$details->{'users'}{$userkey}{'roles'}}; $j++) {
+                            my $curr_role = $details->{'users'}{$userkey}{'roles'}[$j];
+                            my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
+                            my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
                             if ($curr_role eq 'st') {
-                                my $usec = $$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}[0];
+                                my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
                                 $url = '/'.$crsudom.'/'.$crsunum;
                                 if ($usec ne '') {
                                     $url .= '/'.$usec;
                                 }
                                 $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                             } else {
-                                foreach my $usec (@{$$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}}) {
+                                foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
                                     $url = '/'.$crsudom.'/'.$crsunum;
                                     if ($usec ne '') {
                                         $url .= '/'.$usec;
@@ -599,19 +602,19 @@
                     }
                 }
             } else {
-                foreach my $curr_role (@{$$details{$num}{'users'}{$userkey}{'roles'}}) {
-                    my $start = $$details{$num}{'users'}{$userkey}{$curr_role}{'start'};
-                    my $end = $$details{$num}{'users'}{$userkey}{$curr_role}{'end'};
+                foreach my $curr_role (@{$details->{'users'}{$userkey}{'roles'}}) {
+                    my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
+                    my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
                     if ($curr_role eq 'st') {
-                        my $usec = $$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}[0];
+                        my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
                         $url = '/'.$crsudom.'/'.$crsunum;
                         if ($usec ne '') {
                             $url .= '/'.$usec;
                         }
                         $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                     } else {
-                        if (@{$$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
-                            foreach my $usec (@{$$details{$num}{'users'}{$userkey}{$curr_role}{'usec'}}) {
+                        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;
@@ -630,9 +633,9 @@
     }
 
 # Information about keys.
-    if ($$details{$num}{'setkeys'}) {
+    if ($details->{'setkeys'}) {
         $$keysmsg .=
- '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$$details{$num}{'title'}.$linefeed;
+ '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$details->{'title'}.$linefeed;
     }
 # Flush the course logs so reverse user roles immediately updated
     &Apache::lonnet::flushcourselogs();

--raeburn1250294755--