--- loncom/interface/lonuserutils.pm	2007/11/06 18:23:14	1.3
+++ loncom/interface/lonuserutils.pm	2007/12/05 19:11:37	1.10
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Utility functions for managing LON-CAPA user accounts
 #
-# $Id: lonuserutils.pm,v 1.3 2007/11/06 18:23:14 raeburn Exp $
+# $Id: lonuserutils.pm,v 1.10 2007/12/05 19:11:37 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -35,7 +35,8 @@ use Apache::lonnet;
 use Apache::loncommon();
 use Apache::lonhtmlcommon;
 use Apache::lonlocal;
-use LONCAPA();
+use Apache::longroup;
+use LONCAPA qw(:DEFAULT :match);
 
 ###############################################################
 ###############################################################
@@ -85,28 +86,39 @@ sub modifystudent {
 sub modifyuserrole {
     my ($context,$setting,$changeauth,$cid,$udom,$uname,$uid,$umode,$upass,
         $first,$middle,$last,$gene,$sec,$forceid,$desiredhome,$email,$role,
-        $end,$start) = @_;
-    my ($scope,$userresult,$authresult,$roleresult);
+        $end,$start,$checkid) = @_;
+    my ($scope,$userresult,$authresult,$roleresult,$idresult);
     if ($setting eq 'course' || $context eq 'course') {
         $scope = '/'.$cid;
         $scope =~ s/\_/\//g;
         if ($role ne 'cc' && $sec ne '') {
             $scope .='/'.$sec;
         }
-    } elsif ($setting eq 'domain') {
+    } elsif ($context eq 'domain') {
         $scope = '/'.$env{'request.role.domain'}.'/';
-    } elsif ($setting eq 'construction_space') {
+    } elsif ($context eq 'construction_space') {
         $scope =  '/'.$env{'user.domain'}.'/'.$env{'user.name'};
     }
     if ($context eq 'domain') {
         my $uhome = &Apache::lonnet::homeserver($uname,$udom);
         if ($uhome ne 'no_host') {
-            if (($changeauth) && (&Apache::lonnet::allowed('mau',$udom))) {
+            if (($changeauth eq 'Yes') && (&Apache::lonnet::allowed('mau',$udom))) {
                 if ((($umode =~ /^krb4|krb5|internal$/) && $upass ne '') ||
                     ($umode eq 'localauth')) {
                     $authresult = &Apache::lonnet::modifyuserauth($udom,$uname,$umode,$upass);
                 }
             }
+            if (($forceid) && (&Apache::lonnet::allowed('mau',$udom)) &&
+                ($env{'form.recurseid'}) && ($checkid)) {
+                my %userupdate = (
+                                  lastname   => $last,
+                                  middlename => $middle,
+                                  firstname  => $first,
+                                  generation => $gene,
+                                  id         => $uid,
+                                 );
+                $idresult = &propagate_id_change($uname,$udom,\%userupdate);
+            }
         }
     }
     $userresult =
@@ -114,14 +126,75 @@ sub modifyuserrole {
                                     $middle,$last,$gene,$forceid,$desiredhome,
                                     $email,$role,$start,$end);
     if ($userresult eq 'ok') {
-        if ($role ne '') { 
+        if ($role ne '') {
             $roleresult = &Apache::lonnet::assignrole($udom,$uname,$scope,
                                                       $role,$end,$start);
         }
     }
-    return ($userresult,$authresult,$roleresult);
+    return ($userresult,$authresult,$roleresult,$idresult);
+}
+
+sub propagate_id_change {
+    my ($uname,$udom,$user) = @_;
+    my (@types,@roles,@cdoms);
+    @types = ('active','future');
+    @roles = ('st');
+    my $idresult;
+    my %roleshash = &Apache::lonnet::get_my_roles($uname,
+                        $udom,'userroles',\@types,\@roles,\@cdoms);
+    foreach my $item (keys(%roleshash)) {
+        my ($cnum,$cdom,$role) = split(/:/,$item);
+        my ($start,$end) = split(/:/,$roleshash{$item});
+        if (&Apache::lonnet::is_course($cdom,$cnum)) {
+            my %userupdate;
+            my $result = &update_classlist($cdom,$cnum,$udom,$uname,\%userupdate);
+            if ($result eq 'ok') {
+                $idresult .= "Classlist change: $uname:$udom - class -> $cnum:$cdom\n";
+            } else {
+                $idresult .= "Error - $result -during classlist update for $uname:$udom in $cnum:$cdom\n";
+            }
+        }
+    }
+    return $idresult;
 }
 
+sub update_classlist {
+    my ($cdom,$cnum,$udom,$uname,$user) = @_;
+    my ($uid,$classlistentry);
+    my $fullname =
+        &Apache::lonnet::format_name($user->{'firstname'},$user->{'middlename'},
+                                     $user->{'lastname'},$user->{'generation'},
+                                     'lastname');
+    my %classhash = &Apache::lonnet::get('classlist',[$uname.':'.$udom],
+                                         $cdom,$cnum);
+    my @classinfo = split(/:/,$classhash{$uname.':'.$udom});
+    my $ididx=&Apache::loncoursedata::CL_ID() - 2;
+    my $nameidx=&Apache::loncoursedata::CL_FULLNAME() - 2;
+    for (my $i=0; $i<@classinfo; $i++) {
+        if ($i == $ididx) {
+            if (defined($user->{'id'})) {
+                $classlistentry .= $user->{'id'}.':';
+            } else {
+                $classlistentry .= $classinfo[$i].':';
+            }
+        } elsif ($i == $nameidx) {
+            $classlistentry .= $fullname.':';
+        } else {
+            $classlistentry .= $classinfo[$i].':';
+        }
+    }
+    $classlistentry =~ s/:$//;
+    my $reply=&Apache::lonnet::cput('classlist',
+                                    {"$uname:$udom" => $classlistentry},
+                                    $cdom,$cnum);
+    if (($reply eq 'ok') || ($reply eq 'delayed')) {
+        return 'ok';
+    } else {
+        return 'error: '.$reply;
+    }
+}
+
+
 ###############################################################
 ###############################################################
 # build a role type and role selection form
@@ -156,12 +229,18 @@ sub domain_roles_select {
             @roles = &construction_space_roles();
         } else {
             @roles = &course_roles('domain');
+            unshift(@roles,'cr');
         }
         my $order = ['Any',@roles];
         $select_menus{$roletype}->{'order'} = $order; 
         foreach my $role (@roles) {
-            $select_menus{$roletype}->{'select2'}->{$role} = 
-                          &Apache::lonnet::plaintext($role);
+            if ($role eq 'cr') {
+                $select_menus{$roletype}->{'select2'}->{$role} =
+                              &mt('Custom role');
+            } else {
+                $select_menus{$roletype}->{'select2'}->{$role} = 
+                              &Apache::lonnet::plaintext($role);
+            }
         }
         $select_menus{$roletype}->{'select2'}->{'Any'} = &mt('Any');
     }
@@ -637,8 +716,12 @@ sub print_upload_manager_footer {
     my ($options,$cb_script,$coursepick) = &default_role_selector($context,'defaultrole',1);
     if ($context eq 'domain') {
         $Str .= '<span class="LC_role_level">'.&mt('Domain Level').'</span><br />'.$options.'<br /><br /><span class="LC_role_level">'.&mt('Course Level').'</span><br />'.$cb_script.$coursepick;
-    } else {
+    } elsif ($context eq 'construction_space') {
         $Str .= $options;
+    } else {
+        $Str .= '<table><tr><td><span class="LC_nobreak"<b>'.&mt('role').':&nbsp;</b>'.
+                $options.'</span></td><td>&nbsp;</td><td><span class="LC_nobreak">'.
+                '<b>'.&mt('section').':&nbsp;</b><input type="text" name="section" value="" size="12" /></span></td></tr></table>';
     }
     if ($context eq 'course') {
         $Str .= "<h3>".&mt('Full Update')."</h3>\n".
@@ -646,12 +729,9 @@ sub print_upload_manager_footer {
                 ' '.&mt('Full update (also print list of users not enrolled anymore)').
                 "</label></p>\n";
     }
-    $Str .= "<h3>".&mt('ID/Student Number')."</h3>\n";
-    $Str .= "<p>\n".'<label><input type="checkbox" name="forceid" value="yes">';
-    $Str .= &mt('Disable ID/Student Number Safeguard and Force Change '.
-                'of Conflicting IDs').
-                '</label><br />'."\n".
-                &mt('(only do if you know what you are doing.)')."</p><p>\n";
+    if ($context eq 'course' || $context eq 'domain') {
+        $Str .= &forceid_change($context);
+    }
     $Str .= '</div><div class="LC_clear_float_footer"><br /><input type="button"'.
               'onClick="javascript:verify(this.form,this.form.csec)" '.
         'value="Update Users" />'."<br />\n";
@@ -664,6 +744,23 @@ sub print_upload_manager_footer {
     return;
 }
 
+sub forceid_change {
+    my ($context) = @_;
+    my $output = 
+        "<h3>".&mt('ID/Student Number')."</h3>\n".
+        "<p>\n".'<label><input type="checkbox" name="forceid" value="yes">'.
+        &mt('Disable ID/Student Number Safeguard and Force Change '.
+        'of Conflicting IDs').'</label><br />'."\n".
+        &mt('(only do if you know what you are doing.)')."</br><br />\n";
+    if ($context eq 'domain') {
+        $output .= '<label><input type="checkbox" name="recurseid"'.
+                   ' value="yes">'. 
+  &mt('Update ID/Student Number in courses in which user is an Active or Future student, (if forcing change).').
+                   '</label></p>'."\n";
+    }
+    return $output;
+}
+
 ###############################################################
 ###############################################################
 sub print_upload_manager_form {
@@ -1229,9 +1326,10 @@ sub print_userlist {
                         my $cnum = $coursehash{'num'};
                         my $cdesc = $coursehash{'description'};
                         my (@roles,@sections,%access,%users,%userdata,
-                            %users,%statushash);
+                            %statushash);
                         if ($env{'form.showrole'} eq 'Any') {
                             @roles = &course_roles($context);
+                            unshift(@roles,'cr');
                         } else {
                             @roles = ($env{'form.showrole'});
                         }
@@ -1260,6 +1358,8 @@ sub print_userlist {
                     &gather_userinfo($context,$format,\%userlist,$indexhash,
                                      \%userinfo,\%allusers);
                 } else {
+                    $r->print('<input type="hidden" name="phase" value="'.
+                              $env{'form.phase'}.'" /></form>');
                     return;
                 }
             }
@@ -1281,15 +1381,20 @@ sub print_userlist {
         }
     } else {
         # Print out the available choices
+        my $usercount;
         if ($env{'form.action'} eq 'modifystudent') {
-            &show_users_list($r,$context,'view','modify',
-                             $env{'form.Status'},\%userlist,$keylist);
+            ($usercount) = &show_users_list($r,$context,'view',$permission,
+                                 $env{'form.Status'},\%userlist,$keylist);
         } else {
-            &show_users_list($r,$context,$env{'form.output'},'aboutme',
-                             $env{'form.Status'},\%userlist,$keylist);
+            ($usercount) = &show_users_list($r,$context,$env{'form.output'},
+                               $permission,$env{'form.Status'},\%userlist,$keylist);
+        }
+        if (!$usercount) {
+            $r->print('<br />'.&mt('There are no users matching the search criteria.')); 
         }
     }
-    $r->print('</form>');
+    $r->print('<input type="hidden" name="phase" value="'.
+              $env{'form.phase'}.'" /></form>');
 }
 
 sub list_submit_button {
@@ -1388,11 +1493,11 @@ sub courses_selector {
     my %idnums = ();
     my %idlist_titles = ();
     my $caller = 'global';
-    my $totcodes = 0;
     my $format_reply;
     my $jscript = '';
 
-    my $totcodes =
+    my $totcodes = 0;
+    $totcodes =
         &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,
                                                       $cdom,$totcodes);
     if ($totcodes > 0) {
@@ -1594,7 +1699,7 @@ sub process_date_info {
 }
 
 sub show_users_list {
-    my ($r,$context,$mode,$linkto,$statusmode,$userlist,$keylist)=@_;
+    my ($r,$context,$mode,$permission,$statusmode,$userlist,$keylist)=@_;
     #
     # Variables for excel output
     my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
@@ -1647,6 +1752,29 @@ END
     }
     unless ($mode eq 'autoenroll') {
         $r->print(<<END);
+
+<script type="text/javascript" language="Javascript">
+
+function username_display_launch(username,domain) {
+    var target;
+    for (var i=0; i<document.studentform.usernamelink.length; i++) {
+        if (document.studentform.usernamelink[i].checked) {
+            target = document.studentform.usernamelink[i].value;
+        }
+    }
+    if (target == 'modify') {
+        document.studentform.srchterm.value=username;
+        document.studentform.srchdomain.value=domain;
+        document.studentform.phase.value='get_user_info';
+        document.studentform.action.value = 'singleuser';
+        document.studentform.submit();
+    }
+    else {
+        document.location.href = '/adm/'+domain+'/'+username+'/aboutme';
+    }
+}
+</script>
+
 <input type="hidden" name="state" value="$env{'form.state'}" />
 END
     }
@@ -1670,6 +1798,9 @@ END
                        'clicker'    => "clicker id",
                        'photo'      => "photo",
                        'extent'     => "extent",
+                       'link'       => "Behavior of username links",
+                       'aboutme'    => "Display a user's personal page",
+                       'modify'     => "Modify a user's information",
                       );
     if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
         $lt{'extent'} = &mt('Course(s): description, section(s), status');
@@ -1699,73 +1830,98 @@ END
     }
     push(@cols,'email');
 
-    my $rolefilter;
-    if ($env{'form.showrole'} ne 'Any') {
+    my $rolefilter = $env{'form.showrole'};
+    if ($env{'form.showrole'} eq 'cr') {
+        $rolefilter = &mt('custom');  
+    } elsif ($env{'form.showrole'} ne 'Any') {
         $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'});
     }
     my $results_description = &results_header_row($rolefilter,$statusmode,
                                                   $context);
     $r->print('<b>'.$results_description.'</b><br />');
+    my $output;
     if ($mode eq 'html' || $mode eq 'view') {
-        if ($linkto eq 'aboutme') {
-            $r->print(&mt("Select a user name to view the user's personal page."));
-        } elsif ($linkto eq 'modify') {
-            $r->print(&mt("Select a user name to modify the user's information"));
-        }
         $r->print(<<END);
-<input type="hidden" name="sname"  value="" />
-<input type="hidden" name="sdom"   value="" />
+<input type="hidden" name="srchby"  value="uname" />
+<input type="hidden" name="srchin"   value="dom" />
+<input type="hidden" name="srchtype" value="exact" />
+<input type="hidden" name="srchterm" value="" />
+<input type="hidden" name="srchdomain" value="" />
 END
-        $r->print("\n<p>\n".
+         if ($mode ne 'autoenroll') {
+             $output = '<p>';
+             my @linkdests = ('aboutme');
+             if ($permission->{'cusr'}) {
+                 push (@linkdests,'modify');
+                 $output .= '<span class="LC_nobreak">'.$lt{'link'}.':&nbsp;';
+                 my $usernamelink = $env{'form.usernamelink'};
+                 if ($usernamelink eq '') {
+                     $usernamelink = 'aboutme';
+                 }
+                 foreach my $item (@linkdests) {
+                     my $checkedstr = '';
+                     if ($item eq $usernamelink) {
+                         $checkedstr = ' checked="checked" ';
+                     }
+                     $output .= '<label><input type="radio" name="usernamelink" value="'.$item.'"'.$checkedstr.'>&nbsp;'.$lt{$item}.'</label>&nbsp;&nbsp;';
+                 }
+                 $output .= '</span><br />';
+             } else {
+                 $output .= &mt("Click on a username to view the user's personal page.").'<br />';
+             }
+        }
+        $output .= "\n<p>\n".
                   &Apache::loncommon::start_data_table().
-                  &Apache::loncommon::start_data_table_header_row());
+                  &Apache::loncommon::start_data_table_header_row();
         if ($mode eq 'autoenroll') {
-            $r->print("
+            $output .= "
  <th><a href=\"javascript:document.studentform.sortby.value='type';document.studentform.submit();\">$lt{'type'}</a></th>
-            ");
+            ";
         } else {
-            $r->print("
+            $output .= "
 <th>Count</th>
-            ");
+            ";
         }
         foreach my $item (@cols) {
-            $r->print("<th><a href=\"javascript:document.studentform.sortby.value='$item';document.studentform.submit();\">$lt{$item}</a></th>\n");
+            $output .= "<th><a href=\"javascript:document.studentform.sortby.value='$item';document.studentform.submit();\">$lt{$item}</a></th>\n";
         }
         my %role_types = &role_type_names();
         if ($context eq 'course') {
-            # Clicker display on or off?
-            my %clicker_options = &Apache::lonlocal::texthash(
-                                                        'on' => 'Show',
-                                                        'off' => 'Hide',
-                                                       );
-            my $clickerchg = 'on';
-            if ($displayclickers eq 'on') {
-                $clickerchg = 'off';
-            }
-            $r->print('    <th>'."\n".'     '.
-                '<a href="javascript:document.studentform.displayclickers.value='.
+            if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
+                # Clicker display on or off?
+                my %clicker_options = &Apache::lonlocal::texthash(
+                                                            'on' => 'Show',
+                                                            'off' => 'Hide',
+                                                           );
+                my $clickerchg = 'on';
+                if ($displayclickers eq 'on') {
+                    $clickerchg = 'off';
+                }
+                $output .= '    <th>'."\n".'     '.
+                    '<a href="javascript:document.studentform.displayclickers.value='.
                       "'".$clickerchg."'".';document.studentform.submit();">'.
                       $clicker_options{$clickerchg}.'</a>&nbsp;'.$lt{'clicker'}."\n".
-                      '    </th>'."\n");
+                      '    </th>'."\n";
 
-            # Photo display on or off?
-            if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
-                my %photo_options = &Apache::lonlocal::texthash(
-                                                        'on' => 'Show',
-                                                        'off' => 'Hide',
-                                                            );
-                my $photochg = 'on';
-                if ($displayphotos eq 'on') {
-                    $photochg = 'off';
-                }
-                $r->print('    <th>'."\n".'     '.
-            '<a href="javascript:document.studentform.displayphotos.value='.
+                # Photo display on or off?
+                if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
+                    my %photo_options = &Apache::lonlocal::texthash(
+                                                            'on' => 'Show',
+                                                            'off' => 'Hide',
+                                                                );
+                    my $photochg = 'on';
+                    if ($displayphotos eq 'on') {
+                        $photochg = 'off';
+                    }
+                    $output .= '    <th>'."\n".'     '.
+                '<a href="javascript:document.studentform.displayphotos.value='.
                       "'".$photochg."'".';document.studentform.submit();">'.
                       $photo_options{$photochg}.'</a>&nbsp;'.$lt{'photo'}."\n".
-                      '    </th>'."\n");
+                      '    </th>'."\n";
+                }
             }
-            $r->print(&Apache::loncommon::end_data_table_header_row());
-        } 
+            $output .= &Apache::loncommon::end_data_table_header_row();
+        }
 # Done with the HTML header line
     } elsif ($mode eq 'csv') {
         #
@@ -1805,10 +1961,16 @@ END
     foreach my $idx (@$keylist) {
         $index{$idx} = $i++;
     }
+    my $usercount = 0;
     # Get groups, role, permanent e-mail so we can sort on them if
     # necessary.
     foreach my $user (keys(%{$userlist})) {
         my ($uname,$udom,$role,$groups,$email);
+        if (($statusmode ne 'Any') && 
+                 ($userlist->{$user}->[$index{'status'}] ne $statusmode)) {
+            delete($userlist->{$user});
+            next;
+        }
         if ($context eq 'domain') {
             if ($env{'form.roletype'} eq 'domain') {
                 ($role,$uname,$udom) = split(/:/,$user);
@@ -1839,8 +2001,21 @@ END
         if ($emails{'permanentemail'} =~ /\S/) {
             $userlist->{$user}->[$index{'email'}] = $emails{'permanentemail'};
         }
+        $usercount ++;
+    }
+    my $autocount = 0;
+    my $manualcount = 0;
+    my $lockcount = 0;
+    my $unlockcount = 0;
+    if ($usercount) {
+        $r->print($output);
+    } else {
+        if ($mode eq 'autoenroll') {
+            return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
+        } else {
+            return;
+        }
     }
-
     #
     # Sort the users
     my $index  = $index{$sortby};
@@ -1852,18 +2027,14 @@ END
         lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second])            ||
         lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
         } (keys(%$userlist));
-    my $usercount = 0;
-    my $autocount = 0;
-    my $manualcount = 0;
-    my $lockcount = 0;
-    my $unlockcount = 0;
+    my $rowcount = 0;
     foreach my $user (@sorted_users) {
-        my $sdata = $userlist->{$user};
         my %in;
+        my $sdata = $userlist->{$user};
+        $rowcount ++; 
         foreach my $item (@{$keylist}) {
             $in{$item} = $sdata->[$index{$item}];
         }
-        next if (($statusmode ne 'Any') && ($in{'status'} ne $statusmode));
         $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}]); 
         if (! defined($in{'start'}) || $in{'start'} == 0) {
             $in{'start'} = &mt('none');
@@ -1875,45 +2046,36 @@ END
         } else {
             $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
         }
-        $usercount ++;
         if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') {
             $r->print(&Apache::loncommon::start_data_table_row());
-            $r->print("<td>$usercount</td>\n");
-            if ($linkto eq 'aboutme') {
-                $in{'username'} = 
-                    &Apache::loncommon::aboutmewrapper($in{'username'},
-                                                       $in{'username'},
-                                                       $in{'domain'});
-            } elsif ($linkto eq 'modify') {
-                $in{'username'} = '<a href="'.
-                          "javascript:document.studentform.sname.value='".
-                           $in{'username'}.
-                           "';document.studentform.sdom.value='".$in{'domain'}.
-                           "';document.studentform.state.value='selected".
-                           "';document.studentform.submit();".'">'.
-                           $in{'username'}."</a>\n";
-            }
+            $r->print("<td>$rowcount</td>\n");
             foreach my $item (@cols) {
-                $r->print('<td>'.$in{$item}.'</td>'."\n");
+                if ($item eq 'username') {
+                    $r->print('<td>'.&print_username_link($permission,\%in).'</td>');
+                } else {
+                    $r->print('<td>'.$in{$item}.'</td>'."\n");
+                }
             }
             if ($context eq 'course') {
-                if ($displayclickers eq 'on') {
-                    my $clickers =
+                if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
+                    if ($displayclickers eq 'on') {
+                        my $clickers =
                    (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
-                    if ($clickers!~/\w/) { $clickers='-'; }
-                    $r->print('<td>'.$clickers.'</td>');
-                } else {
-                    $r->print('    <td>&nbsp;</td>  ');
-                }
-                if ($displayphotos eq 'on') {
-                    if ($env{'course.'.$env{'request.course.id'}.
-                        '.internal.showphoto'}) {
-                        my $imgurl =
-               &Apache::lonnet::retrievestudentphoto($in{'domain'},$in{'username'},'gif','thumbnail');
-                        $r->print('    <td align="right"><a href="javascript:photowindow('."'".&Apache::lonnet::studentphoto($in{'domain'},$in{'username'},'jpg')."'".')"><img src="'.$imgurl.'" border="1"></a></td>');
+                        if ($clickers!~/\w/) { $clickers='-'; }
+                        $r->print('<td>'.$clickers.'</td>');
                     } else {
                         $r->print('    <td>&nbsp;</td>  ');
                     }
+                    if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
+                        if ($displayphotos eq 'on' && $sdata->[$index{'role'}] eq 'st') {
+                            my $imgurl =
+                        &Apache::lonnet::retrievestudentphoto($in{'domain'},$in{'username'},
+                                                          'gif','thumbnail');
+                            $r->print('    <td align="right"><a href="javascript:photowindow('."'".&Apache::lonnet::studentphoto($in{'domain'},$in{'username'},'jpg')."'".')"><img src="'.$imgurl.'" border="1"></a></td>');
+                        } else {
+                            $r->print('    <td>&nbsp;</td>  ');
+                        }
+                    }
                 }
             }
             $r->print(&Apache::loncommon::end_data_table_row());
@@ -1968,8 +2130,24 @@ END
     }
     if ($mode eq 'autoenroll') {
         return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
+    } else {
+        return ($usercount);
     }
-    return;
+}
+
+sub print_username_link {
+    my ($permission,$in) = @_;
+    my $output;
+    if (!$permission->{'cusr'}) {
+        $output = &Apache::loncommon::aboutmewrapper($in->{'username'},
+                                                     $in->{'username'},
+                                                     $in->{'domain'});
+    } else {
+        $output = '<a href="javascript:username_display_launch('.
+                  "'$in->{'username'}','$in->{'domain'}'".')" />'.
+                  $in->{'username'}.'</a>';
+    }
+    return $output;
 }
 
 sub role_type_names {
@@ -1983,16 +2161,19 @@ sub role_type_names {
 
 sub results_header_row {
     my ($rolefilter,$statusmode,$context) = @_;
-    my $description;
+    my ($description,$showfilter);
+    if ($rolefilter ne 'Any') {
+        $showfilter = $rolefilter;
+    }
     if ($context eq 'course') {
         $description = &mt('Course - ').$env{'course.'.$env{'request.course.id'}.'.description'}.': ';
         if ($statusmode eq 'Expired') {
-            $description .= &mt('Users in course with expired [_1] roles',$rolefilter);
+            $description .= &mt('Users in course with expired [_1] roles',$showfilter);
         }
         if ($statusmode eq 'Future') {
-            $description .= &mt('Users in course with future [_1] roles',$rolefilter);
+            $description .= &mt('Users in course with future [_1] roles',$showfilter);
         } elsif ($statusmode eq 'Active') {
-            $description .= &mt('Users in course with active [_1] roles',$rolefilter);
+            $description .= &mt('Users in course with active [_1] roles',$showfilter);
         } else {
             if ($rolefilter eq 'Any') {
                 $description .= &mt('All users in course');
@@ -2003,14 +2184,14 @@ sub results_header_row {
     } elsif ($context eq 'construction_space') {
         $description = &mt('Author space for [_1].').' ';
         if ($statusmode eq 'Expired') {
-            $description .= &mt('Co-authors with expired [_1] roles',$rolefilter);
+            $description .= &mt('Co-authors with expired [_1] roles',$showfilter);
         } elsif ($statusmode eq 'Future') {
-            $description .= &mt('Co-authors with future [_1] roles',$rolefilter);
+            $description .= &mt('Co-authors with future [_1] roles',$showfilter);
         } elsif ($statusmode eq 'Active') {
-            $description .= &mt('Co-authors with active [_1] roles',$rolefilter);
+            $description .= &mt('Co-authors with active [_1] roles',$showfilter);
         } else {
             if ($rolefilter eq 'Any') {
-                $description .= &mt('All co-authors',$rolefilter);
+                $description .= &mt('All co-authors');
             } else {
                 $description .= &mt('All co-authors with [_1] roles',$rolefilter);
             }
@@ -2020,28 +2201,28 @@ sub results_header_row {
         $description = &mt('Domain - ').$domdesc.': ';
         if ($env{'form.roletype'} eq 'domain') {
             if ($statusmode eq 'Expired') {
-                $description .= &mt('Users in domain with expired [_1] roles',$rolefilter);
+                $description .= &mt('Users in domain with expired [_1] roles',$showfilter);
             } elsif ($statusmode eq 'Future') {
-                $description .= &mt('Users in domain with future [_1] roles',$rolefilter);
+                $description .= &mt('Users in domain with future [_1] roles',$showfilter);
             } elsif ($statusmode eq 'Active') {
-                $description .= &mt('Users in domain with active [_1] roles',$rolefilter);
+                $description .= &mt('Users in domain with active [_1] roles',$showfilter);
             } else {
                 if ($rolefilter eq 'Any') {
-                    $description .= &mt('All users in domain',$rolefilter);
+                    $description .= &mt('All users in domain');
                 } else {
                     $description .= &mt('All users in domain with [_1] roles',$rolefilter);
                 }
             }
         } elsif ($env{'form.roletype'} eq 'construction_space') {
             if ($statusmode eq 'Expired') {
-                $description .= &mt('Co-authors in domain with expired [_1] roles',$rolefilter);
+                $description .= &mt('Co-authors in domain with expired [_1] roles',$showfilter);
             } elsif ($statusmode eq 'Future') {
-                $description .= &mt('Co-authors in domain with future [_1] roles',$rolefilter);
+                $description .= &mt('Co-authors in domain with future [_1] roles',$showfilter);
             } elsif ($statusmode eq 'Active') {
-               $description .= &mt('Co-authors in domain with active [_1] roles',$rolefilter);
+               $description .= &mt('Co-authors in domain with active [_1] roles',$showfilter);
             } else {
                 if ($rolefilter eq 'Any') {
-                    $description .= &mt('All users with co-author roles in domain',$rolefilter);
+                    $description .= &mt('All users with co-author roles in domain',$showfilter);
                 } else {
                     $description .= &mt('All co-authors in domain  with [_1] roles',$rolefilter);
                 }
@@ -2061,11 +2242,11 @@ sub results_header_row {
                 $description .= &mt('All courses in domain').' - ';
             }
             if ($statusmode eq 'Expired') {
-                $description .= &mt('users with expired [_1] roles',$rolefilter);
+                $description .= &mt('users with expired [_1] roles',$showfilter);
             } elsif ($statusmode eq 'Future') {
-                $description .= &mt('users with future [_1] roles',$rolefilter);
+                $description .= &mt('users with future [_1] roles',$showfilter);
             } elsif ($statusmode eq 'Active') {
-                $description .= &mt('users with active [_1] roles',$rolefilter);
+                $description .= &mt('users with active [_1] roles',$showfilter);
             } else {
                 if ($rolefilter eq 'Any') {
                     $description .= &mt('all users');
@@ -2399,6 +2580,7 @@ sub upfile_drop_add {
             # Get information about course groups
             %curr_groups = &Apache::longroup::coursegroups();
         }
+        my (%curr_rules,%got_rules,%alerts);
         # Get new users list
         foreach (@userdata) {
             my %entries=&Apache::loncommon::record_sep($_);
@@ -2430,7 +2612,7 @@ sub upfile_drop_add {
           $entries{$fields{'username'}},$fname,$mname,$lname,$gen).
                               '</b>');
                 } else {
-                    my $username = $entries{$fields{'username'}}; 
+                    my $username = $entries{$fields{'username'}};
                     my $sec;
                     if ($context eq 'course' || $setting eq 'course') {
                         # determine section number
@@ -2501,6 +2683,48 @@ sub upfile_drop_add {
                              \$lname,\$gen,\$sec,\$role) {
                         $$_ =~ s/(\s+$|^\s+)//g;
                     }
+                    # check against rules
+                    my $checkid = 0;
+                    my $newuser = 0;
+                    my (%rulematch,%inst_results,%idinst_results);
+                    my $uhome=&Apache::lonnet::homeserver($username,$domain);
+                    if ($uhome eq 'no_host') {
+                        $checkid = 1;
+                        $newuser = 1;
+                        my $checkhash;
+                        my $checks = { 'username' => 1 };
+                        $checkhash->{$username.':'.$domain} = { 'newuser' => 1, };
+                        &Apache::loncommon::user_rule_check($checkhash,$checks,
+                            \%alerts,\%rulematch,\%inst_results,\%curr_rules,
+                            \%got_rules);
+                        if (ref($alerts{'username'}) eq 'HASH') {
+                            if (ref($alerts{'username'}{$domain}) eq 'HASH') {
+                                next if ($alerts{'username'}{$domain}{$username});
+                            }
+                        }
+                    }
+                    if ($id ne '') {
+                        if (!$newuser) {
+                            my %idhash = &Apache::lonnet::idrget($domain,($username));
+                            if ($idhash{$username} ne $id) {
+                                $checkid = 1;
+                            }
+                        }
+                        if ($checkid) {
+                            my $checkhash;
+                            my $checks = { 'id' => 1 };
+                            $checkhash->{$username.':'.$domain} = { 'newuser' => $newuser,
+                                                                    'id'  => $id };
+                            &Apache::loncommon::user_rule_check($checkhash,$checks,
+                                \%alerts,\%rulematch,\%idinst_results,\%curr_rules,
+                                \%got_rules);
+                            if (ref($alerts{'id'}) eq 'HASH') {
+                                if (ref($alerts{'id'}{$domain}) eq 'HASH') {
+                                    next if ($alerts{'id'}{$domain}{$id});
+                                }
+                            }
+                        }
+                    }
                     if ($password || $env{'form.login'} eq 'loc') {
                         my ($userresult,$authresult,$roleresult);
                         if ($role eq 'st') {
@@ -2519,7 +2743,7 @@ sub upfile_drop_add {
                                     $id,$amode,$password,$fname,
                                     $mname,$lname,$gen,$sec,
                                     $env{'form.forceid'},$desiredhost,
-                                    $email,$role,$enddate,$startdate);
+                                    $email,$role,$enddate,$startdate,$checkid);
                         }
                         $flushc = 
                             &user_change_result($r,$userresult,$authresult,
@@ -2544,9 +2768,7 @@ sub upfile_drop_add {
             }
         } # end of foreach (@userdata)
         # Flush the course logs so reverse user roles immediately updated
-        if ($context eq 'course' || ($context eq 'domain' && $setting eq 'course')) {
-            &Apache::lonnet::flushcourselogs();
-        }
+        &Apache::lonnet::flushcourselogs();
         $r->print("</p>\n<p>\n".&mt('Processed [_1] user(s).',$counts{'user'}).
                   "</p>\n");
         if ($counts{'role'} > 0) {
@@ -2558,6 +2780,39 @@ sub upfile_drop_add {
                       &mt('Authentication changed for [_1] existing users.',
                           $counts{'auth'})."</p>\n");
         }
+        if (keys(%alerts) > 0) {
+            if (ref($alerts{'username'}) eq 'HASH') {
+                foreach my $dom (sort(keys(%{$alerts{'username'}}))) {
+                    my $count;
+                    if (ref($alerts{'username'}{$dom}) eq 'HASH') {
+                        $count = keys(%{$alerts{'username'}{$dom}});
+                    } 
+                    my $domdesc = &Apache::lonnet::domain($domain,'description');
+                    if (ref($curr_rules{$dom}) eq 'HASH') {
+                        $r->print(&Apache::loncommon::instrule_disallow_msg(
+                                 'username',$domdesc,$count,'upload'));
+                    }
+                    $r->print(&Apache::loncommon::user_rule_formats($dom,
+                              $domdesc,$curr_rules{$dom}{'username'},
+                             'username'));
+                }
+            }
+            if (ref($alerts{'id'}) eq 'HASH') {
+                foreach my $dom (sort(keys(%{$alerts{'id'}}))) {
+                    my $count;
+                    if (ref($alerts{'id'}{$dom}) eq 'HASH') {
+                        $count = keys(%{$alerts{'id'}{$dom}});
+                    }
+                    my $domdesc = &Apache::lonnet::domain($domain,'description');
+                    if (ref($curr_rules{$dom}) eq 'HASH') {
+                        $r->print(&Apache::loncommon::instrule_disallow_msg(
+                                 'id',$domdesc,$count,'upload'));
+                    }
+                    $r->print(&Apache::loncommon::user_rule_formats($dom,
+                              $domdesc,$curr_rules{$dom}{'id'},'id'));
+                }
+            }
+        }
         $r->print('<form name="uploadresult" action="/adm/createuser">');
         $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','prevphase','currstate']));
         $r->print('</form>');
@@ -2673,15 +2928,37 @@ sub expire_user_list {
     $r->print('<p>'.&mt('Re-enrollment will re-activate data.')) if ($count);
 }
 
-sub section_check_js {
-    my $groupslist;
-    my %curr_groups = &Apache::longroup::coursegroups();
-    if (%curr_groups) {
-        $groupslist = join('","',sort(keys(%curr_groups)));
+sub classlist_drop {
+    my ($scope,$uname,$udom,$now,$action) = @_;
+    my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
+    my $cid=$cdom.'_'.$cnum;
+    my $user = $uname.':'.$udom;
+    if ($action eq 'drop') {
+        if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
+            my $result =
+                &Apache::lonnet::cput('classlist',
+                                      { $user => $now },
+                                      $env{'course.'.$cid.'.domain'},
+                                      $env{'course.'.$cid.'.num'});
+            return &mt('Drop from classlist: [_1]',
+                       '<b>'.$result.'</b>').'<br />';
+        }
     }
+}
+
+sub active_student_roles {
+    my ($cnum,$cdom,$uname,$udom) = @_;
+    my %roles =
+        &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
+                                      ['future','active'],['st']);
+    return exists($roles{"$cnum:$cdom:st"});
+}
+
+sub section_check_js {
+    my $groupslist= &get_groupslist();
     return <<"END";
 function validate(caller) {
-    var groups = new Array("$groupslist");
+    var groups = new Array($groupslist);
     var secname = caller.value;
     if ((secname == 'all') || (secname == 'none')) {
         alert("'"+secname+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
@@ -2728,5 +3005,144 @@ sub set_login {
     return $response;
 }
 
+sub course_sections {
+    my ($sections_count,$role) = @_;
+    my $output = '';
+    my @sections = (sort {$a <=> $b} keys %{$sections_count});
+    if (scalar(@sections) == 1) {
+        $output = '<select name="currsec_'.$role.'" >'."\n".
+                  '  <option value="">Select</option>'."\n".
+                  '  <option value="">No section</option>'."\n".
+                  '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
+    } else {
+        $output = '<select name="currsec_'.$role.'" ';
+        my $multiple = 4;
+        if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
+        $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
+        foreach my $sec (@sections) {
+            $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
+        }
+    }
+    $output .= '</select>';
+    return $output;
+}
+
+sub get_groupslist {
+    my $groupslist;
+    my %curr_groups = &Apache::longroup::coursegroups();
+    if (%curr_groups) {
+        $groupslist = join('","',sort(keys(%curr_groups)));
+        $groupslist = '"'.$groupslist.'"';
+    }
+    return $groupslist;
+}
+
+sub setsections_javascript {
+    my ($form,$groupslist) = @_;
+    my ($checkincluded,$finish,$roleplace,$setsection_js);
+    if ($form eq 'cu') {
+        $checkincluded = 'formname.elements[i-1].checked == true';
+        $finish = 'formname.submit()';
+        $roleplace = 3;
+    } else {
+        $checkincluded = 'formname.name == "'.$form.'"';
+        $finish = "seccheck = 'ok';";
+        $roleplace = 1;
+        $setsection_js = "var seccheck = 'alert';";
+    }
+    my %alerts = &Apache::lonlocal::texthash(
+                    secd => 'Section designations do not apply to Course Coordinator roles.',
+                    accr => 'A course coordinator role will be added with access to all sections.',
+                    inea => 'In each course, each user may only have one student role at a time.',
+                    youh => 'You had selected ',
+                    secs => 'sections.',
+                    plmo => 'Please modify your selections so they include no more than one section.',
+                    mayn => 'may not be used as the name for a section, as it is a reserved word.',
+                    plch => 'Please choose a different section name.',
+                    mnot => 'may not be used as a section name, as it is the name of a course group.',
+                    secn => 'Section names and group names must be distinct. Please choose a different section name.',
+                 );
+    $setsection_js .= <<"ENDSECCODE";
+
+function setSections(formname) {
+    var re1 = /^currsec_/;
+    var groups = new Array($groupslist);
+    for (var i=0;i<formname.elements.length;i++) {
+        var str = formname.elements[i].name;
+        var checkcurr = str.match(re1);
+        if (checkcurr != null) {
+            if ($checkincluded) {
+                var match = str.split('_');
+                var role = match[$roleplace];
+                if (role == 'cc') {
+                    alert("$alerts{'secd'}\\n$alerts{'accr'}");
+                }
+                else {
+                    var sections = '';
+                    var numsec = 0;
+                    var sections;
+                    for (var j=0; j<formname.elements[i].length; j++) {
+                        if (formname.elements[i].options[j].selected == true ) {
+                            if (formname.elements[i].options[j].value != "") {
+                                if (numsec == 0) {
+                                    if (formname.elements[i].options[j].value != "") {
+                                        sections = formname.elements[i].options[j].value;
+                                        numsec ++;
+                                    }
+                                }
+                                else {
+                                    sections = sections + "," +  formname.elements[i].options[j].value
+                                    numsec ++;
+                                }
+                            }
+                        }
+                    }
+                    if (numsec > 0) {
+                        if (formname.elements[i+1].value != "" && formname.elements[i+1].value != null) {
+                            sections = sections + "," +  formname.elements[i+1].value;
+                        }
+                    }
+                    else {
+                        sections = formname.elements[i+1].value;
+                    }
+                    var newsecs = formname.elements[i+1].value;
+                    var numsplit;
+                    if (newsecs != null && newsecs != "") {
+                        numsplit = newsecs.split(/,/g);
+                        numsec = numsec + numsplit.length;
+                    }
+
+                    if ((role == 'st') && (numsec > 1)) {
+                        alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}")
+                        return;
+                    }
+                    else {
+                        if (numsplit != null) {
+                            for (var j=0; j<numsplit.length; j++) {
+                                if ((numsplit[j] == 'all') ||
+                                    (numsplit[j] == 'none')) {
+                                    alert("'"+numsplit[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
+                                    return;
+                                }
+                                for (var k=0; k<groups.length; k++) {
+                                    if (numsplit[j] == groups[k]) {
+                                        alert("'"+numsplit[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
+                                        return;
+                                    }
+                                }
+                            }
+                        }
+                        formname.elements[i+2].value = sections;
+                    }
+                }
+            }
+        }
+    }
+    $finish
+}
+ENDSECCODE
+    return $setsection_js;
+}
+
 1;