--- loncom/interface/lonuserutils.pm	2007/12/14 00:20:53	1.16
+++ loncom/interface/lonuserutils.pm	2007/12/21 15:22:01	1.19
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Utility functions for managing LON-CAPA user accounts
 #
-# $Id: lonuserutils.pm,v 1.16 2007/12/14 00:20:53 raeburn Exp $
+# $Id: lonuserutils.pm,v 1.19 2007/12/21 15:22:01 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,8 +1758,7 @@ 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 ($mode eq 'autoenroll') {
@@ -2511,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;
@@ -2560,6 +2556,36 @@ 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,$permission) = @_;
     my ($description,$showfilter);
@@ -2671,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']);
@@ -2681,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">
@@ -2715,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>
@@ -2727,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
@@ -2799,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");
@@ -3309,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)) {
@@ -3319,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 {
@@ -3339,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);
@@ -3354,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;
@@ -3387,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 = 
@@ -3490,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
@@ -3754,19 +3810,54 @@ sub can_create_user {
     return $cancreate;
 }
 
+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) = @_;
+    my ($context,$roles) = @_;
     my %permission;
     if ($context eq 'course') {
-        if ((&Apache::lonnet::allowed('cta',$env{'request.course.id'})) ||
-            (&Apache::lonnet::allowed('cin',$env{'request.course.id'})) ||
-            (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) ||
-            (&Apache::lonnet::allowed('cep',$env{'request.course.id'})) ||
-            (&Apache::lonnet::allowed('cst',$env{'request.course.id'}))) {
-            $permission{'cusr'} = 1;
-            $permission{'view'} =
-                 &Apache::lonnet::allowed('vcl',$env{'request.course.id'});
-
+        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;
@@ -3781,6 +3872,15 @@ sub get_permission {
                 $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;
         }
@@ -3788,13 +3888,16 @@ sub get_permission {
         $permission{'cusr'} = &authorpriv($env{'user.name'},$env{'request.role.domain'});
         $permission{'view'} = $permission{'cusr'};
     } else {
-        if ((&Apache::lonnet::allowed('cad',$env{'request.role.domain'})) ||
-            (&Apache::lonnet::allowed('cli',$env{'request.role.domain'})) ||
-            (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) ||
-            (&Apache::lonnet::allowed('csc',$env{'request.role.domain'})) ||
-            (&Apache::lonnet::allowed('cdg',$env{'request.role.domain'})) ||
-            (&Apache::lonnet::allowed('mau',$env{'request.role.domain'}))) {
-            $permission{'cusr'} = 1;
+        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;
@@ -3816,5 +3919,165 @@ sub authorpriv {
          || (&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;