--- loncom/lonnet/perl/lonnet.pm	2010/02/21 06:21:57	1.1051
+++ loncom/lonnet/perl/lonnet.pm	2010/03/15 05:10:03	1.1055
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.1051 2010/02/21 06:21:57 raeburn Exp $
+# $Id: lonnet.pm,v 1.1055 2010/03/15 05:10:03 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -3019,6 +3019,7 @@ sub getannounce {
 
 sub courseidput {
     my ($domain,$storehash,$coursehome,$caller) = @_;
+    return unless (ref($storehash) eq 'HASH');
     my $outcome;
     if ($caller eq 'timeonly') {
         my $cids = '';
@@ -3103,6 +3104,49 @@ sub courseiddump {
     return %returnhash;
 }
 
+sub courselastaccess {
+    my ($cdom,$cnum,$hostidref) = @_;
+    my %returnhash;
+    if ($cdom && $cnum) {
+        my $chome = &homeserver($cnum,$cdom);
+        if ($chome ne 'no_host') {
+            my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
+            &extract_lastaccess(\%returnhash,$rep);
+        }
+    } else {
+        if (!$cdom) { $cdom=''; }
+        my %libserv = &all_library();
+        foreach my $tryserver (keys(%libserv)) {
+            if (ref($hostidref) eq 'ARRAY') {
+                next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
+            } 
+            if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
+                my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
+                &extract_lastaccess(\%returnhash,$rep);
+            }
+        }
+    }
+    return %returnhash;
+}
+
+sub extract_lastaccess {
+    my ($returnhash,$rep) = @_;
+    if (ref($returnhash) eq 'HASH') {
+        unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
+                $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
+                 $rep eq '') {
+            my @pairs=split(/\&/,$rep);
+            foreach my $item (@pairs) {
+                my ($key,$value)=split(/\=/,$item,2);
+                $key = &unescape($key);
+                next if ($key =~ /^error: 2 /);
+                $returnhash->{$key} = &thaw_unescape($value);
+            }
+        }
+    }
+    return;
+}
+
 # ---------------------------------------------------------- DC e-mail
 
 sub dcmailput {
@@ -6357,10 +6401,97 @@ sub assignrole {
             &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
                                              $origstart,$selfenroll,$context);
         }
+        if ($role eq 'cc') {
+            &autoupdate_coowners($url,$end,$start,$uname,$udom);
+        }
     }
     return $answer;
 }
 
+sub autoupdate_coowners {
+    my ($url,$end,$start,$uname,$udom) = @_;
+    my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
+    if (($cdom ne '') && ($cnum ne '')) {
+        my $now = time;
+        my %domdesign = &Apache::loncommon::get_domainconf($cdom);
+        if ($domdesign{$cdom.'.autoassign.co-owners'}) {
+            my %coursehash = &coursedescription($cdom.'_'.$cnum);
+            my $instcode = $coursehash{'internal.coursecode'};
+            if ($instcode ne '') {
+                unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
+                    my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
+                    if ($result eq 'valid') {
+                        my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
+                        if (($end == 0) || ($end > $now)) {
+                             if ($coursehash{'internal.co-owners'}) {
+                                foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
+                                    push(@newcoowners,$coowner);
+                                }
+                                unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
+                                    push(@newcoowners,$uname.':'.$udom);
+                                }
+                                @newcoowners = sort(@newcoowners);
+                            } else {
+                                push(@newcoowners,$uname.':'.$udom);
+                            }
+                        } else {
+                            if ($coursehash{'internal.co-owners'}) {
+                                foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
+                                    unless ($coowner eq $uname.':'.$udom) {
+                                        push(@newcoowners,$coowner);
+                                    }
+                                }
+                                unless (@newcoowners > 0) {
+                                    $delcoowners = 1;
+                                    $coowners = '';
+                                }
+                            }
+                        }
+                        if (@newcoowners || $delcoowners) {
+                            &store_coowners($cdom,$cnum,$coursehash{'home'},
+                                            $delcoowners,@newcoowners);
+                        }
+                    }
+                }
+            }
+        }
+    }
+}
+
+sub store_coowners {
+    my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
+    my $cid = $cdom.'_'.$cnum;
+    my ($coowners,$delresult,$putresult);
+    if (@newcoowners) {
+        $coowners = join(',',@newcoowners);
+        my %coownershash = (
+                            'internal.co-owners' => $coowners,
+                           );
+        $putresult = &put('environment',\%coownershash,$cdom,$cnum);
+        if ($putresult eq 'ok') {
+            if ($env{'course.'.$cid.'.num'} eq $cnum) {
+                &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
+            }
+        }
+    }
+    if ($delcoowners) {
+        $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
+        if ($delresult eq 'ok') {
+            if ($env{'course.'.$cid.'.internal.co-owners'}) {
+                &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
+            }
+        }
+    }
+    if (($putresult eq 'ok') || ($delresult eq 'ok')) {
+        my %crsinfo =
+            &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
+        if (ref($crsinfo{$cid}) eq 'HASH') {
+            $crsinfo{$cid}{'co-owners'} = \@newcoowners;
+            my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
+        }
+    }
+}
+
 # -------------------------------------------------- Modify user authentication
 # Overrides without validation
 
@@ -6685,9 +6816,17 @@ sub createcourse {
     }
     return $uname if ($uname =~ /^error/);
 # -------------------------------------------------- Check supplied server name
-    $course_server = $env{'user.homeserver'} if (! defined($course_server));
-    if (! &is_library($course_server)) {
-        return 'error:bad server name '.$course_server;
+    if (!defined($course_server)) {
+        if (defined(&domain($udom,'primary'))) {
+            $course_server = &domain($udom,'primary');
+        } else {
+            $course_server = $env{'user.home'}; 
+        }
+    }
+    my %host_servers =
+        &Apache::lonnet::get_servers($udom,'library');
+    unless ($host_servers{$course_server}) {
+        return 'error: invalid home server for course: '.$course_server;
     }
 # ------------------------------------------------------------- Make the course
     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',