--- loncom/interface/lonuserutils.pm	2007/12/11 01:17:16	1.13
+++ loncom/interface/lonuserutils.pm	2007/12/21 17:27:57	1.20
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Utility functions for managing LON-CAPA user accounts
 #
-# $Id: lonuserutils.pm,v 1.13 2007/12/11 01:17:16 raeburn Exp $
+# $Id: lonuserutils.pm,v 1.20 2007/12/21 17:27:57 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -46,8 +46,7 @@ sub modifystudent {
     # if $csec is undefined, drop the student from all the courses matching
     # this one.  If $csec is defined, drop them from all other sections of
     # this course and add them to section $csec
-    my $cdom = $env{'course.'.$courseid.'.domain'};
-    my $cnum = $env{'course.'.$courseid.'.num'};
+    my ($cnum,$cdom) = &get_course_identity($courseid);
     my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
     my ($tmp) = keys(%roles);
     # Bail out if we were unable to get the students roles
@@ -236,8 +235,8 @@ sub domain_roles_select {
         } elsif ($roletype eq 'author') {
             @roles = &construction_space_roles();
         } else {
-            @roles = &course_roles('domain');
-            unshift(@roles,'cr');
+            my $custom = 1;
+            @roles = &course_roles('domain',undef,$custom);
         }
         my $order = ['Any',@roles];
         $select_menus{$roletype}->{'order'} = $order; 
@@ -962,11 +961,10 @@ sub make_dates_default {
     my ($startdate,$enddate,$context) = @_;
     my $result = '';
     if ($context eq 'course') {
-        my $dom = $env{'course.'.$env{'request.course.id'}.'.domain'};
-        my $crs = $env{'course.'.$env{'request.course.id'}.'.num'};
+        my ($cnum,$cdom) = &get_course_identity();
         my $put_result = &Apache::lonnet::put('environment',
                 {'default_enrollment_start_date'=>$startdate,
-                 'default_enrollment_end_date'  =>$enddate},$dom,$crs);
+                 'default_enrollment_end_date'  =>$enddate},$cdom,$cnum);
         if ($put_result eq 'ok') {
             $result .= &mt('Set default start and end dates for course').
                        '<br />'."\n";
@@ -1048,7 +1046,8 @@ sub default_role_selector {
 sub default_course_roles {
     my ($context,$checkpriv,%customroles) = @_;
     my $output;
-    my @roles = &course_roles($context,$checkpriv);
+    my $custom = 1;
+    my @roles = &course_roles($context,$checkpriv,$custom);
     foreach my $role (@roles) {
         my $plrole=&Apache::lonnet::plaintext($role);
         $output .= '  <option value="'.$role.'">'.$plrole.'</option>';
@@ -1066,7 +1065,7 @@ sub default_course_roles {
 
 sub construction_space_roles {
     my ($checkpriv) = @_;
-    my @allroles = ('ca','aa');
+    my @allroles = &roles_by_context('author');
     my @roles;
     if ($checkpriv) {
         foreach my $role (@allroles) {
@@ -1082,7 +1081,7 @@ sub construction_space_roles {
 
 sub domain_roles {
     my ($checkpriv) = @_;
-    my @allroles = ('dc','li','dg','au','sc');
+    my @allroles = &roles_by_context('domain');
     my @roles;
     if ($checkpriv) {
         foreach my $role (@allroles) {
@@ -1097,8 +1096,8 @@ sub domain_roles {
 }
 
 sub course_roles {
-    my ($context,$checkpriv) = @_;
-    my @allroles = ('st','ta','ep','in','cc');
+    my ($context,$checkpriv,$custom) = @_;
+    my @allroles = &roles_by_context('course',$custom);
     my @roles;
     if ($context eq 'domain') {
         @roles = @allroles;
@@ -1128,17 +1127,18 @@ sub course_roles {
 
 sub curr_role_permissions {
     my ($context,$setting,$checkpriv) = @_; 
+    my $custom = 1;
     my @roles;
     if ($context eq 'author') {
         @roles = &construction_space_roles($checkpriv);
     } elsif ($context eq 'domain') {
         if ($setting eq 'course') {
-            @roles = &course_roles($context,$checkpriv); 
+            @roles = &course_roles($context,$checkpriv,$custom); 
         } else {
             @roles = &domain_roles($checkpriv);
         }
     } elsif ($context eq 'course') {
-        @roles = &course_roles($context,$checkpriv);
+        @roles = &course_roles($context,$checkpriv,$custom);
     }
     return @roles;
 }
@@ -1287,8 +1287,7 @@ sub print_userlist {
             }
         }
         my $cid =$env{'request.course.id'};
-        my $cdom=$env{'course.'.$cid.'.domain'};
-        my $cnum=$env{'course.'.$cid.'.num'};
+        my ($cnum,$cdom) = &get_course_identity($cid);
         my $showroles;
         if ($env{'form.showrole'} ne 'Any') {
             $showroles = [$env{'form.showrole'}];
@@ -1303,7 +1302,7 @@ sub print_userlist {
         my (%cstr_roles,%dom_roles);
         if ($context eq 'author') {
             # List co-authors and assistant co-authors
-            my @possroles = ('ca','aa');
+            my @possroles = &roles_by_context($context);
             %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                                               \@statuses,\@possroles);
             &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
@@ -1328,7 +1327,7 @@ sub print_userlist {
                         } else {
                             my @possroles;
                             if ($env{'form.showrole'} eq 'Any') {
-                                @possroles = ('ca','aa');
+                                my @possroles = &roles_by_context($context);
                             } else {
                                 @possroles = ($env{'form.showrole'}); 
                             }
@@ -1351,14 +1350,13 @@ sub print_userlist {
                     foreach my $cid (keys(%courses)) {
                         my %coursehash =
                             &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
-                        my $cdom = $coursehash{'domain'};
-                        my $cnum = $coursehash{'num'};
+                        my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
                         next if ($cnum eq '' || $cdom eq '');
-                        my $cdesc = $coursehash{'description'};
+                        my $custom = 1;
                         my (@roles,@sections,%access,%users,%userdata,
                             %statushash);
                         if ($env{'form.showrole'} eq 'Any') {
-                            @roles = &course_roles($context);
+                            @roles = &course_roles($context,undef,$custom);
                             unshift(@roles,'cr');
                         } else {
                             @roles = ($env{'form.showrole'});
@@ -1760,20 +1758,22 @@ sub show_users_list {
     my ($cid,$cdom,$cnum,$classgroups,$displayphotos,$displayclickers);
     if ($context eq 'course') {
         $cid=$env{'request.course.id'};
-        $cdom = $env{'course.'.$cid.'.domain'};
-        $cnum = $env{'course.'.$cid.'.num'};
+        ($cnum,$cdom) = &get_course_identity($cid);
         ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                                      $userlist,$keylist,$cdom,$cnum);
-        if (! exists($env{'form.displayphotos'})) {
-            $env{'form.displayphotos'} = 'off';
-        }
-        $displayphotos = $env{'form.displayphotos'};
-        if (! exists($env{'form.displayclickers'})) {
-            $env{'form.displayclickers'} = 'off';
-        }
-        $displayclickers = $env{'form.displayclickers'};
-        if ($env{'course.'.$cid.'.internal.showphoto'}) {
-            $r->print('
+        if ($mode eq 'autoenroll') {
+            $env{'form.showrole'} = 'st';
+        } else {
+            if (! exists($env{'form.displayphotos'})) {
+                $env{'form.displayphotos'} = 'off';
+            }
+            $displayphotos = $env{'form.displayphotos'};
+            if (! exists($env{'form.displayclickers'})) {
+                $env{'form.displayclickers'} = 'off';
+            }
+            $displayclickers = $env{'form.displayclickers'};
+            if ($env{'course.'.$cid.'.internal.showphoto'}) {
+                $r->print('
 <script type="text/javascript">
 function photowindow(photolink) {
     var title = "Photo_Viewer";
@@ -1783,14 +1783,15 @@ function photowindow(photolink) {
     stdeditbrowser.focus();
 }
 </script>
-           ');
-        }
-        $r->print(<<END);
+               ');
+            }
+            $r->print(<<END);
 <input type="hidden" name="displayphotos" value="$displayphotos" />
 <input type="hidden" name="displayclickers" value="$displayclickers" />
 END
+        }
     }
-    unless ($mode eq 'autoenroll') {
+    if ($mode ne 'autoenroll') {
         my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
         my $alert = &mt("You must select at least one user by checking a user's 'Select' checkbox");
         my $singconfirm = &mt(' for a single user');
@@ -1919,49 +1920,52 @@ END
     } 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 $results_description;
+    if ($mode ne 'autoenroll') {
+        $results_description = &results_header_row($rolefilter,$statusmode,
+                                                   $context,$permission);
+        $r->print('<b>'.$results_description.'</b><br />');
+    }
     my ($output,$actionselect);
-    if ($mode eq 'html' || $mode eq 'view') {
-        if ($permission->{'cusr'}) {
-            $actionselect = &select_actions($context,$setting,$statusmode);
-        }
-        $r->print(<<END);
+    if ($mode eq 'html' || $mode eq 'view' || $mode eq 'autoenroll') {
+        if ($mode ne 'autoenroll') {
+            if ($permission->{'cusr'}) {
+                $actionselect = &select_actions($context,$setting,$statusmode);
+            }
+            $r->print(<<END);
 <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
-         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 />';
-             }
-             if ($actionselect) {
-                 $output .= <<"END"; 
+            $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 />';
+            }
+            if ($actionselect) {
+                $output .= <<"END"; 
 $lt{'ac'}:&nbsp;$actionselect <input type="button" value="$lt{'pr'}" onclick="javascript:verify_action(document.studentform.actionlist)" /></p>
 <p><input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.actionlist)" /> &nbsp;
 <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.actionlist)" />
 END
-             }
+            }
         }
         $output .= "\n<p>\n".
                   &Apache::loncommon::start_data_table().
@@ -1980,7 +1984,7 @@ END
             $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') {
+        if ($context eq 'course' && $mode ne 'autoenroll') {
             if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
                 # Clicker display on or off?
                 my %clicker_options = &Apache::lonlocal::texthash(
@@ -2014,8 +2018,8 @@ END
                       '    </th>'."\n";
                 }
             }
-            $output .= &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') {
         #
@@ -2151,32 +2155,54 @@ END
         }
         if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') {
             $r->print(&Apache::loncommon::start_data_table_row());
-            $r->print("<td>$rowcount</td>\n");
             my $checkval;
-            if ($mode ne 'autoenroll' && $actionselect) {
-                $checkval = $user; 
-                if ($context eq 'course') {
-                    if ($role eq 'st') {
-                        $checkval .= ':st';
-                    }
-                    $checkval .= ':'.$in{'section'};
-                    if ($role eq 'st') {
-                        $checkval .= ':'.$in{'type'}.':'.$in{'lockedtype'};
+            if ($mode eq 'autoenroll') {
+                my $cellentry;
+                if ($in{'type'} eq 'auto') {
+                    $cellentry = '<b>'.&mt('auto').'</b>&nbsp;<label><input type="checkbox" name="chgauto" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;Change</label>';
+                    $autocount ++;
+                } else {
+                    $cellentry = '<table border="0" cellspacing="0"><tr><td rowspan="2"><b>'.&mt('manual').'</b></td><td><nobr><label><input type="checkbox" name="chgmanual" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;Change</label></nobr></td></tr><tr><td><nobr>';
+                    $manualcount ++;
+                    if ($in{'lockedtype'}) {
+                        $cellentry .= '<label><input type="checkbox" name="unlockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;'.&mt('Unlock').'</label>';
+                        $unlockcount ++;
+                    } else {
+                        $cellentry .= '<label><input type="checkbox" name="lockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;'.&mt('Lock').'</label>';
+                        $lockcount ++;
+                    }
+                    $cellentry .= '</nobr></td></tr></table>';
+                }
+                $r->print("<td>$cellentry</td>\n");
+            } else {
+                $r->print("<td>$rowcount</td>\n");
+                $checkval;
+                if ($actionselect) {
+                    $checkval = $user; 
+                    if ($context eq 'course') {
+                        if ($role eq 'st') {
+                            $checkval .= ':st';
+                        }
+                        $checkval .= ':'.$in{'section'};
+                        if ($role eq 'st') {
+                            $checkval .= ':'.$in{'type'}.':'.$in{'lockedtype'};
+                        }
                     }
+                    $r->print('<td><input type="checkbox" name="actionlist" value="'.
+                              $checkval.'"></td>');
                 }
-                $r->print('<td><input type="checkbox" name="actionlist" value="'.
-                          $checkval.'"></td>');
             }
             foreach my $item (@cols) {
                 if ($item eq 'username') {
-                    $r->print('<td>'.&print_username_link($permission,\%in).'</td>');
-                } elsif (($item eq 'start' || $item eq 'end') && ($mode ne 'autoeroll') && ($actionselect)) {
+                    $r->print('<td>'.&print_username_link($mode,$permission,
+                                                          \%in).'</td>');
+                } elsif (($item eq 'start' || $item eq 'end') && ($actionselect)) {
                     $r->print('<td>'.$in{$item}.'<input type="hidden" name="'.$checkval.'_'.$item.'" value="'.$sdata->[$index{$item}].'" /></td>'."\n");
                 } else {
                     $r->print('<td>'.$in{$item}.'</td>'."\n");
                 }
             }
-            if ($context eq 'course') {
+            if (($context eq 'course') && ($mode ne 'autoenroll')) {
                 if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
                     if ($displayclickers eq 'on') {
                         my $clickers =
@@ -2256,9 +2282,11 @@ END
 }
 
 sub print_username_link {
-    my ($permission,$in) = @_;
+    my ($mode,$permission,$in) = @_;
     my $output;
-    if (!$permission->{'cusr'}) {
+    if ($mode eq 'autoenroll') {
+        $output = $in->{'username'};
+    } elsif (!$permission->{'cusr'}) {
         $output = &Apache::loncommon::aboutmewrapper($in->{'username'},
                                                      $in->{'username'},
                                                      $in->{'domain'});
@@ -2480,8 +2508,7 @@ END
                '<form name="'.$formname.'" method="post">'."\n".
                 $date_items;
     if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') {
-        my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
-        my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+        my ($cnum,$cdom) = &get_course_identity();
         my %sections_count =
             &Apache::loncommon::get_sections($cdom,$cnum);
         my $info;
@@ -2529,8 +2556,38 @@ END
     return $output;
 }
 
+sub section_picker {
+    my ($cdom,$cnum,$role,$rowtitle,$permission,$context,$mode) = @_;
+    my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
+    my $sections_select .= &course_sections(\%sections_count,$role);
+    my $secbox = '<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n";
+    if ($mode eq 'upload') {
+        my ($options,$cb_script,$coursepick) =
+            &default_role_selector($context,1);
+        $secbox .= &Apache::lonhtmlcommon::row_title('role','LC_oddrow_value').
+                   $options. &Apache::lonhtmlcommon::row_closure(1)."\n";
+    }
+    $secbox .= &Apache::lonhtmlcommon::row_title($rowtitle,'LC_oddrow_value')."\n";
+    if ($env{'request.course.sec'} eq '') {
+        $secbox .= '<table class="LC_createuser"><tr class="LC_section_row">'."\n".
+                   '<td align="center">'.&mt('Existing sections')."\n".
+                   '<br />'.$sections_select.'</td><td align="center">'.
+                   &mt('New section').'<br />'."\n".
+                   '<input type="text" name="newsec" size="15" />'."\n".
+                   '<input type="hidden" name="sections" value="" />'."\n".
+                   '</td></tr></table>'."\n";
+    } else {
+       $secbox .= '<input type="hidden" name="sections" value="'.
+                   $env{'request.course.sec'}.'" />'.
+                   $env{'request.course.sec'};
+    }
+    $secbox .= &Apache::lonhtmlcommon::row_closure(1)."\n".
+               &Apache::lonhtmlcommon::end_pick_box().'</p>';
+    return $secbox;
+}
+
 sub results_header_row {
-    my ($rolefilter,$statusmode,$context) = @_;
+    my ($rolefilter,$statusmode,$context,$permission) = @_;
     my ($description,$showfilter);
     if ($rolefilter ne 'Any') {
         $showfilter = $rolefilter;
@@ -2550,8 +2607,17 @@ sub results_header_row {
                 $description .= &mt('All users in course with [_1] roles',$rolefilter);
             }
         }
+        if (exists($permission->{'view_section'})) {
+            if ($env{'form.showrole'} eq 'st') {
+                $description .= ' '.&mt('(section [_1] only)',$permission->{'view_section'});
+            } elsif ($env{'form.showrole'} eq 'any') {
+                $description .= ' '.&mt('(section [_1] only)',$permission->{'view_section'});
+            }
+        }
     } elsif ($context eq 'author') {
-        $description = &mt('Author space for [_1].').' ';
+        $description = 
+            &mt('Author space for <span class="LC_cusr_emph">[_1]</span>',
+        &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'})).':&nbsp;&nbsp;';
         if ($statusmode eq 'Expired') {
             $description .= &mt('Co-authors with expired [_1] roles',$showfilter);
         } elsif ($statusmode eq 'Future') {
@@ -2631,8 +2697,9 @@ sub results_header_row {
 #################################################
 #################################################
 sub show_drop_list {
-    my ($r,$classlist,$keylist,$nosort)=@_;
+    my ($r,$classlist,$keylist,$nosort,$permission)=@_;
     my $cid=$env{'request.course.id'};
+    my ($cnum,$cdom) = &get_course_identity($cid);
     if (! exists($env{'form.sortby'})) {
         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                                                 ['sortby']);
@@ -2641,26 +2708,17 @@ sub show_drop_list {
     if ($sortby !~ /^(username|domain|section|groups|fullname|id|start|end)$/) {
         $sortby = 'username';
     }
-    my $cdom = $env{'course.'.$cid.'.domain'};
-    my $cnum = $env{'course.'.$cid,'.num'};
     my ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                                               $classlist,$keylist,$cdom,$cnum);
     #
     my $action = "drop";
+    my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
     $r->print(<<END);
 <input type="hidden" name="sortby" value="$sortby" />
 <input type="hidden" name="action" value="$action" />
 <input type="hidden" name="state"  value="done" />
-<script>
-function checkAll(field) {
-    for (i = 0; i < field.length; i++)
-        field[i].checked = true ;
-}
-
-function uncheckAll(field) {
-    for (i = 0; i < field.length; i++)
-        field[i].checked = false ;
-}
+<script type="text/javascript" language="Javascript">
+$check_uncheck_js
 </script>
 <p>
 <input type="hidden" name="phase" value="four">
@@ -2675,9 +2733,9 @@ my %lt=&Apache::lonlocal::texthash('usrn
                                    'groups' => "active groups",
                                    );
     if ($nosort) {
-        $r->print(&Apache::loncommon::start_data_table());
+        $r->print(&Apache::loncommon::start_data_table().
+                  &Apache::loncommon::start_data_table_header_row());
         $r->print(<<END);
-<tr>
     <th>&nbsp;</th>
     <th>$lt{'usrn'}</th>
     <th>$lt{'dom'}</th>
@@ -2687,32 +2745,32 @@ my %lt=&Apache::lonlocal::texthash('usrn
     <th>$lt{'start'}</th>
     <th>$lt{'end'}</th>
     <th>$lt{'groups'}</th>
-</tr>
 END
-
+        $r->print(&Apache::loncommon::end_data_table_header_row());
     } else  {
-        $r->print(&Apache::loncommon::start_data_table());
+        $r->print(&Apache::loncommon::start_data_table().
+                  &Apache::loncommon::start_data_table_header_row());
         $r->print(<<END);
-<tr><th>&nbsp;</th>
+    <th>&nbsp;</th>
     <th>
-       <a href="/adm/dropadd?action=$action&sortby=username">$lt{'usrn'}</a>
+       <a href="/adm/createuser?action=$action&sortby=username">$lt{'usrn'}</a>
     </th><th>
-       <a href="/adm/dropadd?action=$action&sortby=domain">$lt{'dom'}</a>
+       <a href="/adm/createuser?action=$action&sortby=domain">$lt{'dom'}</a>
     </th><th>
-       <a href="/adm/dropadd?action=$action&sortby=id">ID</a>
+       <a href="/adm/createuser?action=$action&sortby=id">ID</a>
     </th><th>
-       <a href="/adm/dropadd?action=$action&sortby=fullname">$lt{'sn'}</a>
+       <a href="/adm/createuser?action=$action&sortby=fullname">$lt{'sn'}</a>
     </th><th>
-       <a href="/adm/dropadd?action=$action&sortby=section">$lt{'sec'}</a>
+       <a href="/adm/createuser?action=$action&sortby=section">$lt{'sec'}</a>
     </th><th>
-       <a href="/adm/dropadd?action=$action&sortby=start">$lt{'start'}</a>
+       <a href="/adm/createuser?action=$action&sortby=start">$lt{'start'}</a>
     </th><th>
-       <a href="/adm/dropadd?action=$action&sortby=end">$lt{'end'}</a>
+       <a href="/adm/createuser?action=$action&sortby=end">$lt{'end'}</a>
     </th><th>
-       <a href="/adm/dropadd?action=$action&sortby=groups">$lt{'groups'}</a>
+       <a href="/adm/createuser?action=$action&sortby=groups">$lt{'groups'}</a>
     </th>
-</tr>
 END
+        $r->print(&Apache::loncommon::end_data_table_header_row());
     }
     #
     # Sort the students
@@ -2759,6 +2817,13 @@ END
         }
         my $status   = $sdata->[$index{'status'}];
         next if ($status ne 'Active');
+        if ($env{'request.course.sec'} ne '') {
+            if ($section ne $env{'request.course.sec'}) {
+                next;
+            }
+        }
+        my $studentkey = $student.':'.$section;
+        my $startitem = '<input type="hidden" name="'.$studentkey.'_start" value="'.$sdata->[$index{'start'}].'" />';
         #
         $r->print(&Apache::loncommon::start_data_table_row());
         $r->print(<<"END");
@@ -3269,9 +3334,10 @@ sub user_change_result {
 }
 
 # ========================================================= Menu Phase Two Drop
-sub print_expire_menu {
-    my ($r,$context) = @_;
-    $r->print("<h3>".&mt("Expire Users' Roles")."</h3>");
+sub print_drop_menu {
+    my ($r,$context,$permission) = @_;
+    $r->print('<h3>'.&mt("Drop Students").'</h3>'."\n".
+              '<form name="studentform" method="post">'."\n");
     my $cid=$env{'request.course.id'};
     my ($classlist,$keylist) = &Apache::loncoursedata::get_classlist();
     if (! defined($classlist)) {
@@ -3279,11 +3345,11 @@ sub print_expire_menu {
         return;
     }
     # Print out the available choices
-    &show_drop_list($r,$classlist,$keylist);
+    &show_drop_list($r,$classlist,$keylist,$permission);
+    $r->print('</form>'. &Apache::loncommon::end_page());
     return;
 }
 
-
 # ================================================================== Phase four
 
 sub update_user_list {
@@ -3299,12 +3365,18 @@ sub update_user_list {
     my %result_text = ( ok    => { 'revoke'   => 'Revoked',
                                    'delete'   => 'Deleted',
                                    'reenable' => 'Re-enabled',
-                                   'activate' => 'Activated', 
+                                   'activate' => 'Activated',
+                                   'chgdates' => 'Changed Access Dates for',
+                                   'chgsec'   => 'Changed section for',
+                                   'drop'     => 'Dropped',
                                  },
                         error => {'revoke'    => 'revoking',
                                   'delete'    => 'deleting',
                                   'reenable'  => 're-enabling',
                                   'activate'  => 'activating',
+                                  'chgdates'  => 'changing access dates for',
+                                  'chgsec'    => 'changing section for',
+                                  'drop'      => 'dropping',
                                  },
                       );
     my ($startdate,$enddate);
@@ -3314,7 +3386,18 @@ sub update_user_list {
     foreach my $item (@changelist) {
         my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,@sections,
             $scopestem);
-        if ($context eq 'course') {
+        if ($choice eq 'drop') {
+            ($uname,$udom,$sec) = split(/:/,$item,-1);
+            $role = 'st';
+            $cid = $env{'request.course.id'};
+            $scopestem = '/'.$cid;
+            $scopestem =~s/\_/\//g;
+            if ($sec eq '') {
+                $scope = $scopestem;
+            } else {
+                $scope = $scopestem.'/'.$sec;
+            }
+        } elsif ($context eq 'course') {
             ($uname,$udom,$role,$sec,$type,$locktype) = split(/\:/,$item,-1);
             $cid = $env{'request.course.id'};
             $scopestem = '/'.$cid;
@@ -3347,8 +3430,14 @@ sub update_user_list {
         my ($uid,$first,$middle,$last,$gene,$sec);
         my $start = $env{'form.'.$item.'_start'};
         my $end = $env{'form.'.$item.'_end'};
-        # revoke or delete user role
-        if ($choice eq 'revoke') {
+        if ($choice eq 'drop') {
+            # drop students
+            $end = $now;
+            $type = 'manual';
+            $result =
+                &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
+        } elsif ($choice eq 'revoke') {
+            # revoke or delete user role
             $end = $now; 
             if ($role eq 'st') {
                 $result = 
@@ -3450,19 +3539,26 @@ sub update_user_list {
                 }
             }
         }
+        my $extent = $scope;
+        if ($choice eq 'drop' || $context eq 'course') {
+            my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
+            if ($cdesc) {
+                $extent = $cdesc;
+            }
+        }
         if ($result eq 'ok' || $result eq 'ok:') {
             $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for [_3]",
-                          $plrole,$scope,$uname.':'.$udom).'<br />');
+                          $plrole,$extent,$uname.':'.$udom).'<br />');
             $count++;
         } else {
             $r->print(
                 &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for [_3]:[_4]",
-                    $plrole,$scope,$uname.':'.$udom,$result).'<br />');
+                    $plrole,$extent,$uname.':'.$udom,$result).'<br />');
         }
     }
     $r->print('<p><b>'.&mt("$result_text{'ok'}{$choice} role(s) for [quant,_1,user,users,users].",$count).'</b></p>');
     if ($count > 0) {
-        if ($choice eq 'revoke') {
+        if ($choice eq 'revoke' || $choice eq 'drop') {
             $r->print('<p>'.&mt('Re-enabling will re-activate data for the role.</p>'));
         }
         # Flush the course logs so reverse user roles immediately updated
@@ -3691,5 +3787,352 @@ ENDSECCODE
     return $setsection_js; 
 }
 
+sub can_create_user {
+    my ($dom,$context,$usertype) = @_;
+    my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
+    my $cancreate = 1;
+    if (ref($domconf{'usercreation'}) eq 'HASH') {
+        if (ref($domconf{'usercreation'}{'cancreate'}) eq 'HASH') {
+            if ($context eq 'course' || $context eq 'author') {
+                my $creation = $domconf{'usercreation'}{'cancreate'}{$context};
+                if ($creation eq 'none') {
+                    $cancreate = 0;
+                } elsif ($creation ne 'any') {
+                    if (defined($usertype)) {
+                        if ($creation ne $usertype) {
+                            $cancreate = 0;
+                        }
+                    }
+                }
+            }
+        }
+    }
+    return $cancreate;
+}
+
+sub can_modify_userinfo {
+    my ($context,$dom,$fields,$userroles) = @_;
+    my %domconfig =
+       &Apache::lonnet::get_dom('configuration',['usermodification'],
+                                $dom);
+    my %canmodify;
+    if (ref($fields) eq 'ARRAY') {
+        foreach my $field (@{$fields}) {
+            $canmodify{$field}  = 0;
+            if (&Apache::lonnet::allowed('mau',$dom)) {
+                $canmodify{$field} = 1;
+            } else {
+                if (ref($domconfig{'usermodification'}) eq 'HASH') {
+                    if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') {
+                        if (ref($userroles) eq 'ARRAY') {
+                            foreach my $role (@{$userroles}) {
+                                my $testrole;
+                                if ($role =~ /^cr\//) {
+                                    $testrole = 'cr';
+                                } else {
+                                    $testrole = $role;
+                                }
+                                if (ref($domconfig{'usermodification'}{$context}{$testrole}) eq 'HASH') {
+                                    if ($domconfig{'usermodification'}{$context}{$testrole}{$field}) {
+                                        $canmodify{$field} = 1;
+                                        last;
+                                    }
+                                }
+                            }
+                        } else {
+                            foreach my $key (keys(%{$domconfig{'usermodification'}{$context}})) {
+                                if (ref($domconfig{'usermodification'}{$context}{$key}) eq 'HASH') {
+                                    if ($domconfig{'usermodification'}{$context}{$key}{$field}) {
+                                        $canmodify{$field} = 1;
+                                        last;
+                                    }
+                                }
+                            }
+                        }
+                    }
+                } elsif ($context eq 'course') {
+                    if (ref($userroles) eq 'ARRAY') {
+                        if (grep(/^st$/,@{$userroles})) {
+                            $canmodify{$field} = 1;
+                        }
+                    } else {
+                        $canmodify{$field} = 1;
+                    }
+                }
+            }
+        }
+    }
+    return %canmodify;
+}
+
+sub check_usertype {
+    my ($dom,$uname,$rules) = @_;
+    my $usertype;
+    if (ref($rules) eq 'HASH') {
+        my @user_rules = keys(%{$rules});
+        if (@user_rules > 0) {
+            my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules);
+            if (keys(%rule_check) > 0) {
+                $usertype = 'unofficial';
+                foreach my $item (keys(%rule_check)) {
+                    if ($rule_check{$item}) {
+                        $usertype = 'official';
+                        last;
+                    }
+                }
+            }
+        }
+    }
+    return $usertype;
+}
+
+sub roles_by_context {
+    my ($context,$custom) = @_;
+    my @allroles;
+    if ($context eq 'course') {
+        @allroles = ('st','ad','ta','ep','in','cc');
+        if ($custom) {
+            push(@allroles,'cr');
+        }
+    } elsif ($context eq 'author') {
+        @allroles = ('ca','aa');
+    } elsif ($context eq 'domain') {
+        @allroles = ('li','dg','sc','au','dc');
+    }
+    return @allroles;
+}
+
+sub get_permission {
+    my ($context,$roles) = @_;
+    my %permission;
+    if ($context eq 'course') {
+        my $custom = 1;
+        my @allroles = &roles_by_context($context,$custom);
+        foreach my $role (@allroles) {
+            if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
+                $permission{'cusr'} = 1;
+                last;
+            }
+        }
+        if (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) {
+            $permission{'custom'} = 1;
+        }
+        if (&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) {
+            $permission{'view'} = 1;
+        }
+        if (!$permission{'view'}) {
+            my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
+            $permission{'view'} =  &Apache::lonnet::allowed('vcl',$scope);
+            if ($permission{'view'}) {
+                $permission{'view_section'} = $env{'request.course.sec'};
+            }
+        }
+        if (!$permission{'cusr'}) {
+            if ($env{'request.course.sec'} ne '') {
+                my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
+                $permission{'cusr'} = (&Apache::lonnet::allowed('cst',$scope));
+                if ($permission{'cusr'}) {
+                    $permission{'cusr_section'} = $env{'request.course.sec'};
+                }
+            }
+        }
+        if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
+            $permission{'grp_manage'} = 1;
+        }
+    } elsif ($context eq 'author') {
+        $permission{'cusr'} = &authorpriv($env{'user.name'},$env{'request.role.domain'});
+        $permission{'view'} = $permission{'cusr'};
+    } else {
+        my @allroles = &roles_by_context($context);
+        foreach my $role (@allroles) {
+            if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {                $permission{'cusr'} = 1;
+                last;
+            }
+        }
+        if (!$permission{'cusr'}) {
+            if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
+                $permission{'cusr'} = 1;
+            }
+        }
+        if (&Apache::lonnet::allowed('ccr',$env{'request.role.domain'})) {
+            $permission{'custom'} = 1;
+        }
+        $permission{'view'} = $permission{'cusr'};
+    }
+    my $allowed = 0;
+    foreach my $perm (values(%permission)) {
+        if ($perm) { $allowed=1; last; }
+    }
+    return (\%permission,$allowed);
+}
+
+# ==================================================== Figure out author access
+
+sub authorpriv {
+    my ($auname,$audom)=@_;
+    unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
+         || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; }    return 1;
+}
+
+sub get_course_identity {
+    my ($cid) = @_;
+    my ($cnum,$cdom,$cdesc);
+    if ($cid eq '') {
+        $cid = $env{'request.course.id'}
+    }
+    if ($cid ne '') {
+        $cnum = $env{'course.'.$cid.'.num'};
+        $cdom = $env{'course.'.$cid.'.domain'};
+        $cdesc = $env{'course.'.$cid.'.description'};
+        if ($cnum eq '' || $cdom eq '') {
+            my %coursehash =
+                &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
+            $cdom = $coursehash{'domain'};
+            $cnum = $coursehash{'num'};
+            $cdesc = $coursehash{'description'};
+        }
+    }
+    return ($cnum,$cdom,$cdesc);
+}
+
+sub dc_setcourse_js {
+    my ($formname,$mode) = @_;
+    my $dc_setcourse_code;
+    my $cctext = &Apache::lonnet::plaintext('cc');
+    my %alerts = &sectioncheck_alerts();
+    my $role = 'role';
+    if ($mode eq 'upload') {
+        $role = 'courserole';
+    }
+    $dc_setcourse_code = (<<"SCRIPTTOP");
+function setCourse() {
+    var course = document.$formname.dccourse.value;
+    if (course != "") {
+        if (document.$formname.dcdomain.value != document.$formname.origdom.value) {
+            alert("$alerts{'curd'}");
+            return;
+        }
+        var userrole = document.$formname.$role.options[document.$formname.$role.selectedIndex].value
+        var section="";
+        var numsections = 0;
+        var newsecs = new Array();
+        for (var i=0; i<document.$formname.currsec.length; i++) {
+            if (document.$formname.currsec.options[i].selected == true ) {
+                if (document.$formname.currsec.options[i].value != "" && document.$formname.currsec.options[i].value != null) {
+                    if (numsections == 0) {
+                        section = document.$formname.currsec.options[i].value
+                        numsections = 1;
+                    }
+                    else {
+                        section = section + "," +  document.$formname.currsec.options[i].value
+                        numsections ++;
+                    }
+                }
+            }
+        }
+        if (document.$formname.newsec.value != "" && document.$formname.newsec.value != null) {
+            if (numsections == 0) {
+                section = document.$formname.newsec.value
+            }
+            else {
+                section = section + "," +  document.$formname.newsec.value
+            }
+            newsecs = document.$formname.newsec.value.split(/,/g);
+            numsections = numsections + newsecs.length;
+        }
+        if ((userrole == 'st') && (numsections > 1)) {
+            alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
+            return;
+        }
+        for (var j=0; j<newsecs.length; j++) {
+            if ((newsecs[j] == 'all') || (newsecs[j] == 'none')) {
+                alert("'"+newsecs[j]+"' $alerts{'mayn'}.\\n$alerts{'plsc'}.");
+                return;
+            }
+            if (document.$formname.groups.value != '') {
+                var groups = document.$formname.groups.value.split(/,/g);
+                for (var k=0; k<groups.length; k++) {
+                    if (newsecs[j] == groups[k]) {
+                        alert("'"+newsecs[j]+"' $alerts{'mayt'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
+                        return;
+                    }
+                }
+            }
+        }
+        if ((userrole == 'cc') && (numsections > 0)) {
+            alert("$alerts{'secd'} $cctext $alerts{'role'}.\\n$alerts{'accr'}.");
+            section = "";
+        }
+SCRIPTTOP
+    if ($mode ne 'upload') {
+        $dc_setcourse_code .= (<<"ENDSCRIPT");
+        var coursename = "_$env{'request.role.domain'}"+"_"+course+"_"+userrole
+        var numcourse = getIndex(document.$formname.dccourse);
+        if (numcourse == "-1") {
+            alert("$alerts{'thwa'}");
+            return;
+        }
+        else {
+            document.$formname.elements[numcourse].name = "act"+coursename;
+            var numnewsec = getIndex(document.$formname.newsec);
+            if (numnewsec != "-1") {
+                document.$formname.elements[numnewsec].name = "sec"+coursename;
+                document.$formname.elements[numnewsec].value = section;
+            }
+            var numstart = getIndex(document.$formname.start);
+            if (numstart != "-1") {
+                document.$formname.elements[numstart].name = "start"+coursename;
+            }
+            var numend = getIndex(document.$formname.end);
+            if (numend != "-1") {
+                document.$formname.elements[numend].name = "end"+coursename
+            }
+        }
+    }
+    document.$formname.submit();
+}
+
+ENDSCRIPT
+    } else {
+        $dc_setcourse_code .=  "
+        document.$formname.sections.value = section;
+    }
+    return 'ok';
+}
+";
+    }
+    $dc_setcourse_code .= (<<"ENDSCRIPT");
+
+    function getIndex(caller) {
+        for (var i=0;i<document.$formname.elements.length;i++) {
+            if (document.$formname.elements[i] == caller) {
+                return i;
+            }
+        }
+        return -1;
+    }
+ENDSCRIPT
+}
+
+sub sectioncheck_alerts {
+    my %alerts = &Apache::lonlocal::texthash(
+                    curd => 'You must select a course in the current domain',
+                    inea => 'In each course, each user may only have one student role at a time',
+                    youh => 'You had selected',
+                    sect => 'sections',
+                    plsm => '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',
+                    plsc => 'Please choose a different section name',
+                    mayt => 'may not be used as the name for a section, as it is the name of a course group',
+                    secn => 'Section names and group names must be distinct',
+                    secd => 'Section designations do not apply to ',
+                    role => 'roles',
+                    accr => 'role will be added with access to all sections',
+                    thwa => 'There was a problem with your course selection'
+                 );
+    return %alerts;
+}
+
+
 1;