[LON-CAPA-cvs] cvs: loncom /auth lonroles.pm /interface domainprefs.pm loncommon.pm loncreateuser.pm lonmenu.pm lonpickcourse.pm lonuserutils.pm /lonnet/perl lonnet.pm

raeburn raeburn at source.lon-capa.org
Mon Jan 2 14:44:21 EST 2017


raeburn		Mon Jan  2 19:44:21 2017 EDT

  Modified files:              
    /loncom/interface	domainprefs.pm loncommon.pm loncreateuser.pm 
                     	lonmenu.pm lonpickcourse.pm lonuserutils.pm 
    /loncom/auth	lonroles.pm 
    /loncom/lonnet/perl	lonnet.pm 
  Log:
  - Ad hoc roles for helpdesk personnel.
    - Domain configuration for:
      - which user(s) with dh role in domain may use each ad hoc role.
        - set by institutional status, or by include specific people, or by
          exclude specific people.
      - assign role description (displayed on Roles page, and at top left of 
        page, when role active).
      - assign role order (numerical).
    - Course configuration (available for editing by course owner, and viewing
      by co-owner(s).
      - can override domain defaults for which dh role users can use ad hoc role(s)
        in course.
      - can override domain defaults set for course-level privileges for ad hoc
        role(s).  
  
  
-------------- next part --------------
Index: loncom/interface/domainprefs.pm
diff -u loncom/interface/domainprefs.pm:1.284 loncom/interface/domainprefs.pm:1.285
--- loncom/interface/domainprefs.pm:1.284	Mon Dec  5 00:51:53 2016
+++ loncom/interface/domainprefs.pm	Mon Jan  2 19:44:06 2017
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to set domain-wide configuration settings
 #
-# $Id: domainprefs.pm,v 1.284 2016/12/05 00:51:53 raeburn Exp $
+# $Id: domainprefs.pm,v 1.285 2017/01/02 19:44:06 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -687,7 +687,7 @@
     } elsif ($action eq 'requestauthor') {
         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
     } elsif ($action eq 'helpsettings') {
-        $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
+        $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
     } elsif ($action eq 'coursedefaults') {
         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
     } elsif ($action eq 'selfenrollment') {
@@ -3045,6 +3045,7 @@
 sub print_helpsettings {
     my ($position,$dom,$settings,$rowtotal) = @_;
     my $confname = $dom.'-domainconfig';
+    my $formname = 'display';
     my ($datatable,$itemcount);
     if ($position eq 'top') {
         $itemcount = 1;
@@ -3060,20 +3061,69 @@
     } else {
         my $css_class;
         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
-        my %customroles;
-        foreach my $key (keys(%existing)) {
+        my (%customroles,%ordered,%current);
+        if (ref($settings->{'adhoc'}) eq 'HASH') {
+            %current = %{$settings->{'adhoc'}};
+        }
+        my $count = 0;
+        foreach my $key (sort(keys(%existing))) {
             if ($key=~/^rolesdef\_(\w+)$/) {
                 my $rolename = $1;
-                my %privs;
+                my (%privs,$order);
                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
                 $customroles{$rolename} = \%privs;
+                if (ref($current{$rolename}) eq 'HASH') {
+                    $order = $current{$rolename}{'order'};
+                }
+                if ($order eq '') {
+                    $order = $count;
+                }
+                $ordered{$order} = $rolename;
+                $count++;
             }
         }
-        my $count = 0;
+        my $maxnum = scalar(keys(%ordered));
+        my @roles_by_num = ();
+        foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
+            push(@roles_by_num,$item);
+        }
+        my $context = 'domprefs';
+        my $crstype = 'Course';
+        my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
+        my @accesstypes = ('all','none');
+        my ($numstatustypes, at jsarray);
+        if (ref($types) eq 'ARRAY') {
+            if (@{$types} > 0) {
+                $numstatustypes = scalar(@{$types});
+                push(@accesstypes,'status');
+                @jsarray = ('bystatus');
+            }
+        }
+        my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh']);
+        if (keys(%domhelpdesk)) {
+            push(@accesstypes,('inc','exc'));
+            push(@jsarray,('notinc','notexc'));
+        }
+        my $hiddenstr = join("','", at jsarray);
+        $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
         my $context = 'domprefs';
         my $crstype = 'Course';
-        foreach my $role (sort(keys(%customroles))) {
-            my $prefix = 'custhelp'.$count;
+        my $prefix = 'helproles_';
+        my $add_class = 'LC_hidden';
+        foreach my $num (@roles_by_num) {
+            my $role = $ordered{$num};
+            my ($desc,$access, at statuses);
+            if (ref($current{$role}) eq 'HASH') {
+                $desc = $current{$role}{'desc'};
+                $access = $current{$role}{'access'};
+                if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
+                    @statuses = @{$current{$role}{'insttypes'}};
+                }
+            }
+            if ($desc eq '') {
+                $desc = $role;
+            }
+            my $identifier = 'custhelp'.$num;
             my %full=();
             my %levels= (
                          course => {},
@@ -3088,18 +3138,32 @@
             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
             $css_class = $itemcount%2?' class="LC_odd_row"':'';
-            $datatable .= '<tr '.$css_class.'><td><label>'.
-                          '<input type="checkbox" name="modifycusthelp" value="'.$count.'" />'.
-                          '<input type="hidden" name="custhelprole'.$count.'" value="'.$role.'" />'. 
-                          &mt('Modify').'</label></td>'.
-                          '<td>'.&mt('Existing helpdesk role:').' '.
-                          '<b>'.$role.'</b><br />'.
-                          &Apache::lonuserutils::custom_role_header($context,$crstype,
-                                                                    \@templateroles,$prefix).
+            my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
+            $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
+                          '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
+            for (my $k=0; $k<=$maxnum; $k++) {
+                my $vpos = $k+1;
+                my $selstr;
+                if ($k == $num) {
+                    $selstr = ' selected="selected" ';
+                }
+                $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
+            }
+            $datatable .= '</select>'.(' 'x2).
+                          '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
+                          '</td>'.
+                          '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
+                          &mt('Name shown to users:').
+                          '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
+                          '</fieldset>'.
+                          &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
+                                                $othertitle,$usertypes,$types,\%domhelpdesk).
+                          '<fieldset>'.
+                          '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
-                                                                   \%levelscurrent,$prefix).
-                          '<br /></td>';
-            $count ++;
+                                                                   \%levelscurrent,$identifier,
+                                                                   'LC_hidden',$prefix.$num.'_privs').
+                          '</fieldset></td>';
             $itemcount ++;
         }
         $css_class = $itemcount%2?' class="LC_odd_row"':'';
@@ -3113,25 +3177,250 @@
                     );
         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
-        $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><label>'.
+        my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
+        $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
+                      '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
+                      '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
+        for (my $k=0; $k<$maxnum+1; $k++) {
+            my $vpos = $k+1;
+            my $selstr;
+            if ($k == $maxnum) {
+                $selstr = ' selected="selected" ';
+            }
+            $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
+        }
+        $datatable .= '</select> '."\n".
                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
                       '</label></span></td>'.
-                      '<td><span class="LC_nobreak">'.
-                      '<b>'.&mt('Name of new helpdesk role:').'</b> '.
-                      '<input type="text" size="20" name="newcusthelpname" value="" />'.
-                      '</span><br />'.
+                      '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
+                      '<span class="LC_nobreak">'.
+                      &mt('Internal name:').
+                      '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
+                      '</span>'.(' 'x4).
+                      '<span class="LC_nobreak">'.
+                      &mt('Name shown to users:').
+                      '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
+                      '</span></fieldset>'.
+                       &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
+                                             $usertypes,$types,\%domhelpdesk).
+                      '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
                       &Apache::lonuserutils::custom_role_header($context,$crstype,
                                                                 \@templateroles,$newcust).
                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
                                                                \%levelscurrent,$newcust).
-                      '<br /><br />'.
-                      '</td></tr>';
+                      '</fieldset></td></tr>';
         $count ++;
         $$rowtotal += $count;
     }
     return $datatable;
 }
 
+sub adhocbutton {
+    my ($prefix,$num,$field,$visibility) = @_;
+    my %lt = &Apache::lonlocal::texthash(
+                                          show => 'Show details',
+                                          hide => 'Hide details',
+                                        );
+    return '<span style="text-decoration:line-through; font-weight: normal;">'.(' 'x10).
+           '</span>'.(' 'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
+           ' value="'.$lt{$visibility}.'" style="height:20px;" '.
+           'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.(' 'x2);
+}
+
+sub helpsettings_javascript {
+    my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
+    return unless(ref($roles_by_num) eq 'ARRAY');
+    my %html_js_lt = &Apache::lonlocal::texthash(
+                                          show => 'Show details',
+                                          hide => 'Hide details',
+                                        );
+    &html_escape(\%html_js_lt);
+    my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
+    return <<"ENDSCRIPT";
+<script type="text/javascript">
+// <![CDATA[
+
+function reorderHelpRoles(form,item) {
+    var changedVal;
+$jstext
+    var newpos = 'helproles_${total}_pos';
+    var maxh = 1 + $total;
+    var current = new Array();
+    var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
+    if (item == newpos) {
+        changedVal = newitemVal;
+    } else {
+        changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
+        current[newitemVal] = newpos;
+    }
+    for (var i=0; i<helproles.length; i++) {
+        var elementName = 'helproles_'+helproles[i]+'_pos';
+        if (elementName != item) {
+            if (form.elements[elementName]) {
+                var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
+                current[currVal] = elementName;
+            }
+        }
+    }
+    var oldVal;
+    for (var j=0; j<maxh; j++) {
+        if (current[j] == undefined) {
+            oldVal = j;
+        }
+    }
+    if (oldVal < changedVal) {
+        for (var k=oldVal+1; k<=changedVal ; k++) {
+           var elementName = current[k];
+           form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
+        }
+    } else {
+        for (var k=changedVal; k<oldVal; k++) {
+            var elementName = current[k];
+            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
+        }
+    }
+    return;
+}
+
+function helpdeskAccess(num) {
+    var curraccess = null;
+    if (document.$formname.elements['helproles_'+num+'_access'].length) {
+        for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
+            if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
+                curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
+            }
+        }
+    }
+    var shown = Array();
+    var hidden = Array();
+    if (curraccess == 'none') {
+        hidden = Array('$hiddenstr');
+    } else {
+        if (curraccess == 'status') {
+            shown = Array('bystatus');
+            hidden = Array('notinc','notexc');
+        } else {
+            if (curraccess == 'exc') {
+                shown = Array('notexc');
+                hidden = Array('notinc','bystatus');
+            }
+            if (curraccess == 'inc') {
+                shown = Array('notinc');
+                hidden = Array('notexc','bystatus');
+            }
+            if (curraccess == 'all') {
+                hidden = Array('notinc','notexc','bystatus');
+            }
+        }
+    }
+    if (hidden.length > 0) {
+        for (var i=0; i<hidden.length; i++) {
+            if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
+                document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
+            }
+        }
+    }
+    if (shown.length > 0) {
+        for (var i=0; i<shown.length; i++) {
+            if (document.getElementById('helproles_'+num+'_'+shown[i])) {
+                if (shown[i] == 'privs') {
+                    document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
+                } else {
+                    document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
+                }
+            }
+        }
+    }
+    return;
+}
+
+function toggleHelpdeskItem(num,field) {
+    if (document.getElementById('helproles_'+num+'_'+field)) {
+        if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
+            document.getElementById('helproles_'+num+'_'+field).className = 
+                document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
+            if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
+                document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
+            }
+        } else {
+            document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
+            if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
+                document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
+            }
+        }
+    }
+    return;
+}
+
+// ]]>
+</script>
+
+ENDSCRIPT
+}
+
+sub helpdeskroles_access {
+    my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
+        $usertypes,$types,$domhelpdesk) = @_;
+    return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
+    my %lt = &Apache::lonlocal::texthash(
+                    'rou'    => 'Role usage',
+                    'whi'    => 'Which helpdesk personnel may use this role?',
+                    'all'    => 'All',
+                    'none'   => 'None',
+                    'status' => 'Determined based on institutional status',
+                    'inc'    => 'Include all, but exclude specific personnel',
+                    'exc'    => 'Exclude all, but include specific personnel',
+                  );
+    my %usecheck = (
+                     all => ' checked="checked"',
+                   );
+    my %displaydiv = (
+                      status => 'none',
+                      inc    => 'none',
+                      exc    => 'none',
+                      priv   => 'block',
+                     );
+    my $output;
+    if (ref($current) eq 'HASH') {
+        if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
+            if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
+                $usecheck{$current->{access}} = $usecheck{'all'};
+                delete($usecheck{'all'});
+                if ($current->{access} =~ /^(status|inc|exc)$/) {
+                    my $access = $1;
+                    $displaydiv{$access} = 'inline';
+                } elsif ($current->{access} eq 'none') {
+                    $displaydiv{'priv'} = 'none';
+                }
+            }
+        }
+    }
+    $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
+              '<p>'.$lt{'whi'}.'</p>';
+    foreach my $access (@{$accesstypes}) {
+        $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
+                   ' onclick="helpdeskAccess('."'$num'".');" />'.
+                   $lt{$access}.'</label>';
+        if ($access eq 'status') {
+            $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
+                       &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
+                                                                 $othertitle,$usertypes,$types).
+                       '</div>';
+        } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
+            $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
+                       &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
+                       '</div>';
+        } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
+            $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
+                       &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
+                       '</div>';
+        }
+        $output .= '</p>';
+    }
+    $output .= '</fieldset>';
+    return $output;
+}
+
 sub radiobutton_prefs {
     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
         $additional,$align) = @_;
@@ -8367,7 +8656,7 @@
             }
             if ($env{'form.validationdc'}) {
                 my $newval = $env{'form.validationdc'};
-                my %domcoords = &get_active_dcs($dom);
+                my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
                 if (exists($domcoords{$newval})) {
                     $confhash{'validation'}{'dc'} = $newval;
                 }
@@ -9624,7 +9913,7 @@
         $newvals{$item} = 0 if ($newvals{$item} eq '');
     }
     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
-    my %domcoords = &get_active_dcs($dom);
+    my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
     unless (exists($domcoords{$newvals{'xmldc'}})) {
         $newvals{'xmldc'} = '';
     } 
@@ -11810,15 +12099,18 @@
 }
 
 sub modify_helpsettings {
-    my ($r,$dom,$confname,%domconfig) = @_;
+    my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
     my ($resulttext,$errors,%changes,%helphash);
     my %defaultchecked = ('submitbugs' => 'on');
     my @offon = ('off','on');
     my @toggles = ('submitbugs');
-    my %current = ('submitbugs' => '');  
+    my %current = ('submitbugs' => '',
+                   'adhoc'      => {},
+                  );
     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
         %current = %{$domconfig{'helpsettings'}};
     }
+    my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
     foreach my $item (@toggles) {
         if ($defaultchecked{$item} eq 'on') { 
             if ($current{$item} eq '') {
@@ -11841,57 +12133,203 @@
             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
         }
     }
-
-    my @modify = &Apache::loncommon::get_env_multiple('form.modifycusthelp');
+    my $maxnum = $env{'form.helproles_maxnum'};
     my $confname = $dom.'-domainconfig';
     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
-    if (@modify) {
-        foreach my $num (@modify) {
-            my $rolename = $env{'form.custhelprole'.$num};
-            if ($rolename ne '') {
-                if (exists($existing{'rolesdef_'.$rolename})) {
-                    my $prefix = 'custhelp'.$num;
-                    my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$prefix);
-                    my %currprivs;
-                    ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) = 
-                        split(/\_/,$existing{'rolesdef_'.$rolename});
-                    foreach my $level ('c','d','s') {
-                        if ($newprivs{$level} ne $currprivs{$level}) {
-                            $changes{'customrole'}{$rolename} = 1;
-                            my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
-                                                                     $newprivs{'c'},$confname,$dom);
-                            last;
+    my (@allpos,%newsettings,%changedprivs,$newrole);
+    my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
+    my @accesstypes = ('all','none','status','inc','exc');
+    my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh']);
+    my %lt = &Apache::lonlocal::texthash(
+                    s      => 'system',
+                    d      => 'domain',
+                    order  => 'Display order',
+                    access => 'Role usage',
+                    all    => 'All',
+                    none   => 'None',
+                    status => 'Determined based on institutional status',
+                    inc    => 'Include all, but exclude specific personnel',
+                    exc    => 'Exclude all, but include specific personnel',
+    );
+    for (my $num=0; $num<=$maxnum; $num++) {
+        my ($prefix,$identifier,$rolename,%curr);
+        if ($num == $maxnum) {
+            next unless ($env{'form.newcusthelp'} == $maxnum);
+            $identifier = 'custhelp'.$num;
+            $prefix = 'helproles_'.$num;
+            $rolename = $env{'form.custhelpname'.$num};
+            $rolename=~s/[^A-Za-z0-9]//gs;
+            next if ($rolename eq '');
+            next if (exists($existing{'rolesdef_'.$rolename}));
+            my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
+            my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
+                                                     $newprivs{'c'},$confname,$dom);
+            if ($result ne 'ok') {
+                $errors .= '<li><span class="LC_error">'.
+                           &mt('An error occurred storing the new custom role: [_1]',
+                           $result).'</span></li>';
+                next;
+            } else {
+                $changedprivs{$rolename} = \%newprivs;
+                $newrole = $rolename;
+            }
+        } else {
+            $prefix = 'helproles_'.$num;
+            $rolename = $env{'form.'.$prefix};
+            next if ($rolename eq '');
+            next unless (exists($existing{'rolesdef_'.$rolename}));
+            $identifier = 'custhelp'.$num;
+            my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
+            my %currprivs;
+            ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) = 
+                split(/\_/,$existing{'rolesdef_'.$rolename});
+            foreach my $level ('c','d','s') {
+                if ($newprivs{$level} ne $currprivs{$level}) {
+                    my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
+                                                             $newprivs{'c'},$confname,$dom);
+                    if ($result ne 'ok') {
+                        $errors .= '<li><span class="LC_error">'.
+                                   &mt('An error occurred storing privileges for existing role [_1]: [_2]',
+                                       $rolename,$result).'</span></li>';
+                    } else {
+                        $changedprivs{$rolename} = \%newprivs;
+                    }
+                    last;
+                }
+            }
+            if (ref($current{'adhoc'}) eq 'HASH') {
+                if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
+                    %curr = %{$current{'adhoc'}{$rolename}};
+                }
+            }
+        }
+        my $newpos = $env{'form.'.$prefix.'_pos'};
+        $newpos =~ s/\D+//g;
+        $allpos[$newpos] = $rolename;
+        my $newdesc = $env{'form.'.$prefix.'_desc'};
+        $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
+        if ($curr{'desc'}) {
+            if ($curr{'desc'} ne $newdesc) {
+                $changes{'customrole'}{$rolename}{'desc'} = 1;
+                $newsettings{$rolename}{'desc'} = $newdesc;
+            }
+        } elsif ($newdesc ne '') {
+            $changes{'customrole'}{$rolename}{'desc'} = 1;
+            $newsettings{$rolename}{'desc'} = $newdesc;
+        }
+        my $access = $env{'form.'.$prefix.'_access'};
+        if (grep(/^\Q$access\E$/, at accesstypes)) {
+            $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
+            if ($access eq 'status') {
+                my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
+                if (scalar(@statuses) == 0) {
+                    $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none'; 
+                } else {
+                    my (@shownstatus,$numtypes);
+                    $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
+                    if (ref($types) eq 'ARRAY') {
+                        $numtypes = scalar(@{$types});
+                        foreach my $type (sort(@statuses)) {
+                            if ($type eq 'default') {
+                                push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
+                            } elsif (grep(/^\Q$type\E$/,@{$types})) {
+                                push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
+                                push(@shownstatus,$usertypes->{$type});
+                            }
                         }
                     }
+                    if (grep(/^default$/, at statuses)) {
+                        push(@shownstatus,$othertitle);
+                    }
+                    if (scalar(@shownstatus) == 1+$numtypes) {
+                        $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
+                        delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
+                    } else {
+                        $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ', at shownstatus);
+                        if (ref($curr{'status'}) eq 'ARRAY') {
+                            my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
+                            if (@diffs) {
+                                $changes{'customrole'}{$rolename}{$access} = 1;
+                            }
+                        } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
+                            $changes{'customrole'}{$rolename}{$access} = 1;
+                        }
+                    }
+                }
+            } elsif (($access eq 'inc') || ($access eq 'exc')) {
+                my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
+                my @newspecstaff;
+                $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
+                foreach my $person (sort(@personnel)) {
+                    if ($domhelpdesk{$person}) {
+                        push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
+                    }
                 }
+                if (ref($curr{$access}) eq 'ARRAY') {
+                    my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
+                    if (@diffs) {
+                        $changes{'customrole'}{$rolename}{$access} = 1;
+                    }
+                } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
+                    $changes{'customrole'}{$rolename}{$access} = 1;
+                }
+                foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
+                    my ($uname,$udom) = split(/:/,$person);
+                        push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
+                }
+                $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
             }
+        } else {
+            $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
+        }
+        unless ($curr{'access'} eq $access) {
+            $changes{'customrole'}{$rolename}{'access'} = 1;
+            $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
         }
     }
-    if ($env{'form.newcusthelp'} ne '') {
-        my $prefix = 'custhelp'.$env{'form.newcusthelp'};
-        my $rolename = $env{'form.newcusthelpname'};
-        $rolename=~s/[^A-Za-z0-9]//gs;
-        if ($rolename ne '') {
-            unless(exists($existing{'rolesdef_'.$rolename})) {
-                my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$prefix);
-                my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
-                                                         $newprivs{'c'},$confname,$dom);
-                if ($result eq 'ok') {
-                    $changes{'newcustomrole'} = $rolename;
-                } else {
-                    $errors .= '<li><span class="LC_error">'.
-                               &mt('An error occurred storing the new custom role: [_1]',
-                                   $result).'</span></li>';
+    if (@allpos > 0) {
+        my $idx = 0;
+        foreach my $rolename (@allpos) {
+            if ($rolename ne '') {
+                $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
+                if (ref($current{'adhoc'}) eq 'HASH') {
+                    if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
+                        if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
+                            $changes{'customrole'}{$rolename}{'order'} = 1;
+                            $newsettings{$rolename}{'order'} = $idx+1; 
+                        }
+                    }
                 }
+                $idx ++;
             }
         }
     }
-
     my $putresult;
     if (keys(%changes) > 0) {
         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
         if ($putresult eq 'ok') {
-            $resulttext = &mt('Changes made:').'<ul>';
+            if (ref($helphash{'helpsettings'}) eq 'HASH') {
+                $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
+                if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
+                    $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
+                }
+            }
+            my $cachetime = 24*60*60;
+            &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
+            if (ref($lastactref) eq 'HASH') {
+                $lastactref->{'domdefaults'} = 1;
+            }
+        } else {
+            $errors .= '<li><span class="LC_error">'.
+                       &mt('An error occurred storing the settings: [_1]',
+                           $putresult).'</span></li>';
+        }
+    }
+    if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
+        $resulttext = &mt('Changes made:').'<ul>';
+        my (%shownprivs, at levelorder);
+        @levelorder = ('c','d','s');
+        if ((keys(%changes)) && ($putresult eq 'ok')) {
             foreach my $item (sort(keys(%changes))) {
                 if ($item eq 'submitbugs') {
                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
@@ -11899,27 +12337,86 @@
                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
                 } elsif ($item eq 'customrole') {
                     if (ref($changes{'customrole'}) eq 'HASH') {
+                        my @keyorder = ('order','desc','access','status','exc','inc');
+                        my %keytext = &Apache::lonlocal::texthash(
+                                                                   order  => 'Order',
+                                                                   desc   => 'Role description',
+                                                                   access => 'Role usage',
+                                                                   status => 'Allowed instituional types',
+                                                                   exc    => 'Allowed personnel',
+                                                                   inc    => 'Disallowed personnel',
+                        );
                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
-                            $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
-                                                     $role).'</li>';
+                            if (ref($changes{'customrole'}{$role}) eq 'HASH') {
+                                if ($role eq $newrole) {
+                                    $resulttext .= '<li>'.&mt('New custom role added: [_1]',
+                                                              $role).'<ul>';
+                                } else {
+                                    $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
+                                                              $role).'<ul>';
+                                }
+                                foreach my $key (@keyorder) {
+                                    if ($changes{'customrole'}{$role}{$key}) {
+                                        $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
+                                                                  $keytext{$key},$newsettings{$role}{$key}).
+                                                       '</li>';
+                                    }
+                                }
+                                if (ref($changedprivs{$role}) eq 'HASH') {
+                                    $shownprivs{$role} = 1;
+                                    $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
+                                    foreach my $level (@levelorder) {
+                                        foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
+                                            next if ($item eq '');
+                                            my ($priv) = split(/\&/,$item,2);
+                                            if (&Apache::lonnet::plaintext($priv)) {
+                                                $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
+                                                unless ($level eq 'c') {
+                                                    $resulttext .= ' ('.$lt{$level}.')';
+                                                }
+                                                $resulttext .= '</li>';
+                                            }
+                                        }
+                                    }
+                                    $resulttext .= '</ul>';
+                                }
+                                $resulttext .= '</ul></li>';
+                            }
                         }
                     }
-                } elsif ($item eq 'newcustomrole') {
-                    $resulttext .= '<li>'.&mt('New custom role added: [_1]',
-                                              $changes{'newcustomrole'}).'</li>';
                 }
             }
-            $resulttext .= '</ul>';
-        } else {
-            $resulttext = &mt('No changes made to help settings');
-            $errors .= '<li><span class="LC_error">'.
-                       &mt('An error occurred storing the settings: [_1]',
-                           $putresult).'</span></li>';
         }
+        if (keys(%changedprivs)) {
+            foreach my $role (sort(keys(%changedprivs))) {
+                unless ($shownprivs{$role}) {
+                    $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
+                                              $role).'<ul>'.
+                                   '<li>'.&mt('Privileges set to :').'<ul>';
+                    foreach my $level (@levelorder) {
+                        foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
+                            next if ($item eq '');
+                            my ($priv) = split(/\&/,$item,2);
+                            if (&Apache::lonnet::plaintext($priv)) {
+                                $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
+                                unless ($level eq 'c') {
+                                    $resulttext .= ' ('.$lt{$level}.')';
+                                }
+                                $resulttext .= '</li>';
+                            }
+                        }
+                    }
+                    $resulttext .= '</ul></li></ul></li>';
+                }
+            }
+        }
+        $resulttext .= '</ul>';
+    } else {
+        $resulttext = &mt('No changes made to help settings');
     }
     if ($errors) {
         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
-                       $errors.'</ul>';
+                                    $errors.'</ul>';
     }
     return $resulttext;
 }
@@ -13454,21 +13951,6 @@
     return;
 }
 
-sub get_active_dcs {
-    my ($dom) = @_;
-    my $now = time;
-    my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
-    my %domcoords;
-    my $numdcs = 0;
-    foreach my $server (keys(%dompersonnel)) {
-        foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
-            my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
-            $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
-        }
-    }
-    return %domcoords;
-}
-
 sub active_dc_picker {
     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
     my %domcoords = &get_active_dcs($dom);
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.1268 loncom/interface/loncommon.pm:1.1269
--- loncom/interface/loncommon.pm:1.1268	Sun Jan  1 16:32:01 2017
+++ loncom/interface/loncommon.pm	Mon Jan  2 19:44:06 2017
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.1268 2017/01/01 16:32:01 raeburn Exp $
+# $Id: loncommon.pm,v 1.1269 2017/01/02 19:44:06 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -5862,7 +5862,11 @@
         if ($env{'request.role'} !~ /^cr/) {
             $role = &Apache::lonnet::plaintext($role,&course_type());
         } elsif ($role =~ m{^cr/($match_domain)/\1-domainconfig/(\w+)$}) {
-            $role = &mt('Helpdesk[_1]',' '.$2); 
+            if ($env{'request.role.desc'}) {
+                $role = $env{'request.role.desc'};
+            } else {
+                $role = &mt('Helpdesk[_1]',' '.$2);
+            }
         } else {
             $role = (split(/\//,$role,4))[-1]; 
         }
@@ -8122,6 +8126,39 @@
   content:url('/adm/daxe/images/section_icons/warning.png');
 }
 
+#LC_minitab_header {
+  float:left;
+  width:100%;
+  background:#DAE0D2 url("/res/adm/pages/minitabmenu_bg.gif") repeat-x bottom;
+  font-size:93%;
+  line-height:normal;
+  margin: 0.5em 0 0.5em 0;
+}
+#LC_minitab_header ul {
+  margin:0;
+  padding:10px 10px 0;
+  list-style:none;
+}
+#LC_minitab_header li {
+  float:left;
+  background:url("/res/adm/pages/minitabmenu_left.gif") no-repeat left top;
+  margin:0;
+  padding:0 0 0 9px;
+}
+#LC_minitab_header a {
+  display:block;
+  background:url("/res/adm/pages/minitabmenu_right.gif") no-repeat right top;
+  padding:5px 15px 4px 6px;
+}
+#LC_minitab_header #LC_current_minitab {
+  background-image:url("/res/adm/pages/minitabmenu_left_on.gif");
+}
+#LC_minitab_header #LC_current_minitab a {
+  background-image:url("/res/adm/pages/minitabmenu_right_on.gif");
+  padding-bottom:5px;
+}
+
+
 END
 }
 
Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.427 loncom/interface/loncreateuser.pm:1.428
--- loncom/interface/loncreateuser.pm:1.427	Sun Jan  1 14:08:34 2017
+++ loncom/interface/loncreateuser.pm	Mon Jan  2 19:44:06 2017
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.427 2017/01/01 14:08:34 raeburn Exp $
+# $Id: loncreateuser.pm,v 1.428 2017/01/02 19:44:06 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -5038,6 +5038,21 @@
             $r->print(&header(undef,{'no_nav_bar' => 1}).
                      '<span class="LC_error">'.&mt('You do not have permission to view change logs').'</span>');
         }
+    } elsif ($env{'form.action'} eq 'helpdesk') {
+        if (($permission->{'owner'}) || ($permission->{'co-owner'})) {
+            if ($env{'form.state'} eq 'process') {
+                if ($permission->{'owner'}) {
+                    &update_helpdeskaccess($r,$permission,$brcrum);
+                } else {
+                    &print_helpdeskaccess_display($r,$permission,$brcrum);
+                }    
+            } else {
+                &print_helpdeskaccess_display($r,$permission,$brcrum);
+            }
+        } else {
+            $r->print(&header(undef,{'no_nav_bar' => 1}).
+                      '<span class="LC_error">'.&mt('You do not have permission to view helpdesk access').'</span>');
+        }
     } else {
         $bread_crumbs_component = 'User Management';
         $args = { bread_crumbs           => $brcrum,
@@ -5450,7 +5465,15 @@
             },
         );
         push(@{ $menu[2]->{items} }, #Category: Administration
-            {    
+            {
+             linktext => 'Helpdesk Access',
+             icon => 'helpdesk-access.png',
+             #help => 'Course_Helpdesk_Access',
+             url => '/adm/createuser?action=helpdesk',
+             permission => ($permission->{'owner'} || $permission->{'co-owner'}),
+             linktitle => 'Helpdesk access options',
+            },
+            {
              linktext => 'Custom Roles',
              icon => 'emblem-photos.png',
              #help => 'Course_Editing_Custom_Roles',
@@ -7157,6 +7180,969 @@
     return %lt;
 }
 
+sub print_helpdeskaccess_display {
+    my ($r,$permission,$brcrum) = @_;
+    my $formname = 'helpdeskaccess';
+    my $helpitem = 'Course_Helpdesk_Access';
+    push (@{$brcrum},
+             {href => '/adm/createuser?action=helpdesk',
+              text => 'Helpdesk Access',
+              help => $helpitem});
+    my $bread_crumbs_component = 'Helpdesk Staff Access';
+    my $args = { bread_crumbs           => $brcrum,
+                 bread_crumbs_component => $bread_crumbs_component};
+
+    my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+    my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+    my $confname = $cdom.'-domainconfig';
+    my $crstype = &Apache::loncommon::course_type();
+
+    my @accesstypes = ('all','none');
+    my ($numstatustypes, at jsarray);
+    my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($cdom);
+    if (ref($types) eq 'ARRAY') {
+        if (@{$types} > 0) {                
+            $numstatustypes = scalar(@{$types});
+            push(@accesstypes,'status');
+            @jsarray = ('bystatus');
+        }
+    }
+    my %customroles = &get_domain_customroles($cdom,$confname);
+    my %domhelpdesk = &Apache::lonnet::get_active_domroles($cdom,['dh']);
+    if (keys(%domhelpdesk)) {
+       push(@accesstypes,('inc','exc'));
+       push(@jsarray,('notinc','notexc'));
+    }
+    push(@jsarray,'privs');
+    my $hiddenstr = join("','", at jsarray);
+    my $rolestr = join("','",sort(keys(%customroles)));
+
+    my $jscript;
+    my (%settings,%overridden);
+    if (keys(%customroles)) {
+        &get_adhocrole_settings($env{'request.course.id'},\@accesstypes,
+                                $types,\%customroles,\%settings,\%overridden);
+        my %jsfull=();
+        my %jslevels= (
+                     course => {},
+                     domain => {},
+                     system => {},
+                    );
+        my %jslevelscurrent=(
+                           course => {},
+                           domain => {},
+                           system => {},
+                          );
+        my (%privs,%jsprivs);
+        &Apache::lonuserutils::custom_role_privs(\%privs,\%jsfull,\%jslevels,\%jslevelscurrent);
+        foreach my $priv (keys(%jsfull)) {
+            if ($jslevels{'course'}{$priv}) {
+                $jsprivs{$priv} = 1;
+            }
+        }
+        my (%elements,%stored);
+        foreach my $role (keys(%customroles)) {
+            $elements{$role.'_access'} = 'radio';
+            $elements{$role.'_incrs'} = 'radio';
+            if ($numstatustypes) {
+                $elements{$role.'_status'} = 'checkbox';
+            }
+            if (keys(%domhelpdesk) > 0) {
+                $elements{$role.'_staff_inc'} = 'checkbox';
+                $elements{$role.'_staff_exc'} = 'checkbox';
+            }
+            $elements{$role.'_override'} = 'checkbox';      
+            if (ref($settings{$role}) eq 'HASH') {
+                if ($settings{$role}{'access'} ne '') {
+                    my $curraccess = $settings{$role}{'access'};
+                    $stored{$role.'_access'} = $curraccess;
+                    $stored{$role.'_incrs'} = 1;
+                    if ($curraccess eq 'status') {
+                        if (ref($settings{$role}{'status'}) eq 'ARRAY') {
+                            $stored{$role.'_status'} = $settings{$role}{'status'};
+                        }
+                    } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
+                        if (ref($settings{$role}{$curraccess}) eq 'ARRAY') {
+                            $stored{$role.'_staff_'.$curraccess} = $settings{$role}{$curraccess};
+                        }
+                    }
+                } else {
+                    $stored{$role.'_incrs'} = 0;
+                }
+                $stored{$role.'_override'} = [];
+                if ($env{'course.'.$env{'request.course.id'}.'.internal.adhocpriv.'.$role}) {
+                    if (ref($settings{$role}{'off'}) eq 'ARRAY') {
+                        foreach my $priv (@{$settings{$role}{'off'}}) {
+                            push(@{$stored{$role.'_override'}},$priv);
+                        }
+                    }
+                    if (ref($settings{$role}{'on'}) eq 'ARRAY') {
+                        foreach my $priv (@{$settings{$role}{'on'}}) {
+                            unless (grep(/^$priv$/,@{$stored{$role.'_override'}})) {
+                                push(@{$stored{$role.'_override'}},$priv);
+                            }
+                        }
+                    }
+                }
+            } else {
+                $stored{$role.'_incrs'} = 0;
+            }
+        }
+        $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements,\%stored);
+    }
+
+    my $js = <<"ENDJS";
+<script type="text/javascript">
+// <![CDATA[
+$jscript;
+
+function switchRoleTab(caller,role) {
+    if (document.getElementById(role+'_maindiv')) {
+        if (caller.id != 'LC_current_minitab') {
+            if (document.getElementById('LC_current_minitab')) {
+                document.getElementById('LC_current_minitab').id=null;
+            }
+            var roledivs = Array('$rolestr');
+            if (roledivs.length > 0) {
+                for (var i=0; i<roledivs.length; i++) {
+                    if (document.getElementById(roledivs[i]+'_maindiv')) {
+                        document.getElementById(roledivs[i]+'_maindiv').style.display='none';
+                    }
+                }
+            }
+            caller.id = 'LC_current_minitab';
+            document.getElementById(role+'_maindiv').style.display='block';
+        }
+    }
+    return false;
+}         
+
+function helpdeskAccess(role) {
+    var curraccess = null;
+    if (document.$formname.elements[role+'_access'].length) {
+        for (var i=0; i<document.$formname.elements[role+'_access'].length; i++) {
+            if (document.$formname.elements[role+'_access'][i].checked) {
+                curraccess = document.$formname.elements[role+'_access'][i].value;
+            }
+        }
+    }
+    var shown = Array();
+    var hidden = Array();
+    if (curraccess == 'none') {
+        hidden = Array ('$hiddenstr'); 
+    } else {
+        if (curraccess == 'status') {
+            shown = Array ('bystatus','privs'); 
+            hidden = Array ('notinc','notexc');    
+        } else {
+            if (curraccess == 'exc') {
+                shown = Array ('notexc','privs');
+                hidden = Array ('notinc','bystatus');
+            }
+            if (curraccess == 'inc') {
+                shown = Array ('notinc','privs');
+                hidden = Array ('notexc','bystatus');
+            }
+            if (curraccess == 'all') {
+                shown = Array ('privs');
+                hidden = Array ('notinc','notexc','bystatus');
+            }
+        }
+    }
+    if (hidden.length > 0) {
+        for (var i=0; i<hidden.length; i++) {
+            if (document.getElementById(role+'_'+hidden[i])) {
+                document.getElementById(role+'_'+hidden[i]).style.display = 'none'; 
+            }
+        }
+    }
+    if (shown.length > 0) {
+        for (var i=0; i<shown.length; i++) {
+            if (document.getElementById(role+'_'+shown[i])) {
+                if (shown[i] == 'privs') {
+                    document.getElementById(role+'_'+shown[i]).style.display = 'block';
+                } else {
+                    document.getElementById(role+'_'+shown[i]).style.display = 'inline';
+                }
+            }
+        }
+    }
+    return;
+}
+
+function toggleAccess(role) {
+    if ((document.getElementById(role+'_setincrs')) &&
+        (document.getElementById(role+'_setindom'))) {
+        for (var i=0; i<document.$formname.elements[role+'_incrs'].length; i++) {
+            if (document.$formname.elements[role+'_incrs'][i].checked) {
+                if (document.$formname.elements[role+'_incrs'][i].value == 1) {
+                    document.getElementById(role+'_setindom').style.display = 'none';
+                    document.getElementById(role+'_setincrs').style.display = 'block'; 
+                } else {
+                    document.getElementById(role+'_setincrs').style.display = 'none';
+                    document.getElementById(role+'_setindom').style.display = 'block';
+                }
+                break;
+            }
+        }
+    }
+    return;
+}
+
+// ]]>
+</script>
+ENDJS
+
+    $args->{add_entries} = {onload => "javascript:setFormElements(document.$formname)"};
+
+    # print page header
+    $r->print(&header($js,$args));
+    # print form header
+    $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">');
+
+    if (keys(%customroles)) {
+        my %lt = &Apache::lonlocal::texthash(
+                    'aco'    => 'As course owner you may override the defaults set in the domain for role usage and/or privileges.',
+                    'rou'    => 'Role usage',
+                    'whi'    => 'Which helpdesk personnel may use this role?',
+                    'udd'    => 'Use domain default',
+                    'all'    => 'All',
+                    'none'   => 'None',
+                    'status' => 'Determined based on institutional status',
+                    'inc'    => 'Include all, but exclude specific personnel',  
+                    'exc'    => 'Exclude all, but include specific personnel',
+                    'hel'    => 'Helpdesk',
+                    'rpr'    => 'Role privileges',
+                 );
+        $lt{'tfh'} = &mt("Custom [_1]ad hoc[_2] course roles available for use by the domain's helpdesk are as follows",'<i>','</i>');
+        my %domconfig = &Apache::lonnet::get_dom('configuration',['helpsettings'],$cdom);
+        my (%domcurrent,%ordered,%description,%domusage,$disabled);
+        if (ref($domconfig{'helpsettings'}) eq 'HASH') {
+            if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
+                %domcurrent = %{$domconfig{'helpsettings'}{'adhoc'}};
+            }
+        }
+        my $count = 0;
+        foreach my $role (sort(keys(%customroles))) {
+            my ($order,$desc,$access_in_dom);
+            if (ref($domcurrent{$role}) eq 'HASH') {
+                $order = $domcurrent{$role}{'order'};
+                $desc = $domcurrent{$role}{'desc'};
+                $access_in_dom = $domcurrent{$role}{'access'};
+            }
+            if ($order eq '') {
+                $order = $count;
+            }
+            $ordered{$order} = $role;
+            if ($desc ne '') {
+                $description{$role} = $desc;
+            } else {
+                $description{$role}= $role;
+            }
+            $count++;
+        }
+        %domusage = &domain_adhoc_access(\%customroles,\%domcurrent,\@accesstypes,$usertypes,$othertitle);
+        my @roles_by_num = ();
+        foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
+            push(@roles_by_num,$ordered{$item});
+        } 
+        $r->print('<p>'.$lt{'tfh'}.': <b>'.join('</b>, <b>',map { $description{$_}; } @roles_by_num).'</b>.');
+        if ($permission->{'owner'}) {
+            $r->print('<br />'.$lt{'aco'}.'</p><p>');
+            $r->print('<input type="hidden" name="state" value="process" />'.
+                      '<input type="submit" value="'.&mt('Save changes').'" />');
+        } else {
+            if ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'}) {
+                my ($ownername,$ownerdom) = split(/:/,$env{'course.'.$env{'request.course.id'}.'.internal.courseowner'});
+                $r->print('<br />'.&mt('The course owner -- [_1] -- can override the default access and/or privileges for these ad hoc roles.',
+                                    &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($ownername,$ownerdom),$ownername,$ownerdom)));
+            }
+            $disabled = ' disabled="disabled"';
+        }
+        $r->print('</p>');
+
+        $r->print('<div id="LC_minitab_header"><ul>');
+        my $count = 0;
+        my %visibility;
+        foreach my $role (@roles_by_num) {
+            my $id;
+            if ($count == 0) {
+                $id=' id="LC_current_minitab"';
+                $visibility{$role} = ' style="display:block"';    
+            } else {
+                $visibility{$role} = ' style="display:none"';
+            }
+            $count ++;
+            $r->print('<li'.$id.'><a href="#" onclick="javascript:switchRoleTab(this.parentNode,'."'$role'".');">'.$description{$role}.'</a></li>');
+        }
+        $r->print('</ul></div>');
+
+        foreach my $role (@roles_by_num) {
+            my %usecheck = (
+                             all => ' checked="checked"',
+                           );
+            my %displaydiv = (
+                                status => 'none',
+                                inc    => 'none',
+                                exc    => 'none',
+                                priv   => 'block',
+                             );
+            my (%selected,$overridden,$incrscheck,$indomcheck,$indomvis,$incrsvis);
+            if (ref($settings{$role}) eq 'HASH') { 
+                if ($settings{$role}{'access'} ne '') {
+                    $indomvis = ' style="display:none"';
+                    $incrsvis = ' style="display:block"';
+                    $incrscheck = ' checked="checked"'; 
+                    if ($settings{$role}{'access'} ne 'all') {
+                        $usecheck{$settings{$role}{'access'}} = $usecheck{'all'};
+                        delete($usecheck{'all'});
+                        if ($settings{$role}{'access'} eq 'status') {
+                            my $access = 'status';
+                            $displaydiv{$access} = 'inline';
+                            if (ref($settings{$role}{$access}) eq 'ARRAY') {
+                                $selected{$access} = $settings{$role}{$access};
+                            }
+                        } elsif ($settings{$role}{'access'} =~ /^(inc|exc)$/) {
+                            my $access = $1;
+                            $displaydiv{$access} = 'inline';
+                            if (ref($settings{$role}{$access}) eq 'ARRAY') {
+                                $selected{$access} = $settings{$role}{$access};
+                            }
+                        } elsif ($settings{$role}{'access'} eq 'none') {
+                            $displaydiv{'priv'} = 'none';
+                        }
+                    }
+                } else {
+                    $indomcheck = ' checked="checked"';
+                    $indomvis = ' style="display:block"';
+                    $incrsvis = ' style="display:none"';
+                }
+            } else {
+                $indomcheck = ' checked="checked"';
+                $indomvis = ' style="display:block"'; 
+                $incrsvis = ' style="display:none"';
+            }
+            $r->print('<div class="LC_left_float" id="'.$role.'_maindiv"'.$visibility{$role}.'>'.
+                      '<fieldset><legend>'.$lt{'rou'}.'</legend>'.
+                      '<p>'.$lt{'whi'}.' <span class="LC_nobreak">'.
+                      '<label><input type="radio" name="'.$role.'_incrs" value="1"'.$incrscheck.' onclick="toggleAccess('."'$role'".');"'.$disabled.'>'.
+                      &mt('Set here in [_1]',lc($crstype)).'</label>'.
+                      '<span>'.(' 'x2).
+                      '<label><input type="radio" name="'.$role.'_incrs" value="0"'.$indomcheck.' onclick="toggleAccess('."'$role'".');"'.$disabled.'>'.
+                      $lt{'udd'}.'</label><span></p>'.
+                      '<div id="'.$role.'_setindom"'.$indomvis.'>'.
+                      '<span class="LC_cusr_emph">'.$domusage{$role}.'</span></div>'.
+                      '<div id="'.$role.'_setincrs"'.$incrsvis.'>');
+            foreach my $access (@accesstypes) {
+                $r->print('<p><label><input type="radio" name="'.$role.'_access" value="'.$access.'" '.$usecheck{$access}.
+                          ' onclick="helpdeskAccess('."'$role'".');"'.$disabled.' />'.$lt{$access}.'</label>');
+                if ($access eq 'status') {
+                    $r->print('<div id="'.$role.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
+                              &Apache::lonuserutils::adhoc_status_types($cdom,undef,$role,$selected{$access},
+                                                                        $othertitle,$usertypes,$types,$disabled).
+                              '</div>');
+                } elsif (($access eq 'inc') && (keys(%domhelpdesk) > 0)) {
+                    $r->print('<div id="'.$role.'_notinc" style="display:'.$displaydiv{$access}.'">'.
+                              &Apache::lonuserutils::adhoc_staff($access,undef,$role,$selected{$access},
+                                                                 \%domhelpdesk,$disabled).
+                              '</div>');
+                } elsif (($access eq 'exc') && (keys(%domhelpdesk) > 0)) {
+                    $r->print('<div id="'.$role.'_notexc" style="display:'.$displaydiv{$access}.'">'.
+                              &Apache::lonuserutils::adhoc_staff($access,undef,$role,$selected{$access},
+                                                                 \%domhelpdesk,$disabled).
+                              '</div>');
+                }
+                $r->print('</p>');
+            }
+            $r->print('</div></fieldset>');
+            my %full=();
+            my %levels= (
+                         course => {},
+                         domain => {},
+                         system => {},
+                        );
+            my %levelscurrent=(
+                               course => {},
+                               domain => {},
+                               system => {},
+                              );
+            &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
+            $r->print('<fieldset id="'.$role.'_privs" style="display:'.$displaydiv{'priv'}.'">'.
+                      '<legend>'.$lt{'rpr'}.'</legend>'.
+                      &role_priv_table($role,$permission,$crstype,\%full,\%levels,\%levelscurrent,$overridden{$role}).
+                      '</fieldset></div><div style="padding:0;clear:both;margin:0;border:0"></div>');
+        }
+    } else {
+        $r->print(&mt('Helpdesk roles have not yet been created in this domain.'));
+    }
+    # Form Footer
+    $r->print('<input type="hidden" name="action" value="helpdesk" />'
+             .'</form>');
+    return;
+}
+
+sub domain_adhoc_access {
+    my ($roles,$domcurrent,$accesstypes,$usertypes,$othertitle) = @_;
+    my %domusage;
+    return unless ((ref($roles) eq 'HASH') && (ref($domcurrent) eq 'HASH') && (ref($accesstypes) eq 'ARRAY'));
+    foreach my $role (keys(%{$roles})) {
+        if (ref($domcurrent->{$role}) eq 'HASH') {
+            my $access = $domcurrent->{$role}{'access'};
+            if (($access eq '') || (!grep(/^\Q$access\E$/,@{$accesstypes}))) {
+                $access = 'all';
+                $domusage{$role} = &mt('Any user in domain with active [_1] role',&Apache::lonnet::plaintext('dh'));
+            } elsif ($access eq 'status') {
+                if (ref($domcurrent->{$role}{$access}) eq 'ARRAY') {
+                    my @shown;
+                    foreach my $type (@{$domcurrent->{$role}{$access}}) {
+                        unless ($type eq 'default') {
+                            if ($usertypes->{$type}) {
+                                push(@shown,$usertypes->{$type});
+                            }
+                        }
+                    }
+                    if (grep(/^default$/,@{$domcurrent->{$role}{$access}})) {
+                        push(@shown,$othertitle);
+                    }
+                    if (@shown) {
+                        my $shownstatus = join(' '.&mt('or').' ', at shown);
+                        $domusage{$role} = &mt('Any user in domain with active [_1] role, and institutional status: [_2]',
+                                               &Apache::lonnet::plaintext('dh'),$shownstatus);
+                    } else {
+                        $domusage{$role} = &mt('No one in the domain');
+                    }
+                }
+            } elsif ($access eq 'inc') {
+                my @dominc = ();
+                if (ref($domcurrent->{$role}{'inc'}) eq 'ARRAY') {
+                    foreach my $user (@{$domcurrent->{$role}{'inc'}}) {
+                        my ($uname,$udom) = split(/:/,$user);
+                        push(@dominc,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom));
+                    }
+                    my $showninc = join(', ', at dominc);
+                    if ($showninc ne '') {
+                        $domusage{$role} = &mt('Include any user in domain with active [_1] role, except: [_2]',
+                                               &Apache::lonnet::plaintext('dh'),$showninc);
+                    } else {
+                        $domusage{$role} = &mt('Any user in domain with active [_1] role',&Apache::lonnet::plaintext('dh'));
+                    }
+                }
+            } elsif ($access eq 'exc') {
+                my @domexc = ();
+                if (ref($domcurrent->{$role}{'exc'}) eq 'ARRAY') {
+                    foreach my $user (@{$domcurrent->{$role}{'exc'}}) {
+                        my ($uname,$udom) = split(/:/,$user);
+                        push(@domexc,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom));
+                    }
+                }
+                my $shownexc = join(', ', at domexc);
+                if ($shownexc ne '') {
+                    $domusage{$role} = &mt('Only the following in the domain with active [_1] role: [_2]',
+                                           &Apache::lonnet::plaintext('dh'),$shownexc);
+                } else {
+                    $domusage{$role} = &mt('No one in the domain');
+                }
+            } elsif ($access eq 'none') {
+                $domusage{$role} = &mt('No one in the domain');
+            } elsif ($access eq 'all') {
+                $domusage{$role} = &mt('Any user in domain with active [_1] role',&Apache::lonnet::plaintext('dh'));
+            }
+        } else {
+            $domusage{$role} = &mt('Any user in domain with active [_1] role',&Apache::lonnet::plaintext('dh'));
+        }
+    }
+    return %domusage;
+}
+
+sub get_domain_customroles {
+    my ($cdom,$confname) = @_;
+    my %existing=&Apache::lonnet::dump('roles',$cdom,$confname,'rolesdef_');
+    my %customroles;
+    foreach my $key (keys(%existing)) {
+        if ($key=~/^rolesdef\_(\w+)$/) {
+            my $rolename = $1;
+            my %privs;
+            ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
+            $customroles{$rolename} = \%privs;
+        }
+    }
+    return %customroles;
+}
+
+sub role_priv_table {
+    my ($role,$permission,$crstype,$full,$levels,$levelscurrent,$overridden) = @_;
+    return unless ((ref($full) eq 'HASH') && (ref($levels) eq 'HASH') &&
+                   (ref($levelscurrent) eq 'HASH'));
+    my %lt=&Apache::lonlocal::texthash (
+                    'crl'  => 'Course Level Privilege',
+                    'def'  => 'Domain Defaults',
+                    'ove'  => 'Override in Course',
+                    'ine'  => 'In effect',
+                    'dis'  => 'Disabled',
+                    'ena'  => 'Enabled',
+                   );
+    if ($crstype eq 'Community') {
+        $lt{'ove'} = 'Override in Community',
+    }
+    my @status = ('Disabled','Enabled');
+    my (%on,%off);
+    if (ref($overridden) eq 'HASH') {
+        if (ref($overridden->{'on'}) eq 'ARRAY') {
+            map { $on{$_} = 1; } (@{$overridden->{'on'}});
+        }
+        if (ref($overridden->{'off'}) eq 'ARRAY') {
+            map { $off{$_} = 1; } (@{$overridden->{'off'}});
+        }
+    }
+    my $output=&Apache::loncommon::start_data_table().
+               &Apache::loncommon::start_data_table_header_row().
+               '<th>'.$lt{'crl'}.'</th><th>'.$lt{'def'}.'</th><th>'.$lt{'ove'}.
+               '</th><th>'.$lt{'ine'}.'</th>'.
+               &Apache::loncommon::end_data_table_header_row();
+    foreach my $priv (sort(keys(%{$full}))) {
+        next unless ($levels->{'course'}{$priv});
+        my $privtext = &Apache::lonnet::plaintext($priv,$crstype);
+        my ($default,$ineffect);
+        if ($levelscurrent->{'course'}{$priv}) {
+            $default = '<img src="/adm/lonIcons/navmap.correct.gif" alt="'.$lt{'ena'}.'" />';
+            $ineffect = $default;
+        }
+        my ($customstatus,$checked);
+        $output .= &Apache::loncommon::start_data_table_row().
+                   '<td>'.$privtext.'</td>'.
+                   '<td>'.$default.'</td><td>';
+        if (($levelscurrent->{'course'}{$priv}) && ($off{$priv})) {
+            if ($permission->{'owner'}) {
+                $checked = ' checked="checked"';
+            }
+            $customstatus = '<img src="/adm/lonIcons/navmap.wrong.gif" alt="'.$lt{'dis'}.'" />';
+            $ineffect = $customstatus; 
+        } elsif ((!$levelscurrent->{'course'}{$priv}) && ($on{$priv})) {
+            if ($permission->{'owner'}) {
+                $checked = ' checked="checked"'; 
+            }
+            $customstatus = '<img src="/adm/lonIcons/navmap.correct.gif" alt="'.$lt{'ena'}.'" />';
+            $ineffect = $customstatus; 
+        }
+        if ($permission->{'owner'}) {
+            $output .= '<input type="checkbox" name="'.$role.'_override" value="'.$priv.'"'.$checked.' />';
+        } else {
+            $output .= $customstatus;
+        }
+        $output .= '</td><td>'.$ineffect.'</td>'.
+                   &Apache::loncommon::end_data_table_row();
+    }
+    $output .= &Apache::loncommon::end_data_table();
+    return $output;
+}
+
+sub get_adhocrole_settings {
+    my ($cid,$accesstypes,$types,$customroles,$settings,$overridden) = @_;  
+    return unless ((ref($accesstypes) eq 'ARRAY') && (ref($customroles) eq 'HASH') &&
+                   (ref($settings) eq 'HASH') && (ref($overridden) eq 'HASH'));
+    foreach my $role (split(/,/,$env{'course.'.$cid.'.internal.adhocaccess'})) {
+        my ($curraccess,$rest) = split(/=/,$env{'course.'.$cid.'.internal.adhoc.'.$role});
+        if (($curraccess ne '') && (grep(/^\Q$curraccess\E$/,@{$accesstypes}))) {
+            $settings->{$role}{'access'} = $curraccess;
+            if (($curraccess eq 'status') && (ref($types) eq 'ARRAY')) {
+                my @status = split(/,/,$rest);
+                my @currstatus;
+                foreach my $type (@status) {
+                    if ($type eq 'default') {
+                        push(@currstatus,$type);
+                    } elsif (grep(/^\Q$type\E$/,@{$types})) {
+                        push(@currstatus,$type);
+                    }
+                }
+                if (@currstatus) {
+                    $settings->{$role}{$curraccess} = \@currstatus;
+                } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
+                    my @personnel = split(/,/,$rest);
+                    $settings->{$role}{$curraccess} = \@personnel;
+                }
+            }
+        }
+    }
+    foreach my $role (keys(%{$customroles})) {
+        if ($env{'course.'.$cid.'.internal.adhocpriv.'.$role}) {
+            my %currentprivs;
+            if (ref($customroles->{$role}) eq 'HASH') {
+                if (exists($customroles->{$role}{'course'})) {
+                    my %full=();
+                    my %levels= (
+                                  course => {},
+                                  domain => {},
+                                  system => {},
+                                );
+                    my %levelscurrent=(
+                                        course => {},
+                                        domain => {},
+                                        system => {},
+                                      );
+                    &Apache::lonuserutils::custom_role_privs($customroles->{$role},\%full,\%levels,\%levelscurrent);
+                    %currentprivs = %{$levelscurrent{'course'}};
+                }
+            }
+            foreach my $item (split(/,/,$env{'course.'.$cid.'.internal.adhocpriv.'.$role})) {
+                next if ($item eq '');
+                my ($rule,$rest) = split(/=/,$item);
+                next unless (($rule eq 'off') || ($rule eq 'on'));
+                foreach my $priv (split(/:/,$rest)) {
+                    if ($priv ne '') {
+                        if ($rule eq 'off') {
+                            push(@{$overridden->{$role}{'off'}},$priv);
+                            if ($currentprivs{$priv}) {
+                                push(@{$settings->{$role}{'off'}},$priv);
+                            }
+                        } else {
+                            push(@{$overridden->{$role}{'on'}},$priv);
+                            unless ($currentprivs{$priv}) {
+                                push(@{$settings->{$role}{'on'}},$priv);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+    return;
+}
+
+sub update_helpdeskaccess {
+    my ($r,$permission,$brcrum) = @_;
+    my $helpitem = 'Course_Helpdesk_Access';
+    push (@{$brcrum},
+             {href => '/adm/createuser?action=helpdesk',
+              text => 'Helpdesk Access',
+              help => $helpitem},
+             {href => '/adm/createuser?action=helpdesk',
+              text => 'Result',
+              help => $helpitem}
+         );
+    my $bread_crumbs_component = 'Helpdesk Staff Access';
+    my $args = { bread_crumbs           => $brcrum,
+                 bread_crumbs_component => $bread_crumbs_component};
+
+    # print page header
+    $r->print(&header('',$args));
+    unless ((ref($permission) eq 'HASH') && ($permission->{'owner'})) {
+        $r->print('<p class="LC_error">'.&mt('You do not have permission to change helpdesk access.').'</p>');
+        return;
+    }
+    my @accesstypes = ('all','none','status','inc','exc');
+    my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+    my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+    my $confname = $cdom.'-domainconfig';
+    my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($cdom);
+    my $crstype = &Apache::loncommon::course_type();
+    my %customroles = &get_domain_customroles($cdom,$confname);
+    my (%settings,%overridden);
+    &get_adhocrole_settings($env{'request.course.id'},\@accesstypes,
+                            $types,\%customroles,\%settings,\%overridden);
+    my %domhelpdesk = &Apache::lonnet::get_active_domroles($cdom,['dh']);
+    my (%changed,%storehash, at todelete);
+
+    if (keys(%customroles)) {
+        my (%newsettings, at incrs);
+        foreach my $role (keys(%customroles)) {
+            $newsettings{$role} = {
+                                    access => '',
+                                    status => '',
+                                    exc    => '',
+                                    inc    => '',
+                                    on     => '',
+                                    off    => '',
+                                  };
+            my %current;
+            if (ref($settings{$role}) eq 'HASH') {
+                %current = %{$settings{$role}};
+            }
+            if (ref($overridden{$role}) eq 'HASH') {
+                $current{'overridden'} = $overridden{$role};
+            }
+            if ($env{'form.'.$role.'_incrs'}) {
+                my $access = $env{'form.'.$role.'_access'};
+                if (grep(/^\Q$access\E$/, at accesstypes)) {
+                    push(@incrs,$role);
+                    unless ($current{'access'} eq $access) {
+                        $changed{$role}{'access'} = 1;
+                        $storehash{'internal.adhoc.'.$role} = $access; 
+                    }
+                    if ($access eq 'status') {
+                        my @statuses = &Apache::loncommon::get_env_multiple('form.'.$role.'_status');
+                        my @stored;
+                        my @shownstatus;
+                        if (ref($types) eq 'ARRAY') {
+                            foreach my $type (sort(@statuses)) {
+                                if ($type eq 'default') {
+                                    push(@stored,$type);
+                                } elsif (grep(/^\Q$type\E$/,@{$types})) {
+                                    push(@stored,$type);
+                                    push(@shownstatus,$usertypes->{$type});
+                                }
+                            }
+                            if (grep(/^default$/, at statuses)) {
+                                push(@shownstatus,$othertitle);
+                            }
+                            $storehash{'internal.adhoc.'.$role} .= '='.join(',', at stored);
+                        }
+                        $newsettings{$role}{'status'} = join(' '.&mt('or').' ', at shownstatus);
+                        if (ref($current{'status'}) eq 'ARRAY') {
+                            my @diffs = &Apache::loncommon::compare_arrays(\@stored,$current{'status'});
+                            if (@diffs) {
+                                $changed{$role}{'status'} = 1;
+                            }
+                        } elsif (@stored) {
+                            $changed{$role}{'status'} = 1;
+                        }
+                    } elsif (($access eq 'inc') || ($access eq 'exc')) {
+                        my @personnel = &Apache::loncommon::get_env_multiple('form.'.$role.'_staff_'.$access);
+                        my @newspecstaff;
+                        my @stored;
+                        my @currstaff;
+                        foreach my $person (sort(@personnel)) {
+                            if ($domhelpdesk{$person}) {
+                                push(@stored,$person); 
+                            }
+                        }
+                        if (ref($current{$access}) eq 'ARRAY') {
+                            my @diffs = &Apache::loncommon::compare_arrays(\@stored,$current{$access});
+                            if (@diffs) {
+                                $changed{$role}{$access} = 1;
+                            }
+                        } elsif (@stored) {
+                            $changed{$role}{$access} = 1;
+                        }
+                        $storehash{'internal.adhoc.'.$role} .= '='.join(',', at stored);
+                        foreach my $person (@stored) {
+                            my ($uname,$udom) = split(/:/,$person);
+                            push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
+                        }
+                        $newsettings{$role}{$access} = join(', ',sort(@newspecstaff));
+                    }
+                    $newsettings{$role}{'access'} = $access;
+                }
+            } else {
+                if (($current{'access'} ne '') && (grep(/^\Q$current{'access'}\E$/, at accesstypes))) {
+                    $changed{$role}{'access'} = 1;
+                    $newsettings{$role} = {};
+                    push(@todelete,'internal.adhoc.'.$role);
+                }
+            }
+            if (($env{'form.'.$role.'_incrs'}) && ($env{'form.'.$role.'_access'} eq 'none')) {
+                if (ref($current{'overridden'}) eq 'HASH') {
+                    push(@todelete,'internal.adhocpriv.'.$role);
+                }
+            } else {
+                my %full=();
+                my %levels= (
+                             course => {},
+                             domain => {},
+                             system => {},
+                            );
+                my %levelscurrent=(
+                                   course => {},
+                                   domain => {},
+                                   system => {},
+                                  );
+                &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
+                my (@updatedon, at updatedoff, at override);
+                @override = &Apache::loncommon::get_env_multiple('form.'.$role.'_override');
+                if (@override) { 
+                    foreach my $priv (sort(keys(%full))) {
+                        next unless ($levels{'course'}{$priv});
+                        if (grep(/^\Q$priv\E$/, at override)) {
+                            if ($levelscurrent{'course'}{$priv}) {
+                                push(@updatedoff,$priv);
+                            } else {
+                                push(@updatedon,$priv);
+                            }
+                        }
+                    }
+                }
+                if (@updatedon) {
+                    $newsettings{$role}{'on'} = join('</li><li>', map { &Apache::lonnet::plaintext($_,$crstype) } (@updatedon)); 
+                }
+                if (@updatedoff) {
+                    $newsettings{$role}{'off'} = join('</li><li>', map { &Apache::lonnet::plaintext($_,$crstype) } (@updatedoff));
+                }
+                if (ref($current{'overridden'}) eq 'HASH') {
+                    if (ref($current{'overridden'}{'on'}) eq 'ARRAY') {
+                        if (@updatedon) {
+                            my @diffs = &Apache::loncommon::compare_arrays(\@updatedon,$current{'overridden'}{'on'});
+                            if (@diffs) {
+                                $changed{$role}{'on'} = 1;
+                            }
+                        } else {
+                            $changed{$role}{'on'} = 1;
+                        }
+                    } elsif (@updatedon) {
+                        $changed{$role}{'on'} = 1;
+                    }
+                    if (ref($current{'overridden'}{'off'}) eq 'ARRAY') {
+                        if (@updatedoff) {
+                            my @diffs = &Apache::loncommon::compare_arrays(\@updatedoff,$current{'overridden'}{'off'});
+                            if (@diffs) {
+                                $changed{$role}{'off'} = 1;
+                            }
+                        } else {
+                            $changed{$role}{'off'} = 1;
+                        }
+                    } elsif (@updatedoff) {
+                        $changed{$role}{'off'} = 1;
+                    }
+                } else {
+                    if (@updatedon) {
+                        $changed{$role}{'on'} = 1;
+                    }
+                    if (@updatedoff) {
+                        $changed{$role}{'off'} = 1;
+                    }
+                }
+                if (ref($changed{$role}) eq 'HASH') {
+                    if (($changed{$role}{'on'} || $changed{$role}{'off'})) {
+                        my $newpriv;
+                        if (@updatedon) {
+                            $newpriv = 'on='.join(':', at updatedon);
+                        }
+                        if (@updatedoff) {
+                            $newpriv .= ($newpriv ? ',' : '' ).'off='.join(':', at updatedoff);
+                        }
+                        if ($newpriv eq '') {
+                            push(@todelete,'internal.adhocpriv.'.$role);
+                        } else {
+                            $storehash{'internal.adhocpriv.'.$role} = $newpriv;
+                        }
+                    }
+                }
+            }
+        }
+        if (@incrs) {
+            $storehash{'internal.adhocaccess'} = join(',', at incrs);
+        } elsif (@todelete) {
+            push(@todelete,'internal.adhocaccess');
+        }
+        if (keys(%changed)) {
+            my ($putres,$delres);
+            if (keys(%storehash)) {
+                $putres = &Apache::lonnet::put('environment',\%storehash,$cdom,$cnum);
+                my %newenvhash;
+                foreach my $key (keys(%storehash)) {
+                    $newenvhash{'course.'.$env{'request.course.id'}.'.'.$key} = $storehash{$key};
+                }
+                &Apache::lonnet::appenv(\%newenvhash);
+            }
+            if (@todelete) {
+                $delres = &Apache::lonnet::del('environment',\@todelete,$cdom,$cnum);
+                foreach my $key (@todelete) {
+                    &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.'.$key);
+                }
+            }
+            if (($putres eq 'ok') || ($delres eq 'ok')) {
+                my %domconfig = &Apache::lonnet::get_dom('configuration',['helpsettings'],$cdom);
+                my (%domcurrent,%ordered,%description,%domusage);
+                if (ref($domconfig{'helpsettings'}) eq 'HASH') {
+                    if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
+                        %domcurrent = %{$domconfig{'helpsettings'}{'adhoc'}};
+                    }
+                }
+                my $count = 0;
+                foreach my $role (sort(keys(%customroles))) {
+                    my ($order,$desc);
+                    if (ref($domcurrent{$role}) eq 'HASH') {
+                        $order = $domcurrent{$role}{'order'};
+                        $desc = $domcurrent{$role}{'desc'};
+                    }
+                    if ($order eq '') {
+                        $order = $count;
+                    }
+                    $ordered{$order} = $role;
+                    if ($desc ne '') {
+                        $description{$role} = $desc;
+                    } else {
+                        $description{$role}= $role;
+                    }
+                    $count++;
+                }
+                my @roles_by_num = ();
+                foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
+                    push(@roles_by_num,$ordered{$item});
+                }
+                %domusage = &domain_adhoc_access(\%changed,\%domcurrent,\@accesstypes,$usertypes,$othertitle);
+                $r->print(&mt('Helpdesk access settings have been changed as follows').'<br />'); 
+                $r->print('<ul>');
+                foreach my $role (@roles_by_num) {
+                    next unless (ref($changed{$role}) eq 'HASH');
+                    $r->print('<li>'.&mt('Ad hoc role').': <b>'.$description{$role}.'</b>'.
+                              '<ul>');
+                    if ($changed{$role}{'access'} || $changed{$role}{'status'} || $changed{$role}{'inc'} || $changed{$role}{'exc'}) { 
+                        $r->print('<li>');
+                        if ($env{'form.'.$role.'_incrs'}) {
+                            if ($newsettings{$role}{'access'} eq 'all') {
+                                $r->print(&mt('All helpdesk staff can access '.lc($crstype).' with this role.'));
+                            } elsif ($newsettings{$role}{'access'} eq 'none') {
+                                $r->print(&mt('No helpdesk staff can access '.lc($crstype).' with this role.'));
+                            } elsif ($newsettings{$role}{'access'} eq 'status') {
+                                if ($newsettings{$role}{'status'}) {
+                                    my ($access,$rest) = split(/=/,$storehash{'internal.adhoc.'.$role});
+                                    if (split(/,/,$rest) > 1) {  
+                                        $r->print(&mt('Helpdesk staff can use this role if their institutional type is one of: [_1].',
+                                                      $newsettings{$role}{'status'}));
+                                    } else {
+                                        $r->print(&mt('Helpdesk staff can use this role if their institutional type is: [_1].',
+                                                      $newsettings{$role}{'status'}));
+                                    }
+                                } else {
+                                    $r->print(&mt('No helpdesk staff can access '.lc($crstype).' with this role.'));
+                                }
+                            } elsif ($newsettings{$role}{'access'} eq 'exc') {
+                                if ($newsettings{$role}{'exc'}) {
+                                    $r->print(&mt('Helpdesk staff who can use this role are as follows:').' '.$newsettings{$role}{'exc'}.'.');
+                                } else {
+                                    $r->print(&mt('No helpdesk staff can access '.lc($crstype).' with this role.'));
+                                }
+                            } elsif ($newsettings{$role}{'access'} eq 'inc') {
+                                if ($newsettings{$role}{'inc'}) {
+                                    $r->print(&mt('All helpdesk staff may use this role except the following:').' '.$newsettings{$role}{'inc'}.'.');
+                                } else {
+                                    $r->print(&mt('All helpdesk staff may use this role.'));
+                                }
+                            }
+                        } else {
+                            $r->print(&mt('Default access set in the domain now applies.').'<br />'.
+                                      '<span class="LC_cusr_emph">'.$domusage{$role}.'</span>');
+                        }
+                        $r->print('</li>');
+                    }
+                    unless ($newsettings{$role}{'access'} eq 'none') {
+                        if ($changed{$role}{'off'}) {
+                            if ($newsettings{$role}{'off'}) {
+                                $r->print('<li>'.&mt('Privileges which are available by default for this ad hoc role, but are disabled for this specific '.lc($crstype).':').
+                                          '<ul><li>'.$newsettings{$role}{'off'}.'</li></ul></li>');
+                            } else {
+                                $r->print('<li>'.&mt('All privileges available by default for this ad hoc role are enabled.').'</li>'); 
+                            }
+                        }
+                        if ($changed{$role}{'on'}) {  
+                            if ($newsettings{$role}{'on'}) {
+                                $r->print('<li>'.&mt('Privileges which are not available by default for this ad hoc role, but are enabled for this specific '.lc($crstype).':').
+                                          '<ul><li>'.$newsettings{$role}{'on'}.'</li></ul></li>');
+                            } else {
+                                $r->print('<li>'.&mt('None of the privileges unavailable by default for this ad hoc role are enabled.').'</li>'); 
+                            }
+                        }
+                    }
+                    $r->print('</ul></li>');
+                }
+                $r->print('</ul>');
+            }
+        } else {
+            $r->print(&mt('No changes made to helpdesk access settings.')); 
+        }
+    }
+    return;
+}
+
 #-------------------------------------------------- functions for &phase_two
 sub user_search_result {
     my ($context,$srch) = @_;
Index: loncom/interface/lonmenu.pm
diff -u loncom/interface/lonmenu.pm:1.464 loncom/interface/lonmenu.pm:1.465
--- loncom/interface/lonmenu.pm:1.464	Thu Dec  1 16:37:53 2016
+++ loncom/interface/lonmenu.pm	Mon Jan  2 19:44:06 2017
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Routines to control the menu
 #
-# $Id: lonmenu.pm,v 1.464 2016/12/01 16:37:53 raeburn Exp $
+# $Id: lonmenu.pm,v 1.465 2017/01/02 19:44:06 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -2099,7 +2099,7 @@
     my ($cdom,$cnum,$httphost) = @_;
     my $crstype = &Apache::loncommon::course_type();
     my $now = time;
-    my (%courseroles,%seccount,%courseprivs);
+    my (%courseroles,%seccount,%courseprivs,%roledesc);
     my $is_cc;
     my ($js,$form,$switcher);
     my $ccrole;
@@ -2149,7 +2149,7 @@
     if ($is_cc) {
         &get_all_courseroles($cdom,$cnum,\%courseroles,\%seccount,\%courseprivs);
     } elsif ($env{'request.role'} =~ m{^\Qcr/$cdom/$cdom-domainconfig/\E(\w+)\.\Q/$cdom/$cnum\E}) {
-        &get_customadhoc_roles($cdom,$cnum,\%courseroles,\%seccount,\%courseprivs,$privref);
+        &get_customadhoc_roles($cdom,$cnum,\%courseroles,\%seccount,\%courseprivs,\%roledesc,$privref);
     } else {
         my %gotnosection;
         foreach my $item (keys(%env)) {
@@ -2203,7 +2203,7 @@
     }
     if ((keys(%seccount) > 1) || ($numdiffsec > 1)) {
         my @submenu;
-        $js = &jump_to_role($cdom,$cnum,\%seccount,\%courseroles,\%courseprivs,$privref);
+        $js = &jump_to_role($cdom,$cnum,\%seccount,\%courseroles,\%courseprivs,\%roledesc,$privref);
         $form = 
             '<form name="rolechooser" method="post" action="'.$httphost.'/adm/roles">'."\n".
             '  <input type="hidden" name="destinationurl" value="'.
@@ -2245,7 +2245,10 @@
                 if ($include) {
                     my $rolename;
                     if ($role =~ m{^cr/$cdom/$cdom\-domainconfig/(\w+)(?:/\w+|$)}) {
-                        $rolename = &mt('Helpdesk [_1]',$1);
+                        $rolename = $roledesc{$role};
+                        if ($rolename eq '') {
+                            $rolename = &mt('Helpdesk [_1]',$1);
+                        }
                     } else {
                         $rolename = &Apache::lonnet::plaintext($role);
                     }
@@ -2332,18 +2335,23 @@
 }
 
 sub get_customadhoc_roles {
-    my ($cdom,$cnum,$courseroles,$seccount,$courseprivs,$privref) = @_;
+    my ($cdom,$cnum,$courseroles,$seccount,$courseprivs,$roledesc,$privref) = @_;
     unless ((ref($courseroles) eq 'HASH') && (ref($seccount) eq 'HASH') &&
-            (ref($courseprivs) eq 'HASH')) {
+            (ref($courseprivs) eq 'HASH') && (ref($roledesc) eq 'HASH')) {
         return;
     }
-    if ($env{'environment.adhocroles.'.$cdom} ne '') { 
-        my @customroles = split(/,/,$env{'environment.adhocroles.'.$cdom});
-        if (@customroles > 1) {
-            if ($env{"user.role.dh./$cdom/"}) {
-                my ($start,$end)=split(/\./,$env{"user.role.dh./$cdom/"});
-                my $now = time; 
-                if (!($start && ($now<$start)) & !($end && ($now>$end))) {
+    if ($env{"user.role.dh./$cdom/"}) {
+        my ($start,$end)=split(/\./,$env{"user.role.dh./$cdom/"});
+        my $now = time;
+        if (!($start && ($now<$start)) && !($end && ($now>$end))) {
+            my ($possroles,$description) = &Apache::lonnet::get_my_adhocroles($cdom.'_'.$cnum);
+            my %available;
+            if (ref($possroles) eq 'ARRAY') {
+                map { $available{$_} = 1; } @{$possroles};
+            }
+            my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
+            if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
+                if (keys(%{$domdefaults{'adhocroles'}})) {
                     my $numsec = 1;
                     my @sections;
                     my ($allseclist,$cached) =
@@ -2355,16 +2363,17 @@
                         }
                     } else {
                         my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
-                        $numsec += scalar(keys(%sections_count));
-                        $allseclist = join(',',sort(keys(%sections_count)));
+                        @sections = sort(keys(%sections_count));
+                        $numsec += scalar(@sections);
+                        $allseclist = join(',', at sections);
                         &Apache::lonnet::do_cache_new('courseseclist',$cdom.'_'.$cnum,$allseclist);
                     }
                     my (%adhoc,$gotprivs);
                     my $prefix = "cr/$cdom/$cdom".'-domainconfig';
-                    foreach my $role (@customroles) {
+                    foreach my $role (keys(%{$domdefaults{'adhocroles'}})) {
                         next if (($role eq '') || ($role =~ /\W/));
                         $seccount->{"$prefix/$role"} = $numsec;
-                        $courseroles->{"$prefix/$role"} = \@sections;
+                        $roledesc->{"$prefix/$role"} = $description->{$role};
                         if ((ref($privref) eq 'ARRAY') && (@{$privref} > 0)) {
                             if (exists($env{"user.priv.$prefix/$role./$cdom/$cnum./"})) {
                                 $courseprivs->{"$prefix/$role./$cdom/$cnum./"} =
@@ -2385,7 +2394,9 @@
                                         foreach my $key (keys(%roledefs)) {
                                             (undef,my $rolename) = split(/_/,$key);
                                             if ($rolename ne '') {
-                                                $adhoc{$rolename} = $roledefs{$key};
+                                                my ($systempriv,$domainpriv,$coursepriv) = split(/\_/,$roledefs{$key});
+                                                $coursepriv = &Apache::lonnet::course_adhocrole_privs($rolename,$cdom,$cnum,$coursepriv);
+                                                $adhoc{$rolename} = join('_',($systempriv,$domainpriv,$coursepriv));
                                             }
                                         }
                                         &Apache::lonnet::do_cache_new('adhocroles',$cdom,\%adhoc);
@@ -2398,6 +2409,9 @@
                                      split(/\_/,$adhoc{$role});
                             }
                         }
+                        if ($available{$role}) {
+                            $courseroles->{"$prefix/$role"} = \@sections;
+                        }
                     }
                 }
             }
@@ -2407,7 +2421,7 @@
 }
 
 sub jump_to_role {
-    my ($cdom,$cnum,$seccount,$courseroles,$courseprivs,$privref) = @_;
+    my ($cdom,$cnum,$seccount,$courseroles,$courseprivs,$roledesc,$privref) = @_;
     my %lt = &Apache::lonlocal::texthash(
                 this => 'This role has section(s) associated with it.',
                 ente => 'Enter a specific section.',
Index: loncom/interface/lonpickcourse.pm
diff -u loncom/interface/lonpickcourse.pm:1.122 loncom/interface/lonpickcourse.pm:1.123
--- loncom/interface/lonpickcourse.pm:1.122	Wed Nov  2 22:41:18 2016
+++ loncom/interface/lonpickcourse.pm	Mon Jan  2 19:44:06 2017
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Pick a course
 #
-# $Id: lonpickcourse.pm,v 1.122 2016/11/02 22:41:18 raeburn Exp $
+# $Id: lonpickcourse.pm,v 1.123 2017/01/02 19:44:06 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -37,13 +37,32 @@
 use Apache::lonlocal;
 use Apache::longroup;
 use LONCAPA qw(:DEFAULT :match);
+use JSON::DWIW;
 
 sub handler {
     my $r = shift;
+    if ($env{'form.context'} eq 'adhoc') {
+        &Apache::loncommon::content_type($r,'application/json');
+        $r->send_http_header;
+        my ($possroles,$description) = &Apache::lonnet::get_my_adhocroles($env{'form.cid'});
+        if ((ref($possroles) eq 'ARRAY') && (ref($description) eq 'HASH')) {
+            my $response = [];
+            if (@{$possroles}) {
+                foreach my $role (@{$possroles}) {
+                    push(@{$response},
+                                      { name => $role,
+                                        desc => $description->{$role},
+                                      });
+                }
+            }
+            $r->print(JSON::DWIW->to_json({roles => $response}));
+        }
+        return OK;
+    }
     &Apache::loncommon::content_type($r,'text/html');
     $r->send_http_header;
     return OK if $r->header_only;
-
+    
 # ------------------------------------------------------------ Print the screen
 
     # Get parameters from query string
@@ -53,7 +72,8 @@
                                'multiple','type','setroles','fixeddom','cloner',
                                'crscode','crsdom']);
     my ($type,$title,$jscript,$multelement,$multiple,$roleelement,$typeelement,
-        $lastaction,$autosubmit,$submitopener,$cloneruname,$clonerudom,$crscode,$crsdom);
+        $lastaction,$autosubmit,$submitopener,$cloneruname,$clonerudom,$crscode,
+        $crsdom,$rolechooser);
 
     # Get course type - Course, Community or Placement.
     $type = $env{'form.type'};
@@ -75,6 +95,25 @@
         $roleelement = '<input type="hidden" name="roleelement" value="'.$env{'form.roleelement'}.'" />';
         $submitopener = &processpick();
         $autosubmit = 'process_pick("'.$roledom.'","'.$rolename.'")';
+        if ($rolename eq 'dh') {
+            my %lt = &Apache::lonlocal::texthash(
+                     title    => 'Ad hoc role selection',
+                     preamble => 'Please choose an ad hoc role in the course.',
+                     cancel   => 'Click "OK" to enter the course, or "Cancel" to choose a different course.',
+            );
+
+            $rolechooser = <<"END";
+<div id="LC_adhocrole_chooser" title="$lt{'title'}">
+  <p>$lt{'preamble'}</p>
+  <form name="LChelpdeskadhoc" id="LChelpdeskpicker" action="">
+    <div id="LC_choose_adhoc">
+    </div>
+    <input type="submit" tabindex="-1" style="position:absolute; top:-1000px" />
+  </form>
+  <p>$lt{'cancel'}</p>
+</div>
+END
+        }
     }
     if ($env{'form.typeelement'} ne '') {
         $typeelement = '<input type="hidden" name="typeelement" value="'.$env{'form.typeelement'}.'" />';
@@ -149,7 +188,8 @@
     # print javascript functions for choosing a course 
     if ((($env{'form.gosearch'}) && ($env{'form.updater'} eq '')) || 
         $onlyown) {
-        $r->print(&gochoose_javascript($type,$multiple,$autosubmit,$lastaction));
+        $r->print(&gochoose_javascript($type,$multiple,$autosubmit,$lastaction,
+                                       $rolename,$rolechooser));
     }
     $r->print(&Apache::lonhtmlcommon::scripttag($jscript));
     $r->print($submitopener);
@@ -803,7 +843,7 @@
 }
 
 sub gochoose_javascript {
-    my ($type,$multiple,$autosubmit,$lastaction) = @_;
+    my ($type,$multiple,$autosubmit,$lastaction,$rolename,$rolechooser) = @_;
     my %elements = (
                      'Course' => {
                                  name  => 'coursepick',
@@ -821,9 +861,24 @@
                                  list  => 'courselist',
                                  },
                     );
-    my $output = qq|
+
+    my %lt = &Apache::lonlocal::texthash (
+        none => 'You are not eligible to use an ad hoc role for the selected course',
+        ok => 'OK',
+        exit => 'Cancel',       
+    );
+    &js_escape(\%lt);
+
+    my $output;
+    if ($rolechooser) {
+        $output .= qq|
+\$( "#LC_adhocrole_chooser" ).dialog({ autoOpen: false });
+|;
+    }
+    $output .= qq|
 function gochoose(cname,cdom,cdesc) {
     var openerForm = "$env{'form.form'}";
+    var openerRole = "$rolename";
     courseCount = 0;
     var courses = '';
 |;
@@ -899,16 +954,210 @@
         }
 |;
     }
-    $output .= qq|
+    $output .= <<ENDJS;
     if (openerForm == 'portform') {
         document.courselist.cnum.value = cname;
         document.courselist.cdom.value = cdom;
     }
-    $autosubmit
-    $lastaction
+    if ((openerForm == 'rolechoice') && (openerRole == 'dh')) {
+\$("#LC_choose_adhoc").empty();
+var http = new XMLHttpRequest();
+var url = "/adm/pickcourse";
+var params = "cid="+cdom+"_"+cname+"&context=adhoc";
+http.open("POST", url, true);
+http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+http.onreadystatechange = function() {
+    if(http.readyState == 4 && http.status == 200) {
+        var data = \$.parseJSON(http.responseText);
+        var len = data.roles.length;
+        if (len == '' || len == null || len == 0) {
+            alert('$lt{none}');  
+        } else {
+            if (len == 1) {
+                process_pick(cdom,data.roles[0].name);
+                $lastaction;
+            } else {
+                var str = '';
+                for (var i=0; i<data.roles.length; i++) { 
+                    \$("<label><input type='radio' value='"+data.roles[i].name+"' name='LC_get_role' id='LC_get_role_"+i+"' />"+data.roles[i].desc+"</label><span>  </span>")
+                    .appendTo("#LC_choose_adhoc");
+                }
+                \$( "#LC_get_role_0").prop("checked", true);
+                \$( "#LC_adhocrole_chooser" ).dialog({ autoOpen: false });
+                \$( "#LC_adhocrole_chooser" ).dialog("open");
+                \$( "#LC_adhocrole_chooser" ).dialog({
+                 height: 400,
+                 width: 500,
+                 modal: true,
+                 resizable: false,
+                 buttons: [
+                       {
+                         text: "$lt{'ok'}",
+                         click: function() {
+                                     var rolename = \$('input[name=LC_get_role]:checked', '#LChelpdeskpicker').val();
+                                     process_pick(cdom,rolename);
+                                     \$("#LC_adhocrole_chooser").dialog( "close" );
+                                     $lastaction;
+                                } 
+                       },
+                       {
+                         text: "$lt{'exit'}",
+                         click: function() {
+                                     \$("#LC_adhocrole_chooser").dialog( "close" );
+                                }
+                       }
+                     ],
+                });
+                \$( "#LC_adhocrole_chooser" ).find( "form" ).on( "submit", function( event ) {
+                   event.preventDefault();
+                   var rolename = \$('input[name=LC_get_role]:checked', '#LChelpdeskpicker').val()
+                   process_pick(cdom,rolename);
+                   \$("#LC_adhocrole_chooser").dialog( "close" );
+                   $lastaction;
+                   });
+            }
+        }
+    }
 }
-|;
-    return &Apache::lonhtmlcommon::scripttag($output);
+http.send(params);
+    } else {
+        $autosubmit
+        $lastaction
+    }
+}
+
+ENDJS
+    return $rolechooser.&Apache::lonhtmlcommon::scripttag($output);
+}
+
+sub get_my_adhocroles {
+    my (@okroles,%description);
+    if ($env{'form.cid'} =~ /^($match_domain)_($match_courseid)$/) {
+        my $cdom = $1;
+        my $cnum = $2;
+        if ($env{"user.role.dh./$cdom/"}) {
+            my $then=$env{'user.login.time'};
+            my $update=$env{'user.update.time'};
+            my $liverole = 1;
+            my ($tstart,$tend)=split(/\./,$env{'user.role.dh./'.$cdom});
+            my $limit = $update;
+            if ($env{'request.role'} eq 'dh./'.$cdom.'/') {
+                $limit = $then;
+            }
+            if ($tstart && $tstart>$limit) { $liverole = 0; }
+            if ($tend   && $tend  <$limit) { $liverole = 0; }
+            if ($liverole) {
+                if (&Apache::lonnet::homeserver($cnum,$cdom) ne 'no_host') {
+                    my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
+                    if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
+                        my $count = 0;
+                        my %domcurrent = %{$domdefaults{'adhocroles'}};
+                        my (%ordered,%access_in_dom);
+                        foreach my $role (sort(keys(%{$domdefaults{'adhocroles'}}))) {
+                            my ($order,$desc,$access_in_dom);
+                            if (ref($domcurrent{$role}) eq 'HASH') {
+                                $order = $domcurrent{$role}{'order'};
+                                $desc = $domcurrent{$role}{'desc'};
+                                $access_in_dom{$role} = $domcurrent{$role}{'access'};
+                            }
+                            if ($order eq '') {
+                                $order = $count;
+                            }
+                            $ordered{$order} = $role;
+                            if ($desc ne '') {
+                                $description{$role} = $desc;
+                            } else {
+                                $description{$role}= $role;
+                            }
+                            $count++;
+                        }
+                        my @roles_by_num = ();
+                        foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
+                            push(@roles_by_num,$ordered{$item});
+                        }
+                        if (@roles_by_num) {
+                            my %settings = &Apache::lonnet::dump('environment',$cdom,$cnum,'internal\.adhoc');
+                            my %setincrs;
+                            if ($settings{'internal.adhocaccess'}) {
+                                map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
+                            }
+                            my @statuses;
+                            if ($env{'environment.inststatus'}) {
+                                @statuses = split(/,/,$env{'environment.inststatus'});
+                            }
+                            my $user = $env{'user.name'}.':'.$env{'user.domain'};
+                            foreach my $role (@roles_by_num) {
+                                my ($curraccess, at okstatus, at personnel);
+                                if ($setincrs{$role}) {
+                                    ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
+                                    if ($curraccess eq 'none') {
+                                        next;
+                                    } elsif ($curraccess eq 'all') {
+                                        push(@okroles,$role);
+                                    } elsif ($curraccess eq 'status') {
+                                        @okstatus = split(/\&/,$rest);
+                                    } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
+                                        @personnel = split(/\&/,$rest);
+                                    }
+                                } else {
+                                    $curraccess = $access_in_dom{$role};
+                                    if ($curraccess eq 'status') {
+                                        if (ref($domcurrent{$role}{$curraccess}) eq 'ARRAY') {
+                                            @okstatus = @{$domcurrent{$role}{$curraccess}};
+                                        }
+                                    } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
+                                        if (ref($domcurrent{$role}{$curraccess}) eq 'ARRAY') {
+                                            @personnel = @{$domcurrent{$role}{$curraccess}};
+                                        }
+                                    }
+                                }
+                                if ($curraccess eq 'none') {
+                                    next;
+                                } elsif ($curraccess eq 'all') {
+                                    push(@okroles,$role);
+                                } elsif ($curraccess eq 'status') {
+                                    if (@okstatus) {
+                                        if (!@statuses) {
+                                            if (grep(/^default$/, at okstatus)) {
+                                                push(@okroles,$role);
+                                            }
+                                        } else {
+                                            foreach my $status (@okstatus) {
+                                                if (grep(/^\Q$status\E$/, at statuses)) {
+                                                    push(@okroles,$role);
+                                                    last;
+                                                }
+                                            }
+                                        }
+                                    }
+                                } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
+                                    if (grep(/^\Q$user\E$/, at personnel)) {
+                                        if ($curraccess eq 'exc') {
+                                            push(@okroles,$role);
+                                        }
+                                    } elsif ($curraccess eq 'inc') {
+                                        push(@okroles,$role);
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    my $response = [];
+    if (@okroles) {
+        foreach my $role (@okroles) {
+            push(@{$response},
+                              { name => $role,
+                                desc => $description{$role},
+                              });
+        }
+    }
+    my $json = JSON::DWIW->to_json({roles => $response});
+    return $json;
 }
 
 1;
@@ -1038,9 +1287,9 @@
 
 =item *
 X<gochoose_javascript()>
-B<gochoose_javascript($type,$multiple,$autosubmit,$lastaction)>:
+B<gochoose_javascript($type,$multiple,$autosubmit,$lastaction,$rolename)>:
 
-Input: 4 - course type; single (0) or multiple courses (1); in context of DC selecting a CC role in a course: javascript code from &processpick(); final action to take after user chooses course(s):  either close window, or submit form for display of next page etc.
+Input: 5 - course type; single (0) or multiple courses (1); in context of DC selecting a CC role in a course: javascript code from &processpick(); final action to take after user chooses course(s):  either close window, or submit form for display of next page etc.; rolename (e.g., dh) of user's current role.
 
 Output: 1  $output - javascript wrapped in E<lt>scriptE<gt>E<lt>/scriptE<gt> tags
 
Index: loncom/interface/lonuserutils.pm
diff -u loncom/interface/lonuserutils.pm:1.179 loncom/interface/lonuserutils.pm:1.180
--- loncom/interface/lonuserutils.pm:1.179	Sun Nov 13 21:09:56 2016
+++ loncom/interface/lonuserutils.pm	Mon Jan  2 19:44:07 2017
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Utility functions for managing LON-CAPA user accounts
 #
-# $Id: lonuserutils.pm,v 1.179 2016/11/13 21:09:56 raeburn Exp $
+# $Id: lonuserutils.pm,v 1.180 2017/01/02 19:44:07 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -5891,6 +5891,17 @@
                 }
             }
         }
+        if ($env{'request.course.id'}) {
+            my $user = $env{'user.name'}.':'.$env{'user.domain'};
+            if (($user ne '') && ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq
+                                  $user)) {
+                $permission{'owner'} = 1;
+            } elsif (($user ne '') && ($env{'course.'.$env{'request.course.id'}.'.internal.co-owners'} ne '')) {
+                if (grep(/^\Q$user\E$/,split(/,/,$env{'course.'.$env{'request.course.id'}.'.internal.co-owners'}))) {
+                    $permission{'co-owner'} = 1;
+                }
+            }
+        }
     } elsif ($context eq 'author') {
         $permission{'cusr'} = &authorpriv($env{'user.name'},$env{'request.role.domain'});
         $permission{'view'} = $permission{'cusr'};
@@ -5916,6 +5927,9 @@
         if (&Apache::lonnet::allowed('vur',$env{'request.role.domain'})) {
             $permission{'view'} = 1;
         }
+        if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
+            $permission{'owner'} = 1;
+        }
     }
     my $allowed = 0;
     foreach my $perm (values(%permission)) {
@@ -6411,7 +6425,7 @@
 }
 
 sub custom_role_table {
-    my ($crstype,$full,$levels,$levelscurrent,$prefix) = @_;
+    my ($crstype,$full,$levels,$levelscurrent,$prefix,$add_class,$id) = @_;
     return unless ((ref($full) eq 'HASH') && (ref($levels) eq 'HASH') &&
                    (ref($levelscurrent) eq 'HASH'));
     my %lt=&Apache::lonlocal::texthash (
@@ -6425,7 +6439,7 @@
                system => '_s',
              );
 
-    my $output=&Apache::loncommon::start_data_table().
+    my $output=&Apache::loncommon::start_data_table($add_class,$id).
                &Apache::loncommon::start_data_table_header_row().
                '<th>'.$lt{'prv'}.'</th><th>'.$lt{'crl'}.'</th><th>'.$lt{'dml'}.
                '</th><th>'.$lt{'ssl'}.'</th>'.
@@ -6682,5 +6696,116 @@
     return %privs;
 }
 
+sub adhoc_status_types {
+    my ($cdom,$context,$role,$selectedref,$othertitle,$usertypes,$types,$disabled) = @_;
+    my $output = &Apache::loncommon::start_data_table();
+    my $numinrow = 3;
+    my $rem;
+    if (ref($types) eq 'ARRAY') {
+        for (my $i=0; $i<@{$types}; $i++) {
+            if (defined($usertypes->{$types->[$i]})) {
+                my $rem = $i%($numinrow);
+                if ($rem == 0) {
+                    if ($i > 0) {
+                        $output .= &Apache::loncommon::end_data_table_row();
+                    }
+                    $output .= &Apache::loncommon::start_data_table_row();
+                }
+                my $check;
+                if (ref($selectedref) eq 'ARRAY') {
+                    if (grep(/^\Q$types->[$i]\E$/,@{$selectedref})) {
+                        $check = ' checked="checked"';
+                    }
+                }
+                $output .= '<td>'.
+                           '<span class="LC_nobreak"><label>'.
+                           '<input type="checkbox" name="'.$context.$role.'_status" '.
+                           'value="'.$types->[$i].'"'.$check.$disabled.' />'.
+                           $usertypes->{$types->[$i]}.'</label></span></td>';
+            }
+        }
+        $rem = @{$types}%($numinrow);
+    }
+    my $colsleft = $numinrow - $rem;
+    if (($rem == 0) && (@{$types} > 0)) {
+        $output .= &Apache::loncommon::start_data_table_row();
+    }
+    if ($colsleft > 1) {
+        $output .= '<td colspan="'.$colsleft.'">';
+    } else {
+        $output .= '<td>';
+    }
+    my $defcheck;
+    if (ref($selectedref) eq 'ARRAY') {
+        if (grep(/^default$/,@{$selectedref})) {
+            $defcheck = ' checked="checked"';
+        }
+    }
+    $output .= '<span class="LC_nobreak"><label>'.
+               '<input type="checkbox" name="'.$context.$role.'_status"'.
+               'value="default"'.$defcheck.$disabled.' />'.
+               $othertitle.'</label></span></td>'.
+               &Apache::loncommon::end_data_table_row().
+               &Apache::loncommon::end_data_table();
+    return $output;
+}
+
+sub adhoc_staff {
+    my ($access,$context,$role,$selectedref,$adhocref,$disabled) = @_;
+    my $output;
+    if (ref($adhocref) eq 'HASH') {
+        my %by_fullname;
+        my $numinrow = 4;
+        my $rem;
+        my @personnel = keys(%{$adhocref});
+        if (@personnel) {
+            foreach my $person (@personnel) {
+                my ($uname,$udom) = split(/:/,$person);
+                my $fullname = &Apache::loncommon::plainname($uname,$udom,'lastname');
+                $by_fullname{$fullname} = $person;
+            }
+            my @sorted = sort(keys(%by_fullname));
+            my $count = scalar(@sorted);
+            $output = &Apache::loncommon::start_data_table();
+            for (my $i=0; $i<$count; $i++) {
+                my $rem = $i%($numinrow);
+                if ($rem == 0) {
+                    if ($i > 0) {
+                        $output .= &Apache::loncommon::end_data_table_row();
+                    }
+                    $output .= &Apache::loncommon::start_data_table_row();
+                }
+                my $check;
+                my $user = $by_fullname{$sorted[$i]};
+                if (ref($selectedref) eq 'ARRAY') {
+                    if (grep(/^\Q$user\E$/,@{$selectedref})) {
+                        $check = ' checked="checked"';
+                    }
+                }
+                if ($i == $count-1) {
+                    my $colsleft = $numinrow - $rem;
+                    if ($colsleft > 1) {
+                        $output .= '<td colspan="'.$colsleft.'">';
+                    } else {
+                        $output .= '<td>';
+                    }
+                } else {
+                    $output .= '<td>';
+                }
+                $output .= '<span class="LC_nobreak"><label>'.
+                           '<input type="checkbox" name="'.$context.$role.'_staff_'.$access.'" '.
+                           'value="'.$user.'"'.$check.$disabled.' />'.$sorted[$i].
+                           '</label></span></td>';
+                if ($i == $count-1) {
+                    $output .= &Apache::loncommon::end_data_table_row();
+                }
+            }
+            $output .= &Apache::loncommon::end_data_table();
+        }
+    }
+    return $output;
+}
+
+
 1;
 
Index: loncom/auth/lonroles.pm
diff -u loncom/auth/lonroles.pm:1.322 loncom/auth/lonroles.pm:1.323
--- loncom/auth/lonroles.pm:1.322	Tue Nov  8 23:15:34 2016
+++ loncom/auth/lonroles.pm	Mon Jan  2 19:44:11 2017
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # User Roles Screen
 #
-# $Id: lonroles.pm,v 1.322 2016/11/08 23:15:34 raeburn Exp $
+# $Id: lonroles.pm,v 1.323 2017/01/02 19:44:11 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -311,11 +311,9 @@
             my $cnum = $3;
             my $sec = $4;
             if ($custom_adhoc) {
-                my %adhocroles = &Apache::lonnet::userenvironment($env{'user.domain'},$env{'user.name'},
-                                                                  'adhocroles.'.$cdom);
-                if (keys(%adhocroles)) {
-                    my @adhoc = split(',',$adhocroles{'adhocroles.'.$cdom});
-                    if (grep(/^\Q$rolename\E$/, at adhoc)) {
+                my ($possroles,$description) = &Apache::lonnet::get_my_adhocroles($cdom.'_'.$cnum);
+                if (ref($possroles) eq 'ARRAY') {
+                    if (grep(/^\Q$rolename\E$/,@{$possroles})) { 
                         if (&Apache::lonnet::check_adhoc_privs($cdom,$cnum,$update,$refresh,$now,
                                                                "cr/$cdom/$cdom".'-domainconfig/'.$rolename,undef,$sec)) {
                             &Apache::lonnet::appenv({"environment.internal.$cdom.$cnum.cr/$cdom/$cdom".'-domainconfig/'."$rolename.adhoc" => time});
@@ -399,18 +397,9 @@
 # Is this an ad hoc custom role in a course/community?
                     if (my ($domain,$rolename,$coursenum,$sec) = ($envkey =~ m{^form\.cr/($match_domain)/\1\-domainconfig/(\w+)\./\1/($match_courseid)(?:/(\w+)|$)})) {
                         if ($dhroles{$domain}) {
-                            my @adhoc; 
-                            if ($env{'environment.adhocroles.'.$domain}) {
-                                @adhoc = split(',',$env{'environment.adhocroles.'.$domain});
-                            } else {
-                                my %adhocroles = &Apache::lonnet::userenvironment($env{'user.domain'},$env{'user.name'},
-                                                                                  'adhocroles.'.$domain);
-                                if (keys(%adhocroles)) {
-                                    @adhoc = split(',',$adhocroles{'adhocroles.'.$domain});
-                                }
-                            }
-                            if ((@adhoc > 0) && ($rolename ne '')) {
-                                if (grep(/^\Q$rolename\E$/, at adhoc)) {
+                            my ($possroles,$description) = &Apache::lonnet::get_my_adhocroles($domain.'_'.$coursenum);
+                            if (ref($possroles) eq 'ARRAY') {
+                                if (grep(/^\Q$rolename\E$/,@{$possroles})) {
                                     if (&Apache::lonnet::check_adhoc_privs($domain,$coursenum,$update,$refresh,$now,
                                                                            "cr/$domain/$domain".'-domainconfig/'.$rolename,
                                                                            undef,$sec)) {
@@ -566,7 +555,7 @@
 					 $env{'user.name'},
 					 $env{'user.home'},
 					 "Role ".$trolecode);
-		    
+
 		    &Apache::lonnet::appenv(
 					   {'request.role'        => $trolecode,
 					    'request.role.domain' => $cdom,
@@ -575,6 +564,15 @@
                     my $tadv=0;
 
 		    if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) {
+                        if ($role =~ m{^\Qcr/$cdom/$cdom\E\-domainconfig/(\w+)$}) {
+                            my $rolename = $1;
+                            my %domdef = &Apache::lonnet::get_domain_defaults($cdom);
+                            if (ref($domdef{'adhocroles'}) eq 'HASH') {
+                                if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
+                                    &Apache::lonnet::appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'}});
+                                }
+                            }
+                        }
                         my $msg;
 			my ($furl,$ferr)=
 			    &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
@@ -1047,7 +1045,7 @@
             my $doheaders = &roletable_headers($r,\%roleclass,\%sortrole,
                                                $nochoose);
             &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,
-                            \%roletext);
+                            \%roletext,$update,$then);
             my $tremark='';
             my $tbg;
             if ($env{'request.role'} eq 'cm') {
@@ -1118,9 +1116,8 @@
                 if ($role =~ m{^dc\./($match_domain)/$} 
 		    && $dcroles{$1}) {
 		    $output .= &adhoc_roles_row($1,'recent');
-                } elsif ($role =~ m{^dh\./($match_domain)/$}
-                         && ($env{'environment.adhocroles.'.$1} ne '')) {
-                    $output .= &adhoc_customroles_row($1,'recent');
+                } elsif ($role =~ m{^(dh)\./($match_domain)/$}) {
+                    $output .= &adhoc_customroles_row($1,$2,'recent',$update,$then);
                 }
 	    } elsif ($numdc > 0) {
                 unless ($role =~/^error\:/) {
@@ -1149,7 +1146,7 @@
             $doheaders ++;
 	}
     }
-    &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,\%roletext);
+    &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,\%roletext,$update,$then);
     if ($countactive > 1) {
         my $tremark='';
         my $tbg;
@@ -1226,6 +1223,7 @@
     my $tryagain = $env{'form.tryagain'};
     my @ids = &Apache::lonnet::current_machine_ids();
     if (ref($roles_in_env) eq 'HASH') {
+        my %adhocdesc;
         foreach my $envkey (sort(keys(%{$roles_in_env}))) {
             my $button = 1;
             my $switchserver='';
@@ -1329,8 +1327,27 @@
                     $ttype = &Apache::loncommon::course_type($tcourseid);
                     if ($role !~ /^cr/) {
                         $trole = &Apache::lonnet::plaintext($role,$ttype,$tcourseid);
-                    } elsif ($role =~ m{^cr/($match_domain)/\1-domainconfig/(\w+)$}) {
-                        $trole = &mt('Helpdesk[_1]',' '.$2);
+                    } elsif ($role =~ m{^\Qcr/$tdom/$tdom\E\-domainconfig/(\w+)$}) {
+                        my $rolename = $1;
+                        my $desc;
+                        if (ref($adhocdesc{$tdom}) eq 'HASH') {
+                            $desc = $adhocdesc{$tdom}{$rolename};
+                        } else {
+                            my %domdef = &Apache::lonnet::get_domain_defaults($tdom);
+                            if (ref($domdef{'adhocroles'}) eq 'HASH') {
+                                foreach my $rolename (sort(keys(%{$domdef{'adhocroles'}}))) {
+                                    if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
+                                        $adhocdesc{$tdom}{$rolename} = $domdef{'adhocroles'}{$rolename}{'desc'};
+                                        $desc = $adhocdesc{$tdom}{$rolename};
+                                    }
+                                }
+                            }
+                        }
+                        if ($desc ne '') {
+                            $trole = $desc;
+                        } else {
+                            $trole = &mt('Helpdesk[_1]',' '.$rolename);
+                        }
                     } else {
                         $trole = (split(/\//,$role,4))[-1];
                     }
@@ -1507,7 +1524,7 @@
 }
 
 sub print_rolerows {
-    my ($r,$doheaders,$roleclass,$sortrole,$dcroles,$roletext) = @_;
+    my ($r,$doheaders,$roleclass,$sortrole,$dcroles,$roletext,$update,$then) = @_;
     if ((ref($roleclass) eq 'HASH') && (ref($sortrole) eq 'HASH')) {
         my @types = &roletypes();
         foreach my $type (@types) {
@@ -1531,9 +1548,8 @@
                                     $output .= &adhoc_roles_row($1,'');
                                 }
                             }
-                        } elsif (($sortrole->{$which} =~ m{^user\.role\.dh\./($match_domain)/}) &&
-                                 ($env{'environment.adhocroles.'.$1} ne '')) {
-                            $output .= &adhoc_customroles_row($1,'');
+                        } elsif ($sortrole->{$which} =~ m{^user\.role\.(dh)\./($match_domain)/}) {
+                            $output .= &adhoc_customroles_row($1,$2,'',$update,$then);
                         }
                     }
                 }
@@ -1817,8 +1833,11 @@
                         $numdc++;
                     } else {
                         $dhroles->{$roledom} = $envkey;
-                        if ($env{'environment.adhocroles.'.$roledom} ne '') {
-                            $numadhoc ++;
+                        my %domdefaults = &Apache::lonnet::get_domain_defaults($roledom);
+                        if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
+                            if (keys(%{$domdefaults{'adhocroles'}})) {
+                                $numadhoc ++;
+                            }
                         }
                         $numdh++;
                     }
@@ -2042,20 +2061,25 @@
 }
 
 sub adhoc_customroles_row {
-    my ($dhdom,$rowtype) = @_;
-    my $output = &Apache::loncommon::continue_data_table_row()
-                 .' <td colspan="5" class="LC_textsize_mobile">'
-                 .&mt('[_1]Ad hoc[_2] course/community roles in domain [_3] --',
-                      '<span class="LC_cusr_emph">','</span>',$dhdom);
-    my @customroles = split(/,/,$env{'environment.adhocroles.'.$dhdom});
-    my $count = 0;
-    foreach my $role (@customroles) {
-        next if (($role eq '') || ($role =~ /\W/));
-        $output .= ' '.$role.': '.&courselink($dhdom,$rowtype,$role).' |';
-        $count ++;
-    }
-    if ($count) {
-        return $output;
+    my ($role,$dhdom,$rowtype,$update,$then) = @_;
+    my $liverole = 1;
+    my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$dhdom/"});
+    my $limit = $update;
+    if (($role eq 'dh') && ($env{'request.role'} eq 'dh./'.$dhdom.'/')) {
+        $limit = $then;
+    }
+    if ($tstart && $tstart>$limit) { $liverole = 0; }
+    if ($tend   && $tend  <$limit) { $liverole = 0; }
+    return unless ($liverole);
+    my %domdefaults = &Apache::lonnet::get_domain_defaults($dhdom); 
+    if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
+        if (scalar(keys(%{$domdefaults{'adhocroles'}})) > 0) {
+            return &Apache::loncommon::continue_data_table_row()
+                  .' <td colspan="5" class="LC_textsize_mobile">'
+                  .&mt('[_1]Ad hoc[_2] course/community roles in domain [_3] --',
+                       '<span class="LC_cusr_emph">','</span>',$dhdom)
+                  .' '.&courselink($dhdom,$rowtype,$role);
+        }
     }
     return;
 }
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.1331 loncom/lonnet/perl/lonnet.pm:1.1332
--- loncom/lonnet/perl/lonnet.pm:1.1331	Mon Dec  5 00:52:02 2016
+++ loncom/lonnet/perl/lonnet.pm	Mon Jan  2 19:44:20 2017
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.1331 2016/12/05 00:52:02 raeburn Exp $
+# $Id: lonnet.pm,v 1.1332 2017/01/02 19:44:20 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -2247,7 +2247,7 @@
                                   'coursedefaults','usersessions',
                                   'requestauthor','selfenrollment',
                                   'coursecategories','ssl','autoenroll',
-                                  'trust'],$domain);
+                                  'trust','helpsettings'],$domain);
     my @coursetypes = ('official','unofficial','community','textbook','placement');
     if (ref($domconfig{'defaults'}) eq 'HASH') {
         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
@@ -2392,6 +2392,12 @@
     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
     }
+    if (ref($domconfig{'helpsettings'}) eq 'HASH') {
+        $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
+        if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
+            $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
+        }
+    }
     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
     return %domdefaults;
 }
@@ -4484,6 +4490,131 @@
     return %returnhash;
 }
 
+sub get_my_adhocroles {
+    my ($cid) = @_;
+    my (@possroles,%description);
+    if ($cid =~ /^($match_domain)_($match_courseid)$/) {
+        my $cdom = $1;
+        my $cnum = $2;
+        if ($env{"user.role.dh./$cdom/"}) {
+            my $then=$env{'user.login.time'};
+            my $update=$env{'user.update.time'};
+            my $liverole = 1;
+            my ($tstart,$tend)=split(/\./,$env{'user.role.dh./'.$cdom});
+            my $limit = $update;
+            if ($env{'request.role'} eq 'dh./'.$cdom.'/') {
+                $limit = $then;
+            }
+            if ($tstart && $tstart>$limit) { $liverole = 0; }
+            if ($tend   && $tend  <$limit) { $liverole = 0; }
+            if ($liverole) {
+                if (&homeserver($cnum,$cdom) ne 'no_host') {
+                    my %domdefaults = &get_domain_defaults($cdom);
+                    if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
+                        my $count = 0;
+                        my %domcurrent = %{$domdefaults{'adhocroles'}};
+                        my (%ordered,%access_in_dom);
+                        foreach my $role (sort(keys(%domcurrent))) {
+                            my ($order,$desc,$access_in_dom);
+                            if (ref($domcurrent{$role}) eq 'HASH') {
+                                $order = $domcurrent{$role}{'order'};
+                                $desc = $domcurrent{$role}{'desc'};
+                                $access_in_dom{$role} = $domcurrent{$role}{'access'};
+                            }
+                            if ($order eq '') {
+                                $order = $count;
+                            }
+                            $ordered{$order} = $role;
+                            if ($desc ne '') {
+                                $description{$role} = $desc;
+                            } else {
+                                $description{$role}= $role;
+                            }
+                            $count++;
+                        }
+                        my @roles_by_num = ();
+                        foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
+                            push(@roles_by_num,$ordered{$item});
+                        }
+                        if (@roles_by_num) {
+                            my %settings;
+                            if ($env{'request.course.id'} eq $cid) {
+                                foreach my $envkey (keys(%env)) {
+                                    if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
+                                        $settings{$1} = $env{$envkey};
+                                    }
+                                }
+                            } else {
+                                %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
+                            }
+                            my %setincrs;
+                            if ($settings{'internal.adhocaccess'}) {
+                                map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
+                            }
+                            my @statuses;
+                            if ($env{'environment.inststatus'}) {
+                                @statuses = split(/,/,$env{'environment.inststatus'});
+                            }
+                            my $user = $env{'user.name'}.':'.$env{'user.domain'};
+                            foreach my $role (@roles_by_num) {
+                                my ($curraccess, at okstatus, at personnel);
+                                if ($setincrs{$role}) {
+                                    ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
+                                    if ($curraccess eq 'status') {
+                                        @okstatus = split(/\&/,$rest);
+                                    } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
+                                        @personnel = split(/\&/,$rest);
+                                    }
+                                } else {
+                                    $curraccess = $access_in_dom{$role};
+                                    if ($curraccess eq 'status') {
+                                        if (ref($domcurrent{$role}{$curraccess}) eq 'ARRAY') {
+                                            @okstatus = @{$domcurrent{$role}{$curraccess}};
+                                        }
+                                    } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
+                                        if (ref($domcurrent{$role}{$curraccess}) eq 'ARRAY') {
+                                            @personnel = @{$domcurrent{$role}{$curraccess}};
+                                        }
+                                    }
+                                }
+                                if ($curraccess eq 'none') {
+                                    next;
+                                } elsif ($curraccess eq 'all') {
+                                    push(@possroles,$role);
+                                } elsif ($curraccess eq 'status') {
+                                    if (@okstatus) {
+                                        if (!@statuses) {
+                                            if (grep(/^default$/, at okstatus)) {
+                                                push(@possroles,$role);
+                                            }
+                                        } else {
+                                            foreach my $status (@okstatus) {
+                                                if (grep(/^\Q$status\E$/, at statuses)) {
+                                                    push(@possroles,$role);
+                                                    last;
+                                                }
+                                            }
+                                        }
+                                    }
+                                } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
+                                    if (grep(/^\Q$user\E$/, at personnel)) {
+                                        if ($curraccess eq 'exc') {
+                                            push(@possroles,$role);
+                                        }
+                                    } elsif ($curraccess eq 'inc') {
+                                        push(@possroles,$role);
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+    return (\@possroles,\%description);
+}
+
 # ----------------------------------------------------- Frontpage Announcements
 #
 #
@@ -4724,6 +4855,21 @@
     return %personnel;
 }
 
+sub get_active_domroles {
+    my ($dom,$roles) = @_;
+    return () unless (ref($roles) eq 'ARRAY');
+    my $now = time;
+    my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
+    my %domroles;
+    foreach my $server (keys(%dompersonnel)) {
+        foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
+            my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
+            $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
+        }
+    }
+    return %domroles;
+}
+
 # ----------------------------------------------------------- Interval timing 
 
 {
@@ -5616,6 +5762,10 @@
                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                 }
                 if (($trest ne '') && (defined($coursepriv))) {
+                    if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
+                        my $rolename = $1;
+                        $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
+                    }
                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                 }
@@ -5624,6 +5774,48 @@
     }
 }
 
+sub course_adhocrole_privs {
+    my ($rolename,$cdom,$cnum,$coursepriv) = @_;
+    my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
+    if ($overrides{"internal.adhocpriv.$rolename"}) {
+        my (%currprivs,%storeprivs);
+        foreach my $item (split(/:/,$coursepriv)) {
+            my ($priv,$restrict) = split(/\&/,$item);
+            $currprivs{$priv} = $restrict;
+        }
+        my (%possadd,%possremove,%full);
+        foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
+            my ($priv,$restrict)=split(/\&/,$item);
+            $full{$priv} = $restrict;
+        }
+        foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
+             next if ($item eq '');
+             my ($rule,$rest) = split(/=/,$item);
+             next unless (($rule eq 'off') || ($rule eq 'on'));
+             foreach my $priv (split(/:/,$rest)) {
+                 if ($priv ne '') {
+                     if ($rule eq 'off') {
+                         $possremove{$priv} = 1;
+                     } else {
+                         $possadd{$priv} = 1;
+                     }
+                 }
+             }
+         }
+         foreach my $priv (sort(keys(%full))) {
+             if (exists($currprivs{$priv})) {
+                 unless (exists($possremove{$priv})) {
+                     $storeprivs{$priv} = $currprivs{$priv};
+                 }
+             } elsif (exists($possadd{$priv})) {
+                 $storeprivs{$priv} = $full{$priv};
+             }
+         }
+         $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
+     }
+     return $coursepriv;
+}
+
 sub group_roleprivs {
     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
     my $access = 1;
@@ -5867,8 +6059,15 @@
     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
                                   $env{'user.name'},1);
     my %rolehash = ();
-    if ($role =~ m{^cr/$dcdom/$dcdom\Q-domainconfig\E/}) {
+    if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
+        my $rolename = $1;
         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
+        my %domdef = &get_domain_defaults($dcdom);
+        if (ref($domdef{'adhocroles'}) eq 'HASH') {
+            if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
+                &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
+            }
+        }
     } else {
         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
     }
@@ -5878,7 +6077,7 @@
     unless ($caller eq 'constructaccess' && $env{'request.course.id'}) {
         &appenv( {'request.role'        => $spec,
                   'request.role.domain' => $dcdom,
-                  'request.course.sec'  => ''
+                  'request.course.sec'  => $sec,
                  }
                );
         my $tadv=0;


More information about the LON-CAPA-cvs mailing list