--- loncom/interface/loncommon.pm	2007/12/11 00:47:58	1.624
+++ loncom/interface/loncommon.pm	2008/01/04 18:08:11	1.632
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.624 2007/12/11 00:47:58 raeburn Exp $
+# $Id: loncommon.pm,v 1.632 2008/01/04 18:08:11 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -472,7 +472,10 @@ sub setsec_javascript {
     my ($sec_element,$formname) = @_;
     my $setsections = qq|
 function setSect(sectionlist) {
-    var sectionsArray = sectionlist.split(",");
+    var sectionsArray = new Array();
+    if ((sectionlist != '') && (typeof sectionlist != "undefined")) {
+        sectionsArray = sectionlist.split(",");
+    }
     var numSections = sectionsArray.length;
     document.$formname.$sec_element.length = 0;
     if (numSections == 0) {
@@ -3667,45 +3670,78 @@ sub get_domainconf {
 
     my %domconfig = &Apache::lonnet::get_dom('configuration',
 					     ['login','rolecolors'],$udom);
-    my %designhash;
+    my (%designhash,%legacy);
     if (keys(%domconfig) > 0) {
         if (ref($domconfig{'login'}) eq 'HASH') {
-            foreach my $key (keys(%{$domconfig{'login'}})) {
-                $designhash{$udom.'.login.'.$key}=$domconfig{'login'}{$key};
+            if (keys(%{$domconfig{'login'}})) {
+                foreach my $key (keys(%{$domconfig{'login'}})) {
+                    $designhash{$udom.'.login.'.$key}=$domconfig{'login'}{$key};
+                }
+            } else {
+                $legacy{'login'} = 1;
             }
+        } else {
+            $legacy{'login'} = 1;
         }
         if (ref($domconfig{'rolecolors'}) eq 'HASH') {
-            foreach my $role (keys(%{$domconfig{'rolecolors'}})) {
-                if (ref($domconfig{'rolecolors'}{$role}) eq 'HASH') {
-                    foreach my $item (keys(%{$domconfig{'rolecolors'}{$role}})) {
-                        $designhash{$udom.'.'.$role.'.'.$item}=$domconfig{'rolecolors'}{$role}{$item};
+            if (keys(%{$domconfig{'rolecolors'}})) {
+                foreach my $role (keys(%{$domconfig{'rolecolors'}})) {
+                    if (ref($domconfig{'rolecolors'}{$role}) eq 'HASH') {
+                        foreach my $item (keys(%{$domconfig{'rolecolors'}{$role}})) {
+                            $designhash{$udom.'.'.$role.'.'.$item}=$domconfig{'rolecolors'}{$role}{$item};
+                        }
                     }
                 }
+            } else {
+                $legacy{'rolecolors'} = 1;
             }
+        } else {
+            $legacy{'rolecolors'} = 1;
         }
-    } else {
-        my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
-        my $designfile =  $designdir.'/'.$udom.'.tab';
-        if (-e $designfile) {
-            if ( open (my $fh,"<$designfile") ) {
-                while (my $line = <$fh>) {
-                    next if ($line =~ /^\#/);
-                    chomp($line);
-                    my ($key,$val)=(split(/\=/,$line));
-                    if ($val) { $designhash{$udom.'.'.$key}=$val; }
+        if (keys(%legacy) > 0) {
+            my %legacyhash = &get_legacy_domconf($udom);
+            foreach my $item (keys(%legacyhash)) {
+                if ($item =~ /^\Q$udom\E\.login/) {
+                    if ($legacy{'login'}) { 
+                        $designhash{$item} = $legacyhash{$item};
+                    }
+                } else {
+                    if ($legacy{'rolecolors'}) {
+                        $designhash{$item} = $legacyhash{$item};
+                    }
                 }
-                close($fh);
             }
         }
-        if (-e '/home/httpd/html/adm/lonDomLogos/'.$udom.'.gif') {
-            $designhash{$udom.'.login.domlogo'} = "/adm/lonDomLogos/$udom.gif";
-        }
+    } else {
+        %designhash = &get_legacy_domconf($udom); 
     }
     &Apache::lonnet::do_cache_new('domainconfig',$udom,\%designhash,
 				  $cachetime);
     return %designhash;
 }
 
+sub get_legacy_domconf {
+    my ($udom) = @_;
+    my %legacyhash;
+    my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
+    my $designfile =  $designdir.'/'.$udom.'.tab';
+    if (-e $designfile) {
+        if ( open (my $fh,"<$designfile") ) {
+            while (my $line = <$fh>) {
+                next if ($line =~ /^\#/);
+                chomp($line);
+                my ($key,$val)=(split(/\=/,$line));
+                if ($val) { $legacyhash{$udom.'.'.$key}=$val; }
+            }
+            close($fh);
+        }
+    }
+    if (-e '/home/httpd/html/adm/lonDomLogos/'.$udom.'.gif') {
+        $legacyhash{$udom.'.login.domlogo'} = "/adm/lonDomLogos/$udom.gif";
+    }
+    return %legacyhash;
+}
+
 =pod
 
 =item * &domainlogo()
@@ -5848,6 +5884,8 @@ previous, future, or all.
 6. reference to results object (hash of hashes).
 7. reference to optional userdata hash
 8. reference to optional statushash
+9. flag if privileged users (except those set to unhide in
+   course settings) should be excluded    
 Keys of top level results hash are roles.
 Keys of inner hashes are username:domain, with 
 values set to access type.
@@ -5864,7 +5902,7 @@ of the possibility of multiple values fo
 ###############################################
 
 sub get_course_users {
-    my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata,$statushash) = @_;
+    my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata,$statushash,$hidepriv) = @_;
     my %idx = ();
     my %seclists;
 
@@ -5940,6 +5978,17 @@ sub get_course_users {
                               active   => 'Active',
                               future   => 'Future',
                             );
+        my %nothide;
+        if ($hidepriv) {
+            my %coursehash=&Apache::lonnet::coursedescription($cdom.'_'.$cnum);
+            foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
+                if ($user !~ /:/) {
+                    $nothide{join(':',split(/[\@]/,$user))}=1;
+                } else {
+                    $nothide{$user} = 1;
+                }
+            }
+        }
         foreach my $person (sort(keys(%coursepersonnel))) {
             my $match = 0;
             my $secmatch = 0;
@@ -5973,6 +6022,12 @@ sub get_course_users {
                     $usec = 'none';
                 }
                 if ($uname ne '' && $udom ne '') {
+                    if ($hidepriv) {
+                        if ((&Apache::lonnet::privileged($uname,$udom)) &&
+                            (!$nothide{$uname.':'.$udom})) {
+                            next;
+                        }
+                    }
                     if ($end > 0 && $end < $now) {
                         $status = 'previous';
                     } elsif ($start > $now) {
@@ -6236,14 +6291,14 @@ sub get_secgrprole_info {
 }
 
 sub user_picker {
-    my ($dom,$srch,$forcenewuser,$caller) = @_;
+    my ($dom,$srch,$forcenewuser,$caller,$cancreate,$usertype) = @_;
     my $currdom = $dom;
     my %curr_selected = (
                         srchin => 'dom',
                         srchby => 'lastname',
                       );
     my $srchterm;
-    if (ref($srch) eq 'HASH') {
+    if ((ref($srch) eq 'HASH') && ($env{'form.origform'} ne 'crtusername')) {
         if ($srch->{'srchby'} ne '') {
             $curr_selected{'srchby'} = $srch->{'srchby'};
         }
@@ -6330,7 +6385,16 @@ sub user_picker {
     if ($forcenewuser) {
         if (ref($srch) eq 'HASH') {
             if ($srch->{'srchby'} eq 'uname' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchin'} eq 'dom' && $srch->{'srchdomain'} eq $env{'request.role.domain'}) {
-	        $new_user_create = '<p> <input type="submit" name="forcenew" value="'.&HTML::Entities::encode(&mt('Make new user "[_1]"',$srchterm),'<>&"').'" onclick="javascript:setSearch(\'1\','.$caller.');" /> </p>';
+                if ($cancreate) {
+                    $new_user_create = '<p> <input type="submit" name="forcenew" value="'.&HTML::Entities::encode(&mt('Make new user "[_1]"',$srchterm),'<>&"').'" onclick="javascript:setSearch(\'1\','.$caller.');" /> </p>';
+                } else {
+                    my $helplink = ' href="javascript:helpMenu('."'display'".')"';
+                    my %usertypetext = (
+                        official   => 'institutional',
+                        unofficial => 'non-institutional',
+                    );
+                    $new_user_create = '<br /><span class="LC_warning">'.&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.").' '.&mt('Contact the <a[_1]>helpdesk</a> for assistance.',$helplink).'</span><br /><br />';
+                }
             }
         }
 
@@ -7589,6 +7653,8 @@ a hash ref describing the data to be sto
 
 Returns: both routines return nothing
 
+=back
+
 =cut
 
 #######################################################
@@ -7744,7 +7810,7 @@ sub build_recipient_list {
 
 sub commit_customrole {
     my ($udom,$uname,$url,$three,$four,$five,$start,$end) = @_;
-    my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.'@'.$three.' in '.$url.
+    my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.':'.$three.' in '.$url.
                          ($start?', '.&mt('starting').' '.localtime($start):'').
                          ($end?', ending '.localtime($end):'').': <b>'.
               &Apache::lonnet::assigncustomrole(
@@ -7765,8 +7831,8 @@ sub commit_standardrole {
         my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$three,$start,$end,
                                          $one,$two,$sec,$context);
         if (($result =~ /^error/) || ($result eq 'not_in_class') || 
-            ($result eq 'unknown_course')) {
-            $output = "Error: $result\n"; 
+            ($result eq 'unknown_course') || ($result eq 'refused')) {
+            $output = $logmsg.' '.&mt('Error: ').$result."\n"; 
         } else {
             $output = $logmsg.$linefeed.&mt('Assigning').' '.$three.' in '.$url.
                ($start?', '.&mt('starting').' '.localtime($start):'').
@@ -7795,7 +7861,7 @@ sub commit_standardrole {
 
 sub commit_studentrole {
     my ($logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context) = @_;
-    my ($result,$linefeed);
+    my ($result,$linefeed,$oldsecurl,$newsecurl);
     if ($context eq 'auto') {
         $linefeed = "\n";
     } else {
@@ -7807,15 +7873,36 @@ sub commit_studentrole {
         my $secchange = 0;
         my $expire_role_result;
         my $modify_section_result;
-        unless ($oldsec eq '-1') {
-            unless ($sec eq $oldsec) {
+        if ($oldsec ne '-1') { 
+            if ($oldsec ne $sec) {
                 $secchange = 1;
+                my $now = time;
                 my $uurl='/'.$cid;
                 $uurl=~s/\_/\//g;
                 if ($oldsec) {
                     $uurl.='/'.$oldsec;
                 }
-                $expire_role_result = &Apache::lonnet::assignrole($udom,$uname,$uurl,'st',time);
+                $oldsecurl = $uurl;
+                $expire_role_result = 
+                    &Apache::lonnet::assignrole($udom,$uname,$uurl,'st',$now);
+                if ($env{'request.course.sec'} ne '') { 
+                    if ($expire_role_result eq 'refused') {
+                        my @roles = ('st');
+                        my @statuses = ('previous');
+                        my @roledoms = ($one);
+                        my $withsec = 1;
+                        my %roleshash = 
+                            &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
+                                              \@statuses,\@roles,\@roledoms,$withsec);
+                        if (defined ($roleshash{$two.':'.$one.':st:'.$oldsec})) {
+                            my ($oldstart,$oldend) = 
+                                split(':',$roleshash{$two.':'.$one.':st:'.$oldsec});
+                            if ($oldend > 0 && $oldend <= $now) {
+                                $expire_role_result = 'ok';
+                            }
+                        }
+                    }
+                }
                 $result = $expire_role_result;
             }
         }
@@ -7823,21 +7910,55 @@ sub commit_studentrole {
             $modify_section_result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,'','',$cid);
             if ($modify_section_result =~ /^ok/) {
                 if ($secchange == 1) {
-                    $$logmsg .= "Section for $uname switched from old section: $oldsec to new section: $sec".$linefeed;
+                    if ($sec eq '') {
+                        $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to student role without a section.',$uname,$oldsec).$linefeed;
+                    } else {
+                        $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to new section: [_3].',$uname,$oldsec,$sec).$linefeed;
+                    }
                 } elsif ($oldsec eq '-1') {
-                    $$logmsg .= "New student role for $uname in section $sec in course $cid".$linefeed;
+                    if ($sec eq '') {
+                        $$logmsg .= &mt('New student role without a section for [_1] in course [_2].',$uname,$cid).$linefeed;
+                    } else {
+                        $$logmsg .= &mt('New student role for [_1] in section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
+                    }
                 } else {
-                    $$logmsg .= "Student $uname assigned to unchanged section $sec in course $cid".$linefeed;
+                    if ($sec eq '') {
+                        $$logmsg .= &mt('Student [_1] assigned to course [_2] without a section.',$uname,$cid).$linefeed;
+                    } else {
+                        $$logmsg .= &mt('Student [_1] assigned to section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
+                    }
                 }
             } else {
-                $$logmsg .= "Error when attempting section change for $uname from old section $oldsec to new section: $sec in course $cid -error: $modify_section_result".$linefeed;
+                if ($secchange) {       
+                    $$logmsg .= &mt('Error when attempting section change for [_1] from old section "[_2]" to new section: "[_3]" in course [_4] -error:',$uname,$oldsec,$sec,$cid).' '.$modify_section_result.$linefeed;
+                } else {
+                    $$logmsg .= &mt('Error when attempting to modify role for [_1] for section: "[_2]" in course [_3] -error:',$uname,$sec,$cid).' '.$modify_section_result.$linefeed;
+                }
             }
             $result = $modify_section_result;
         } elsif ($secchange == 1) {
-            $$logmsg .= "Error when attempting to expire role for $uname in old section $oldsec in course $cid -error: $expire_role_result".$linefeed;
+            if ($oldsec eq '') {
+                $$logmsg .= &mt('Error when attempting to expire existing role without a section for [_1] in course [_3] -error: ',$uname,$cid).' '.$expire_role_result.$linefeed;
+            } else {
+                $$logmsg .= &mt('Error when attempting to expire existing role for [_1] in section [_2] in course [_3] -error: ',$uname,$oldsec,$cid).' '.$expire_role_result.$linefeed;
+            }
+            if ($expire_role_result eq 'refused') {
+                my $newsecurl = '/'.$cid;
+                $newsecurl =~ s/\_/\//g;
+                if ($sec ne '') {
+                    $newsecurl.='/'.$sec;
+                }
+                if (&Apache::lonnet::allowed('cst',$newsecurl) && !(&Apache::lonnet::allowed('cst',$oldsecurl))) {
+                    if ($sec eq '') {
+                        $$logmsg .= &mt('Although your current role has privileges to add students to section "[_1]", you do not have privileges to modify existing enrollments unaffiliated with any section.',$sec).$linefeed;
+                    } else {
+                        $$logmsg .= &mt('Although your current role has privileges to add students to section "[_1]", you do not have privileges to modify existing enrollments in other sections.',$sec).$linefeed;
+                    }
+                }
+            }
         }
     } else {
-        $$logmsg .= "Incomplete course id defined.  Addition of user $uname from domain $udom to course $one\_$two, section $sec not completed.$linefeed";
+        $$logmsg .= &mt('Incomplete course id defined.').$linefeed.&mt('Addition of user [_1] from domain [_2] to course [_3], section [_4] not completed.',$uname,$udom,$one.'_'.$two,$sec).$linefeed;
         $result = "error: incomplete course id\n";
     }
     return $result;
@@ -8049,7 +8170,7 @@ sub construct_course {
     }
     if ($args->{'notify_dc'}) {
         if ($uname ne '') { 
-            push(@notified,$uname.'@'.$udom);
+            push(@notified,$uname.':'.$udom);
         }
     }
     if (@notified > 0) {