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

raeburn raeburn at source.lon-capa.org
Wed Aug 10 10:54:42 EDT 2011


raeburn		Wed Aug 10 14:54:42 2011 EDT

  Modified files:              
    /loncom/interface	domainprefs.pm 
  Log:
  - Setting domain configuration for spares to offload to.
    - Javascript to modify contents of "new spare" dropdown lists based on
      checked/unchecked state of current offload targets.
    - Javascript to to modify contents of "new spare" dropdown lists to
      disallow selection of the same offload target in both primary
      and default for a specific server.
    - On multi-domain servers, setting of offload targets for server is
      made by DC for domain identified as default domain for server.
    - If multi-domain server is being offered in list of possible offload
      targets identify with hostID of the server for current domain (if
      exists) otherwise use default lonHostID.
  
  
-------------- next part --------------
Index: loncom/interface/domainprefs.pm
diff -u loncom/interface/domainprefs.pm:1.151 loncom/interface/domainprefs.pm:1.152
--- loncom/interface/domainprefs.pm:1.151	Tue Aug  9 12:16:41 2011
+++ loncom/interface/domainprefs.pm	Wed Aug 10 14:54:42 2011
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to set domain-wide configuration settings
 #
-# $Id: domainprefs.pm,v 1.151 2011/08/09 12:16:41 raeburn Exp $
+# $Id: domainprefs.pm,v 1.152 2011/08/10 14:54:42 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -375,7 +375,6 @@
                             ],
                  },
     );
-    my $js;
     if (keys(%servers) > 1) {
         $prefs{'login'}  = { text   => 'Log-in page options',
                              help   => 'Domain_Configuration_Login_Page',
@@ -384,10 +383,6 @@
                                        {col1 => 'Log-in Page Items',
                                         col2 => ''}],
                            };
-        my ($othertitle,$usertypes,$types) =
-            &Apache::loncommon::sorted_inst_types($dom);
-
-        $js = &lonbalance_targets_js($dom,$types,\%servers); 
     }
     my @roles = ('student','coordinator','author','admin');
     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
@@ -398,6 +393,14 @@
     if ($phase eq 'process') {
         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
     } elsif ($phase eq 'display') {
+        my $js;
+        if (keys(%servers) > 1) {
+            my ($othertitle,$usertypes,$types) =
+                &Apache::loncommon::sorted_inst_types($dom);
+            $js = &lonbalance_targets_js($dom,$types,\%servers).
+                  &new_spares_js().
+                  &common_domprefs_js();
+        }
         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
     } else {
         if (keys(%domconfig) == 0) {
@@ -2336,13 +2339,14 @@
     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 
     my @alldoms = &Apache::lonnet::all_domains();
-    my %uniques = &Apache::lonnet::get_unique_servers(\@alldoms);
+    my %serverhomes = %Apache::lonnet::serverhomeIDs;
     my %servers = &Apache::lonnet::internet_dom_servers($dom);
+    my %altids = &id_for_thisdom(%servers);
     my $itemcount = 1;
     if ($position eq 'top') {
-        if (keys(%uniques) > 1) {
+        if (keys(%serverhomes) > 1) {
             my %spareid = &current_offloads_to($dom,$settings,\%servers);
-            $datatable .= &spares_row(\%servers,\%spareid,\%uniques,$rowtotal);
+            $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
         } else {
             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
@@ -2515,14 +2519,16 @@
 sub current_offloads_to {
     my ($dom,$settings,$servers) = @_;
     my (%spareid,%otherdomconfigs);
-    if ((ref($settings) eq 'HASH') && (ref($servers) eq 'HASH')) {
+    if (ref($servers) eq 'HASH') {
         foreach my $lonhost (sort(keys(%{$servers}))) {
             my $gotspares;
-            if (ref($settings->{'spares'}) eq 'HASH') {
-                if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
-                    $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
-                    $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
-                    $gotspares = 1;
+            if (ref($settings) eq 'HASH') {
+                if (ref($settings->{'spares'}) eq 'HASH') {
+                    if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
+                        $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
+                        $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
+                        $gotspares = 1;
+                    }
                 }
             }
             unless ($gotspares) {
@@ -2590,63 +2596,101 @@
 }
 
 sub spares_row {
-    my ($servers,$spareid,$uniques,$rowtotal) = @_;
+    my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
     my $css_class;
     my $numinrow = 4;
     my $itemcount = 1;
     my $datatable;
-    if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH')) {
+    my %typetitles = &sparestype_titles();
+    if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
         foreach my $server (sort(keys(%{$servers}))) {
+            my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
+            my ($othercontrol,$serverdom);
+            if ($serverhome ne $server) {
+                $serverdom = &Apache::lonnet::host_domain($serverhome);
+                $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
+            } else {
+                $serverdom = &Apache::lonnet::host_domain($server);
+                if ($serverdom ne $dom) {
+                    $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
+                }
+            }
+            next unless (ref($spareid->{$server}) eq 'HASH');
             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
             $datatable .= '<tr'.$css_class.'>
                            <td rowspan="2">
-                            <span class="LC_nobreak"><b>'.$server.'</b> when busy, offloads to:</span></td>';
+                            <span class="LC_nobreak"><b>'.$server.'</b> when busy, offloads to:</span></td>'."\n";
             my (%current,%canselect);
-            if (ref($spareid->{$server}) eq 'HASH') {
-                foreach my $type ('primary','default') {
+            my @choices = 
+                &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
+            foreach my $type ('primary','default') {
+                if (ref($spareid->{$server}) eq 'HASH') {
                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
                         my @spares = @{$spareid->{$server}{$type}};
                         if (@spares > 0) {
-                            $current{$type} .= '<table>';
-                            for (my $i=0;  $i<@spares; $i++) {
-                                my $rem = $i%($numinrow);
-                                if ($rem == 0) {
-                                    if ($i > 0) {
-                                        $current{$type} .= '</tr>';
+                            if ($othercontrol) {
+                                $current{$type} = join(', ', at spares);
+                            } else {
+                                $current{$type} .= '<table>';
+                                my $numspares = scalar(@spares);
+                                for (my $i=0;  $i<@spares; $i++) {
+                                    my $rem = $i%($numinrow);
+                                    if ($rem == 0) {
+                                        if ($i > 0) {
+                                            $current{$type} .= '</tr>';
+                                        }
+                                        $current{$type} .= '<tr>';
                                     }
-                                    $current{$type} .= '<tr>';
+                                    $current{$type} .= '<td><label><input type="checkbox" name="spare_'.$type.'_'.$server.'" id="spare_'.$type.'_'.$server.'_'.$i.'" checked="checked" value="'.$spareid->{$server}{$type}[$i].'" onclick="updateNewSpares(this.form,'."'$server'".');" /> '.
+                                                       $spareid->{$server}{$type}[$i].
+                                                       '</label></td>'."\n";
+                                }
+                                my $rem = @spares%($numinrow);
+                                my $colsleft = $numinrow - $rem;
+                                if ($colsleft > 1 ) {
+                                    $current{$type} .= '<td colspan="'.$colsleft.
+                                                       '" class="LC_left_item">'.
+                                                       ' </td>';
+                                } elsif ($colsleft == 1) {
+                                    $current{$type} .= '<td class="LC_left_item"> </td>'."\n";
                                 }
-                                $current{$type} .= '<td><label><input type="checkbox" name="spare_'.$type.'_'.$server.'" checked="checked" value="'.$spareid->{$server}{$type}[$i].'" /> '.
-                                                   $spareid->{$server}{$type}[$i].
-                                                   '</label></td>';
-                            }
-                            my $rem = @spares%($numinrow);
-                            my $colsleft = $numinrow - $rem;
-                            if ($colsleft > 1 ) {
-                                $current{$type} .= '<td colspan="'.$colsleft.
-                                                   '" class="LC_left_item">'.
-                                                   ' </td>';
-                            } elsif ($colsleft == 1) {
-                                $current{$type} .= '<td class="LC_left_item"> </td>';
+                                $current{$type} .= '</tr></table>';
                             }
                         }
-                        $current{$type} .= '</tr></table>';
                     }
                     if ($current{$type} eq '') {
                         $current{$type} = &mt('None specified');
                     }
-                    $canselect{$type} =
-                        &newspare_select($server,$type,$spareid->{$server}{$type},$uniques);
+                    if ($othercontrol) {
+                        if ($type eq 'primary') {
+                            $canselect{$type} = $othercontrol;
+                        }
+                    } else {
+                        $canselect{$type} = 
+                            &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').' '.
+                            '<select name="newspare_'.$type.'_'.$server.'" '.
+                            'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
+                            '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
+                        if (@choices > 0) {
+                            foreach my $lonhost (@choices) {
+                                $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
+                            }
+                        }
+                        $canselect{$type} .= '</select>'."\n";
+                    }
+                } else {
+                    $current{$type} = &mt('Could not be determined');
+                    if ($type eq 'primary') {
+                        $canselect{$type} =  $othercontrol;
+                    }
                 }
+                if ($type eq 'default') {
+                    $datatable .= '<tr'.$css_class.'>';
+                }
+                $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
+                              '<td>'.$current{$type}.'</td>'."\n".
+                              '<td>'.$canselect{$type}.'</td></tr>'."\n";
             }
-            $datatable .= '<td><i>'.&mt('primary').'</i><td>'.$current{'primary'}.'</td>'.
-                          '<td>'.&mt('Add new [_1]primary[_2]:','<i>','</i>').' '.
-                          $canselect{'primary'}.'</td></tr>'.
-                          '<tr'.$css_class.'>'.
-                          '<td><i>'.&mt('default').'</i></td>'.
-                          '<td>'.$current{'default'}.'</td>'.
-                          '<td>'.&mt('Add new [_1]default[_2]:','<i>','</i>').' '.
-                          $canselect{'default'}.'</td></tr>';
             $itemcount ++;
         }
     }
@@ -2654,26 +2698,42 @@
     return $datatable;
 }
 
-sub newspare_select {
-    my ($server,$type,$currspares,$uniques) = @_;
-    my $output;
-    if (ref($uniques) eq 'HASH') {
-        if (keys(%{$uniques}) > 1) {
-            $output = '<select name="newspare_'.$type.'_'.$server.'">'."\n".
-                      '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
-            foreach my $lonhost (sort(keys(%{$uniques}))) {
-                next if ($lonhost eq $server);
-                if (ref($currspares) eq 'ARRAY') {
-                    if (@{$currspares} > 0) {
-                        next if (grep(/^\Q$lonhost\E$/,@{$currspares}));
+sub possible_newspares {
+    my ($server,$currspares,$serverhomes,$altids) = @_;
+    my $serverhostname = &Apache::lonnet::hostname($server);
+    my %excluded;
+    if ($serverhostname ne '') {
+        %excluded = (
+                       $serverhostname => 1,
+                    );
+    }
+    if (ref($currspares) eq 'HASH') {
+        foreach my $type (keys(%{$currspares})) {
+            if (ref($currspares->{$type}) eq 'ARRAY') {
+                if (@{$currspares->{$type}} > 0) {
+                    foreach my $curr (@{$currspares->{$type}}) {
+                        my $hostname = &Apache::lonnet::hostname($curr);
+                        $excluded{$hostname} = 1;
                     }
                 }
-                $output .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
             }
-            $output .= '<select>';
         }
     }
-    return $output;
+    my @choices;
+    if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
+        if (keys(%{$serverhomes}) > 1) {
+            foreach my $name (sort(keys(%{$serverhomes}))) {
+                unless ($excluded{$name}) {
+                    if (exists($altids->{$serverhomes->{$name}})) {
+                        push(@choices,$altids->{$serverhomes->{$name}});
+                    } else {
+                        push(@choices,$serverhomes->{$name});
+                    }
+                }
+            }
+        }
+    }
+    return sort(@choices);
 }
 
 sub print_loadbalancing {
@@ -7342,7 +7402,6 @@
     }
 
     my @alldoms = &Apache::lonnet::all_domains();
-    my %uniques = &Apache::lonnet::get_unique_servers(\@alldoms);
     my %servers = &Apache::lonnet::internet_dom_servers($dom);
     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
     my $savespares;
@@ -7350,34 +7409,26 @@
     foreach my $lonhost (sort(keys(%servers))) {
         my $serverhomeID =
             &Apache::lonnet::get_server_homeID($servers{$lonhost});
+        my $serverhostname = &Apache::lonnet::hostname($lonhost);
         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
         my %spareschg;
         foreach my $type (@{$types{'spares'}}) {
             my @okspares;
             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
             foreach my $server (@checked) {
-                unless (($server eq $lonhost) || ($server eq $serverhomeID)) {
-                    if ($uniques{$server}) {
-                        push(@okspares,$server);
+                if (&Apache::lonnet::hostname($server) ne '') {
+                    unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
+                        unless (grep(/^\Q$server\E$/, at okspares)) {
+                            push(@okspares,$server);
+                        }
                     }
                 }
             }
             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
             my $newspare;
-            if (($new ne '') && ($uniques{$new})) {
-                unless (($new eq $lonhost) || ($new eq $serverhomeID)) {
+            if (($new ne '') && (&Apache::lonnet::hostname($new))) {
+                unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
                     $newspare = $new;
-                    $spareschg{$type} = 1;
-                }
-            }
-            if (ref($spareid{$lonhost}) eq 'HASH') {
-                if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
-                    my @diffs = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{'spares'}{$lonhost}{$type},\@okspares);
-                    if (@diffs > 0) {
-                        $spareschg{$type} = 1;
-                    } elsif ($new ne '') {
-                        $spareschg{$type} = 1;
-                    }
                 }
             }
             my @spares;
@@ -7387,6 +7438,14 @@
                 @spares = sort(@okspares);
             }
             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
+            if (ref($spareid{$lonhost}) eq 'HASH') {
+                if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
+                    my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
+                    if (@diffs > 0) {
+                        $spareschg{$type} = 1;
+                    }
+                }
+            }
         }
         if (keys(%spareschg) > 0) {
             $changes{'spares'}{$lonhost} = \%spareschg;
@@ -7813,6 +7872,18 @@
            );
 }
 
+sub id_for_thisdom {
+    my (%servers) = @_;
+    my %altids;
+    foreach my $server (keys(%servers)) {
+        my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
+        if ($serverhome ne $server) {
+            $altids{$serverhome} = $server;
+        }
+    }
+    return %altids;
+}
+
 sub count_servers {
     my ($currbalancer,%servers) = @_;
     my (@spares,$numspares);
@@ -7875,7 +7946,7 @@
 
     document.getElementById('loadbalancing_targets').style.display='block';
     document.getElementById('loadbalancing_disabled').style.display='none';
-
+ 
     for (var i=0; i<offloadtypes.length; i++) {
         var count = 0;
         for (var j=0; j<alltargets.length; j++) {
@@ -7998,20 +8069,129 @@
     return;
 }
 
+// ]]>
+</script>
+
+END
+}
+
+sub new_spares_js {
+    my @sparestypes = ('primary','default');
+    my $types = join("','", at sparestypes);
+    my $select = &mt('Select');
+    return <<"END";
+
+<script type="text/javascript">
+// <![CDATA[
+
+function updateNewSpares(formname,lonhost) {
+    var types = new Array('$types');
+    var include = new Array();
+    var exclude = new Array();
+    for (var i=0; i<types.length; i++) {
+        var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
+        for (var j=0; j<spareboxes.length; j++) {
+            if (formname.elements[spareboxes[j]].checked) {
+                exclude.push(formname.elements[spareboxes[j]].value);
+            } else {
+                include.push(formname.elements[spareboxes[j]].value);
+            }
+        }
+    }
+    for (var i=0; i<types.length; i++) {
+        var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
+        var selIdx = newSpare.selectedIndex;
+        var currnew = newSpare.options[selIdx].value;
+        var okSpares = new Array();
+        for (var j=0; j<newSpare.options.length; j++) {
+            var possible = newSpare.options[j].value;
+            if (possible != '') {
+                if (exclude.indexOf(possible) == -1) {
+                    okSpares.push(possible);
+                } else {
+                    if (currnew == possible) {
+                        selIdx = 0;
+                    }
+                }
+            }
+        }
+        for (var k=0; k<include.length; k++) {
+            if (okSpares.indexOf(include[k]) == -1) {
+                okSpares.push(include[k]);
+            }
+        }
+        okSpares.sort();
+        newSpare.options.length = 0;
+        if (selIdx == 0) {
+            newSpare.options[0] = new Option("$select","",true,true);
+        } else {
+            newSpare.options[0] = new Option("$select","",false,false);
+        }
+        for (var m=0; m<okSpares.length; m++) {
+            var idx = m+1;
+            var selThis = 0;
+            if (selIdx != 0) {
+                if (okSpares[m] == currnew) {
+                    selThis = 1;
+                }
+            }
+            if (selThis == 1) {
+                newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
+            } else {
+                newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
+            }
+        }
+    }
+    return;
+}
+
+function checkNewSpares(lonhost,type) {
+    var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
+    var chosen =  newSpare.options[newSpare.selectedIndex].value;
+    if (chosen != '') { 
+        var othertype;
+        var othernewSpare;
+        if (type == 'primary') {
+            othernewSpare = document.getElementById('newspare_default_'+lonhost);
+        }
+        if (type == 'default') {
+            othernewSpare = document.getElementById('newspare_primary_'+lonhost);
+        }
+        if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
+            othernewSpare.selectedIndex = 0;
+        }
+    }
+    return;
+}
+
+// ]]>
+</script>
+
+END
+
+}
+
+sub common_domprefs_js {
+    return <<"END";
+
+<script type="text/javascript">
+// <![CDATA[
+
 function getIndicesByName(formname,item) {
-    var radiogroup = new Array(); 
+    var group = new Array();
     for (var i=0;i<formname.elements.length;i++) {
         if (formname.elements[i].name == item) {
-            radiogroup.push(formname.elements[i].id);
+            group.push(formname.elements[i].id);
         }
     }
-    return radiogroup; 
+    return group;
 }
 
 // ]]>
 </script>
 
 END
+
 }
 
 1;


More information about the LON-CAPA-cvs mailing list