[LON-CAPA-cvs] cvs: loncom(version_2_11_X) /interface domainprefs.pm

raeburn raeburn at source.lon-capa.org
Sun Jan 22 08:28:07 EST 2017


raeburn		Sun Jan 22 13:28:07 2017 EDT

  Modified files:              (Branch: version_2_11_X)
    /loncom/interface	domainprefs.pm 
  Log:
  - For 2.11
    - Backport 1.285, 1.287.
  
  
-------------- next part --------------
Index: loncom/interface/domainprefs.pm
diff -u loncom/interface/domainprefs.pm:1.160.6.76 loncom/interface/domainprefs.pm:1.160.6.77
--- loncom/interface/domainprefs.pm:1.160.6.76	Sat Jan 21 21:51:56 2017
+++ loncom/interface/domainprefs.pm	Sun Jan 22 13:28:05 2017
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to set domain-wide configuration settings
 #
-# $Id: domainprefs.pm,v 1.160.6.76 2017/01/21 21:51:56 raeburn Exp $
+# $Id: domainprefs.pm,v 1.160.6.77 2017/01/22 13:28:05 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -632,7 +632,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') {
@@ -2884,6 +2884,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;
@@ -2899,20 +2900,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 => {},
@@ -2927,18 +2977,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"':'';
@@ -2952,25 +3016,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) = @_;
@@ -7655,7 +7944,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;
                 }
@@ -8391,7 +8680,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'} = '';
     } 
@@ -10547,15 +10836,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 '') {
@@ -10578,57 +10870,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}].'".',
@@ -10636,27 +11074,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;
 }
@@ -11892,24 +12389,9 @@
     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);
+    my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
     my @domcoord = keys(%domcoords);
     if (keys(%currhash)) {
         foreach my $dc (keys(%currhash)) {


More information about the LON-CAPA-cvs mailing list