File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.8: download - view: text, annotated - select for diffs
Tue Oct 2 19:38:00 2012 UTC (11 years, 9 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11.
  - Backport 1.172, 1.173.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.8 2012/10/02 19:38:00 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas,
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course.
   98: 
   99: Outputs: 1
  100: 
  101: $datatable  - HTML containing form elements which allow settings to be changed. 
  102: 
  103: In the case of course requests, radio buttons are displayed for each institutional
  104: affiliate type (and also default, and _LC_adv) for each of the course types 
  105: (official, unofficial and community).  In each case the radio buttons allow the 
  106: selection of one of four values:
  107: 
  108: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  109: which have the following effects:
  110: 
  111: 0
  112: 
  113: =over
  114: 
  115: - course requests are not allowed for this course types/affiliation
  116: 
  117: =back
  118: 
  119: approval 
  120: 
  121: =over 
  122: 
  123: - course requests must be approved by a Doman Coordinator in the 
  124: course's domain
  125: 
  126: =back
  127: 
  128: validate 
  129: 
  130: =over
  131: 
  132: - an institutional validation (e.g., check requestor is instructor
  133: of record) needs to be passed before the course will be created.  The required
  134: validation is in localenroll.pm on the primary library server for the course 
  135: domain.
  136: 
  137: =back
  138: 
  139: autolimit 
  140: 
  141: =over
  142:  
  143: - course requests will be processed automatically up to a limit of
  144: N requests for the course type for the particular requestor.
  145: If N is undefined, there is no limit to the number of course requests
  146: which a course owner may submit and have processed automatically. 
  147: 
  148: =back
  149: 
  150: =item modify_quotas() 
  151: 
  152: =back
  153: 
  154: =cut
  155: 
  156: package Apache::domainprefs;
  157: 
  158: use strict;
  159: use Apache::Constants qw(:common :http);
  160: use Apache::lonnet;
  161: use Apache::loncommon();
  162: use Apache::lonhtmlcommon();
  163: use Apache::lonlocal;
  164: use Apache::lonmsg();
  165: use Apache::lonconfigsettings;
  166: use LONCAPA qw(:DEFAULT :match);
  167: use LONCAPA::Enrollment;
  168: use LONCAPA::lonauthcgi();
  169: use File::Copy;
  170: use Locale::Language;
  171: use DateTime::TimeZone;
  172: use DateTime::Locale;
  173: 
  174: my $registered_cleanup;
  175: my $modified_urls;
  176: 
  177: sub handler {
  178:     my $r=shift;
  179:     if ($r->header_only) {
  180:         &Apache::loncommon::content_type($r,'text/html');
  181:         $r->send_http_header;
  182:         return OK;
  183:     }
  184: 
  185:     my $context = 'domain';
  186:     my $dom = $env{'request.role.domain'};
  187:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  188:     if (&Apache::lonnet::allowed('mau',$dom)) {
  189:         &Apache::loncommon::content_type($r,'text/html');
  190:         $r->send_http_header;
  191:     } else {
  192:         $env{'user.error.msg'}=
  193:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  194:         return HTTP_NOT_ACCEPTABLE;
  195:     }
  196: 
  197:     $registered_cleanup=0;
  198:     @{$modified_urls}=();
  199: 
  200:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  201:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  202:                                             ['phase','actions']);
  203:     my $phase = 'pickactions';
  204:     if ( exists($env{'form.phase'}) ) {
  205:         $phase = $env{'form.phase'};
  206:     }
  207:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  208:     my %domconfig =
  209:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  210:                 'quotas','autoenroll','autoupdate','autocreate',
  211:                 'directorysrch','usercreation','usermodification',
  212:                 'contacts','defaults','scantron','coursecategories',
  213:                 'serverstatuses','requestcourses','usersessions',
  214:                 'loadbalancing','requestauthor'],$dom);
  215:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  216:                        'autoupdate','autocreate','directorysrch','contacts',
  217:                        'usercreation','usermodification','scantron',
  218:                        'requestcourses','requestauthor','coursecategories',
  219:                        'serverstatuses','usersessions');
  220:     my %existing;
  221:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  222:         %existing = %{$domconfig{'loadbalancing'}};
  223:     }
  224:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  225:         push(@prefs_order,'loadbalancing');
  226:     }
  227:     my %prefs = (
  228:         'rolecolors' =>
  229:                    { text => 'Default color schemes',
  230:                      help => 'Domain_Configuration_Color_Schemes',
  231:                      header => [{col1 => 'Student Settings',
  232:                                  col2 => '',},
  233:                                 {col1 => 'Coordinator Settings',
  234:                                  col2 => '',},
  235:                                 {col1 => 'Author Settings',
  236:                                  col2 => '',},
  237:                                 {col1 => 'Administrator Settings',
  238:                                  col2 => '',}],
  239:                     },
  240:         'login' =>
  241:                     { text => 'Log-in page options',
  242:                       help => 'Domain_Configuration_Login_Page',
  243:                       header => [{col1 => 'Log-in Page Items',
  244:                                   col2 => '',},
  245:                                  {col1 => 'Log-in Help',
  246:                                   col2 => 'Value'}],
  247:                     },
  248:         'defaults' => 
  249:                     { text => 'Default authentication/language/timezone/portal',
  250:                       help => 'Domain_Configuration_LangTZAuth',
  251:                       header => [{col1 => 'Setting',
  252:                                   col2 => 'Value'}],
  253:                     },
  254:         'quotas' => 
  255:                     { text => 'Blogs, personal web pages, webDAV, portfolios',
  256:                       help => 'Domain_Configuration_Quotas',
  257:                       header => [{col1 => 'User affiliation',
  258:                                   col2 => 'Available tools',
  259:                                   col3 => 'Portfolio quota',}],
  260:                     },
  261:         'autoenroll' =>
  262:                    { text => 'Auto-enrollment settings',
  263:                      help => 'Domain_Configuration_Auto_Enrollment',
  264:                      header => [{col1 => 'Configuration setting',
  265:                                  col2 => 'Value(s)'}],
  266:                    },
  267:         'autoupdate' => 
  268:                    { text => 'Auto-update settings',
  269:                      help => 'Domain_Configuration_Auto_Updates',
  270:                      header => [{col1 => 'Setting',
  271:                                  col2 => 'Value',},
  272:                                 {col1 => 'Setting',
  273:                                  col2 => 'Affiliation'},
  274:                                 {col1 => 'User population',
  275:                                  col2 => 'Updateable user data'}],
  276:                   },
  277:         'autocreate' => 
  278:                   { text => 'Auto-course creation settings',
  279:                      help => 'Domain_Configuration_Auto_Creation',
  280:                      header => [{col1 => 'Configuration Setting',
  281:                                  col2 => 'Value',}],
  282:                   },
  283:         'directorysrch' => 
  284:                   { text => 'Institutional directory searches',
  285:                     help => 'Domain_Configuration_InstDirectory_Search',
  286:                     header => [{col1 => 'Setting',
  287:                                 col2 => 'Value',}],
  288:                   },
  289:         'contacts' =>
  290:                   { text => 'Contact Information',
  291:                     help => 'Domain_Configuration_Contact_Info',
  292:                     header => [{col1 => 'Setting',
  293:                                 col2 => 'Value',}],
  294:                   },
  295: 
  296:         'usercreation' => 
  297:                   { text => 'User creation',
  298:                     help => 'Domain_Configuration_User_Creation',
  299:                     header => [{col1 => 'Format rule type',
  300:                                 col2 => 'Format rules in force'},
  301:                                {col1 => 'User account creation',
  302:                                 col2 => 'Usernames which may be created',},
  303:                                {col1 => 'Context',
  304:                                 col2 => 'Assignable authentication types'}],
  305:                   },
  306:         'usermodification' =>
  307:                   { text => 'User modification',
  308:                     help => 'Domain_Configuration_User_Modification',
  309:                     header => [{col1 => 'Target user has role',
  310:                                 col2 => 'User information updateable in author context'},
  311:                                {col1 => 'Target user has role',
  312:                                 col2 => 'User information updateable in course context'},
  313:                                {col1 => "Status of user",
  314:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  315:                   },
  316:         'scantron' =>
  317:                   { text => 'Bubblesheet format file',
  318:                     help => 'Domain_Configuration_Scantron_Format',
  319:                     header => [ {col1 => 'Item',
  320:                                  col2 => '',
  321:                               }],
  322:                   },
  323:         'requestcourses' => 
  324:                  {text => 'Request creation of courses',
  325:                   help => 'Domain_Configuration_Request_Courses',
  326:                   header => [{col1 => 'User affiliation',
  327:                               col2 => 'Availability/Processing of requests',},
  328:                              {col1 => 'Setting',
  329:                               col2 => 'Value'}],
  330:                  },
  331:         'requestauthor' =>
  332:                  {text => 'Request authoring space',
  333:                   help => 'Domain_Configuration_Request_Author',
  334:                   header => [{col1 => 'User affiliation',
  335:                               col2 => 'Availability/Processing of requests',},
  336:                              {col1 => 'Setting',
  337:                               col2 => 'Value'}],
  338:                  },
  339:         'coursecategories' =>
  340:                   { text => 'Cataloging of courses/communities',
  341:                     help => 'Domain_Configuration_Cataloging_Courses',
  342:                     header => [{col1 => 'Category settings',
  343:                                 col2 => '',},
  344:                                {col1 => 'Categories',
  345:                                 col2 => '',
  346:                                }],
  347:                   },
  348:         'serverstatuses' =>
  349:                  {text   => 'Access to server status pages',
  350:                   help   => 'Domain_Configuration_Server_Status',
  351:                   header => [{col1 => 'Status Page',
  352:                               col2 => 'Other named users',
  353:                               col3 => 'Specific IPs',
  354:                             }],
  355:                  },
  356:         'usersessions' =>
  357:                  {text  => 'User session hosting/offloading',
  358:                   help  => 'Domain_Configuration_User_Sessions',
  359:                   header => [{col1 => 'Domain server',
  360:                               col2 => 'Servers to offload sessions to when busy'},
  361:                              {col1 => 'Hosting of users from other domains',
  362:                               col2 => 'Rules'},
  363:                              {col1 => "Hosting domain's own users elsewhere",
  364:                               col2 => 'Rules'}],
  365:                  },
  366:          'loadbalancing' =>
  367:                  {text  => 'Dedicated Load Balancer(s)',
  368:                   help  => 'Domain_Configuration_Load_Balancing',
  369:                   header => [{col1 => 'Balancers',
  370:                               col2 => 'Default destinations',
  371:                               col3 => 'User affliation',
  372:                               col4 => 'Overrides'},
  373:                             ],
  374:                  },
  375:     );
  376:     if (keys(%servers) > 1) {
  377:         $prefs{'login'}  = { text   => 'Log-in page options',
  378:                              help   => 'Domain_Configuration_Login_Page',
  379:                             header => [{col1 => 'Log-in Service',
  380:                                         col2 => 'Server Setting',},
  381:                                        {col1 => 'Log-in Page Items',
  382:                                         col2 => ''},
  383:                                        {col1 => 'Log-in Help',
  384:                                         col2 => 'Value'}],
  385:                            };
  386:     }
  387:     my @roles = ('student','coordinator','author','admin');
  388:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  389:     &Apache::lonhtmlcommon::add_breadcrumb
  390:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  391:       text=>"Settings to display/modify"});
  392:     my $confname = $dom.'-domainconfig';
  393:     if ($phase eq 'process') {
  394:         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
  395:     } elsif ($phase eq 'display') {
  396:         my $js = &recaptcha_js();
  397:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  398:             my ($othertitle,$usertypes,$types) =
  399:                 &Apache::loncommon::sorted_inst_types($dom);
  400:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  401:                                           $domconfig{'loadbalancing'}).
  402:                    &new_spares_js().
  403:                    &common_domprefs_js().
  404:                    &Apache::loncommon::javascript_array_indexof();
  405:         }
  406:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  407:     } else {
  408:         if (keys(%domconfig) == 0) {
  409:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  410:             my @ids=&Apache::lonnet::current_machine_ids();
  411:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  412:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  413:                 my @loginimages = ('img','logo','domlogo','login');
  414:                 my $custom_img_count = 0;
  415:                 foreach my $img (@loginimages) {
  416:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  417:                         $custom_img_count ++;
  418:                     }
  419:                 }
  420:                 foreach my $role (@roles) {
  421:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  422:                         $custom_img_count ++;
  423:                     }
  424:                 }
  425:                 if ($custom_img_count > 0) {
  426:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  427:                     my $switch_server = &check_switchserver($dom,$confname);
  428:                     $r->print(
  429:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  430:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  431:     &mt("Thereafter, (with a Domain Coordinator role selected in the domain) you will be able to update settings when logged in to any server in the LON-CAPA network.").'<br />'.
  432:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  433:                     if ($switch_server) {
  434:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  435:                     }
  436:                     $r->print(&Apache::loncommon::end_page());
  437:                     return OK;
  438:                 }
  439:             }
  440:         }
  441:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  442:     }
  443:     return OK;
  444: }
  445: 
  446: sub process_changes {
  447:     my ($r,$dom,$confname,$action,$roles,$values) = @_;
  448:     my %domconfig;
  449:     if (ref($values) eq 'HASH') {
  450:         %domconfig = %{$values};
  451:     }
  452:     my $output;
  453:     if ($action eq 'login') {
  454:         $output = &modify_login($r,$dom,$confname,%domconfig);
  455:     } elsif ($action eq 'rolecolors') {
  456:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  457:                                      %domconfig);
  458:     } elsif ($action eq 'quotas') {
  459:         $output = &modify_quotas($dom,$action,%domconfig);
  460:     } elsif ($action eq 'autoenroll') {
  461:         $output = &modify_autoenroll($dom,%domconfig);
  462:     } elsif ($action eq 'autoupdate') {
  463:         $output = &modify_autoupdate($dom,%domconfig);
  464:     } elsif ($action eq 'autocreate') {
  465:         $output = &modify_autocreate($dom,%domconfig);
  466:     } elsif ($action eq 'directorysrch') {
  467:         $output = &modify_directorysrch($dom,%domconfig);
  468:     } elsif ($action eq 'usercreation') {
  469:         $output = &modify_usercreation($dom,%domconfig);
  470:     } elsif ($action eq 'usermodification') {
  471:         $output = &modify_usermodification($dom,%domconfig);
  472:     } elsif ($action eq 'contacts') {
  473:         $output = &modify_contacts($dom,%domconfig);
  474:     } elsif ($action eq 'defaults') {
  475:         $output = &modify_defaults($dom,$r);
  476:     } elsif ($action eq 'scantron') {
  477:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  478:     } elsif ($action eq 'coursecategories') {
  479:         $output = &modify_coursecategories($dom,%domconfig);
  480:     } elsif ($action eq 'serverstatuses') {
  481:         $output = &modify_serverstatuses($dom,%domconfig);
  482:     } elsif ($action eq 'requestcourses') {
  483:         $output = &modify_quotas($dom,$action,%domconfig);
  484:     } elsif ($action eq 'requestauthor') {
  485:         $output = &modify_quotas($dom,$action,%domconfig);
  486:     } elsif ($action eq 'usersessions') {
  487:         $output = &modify_usersessions($dom,%domconfig);
  488:     } elsif ($action eq 'loadbalancing') {
  489:         $output = &modify_loadbalancing($dom,%domconfig);
  490:     }
  491:     return $output;
  492: }
  493: 
  494: sub print_config_box {
  495:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  496:     my $rowtotal = 0;
  497:     my $output;
  498:     if ($action eq 'coursecategories') {
  499:         $output = &coursecategories_javascript($settings);
  500:     }
  501:     $output .= 
  502:          '<table class="LC_nested_outer">
  503:           <tr>
  504:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  505:            &mt($item->{text}).'&nbsp;'.
  506:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  507:           '</tr>';
  508:     $rowtotal ++;
  509:     my $numheaders = 1;
  510:     if (ref($item->{'header'}) eq 'ARRAY') {
  511:         $numheaders = scalar(@{$item->{'header'}});
  512:     }
  513:     if ($numheaders > 1) {
  514:         my $colspan = '';
  515:         my $rightcolspan = '';
  516:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  517:             (($action eq 'login') && ($numheaders < 3))) {
  518:             $colspan = ' colspan="2"';
  519:         }
  520:         if ($action eq 'usersessions') {
  521:             $rightcolspan = ' colspan="3"'; 
  522:         }
  523:         $output .= '
  524:           <tr>
  525:            <td>
  526:             <table class="LC_nested">
  527:              <tr class="LC_info_row">
  528:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  529:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  530:              </tr>';
  531:         $rowtotal ++;
  532:         if ($action eq 'autoupdate') {
  533:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  534:         } elsif ($action eq 'usercreation') {
  535:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  536:         } elsif ($action eq 'usermodification') {
  537:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  538:         } elsif ($action eq 'coursecategories') {
  539:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  540:         } elsif ($action eq 'login') {
  541:             if ($numheaders == 3) {
  542:                 $colspan = ' colspan="2"';
  543:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  544:             } else {
  545:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  546:             }
  547:         } elsif ($action eq 'requestcourses') {
  548:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  549:         } elsif ($action eq 'requestauthor') {
  550:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  551:         } elsif ($action eq 'usersessions') {
  552:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  553:         } elsif ($action eq 'rolecolors') {
  554:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  555:         }
  556:         $output .= '
  557:            </table>
  558:           </td>
  559:          </tr>
  560:          <tr>
  561:            <td>
  562:             <table class="LC_nested">
  563:              <tr class="LC_info_row">
  564:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  565:         $output .= '
  566:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  567:              </tr>';
  568:             $rowtotal ++;
  569:         if ($action eq 'autoupdate') {
  570:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  571:            </table>
  572:           </td>
  573:          </tr>
  574:          <tr>
  575:            <td>
  576:             <table class="LC_nested">
  577:              <tr class="LC_info_row">
  578:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  579:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  580:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  581:             $rowtotal ++;
  582:         } elsif ($action eq 'usercreation') {
  583:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  584:            </table>
  585:           </td>
  586:          </tr>
  587:          <tr>
  588:            <td>
  589:             <table class="LC_nested">
  590:              <tr class="LC_info_row">
  591:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  592:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  593:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  594:             $rowtotal ++;
  595:         } elsif ($action eq 'usermodification') {
  596:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  597:            </table>
  598:           </td>
  599:          </tr>
  600:          <tr>
  601:            <td>
  602:             <table class="LC_nested">
  603:              <tr class="LC_info_row">
  604:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  605:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  606:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  607:             $rowtotal ++;
  608:         } elsif ($action eq 'coursecategories') {
  609:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  610:         } elsif ($action eq 'login') {
  611:             if ($numheaders == 3) {
  612:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  613:            </table>
  614:           </td>
  615:          </tr>
  616:          <tr>
  617:            <td>
  618:             <table class="LC_nested">
  619:              <tr class="LC_info_row">
  620:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  621:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  622:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  623:                 $rowtotal ++;
  624:             } else {
  625:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  626:             }
  627:         } elsif ($action eq 'requestcourses') {
  628:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  629:         } elsif ($action eq 'requestauthor') {
  630:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  631:         } elsif ($action eq 'usersessions') {
  632:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  633:            </table>
  634:           </td>
  635:          </tr>
  636:          <tr>
  637:            <td>
  638:             <table class="LC_nested">
  639:              <tr class="LC_info_row">
  640:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  641:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  642:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  643:             $rowtotal ++;
  644:         } elsif ($action eq 'coursedefaults') {
  645:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  646:         } elsif ($action eq 'rolecolors') {
  647:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  648:            </table>
  649:           </td>
  650:          </tr>
  651:          <tr>
  652:            <td>
  653:             <table class="LC_nested">
  654:              <tr class="LC_info_row">
  655:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  656:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  657:               <td class="LC_right_item" valign="top">'.
  658:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  659:              </tr>'.
  660:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  661:            </table>
  662:           </td>
  663:          </tr>
  664:          <tr>
  665:            <td>
  666:             <table class="LC_nested">
  667:              <tr class="LC_info_row">
  668:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  669:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  670:              </tr>'.
  671:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  672:             $rowtotal += 2;
  673:         }
  674:     } else {
  675:         $output .= '
  676:           <tr>
  677:            <td>
  678:             <table class="LC_nested">
  679:              <tr class="LC_info_row">';
  680:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  681:             $output .= '  
  682:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  683:         } elsif ($action eq 'serverstatuses') {
  684:             $output .= '
  685:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  686:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  687: 
  688:         } else {
  689:             $output .= '
  690:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  691:         }
  692:         if (defined($item->{'header'}->[0]->{'col3'})) {
  693:             $output .= '<td class="LC_left_item" valign="top">'.
  694:                        &mt($item->{'header'}->[0]->{'col2'});
  695:             if ($action eq 'serverstatuses') {
  696:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  697:             } 
  698:         } else {
  699:             $output .= '<td class="LC_right_item" valign="top">'.
  700:                        &mt($item->{'header'}->[0]->{'col2'});
  701:         }
  702:         $output .= '</td>';
  703:         if ($item->{'header'}->[0]->{'col3'}) {
  704:             if (defined($item->{'header'}->[0]->{'col4'})) {
  705:                 $output .= '<td class="LC_left_item" valign="top">'.
  706:                             &mt($item->{'header'}->[0]->{'col3'});
  707:             } else {
  708:                 $output .= '<td class="LC_right_item" valign="top">'.
  709:                            &mt($item->{'header'}->[0]->{'col3'});
  710:             }
  711:             if ($action eq 'serverstatuses') {
  712:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  713:             }
  714:             $output .= '</td>';
  715:         }
  716:         if ($item->{'header'}->[0]->{'col4'}) {
  717:             $output .= '<td class="LC_right_item" valign="top">'.
  718:                        &mt($item->{'header'}->[0]->{'col4'});
  719:         }
  720:         $output .= '</tr>';
  721:         $rowtotal ++;
  722:         if ($action eq 'quotas') {
  723:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  724:         } elsif ($action eq 'autoenroll') {
  725:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  726:         } elsif ($action eq 'autocreate') {
  727:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  728:         } elsif ($action eq 'directorysrch') {
  729:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  730:         } elsif ($action eq 'contacts') {
  731:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  732:         } elsif ($action eq 'defaults') {
  733:             $output .= &print_defaults($dom,\$rowtotal);
  734:         } elsif ($action eq 'scantron') {
  735:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  736:         } elsif ($action eq 'serverstatuses') {
  737:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  738:         } elsif ($action eq 'helpsettings') {
  739:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  740:         } elsif ($action eq 'loadbalancing') {
  741:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  742:         }
  743:     }
  744:     $output .= '
  745:    </table>
  746:   </td>
  747:  </tr>
  748: </table><br />';
  749:     return ($output,$rowtotal);
  750: }
  751: 
  752: sub print_login {
  753:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  754:     my ($css_class,$datatable);
  755:     my %choices = &login_choices();
  756: 
  757:     if ($caller eq 'service') {
  758:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  759:         my $choice = $choices{'disallowlogin'};
  760:         $css_class = ' class="LC_odd_row"';
  761:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  762:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  763:                       '<th>'.$choices{'server'}.'</th>'.
  764:                       '<th>'.$choices{'serverpath'}.'</th>'.
  765:                       '<th>'.$choices{'custompath'}.'</th>'.
  766:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  767:         my %disallowed;
  768:         if (ref($settings) eq 'HASH') {
  769:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  770:                %disallowed = %{$settings->{'loginvia'}};
  771:             }
  772:         }
  773:         foreach my $lonhost (sort(keys(%servers))) {
  774:             my $direct = 'selected="selected"';
  775:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  776:                 if ($disallowed{$lonhost}{'server'} ne '') {
  777:                     $direct = '';
  778:                 }
  779:             }
  780:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  781:                           '<td><select name="'.$lonhost.'_server">'.
  782:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  783:                           '</option>';
  784:             foreach my $hostid (keys(%servers)) {
  785:                 next if ($servers{$hostid} eq $servers{$lonhost});
  786:                 my $selected = '';
  787:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  788:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  789:                         $selected = 'selected="selected"';
  790:                     }
  791:                 }
  792:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  793:                               $servers{$hostid}.'</option>';
  794:             }
  795:             $datatable .= '</select></td>'.
  796:                           '<td><select name="'.$lonhost.'_serverpath">';
  797:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  798:                 my $pathname = $path;
  799:                 if ($path eq 'custom') {
  800:                     $pathname = &mt('Custom Path').' ->';
  801:                 }
  802:                 my $selected = '';
  803:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  804:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  805:                         $selected = 'selected="selected"';
  806:                     }
  807:                 } elsif ($path eq '') {
  808:                     $selected = 'selected="selected"';
  809:                 }
  810:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  811:             }
  812:             $datatable .= '</select></td>';
  813:             my ($custom,$exempt);
  814:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  815:                 $custom = $disallowed{$lonhost}{'custompath'};
  816:                 $exempt = $disallowed{$lonhost}{'exempt'};
  817:             }
  818:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  819:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  820:                           '</tr>';
  821:         }
  822:         $datatable .= '</table></td></tr>';
  823:         return $datatable;
  824:     } elsif ($caller eq 'page') {
  825:         my %defaultchecked = ( 
  826:                                'coursecatalog' => 'on',
  827:                                'adminmail'     => 'off',
  828:                                'newuser'       => 'off',
  829:                              );
  830:         my @toggles = ('coursecatalog','adminmail','newuser');
  831:         my (%checkedon,%checkedoff);
  832:         foreach my $item (@toggles) {
  833:             if ($defaultchecked{$item} eq 'on') { 
  834:                 $checkedon{$item} = ' checked="checked" ';
  835:                 $checkedoff{$item} = ' ';
  836:             } elsif ($defaultchecked{$item} eq 'off') {
  837:                 $checkedoff{$item} = ' checked="checked" ';
  838:                 $checkedon{$item} = ' ';
  839:             }
  840:         }
  841:         my @images = ('img','logo','domlogo','login');
  842:         my @logintext = ('textcol','bgcol');
  843:         my @bgs = ('pgbg','mainbg','sidebg');
  844:         my @links = ('link','alink','vlink');
  845:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  846:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  847:         my (%is_custom,%designs);
  848:         my %defaults = (
  849:                        font => $defaultdesign{'login.font'},
  850:                        );
  851:         foreach my $item (@images) {
  852:             $defaults{$item} = $defaultdesign{'login.'.$item};
  853:             $defaults{'showlogo'}{$item} = 1;
  854:         }
  855:         foreach my $item (@bgs) {
  856:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  857:         }
  858:         foreach my $item (@logintext) {
  859:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  860:         }
  861:         foreach my $item (@links) {
  862:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  863:         }
  864:         if (ref($settings) eq 'HASH') {
  865:             foreach my $item (@toggles) {
  866:                 if ($settings->{$item} eq '1') {
  867:                     $checkedon{$item} =  ' checked="checked" ';
  868:                     $checkedoff{$item} = ' ';
  869:                 } elsif ($settings->{$item} eq '0') {
  870:                     $checkedoff{$item} =  ' checked="checked" ';
  871:                     $checkedon{$item} = ' ';
  872:                 }
  873:             }
  874:             foreach my $item (@images) {
  875:                 if (defined($settings->{$item})) {
  876:                     $designs{$item} = $settings->{$item};
  877:                     $is_custom{$item} = 1;
  878:                 }
  879:                 if (defined($settings->{'showlogo'}{$item})) {
  880:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  881:                 }
  882:             }
  883:             foreach my $item (@logintext) {
  884:                 if ($settings->{$item} ne '') {
  885:                     $designs{'logintext'}{$item} = $settings->{$item};
  886:                     $is_custom{$item} = 1;
  887:                 }
  888:             }
  889:             if ($settings->{'font'} ne '') {
  890:                 $designs{'font'} = $settings->{'font'};
  891:                 $is_custom{'font'} = 1;
  892:             }
  893:             foreach my $item (@bgs) {
  894:                 if ($settings->{$item} ne '') {
  895:                     $designs{'bgs'}{$item} = $settings->{$item};
  896:                     $is_custom{$item} = 1;
  897:                 }
  898:             }
  899:             foreach my $item (@links) {
  900:                 if ($settings->{$item} ne '') {
  901:                     $designs{'links'}{$item} = $settings->{$item};
  902:                     $is_custom{$item} = 1;
  903:                 }
  904:             }
  905:         } else {
  906:             if ($designhash{$dom.'.login.font'} ne '') {
  907:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  908:                 $is_custom{'font'} = 1;
  909:             }
  910:             foreach my $item (@images) {
  911:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  912:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  913:                     $is_custom{$item} = 1;
  914:                 }
  915:             }
  916:             foreach my $item (@bgs) {
  917:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  918:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  919:                     $is_custom{$item} = 1;
  920:                 }
  921:             }
  922:             foreach my $item (@links) {
  923:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  924:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  925:                     $is_custom{$item} = 1;
  926:                 }
  927:             }
  928:         }
  929:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  930:                                                       logo => 'Institution Logo',
  931:                                                       domlogo => 'Domain Logo',
  932:                                                       login => 'Login box');
  933:         my $itemcount = 1;
  934:         foreach my $item (@toggles) {
  935:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
  936:             $datatable .=  
  937:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  938:                 '</td><td>'.
  939:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
  940:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
  941:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
  942:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
  943:                 '</tr>';
  944:             $itemcount ++;
  945:         }
  946:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
  947:         $datatable .= '</tr></table></td></tr>';
  948:     } elsif ($caller eq 'help') {
  949:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
  950:         my $switchserver = &check_switchserver($dom,$confname);
  951:         my $itemcount = 1;
  952:         $defaulturl = '/adm/loginproblems.html';
  953:         $defaulttype = 'default';
  954:         %lt = &Apache::lonlocal::texthash (
  955:                      del     => 'Delete?',
  956:                      rep     => 'Replace:',
  957:                      upl     => 'Upload:',
  958:                      default => 'Default',
  959:                      custom  => 'Custom',
  960:                                              );
  961:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
  962:         my @currlangs;
  963:         if (ref($settings) eq 'HASH') {
  964:             if (ref($settings->{'helpurl'}) eq 'HASH') {
  965:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
  966:                     next if ($settings->{'helpurl'}{$key} eq '');
  967:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
  968:                     $type{$key} = 'custom';
  969:                     unless ($key eq 'nolang') {
  970:                         push(@currlangs,$key);
  971:                     }
  972:                 }
  973:             } elsif ($settings->{'helpurl'} ne '') {
  974:                 $type{'nolang'} = 'custom';
  975:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
  976:             }
  977:         }
  978:         foreach my $lang ('nolang',sort(@currlangs)) {
  979:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
  980:             $datatable .= '<tr'.$css_class.'>';
  981:             if ($url{$lang} eq '') {
  982:                 $url{$lang} = $defaulturl;
  983:             }
  984:             if ($type{$lang} eq '') {
  985:                 $type{$lang} = $defaulttype;
  986:             }
  987:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
  988:             if ($lang eq 'nolang') {
  989:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
  990:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
  991:             } else {
  992:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
  993:                                   $langchoices{$lang},
  994:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
  995:             }
  996:             $datatable .= '</span></td>'."\n".
  997:                           '<td class="LC_left_item">';
  998:             if ($type{$lang} eq 'custom') {
  999:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1000:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1001:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1002:             } else {
 1003:                 $datatable .= $lt{'upl'};
 1004:             }
 1005:             $datatable .='<br />';
 1006:             if ($switchserver) {
 1007:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1008:             } else {
 1009:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1010:             }
 1011:             $datatable .= '</td></tr>';
 1012:             $itemcount ++;
 1013:         }
 1014:         my @addlangs;
 1015:         foreach my $lang (sort(keys(%langchoices))) {
 1016:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1017:             push(@addlangs,$lang);
 1018:         }
 1019:         if (@addlangs > 0) {
 1020:             my %toadd;
 1021:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1022:             $toadd{''} = &mt('Select');
 1023:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1024:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1025:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1026:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1027:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1028:             if ($switchserver) {
 1029:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1030:             } else {
 1031:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1032:             }
 1033:             $datatable .= '</td></tr>';
 1034:             $itemcount ++;
 1035:         }
 1036:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1037:     }
 1038:     return $datatable;
 1039: }
 1040: 
 1041: sub login_choices {
 1042:     my %choices =
 1043:         &Apache::lonlocal::texthash (
 1044:             coursecatalog => 'Display Course/Community Catalog link?',
 1045:             adminmail     => "Display Administrator's E-mail Address?",
 1046:             disallowlogin => "Login page requests redirected",
 1047:             hostid        => "Server",
 1048:             server        => "Redirect to:",
 1049:             serverpath    => "Path",
 1050:             custompath    => "Custom", 
 1051:             exempt        => "Exempt IP(s)",
 1052:             directlogin   => "No redirect",
 1053:             newuser       => "Link to create a user account",
 1054:             img           => "Header",
 1055:             logo          => "Main Logo",
 1056:             domlogo       => "Domain Logo",
 1057:             login         => "Log-in Header", 
 1058:             textcol       => "Text color",
 1059:             bgcol         => "Box color",
 1060:             bgs           => "Background colors",
 1061:             links         => "Link colors",
 1062:             font          => "Font color",
 1063:             pgbg          => "Header",
 1064:             mainbg        => "Page",
 1065:             sidebg        => "Login box",
 1066:             link          => "Link",
 1067:             alink         => "Active link",
 1068:             vlink         => "Visited link",
 1069:         );
 1070:     return %choices;
 1071: }
 1072: 
 1073: sub print_rolecolors {
 1074:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1075:     my %choices = &color_font_choices();
 1076:     my @bgs = ('pgbg','tabbg','sidebg');
 1077:     my @links = ('link','alink','vlink');
 1078:     my @images = ('img');
 1079:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1080:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1081:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1082:     my (%is_custom,%designs);
 1083:     my %defaults = (
 1084:                    img => $defaultdesign{$role.'.img'},
 1085:                    font => $defaultdesign{$role.'.font'},
 1086: 		   fontmenu => $defaultdesign{$role.'.fontmenu'},
 1087:                    );
 1088:     foreach my $item (@bgs) {
 1089:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1090:     }
 1091:     foreach my $item (@links) {
 1092:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1093:     }
 1094:     if (ref($settings) eq 'HASH') {
 1095:         if (ref($settings->{$role}) eq 'HASH') {
 1096:             if ($settings->{$role}->{'img'} ne '') {
 1097:                 $designs{'img'} = $settings->{$role}->{'img'};
 1098:                 $is_custom{'img'} = 1;
 1099:             }
 1100:             if ($settings->{$role}->{'font'} ne '') {
 1101:                 $designs{'font'} = $settings->{$role}->{'font'};
 1102:                 $is_custom{'font'} = 1;
 1103:             }
 1104:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1105:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1106:                 $is_custom{'fontmenu'} = 1;
 1107:             }
 1108:             foreach my $item (@bgs) {
 1109:                 if ($settings->{$role}->{$item} ne '') {
 1110:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1111:                     $is_custom{$item} = 1;
 1112:                 }
 1113:             }
 1114:             foreach my $item (@links) {
 1115:                 if ($settings->{$role}->{$item} ne '') {
 1116:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1117:                     $is_custom{$item} = 1;
 1118:                 }
 1119:             }
 1120:         }
 1121:     } else {
 1122:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1123:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1124:             $is_custom{'img'} = 1;
 1125:         }
 1126:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1127:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1128:             $is_custom{'fontmenu'} = 1; 
 1129:         }
 1130:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1131:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1132:             $is_custom{'font'} = 1;
 1133:         }
 1134:         foreach my $item (@bgs) {
 1135:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1136:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1137:                 $is_custom{$item} = 1;
 1138:             
 1139:             }
 1140:         }
 1141:         foreach my $item (@links) {
 1142:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1143:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1144:                 $is_custom{$item} = 1;
 1145:             }
 1146:         }
 1147:     }
 1148:     my $itemcount = 1;
 1149:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1150:     $datatable .= '</tr></table></td></tr>';
 1151:     return $datatable;
 1152: }
 1153: 
 1154: sub display_color_options {
 1155:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1156:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1157:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1158:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1159:     my $datatable = '<tr'.$css_class.'>'.
 1160:         '<td>'.$choices->{'font'}.'</td>';
 1161:     if (!$is_custom->{'font'}) {
 1162:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1163:     } else {
 1164:         $datatable .= '<td>&nbsp;</td>';
 1165:     }
 1166:     my $fontlink = &color_pick($phase,$role,'font',$choices->{'font'},$designs->{'font'});
 1167:     $datatable .= '<td><span class="LC_nobreak">'.
 1168:                   '<input type="text" size="10" name="'.$role.'_font"'.
 1169:                   ' value="'.$designs->{'font'}.'" />&nbsp;'.$fontlink.
 1170:                   '&nbsp;<span id="css_'.$role.'_font" style="background-color: '.
 1171:                   $designs->{'font'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1172:                   '</span></td></tr>';
 1173:     unless ($role eq 'login') { 
 1174:         $datatable .= '<tr'.$css_class.'>'.
 1175:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1176:         if (!$is_custom->{'fontmenu'}) {
 1177:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1178:         } else {
 1179:             $datatable .= '<td>&nbsp;</td>';
 1180:         }
 1181:         $fontlink = &color_pick($phase,$role,'fontmenu',$choices->{'fontmenu'},$designs->{'fontmenu'});
 1182:         $datatable .= '<td><span class="LC_nobreak">'.
 1183:                       '<input type="text" size="10" name="'.$role.'_fontmenu"'.
 1184:                       ' value="'.$designs->{'fontmenu'}.'" />&nbsp;'.$fontlink.
 1185:                       '&nbsp;<span id="css_'.$role.'_fontmenu" style="background-color: '.
 1186:                       $designs->{'fontmenu'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1187:                       '</span></td></tr>';
 1188:     }
 1189:     my $switchserver = &check_switchserver($dom,$confname);
 1190:     foreach my $img (@{$images}) {
 1191: 	$itemcount ++;
 1192:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1193:         $datatable .= '<tr'.$css_class.'>'.
 1194:                       '<td>'.$choices->{$img};
 1195:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1196:         if ($role eq 'login') {
 1197:             if ($img eq 'login') {
 1198:                 $login_hdr_pick =
 1199:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1200:                 $logincolors =
 1201:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1202:                                             $designs);
 1203:             } elsif ($img ne 'domlogo') {
 1204:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1205:             }
 1206:         }
 1207:         $datatable .= '</td>';
 1208:         if ($designs->{$img} ne '') {
 1209:             $imgfile = $designs->{$img};
 1210: 	    $img_import = ($imgfile =~ m{^/adm/});
 1211:         } else {
 1212:             $imgfile = $defaults->{$img};
 1213:         }
 1214:         if ($imgfile) {
 1215:             my ($showfile,$fullsize);
 1216:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1217:                 my $urldir = $1;
 1218:                 my $filename = $2;
 1219:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1220:                 if (@info) {
 1221:                     my $thumbfile = 'tn-'.$filename;
 1222:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1223:                     if (@thumb) {
 1224:                         $showfile = $urldir.'/'.$thumbfile;
 1225:                     } else {
 1226:                         $showfile = $imgfile;
 1227:                     }
 1228:                 } else {
 1229:                     $showfile = '';
 1230:                 }
 1231:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1232:                 $showfile = $imgfile;
 1233:                 my $imgdir = $1;
 1234:                 my $filename = $2;
 1235:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1236:                     $showfile = "/$imgdir/tn-".$filename;
 1237:                 } else {
 1238:                     my $input = $londocroot.$imgfile;
 1239:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1240:                     if (!-e $output) {
 1241:                         my ($width,$height) = &thumb_dimensions();
 1242:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1243:                         if ($fullwidth ne '' && $fullheight ne '') {
 1244:                             if ($fullwidth > $width && $fullheight > $height) { 
 1245:                                 my $size = $width.'x'.$height;
 1246:                                 system("convert -sample $size $input $output");
 1247:                                 $showfile = "/$imgdir/tn-".$filename;
 1248:                             }
 1249:                         }
 1250:                     }
 1251:                 }
 1252:             }
 1253:             if ($showfile) {
 1254:                 if ($showfile =~ m{^/(adm|res)/}) {
 1255:                     if ($showfile =~ m{^/res/}) {
 1256:                         my $local_showfile =
 1257:                             &Apache::lonnet::filelocation('',$showfile);
 1258:                         &Apache::lonnet::repcopy($local_showfile);
 1259:                     }
 1260:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1261:                 }
 1262:                 if ($imgfile) {
 1263:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1264:                         if ($imgfile =~ m{^/res/}) {
 1265:                             my $local_imgfile =
 1266:                                 &Apache::lonnet::filelocation('',$imgfile);
 1267:                             &Apache::lonnet::repcopy($local_imgfile);
 1268:                         }
 1269:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1270:                     } else {
 1271:                         $fullsize = $imgfile;
 1272:                     }
 1273:                 }
 1274:                 $datatable .= '<td>';
 1275:                 if ($img eq 'login') {
 1276:                     $datatable .= $login_hdr_pick;
 1277:                 } 
 1278:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1279:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1280:             } else {
 1281:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1282:                               &mt('Upload:');
 1283:             }
 1284:         } else {
 1285:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1286:                           &mt('Upload:');
 1287:         }
 1288:         if ($switchserver) {
 1289:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1290:         } else {
 1291:             if ($img ne 'login') { # suppress file selection for Log-in header
 1292:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1293:             }
 1294:         }
 1295:         $datatable .= '</td></tr>';
 1296:     }
 1297:     $itemcount ++;
 1298:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1299:     $datatable .= '<tr'.$css_class.'>'.
 1300:                   '<td>'.$choices->{'bgs'}.'</td>';
 1301:     my $bgs_def;
 1302:     foreach my $item (@{$bgs}) {
 1303:         if (!$is_custom->{$item}) {
 1304:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span id="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 1305:         }
 1306:     }
 1307:     if ($bgs_def) {
 1308:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1309:     } else {
 1310:         $datatable .= '<td>&nbsp;</td>';
 1311:     }
 1312:     $datatable .= '<td class="LC_right_item">'.
 1313:                   '<table border="0"><tr>';
 1314:     foreach my $item (@{$bgs}) {
 1315:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'bgs'}{$item});
 1316:         $datatable .= '<td align="center">'.$link;
 1317:         if ($designs->{'bgs'}{$item}) {
 1318:             $datatable .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1319:         }
 1320:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'bgs'}{$item}.
 1321:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1322:     }
 1323:     $datatable .= '</tr></table></td></tr>';
 1324:     $itemcount ++;
 1325:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1326:     $datatable .= '<tr'.$css_class.'>'.
 1327:                   '<td>'.$choices->{'links'}.'</td>';
 1328:     my $links_def;
 1329:     foreach my $item (@{$links}) {
 1330:         if (!$is_custom->{$item}) {
 1331:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1332:         }
 1333:     }
 1334:     if ($links_def) {
 1335:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1336:     } else {
 1337:         $datatable .= '<td>&nbsp;</td>';
 1338:     }
 1339:     $datatable .= '<td class="LC_right_item">'.
 1340:                   '<table border="0"><tr>';
 1341:     foreach my $item (@{$links}) {
 1342:         $datatable .= '<td align="center">'."\n".
 1343:                       &color_pick($phase,$role,$item,$choices->{$item},
 1344:                                   $designs->{'links'}{$item});
 1345:         if ($designs->{'links'}{$item}) {
 1346:             $datatable.='&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'links'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1347:         }
 1348:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'links'}{$item}.
 1349:                       '" /></td>';
 1350:     }
 1351:     $$rowtotal += $itemcount;
 1352:     return $datatable;
 1353: }
 1354: 
 1355: sub logo_display_options {
 1356:     my ($img,$defaults,$designs) = @_;
 1357:     my $checkedon;
 1358:     if (ref($defaults) eq 'HASH') {
 1359:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1360:             if ($defaults->{'showlogo'}{$img}) {
 1361:                 $checkedon = 'checked="checked" ';     
 1362:             }
 1363:         } 
 1364:     }
 1365:     if (ref($designs) eq 'HASH') {
 1366:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1367:             if (defined($designs->{'showlogo'}{$img})) {
 1368:                 if ($designs->{'showlogo'}{$img} == 0) {
 1369:                     $checkedon = '';
 1370:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1371:                     $checkedon = 'checked="checked" ';
 1372:                 }
 1373:             }
 1374:         }
 1375:     }
 1376:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1377:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1378:            &mt('show').'</label>'."\n";
 1379: }
 1380: 
 1381: sub login_header_options  {
 1382:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1383:     my $output = '';
 1384:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1385:         $output .= &mt('Text default(s):').'<br />';
 1386:         if (!$is_custom->{'textcol'}) {
 1387:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1388:                        '&nbsp;&nbsp;&nbsp;';
 1389:         }
 1390:         if (!$is_custom->{'bgcol'}) {
 1391:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1392:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1393:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1394:         }
 1395:         $output .= '<br />';
 1396:     }
 1397:     $output .='<br />';
 1398:     return $output;
 1399: }
 1400: 
 1401: sub login_text_colors {
 1402:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1403:     my $color_menu = '<table border="0"><tr>';
 1404:     foreach my $item (@{$logintext}) {
 1405:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1406:         $color_menu .= '<td align="center">'.$link;
 1407:         if ($designs->{'logintext'}{$item}) {
 1408:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1409:         }
 1410:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1411:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1412:                        '<td>&nbsp;</td>';
 1413:     }
 1414:     $color_menu .= '</tr></table><br />';
 1415:     return $color_menu;
 1416: }
 1417: 
 1418: sub image_changes {
 1419:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1420:     my $output;
 1421:     if ($img eq 'login') {
 1422:             # suppress image for Log-in header
 1423:     } elsif (!$is_custom) {
 1424:         if ($img ne 'domlogo') {
 1425:             $output .= &mt('Default image:').'<br />';
 1426:         } else {
 1427:             $output .= &mt('Default in use:').'<br />';
 1428:         }
 1429:     }
 1430:     if ($img eq 'login') { # suppress image for Log-in header
 1431:         $output .= '<td>'.$logincolors;
 1432:     } else {
 1433:         if ($img_import) {
 1434:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1435:         }
 1436:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1437:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1438:         if ($is_custom) {
 1439:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1440:                        '<input type="checkbox" name="'.
 1441:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1442:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1443:         } else {
 1444:             $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1445:         }
 1446:     }
 1447:     return $output;
 1448: }
 1449: 
 1450: sub color_pick {
 1451:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1452:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1453:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1454:                ');">'.$desc.'</a>';
 1455:     return $link;
 1456: }
 1457: 
 1458: sub print_quotas {
 1459:     my ($dom,$settings,$rowtotal,$action) = @_;
 1460:     my $context;
 1461:     if ($action eq 'quotas') {
 1462:         $context = 'tools';
 1463:     } else {
 1464:         $context = $action;
 1465:     }
 1466:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 1467:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1468:     my $typecount = 0;
 1469:     my ($css_class,%titles);
 1470:     if ($context eq 'requestcourses') {
 1471:         @usertools = ('official','unofficial','community');
 1472:         @options =('norequest','approval','validate','autolimit');
 1473:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1474:         %titles = &courserequest_titles();
 1475:     } elsif ($context eq 'requestauthor') {
 1476:         @usertools = ('author');
 1477:         @options = ('norequest','approval','automatic');
 1478:         %titles = &authorrequest_titles();
 1479:     } else {
 1480:         @usertools = ('aboutme','blog','webdav','portfolio');
 1481:         %titles = &tool_titles();
 1482:     }
 1483:     if (ref($types) eq 'ARRAY') {
 1484:         foreach my $type (@{$types}) {
 1485:             my $currdefquota;
 1486:             unless (($context eq 'requestcourses') ||
 1487:                     ($context eq 'requestauthor')) {
 1488:                 if (ref($settings) eq 'HASH') {
 1489:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1490:                         $currdefquota = $settings->{defaultquota}->{$type}; 
 1491:                     } else {
 1492:                         $currdefquota = $settings->{$type};
 1493:                     }
 1494:                 }
 1495:             }
 1496:             if (defined($usertypes->{$type})) {
 1497:                 $typecount ++;
 1498:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1499:                 $datatable .= '<tr'.$css_class.'>'.
 1500:                               '<td>'.$usertypes->{$type}.'</td>'.
 1501:                               '<td class="LC_left_item">';
 1502:                 if ($context eq 'requestcourses') {
 1503:                     $datatable .= '<table><tr>';
 1504:                 }
 1505:                 my %cell;  
 1506:                 foreach my $item (@usertools) {
 1507:                     if ($context eq 'requestcourses') {
 1508:                         my ($curroption,$currlimit);
 1509:                         if (ref($settings) eq 'HASH') {
 1510:                             if (ref($settings->{$item}) eq 'HASH') {
 1511:                                 $curroption = $settings->{$item}->{$type};
 1512:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1513:                                     $currlimit = $1; 
 1514:                                 }
 1515:                             }
 1516:                         }
 1517:                         if (!$curroption) {
 1518:                             $curroption = 'norequest';
 1519:                         }
 1520:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1521:                         foreach my $option (@options) {
 1522:                             my $val = $option;
 1523:                             if ($option eq 'norequest') {
 1524:                                 $val = 0;  
 1525:                             }
 1526:                             if ($option eq 'validate') {
 1527:                                 my $canvalidate = 0;
 1528:                                 if (ref($validations{$item}) eq 'HASH') { 
 1529:                                     if ($validations{$item}{$type}) {
 1530:                                         $canvalidate = 1;
 1531:                                     }
 1532:                                 }
 1533:                                 next if (!$canvalidate);
 1534:                             }
 1535:                             my $checked = '';
 1536:                             if ($option eq $curroption) {
 1537:                                 $checked = ' checked="checked"';
 1538:                             } elsif ($option eq 'autolimit') {
 1539:                                 if ($curroption =~ /^autolimit/) {
 1540:                                     $checked = ' checked="checked"';
 1541:                                 }                       
 1542:                             } 
 1543:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1544:                                   '<input type="radio" name="crsreq_'.$item.
 1545:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1546:                                   $titles{$option}.'</label>';
 1547:                             if ($option eq 'autolimit') {
 1548:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1549:                                                 $item.'_limit_'.$type.'" size="1" '.
 1550:                                                 'value="'.$currlimit.'" />';
 1551:                             }
 1552:                             $cell{$item} .= '</span> ';
 1553:                             if ($option eq 'autolimit') {
 1554:                                 $cell{$item} .= $titles{'unlimited'};
 1555:                             }
 1556:                         }
 1557:                     } elsif ($context eq 'requestauthor') {
 1558:                         my $curroption;
 1559:                         if (ref($settings) eq 'HASH') {
 1560:                             $curroption = $settings->{$type};
 1561:                         }
 1562:                         if (!$curroption) {
 1563:                             $curroption = 'norequest';
 1564:                         }
 1565:                         foreach my $option (@options) {
 1566:                             my $val = $option;
 1567:                             if ($option eq 'norequest') {
 1568:                                 $val = 0;
 1569:                             }
 1570:                             my $checked = '';
 1571:                             if ($option eq $curroption) {
 1572:                                 $checked = ' checked="checked"';
 1573:                             }
 1574:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1575:                                   '<input type="radio" name="authorreq_'.$type.
 1576:                                   '" value="'.$val.'"'.$checked.' />'.
 1577:                                   $titles{$option}.'</label></span>&nbsp; ';
 1578:                         }
 1579:                     } else {
 1580:                         my $checked = 'checked="checked" ';
 1581:                         if (ref($settings) eq 'HASH') {
 1582:                             if (ref($settings->{$item}) eq 'HASH') {
 1583:                                 if ($settings->{$item}->{$type} == 0) {
 1584:                                     $checked = '';
 1585:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1586:                                     $checked =  'checked="checked" ';
 1587:                                 }
 1588:                             }
 1589:                         }
 1590:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1591:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1592:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1593:                                       '</label></span>&nbsp; ';
 1594:                     }
 1595:                 }
 1596:                 if ($context eq 'requestcourses') {
 1597:                     $datatable .= '</tr><tr>';
 1598:                     foreach my $item (@usertools) {
 1599:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1600:                     }
 1601:                     $datatable .= '</tr></table>';
 1602:                 }
 1603:                 $datatable .= '</td>';
 1604:                 unless (($context eq 'requestcourses') ||
 1605:                         ($context eq 'requestauthor')) {
 1606:                     $datatable .= 
 1607:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1608:                               '<input type="text" name="quota_'.$type.
 1609:                               '" value="'.$currdefquota.
 1610:                               '" size="5" /> Mb</span></td>';
 1611:                 }
 1612:                 $datatable .= '</tr>';
 1613:             }
 1614:         }
 1615:     }
 1616:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1617:         $defaultquota = '20';
 1618:         if (ref($settings) eq 'HASH') {
 1619:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1620:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1621:             } elsif (defined($settings->{'default'})) {
 1622:                 $defaultquota = $settings->{'default'};
 1623:             }
 1624:         }
 1625:     }
 1626:     $typecount ++;
 1627:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1628:     $datatable .= '<tr'.$css_class.'>'.
 1629:                   '<td>'.$othertitle.'</td>'.
 1630:                   '<td class="LC_left_item">';
 1631:     if ($context eq 'requestcourses') {
 1632:         $datatable .= '<table><tr>';
 1633:     }
 1634:     my %defcell;
 1635:     foreach my $item (@usertools) {
 1636:         if ($context eq 'requestcourses') {
 1637:             my ($curroption,$currlimit);
 1638:             if (ref($settings) eq 'HASH') {
 1639:                 if (ref($settings->{$item}) eq 'HASH') {
 1640:                     $curroption = $settings->{$item}->{'default'};
 1641:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1642:                         $currlimit = $1;
 1643:                     }
 1644:                 }
 1645:             }
 1646:             if (!$curroption) {
 1647:                 $curroption = 'norequest';
 1648:             }
 1649:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1650:             foreach my $option (@options) {
 1651:                 my $val = $option;
 1652:                 if ($option eq 'norequest') {
 1653:                     $val = 0;
 1654:                 }
 1655:                 if ($option eq 'validate') {
 1656:                     my $canvalidate = 0;
 1657:                     if (ref($validations{$item}) eq 'HASH') {
 1658:                         if ($validations{$item}{'default'}) {
 1659:                             $canvalidate = 1;
 1660:                         }
 1661:                     }
 1662:                     next if (!$canvalidate);
 1663:                 }
 1664:                 my $checked = '';
 1665:                 if ($option eq $curroption) {
 1666:                     $checked = ' checked="checked"';
 1667:                 } elsif ($option eq 'autolimit') {
 1668:                     if ($curroption =~ /^autolimit/) {
 1669:                         $checked = ' checked="checked"';
 1670:                     }
 1671:                 }
 1672:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1673:                                   '<input type="radio" name="crsreq_'.$item.
 1674:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1675:                                   $titles{$option}.'</label>';
 1676:                 if ($option eq 'autolimit') {
 1677:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1678:                                        $item.'_limit_default" size="1" '.
 1679:                                        'value="'.$currlimit.'" />';
 1680:                 }
 1681:                 $defcell{$item} .= '</span> ';
 1682:                 if ($option eq 'autolimit') {
 1683:                     $defcell{$item} .= $titles{'unlimited'};
 1684:                 }
 1685:             }
 1686:         } elsif ($context eq 'requestauthor') {
 1687:             my $curroption;
 1688:             if (ref($settings) eq 'HASH') {
 1689:                 $curroption = $settings->{'default'};
 1690:             }
 1691:             if (!$curroption) {
 1692:                 $curroption = 'norequest';
 1693:             }
 1694:             foreach my $option (@options) {
 1695:                 my $val = $option;
 1696:                 if ($option eq 'norequest') {
 1697:                     $val = 0;
 1698:                 }
 1699:                 my $checked = '';
 1700:                 if ($option eq $curroption) {
 1701:                     $checked = ' checked="checked"';
 1702:                 }
 1703:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1704:                               '<input type="radio" name="authorreq_default"'.
 1705:                               ' value="'.$val.'"'.$checked.' />'.
 1706:                               $titles{$option}.'</label></span>&nbsp; ';
 1707:             }
 1708:         } else {
 1709:             my $checked = 'checked="checked" ';
 1710:             if (ref($settings) eq 'HASH') {
 1711:                 if (ref($settings->{$item}) eq 'HASH') {
 1712:                     if ($settings->{$item}->{'default'} == 0) {
 1713:                         $checked = '';
 1714:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1715:                         $checked = 'checked="checked" ';
 1716:                     }
 1717:                 }
 1718:             }
 1719:             $datatable .= '<span class="LC_nobreak"><label>'.
 1720:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1721:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1722:                           '</label></span>&nbsp; ';
 1723:         }
 1724:     }
 1725:     if ($context eq 'requestcourses') {
 1726:         $datatable .= '</tr><tr>';
 1727:         foreach my $item (@usertools) {
 1728:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1729:         }
 1730:         $datatable .= '</tr></table>';
 1731:     }
 1732:     $datatable .= '</td>';
 1733:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1734:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1735:                       '<input type="text" name="defaultquota" value="'.
 1736:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1737:     }
 1738:     $datatable .= '</tr>';
 1739:     $typecount ++;
 1740:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1741:     $datatable .= '<tr'.$css_class.'>'.
 1742:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1743:     if ($context eq 'requestcourses') {
 1744:         $datatable .= &mt('(overrides affiliation, if set)').
 1745:                       '</td>'.
 1746:                       '<td class="LC_left_item">'.
 1747:                       '<table><tr>';
 1748:     } else {
 1749:         $datatable .= &mt('(overrides affiliation, if checked)').
 1750:                       '</td>'.
 1751:                       '<td class="LC_left_item" colspan="2">'.
 1752:                       '<br />';
 1753:     }
 1754:     my %advcell;
 1755:     foreach my $item (@usertools) {
 1756:         if ($context eq 'requestcourses') {
 1757:             my ($curroption,$currlimit);
 1758:             if (ref($settings) eq 'HASH') {
 1759:                 if (ref($settings->{$item}) eq 'HASH') {
 1760:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1761:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1762:                         $currlimit = $1;
 1763:                     }
 1764:                 }
 1765:             }
 1766:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1767:             my $checked = '';
 1768:             if ($curroption eq '') {
 1769:                 $checked = ' checked="checked"';
 1770:             }
 1771:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1772:                                '<input type="radio" name="crsreq_'.$item.
 1773:                                '__LC_adv" value=""'.$checked.' />'.
 1774:                                &mt('No override set').'</label></span>&nbsp; ';
 1775:             foreach my $option (@options) {
 1776:                 my $val = $option;
 1777:                 if ($option eq 'norequest') {
 1778:                     $val = 0;
 1779:                 }
 1780:                 if ($option eq 'validate') {
 1781:                     my $canvalidate = 0;
 1782:                     if (ref($validations{$item}) eq 'HASH') {
 1783:                         if ($validations{$item}{'_LC_adv'}) {
 1784:                             $canvalidate = 1;
 1785:                         }
 1786:                     }
 1787:                     next if (!$canvalidate);
 1788:                 }
 1789:                 my $checked = '';
 1790:                 if ($val eq $curroption) {
 1791:                     $checked = ' checked="checked"';
 1792:                 } elsif ($option eq 'autolimit') {
 1793:                     if ($curroption =~ /^autolimit/) {
 1794:                         $checked = ' checked="checked"';
 1795:                     }
 1796:                 }
 1797:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1798:                                   '<input type="radio" name="crsreq_'.$item.
 1799:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1800:                                   $titles{$option}.'</label>';
 1801:                 if ($option eq 'autolimit') {
 1802:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1803:                                        $item.'_limit__LC_adv" size="1" '.
 1804:                                        'value="'.$currlimit.'" />';
 1805:                 }
 1806:                 $advcell{$item} .= '</span> ';
 1807:                 if ($option eq 'autolimit') {
 1808:                     $advcell{$item} .= $titles{'unlimited'};
 1809:                 }
 1810:             }
 1811:         } elsif ($context eq 'requestauthor') {
 1812:             my $curroption;
 1813:             if (ref($settings) eq 'HASH') {
 1814:                 $curroption = $settings->{'_LC_adv'};
 1815:             }
 1816:             my $checked = '';
 1817:             if ($curroption eq '') {
 1818:                 $checked = ' checked="checked"';
 1819:             }
 1820:             $datatable .= '<span class="LC_nobreak"><label>'.
 1821:                           '<input type="radio" name="authorreq__LC_adv"'.
 1822:                           ' value=""'.$checked.' />'.
 1823:                           &mt('No override set').'</label></span>&nbsp; ';
 1824:             foreach my $option (@options) {
 1825:                 my $val = $option;
 1826:                 if ($option eq 'norequest') {
 1827:                     $val = 0;
 1828:                 }
 1829:                 my $checked = '';
 1830:                 if ($val eq $curroption) {
 1831:                     $checked = ' checked="checked"';
 1832:                 }
 1833:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1834:                               '<input type="radio" name="authorreq__LC_adv"'.
 1835:                               ' value="'.$val.'"'.$checked.' />'.
 1836:                               $titles{$option}.'</label></span>&nbsp; ';
 1837:             }
 1838:         } else {
 1839:             my $checked = 'checked="checked" ';
 1840:             if (ref($settings) eq 'HASH') {
 1841:                 if (ref($settings->{$item}) eq 'HASH') {
 1842:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1843:                         $checked = '';
 1844:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1845:                         $checked = 'checked="checked" ';
 1846:                     }
 1847:                 }
 1848:             }
 1849:             $datatable .= '<span class="LC_nobreak"><label>'.
 1850:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1851:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1852:                           '</label></span>&nbsp; ';
 1853:         }
 1854:     }
 1855:     if ($context eq 'requestcourses') {
 1856:         $datatable .= '</tr><tr>';
 1857:         foreach my $item (@usertools) {
 1858:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1859:         }
 1860:         $datatable .= '</tr></table>';
 1861:     }
 1862:     $datatable .= '</td></tr>';
 1863:     $$rowtotal += $typecount;
 1864:     return $datatable;
 1865: }
 1866: 
 1867: sub print_requestmail {
 1868:     my ($dom,$action,$settings,$rowtotal) = @_;
 1869:     my ($now,$datatable,%dompersonnel,@domcoord,@currapproval,$rows);
 1870:     $now = time;
 1871:     $rows = 0;
 1872:     %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 1873:     foreach my $server (keys(%dompersonnel)) {
 1874:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 1875:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 1876:             if (!grep(/^$uname:$udom$/,@domcoord)) {
 1877:                 push(@domcoord,$uname.':'.$udom);
 1878:             }
 1879:         }
 1880:     }
 1881:     if (ref($settings) eq 'HASH') {
 1882:         if (ref($settings->{'notify'}) eq 'HASH') {
 1883:             if ($settings->{'notify'}{'approval'} ne '') {
 1884:                @currapproval = split(',',$settings->{'notify'}{'approval'});
 1885:             }
 1886:         }
 1887:     }
 1888:     if (@currapproval) {
 1889:         foreach my $dc (@currapproval) {
 1890:             unless (grep(/^\Q$dc\E$/,@domcoord)) {
 1891:                 push(@domcoord,$dc);
 1892:             }
 1893:         }
 1894:     }
 1895:     @domcoord = sort(@domcoord);
 1896:     my $numinrow = 4;
 1897:     my $numdc = @domcoord;
 1898:     my $css_class = 'class="LC_odd_row"';
 1899:     my $text;
 1900:     if ($action eq 'requestcourses') {
 1901:         $text = &mt('Receive notification of course requests requiring approval');
 1902:     } else {
 1903:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1904:     }
 1905:     $datatable = '<tr '.$css_class.'>'.
 1906:                  ' <td>'.$text.'</td>'.
 1907:                  ' <td class="LC_left_item">';
 1908:     if (@domcoord > 0) {
 1909:         $datatable .= '<table>';
 1910:         for (my $i=0; $i<$numdc; $i++) {
 1911:             my $rem = $i%($numinrow);
 1912:             if ($rem == 0) {
 1913:                 if ($i > 0) {
 1914:                     $datatable .= '</tr>';
 1915:                 }
 1916:                 $datatable .= '<tr>';
 1917:                 $rows ++;
 1918:             }
 1919:             my $check = ' ';
 1920:             if (grep(/^\Q$domcoord[$i]\E$/,@currapproval)) {
 1921:                 $check = ' checked="checked" ';
 1922:             }
 1923:             my ($uname,$udom) = split(':',$domcoord[$i]);
 1924:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
 1925:             if ($i == $numdc-1) {
 1926:                 my $colsleft = $numinrow-$rem;
 1927:                 if ($colsleft > 1) {
 1928:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 1929:                 } else {
 1930:                     $datatable .= '<td class="LC_left_item">';
 1931:                 }
 1932:             } else {
 1933:                 $datatable .= '<td class="LC_left_item">';
 1934:             }
 1935:             $datatable .= '<span class="LC_nobreak"><label>'.
 1936:                           '<input type="checkbox" name="reqapprovalnotify" '.
 1937:                           'value="'.$domcoord[$i].'"'.$check.'/>'.
 1938:                           $fullname.'</label></span></td>';
 1939:         }
 1940:         $datatable .= '</tr></table>';
 1941:     } else {
 1942:         $datatable .= &mt('There are no active Domain Coordinators');
 1943:         $rows ++;
 1944:     }
 1945:     $datatable .='</td></tr>';
 1946:     $$rowtotal += $rows;
 1947:     return $datatable;
 1948: }
 1949: 
 1950: sub print_autoenroll {
 1951:     my ($dom,$settings,$rowtotal) = @_;
 1952:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1953:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1954:     if (ref($settings) eq 'HASH') {
 1955:         if (exists($settings->{'run'})) {
 1956:             if ($settings->{'run'} eq '0') {
 1957:                 $runoff = ' checked="checked" ';
 1958:                 $runon = ' ';
 1959:             } else {
 1960:                 $runon = ' checked="checked" ';
 1961:                 $runoff = ' ';
 1962:             }
 1963:         } else {
 1964:             if ($autorun) {
 1965:                 $runon = ' checked="checked" ';
 1966:                 $runoff = ' ';
 1967:             } else {
 1968:                 $runoff = ' checked="checked" ';
 1969:                 $runon = ' ';
 1970:             }
 1971:         }
 1972:         if (exists($settings->{'co-owners'})) {
 1973:             if ($settings->{'co-owners'} eq '0') {
 1974:                 $coownersoff = ' checked="checked" ';
 1975:                 $coownerson = ' ';
 1976:             } else {
 1977:                 $coownerson = ' checked="checked" ';
 1978:                 $coownersoff = ' ';
 1979:             }
 1980:         } else {
 1981:             $coownersoff = ' checked="checked" ';
 1982:             $coownerson = ' ';
 1983:         }
 1984:         if (exists($settings->{'sender_domain'})) {
 1985:             $defdom = $settings->{'sender_domain'};
 1986:         }
 1987:     } else {
 1988:         if ($autorun) {
 1989:             $runon = ' checked="checked" ';
 1990:             $runoff = ' ';
 1991:         } else {
 1992:             $runoff = ' checked="checked" ';
 1993:             $runon = ' ';
 1994:         }
 1995:     }
 1996:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 1997:     my $notif_sender;
 1998:     if (ref($settings) eq 'HASH') {
 1999:         $notif_sender = $settings->{'sender_uname'};
 2000:     }
 2001:     my $datatable='<tr class="LC_odd_row">'.
 2002:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2003:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2004:                   '<input type="radio" name="autoenroll_run"'.
 2005:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2006:                   '<label><input type="radio" name="autoenroll_run"'.
 2007:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2008:                   '</tr><tr>'.
 2009:                   '<td>'.&mt('Notification messages - sender').
 2010:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2011:                   &mt('username').':&nbsp;'.
 2012:                   '<input type="text" name="sender_uname" value="'.
 2013:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2014:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2015:                   '<tr class="LC_odd_row">'.
 2016:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2017:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2018:                   '<input type="radio" name="autoassign_coowners"'.
 2019:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2020:                   '<label><input type="radio" name="autoassign_coowners"'.
 2021:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2022:                   '</tr>';
 2023:     $$rowtotal += 3;
 2024:     return $datatable;
 2025: }
 2026: 
 2027: sub print_autoupdate {
 2028:     my ($position,$dom,$settings,$rowtotal) = @_;
 2029:     my $datatable;
 2030:     if ($position eq 'top') {
 2031:         my $updateon = ' ';
 2032:         my $updateoff = ' checked="checked" ';
 2033:         my $classlistson = ' ';
 2034:         my $classlistsoff = ' checked="checked" ';
 2035:         if (ref($settings) eq 'HASH') {
 2036:             if ($settings->{'run'} eq '1') {
 2037:                 $updateon = $updateoff;
 2038:                 $updateoff = ' ';
 2039:             }
 2040:             if ($settings->{'classlists'} eq '1') {
 2041:                 $classlistson = $classlistsoff;
 2042:                 $classlistsoff = ' ';
 2043:             }
 2044:         }
 2045:         my %title = (
 2046:                    run => 'Auto-update active?',
 2047:                    classlists => 'Update information in classlists?',
 2048:                     );
 2049:         $datatable = '<tr class="LC_odd_row">'. 
 2050:                   '<td>'.&mt($title{'run'}).'</td>'.
 2051:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2052:                   '<input type="radio" name="autoupdate_run"'.
 2053:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2054:                   '<label><input type="radio" name="autoupdate_run"'.
 2055:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2056:                   '</tr><tr>'.
 2057:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2058:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2059:                   '<label><input type="radio" name="classlists"'.
 2060:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2061:                   '<label><input type="radio" name="classlists"'.
 2062:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2063:                   '</tr>';
 2064:         $$rowtotal += 2;
 2065:     } elsif ($position eq 'middle') {
 2066:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2067:         my $numinrow = 3;
 2068:         my $locknamesettings;
 2069:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2070:                                      $dom,$numinrow,$othertitle,
 2071:                                     'lockablenames');
 2072:         $$rowtotal ++;
 2073:     } else {
 2074:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2075:         my @fields = ('lastname','firstname','middlename','generation',
 2076:                       'permanentemail','id');
 2077:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2078:         my $numrows = 0;
 2079:         if (ref($types) eq 'ARRAY') {
 2080:             if (@{$types} > 0) {
 2081:                 $datatable = 
 2082:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2083:                                          \@fields,$types,\$numrows);
 2084:                     $$rowtotal += @{$types}; 
 2085:             }
 2086:         }
 2087:         $datatable .= 
 2088:             &usertype_update_row($settings,{'default' => $othertitle},
 2089:                                  \%fieldtitles,\@fields,['default'],
 2090:                                  \$numrows);
 2091:         $$rowtotal ++;     
 2092:     }
 2093:     return $datatable;
 2094: }
 2095: 
 2096: sub print_autocreate {
 2097:     my ($dom,$settings,$rowtotal) = @_;
 2098:     my (%createon,%createoff);
 2099:     my $curr_dc;
 2100:     my @types = ('xml','req');
 2101:     if (ref($settings) eq 'HASH') {
 2102:         foreach my $item (@types) {
 2103:             $createoff{$item} = ' checked="checked" ';
 2104:             $createon{$item} = ' ';
 2105:             if (exists($settings->{$item})) {
 2106:                 if ($settings->{$item}) {
 2107:                     $createon{$item} = ' checked="checked" ';
 2108:                     $createoff{$item} = ' ';
 2109:                 }
 2110:             }
 2111:         }
 2112:         $curr_dc = $settings->{'xmldc'};
 2113:     } else {
 2114:         foreach my $item (@types) {
 2115:             $createoff{$item} = ' checked="checked" ';
 2116:             $createon{$item} = ' ';
 2117:         }
 2118:     }
 2119:     $$rowtotal += 2;
 2120:     my $datatable='<tr class="LC_odd_row">'.
 2121:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2122:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2123:                   '<input type="radio" name="autocreate_xml"'.
 2124:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2125:                   '<label><input type="radio" name="autocreate_xml"'.
 2126:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2127:                   '</td></tr><tr>'.
 2128:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2129:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2130:                   '<input type="radio" name="autocreate_req"'.
 2131:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2132:                   '<label><input type="radio" name="autocreate_req"'.
 2133:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2134:     my ($numdc,$dctable) = &active_dc_picker($dom,$curr_dc);
 2135:     if ($numdc > 1) {
 2136:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2137:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2138:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2139:         $$rowtotal ++ ;
 2140:     } else {
 2141:         $datatable .= $dctable.'</td></tr>';
 2142:     }
 2143:     return $datatable;
 2144: }
 2145: 
 2146: sub print_directorysrch {
 2147:     my ($dom,$settings,$rowtotal) = @_;
 2148:     my $srchon = ' ';
 2149:     my $srchoff = ' checked="checked" ';
 2150:     my ($exacton,$containson,$beginson);
 2151:     my $localon = ' ';
 2152:     my $localoff = ' checked="checked" ';
 2153:     if (ref($settings) eq 'HASH') {
 2154:         if ($settings->{'available'} eq '1') {
 2155:             $srchon = $srchoff;
 2156:             $srchoff = ' ';
 2157:         }
 2158:         if ($settings->{'localonly'} eq '1') {
 2159:             $localon = $localoff;
 2160:             $localoff = ' ';
 2161:         }
 2162:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2163:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2164:                 if ($type eq 'exact') {
 2165:                     $exacton = ' checked="checked" ';
 2166:                 } elsif ($type eq 'contains') {
 2167:                     $containson = ' checked="checked" ';
 2168:                 } elsif ($type eq 'begins') {
 2169:                     $beginson = ' checked="checked" ';
 2170:                 }
 2171:             }
 2172:         } else {
 2173:             if ($settings->{'searchtypes'} eq 'exact') {
 2174:                 $exacton = ' checked="checked" ';
 2175:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2176:                 $containson = ' checked="checked" ';
 2177:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2178:                 $exacton = ' checked="checked" ';
 2179:                 $containson = ' checked="checked" ';
 2180:             }
 2181:         }
 2182:     }
 2183:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2184:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2185: 
 2186:     my $numinrow = 4;
 2187:     my $cansrchrow = 0;
 2188:     my $datatable='<tr class="LC_odd_row">'.
 2189:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2190:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2191:                   '<input type="radio" name="dirsrch_available"'.
 2192:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2193:                   '<label><input type="radio" name="dirsrch_available"'.
 2194:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2195:                   '</tr><tr>'.
 2196:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2197:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2198:                   '<input type="radio" name="dirsrch_localonly"'.
 2199:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2200:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2201:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2202:                   '</tr>';
 2203:     $$rowtotal += 2;
 2204:     if (ref($usertypes) eq 'HASH') {
 2205:         if (keys(%{$usertypes}) > 0) {
 2206:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2207:                                          $numinrow,$othertitle,'cansearch');
 2208:             $cansrchrow = 1;
 2209:         }
 2210:     }
 2211:     if ($cansrchrow) {
 2212:         $$rowtotal ++;
 2213:         $datatable .= '<tr>';
 2214:     } else {
 2215:         $datatable .= '<tr class="LC_odd_row">';
 2216:     }
 2217:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2218:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2219:     foreach my $title (@{$titleorder}) {
 2220:         if (defined($searchtitles->{$title})) {
 2221:             my $check = ' ';
 2222:             if (ref($settings) eq 'HASH') {
 2223:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2224:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2225:                         $check = ' checked="checked" ';
 2226:                     }
 2227:                 }
 2228:             }
 2229:             $datatable .= '<td class="LC_left_item">'.
 2230:                           '<span class="LC_nobreak"><label>'.
 2231:                           '<input type="checkbox" name="searchby" '.
 2232:                           'value="'.$title.'"'.$check.'/>'.
 2233:                           $searchtitles->{$title}.'</label></span></td>';
 2234:         }
 2235:     }
 2236:     $datatable .= '</tr></table></td></tr>';
 2237:     $$rowtotal ++;
 2238:     if ($cansrchrow) {
 2239:         $datatable .= '<tr class="LC_odd_row">';
 2240:     } else {
 2241:         $datatable .= '<tr>';
 2242:     }
 2243:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2244:                   '<td class="LC_left_item" colspan="2">'.
 2245:                   '<span class="LC_nobreak"><label>'.
 2246:                   '<input type="checkbox" name="searchtypes" '.
 2247:                   $exacton.' value="exact" />'.&mt('Exact match').
 2248:                   '</label>&nbsp;'.
 2249:                   '<label><input type="checkbox" name="searchtypes" '.
 2250:                   $beginson.' value="begins" />'.&mt('Begins with').
 2251:                   '</label>&nbsp;'.
 2252:                   '<label><input type="checkbox" name="searchtypes" '.
 2253:                   $containson.' value="contains" />'.&mt('Contains').
 2254:                   '</label></span></td></tr>';
 2255:     $$rowtotal ++;
 2256:     return $datatable;
 2257: }
 2258: 
 2259: sub print_contacts {
 2260:     my ($dom,$settings,$rowtotal) = @_;
 2261:     my $datatable;
 2262:     my @contacts = ('adminemail','supportemail');
 2263:     my (%checked,%to,%otheremails,%bccemails);
 2264:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2265:                     'requestsmail');
 2266:     foreach my $type (@mailings) {
 2267:         $otheremails{$type} = '';
 2268:     }
 2269:     $bccemails{'helpdeskmail'} = '';
 2270:     if (ref($settings) eq 'HASH') {
 2271:         foreach my $item (@contacts) {
 2272:             if (exists($settings->{$item})) {
 2273:                 $to{$item} = $settings->{$item};
 2274:             }
 2275:         }
 2276:         foreach my $type (@mailings) {
 2277:             if (exists($settings->{$type})) {
 2278:                 if (ref($settings->{$type}) eq 'HASH') {
 2279:                     foreach my $item (@contacts) {
 2280:                         if ($settings->{$type}{$item}) {
 2281:                             $checked{$type}{$item} = ' checked="checked" ';
 2282:                         }
 2283:                     }
 2284:                     $otheremails{$type} = $settings->{$type}{'others'};
 2285:                     if ($type eq 'helpdeskmail') {
 2286:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2287:                     }
 2288:                 }
 2289:             } elsif ($type eq 'lonstatusmail') {
 2290:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2291:             }
 2292:         }
 2293:     } else {
 2294:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2295:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2296:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2297:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2298:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2299:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2300:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2301:     }
 2302:     my ($titles,$short_titles) = &contact_titles();
 2303:     my $rownum = 0;
 2304:     my $css_class;
 2305:     foreach my $item (@contacts) {
 2306:         $rownum ++;
 2307:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2308:         $datatable .= '<tr'.$css_class.'>'. 
 2309:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2310:                   '</span></td><td class="LC_right_item">'.
 2311:                   '<input type="text" name="'.$item.'" value="'.
 2312:                   $to{$item}.'" /></td></tr>';
 2313:     }
 2314:     foreach my $type (@mailings) {
 2315:         $rownum ++;
 2316:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2317:         $datatable .= '<tr'.$css_class.'>'.
 2318:                       '<td><span class="LC_nobreak">'.
 2319:                       $titles->{$type}.': </span></td>'.
 2320:                       '<td class="LC_left_item">'.
 2321:                       '<span class="LC_nobreak">';
 2322:         foreach my $item (@contacts) {
 2323:             $datatable .= '<label>'.
 2324:                           '<input type="checkbox" name="'.$type.'"'.
 2325:                           $checked{$type}{$item}.
 2326:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2327:                           '</label>&nbsp;';
 2328:         }
 2329:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2330:                       '<input type="text" name="'.$type.'_others" '.
 2331:                       'value="'.$otheremails{$type}.'"  />';
 2332:         if ($type eq 'helpdeskmail') {
 2333:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2334:                           '<input type="text" name="'.$type.'_bcc" '.
 2335:                           'value="'.$bccemails{$type}.'"  />';
 2336:         }
 2337:         $datatable .= '</td></tr>'."\n";
 2338:     }
 2339:     $$rowtotal += $rownum;
 2340:     return $datatable;
 2341: }
 2342: 
 2343: sub print_helpsettings {
 2344:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2345:     my ($datatable,$itemcount);
 2346:     $itemcount = 1;
 2347:     my (%choices,%defaultchecked,@toggles);
 2348:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2349:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2350:                                  &mt('LON-CAPA bug tracker'),600,500));
 2351:     %defaultchecked = ('submitbugs' => 'on');
 2352:     @toggles = ('submitbugs',);
 2353: 
 2354:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2355:                                                  \%choices,$itemcount);
 2356:     return $datatable;
 2357: }
 2358: 
 2359: sub radiobutton_prefs {
 2360:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount) = @_;
 2361:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2362:                    (ref($choices) eq 'HASH'));
 2363: 
 2364:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2365: 
 2366:     foreach my $item (@{$toggles}) {
 2367:         if ($defaultchecked->{$item} eq 'on') {
 2368:             $checkedon{$item} = ' checked="checked" ';
 2369:             $checkedoff{$item} = ' ';
 2370:         } elsif ($defaultchecked->{$item} eq 'off') {
 2371:             $checkedoff{$item} = ' checked="checked" ';
 2372:             $checkedon{$item} = ' ';
 2373:         }
 2374:     }
 2375:     if (ref($settings) eq 'HASH') {
 2376:         foreach my $item (@{$toggles}) {
 2377:             if ($settings->{$item} eq '1') {
 2378:                 $checkedon{$item} =  ' checked="checked" ';
 2379:                 $checkedoff{$item} = ' ';
 2380:             } elsif ($settings->{$item} eq '0') {
 2381:                 $checkedoff{$item} =  ' checked="checked" ';
 2382:                 $checkedon{$item} = ' ';
 2383:             }
 2384:         }
 2385:     }
 2386:     foreach my $item (@{$toggles}) {
 2387:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2388:         $datatable .=
 2389:             '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices->{$item}.
 2390:             '</span></td>'.
 2391:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2392:             '<label><input type="radio" name="'.
 2393:             $item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').
 2394:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2395:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2396:             '</span></td>'.
 2397:             '</tr>';
 2398:         $itemcount ++;
 2399:     }
 2400:     return ($datatable,$itemcount);
 2401: }
 2402: 
 2403: sub print_coursedefaults {
 2404:     my ($position,$dom,$settings,$rowtotal) = @_;
 2405:     my ($css_class,$datatable);
 2406:     my $itemcount = 1;
 2407:     if ($position eq 'top') {
 2408:         my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2409:         %choices =
 2410:             &Apache::lonlocal::texthash (
 2411:                 canuse_pdfforms => 'Course/Community users can create/upload PDF forms',
 2412:         );
 2413:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2414:         @toggles = ('canuse_pdfforms',);
 2415:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2416:                                                  \%choices,$itemcount);
 2417:         $$rowtotal += $itemcount;
 2418:     } else {
 2419:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2420:         my %choices =
 2421:             &Apache::lonlocal::texthash (
 2422:                 anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2423:         );
 2424:         my $currdefresponder;
 2425:         if (ref($settings) eq 'HASH') {
 2426:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2427:         }
 2428:         if (!$currdefresponder) {
 2429:             $currdefresponder = 10;
 2430:         } elsif ($currdefresponder < 1) {
 2431:             $currdefresponder = 1;
 2432:         }
 2433:         $datatable .=
 2434:                '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices{'anonsurvey_threshold'}.
 2435:                 '</span></td>'.
 2436:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2437:                 '<input type="text" name="anonsurvey_threshold"'.
 2438:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2439:                 '</td></tr>';
 2440:     }
 2441:     return $datatable;
 2442: }
 2443: 
 2444: sub print_usersessions {
 2445:     my ($position,$dom,$settings,$rowtotal) = @_;
 2446:     my ($css_class,$datatable,%checked,%choices);
 2447:     my (%by_ip,%by_location,@intdoms);
 2448:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2449: 
 2450:     my @alldoms = &Apache::lonnet::all_domains();
 2451:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2452:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2453:     my %altids = &id_for_thisdom(%servers);
 2454:     my $itemcount = 1;
 2455:     if ($position eq 'top') {
 2456:         if (keys(%serverhomes) > 1) {
 2457:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2458:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2459:         } else {
 2460:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2461:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2462:         }
 2463:     } else {
 2464:         if (keys(%by_location) == 0) {
 2465:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2466:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2467:         } else {
 2468:             my %lt = &usersession_titles();
 2469:             my $numinrow = 5;
 2470:             my $prefix;
 2471:             my @types;
 2472:             if ($position eq 'bottom') {
 2473:                 $prefix = 'remote';
 2474:                 @types = ('version','excludedomain','includedomain');
 2475:             } else {
 2476:                 $prefix = 'hosted';
 2477:                 @types = ('excludedomain','includedomain');
 2478:             }
 2479:             my (%current,%checkedon,%checkedoff);
 2480:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2481:             my @locations = sort(keys(%by_location));
 2482:             foreach my $type (@types) {
 2483:                 $checkedon{$type} = '';
 2484:                 $checkedoff{$type} = ' checked="checked"';
 2485:             }
 2486:             if (ref($settings) eq 'HASH') {
 2487:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2488:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2489:                         $current{$key} = $settings->{$prefix}{$key};
 2490:                         if ($key eq 'version') {
 2491:                             if ($current{$key} ne '') {
 2492:                                 $checkedon{$key} = ' checked="checked"';
 2493:                                 $checkedoff{$key} = '';
 2494:                             }
 2495:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2496:                             $checkedon{$key} = ' checked="checked"';
 2497:                             $checkedoff{$key} = '';
 2498:                         }
 2499:                     }
 2500:                 }
 2501:             }
 2502:             foreach my $type (@types) {
 2503:                 next if ($type ne 'version' && !@locations);
 2504:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2505:                 $datatable .= '<tr'.$css_class.'>
 2506:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2507:                                <span class="LC_nobreak">&nbsp;
 2508:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2509:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2510:                 if ($type eq 'version') {
 2511:                     my $selector = '<select name="'.$prefix.'_version">';
 2512:                     foreach my $version (@lcversions) {
 2513:                         my $selected = '';
 2514:                         if ($current{'version'} eq $version) {
 2515:                             $selected = ' selected="selected"';
 2516:                         }
 2517:                         $selector .= ' <option value="'.$version.'"'.
 2518:                                      $selected.'>'.$version.'</option>';
 2519:                     }
 2520:                     $selector .= '</select> ';
 2521:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2522:                 } else {
 2523:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2524:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2525:                                  ' />'.('&nbsp;'x2).
 2526:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2527:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2528:                                  "\n".
 2529:                                  '</div><div><table>';
 2530:                     my $rem;
 2531:                     for (my $i=0; $i<@locations; $i++) {
 2532:                         my ($showloc,$value,$checkedtype);
 2533:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2534:                             my $ip = $by_location{$locations[$i]}->[0];
 2535:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2536:                                  $value = join(':',@{$by_ip{$ip}});
 2537:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2538:                                 if (ref($current{$type}) eq 'ARRAY') {
 2539:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2540:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2541:                                             $checkedtype = ' checked="checked"';
 2542:                                             last;
 2543:                                         }
 2544:                                     }
 2545:                                 }
 2546:                             }
 2547:                         }
 2548:                         $rem = $i%($numinrow);
 2549:                         if ($rem == 0) {
 2550:                             if ($i > 0) {
 2551:                                 $datatable .= '</tr>';
 2552:                             }
 2553:                             $datatable .= '<tr>';
 2554:                         }
 2555:                         $datatable .= '<td class="LC_left_item">'.
 2556:                                       '<span class="LC_nobreak"><label>'.
 2557:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2558:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2559:                                       '</label></span></td>';
 2560:                     }
 2561:                     $rem = @locations%($numinrow);
 2562:                     my $colsleft = $numinrow - $rem;
 2563:                     if ($colsleft > 1 ) {
 2564:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2565:                                       '&nbsp;</td>';
 2566:                     } elsif ($colsleft == 1) {
 2567:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2568:                     }
 2569:                     $datatable .= '</tr></table>';
 2570:                 }
 2571:                 $datatable .= '</td></tr>';
 2572:                 $itemcount ++;
 2573:             }
 2574:         }
 2575:     }
 2576:     $$rowtotal += $itemcount;
 2577:     return $datatable;
 2578: }
 2579: 
 2580: sub build_location_hashes {
 2581:     my ($intdoms,$by_ip,$by_location) = @_;
 2582:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2583:                   (ref($by_location) eq 'HASH')); 
 2584:     my %iphost = &Apache::lonnet::get_iphost();
 2585:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2586:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2587:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2588:         foreach my $id (@{$iphost{$primary_ip}}) {
 2589:             my $intdom = &Apache::lonnet::internet_dom($id);
 2590:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2591:                 push(@{$intdoms},$intdom);
 2592:             }
 2593:         }
 2594:     }
 2595:     foreach my $ip (keys(%iphost)) {
 2596:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2597:             foreach my $id (@{$iphost{$ip}}) {
 2598:                 my $location = &Apache::lonnet::internet_dom($id);
 2599:                 if ($location) {
 2600:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2601:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2602:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2603:                             push(@{$by_ip->{$ip}},$location);
 2604:                         }
 2605:                     } else {
 2606:                         $by_ip->{$ip} = [$location];
 2607:                     }
 2608:                 }
 2609:             }
 2610:         }
 2611:     }
 2612:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2613:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2614:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2615:             my $first = $by_ip->{$ip}->[0];
 2616:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2617:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2618:                     push(@{$by_location->{$first}},$ip);
 2619:                 }
 2620:             } else {
 2621:                 $by_location->{$first} = [$ip];
 2622:             }
 2623:         }
 2624:     }
 2625:     return;
 2626: }
 2627: 
 2628: sub current_offloads_to {
 2629:     my ($dom,$settings,$servers) = @_;
 2630:     my (%spareid,%otherdomconfigs);
 2631:     if (ref($servers) eq 'HASH') {
 2632:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2633:             my $gotspares;
 2634:             if (ref($settings) eq 'HASH') {
 2635:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2636:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2637:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2638:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2639:                         $gotspares = 1;
 2640:                     }
 2641:                 }
 2642:             }
 2643:             unless ($gotspares) {
 2644:                 my $gotspares;
 2645:                 my $serverhomeID =
 2646:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2647:                 my $serverhomedom =
 2648:                     &Apache::lonnet::host_domain($serverhomeID);
 2649:                 if ($serverhomedom ne $dom) {
 2650:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2651:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2652:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2653:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2654:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2655:                                 $gotspares = 1;
 2656:                             }
 2657:                         }
 2658:                     } else {
 2659:                         $otherdomconfigs{$serverhomedom} =
 2660:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2661:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2662:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2663:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2664:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2665:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2666:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2667:                                         $gotspares = 1;
 2668:                                     }
 2669:                                 }
 2670:                             }
 2671:                         }
 2672:                     }
 2673:                 }
 2674:             }
 2675:             unless ($gotspares) {
 2676:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2677:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2678:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2679:                } else {
 2680:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2681:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2682:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2683:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2684:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2685:                     } else {
 2686:                         my %what = (
 2687:                              spareid => 1,
 2688:                         );
 2689:                         my ($result,$returnhash) = 
 2690:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2691:                         if ($result eq 'ok') { 
 2692:                             if (ref($returnhash) eq 'HASH') {
 2693:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2694:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2695:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2696:                                 }
 2697:                             }
 2698:                         }
 2699:                     }
 2700:                 }
 2701:             }
 2702:         }
 2703:     }
 2704:     return %spareid;
 2705: }
 2706: 
 2707: sub spares_row {
 2708:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2709:     my $css_class;
 2710:     my $numinrow = 4;
 2711:     my $itemcount = 1;
 2712:     my $datatable;
 2713:     my %typetitles = &sparestype_titles();
 2714:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2715:         foreach my $server (sort(keys(%{$servers}))) {
 2716:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2717:             my ($othercontrol,$serverdom);
 2718:             if ($serverhome ne $server) {
 2719:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2720:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2721:             } else {
 2722:                 $serverdom = &Apache::lonnet::host_domain($server);
 2723:                 if ($serverdom ne $dom) {
 2724:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2725:                 }
 2726:             }
 2727:             next unless (ref($spareid->{$server}) eq 'HASH');
 2728:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2729:             $datatable .= '<tr'.$css_class.'>
 2730:                            <td rowspan="2">
 2731:                             <span class="LC_nobreak"><b>'.$server.'</b> when busy, offloads to:</span></td>'."\n";
 2732:             my (%current,%canselect);
 2733:             my @choices = 
 2734:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2735:             foreach my $type ('primary','default') {
 2736:                 if (ref($spareid->{$server}) eq 'HASH') {
 2737:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2738:                         my @spares = @{$spareid->{$server}{$type}};
 2739:                         if (@spares > 0) {
 2740:                             if ($othercontrol) {
 2741:                                 $current{$type} = join(', ',@spares);
 2742:                             } else {
 2743:                                 $current{$type} .= '<table>';
 2744:                                 my $numspares = scalar(@spares);
 2745:                                 for (my $i=0;  $i<@spares; $i++) {
 2746:                                     my $rem = $i%($numinrow);
 2747:                                     if ($rem == 0) {
 2748:                                         if ($i > 0) {
 2749:                                             $current{$type} .= '</tr>';
 2750:                                         }
 2751:                                         $current{$type} .= '<tr>';
 2752:                                     }
 2753:                                     $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'".');" />&nbsp;'.
 2754:                                                        $spareid->{$server}{$type}[$i].
 2755:                                                        '</label></td>'."\n";
 2756:                                 }
 2757:                                 my $rem = @spares%($numinrow);
 2758:                                 my $colsleft = $numinrow - $rem;
 2759:                                 if ($colsleft > 1 ) {
 2760:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2761:                                                        '" class="LC_left_item">'.
 2762:                                                        '&nbsp;</td>';
 2763:                                 } elsif ($colsleft == 1) {
 2764:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2765:                                 }
 2766:                                 $current{$type} .= '</tr></table>';
 2767:                             }
 2768:                         }
 2769:                     }
 2770:                     if ($current{$type} eq '') {
 2771:                         $current{$type} = &mt('None specified');
 2772:                     }
 2773:                     if ($othercontrol) {
 2774:                         if ($type eq 'primary') {
 2775:                             $canselect{$type} = $othercontrol;
 2776:                         }
 2777:                     } else {
 2778:                         $canselect{$type} = 
 2779:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2780:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2781:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2782:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2783:                         if (@choices > 0) {
 2784:                             foreach my $lonhost (@choices) {
 2785:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2786:                             }
 2787:                         }
 2788:                         $canselect{$type} .= '</select>'."\n";
 2789:                     }
 2790:                 } else {
 2791:                     $current{$type} = &mt('Could not be determined');
 2792:                     if ($type eq 'primary') {
 2793:                         $canselect{$type} =  $othercontrol;
 2794:                     }
 2795:                 }
 2796:                 if ($type eq 'default') {
 2797:                     $datatable .= '<tr'.$css_class.'>';
 2798:                 }
 2799:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2800:                               '<td>'.$current{$type}.'</td>'."\n".
 2801:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2802:             }
 2803:             $itemcount ++;
 2804:         }
 2805:     }
 2806:     $$rowtotal += $itemcount;
 2807:     return $datatable;
 2808: }
 2809: 
 2810: sub possible_newspares {
 2811:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2812:     my $serverhostname = &Apache::lonnet::hostname($server);
 2813:     my %excluded;
 2814:     if ($serverhostname ne '') {
 2815:         %excluded = (
 2816:                        $serverhostname => 1,
 2817:                     );
 2818:     }
 2819:     if (ref($currspares) eq 'HASH') {
 2820:         foreach my $type (keys(%{$currspares})) {
 2821:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2822:                 if (@{$currspares->{$type}} > 0) {
 2823:                     foreach my $curr (@{$currspares->{$type}}) {
 2824:                         my $hostname = &Apache::lonnet::hostname($curr);
 2825:                         $excluded{$hostname} = 1;
 2826:                     }
 2827:                 }
 2828:             }
 2829:         }
 2830:     }
 2831:     my @choices;
 2832:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2833:         if (keys(%{$serverhomes}) > 1) {
 2834:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2835:                 unless ($excluded{$name}) {
 2836:                     if (exists($altids->{$serverhomes->{$name}})) {
 2837:                         push(@choices,$altids->{$serverhomes->{$name}});
 2838:                     } else {
 2839:                         push(@choices,$serverhomes->{$name});
 2840:                     }
 2841:                 }
 2842:             }
 2843:         }
 2844:     }
 2845:     return sort(@choices);
 2846: }
 2847: 
 2848: sub print_loadbalancing {
 2849:     my ($dom,$settings,$rowtotal) = @_;
 2850:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2851:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2852:     my $numinrow = 1;
 2853:     my $datatable;
 2854:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2855:     my (%currbalancer,%currtargets,%currrules,%existing);
 2856:     if (ref($settings) eq 'HASH') {
 2857:         %existing = %{$settings};
 2858:     }
 2859:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 2860:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 2861:                                   \%currtargets,\%currrules);
 2862:     } else {
 2863:         return;
 2864:     }
 2865:     my ($othertitle,$usertypes,$types) =
 2866:         &Apache::loncommon::sorted_inst_types($dom);
 2867:     my $rownum = 6;
 2868:     if (ref($types) eq 'ARRAY') {
 2869:         $rownum += scalar(@{$types});
 2870:     }
 2871:     my @css_class = ('LC_odd_row','LC_even_row');
 2872:     my $balnum = 0;
 2873:     my $islast;
 2874:     my (@toshow,$disabledtext);
 2875:     if (keys(%currbalancer) > 0) {
 2876:         @toshow = sort(keys(%currbalancer));
 2877:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 2878:             push(@toshow,'');
 2879:         }
 2880:     } else {
 2881:         @toshow = ('');
 2882:         $disabledtext = &mt('No existing load balancer');
 2883:     }
 2884:     foreach my $lonhost (@toshow) {
 2885:         if ($balnum == scalar(@toshow)-1) {
 2886:             $islast = 1;
 2887:         } else {
 2888:             $islast = 0;
 2889:         }
 2890:         my $cssidx = $balnum%2;
 2891:         my $targets_div_style = 'display: none';
 2892:         my $disabled_div_style = 'display: block';
 2893:         my $homedom_div_style = 'display: none';
 2894:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 2895:                       '<td rowspan="'.$rownum.'" valign="top">'.
 2896:                       '<p>';
 2897:         if ($lonhost eq '') {
 2898:             $datatable .= '<span class="LC_nobreak">';
 2899:             if (keys(%currbalancer) > 0) {
 2900:                 $datatable .= &mt('Add balancer:');
 2901:             } else {
 2902:                 $datatable .= &mt('Enable balancer:');
 2903:             }
 2904:             $datatable .= '&nbsp;'.
 2905:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 2906:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 2907:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 2908:                           '<option value="" selected="selected">'.&mt('None').
 2909:                           '</option>'."\n";
 2910:             foreach my $server (sort(keys(%servers))) {
 2911:                 next if ($currbalancer{$server});
 2912:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 2913:             }
 2914:             $datatable .=
 2915:                 '</select>'."\n".
 2916:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 2917:         } else {
 2918:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 2919:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 2920:                            &mt('Stop balancing').'</label>'.
 2921:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 2922:             $targets_div_style = 'display: block';
 2923:             $disabled_div_style = 'display: none';
 2924:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 2925:                 $homedom_div_style = 'display: block';
 2926:             }
 2927:         }
 2928:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 2929:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 2930:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 2931:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2932:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 2933:         my @sparestypes = ('primary','default');
 2934:         my %typetitles = &sparestype_titles();
 2935:         foreach my $sparetype (@sparestypes) {
 2936:             my $targettable;
 2937:             for (my $i=0; $i<$numspares; $i++) {
 2938:                 my $checked;
 2939:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 2940:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 2941:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 2942:                             $checked = ' checked="checked"';
 2943:                         }
 2944:                     }
 2945:                 }
 2946:                 my ($chkboxval,$disabled);
 2947:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 2948:                     $chkboxval = $spares[$i];
 2949:                 }
 2950:                 if (exists($currbalancer{$spares[$i]})) {
 2951:                     $disabled = ' disabled="disabled"';
 2952:                 }
 2953:                 $targettable .=
 2954:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 2955:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 2956:                     '</span></label></td>';
 2957:                 my $rem = $i%($numinrow);
 2958:                 if ($rem == 0) {
 2959:                     if (($i > 0) && ($i < $numspares-1)) {
 2960:                         $targettable .= '</tr>';
 2961:                     }
 2962:                     if ($i < $numspares-1) {
 2963:                         $targettable .= '<tr>';
 2964:                     }
 2965:                 }
 2966:             }
 2967:             if ($targettable ne '') {
 2968:                 my $rem = $numspares%($numinrow);
 2969:                 my $colsleft = $numinrow - $rem;
 2970:                 if ($colsleft > 1 ) {
 2971:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2972:                                     '&nbsp;</td>';
 2973:                 } elsif ($colsleft == 1) {
 2974:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 2975:                 }
 2976:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 2977:                                '<table><tr>'.$targettable.'</tr></table><br />';
 2978:             }
 2979:         }
 2980:         my $cssidx = $balnum%2;
 2981:         $datatable .= '</div></td></tr>'.
 2982:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 2983:                                            $othertitle,$usertypes,$types,\%servers,
 2984:                                            \%currbalancer,$lonhost,
 2985:                                            $targets_div_style,$homedom_div_style,
 2986:                                            $css_class[$cssidx],$balnum,$islast);
 2987:         $$rowtotal += $rownum;
 2988:         $balnum ++;
 2989:     }
 2990:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 2991:     return $datatable;
 2992: }
 2993: 
 2994: sub get_loadbalancers_config {
 2995:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 2996:     return unless ((ref($servers) eq 'HASH') &&
 2997:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 2998:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 2999:     if (keys(%{$existing}) > 0) {
 3000:         my $oldlonhost;
 3001:         foreach my $key (sort(keys(%{$existing}))) {
 3002:             if ($key eq 'lonhost') {
 3003:                 $oldlonhost = $existing->{'lonhost'};
 3004:                 $currbalancer->{$oldlonhost} = 1;
 3005:             } elsif ($key eq 'targets') {
 3006:                 if ($oldlonhost) {
 3007:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3008:                 }
 3009:             } elsif ($key eq 'rules') {
 3010:                 if ($oldlonhost) {
 3011:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3012:                 }
 3013:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3014:                 $currbalancer->{$key} = 1;
 3015:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3016:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3017:             }
 3018:         }
 3019:     } else {
 3020:         my ($balancerref,$targetsref) =
 3021:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3022:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3023:             foreach my $server (sort(keys(%{$balancerref}))) {
 3024:                 $currbalancer->{$server} = 1;
 3025:                 $currtargets->{$server} = $targetsref->{$server};
 3026:             }
 3027:         }
 3028:     }
 3029:     return;
 3030: }
 3031: 
 3032: sub loadbalancing_rules {
 3033:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3034:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3035:         $css_class,$balnum,$islast) = @_;
 3036:     my $output;
 3037:     my $num = 0;
 3038:     my ($alltypes,$othertypes,$titles) =
 3039:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3040:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3041:         foreach my $type (@{$alltypes}) {
 3042:             $num ++;
 3043:             my $current;
 3044:             if (ref($currrules) eq 'HASH') {
 3045:                 $current = $currrules->{$type};
 3046:             }
 3047:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3048:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3049:                     $current = '';
 3050:                 }
 3051:             }
 3052:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3053:                                              $servers,$currbalancer,$lonhost,$dom,
 3054:                                              $targets_div_style,$homedom_div_style,
 3055:                                              $css_class,$balnum,$num,$islast);
 3056:         }
 3057:     }
 3058:     return $output;
 3059: }
 3060: 
 3061: sub loadbalancing_titles {
 3062:     my ($dom,$intdom,$usertypes,$types) = @_;
 3063:     my %othertypes = (
 3064:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3065:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3066:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3067:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3068:                      );
 3069:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 3070:     if (ref($types) eq 'ARRAY') {
 3071:         unshift(@alltypes,@{$types},'default');
 3072:     }
 3073:     my %titles;
 3074:     foreach my $type (@alltypes) {
 3075:         if ($type =~ /^_LC_/) {
 3076:             $titles{$type} = $othertypes{$type};
 3077:         } elsif ($type eq 'default') {
 3078:             $titles{$type} = &mt('All users from [_1]',$dom);
 3079:             if (ref($types) eq 'ARRAY') {
 3080:                 if (@{$types} > 0) {
 3081:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3082:                 }
 3083:             }
 3084:         } elsif (ref($usertypes) eq 'HASH') {
 3085:             $titles{$type} = $usertypes->{$type};
 3086:         }
 3087:     }
 3088:     return (\@alltypes,\%othertypes,\%titles);
 3089: }
 3090: 
 3091: sub loadbalance_rule_row {
 3092:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3093:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3094:     my @rulenames = ('default','homeserver');
 3095:     my %ruletitles = &offloadtype_text();
 3096:     if ($type eq '_LC_external') {
 3097:         push(@rulenames,'externalbalancer');
 3098:     } else {
 3099:         push(@rulenames,'specific');
 3100:     }
 3101:     push(@rulenames,'none');
 3102:     my $style = $targets_div_style;
 3103:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3104:         $style = $homedom_div_style;
 3105:     }
 3106:     my $space;
 3107:     if ($islast && $num == 1) {
 3108:         $space = '<div display="inline-block">&nbsp;</div>';
 3109:     }
 3110:     my $output =
 3111:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3112:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3113:         '<td valaign="top">'.$space.
 3114:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3115:     for (my $i=0; $i<@rulenames; $i++) {
 3116:         my $rule = $rulenames[$i];
 3117:         my ($checked,$extra);
 3118:         if ($rulenames[$i] eq 'default') {
 3119:             $rule = '';
 3120:         }
 3121:         if ($rulenames[$i] eq 'specific') {
 3122:             if (ref($servers) eq 'HASH') {
 3123:                 my $default;
 3124:                 if (($current ne '') && (exists($servers->{$current}))) {
 3125:                     $checked = ' checked="checked"';
 3126:                 }
 3127:                 unless ($checked) {
 3128:                     $default = ' selected="selected"';
 3129:                 }
 3130:                 $extra =
 3131:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3132:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3133:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3134:                     '<option value=""'.$default.'></option>'."\n";
 3135:                 foreach my $server (sort(keys(%{$servers}))) {
 3136:                     if (ref($currbalancer) eq 'HASH') {
 3137:                         next if (exists($currbalancer->{$server}));
 3138:                     }
 3139:                     my $selected;
 3140:                     if ($server eq $current) {
 3141:                         $selected = ' selected="selected"';
 3142:                     }
 3143:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3144:                 }
 3145:                 $extra .= '</select>';
 3146:             }
 3147:         } elsif ($rule eq $current) {
 3148:             $checked = ' checked="checked"';
 3149:         }
 3150:         $output .= '<span class="LC_nobreak"><label>'.
 3151:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3152:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3153:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3154:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3155:                    '</label>'.$extra.'</span><br />'."\n";
 3156:     }
 3157:     $output .= '</div></td></tr>'."\n";
 3158:     return $output;
 3159: }
 3160: 
 3161: sub offloadtype_text {
 3162:     my %ruletitles = &Apache::lonlocal::texthash (
 3163:            'default'          => 'Offloads to default destinations',
 3164:            'homeserver'       => "Offloads to user's home server",
 3165:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3166:            'specific'         => 'Offloads to specific server',
 3167:            'none'             => 'No offload',
 3168:     );
 3169:     return %ruletitles;
 3170: }
 3171: 
 3172: sub sparestype_titles {
 3173:     my %typestitles = &Apache::lonlocal::texthash (
 3174:                           'primary' => 'primary',
 3175:                           'default' => 'default',
 3176:                       );
 3177:     return %typestitles;
 3178: }
 3179: 
 3180: sub contact_titles {
 3181:     my %titles = &Apache::lonlocal::texthash (
 3182:                    'supportemail' => 'Support E-mail address',
 3183:                    'adminemail'   => 'Default Server Admin E-mail address',
 3184:                    'errormail'    => 'Error reports to be e-mailed to',
 3185:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3186:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3187:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3188:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3189:                  );
 3190:     my %short_titles = &Apache::lonlocal::texthash (
 3191:                            adminemail   => 'Admin E-mail address',
 3192:                            supportemail => 'Support E-mail',
 3193:                        );   
 3194:     return (\%titles,\%short_titles);
 3195: }
 3196: 
 3197: sub tool_titles {
 3198:     my %titles = &Apache::lonlocal::texthash (
 3199:                      aboutme    => 'Personal web page',
 3200:                      blog       => 'Blog',
 3201:                      webdav     => 'WebDAV',
 3202:                      portfolio  => 'Portfolio',
 3203:                      official   => 'Official courses (with institutional codes)',
 3204:                      unofficial => 'Unofficial courses',
 3205:                      community  => 'Communities',
 3206:                  );
 3207:     return %titles;
 3208: }
 3209: 
 3210: sub courserequest_titles {
 3211:     my %titles = &Apache::lonlocal::texthash (
 3212:                                    official   => 'Official',
 3213:                                    unofficial => 'Unofficial',
 3214:                                    community  => 'Communities',
 3215:                                    norequest  => 'Not allowed',
 3216:                                    approval   => 'Approval by Dom. Coord.',
 3217:                                    validate   => 'With validation',
 3218:                                    autolimit  => 'Numerical limit',
 3219:                                    unlimited  => '(blank for unlimited)',
 3220:                  );
 3221:     return %titles;
 3222: }
 3223: 
 3224: sub authorrequest_titles {
 3225:     my %titles = &Apache::lonlocal::texthash (
 3226:                                    norequest  => 'Not allowed',
 3227:                                    approval   => 'Approval by Dom. Coord.',
 3228:                                    automatic  => 'Automatic approval',
 3229:                  );
 3230:     return %titles;
 3231: }
 3232: 
 3233: sub courserequest_conditions {
 3234:     my %conditions = &Apache::lonlocal::texthash (
 3235:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3236:        validate   => '(Processing of request subject to instittutional validation).',
 3237:                  );
 3238:     return %conditions;
 3239: }
 3240: 
 3241: 
 3242: sub print_usercreation {
 3243:     my ($position,$dom,$settings,$rowtotal) = @_;
 3244:     my $numinrow = 4;
 3245:     my $datatable;
 3246:     if ($position eq 'top') {
 3247:         $$rowtotal ++;
 3248:         my $rowcount = 0;
 3249:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3250:         if (ref($rules) eq 'HASH') {
 3251:             if (keys(%{$rules}) > 0) {
 3252:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3253:                                                 $ruleorder,$numinrow,$rowcount);
 3254:                 $$rowtotal ++;
 3255:                 $rowcount ++;
 3256:             }
 3257:         }
 3258:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3259:         if (ref($idrules) eq 'HASH') {
 3260:             if (keys(%{$idrules}) > 0) {
 3261:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3262:                                                 $idruleorder,$numinrow,$rowcount);
 3263:                 $$rowtotal ++;
 3264:                 $rowcount ++;
 3265:             }
 3266:         }
 3267:         my ($emailrules,$emailruleorder) = 
 3268:             &Apache::lonnet::inst_userrules($dom,'email');
 3269:         if (ref($emailrules) eq 'HASH') {
 3270:             if (keys(%{$emailrules}) > 0) {
 3271:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3272:                                                 $emailruleorder,$numinrow,$rowcount);
 3273:                 $$rowtotal ++;
 3274:                 $rowcount ++;
 3275:             }
 3276:         }
 3277:         if ($rowcount == 0) {
 3278:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3279:             $$rowtotal ++;
 3280:             $rowcount ++;
 3281:         }
 3282:     } elsif ($position eq 'middle') {
 3283:         my @creators = ('author','course','requestcrs','selfcreate');
 3284:         my ($rules,$ruleorder) =
 3285:             &Apache::lonnet::inst_userrules($dom,'username');
 3286:         my %lt = &usercreation_types();
 3287:         my %checked;
 3288:         my @selfcreate; 
 3289:         if (ref($settings) eq 'HASH') {
 3290:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3291:                 foreach my $item (@creators) {
 3292:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3293:                 }
 3294:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3295:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3296:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3297:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3298:                         @selfcreate = ('email','login','sso');
 3299:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3300:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3301:                     }
 3302:                 }
 3303:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3304:                 foreach my $item (@creators) {
 3305:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3306:                         $checked{$item} = 'none';
 3307:                     }
 3308:                 }
 3309:             }
 3310:         }
 3311:         my $rownum = 0;
 3312:         foreach my $item (@creators) {
 3313:             $rownum ++;
 3314:             if ($item ne 'selfcreate') {  
 3315:                 if ($checked{$item} eq '') {
 3316:                     $checked{$item} = 'any';
 3317:                 }
 3318:             }
 3319:             my $css_class;
 3320:             if ($rownum%2) {
 3321:                 $css_class = '';
 3322:             } else {
 3323:                 $css_class = ' class="LC_odd_row" ';
 3324:             }
 3325:             $datatable .= '<tr'.$css_class.'>'.
 3326:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3327:                          '</span></td><td align="right">';
 3328:             my @options;
 3329:             if ($item eq 'selfcreate') {
 3330:                 push(@options,('email','login','sso'));
 3331:             } else {
 3332:                 @options = ('any');
 3333:                 if (ref($rules) eq 'HASH') {
 3334:                     if (keys(%{$rules}) > 0) {
 3335:                         push(@options,('official','unofficial'));
 3336:                     }
 3337:                 }
 3338:                 push(@options,'none');
 3339:             }
 3340:             foreach my $option (@options) {
 3341:                 my $type = 'radio';
 3342:                 my $check = ' ';
 3343:                 if ($item eq 'selfcreate') {
 3344:                     $type = 'checkbox';
 3345:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3346:                         $check = ' checked="checked" ';
 3347:                     }
 3348:                 } else {
 3349:                     if ($checked{$item} eq $option) {
 3350:                         $check = ' checked="checked" ';
 3351:                     }
 3352:                 } 
 3353:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3354:                               '<input type="'.$type.'" name="can_createuser_'.
 3355:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3356:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3357:             }
 3358:             $datatable .= '</td></tr>';
 3359:         }
 3360:         my ($othertitle,$usertypes,$types) =
 3361:             &Apache::loncommon::sorted_inst_types($dom);
 3362:         my $createsettings;
 3363:         if (ref($settings) eq 'HASH') {
 3364:             $createsettings = $settings->{cancreate};
 3365:         }
 3366:         if (ref($usertypes) eq 'HASH') {
 3367:             if (keys(%{$usertypes}) > 0) {
 3368:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3369:                                              $dom,$numinrow,$othertitle,
 3370:                                              'statustocreate');
 3371:                 $$rowtotal ++;
 3372:                 $rownum ++;
 3373:             }
 3374:         }
 3375:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3376:     } else {
 3377:         my @contexts = ('author','course','domain');
 3378:         my @authtypes = ('int','krb4','krb5','loc');
 3379:         my %checked;
 3380:         if (ref($settings) eq 'HASH') {
 3381:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3382:                 foreach my $item (@contexts) {
 3383:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3384:                         foreach my $auth (@authtypes) {
 3385:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3386:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3387:                             }
 3388:                         }
 3389:                     }
 3390:                 }
 3391:             }
 3392:         } else {
 3393:             foreach my $item (@contexts) {
 3394:                 foreach my $auth (@authtypes) {
 3395:                     $checked{$item}{$auth} = ' checked="checked" ';
 3396:                 }
 3397:             }
 3398:         }
 3399:         my %title = &context_names();
 3400:         my %authname = &authtype_names();
 3401:         my $rownum = 0;
 3402:         my $css_class; 
 3403:         foreach my $item (@contexts) {
 3404:             if ($rownum%2) {
 3405:                 $css_class = '';
 3406:             } else {
 3407:                 $css_class = ' class="LC_odd_row" ';
 3408:             }
 3409:             $datatable .=   '<tr'.$css_class.'>'.
 3410:                             '<td>'.$title{$item}.
 3411:                             '</td><td class="LC_left_item">'.
 3412:                             '<span class="LC_nobreak">';
 3413:             foreach my $auth (@authtypes) {
 3414:                 $datatable .= '<label>'. 
 3415:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3416:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3417:                               $authname{$auth}.'</label>&nbsp;';
 3418:             }
 3419:             $datatable .= '</span></td></tr>';
 3420:             $rownum ++;
 3421:         }
 3422:         $$rowtotal += $rownum;
 3423:     }
 3424:     return $datatable;
 3425: }
 3426: 
 3427: sub captcha_choice {
 3428:     my ($context,$settings,$itemcount) = @_;
 3429:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3430:     my %lt = &captcha_phrases();
 3431:     $keyentry = 'hidden';
 3432:     if ($context eq 'cancreate') {
 3433:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3434:     } elsif ($context eq 'login') {
 3435:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3436:     }
 3437:     if (ref($settings) eq 'HASH') {
 3438:         if ($settings->{'captcha'}) {
 3439:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3440:         } else {
 3441:             $checked{'original'} = ' checked="checked"';
 3442:         }
 3443:         if ($settings->{'captcha'} eq 'recaptcha') {
 3444:             $pubtext = $lt{'pub'};
 3445:             $privtext = $lt{'priv'};
 3446:             $keyentry = 'text';
 3447:         }
 3448:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3449:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3450:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3451:         }
 3452:     } else {
 3453:         $checked{'original'} = ' checked="checked"';
 3454:     }
 3455:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3456:     my $output = '<tr'.$css_class.'>'.
 3457:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3458:                  '<table><tr><td>'."\n";
 3459:     foreach my $option ('original','recaptcha','notused') {
 3460:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3461:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3462:                    $lt{$option}.'</label></span>';
 3463:         unless ($option eq 'notused') {
 3464:             $output .= ('&nbsp;'x2)."\n";
 3465:         }
 3466:     }
 3467: #
 3468: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3469: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3470: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 3471: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3472: #
 3473:     $output .= '</td></tr>'."\n".
 3474:                '<tr><td>'."\n".
 3475:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3476:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3477:                $currpub.'" size="40" /></span><br />'."\n".
 3478:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3479:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3480:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3481:                '</td></tr>';
 3482:     return $output;
 3483: }
 3484: 
 3485: sub user_formats_row {
 3486:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3487:     my $output;
 3488:     my %text = (
 3489:                    'username' => 'new usernames',
 3490:                    'id'       => 'IDs',
 3491:                    'email'    => 'self-created accounts (e-mail)',
 3492:                );
 3493:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3494:     $output = '<tr '.$css_class.'>'.
 3495:               '<td><span class="LC_nobreak">';
 3496:     if ($type eq 'email') {
 3497:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3498:     } else {
 3499:         $output .= &mt("Format rules to check for $text{$type}: ");
 3500:     }
 3501:     $output .= '</span></td>'.
 3502:                '<td class="LC_left_item" colspan="2"><table>';
 3503:     my $rem;
 3504:     if (ref($ruleorder) eq 'ARRAY') {
 3505:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3506:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3507:                 my $rem = $i%($numinrow);
 3508:                 if ($rem == 0) {
 3509:                     if ($i > 0) {
 3510:                         $output .= '</tr>';
 3511:                     }
 3512:                     $output .= '<tr>';
 3513:                 }
 3514:                 my $check = ' ';
 3515:                 if (ref($settings) eq 'HASH') {
 3516:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3517:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3518:                             $check = ' checked="checked" ';
 3519:                         }
 3520:                     }
 3521:                 }
 3522:                 $output .= '<td class="LC_left_item">'.
 3523:                            '<span class="LC_nobreak"><label>'.
 3524:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3525:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3526:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3527:             }
 3528:         }
 3529:         $rem = @{$ruleorder}%($numinrow);
 3530:     }
 3531:     my $colsleft = $numinrow - $rem;
 3532:     if ($colsleft > 1 ) {
 3533:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3534:                    '&nbsp;</td>';
 3535:     } elsif ($colsleft == 1) {
 3536:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3537:     }
 3538:     $output .= '</tr></table></td></tr>';
 3539:     return $output;
 3540: }
 3541: 
 3542: sub usercreation_types {
 3543:     my %lt = &Apache::lonlocal::texthash (
 3544:                     author     => 'When adding a co-author',
 3545:                     course     => 'When adding a user to a course',
 3546:                     requestcrs => 'When requesting a course',
 3547:                     selfcreate => 'User creates own account', 
 3548:                     any        => 'Any',
 3549:                     official   => 'Institutional only ',
 3550:                     unofficial => 'Non-institutional only',
 3551:                     email      => 'E-mail address',
 3552:                     login      => 'Institutional Login',
 3553:                     sso        => 'SSO', 
 3554:                     none       => 'None',
 3555:     );
 3556:     return %lt;
 3557: }
 3558: 
 3559: sub authtype_names {
 3560:     my %lt = &Apache::lonlocal::texthash(
 3561:                       int    => 'Internal',
 3562:                       krb4   => 'Kerberos 4',
 3563:                       krb5   => 'Kerberos 5',
 3564:                       loc    => 'Local',
 3565:                   );
 3566:     return %lt;
 3567: }
 3568: 
 3569: sub context_names {
 3570:     my %context_title = &Apache::lonlocal::texthash(
 3571:        author => 'Creating users when an Author',
 3572:        course => 'Creating users when in a course',
 3573:        domain => 'Creating users when a Domain Coordinator',
 3574:     );
 3575:     return %context_title;
 3576: }
 3577: 
 3578: sub print_usermodification {
 3579:     my ($position,$dom,$settings,$rowtotal) = @_;
 3580:     my $numinrow = 4;
 3581:     my ($context,$datatable,$rowcount);
 3582:     if ($position eq 'top') {
 3583:         $rowcount = 0;
 3584:         $context = 'author'; 
 3585:         foreach my $role ('ca','aa') {
 3586:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3587:                                                    $numinrow,$rowcount);
 3588:             $$rowtotal ++;
 3589:             $rowcount ++;
 3590:         }
 3591:     } elsif ($position eq 'middle') {
 3592:         $context = 'course';
 3593:         $rowcount = 0;
 3594:         foreach my $role ('st','ep','ta','in','cr') {
 3595:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3596:                                                    $numinrow,$rowcount);
 3597:             $$rowtotal ++;
 3598:             $rowcount ++;
 3599:         }
 3600:     } elsif ($position eq 'bottom') {
 3601:         $context = 'selfcreate';
 3602:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3603:         $usertypes->{'default'} = $othertitle;
 3604:         if (ref($types) eq 'ARRAY') {
 3605:             push(@{$types},'default');
 3606:             $usertypes->{'default'} = $othertitle;
 3607:             foreach my $status (@{$types}) {
 3608:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3609:                                                        $numinrow,$rowcount,$usertypes);
 3610:                 $$rowtotal ++;
 3611:                 $rowcount ++;
 3612:             }
 3613:         }
 3614:     }
 3615:     return $datatable;
 3616: }
 3617: 
 3618: sub print_defaults {
 3619:     my ($dom,$rowtotal) = @_;
 3620:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3621:                  'datelocale_def','portal_def');
 3622:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3623:     my $titles = &defaults_titles($dom);
 3624:     my $rownum = 0;
 3625:     my ($datatable,$css_class);
 3626:     foreach my $item (@items) {
 3627:         if ($rownum%2) {
 3628:             $css_class = '';
 3629:         } else {
 3630:             $css_class = ' class="LC_odd_row" ';
 3631:         }
 3632:         $datatable .= '<tr'.$css_class.'>'.
 3633:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3634:                   '</span></td><td class="LC_right_item">';
 3635:         if ($item eq 'auth_def') {
 3636:             my @authtypes = ('internal','krb4','krb5','localauth');
 3637:             my %shortauth = (
 3638:                              internal => 'int',
 3639:                              krb4 => 'krb4',
 3640:                              krb5 => 'krb5',
 3641:                              localauth  => 'loc'
 3642:                            );
 3643:             my %authnames = &authtype_names();
 3644:             foreach my $auth (@authtypes) {
 3645:                 my $checked = ' ';
 3646:                 if ($domdefaults{$item} eq $auth) {
 3647:                     $checked = ' checked="checked" ';
 3648:                 }
 3649:                 $datatable .= '<label><input type="radio" name="'.$item.
 3650:                               '" value="'.$auth.'"'.$checked.'/>'.
 3651:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3652:             }
 3653:         } elsif ($item eq 'timezone_def') {
 3654:             my $includeempty = 1;
 3655:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3656:         } elsif ($item eq 'datelocale_def') {
 3657:             my $includeempty = 1;
 3658:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3659:         } elsif ($item eq 'lang_def') {
 3660:             my %langchoices = &get_languages_hash();
 3661:             $langchoices{''} = 'No language preference';
 3662:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 3663:             $datatable .= &Apache::loncommon::select_form($domdefaults{$item},$item,
 3664:                                                           \%langchoices);
 3665:         } else {
 3666:             my $size;
 3667:             if ($item eq 'portal_def') {
 3668:                 $size = ' size="25"';
 3669:             }
 3670:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3671:                           $domdefaults{$item}.'"'.$size.' />';
 3672:         }
 3673:         $datatable .= '</td></tr>';
 3674:         $rownum ++;
 3675:     }
 3676:     $$rowtotal += $rownum;
 3677:     return $datatable;
 3678: }
 3679: 
 3680: sub get_languages_hash {
 3681:     my %langchoices;
 3682:     foreach my $id (&Apache::loncommon::languageids()) {
 3683:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 3684:         if ($code ne '') {
 3685:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 3686:         }
 3687:     }
 3688:     return %langchoices;
 3689: }
 3690: 
 3691: sub defaults_titles {
 3692:     my ($dom) = @_;
 3693:     my %titles = &Apache::lonlocal::texthash (
 3694:                    'auth_def'      => 'Default authentication type',
 3695:                    'auth_arg_def'  => 'Default authentication argument',
 3696:                    'lang_def'      => 'Default language',
 3697:                    'timezone_def'  => 'Default timezone',
 3698:                    'datelocale_def' => 'Default locale for dates',
 3699:                    'portal_def'     => 'Portal/Default URL',
 3700:                  );
 3701:     if ($dom) {
 3702:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3703:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3704:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3705:         $protocol = 'http' if ($protocol ne 'https');
 3706:         if ($uint_dom) {
 3707:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3708:                                          $uint_dom);
 3709:         }
 3710:     }
 3711:     return (\%titles);
 3712: }
 3713: 
 3714: sub print_scantronformat {
 3715:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3716:     my $itemcount = 1;
 3717:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3718:         %confhash);
 3719:     my $switchserver = &check_switchserver($dom,$confname);
 3720:     my %lt = &Apache::lonlocal::texthash (
 3721:                 default => 'Default bubblesheet format file error',
 3722:                 custom  => 'Custom bubblesheet format file error',
 3723:              );
 3724:     my %scantronfiles = (
 3725:         default => 'default.tab',
 3726:         custom => 'custom.tab',
 3727:     );
 3728:     foreach my $key (keys(%scantronfiles)) {
 3729:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3730:                               .$scantronfiles{$key};
 3731:     }
 3732:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3733:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3734:         if (!$switchserver) {
 3735:             my $servadm = $r->dir_config('lonAdmEMail');
 3736:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3737:             if ($configuserok eq 'ok') {
 3738:                 if ($author_ok eq 'ok') {
 3739:                     my %legacyfile = (
 3740:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3741:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3742:                     );
 3743:                     my %md5chk;
 3744:                     foreach my $type (keys(%legacyfile)) {
 3745:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3746:                         chomp($md5chk{$type});
 3747:                     }
 3748:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3749:                         foreach my $type (keys(%legacyfile)) {
 3750:                             ($scantronurls{$type},my $error) = 
 3751:                                 &legacy_scantronformat($r,$dom,$confname,
 3752:                                                  $type,$legacyfile{$type},
 3753:                                                  $scantronurls{$type},
 3754:                                                  $scantronfiles{$type});
 3755:                             if ($error ne '') {
 3756:                                 $error{$type} = $error;
 3757:                             }
 3758:                         }
 3759:                         if (keys(%error) == 0) {
 3760:                             $is_custom = 1;
 3761:                             $confhash{'scantron'}{'scantronformat'} = 
 3762:                                 $scantronurls{'custom'};
 3763:                             my $putresult = 
 3764:                                 &Apache::lonnet::put_dom('configuration',
 3765:                                                          \%confhash,$dom);
 3766:                             if ($putresult ne 'ok') {
 3767:                                 $error{'custom'} = 
 3768:                                     '<span class="LC_error">'.
 3769:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3770:                             }
 3771:                         }
 3772:                     } else {
 3773:                         ($scantronurls{'default'},my $error) =
 3774:                             &legacy_scantronformat($r,$dom,$confname,
 3775:                                           'default',$legacyfile{'default'},
 3776:                                           $scantronurls{'default'},
 3777:                                           $scantronfiles{'default'});
 3778:                         if ($error eq '') {
 3779:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3780:                             my $putresult =
 3781:                                 &Apache::lonnet::put_dom('configuration',
 3782:                                                          \%confhash,$dom);
 3783:                             if ($putresult ne 'ok') {
 3784:                                 $error{'default'} =
 3785:                                     '<span class="LC_error">'.
 3786:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3787:                             }
 3788:                         } else {
 3789:                             $error{'default'} = $error;
 3790:                         }
 3791:                     }
 3792:                 }
 3793:             }
 3794:         } else {
 3795:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3796:         }
 3797:     }
 3798:     if (ref($settings) eq 'HASH') {
 3799:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3800:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3801:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3802:                 $scantronurl = '';
 3803:             } else {
 3804:                 $scantronurl = $settings->{'scantronformat'};
 3805:             }
 3806:             $is_custom = 1;
 3807:         } else {
 3808:             $scantronurl = $scantronurls{'default'};
 3809:         }
 3810:     } else {
 3811:         if ($is_custom) {
 3812:             $scantronurl = $scantronurls{'custom'};
 3813:         } else {
 3814:             $scantronurl = $scantronurls{'default'};
 3815:         }
 3816:     }
 3817:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3818:     $datatable .= '<tr'.$css_class.'>';
 3819:     if (!$is_custom) {
 3820:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3821:                       '<span class="LC_nobreak">';
 3822:         if ($scantronurl) {
 3823:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3824:                           &mt('Default bubblesheet format file').'</a>';
 3825:         } else {
 3826:             $datatable = &mt('File unavailable for display');
 3827:         }
 3828:         $datatable .= '</span></td>';
 3829:         if (keys(%error) == 0) { 
 3830:             $datatable .= '<td valign="bottom">';
 3831:             if (!$switchserver) {
 3832:                 $datatable .= &mt('Upload:').'<br />';
 3833:             }
 3834:         } else {
 3835:             my $errorstr;
 3836:             foreach my $key (sort(keys(%error))) {
 3837:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3838:             }
 3839:             $datatable .= '<td>'.$errorstr;
 3840:         }
 3841:     } else {
 3842:         if (keys(%error) > 0) {
 3843:             my $errorstr;
 3844:             foreach my $key (sort(keys(%error))) {
 3845:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3846:             } 
 3847:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3848:         } elsif ($scantronurl) {
 3849:             $datatable .= '<td><span class="LC_nobreak">'.
 3850:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3851:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3852:                           '<input type="checkbox" name="scantronformat_del"'.
 3853:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3854:                           '<td><span class="LC_nobreak">&nbsp;'.
 3855:                           &mt('Replace:').'</span><br />';
 3856:         }
 3857:     }
 3858:     if (keys(%error) == 0) {
 3859:         if ($switchserver) {
 3860:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3861:         } else {
 3862:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3863:                          '<input type="file" name="scantronformat" /></span>';
 3864:         }
 3865:     }
 3866:     $datatable .= '</td></tr>';
 3867:     $$rowtotal ++;
 3868:     return $datatable;
 3869: }
 3870: 
 3871: sub legacy_scantronformat {
 3872:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3873:     my ($url,$error);
 3874:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3875:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3876:         (my $result,$url) =
 3877:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3878:                          '','',$newfile);
 3879:         if ($result ne 'ok') {
 3880:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3881:         }
 3882:     }
 3883:     return ($url,$error);
 3884: }
 3885: 
 3886: sub print_coursecategories {
 3887:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3888:     my $datatable;
 3889:     if ($position eq 'top') {
 3890:         my $toggle_cats_crs = ' ';
 3891:         my $toggle_cats_dom = ' checked="checked" ';
 3892:         my $can_cat_crs = ' ';
 3893:         my $can_cat_dom = ' checked="checked" ';
 3894:         my $toggle_catscomm_comm = ' ';
 3895:         my $toggle_catscomm_dom = ' checked="checked" ';
 3896:         my $can_catcomm_comm = ' ';
 3897:         my $can_catcomm_dom = ' checked="checked" ';
 3898: 
 3899:         if (ref($settings) eq 'HASH') {
 3900:             if ($settings->{'togglecats'} eq 'crs') {
 3901:                 $toggle_cats_crs = $toggle_cats_dom;
 3902:                 $toggle_cats_dom = ' ';
 3903:             }
 3904:             if ($settings->{'categorize'} eq 'crs') {
 3905:                 $can_cat_crs = $can_cat_dom;
 3906:                 $can_cat_dom = ' ';
 3907:             }
 3908:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3909:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3910:                 $toggle_catscomm_dom = ' ';
 3911:             }
 3912:             if ($settings->{'categorizecomm'} eq 'comm') {
 3913:                 $can_catcomm_comm = $can_catcomm_dom;
 3914:                 $can_catcomm_dom = ' ';
 3915:             }
 3916:         }
 3917:         my %title = &Apache::lonlocal::texthash (
 3918:                      togglecats     => 'Show/Hide a course in catalog',
 3919:                      togglecatscomm => 'Show/Hide a community in catalog',
 3920:                      categorize     => 'Assign a category to a course',
 3921:                      categorizecomm => 'Assign a category to a community',
 3922:                     );
 3923:         my %level = &Apache::lonlocal::texthash (
 3924:                      dom  => 'Set in Domain',
 3925:                      crs  => 'Set in Course',
 3926:                      comm => 'Set in Community',
 3927:                     );
 3928:         $datatable = '<tr class="LC_odd_row">'.
 3929:                   '<td>'.$title{'togglecats'}.'</td>'.
 3930:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3931:                   '<input type="radio" name="togglecats"'.
 3932:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3933:                   '<label><input type="radio" name="togglecats"'.
 3934:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3935:                   '</tr><tr>'.
 3936:                   '<td>'.$title{'categorize'}.'</td>'.
 3937:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3938:                   '<label><input type="radio" name="categorize"'.
 3939:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3940:                   '<label><input type="radio" name="categorize"'.
 3941:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3942:                   '</tr><tr class="LC_odd_row">'.
 3943:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 3944:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3945:                   '<input type="radio" name="togglecatscomm"'.
 3946:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3947:                   '<label><input type="radio" name="togglecatscomm"'.
 3948:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3949:                   '</tr><tr>'.
 3950:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 3951:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3952:                   '<label><input type="radio" name="categorizecomm"'.
 3953:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3954:                   '<label><input type="radio" name="categorizecomm"'.
 3955:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3956:                   '</tr>';
 3957:         $$rowtotal += 4;
 3958:     } else {
 3959:         my $css_class;
 3960:         my $itemcount = 1;
 3961:         my $cathash; 
 3962:         if (ref($settings) eq 'HASH') {
 3963:             $cathash = $settings->{'cats'};
 3964:         }
 3965:         if (ref($cathash) eq 'HASH') {
 3966:             my (@cats,@trails,%allitems,%idx,@jsarray);
 3967:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 3968:                                                    \%allitems,\%idx,\@jsarray);
 3969:             my $maxdepth = scalar(@cats);
 3970:             my $colattrib = '';
 3971:             if ($maxdepth > 2) {
 3972:                 $colattrib = ' colspan="2" ';
 3973:             }
 3974:             my @path;
 3975:             if (@cats > 0) {
 3976:                 if (ref($cats[0]) eq 'ARRAY') {
 3977:                     my $numtop = @{$cats[0]};
 3978:                     my $maxnum = $numtop;
 3979:                     my %default_names = (
 3980:                           instcode    => &mt('Official courses'),
 3981:                           communities => &mt('Communities'),
 3982:                     );
 3983: 
 3984:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 3985:                         ($cathash->{'instcode::0'} eq '') ||
 3986:                         (!grep(/^communities$/,@{$cats[0]})) || 
 3987:                         ($cathash->{'communities::0'} eq '')) {
 3988:                         $maxnum ++;
 3989:                     }
 3990:                     my $lastidx;
 3991:                     for (my $i=0; $i<$numtop; $i++) {
 3992:                         my $parent = $cats[0][$i];
 3993:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3994:                         my $item = &escape($parent).'::0';
 3995:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 3996:                         $lastidx = $idx{$item};
 3997:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3998:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 3999:                         for (my $k=0; $k<=$maxnum; $k++) {
 4000:                             my $vpos = $k+1;
 4001:                             my $selstr;
 4002:                             if ($k == $i) {
 4003:                                 $selstr = ' selected="selected" ';
 4004:                             }
 4005:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4006:                         }
 4007:                         $datatable .= '</select></td><td>';
 4008:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4009:                             $datatable .=  '<span class="LC_nobreak">'
 4010:                                            .$default_names{$parent}.'</span>';
 4011:                             if ($parent eq 'instcode') {
 4012:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4013:                                               .&mt('with institutional codes')
 4014:                                               .')</span></td><td'.$colattrib.'>';
 4015:                             } else {
 4016:                                 $datatable .= '<table><tr><td>';
 4017:                             }
 4018:                             $datatable .= '<span class="LC_nobreak">'
 4019:                                           .'<label><input type="radio" name="'
 4020:                                           .$parent.'" value="1" checked="checked" />'
 4021:                                           .&mt('Display').'</label>';
 4022:                             if ($parent eq 'instcode') {
 4023:                                 $datatable .= '&nbsp;';
 4024:                             } else {
 4025:                                 $datatable .= '</span></td></tr><tr><td>'
 4026:                                               .'<span class="LC_nobreak">';
 4027:                             }
 4028:                             $datatable .= '<label><input type="radio" name="'
 4029:                                           .$parent.'" value="0" />'
 4030:                                           .&mt('Do not display').'</label></span>';
 4031:                             if ($parent eq 'communities') {
 4032:                                 $datatable .= '</td></tr></table>';
 4033:                             }
 4034:                             $datatable .= '</td>';
 4035:                         } else {
 4036:                             $datatable .= $parent
 4037:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 4038:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4039:                         }
 4040:                         my $depth = 1;
 4041:                         push(@path,$parent);
 4042:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4043:                         pop(@path);
 4044:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4045:                         $itemcount ++;
 4046:                     }
 4047:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4048:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4049:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4050:                     for (my $k=0; $k<=$maxnum; $k++) {
 4051:                         my $vpos = $k+1;
 4052:                         my $selstr;
 4053:                         if ($k == $numtop) {
 4054:                             $selstr = ' selected="selected" ';
 4055:                         }
 4056:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4057:                     }
 4058:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4059:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4060:                                   .'</tr>'."\n";
 4061:                     $itemcount ++;
 4062:                     foreach my $default ('instcode','communities') {
 4063:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4064:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4065:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4066:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4067:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4068:                             for (my $k=0; $k<=$maxnum; $k++) {
 4069:                                 my $vpos = $k+1;
 4070:                                 my $selstr;
 4071:                                 if ($k == $maxnum) {
 4072:                                     $selstr = ' selected="selected" ';
 4073:                                 }
 4074:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4075:                             }
 4076:                             $datatable .= '</select></span></td>'.
 4077:                                           '<td><span class="LC_nobreak">'.
 4078:                                           $default_names{$default}.'</span>';
 4079:                             if ($default eq 'instcode') {
 4080:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4081:                                               .&mt('with institutional codes').')</span>';
 4082:                             }
 4083:                             $datatable .= '</td>'
 4084:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4085:                                           .&mt('Display').'</label>&nbsp;'
 4086:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4087:                                           .&mt('Do not display').'</label></span></td></tr>';
 4088:                         }
 4089:                     }
 4090:                 }
 4091:             } else {
 4092:                 $datatable .= &initialize_categories($itemcount);
 4093:             }
 4094:         } else {
 4095:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4096:                           .&initialize_categories($itemcount);
 4097:         }
 4098:         $$rowtotal += $itemcount;
 4099:     }
 4100:     return $datatable;
 4101: }
 4102: 
 4103: sub print_serverstatuses {
 4104:     my ($dom,$settings,$rowtotal) = @_;
 4105:     my $datatable;
 4106:     my @pages = &serverstatus_pages();
 4107:     my (%namedaccess,%machineaccess);
 4108:     foreach my $type (@pages) {
 4109:         $namedaccess{$type} = '';
 4110:         $machineaccess{$type}= '';
 4111:     }
 4112:     if (ref($settings) eq 'HASH') {
 4113:         foreach my $type (@pages) {
 4114:             if (exists($settings->{$type})) {
 4115:                 if (ref($settings->{$type}) eq 'HASH') {
 4116:                     foreach my $key (keys(%{$settings->{$type}})) {
 4117:                         if ($key eq 'namedusers') {
 4118:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4119:                         } elsif ($key eq 'machines') {
 4120:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4121:                         }
 4122:                     }
 4123:                 }
 4124:             }
 4125:         }
 4126:     }
 4127:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4128:     my $rownum = 0;
 4129:     my $css_class;
 4130:     foreach my $type (@pages) {
 4131:         $rownum ++;
 4132:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4133:         $datatable .= '<tr'.$css_class.'>'.
 4134:                       '<td><span class="LC_nobreak">'.
 4135:                       $titles->{$type}.'</span></td>'.
 4136:                       '<td class="LC_left_item">'.
 4137:                       '<input type="text" name="'.$type.'_namedusers" '.
 4138:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4139:                       '<td class="LC_right_item">'.
 4140:                       '<span class="LC_nobreak">'.
 4141:                       '<input type="text" name="'.$type.'_machines" '.
 4142:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4143:                       '</td></tr>'."\n";
 4144:     }
 4145:     $$rowtotal += $rownum;
 4146:     return $datatable;
 4147: }
 4148: 
 4149: sub serverstatus_pages {
 4150:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4151:             'clusterstatus','metadata_keywords','metadata_harvest',
 4152:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4153: }
 4154: 
 4155: sub coursecategories_javascript {
 4156:     my ($settings) = @_;
 4157:     my ($output,$jstext,$cathash);
 4158:     if (ref($settings) eq 'HASH') {
 4159:         $cathash = $settings->{'cats'};
 4160:     }
 4161:     if (ref($cathash) eq 'HASH') {
 4162:         my (@cats,@jsarray,%idx);
 4163:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4164:         if (@jsarray > 0) {
 4165:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4166:             for (my $i=0; $i<@jsarray; $i++) {
 4167:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4168:                     my $catstr = join('","',@{$jsarray[$i]});
 4169:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4170:                 }
 4171:             }
 4172:         }
 4173:     } else {
 4174:         $jstext  = '    var categories = Array(1);'."\n".
 4175:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4176:     }
 4177:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4178:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4179:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4180:     $output = <<"ENDSCRIPT";
 4181: <script type="text/javascript">
 4182: // <![CDATA[
 4183: function reorderCats(form,parent,item,idx) {
 4184:     var changedVal;
 4185: $jstext
 4186:     var newpos = 'addcategory_pos';
 4187:     var current = new Array;
 4188:     if (parent == '') {
 4189:         var has_instcode = 0;
 4190:         var maxtop = categories[idx].length;
 4191:         for (var j=0; j<maxtop; j++) {
 4192:             if (categories[idx][j] == 'instcode::0') {
 4193:                 has_instcode == 1;
 4194:             }
 4195:         }
 4196:         if (has_instcode == 0) {
 4197:             categories[idx][maxtop] = 'instcode_pos';
 4198:         }
 4199:     } else {
 4200:         newpos += '_'+parent;
 4201:     }
 4202:     var maxh = 1 + categories[idx].length;
 4203:     var current = new Array;
 4204:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4205:     if (item == newpos) {
 4206:         changedVal = newitemVal;
 4207:     } else {
 4208:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4209:         current[newitemVal] = newpos;
 4210:     }
 4211:     for (var i=0; i<categories[idx].length; i++) {
 4212:         var elementName = categories[idx][i];
 4213:         if (elementName != item) {
 4214:             if (form.elements[elementName]) {
 4215:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4216:                 current[currVal] = elementName;
 4217:             }
 4218:         }
 4219:     }
 4220:     var oldVal;
 4221:     for (var j=0; j<maxh; j++) {
 4222:         if (current[j] == undefined) {
 4223:             oldVal = j;
 4224:         }
 4225:     }
 4226:     if (oldVal < changedVal) {
 4227:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4228:            var elementName = current[k];
 4229:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4230:         }
 4231:     } else {
 4232:         for (var k=changedVal; k<oldVal; k++) {
 4233:             var elementName = current[k];
 4234:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4235:         }
 4236:     }
 4237:     return;
 4238: }
 4239: 
 4240: function categoryCheck(form) {
 4241:     if (form.elements['addcategory_name'].value == 'instcode') {
 4242:         alert('$instcode_reserved\\n$choose_again');
 4243:         return false;
 4244:     }
 4245:     if (form.elements['addcategory_name'].value == 'communities') {
 4246:         alert('$communities_reserved\\n$choose_again');
 4247:         return false;
 4248:     }
 4249:     return true;
 4250: }
 4251: 
 4252: // ]]>
 4253: </script>
 4254: 
 4255: ENDSCRIPT
 4256:     return $output;
 4257: }
 4258: 
 4259: sub initialize_categories {
 4260:     my ($itemcount) = @_;
 4261:     my ($datatable,$css_class,$chgstr);
 4262:     my %default_names = (
 4263:                       instcode    => 'Official courses (with institutional codes)',
 4264:                       communities => 'Communities',
 4265:                         );
 4266:     my $select0 = ' selected="selected"';
 4267:     my $select1 = '';
 4268:     foreach my $default ('instcode','communities') {
 4269:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4270:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4271:         if ($default eq 'communities') {
 4272:             $select1 = $select0;
 4273:             $select0 = '';
 4274:         }
 4275:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4276:                      .'<select name="'.$default.'_pos">'
 4277:                      .'<option value="0"'.$select0.'>1</option>'
 4278:                      .'<option value="1"'.$select1.'>2</option>'
 4279:                      .'<option value="2">3</option></select>&nbsp;'
 4280:                      .$default_names{$default}
 4281:                      .'</span></td><td><span class="LC_nobreak">'
 4282:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4283:                      .&mt('Display').'</label>&nbsp;<label>'
 4284:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4285:                  .'</label></span></td></tr>';
 4286:         $itemcount ++;
 4287:     }
 4288:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4289:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4290:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4291:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4292:                   .'<option value="0">1</option>'
 4293:                   .'<option value="1">2</option>'
 4294:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4295:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4296:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4297:     return $datatable;
 4298: }
 4299: 
 4300: sub build_category_rows {
 4301:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4302:     my ($text,$name,$item,$chgstr);
 4303:     if (ref($cats) eq 'ARRAY') {
 4304:         my $maxdepth = scalar(@{$cats});
 4305:         if (ref($cats->[$depth]) eq 'HASH') {
 4306:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4307:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4308:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4309:                 $text .= '<td><table class="LC_datatable">';
 4310:                 my ($idxnum,$parent_name,$parent_item);
 4311:                 my $higher = $depth - 1;
 4312:                 if ($higher == 0) {
 4313:                     $parent_name = &escape($parent).'::'.$higher;
 4314:                 } else {
 4315:                     if (ref($path) eq 'ARRAY') {
 4316:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4317:                     }
 4318:                 }
 4319:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4320:                 for (my $j=0; $j<=$numchildren; $j++) {
 4321:                     if ($j < $numchildren) {
 4322:                         $name = $cats->[$depth]{$parent}[$j];
 4323:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4324:                         $idxnum = $idx->{$item};
 4325:                     } else {
 4326:                         $name = $parent_name;
 4327:                         $item = $parent_item;
 4328:                     }
 4329:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4330:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4331:                     for (my $i=0; $i<=$numchildren; $i++) {
 4332:                         my $vpos = $i+1;
 4333:                         my $selstr;
 4334:                         if ($j == $i) {
 4335:                             $selstr = ' selected="selected" ';
 4336:                         }
 4337:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4338:                     }
 4339:                     $text .= '</select>&nbsp;';
 4340:                     if ($j < $numchildren) {
 4341:                         my $deeper = $depth+1;
 4342:                         $text .= $name.'&nbsp;'
 4343:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4344:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4345:                         if(ref($path) eq 'ARRAY') {
 4346:                             push(@{$path},$name);
 4347:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4348:                             pop(@{$path});
 4349:                         }
 4350:                     } else {
 4351:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4352:                         if ($j == $numchildren) {
 4353:                             $text .= $name;
 4354:                         } else {
 4355:                             $text .= $item;
 4356:                         }
 4357:                         $text .= '" value="" />';
 4358:                     }
 4359:                     $text .= '</td></tr>';
 4360:                 }
 4361:                 $text .= '</table></td>';
 4362:             } else {
 4363:                 my $higher = $depth-1;
 4364:                 if ($higher == 0) {
 4365:                     $name = &escape($parent).'::'.$higher;
 4366:                 } else {
 4367:                     if (ref($path) eq 'ARRAY') {
 4368:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4369:                     }
 4370:                 }
 4371:                 my $colspan;
 4372:                 if ($parent ne 'instcode') {
 4373:                     $colspan = $maxdepth - $depth - 1;
 4374:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4375:                 }
 4376:             }
 4377:         }
 4378:     }
 4379:     return $text;
 4380: }
 4381: 
 4382: sub modifiable_userdata_row {
 4383:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4384:     my $rolename;
 4385:     if ($context eq 'selfcreate') {
 4386:         if (ref($usertypes) eq 'HASH') {
 4387:             $rolename = $usertypes->{$role};
 4388:         } else {
 4389:             $rolename = $role;
 4390:         }
 4391:     } else {
 4392:         if ($role eq 'cr') {
 4393:             $rolename = &mt('Custom role');
 4394:         } else {
 4395:             $rolename = &Apache::lonnet::plaintext($role);
 4396:         }
 4397:     }
 4398:     my @fields = ('lastname','firstname','middlename','generation',
 4399:                   'permanentemail','id');
 4400:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4401:     my $output;
 4402:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4403:     $output = '<tr '.$css_class.'>'.
 4404:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4405:               '<td class="LC_left_item" colspan="2"><table>';
 4406:     my $rem;
 4407:     my %checks;
 4408:     if (ref($settings) eq 'HASH') {
 4409:         if (ref($settings->{$context}) eq 'HASH') {
 4410:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4411:                 foreach my $field (@fields) {
 4412:                     if ($settings->{$context}->{$role}->{$field}) {
 4413:                         $checks{$field} = ' checked="checked" ';
 4414:                     }
 4415:                 }
 4416:             }
 4417:         }
 4418:     }
 4419:     for (my $i=0; $i<@fields; $i++) {
 4420:         my $rem = $i%($numinrow);
 4421:         if ($rem == 0) {
 4422:             if ($i > 0) {
 4423:                 $output .= '</tr>';
 4424:             }
 4425:             $output .= '<tr>';
 4426:         }
 4427:         my $check = ' ';
 4428:         if (exists($checks{$fields[$i]})) {
 4429:             $check = $checks{$fields[$i]}
 4430:         } else {
 4431:             if ($role eq 'st') {
 4432:                 if (ref($settings) ne 'HASH') {
 4433:                     $check = ' checked="checked" '; 
 4434:                 }
 4435:             }
 4436:         }
 4437:         $output .= '<td class="LC_left_item">'.
 4438:                    '<span class="LC_nobreak"><label>'.
 4439:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4440:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4441:                    '</label></span></td>';
 4442:         $rem = @fields%($numinrow);
 4443:     }
 4444:     my $colsleft = $numinrow - $rem;
 4445:     if ($colsleft > 1 ) {
 4446:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4447:                    '&nbsp;</td>';
 4448:     } elsif ($colsleft == 1) {
 4449:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4450:     }
 4451:     $output .= '</tr></table></td></tr>';
 4452:     return $output;
 4453: }
 4454: 
 4455: sub insttypes_row {
 4456:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4457:     my %lt = &Apache::lonlocal::texthash (
 4458:                       cansearch => 'Users allowed to search',
 4459:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4460:                       lockablenames => 'User preference to lock name',
 4461:              );
 4462:     my $showdom;
 4463:     if ($context eq 'cansearch') {
 4464:         $showdom = ' ('.$dom.')';
 4465:     }
 4466:     my $class = 'LC_left_item';
 4467:     if ($context eq 'statustocreate') {
 4468:         $class = 'LC_right_item';
 4469:     }
 4470:     my $output =  '<tr class="LC_odd_row">'.
 4471:                   '<td>'.$lt{$context}.$showdom.
 4472:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4473:     my $rem;
 4474:     if (ref($types) eq 'ARRAY') {
 4475:         for (my $i=0; $i<@{$types}; $i++) {
 4476:             if (defined($usertypes->{$types->[$i]})) {
 4477:                 my $rem = $i%($numinrow);
 4478:                 if ($rem == 0) {
 4479:                     if ($i > 0) {
 4480:                         $output .= '</tr>';
 4481:                     }
 4482:                     $output .= '<tr>';
 4483:                 }
 4484:                 my $check = ' ';
 4485:                 if (ref($settings) eq 'HASH') {
 4486:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4487:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4488:                             $check = ' checked="checked" ';
 4489:                         }
 4490:                     } elsif ($context eq 'statustocreate') {
 4491:                         $check = ' checked="checked" ';
 4492:                     }
 4493:                 }
 4494:                 $output .= '<td class="LC_left_item">'.
 4495:                            '<span class="LC_nobreak"><label>'.
 4496:                            '<input type="checkbox" name="'.$context.'" '.
 4497:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4498:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4499:             }
 4500:         }
 4501:         $rem = @{$types}%($numinrow);
 4502:     }
 4503:     my $colsleft = $numinrow - $rem;
 4504:     if (($rem == 0) && (@{$types} > 0)) {
 4505:         $output .= '<tr>';
 4506:     }
 4507:     if ($colsleft > 1) {
 4508:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4509:     } else {
 4510:         $output .= '<td class="LC_left_item">';
 4511:     }
 4512:     my $defcheck = ' ';
 4513:     if (ref($settings) eq 'HASH') {  
 4514:         if (ref($settings->{$context}) eq 'ARRAY') {
 4515:             if (grep(/^default$/,@{$settings->{$context}})) {
 4516:                 $defcheck = ' checked="checked" ';
 4517:             }
 4518:         } elsif ($context eq 'statustocreate') {
 4519:             $defcheck = ' checked="checked" ';
 4520:         }
 4521:     }
 4522:     $output .= '<span class="LC_nobreak"><label>'.
 4523:                '<input type="checkbox" name="'.$context.'" '.
 4524:                'value="default"'.$defcheck.'/>'.
 4525:                $othertitle.'</label></span></td>'.
 4526:                '</tr></table></td></tr>';
 4527:     return $output;
 4528: }
 4529: 
 4530: sub sorted_searchtitles {
 4531:     my %searchtitles = &Apache::lonlocal::texthash(
 4532:                          'uname' => 'username',
 4533:                          'lastname' => 'last name',
 4534:                          'lastfirst' => 'last name, first name',
 4535:                      );
 4536:     my @titleorder = ('uname','lastname','lastfirst');
 4537:     return (\%searchtitles,\@titleorder);
 4538: }
 4539: 
 4540: sub sorted_searchtypes {
 4541:     my %srchtypes_desc = (
 4542:                            exact    => 'is exact match',
 4543:                            contains => 'contains ..',
 4544:                            begins   => 'begins with ..',
 4545:                          );
 4546:     my @srchtypeorder = ('exact','begins','contains');
 4547:     return (\%srchtypes_desc,\@srchtypeorder);
 4548: }
 4549: 
 4550: sub usertype_update_row {
 4551:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4552:     my $datatable;
 4553:     my $numinrow = 4;
 4554:     foreach my $type (@{$types}) {
 4555:         if (defined($usertypes->{$type})) {
 4556:             $$rownums ++;
 4557:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4558:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4559:                           '</td><td class="LC_left_item"><table>';
 4560:             for (my $i=0; $i<@{$fields}; $i++) {
 4561:                 my $rem = $i%($numinrow);
 4562:                 if ($rem == 0) {
 4563:                     if ($i > 0) {
 4564:                         $datatable .= '</tr>';
 4565:                     }
 4566:                     $datatable .= '<tr>';
 4567:                 }
 4568:                 my $check = ' ';
 4569:                 if (ref($settings) eq 'HASH') {
 4570:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4571:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4572:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4573:                                 $check = ' checked="checked" ';
 4574:                             }
 4575:                         }
 4576:                     }
 4577:                 }
 4578: 
 4579:                 if ($i == @{$fields}-1) {
 4580:                     my $colsleft = $numinrow - $rem;
 4581:                     if ($colsleft > 1) {
 4582:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4583:                     } else {
 4584:                         $datatable .= '<td>';
 4585:                     }
 4586:                 } else {
 4587:                     $datatable .= '<td>';
 4588:                 }
 4589:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4590:                               '<input type="checkbox" name="updateable_'.$type.
 4591:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4592:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4593:             }
 4594:             $datatable .= '</tr></table></td></tr>';
 4595:         }
 4596:     }
 4597:     return $datatable;
 4598: }
 4599: 
 4600: sub modify_login {
 4601:     my ($r,$dom,$confname,%domconfig) = @_;
 4602:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4603:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4604:     %title = ( coursecatalog => 'Display course catalog',
 4605:                adminmail => 'Display administrator E-mail address',
 4606:                newuser => 'Link for visitors to create a user account',
 4607:                loginheader => 'Log-in box header');
 4608:     @offon = ('off','on');
 4609:     if (ref($domconfig{login}) eq 'HASH') {
 4610:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4611:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4612:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4613:             }
 4614:         }
 4615:     }
 4616:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4617:                                            \%domconfig,\%loginhash);
 4618:     my @toggles = ('coursecatalog','adminmail','newuser');
 4619:     foreach my $item (@toggles) {
 4620:         $loginhash{login}{$item} = $env{'form.'.$item};
 4621:     }
 4622:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4623:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4624:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4625:                                          \%loginhash);
 4626:     }
 4627: 
 4628:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4629:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4630:     if (keys(%servers) > 1) {
 4631:         foreach my $lonhost (keys(%servers)) {
 4632:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4633:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4634:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4635:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4636:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4637:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4638:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4639:                         $changes{'loginvia'}{$lonhost} = 1;
 4640:                     } else {
 4641:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4642:                         $changes{'loginvia'}{$lonhost} = 1;
 4643:                     }
 4644:                 } else {
 4645:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4646:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4647:                         $changes{'loginvia'}{$lonhost} = 1;
 4648:                     }
 4649:                 }
 4650:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4651:                     foreach my $item (@loginvia_attribs) {
 4652:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4653:                     }
 4654:                 } else {
 4655:                     foreach my $item (@loginvia_attribs) {
 4656:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4657:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4658:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4659:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4660:                                 $new = '/';
 4661:                             }
 4662:                         }
 4663:                         if (($item eq 'custompath') && 
 4664:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4665:                             $new = '';
 4666:                         }
 4667:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4668:                             $changes{'loginvia'}{$lonhost} = 1;
 4669:                         }
 4670:                         if ($item eq 'exempt') {
 4671:                             $new =~ s/^\s+//;
 4672:                             $new =~ s/\s+$//;
 4673:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4674:                             my @okips;
 4675:                             foreach my $ip (@poss_ips) {
 4676:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4677:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4678:                                         push(@okips,$ip); 
 4679:                                     }
 4680:                                 }
 4681:                             }
 4682:                             if (@okips > 0) {
 4683:                                 $new = join(',',@okips); 
 4684:                             } else {
 4685:                                 $new = ''; 
 4686:                             }
 4687:                         }
 4688:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4689:                     }
 4690:                 }
 4691:             } else {
 4692:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4693:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4694:                     $changes{'loginvia'}{$lonhost} = 1;
 4695:                     foreach my $item (@loginvia_attribs) {
 4696:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4697:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4698:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4699:                                 $new = '/';
 4700:                             }
 4701:                         }
 4702:                         if (($item eq 'custompath') && 
 4703:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4704:                             $new = '';
 4705:                         }
 4706:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4707:                     }
 4708:                 }
 4709:             }
 4710:         }
 4711:     }
 4712: 
 4713:     my $servadm = $r->dir_config('lonAdmEMail');
 4714:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 4715:     if (ref($domconfig{'login'}) eq 'HASH') {
 4716:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 4717:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 4718:                 if ($lang eq 'nolang') {
 4719:                     push(@currlangs,$lang);
 4720:                 } elsif (defined($langchoices{$lang})) {
 4721:                     push(@currlangs,$lang);
 4722:                 } else {
 4723:                     next;
 4724:                 }
 4725:             }
 4726:         }
 4727:     }
 4728:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 4729:     if (@currlangs > 0) {
 4730:         foreach my $lang (@currlangs) {
 4731:             if (grep(/^\Q$lang\E$/,@delurls)) {
 4732:                 $changes{'helpurl'}{$lang} = 1;
 4733:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 4734:                 $changes{'helpurl'}{$lang} = 1;
 4735:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 4736:                 push(@newlangs,$lang);
 4737:             } else {
 4738:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4739:             }
 4740:         }
 4741:     }
 4742:     unless (grep(/^nolang$/,@currlangs)) {
 4743:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 4744:             $changes{'helpurl'}{'nolang'} = 1;
 4745:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 4746:             push(@newlangs,'nolang');
 4747:         }
 4748:     }
 4749:     if ($env{'form.loginhelpurl_add_lang'}) {
 4750:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 4751:             ($env{'form.loginhelpurl_add_file.filename'})) {
 4752:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 4753:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 4754:         }
 4755:     }
 4756:     if ((@newlangs > 0) || ($addedfile)) {
 4757:         my $error;
 4758:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 4759:         if ($configuserok eq 'ok') {
 4760:             if ($switchserver) {
 4761:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 4762:             } elsif ($author_ok eq 'ok') {
 4763:                 my @allnew = @newlangs;
 4764:                 if ($addedfile ne '') {
 4765:                     push(@allnew,$addedfile);
 4766:                 }
 4767:                 foreach my $lang (@allnew) {
 4768:                     my $formelem = 'loginhelpurl_'.$lang;
 4769:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 4770:                         $formelem = 'loginhelpurl_add_file';
 4771:                     }
 4772:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 4773:                                                                "help/$lang",'','',$newfile{$lang});
 4774:                     if ($result eq 'ok') {
 4775:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 4776:                         $changes{'helpurl'}{$lang} = 1;
 4777:                     } else {
 4778:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 4779:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 4780:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 4781:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 4782: 
 4783:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4784:                         }
 4785:                     }
 4786:                 }
 4787:             } else {
 4788:                 $error = &mt("Upload of custom log-in help file(s) failed because an author role could not be assigned to a Domain Configuration user ([_1]) in domain: [_2].  Error was: [_3].",$confname,$dom,$author_ok);
 4789:             }
 4790:         } else {
 4791:             $error = &mt("Upload of custom log-in help file(s) failed because a Domain Configuration user ([_1]) could not be created in domain: [_2].  Error was: [_3].",$confname,$dom,$configuserok);
 4792:         }
 4793:         if ($error) {
 4794:             &Apache::lonnet::logthis($error);
 4795:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4796:         }
 4797:     }
 4798:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 4799: 
 4800:     my $defaulthelpfile = '/adm/loginproblems.html';
 4801:     my $defaulttext = &mt('Default in use');
 4802: 
 4803:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4804:                                              $dom);
 4805:     if ($putresult eq 'ok') {
 4806:         my @toggles = ('coursecatalog','adminmail','newuser');
 4807:         my %defaultchecked = (
 4808:                     'coursecatalog' => 'on',
 4809:                     'adminmail'     => 'off',
 4810:                     'newuser'       => 'off',
 4811:         );
 4812:         if (ref($domconfig{'login'}) eq 'HASH') {
 4813:             foreach my $item (@toggles) {
 4814:                 if ($defaultchecked{$item} eq 'on') { 
 4815:                     if (($domconfig{'login'}{$item} eq '0') &&
 4816:                         ($env{'form.'.$item} eq '1')) {
 4817:                         $changes{$item} = 1;
 4818:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4819:                               $domconfig{'login'}{$item} eq '1') &&
 4820:                              ($env{'form.'.$item} eq '0')) {
 4821:                         $changes{$item} = 1;
 4822:                     }
 4823:                 } elsif ($defaultchecked{$item} eq 'off') {
 4824:                     if (($domconfig{'login'}{$item} eq '1') &&
 4825:                         ($env{'form.'.$item} eq '0')) {
 4826:                         $changes{$item} = 1;
 4827:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4828:                               $domconfig{'login'}{$item} eq '0') &&
 4829:                              ($env{'form.'.$item} eq '1')) {
 4830:                         $changes{$item} = 1;
 4831:                     }
 4832:                 }
 4833:             }
 4834:         }
 4835:         if (keys(%changes) > 0 || $colchgtext) {
 4836:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4837:             $resulttext = &mt('Changes made:').'<ul>';
 4838:             foreach my $item (sort(keys(%changes))) {
 4839:                 if ($item eq 'loginvia') {
 4840:                     if (ref($changes{$item}) eq 'HASH') {
 4841:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4842:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4843:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4844:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4845:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4846:                                     $protocol = 'http' if ($protocol ne 'https');
 4847:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4848: 
 4849:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4850:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4851:                                     } else {
 4852:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4853:                                     }
 4854:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4855:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4856:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4857:                                     }
 4858:                                     $resulttext .= '</li>';
 4859:                                 } else {
 4860:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4861:                                 }
 4862:                             } else {
 4863:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4864:                             }
 4865:                         }
 4866:                         $resulttext .= '</ul></li>';
 4867:                     }
 4868:                 } elsif ($item eq 'helpurl') {
 4869:                     if (ref($changes{$item}) eq 'HASH') {
 4870:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 4871:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 4872:                                 my ($chg,$link);
 4873:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 4874:                                 if ($lang eq 'nolang') {
 4875:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 4876:                                 } else {
 4877:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 4878:                                 }
 4879:                                 $resulttext .= '<li>'.$chg.'</li>';
 4880:                             } else {
 4881:                                 my $chg;
 4882:                                 if ($lang eq 'nolang') {
 4883:                                     $chg = &mt('custom log-in help file for no preferred language');
 4884:                                 } else {
 4885:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 4886:                                 }
 4887:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 4888:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 4889:                                                       '?inhibitmenu=yes',$chg,600,500).
 4890:                                                '</li>';
 4891:                             }
 4892:                         }
 4893:                     }
 4894:                 } elsif ($item eq 'captcha') {
 4895:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4896:                         my $chgtxt;
 4897:                         if ($loginhash{'login'}{$item} eq 'notused') {
 4898:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 4899:                         } else {
 4900:                             my %captchas = &captcha_phrases();
 4901:                             if ($captchas{$loginhash{'login'}{$item}}) {
 4902:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 4903:                             } else {
 4904:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 4905:                             }
 4906:                         }
 4907:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4908:                     }
 4909:                 } elsif ($item eq 'recaptchakeys') {
 4910:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4911:                         my ($privkey,$pubkey);
 4912:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 4913:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 4914:                             $privkey = $loginhash{'login'}{$item}{'private'};
 4915:                         }
 4916:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 4917:                         if (!$pubkey) {
 4918:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 4919:                         } else {
 4920:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 4921:                         }
 4922:                         if (!$privkey) {
 4923:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 4924:                         } else {
 4925:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 4926:                         }
 4927:                         $chgtxt .= '</ul>';
 4928:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4929:                     }
 4930:                 } else {
 4931:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4932:                 }
 4933:             }
 4934:             $resulttext .= $colchgtext.'</ul>';
 4935:         } else {
 4936:             $resulttext = &mt('No changes made to log-in page settings');
 4937:         }
 4938:     } else {
 4939:         $resulttext = '<span class="LC_error">'.
 4940: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4941:     }
 4942:     if ($errors) {
 4943:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 4944:                        $errors.'</ul>';
 4945:     }
 4946:     return $resulttext;
 4947: }
 4948: 
 4949: sub color_font_choices {
 4950:     my %choices =
 4951:         &Apache::lonlocal::texthash (
 4952:             img => "Header",
 4953:             bgs => "Background colors",
 4954:             links => "Link colors",
 4955:             images => "Images",
 4956:             font => "Font color",
 4957:             fontmenu => "Font Menu",
 4958:             pgbg => "Page",
 4959:             tabbg => "Header",
 4960:             sidebg => "Border",
 4961:             link => "Link",
 4962:             alink => "Active link",
 4963:             vlink => "Visited link",
 4964:         );
 4965:     return %choices;
 4966: }
 4967: 
 4968: sub modify_rolecolors {
 4969:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 4970:     my ($resulttext,%rolehash);
 4971:     $rolehash{'rolecolors'} = {};
 4972:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 4973:         if ($domconfig{'rolecolors'} eq '') {
 4974:             $domconfig{'rolecolors'} = {};
 4975:         }
 4976:     }
 4977:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 4978:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 4979:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 4980:                                              $dom);
 4981:     if ($putresult eq 'ok') {
 4982:         if (keys(%changes) > 0) {
 4983:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4984:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 4985:                                              $rolehash{'rolecolors'});
 4986:         } else {
 4987:             $resulttext = &mt('No changes made to default color schemes');
 4988:         }
 4989:     } else {
 4990:         $resulttext = '<span class="LC_error">'.
 4991: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4992:     }
 4993:     if ($errors) {
 4994:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 4995:                        $errors.'</ul>';
 4996:     }
 4997:     return $resulttext;
 4998: }
 4999: 
 5000: sub modify_colors {
 5001:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5002:     my (%changes,%choices);
 5003:     my @bgs;
 5004:     my @links = ('link','alink','vlink');
 5005:     my @logintext;
 5006:     my @images;
 5007:     my $servadm = $r->dir_config('lonAdmEMail');
 5008:     my $errors;
 5009:     foreach my $role (@{$roles}) {
 5010:         if ($role eq 'login') {
 5011:             %choices = &login_choices();
 5012:             @logintext = ('textcol','bgcol');
 5013:         } else {
 5014:             %choices = &color_font_choices();
 5015:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5016:         }
 5017:         if ($role eq 'login') {
 5018:             @images = ('img','logo','domlogo','login');
 5019:             @bgs = ('pgbg','mainbg','sidebg');
 5020:         } else {
 5021:             @images = ('img');
 5022:             @bgs = ('pgbg','tabbg','sidebg'); 
 5023:         }
 5024:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5025:         foreach my $item (@bgs,@links,@logintext) {
 5026:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5027:         }
 5028:         my ($configuserok,$author_ok,$switchserver) = 
 5029:             &config_check($dom,$confname,$servadm);
 5030:         my ($width,$height) = &thumb_dimensions();
 5031:         if (ref($domconfig->{$role}) ne 'HASH') {
 5032:             $domconfig->{$role} = {};
 5033:         }
 5034:         foreach my $img (@images) {
 5035:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5036:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5037:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5038:                 } else { 
 5039:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5040:                 }
 5041:             } 
 5042: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5043: 		 && !defined($domconfig->{$role}{$img})
 5044: 		 && !$env{'form.'.$role.'_del_'.$img}
 5045: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5046: 		# import the old configured image from the .tab setting
 5047: 		# if they haven't provided a new one 
 5048: 		$domconfig->{$role}{$img} = 
 5049: 		    $env{'form.'.$role.'_import_'.$img};
 5050: 	    }
 5051:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5052:                 my $error;
 5053:                 if ($configuserok eq 'ok') {
 5054:                     if ($switchserver) {
 5055:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5056:                     } else {
 5057:                         if ($author_ok eq 'ok') {
 5058:                             my ($result,$logourl) = 
 5059:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5060:                                            $dom,$confname,$img,$width,$height);
 5061:                             if ($result eq 'ok') {
 5062:                                 $confhash->{$role}{$img} = $logourl;
 5063:                                 $changes{$role}{'images'}{$img} = 1;
 5064:                             } else {
 5065:                                 $error = &mt("Upload of [_1] image for $role page(s) failed because an error occurred publishing the file in RES space. Error was: [_2].",$choices{img},$result);
 5066:                             }
 5067:                         } else {
 5068:                             $error = &mt("Upload of [_1] image for $role page(s) failed because an author role could not be assigned to a Domain Configuration user ([_2]) in domain: [_3].  Error was: [_4].",$choices{$img},$confname,$dom,$author_ok);
 5069:                         }
 5070:                     }
 5071:                 } else {
 5072:                     $error = &mt("Upload of [_1] image for $role page(s) failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$choices{$img},$confname,$dom,$configuserok);
 5073:                 }
 5074:                 if ($error) {
 5075:                     &Apache::lonnet::logthis($error);
 5076:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5077:                 }
 5078:             } elsif ($domconfig->{$role}{$img} ne '') {
 5079:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5080:                     my $error;
 5081:                     if ($configuserok eq 'ok') {
 5082: # is confname an author?
 5083:                         if ($switchserver eq '') {
 5084:                             if ($author_ok eq 'ok') {
 5085:                                 my ($result,$logourl) = 
 5086:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5087:                                             $dom,$confname,$img,$width,$height);
 5088:                                 if ($result eq 'ok') {
 5089:                                     $confhash->{$role}{$img} = $logourl;
 5090: 				    $changes{$role}{'images'}{$img} = 1;
 5091:                                 }
 5092:                             }
 5093:                         }
 5094:                     }
 5095:                 }
 5096:             }
 5097:         }
 5098:         if (ref($domconfig) eq 'HASH') {
 5099:             if (ref($domconfig->{$role}) eq 'HASH') {
 5100:                 foreach my $img (@images) {
 5101:                     if ($domconfig->{$role}{$img} ne '') {
 5102:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5103:                             $confhash->{$role}{$img} = '';
 5104:                             $changes{$role}{'images'}{$img} = 1;
 5105:                         } else {
 5106:                             if ($confhash->{$role}{$img} eq '') {
 5107:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5108:                             }
 5109:                         }
 5110:                     } else {
 5111:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5112:                             $confhash->{$role}{$img} = '';
 5113:                             $changes{$role}{'images'}{$img} = 1;
 5114:                         } 
 5115:                     }
 5116:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5117:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5118:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5119:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5120:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5121:                             }
 5122:                         } else {
 5123:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5124:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5125:                             }
 5126:                         }
 5127:                     }
 5128:                 }
 5129:                 if ($domconfig->{$role}{'font'} ne '') {
 5130:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5131:                         $changes{$role}{'font'} = 1;
 5132:                     }
 5133:                 } else {
 5134:                     if ($confhash->{$role}{'font'}) {
 5135:                         $changes{$role}{'font'} = 1;
 5136:                     }
 5137:                 }
 5138:                 if ($role ne 'login') {
 5139:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5140:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5141:                             $changes{$role}{'fontmenu'} = 1;
 5142:                         }
 5143:                     } else {
 5144:                         if ($confhash->{$role}{'fontmenu'}) {
 5145:                             $changes{$role}{'fontmenu'} = 1;
 5146:                         }
 5147:                     }
 5148:                 }
 5149:                 foreach my $item (@bgs) {
 5150:                     if ($domconfig->{$role}{$item} ne '') {
 5151:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5152:                             $changes{$role}{'bgs'}{$item} = 1;
 5153:                         } 
 5154:                     } else {
 5155:                         if ($confhash->{$role}{$item}) {
 5156:                             $changes{$role}{'bgs'}{$item} = 1;
 5157:                         }
 5158:                     }
 5159:                 }
 5160:                 foreach my $item (@links) {
 5161:                     if ($domconfig->{$role}{$item} ne '') {
 5162:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5163:                             $changes{$role}{'links'}{$item} = 1;
 5164:                         }
 5165:                     } else {
 5166:                         if ($confhash->{$role}{$item}) {
 5167:                             $changes{$role}{'links'}{$item} = 1;
 5168:                         }
 5169:                     }
 5170:                 }
 5171:                 foreach my $item (@logintext) {
 5172:                     if ($domconfig->{$role}{$item} ne '') {
 5173:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5174:                             $changes{$role}{'logintext'}{$item} = 1;
 5175:                         }
 5176:                     } else {
 5177:                         if ($confhash->{$role}{$item}) {
 5178:                             $changes{$role}{'logintext'}{$item} = 1;
 5179:                         }
 5180:                     }
 5181:                 }
 5182:             } else {
 5183:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5184:                                         \@logintext,$confhash,\%changes); 
 5185:             }
 5186:         } else {
 5187:             &default_change_checker($role,\@images,\@links,\@bgs,
 5188:                                     \@logintext,$confhash,\%changes); 
 5189:         }
 5190:     }
 5191:     return ($errors,%changes);
 5192: }
 5193: 
 5194: sub config_check {
 5195:     my ($dom,$confname,$servadm) = @_;
 5196:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5197:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5198:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5199:                                                    $confname,$servadm);
 5200:     if ($configuserok eq 'ok') {
 5201:         $switchserver = &check_switchserver($dom,$confname);
 5202:         if ($switchserver eq '') {
 5203:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5204:         }
 5205:     }
 5206:     return ($configuserok,$author_ok,$switchserver);
 5207: }
 5208: 
 5209: sub default_change_checker {
 5210:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5211:     foreach my $item (@{$links}) {
 5212:         if ($confhash->{$role}{$item}) {
 5213:             $changes->{$role}{'links'}{$item} = 1;
 5214:         }
 5215:     }
 5216:     foreach my $item (@{$bgs}) {
 5217:         if ($confhash->{$role}{$item}) {
 5218:             $changes->{$role}{'bgs'}{$item} = 1;
 5219:         }
 5220:     }
 5221:     foreach my $item (@{$logintext}) {
 5222:         if ($confhash->{$role}{$item}) {
 5223:             $changes->{$role}{'logintext'}{$item} = 1;
 5224:         }
 5225:     }
 5226:     foreach my $img (@{$images}) {
 5227:         if ($env{'form.'.$role.'_del_'.$img}) {
 5228:             $confhash->{$role}{$img} = '';
 5229:             $changes->{$role}{'images'}{$img} = 1;
 5230:         }
 5231:         if ($role eq 'login') {
 5232:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5233:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5234:             }
 5235:         }
 5236:     }
 5237:     if ($confhash->{$role}{'font'}) {
 5238:         $changes->{$role}{'font'} = 1;
 5239:     }
 5240: }
 5241: 
 5242: sub display_colorchgs {
 5243:     my ($dom,$changes,$roles,$confhash) = @_;
 5244:     my (%choices,$resulttext);
 5245:     if (!grep(/^login$/,@{$roles})) {
 5246:         $resulttext = &mt('Changes made:').'<br />';
 5247:     }
 5248:     foreach my $role (@{$roles}) {
 5249:         if ($role eq 'login') {
 5250:             %choices = &login_choices();
 5251:         } else {
 5252:             %choices = &color_font_choices();
 5253:         }
 5254:         if (ref($changes->{$role}) eq 'HASH') {
 5255:             if ($role ne 'login') {
 5256:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5257:             }
 5258:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5259:                 if ($role ne 'login') {
 5260:                     $resulttext .= '<ul>';
 5261:                 }
 5262:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5263:                     if ($role ne 'login') {
 5264:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5265:                     }
 5266:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5267:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5268:                             if ($confhash->{$role}{$key}{$item}) {
 5269:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5270:                             } else {
 5271:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5272:                             }
 5273:                         } elsif ($confhash->{$role}{$item} eq '') {
 5274:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5275:                         } else {
 5276:                             my $newitem = $confhash->{$role}{$item};
 5277:                             if ($key eq 'images') {
 5278:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5279:                             }
 5280:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5281:                         }
 5282:                     }
 5283:                     if ($role ne 'login') {
 5284:                         $resulttext .= '</ul></li>';
 5285:                     }
 5286:                 } else {
 5287:                     if ($confhash->{$role}{$key} eq '') {
 5288:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5289:                     } else {
 5290:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5291:                     }
 5292:                 }
 5293:                 if ($role ne 'login') {
 5294:                     $resulttext .= '</ul>';
 5295:                 }
 5296:             }
 5297:         }
 5298:     }
 5299:     return $resulttext;
 5300: }
 5301: 
 5302: sub thumb_dimensions {
 5303:     return ('200','50');
 5304: }
 5305: 
 5306: sub check_dimensions {
 5307:     my ($inputfile) = @_;
 5308:     my ($fullwidth,$fullheight);
 5309:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5310:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5311:             my $imageinfo = <PIPE>;
 5312:             if (!close(PIPE)) {
 5313:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5314:             }
 5315:             chomp($imageinfo);
 5316:             my ($fullsize) = 
 5317:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5318:             if ($fullsize) {
 5319:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5320:             }
 5321:         }
 5322:     }
 5323:     return ($fullwidth,$fullheight);
 5324: }
 5325: 
 5326: sub check_configuser {
 5327:     my ($uhome,$dom,$confname,$servadm) = @_;
 5328:     my ($configuserok,%currroles);
 5329:     if ($uhome eq 'no_host') {
 5330:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5331:         my $configpass = &LONCAPA::Enrollment::create_password();
 5332:         $configuserok = 
 5333:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5334:                              $configpass,'','','','','',undef,$servadm);
 5335:     } else {
 5336:         $configuserok = 'ok';
 5337:         %currroles = 
 5338:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5339:     }
 5340:     return ($configuserok,%currroles);
 5341: }
 5342: 
 5343: sub check_authorstatus {
 5344:     my ($dom,$confname,%currroles) = @_;
 5345:     my $author_ok;
 5346:     if (!$currroles{':'.$dom.':au'}) {
 5347:         my $start = time;
 5348:         my $end = 0;
 5349:         $author_ok = 
 5350:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5351:                                         'au',$end,$start,'','','domconfig');
 5352:     } else {
 5353:         $author_ok = 'ok';
 5354:     }
 5355:     return $author_ok;
 5356: }
 5357: 
 5358: sub publishlogo {
 5359:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5360:     my ($output,$fname,$logourl);
 5361:     if ($action eq 'upload') {
 5362:         $fname=$env{'form.'.$formname.'.filename'};
 5363:         chop($env{'form.'.$formname});
 5364:     } else {
 5365:         ($fname) = ($formname =~ /([^\/]+)$/);
 5366:     }
 5367:     if ($savefileas ne '') {
 5368:         $fname = $savefileas;
 5369:     }
 5370:     $fname=&Apache::lonnet::clean_filename($fname);
 5371: # See if there is anything left
 5372:     unless ($fname) { return ('error: no uploaded file'); }
 5373:     $fname="$subdir/$fname";
 5374:     my $docroot=$r->dir_config('lonDocRoot');
 5375:     my $filepath="$docroot/priv";
 5376:     my $relpath = "$dom/$confname";
 5377:     my ($fnamepath,$file,$fetchthumb);
 5378:     $file=$fname;
 5379:     if ($fname=~m|/|) {
 5380:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5381:     }
 5382:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");  
 5383:     my $count;
 5384:     for ($count=5;$count<=$#parts;$count++) {
 5385:         $filepath.="/$parts[$count]";
 5386:         if ((-e $filepath)!=1) {
 5387:             mkdir($filepath,02770);
 5388:         }
 5389:     }
 5390:     # Check for bad extension and disallow upload
 5391:     if ($file=~/\.(\w+)$/ &&
 5392:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5393:         $output = 
 5394:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 5395:     } elsif ($file=~/\.(\w+)$/ &&
 5396:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5397:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5398:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5399:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5400:     } elsif (-d "$filepath/$file") {
 5401:         $output = &mt('File name is a directory name - rename the file and re-upload');
 5402:     } else {
 5403:         my $source = $filepath.'/'.$file;
 5404:         my $logfile;
 5405:         if (!open($logfile,">>$source".'.log')) {
 5406:             return (&mt('No write permission to Construction Space'));
 5407:         }
 5408:         print $logfile
 5409: "\n================= Publish ".localtime()." ================\n".
 5410: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5411: # Save the file
 5412:         if (!open(FH,'>'.$source)) {
 5413:             &Apache::lonnet::logthis('Failed to create '.$source);
 5414:             return (&mt('Failed to create file'));
 5415:         }
 5416:         if ($action eq 'upload') {
 5417:             if (!print FH ($env{'form.'.$formname})) {
 5418:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5419:                 return (&mt('Failed to write file'));
 5420:             }
 5421:         } else {
 5422:             my $original = &Apache::lonnet::filelocation('',$formname);
 5423:             if(!copy($original,$source)) {
 5424:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5425:                 return (&mt('Failed to write file'));
 5426:             }
 5427:         }
 5428:         close(FH);
 5429:         chmod(0660, $source); # Permissions to rw-rw---.
 5430: 
 5431:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5432:         my $copyfile=$targetdir.'/'.$file;
 5433: 
 5434:         my @parts=split(/\//,$targetdir);
 5435:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5436:         for (my $count=5;$count<=$#parts;$count++) {
 5437:             $path.="/$parts[$count]";
 5438:             if (!-e $path) {
 5439:                 print $logfile "\nCreating directory ".$path;
 5440:                 mkdir($path,02770);
 5441:             }
 5442:         }
 5443:         my $versionresult;
 5444:         if (-e $copyfile) {
 5445:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5446:         } else {
 5447:             $versionresult = 'ok';
 5448:         }
 5449:         if ($versionresult eq 'ok') {
 5450:             if (copy($source,$copyfile)) {
 5451:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5452:                 $output = 'ok';
 5453:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5454:                 push(@{$modified_urls},[$copyfile,$source]);
 5455:                 my $metaoutput = 
 5456:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5457:                 unless ($registered_cleanup) {
 5458:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5459:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5460:                     $registered_cleanup=1;
 5461:                 }
 5462:             } else {
 5463:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5464:                 $output = &mt('Failed to copy file to RES space').", $!";
 5465:             }
 5466:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5467:                 my $inputfile = $filepath.'/'.$file;
 5468:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5469:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5470:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5471:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5472:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5473:                         system("convert -sample $thumbsize $inputfile $outfile");
 5474:                         chmod(0660, $filepath.'/tn-'.$file);
 5475:                         if (-e $outfile) {
 5476:                             my $copyfile=$targetdir.'/tn-'.$file;
 5477:                             if (copy($outfile,$copyfile)) {
 5478:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5479:                                 my $thumb_metaoutput = 
 5480:                                     &write_metadata($dom,$confname,$formname,
 5481:                                                     $targetdir,'tn-'.$file,$logfile);
 5482:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5483:                                 unless ($registered_cleanup) {
 5484:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5485:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5486:                                     $registered_cleanup=1;
 5487:                                 }
 5488:                             } else {
 5489:                                 print $logfile "\nUnable to write ".$copyfile.
 5490:                                                ':'.$!."\n";
 5491:                             }
 5492:                         }
 5493:                     }
 5494:                 }
 5495:             }
 5496:         } else {
 5497:             $output = $versionresult;
 5498:         }
 5499:     }
 5500:     return ($output,$logourl);
 5501: }
 5502: 
 5503: sub logo_versioning {
 5504:     my ($targetdir,$file,$logfile) = @_;
 5505:     my $target = $targetdir.'/'.$file;
 5506:     my ($maxversion,$fn,$extn,$output);
 5507:     $maxversion = 0;
 5508:     if ($file =~ /^(.+)\.(\w+)$/) {
 5509:         $fn=$1;
 5510:         $extn=$2;
 5511:     }
 5512:     opendir(DIR,$targetdir);
 5513:     while (my $filename=readdir(DIR)) {
 5514:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5515:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5516:         }
 5517:     }
 5518:     $maxversion++;
 5519:     print $logfile "\nCreating old version ".$maxversion."\n";
 5520:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5521:     if (copy($target,$copyfile)) {
 5522:         print $logfile "Copied old target to ".$copyfile."\n";
 5523:         $copyfile=$copyfile.'.meta';
 5524:         if (copy($target.'.meta',$copyfile)) {
 5525:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5526:             $output = 'ok';
 5527:         } else {
 5528:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5529:             $output = &mt('Failed to copy old meta').", $!, ";
 5530:         }
 5531:     } else {
 5532:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5533:         $output = &mt('Failed to copy old target').", $!, ";
 5534:     }
 5535:     return $output;
 5536: }
 5537: 
 5538: sub write_metadata {
 5539:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5540:     my (%metadatafields,%metadatakeys,$output);
 5541:     $metadatafields{'title'}=$formname;
 5542:     $metadatafields{'creationdate'}=time;
 5543:     $metadatafields{'lastrevisiondate'}=time;
 5544:     $metadatafields{'copyright'}='public';
 5545:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5546:                                          $env{'user.domain'};
 5547:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5548:     $metadatafields{'domain'}=$dom;
 5549:     {
 5550:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5551:         my $mfh;
 5552:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5553:             foreach (sort keys %metadatafields) {
 5554:                 unless ($_=~/\./) {
 5555:                     my $unikey=$_;
 5556:                     $unikey=~/^([A-Za-z]+)/;
 5557:                     my $tag=$1;
 5558:                     $tag=~tr/A-Z/a-z/;
 5559:                     print $mfh "\n\<$tag";
 5560:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5561:                         my $value=$metadatafields{$unikey.'.'.$_};
 5562:                         $value=~s/\"/\'\'/g;
 5563:                         print $mfh ' '.$_.'="'.$value.'"';
 5564:                     }
 5565:                     print $mfh '>'.
 5566:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5567:                             .'</'.$tag.'>';
 5568:                 }
 5569:             }
 5570:             $output = 'ok';
 5571:             print $logfile "\nWrote metadata";
 5572:             close($mfh);
 5573:         } else {
 5574:             print $logfile "\nFailed to open metadata file";
 5575:             $output = &mt('Could not write metadata');
 5576:         }
 5577:     }
 5578:     return $output;
 5579: }
 5580: 
 5581: sub notifysubscribed {
 5582:     foreach my $targetsource (@{$modified_urls}){
 5583:         next unless (ref($targetsource) eq 'ARRAY');
 5584:         my ($target,$source)=@{$targetsource};
 5585:         if ($source ne '') {
 5586:             if (open(my $logfh,'>>'.$source.'.log')) {
 5587:                 print $logfh "\nCleanup phase: Notifications\n";
 5588:                 my @subscribed=&subscribed_hosts($target);
 5589:                 foreach my $subhost (@subscribed) {
 5590:                     print $logfh "\nNotifying host ".$subhost.':';
 5591:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5592:                     print $logfh $reply;
 5593:                 }
 5594:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5595:                 foreach my $subhost (@subscribedmeta) {
 5596:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5597:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5598:                                                         $subhost);
 5599:                     print $logfh $reply;
 5600:                 }
 5601:                 print $logfh "\n============ Done ============\n";
 5602:                 close($logfh);
 5603:             }
 5604:         }
 5605:     }
 5606:     return OK;
 5607: }
 5608: 
 5609: sub subscribed_hosts {
 5610:     my ($target) = @_;
 5611:     my @subscribed;
 5612:     if (open(my $fh,"<$target.subscription")) {
 5613:         while (my $subline=<$fh>) {
 5614:             if ($subline =~ /^($match_lonid):/) {
 5615:                 my $host = $1;
 5616:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5617:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5618:                         push(@subscribed,$host);
 5619:                     }
 5620:                 }
 5621:             }
 5622:         }
 5623:     }
 5624:     return @subscribed;
 5625: }
 5626: 
 5627: sub check_switchserver {
 5628:     my ($dom,$confname) = @_;
 5629:     my ($allowed,$switchserver);
 5630:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5631:     if ($home eq 'no_host') {
 5632:         $home = &Apache::lonnet::domain($dom,'primary');
 5633:     }
 5634:     my @ids=&Apache::lonnet::current_machine_ids();
 5635:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5636:     if (!$allowed) {
 5637: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&role=dc./'.$dom.'/">'.&mt('Switch Server').'</a>';
 5638:     }
 5639:     return $switchserver;
 5640: }
 5641: 
 5642: sub modify_quotas {
 5643:     my ($dom,$action,%domconfig) = @_;
 5644:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5645:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5646:     if ($action eq 'quotas') {
 5647:         $context = 'tools'; 
 5648:     } else { 
 5649:         $context = $action;
 5650:     }
 5651:     if ($context eq 'requestcourses') {
 5652:         @usertools = ('official','unofficial','community');
 5653:         @options =('norequest','approval','validate','autolimit');
 5654:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5655:         %titles = &courserequest_titles();
 5656:         $toolregexp = join('|',@usertools);
 5657:         %conditions = &courserequest_conditions();
 5658:     } elsif ($context eq 'requestauthor') {
 5659:         @usertools = ('author');
 5660:         %titles = &authorrequest_titles();
 5661:     } else {
 5662:         @usertools = ('aboutme','blog','webdav','portfolio');
 5663:         %titles = &tool_titles();
 5664:     }
 5665:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5666:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5667:     foreach my $key (keys(%env)) {
 5668:         if ($context eq 'requestcourses') {
 5669:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5670:                 my $item = $1;
 5671:                 my $type = $2;
 5672:                 if ($type =~ /^limit_(.+)/) {
 5673:                     $limithash{$item}{$1} = $env{$key};
 5674:                 } else {
 5675:                     $confhash{$item}{$type} = $env{$key};
 5676:                 }
 5677:             }
 5678:         } elsif ($context eq 'requestauthor') {
 5679:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5680:                 $confhash{$1} = $env{$key};
 5681:             }
 5682:         } else {
 5683:             if ($key =~ /^form\.quota_(.+)$/) {
 5684:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5685:             }
 5686:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5687:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5688:             }
 5689:         }
 5690:     }
 5691:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5692:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5693:         @approvalnotify = sort(@approvalnotify);
 5694:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5695:         if (ref($domconfig{$action}) eq 'HASH') {
 5696:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5697:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5698:                     $changes{'notify'}{'approval'} = 1;
 5699:                 }
 5700:             } else {
 5701:                 if ($confhash{'notify'}{'approval'}) {
 5702:                     $changes{'notify'}{'approval'} = 1;
 5703:                 }
 5704:             }
 5705:         } else {
 5706:             if ($confhash{'notify'}{'approval'}) {
 5707:                 $changes{'notify'}{'approval'} = 1;
 5708:             }
 5709:         }
 5710:     } else {
 5711:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5712:     }
 5713:     foreach my $item (@usertools) {
 5714:         foreach my $type (@{$types},'default','_LC_adv') {
 5715:             my $unset; 
 5716:             if ($context eq 'requestcourses') {
 5717:                 $unset = '0';
 5718:                 if ($type eq '_LC_adv') {
 5719:                     $unset = '';
 5720:                 }
 5721:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5722:                     $confhash{$item}{$type} .= '=';
 5723:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5724:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5725:                     }
 5726:                 }
 5727:             } elsif ($context eq 'requestauthor') {
 5728:                 $unset = '0';
 5729:                 if ($type eq '_LC_adv') {
 5730:                     $unset = '';
 5731:                 }
 5732:             } else {
 5733:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5734:                     $confhash{$item}{$type} = 1;
 5735:                 } else {
 5736:                     $confhash{$item}{$type} = 0;
 5737:                 }
 5738:             }
 5739:             if (ref($domconfig{$action}) eq 'HASH') {
 5740:                 if ($action eq 'requestauthor') {
 5741:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5742:                         $changes{$type} = 1;
 5743:                     }
 5744:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5745:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5746:                         $changes{$item}{$type} = 1;
 5747:                     }
 5748:                 } else {
 5749:                     if ($context eq 'requestcourses') {
 5750:                         if ($confhash{$item}{$type} ne $unset) {
 5751:                             $changes{$item}{$type} = 1;
 5752:                         }
 5753:                     } else {
 5754:                         if (!$confhash{$item}{$type}) {
 5755:                             $changes{$item}{$type} = 1;
 5756:                         }
 5757:                     }
 5758:                 }
 5759:             } else {
 5760:                 if ($context eq 'requestcourses') {
 5761:                     if ($confhash{$item}{$type} ne $unset) {
 5762:                         $changes{$item}{$type} = 1;
 5763:                     }
 5764:                 } elsif ($context eq 'requestauthor') {
 5765:                     if ($confhash{$type} ne $unset) {
 5766:                         $changes{$type} = 1;
 5767:                     }
 5768:                 } else {
 5769:                     if (!$confhash{$item}{$type}) {
 5770:                         $changes{$item}{$type} = 1;
 5771:                     }
 5772:                 }
 5773:             }
 5774:         }
 5775:     }
 5776:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5777:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5778:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5779:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5780:                     if (exists($confhash{'defaultquota'}{$key})) {
 5781:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5782:                             $changes{'defaultquota'}{$key} = 1;
 5783:                         }
 5784:                     } else {
 5785:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5786:                     }
 5787:                 }
 5788:             } else {
 5789:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5790:                     if (exists($confhash{'defaultquota'}{$key})) {
 5791:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5792:                             $changes{'defaultquota'}{$key} = 1;
 5793:                         }
 5794:                     } else {
 5795:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5796:                     }
 5797:                 }
 5798:             }
 5799:         }
 5800:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5801:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5802:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5803:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5804:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5805:                             $changes{'defaultquota'}{$key} = 1;
 5806:                         }
 5807:                     } else {
 5808:                         if (!exists($domconfig{'quotas'}{$key})) {
 5809:                             $changes{'defaultquota'}{$key} = 1;
 5810:                         }
 5811:                     }
 5812:                 } else {
 5813:                     $changes{'defaultquota'}{$key} = 1;
 5814:                 }
 5815:             }
 5816:         }
 5817:     }
 5818: 
 5819:     if ($context eq 'requestauthor') {
 5820:         $domdefaults{'requestauthor'} = \%confhash;
 5821:     } else {
 5822:         foreach my $key (keys(%confhash)) {
 5823:             $domdefaults{$key} = $confhash{$key};
 5824:         }
 5825:     }
 5826: 
 5827:     my %quotahash = (
 5828:                       $action => { %confhash }
 5829:                     );
 5830:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5831:                                              $dom);
 5832:     if ($putresult eq 'ok') {
 5833:         if (keys(%changes) > 0) {
 5834:             my $cachetime = 24*60*60;
 5835:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5836: 
 5837:             $resulttext = &mt('Changes made:').'<ul>';
 5838:             unless (($context eq 'requestcourses') ||
 5839:                     ($context eq 'requestauthor')) {
 5840:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5841:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5842:                     foreach my $type (@{$types},'default') {
 5843:                         if (defined($changes{'defaultquota'}{$type})) {
 5844:                             my $typetitle = $usertypes->{$type};
 5845:                             if ($type eq 'default') {
 5846:                                 $typetitle = $othertitle;
 5847:                             }
 5848:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5849:                         }
 5850:                     }
 5851:                     $resulttext .= '</ul></li>';
 5852:                 }
 5853:             }
 5854:             my %newenv;
 5855:             foreach my $item (@usertools) {
 5856:                 my (%haschgs,%inconf);
 5857:                 if ($context eq 'requestauthor') {
 5858:                     %haschgs = %changes;
 5859:                     %inconf = %confhash;
 5860:                 } else {
 5861:                     if (ref($changes{$item}) eq 'HASH') {
 5862:                         %haschgs = %{$changes{$item}};
 5863:                     }
 5864:                     if (ref($confhash{$item}) eq 'HASH') {
 5865:                         %inconf = %{$confhash{$item}};
 5866:                     }
 5867:                 }
 5868:                 if (keys(%haschgs) > 0) {
 5869:                     my $newacc = 
 5870:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5871:                                                           $env{'user.domain'},
 5872:                                                           $item,'reload',$context);
 5873:                     if (($context eq 'requestcourses') ||
 5874:                         ($context eq 'requestauthor')) {
 5875:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5876:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5877:                         }
 5878:                     } else {
 5879:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5880:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5881:                         }
 5882:                     }
 5883:                     unless ($context eq 'requestauthor') {
 5884:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5885:                     }
 5886:                     foreach my $type (@{$types},'default','_LC_adv') {
 5887:                         if ($haschgs{$type}) {
 5888:                             my $typetitle = $usertypes->{$type};
 5889:                             if ($type eq 'default') {
 5890:                                 $typetitle = $othertitle;
 5891:                             } elsif ($type eq '_LC_adv') {
 5892:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5893:                             }
 5894:                             if ($inconf{$type}) {
 5895:                                 if ($context eq 'requestcourses') {
 5896:                                     my $cond;
 5897:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 5898:                                         if ($1 eq '') {
 5899:                                             $cond = &mt('(Automatic processing of any request).');
 5900:                                         } else {
 5901:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5902:                                         }
 5903:                                     } else { 
 5904:                                         $cond = $conditions{$inconf{$type}};
 5905:                                     }
 5906:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5907:                                 } elsif ($context eq 'requestauthor') {
 5908:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 5909:                                                              $titles{$inconf{$type}},$typetitle);
 5910: 
 5911:                                 } else {
 5912:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5913:                                 }
 5914:                             } else {
 5915:                                 if ($type eq '_LC_adv') {
 5916:                                     if ($inconf{$type} eq '0') {
 5917:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5918:                                     } else { 
 5919:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5920:                                     }
 5921:                                 } else {
 5922:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5923:                                 }
 5924:                             }
 5925:                         }
 5926:                     }
 5927:                     unless ($context eq 'requestauthor') {
 5928:                         $resulttext .= '</ul></li>';
 5929:                     }
 5930:                 }
 5931:             }
 5932:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 5933:                 if (ref($changes{'notify'}) eq 'HASH') {
 5934:                     if ($changes{'notify'}{'approval'}) {
 5935:                         if (ref($confhash{'notify'}) eq 'HASH') {
 5936:                             if ($confhash{'notify'}{'approval'}) {
 5937:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 5938:                             } else {
 5939:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 5940:                             }
 5941:                         }
 5942:                     }
 5943:                 }
 5944:             }
 5945:             $resulttext .= '</ul>';
 5946:             if (keys(%newenv)) {
 5947:                 &Apache::lonnet::appenv(\%newenv);
 5948:             }
 5949:         } else {
 5950:             if ($context eq 'requestcourses') {
 5951:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 5952:             } elsif ($context eq 'requestauthor') {
 5953:                 $resulttext = &mt('No changes made to rights to request author space.');
 5954:             } else {
 5955:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 5956:             }
 5957:         }
 5958:     } else {
 5959:         $resulttext = '<span class="LC_error">'.
 5960: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5961:     }
 5962:     return $resulttext;
 5963: }
 5964: 
 5965: sub modify_autoenroll {
 5966:     my ($dom,%domconfig) = @_;
 5967:     my ($resulttext,%changes);
 5968:     my %currautoenroll;
 5969:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 5970:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 5971:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 5972:         }
 5973:     }
 5974:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 5975:     my %title = ( run => 'Auto-enrollment active',
 5976:                   sender => 'Sender for notification messages',
 5977:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 5978:     my @offon = ('off','on');
 5979:     my $sender_uname = $env{'form.sender_uname'};
 5980:     my $sender_domain = $env{'form.sender_domain'};
 5981:     if ($sender_domain eq '') {
 5982:         $sender_uname = '';
 5983:     } elsif ($sender_uname eq '') {
 5984:         $sender_domain = '';
 5985:     }
 5986:     my $coowners = $env{'form.autoassign_coowners'};
 5987:     my %autoenrollhash =  (
 5988:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 5989:                                        'sender_uname' => $sender_uname,
 5990:                                        'sender_domain' => $sender_domain,
 5991:                                        'co-owners' => $coowners,
 5992:                                 }
 5993:                      );
 5994:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 5995:                                              $dom);
 5996:     if ($putresult eq 'ok') {
 5997:         if (exists($currautoenroll{'run'})) {
 5998:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 5999:                  $changes{'run'} = 1;
 6000:              }
 6001:         } elsif ($autorun) {
 6002:             if ($env{'form.autoenroll_run'} ne '1') {
 6003:                  $changes{'run'} = 1;
 6004:             }
 6005:         }
 6006:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6007:             $changes{'sender'} = 1;
 6008:         }
 6009:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6010:             $changes{'sender'} = 1;
 6011:         }
 6012:         if ($currautoenroll{'co-owners'} ne '') {
 6013:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6014:                 $changes{'coowners'} = 1;
 6015:             }
 6016:         } elsif ($coowners) {
 6017:             $changes{'coowners'} = 1;
 6018:         }      
 6019:         if (keys(%changes) > 0) {
 6020:             $resulttext = &mt('Changes made:').'<ul>';
 6021:             if ($changes{'run'}) {
 6022:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6023:             }
 6024:             if ($changes{'sender'}) {
 6025:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6026:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6027:                 } else {
 6028:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6029:                 }
 6030:             }
 6031:             if ($changes{'coowners'}) {
 6032:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6033:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6034:             }
 6035:             $resulttext .= '</ul>';
 6036:         } else {
 6037:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6038:         }
 6039:     } else {
 6040:         $resulttext = '<span class="LC_error">'.
 6041: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6042:     }
 6043:     return $resulttext;
 6044: }
 6045: 
 6046: sub modify_autoupdate {
 6047:     my ($dom,%domconfig) = @_;
 6048:     my ($resulttext,%currautoupdate,%fields,%changes);
 6049:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6050:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6051:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6052:         }
 6053:     }
 6054:     my @offon = ('off','on');
 6055:     my %title = &Apache::lonlocal::texthash (
 6056:                    run => 'Auto-update:',
 6057:                    classlists => 'Updates to user information in classlists?'
 6058:                 );
 6059:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6060:     my %fieldtitles = &Apache::lonlocal::texthash (
 6061:                         id => 'Student/Employee ID',
 6062:                         permanentemail => 'E-mail address',
 6063:                         lastname => 'Last Name',
 6064:                         firstname => 'First Name',
 6065:                         middlename => 'Middle Name',
 6066:                         generation => 'Generation',
 6067:                       );
 6068:     $othertitle = &mt('All users');
 6069:     if (keys(%{$usertypes}) >  0) {
 6070:         $othertitle = &mt('Other users');
 6071:     }
 6072:     foreach my $key (keys(%env)) {
 6073:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6074:             my ($usertype,$item) = ($1,$2);
 6075:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6076:                 if ($usertype eq 'default') {   
 6077:                     push(@{$fields{$1}},$2);
 6078:                 } elsif (ref($types) eq 'ARRAY') {
 6079:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6080:                         push(@{$fields{$1}},$2);
 6081:                     }
 6082:                 }
 6083:             }
 6084:         }
 6085:     }
 6086:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6087:     @lockablenames = sort(@lockablenames);
 6088:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6089:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6090:         if (@changed) {
 6091:             $changes{'lockablenames'} = 1;
 6092:         }
 6093:     } else {
 6094:         if (@lockablenames) {
 6095:             $changes{'lockablenames'} = 1;
 6096:         }
 6097:     }
 6098:     my %updatehash = (
 6099:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6100:                                       classlists => $env{'form.classlists'},
 6101:                                       fields => {%fields},
 6102:                                       lockablenames => \@lockablenames,
 6103:                                     }
 6104:                      );
 6105:     foreach my $key (keys(%currautoupdate)) {
 6106:         if (($key eq 'run') || ($key eq 'classlists')) {
 6107:             if (exists($updatehash{autoupdate}{$key})) {
 6108:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6109:                     $changes{$key} = 1;
 6110:                 }
 6111:             }
 6112:         } elsif ($key eq 'fields') {
 6113:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6114:                 foreach my $item (@{$types},'default') {
 6115:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6116:                         my $change = 0;
 6117:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6118:                             if (!exists($fields{$item})) {
 6119:                                 $change = 1;
 6120:                                 last;
 6121:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6122:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6123:                                     $change = 1;
 6124:                                     last;
 6125:                                 }
 6126:                             }
 6127:                         }
 6128:                         if ($change) {
 6129:                             push(@{$changes{$key}},$item);
 6130:                         }
 6131:                     } 
 6132:                 }
 6133:             }
 6134:         } elsif ($key eq 'lockablenames') {
 6135:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6136:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6137:                 if (@changed) {
 6138:                     $changes{'lockablenames'} = 1;
 6139:                 }
 6140:             } else {
 6141:                 if (@lockablenames) {
 6142:                     $changes{'lockablenames'} = 1;
 6143:                 }
 6144:             }
 6145:         }
 6146:     }
 6147:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6148:         if (@lockablenames) {
 6149:             $changes{'lockablenames'} = 1;
 6150:         }
 6151:     }
 6152:     foreach my $item (@{$types},'default') {
 6153:         if (defined($fields{$item})) {
 6154:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6155:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6156:                     my $change = 0;
 6157:                     if (ref($fields{$item}) eq 'ARRAY') {
 6158:                         foreach my $type (@{$fields{$item}}) {
 6159:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6160:                                 $change = 1;
 6161:                                 last;
 6162:                             }
 6163:                         }
 6164:                     }
 6165:                     if ($change) {
 6166:                         push(@{$changes{'fields'}},$item);
 6167:                     }
 6168:                 } else {
 6169:                     push(@{$changes{'fields'}},$item);
 6170:                 }
 6171:             } else {
 6172:                 push(@{$changes{'fields'}},$item);
 6173:             }
 6174:         }
 6175:     }
 6176:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6177:                                              $dom);
 6178:     if ($putresult eq 'ok') {
 6179:         if (keys(%changes) > 0) {
 6180:             $resulttext = &mt('Changes made:').'<ul>';
 6181:             foreach my $key (sort(keys(%changes))) {
 6182:                 if ($key eq 'lockablenames') {
 6183:                     $resulttext .= '<li>';
 6184:                     if (@lockablenames) {
 6185:                         $usertypes->{'default'} = $othertitle;
 6186:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6187:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6188:                     } else {
 6189:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6190:                     }
 6191:                     $resulttext .= '</li>';
 6192:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6193:                     foreach my $item (@{$changes{$key}}) {
 6194:                         my @newvalues;
 6195:                         foreach my $type (@{$fields{$item}}) {
 6196:                             push(@newvalues,$fieldtitles{$type});
 6197:                         }
 6198:                         my $newvaluestr;
 6199:                         if (@newvalues > 0) {
 6200:                             $newvaluestr = join(', ',@newvalues);
 6201:                         } else {
 6202:                             $newvaluestr = &mt('none');
 6203:                         }
 6204:                         if ($item eq 'default') {
 6205:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6206:                         } else {
 6207:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6208:                         }
 6209:                     }
 6210:                 } else {
 6211:                     my $newvalue;
 6212:                     if ($key eq 'run') {
 6213:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6214:                     } else {
 6215:                         $newvalue = $offon[$env{'form.'.$key}];
 6216:                     }
 6217:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6218:                 }
 6219:             }
 6220:             $resulttext .= '</ul>';
 6221:         } else {
 6222:             $resulttext = &mt('No changes made to autoupdates');
 6223:         }
 6224:     } else {
 6225:         $resulttext = '<span class="LC_error">'.
 6226: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6227:     }
 6228:     return $resulttext;
 6229: }
 6230: 
 6231: sub modify_autocreate {
 6232:     my ($dom,%domconfig) = @_;
 6233:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6234:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6235:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6236:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6237:         }
 6238:     }
 6239:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6240:                  req => 'Auto-creation of validated requests for official courses',
 6241:                  xmldc => 'Identity of course creator of courses from XML files',
 6242:                );
 6243:     my @types = ('xml','req');
 6244:     foreach my $item (@types) {
 6245:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6246:         $newvals{$item} =~ s/\D//g;
 6247:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6248:     }
 6249:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6250:     my %domcoords = &get_active_dcs($dom);
 6251:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6252:         $newvals{'xmldc'} = '';
 6253:     } 
 6254:     %autocreatehash =  (
 6255:                         autocreate => { xml => $newvals{'xml'},
 6256:                                         req => $newvals{'req'},
 6257:                                       }
 6258:                        );
 6259:     if ($newvals{'xmldc'} ne '') {
 6260:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6261:     }
 6262:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6263:                                              $dom);
 6264:     if ($putresult eq 'ok') {
 6265:         my @items = @types;
 6266:         if ($newvals{'xml'}) {
 6267:             push(@items,'xmldc');
 6268:         }
 6269:         foreach my $item (@items) {
 6270:             if (exists($currautocreate{$item})) {
 6271:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6272:                     $changes{$item} = 1;
 6273:                 }
 6274:             } elsif ($newvals{$item}) {
 6275:                 $changes{$item} = 1;
 6276:             }
 6277:         }
 6278:         if (keys(%changes) > 0) {
 6279:             my @offon = ('off','on'); 
 6280:             $resulttext = &mt('Changes made:').'<ul>';
 6281:             foreach my $item (@types) {
 6282:                 if ($changes{$item}) {
 6283:                     my $newtxt = $offon[$newvals{$item}];
 6284:                     $resulttext .= '<li>'.&mt("$title{$item} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>';
 6285:                 }
 6286:             }
 6287:             if ($changes{'xmldc'}) {
 6288:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6289:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6290:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>'; 
 6291:             }
 6292:             $resulttext .= '</ul>';
 6293:         } else {
 6294:             $resulttext = &mt('No changes made to auto-creation settings');
 6295:         }
 6296:     } else {
 6297:         $resulttext = '<span class="LC_error">'.
 6298:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6299:     }
 6300:     return $resulttext;
 6301: }
 6302: 
 6303: sub modify_directorysrch {
 6304:     my ($dom,%domconfig) = @_;
 6305:     my ($resulttext,%changes);
 6306:     my %currdirsrch;
 6307:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6308:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6309:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6310:         }
 6311:     }
 6312:     my %title = ( available => 'Directory search available',
 6313:                   localonly => 'Other domains can search',
 6314:                   searchby => 'Search types',
 6315:                   searchtypes => 'Search latitude');
 6316:     my @offon = ('off','on');
 6317:     my @otherdoms = ('Yes','No');
 6318: 
 6319:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6320:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6321:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6322: 
 6323:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6324:     if (keys(%{$usertypes}) == 0) {
 6325:         @cansearch = ('default');
 6326:     } else {
 6327:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6328:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6329:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6330:                     push(@{$changes{'cansearch'}},$type);
 6331:                 }
 6332:             }
 6333:             foreach my $type (@cansearch) {
 6334:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6335:                     push(@{$changes{'cansearch'}},$type);
 6336:                 }
 6337:             }
 6338:         } else {
 6339:             push(@{$changes{'cansearch'}},@cansearch);
 6340:         }
 6341:     }
 6342: 
 6343:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6344:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6345:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6346:                 push(@{$changes{'searchby'}},$by);
 6347:             }
 6348:         }
 6349:         foreach my $by (@searchby) {
 6350:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6351:                 push(@{$changes{'searchby'}},$by);
 6352:             }
 6353:         }
 6354:     } else {
 6355:         push(@{$changes{'searchby'}},@searchby);
 6356:     }
 6357: 
 6358:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6359:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6360:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6361:                 push(@{$changes{'searchtypes'}},$type);
 6362:             }
 6363:         }
 6364:         foreach my $type (@searchtypes) {
 6365:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6366:                 push(@{$changes{'searchtypes'}},$type);
 6367:             }
 6368:         }
 6369:     } else {
 6370:         if (exists($currdirsrch{'searchtypes'})) {
 6371:             foreach my $type (@searchtypes) {  
 6372:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6373:                     push(@{$changes{'searchtypes'}},$type);
 6374:                 }
 6375:             }
 6376:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6377:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6378:             }   
 6379:         } else {
 6380:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6381:         }
 6382:     }
 6383: 
 6384:     my %dirsrch_hash =  (
 6385:             directorysrch => { available => $env{'form.dirsrch_available'},
 6386:                                cansearch => \@cansearch,
 6387:                                localonly => $env{'form.dirsrch_localonly'},
 6388:                                searchby => \@searchby,
 6389:                                searchtypes => \@searchtypes,
 6390:                              }
 6391:             );
 6392:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6393:                                              $dom);
 6394:     if ($putresult eq 'ok') {
 6395:         if (exists($currdirsrch{'available'})) {
 6396:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6397:                  $changes{'available'} = 1;
 6398:              }
 6399:         } else {
 6400:             if ($env{'form.dirsrch_available'} eq '1') {
 6401:                 $changes{'available'} = 1;
 6402:             }
 6403:         }
 6404:         if (exists($currdirsrch{'localonly'})) {
 6405:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6406:                  $changes{'localonly'} = 1;
 6407:              }
 6408:         } else {
 6409:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6410:                 $changes{'localonly'} = 1;
 6411:             }
 6412:         }
 6413:         if (keys(%changes) > 0) {
 6414:             $resulttext = &mt('Changes made:').'<ul>';
 6415:             if ($changes{'available'}) {
 6416:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6417:             }
 6418:             if ($changes{'localonly'}) {
 6419:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6420:             }
 6421: 
 6422:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6423:                 my $chgtext;
 6424:                 if (ref($usertypes) eq 'HASH') {
 6425:                     if (keys(%{$usertypes}) > 0) {
 6426:                         foreach my $type (@{$types}) {
 6427:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6428:                                 $chgtext .= $usertypes->{$type}.'; ';
 6429:                             }
 6430:                         }
 6431:                         if (grep(/^default$/,@cansearch)) {
 6432:                             $chgtext .= $othertitle;
 6433:                         } else {
 6434:                             $chgtext =~ s/\; $//;
 6435:                         }
 6436:                         $resulttext .= '<li>'.&mt("Users from domain '<span class=\"LC_cusr_emph\">[_1]</span>' permitted to search the institutional directory set to: [_2]",$dom,$chgtext).'</li>';
 6437:                     }
 6438:                 }
 6439:             }
 6440:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6441:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6442:                 my $chgtext;
 6443:                 foreach my $type (@{$titleorder}) {
 6444:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6445:                         if (defined($searchtitles->{$type})) {
 6446:                             $chgtext .= $searchtitles->{$type}.'; ';
 6447:                         }
 6448:                     }
 6449:                 }
 6450:                 $chgtext =~ s/\; $//;
 6451:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6452:             }
 6453:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6454:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6455:                 my $chgtext;
 6456:                 foreach my $type (@{$srchtypeorder}) {
 6457:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6458:                         if (defined($srchtypes_desc->{$type})) {
 6459:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6460:                         }
 6461:                     }
 6462:                 }
 6463:                 $chgtext =~ s/\; $//;
 6464:                 $resulttext .= '<li>'.&mt("$title{'searchtypes'} set to: \"[_1]\"",$chgtext).'</li>';
 6465:             }
 6466:             $resulttext .= '</ul>';
 6467:         } else {
 6468:             $resulttext = &mt('No changes made to institution directory search settings');
 6469:         }
 6470:     } else {
 6471:         $resulttext = '<span class="LC_error">'.
 6472:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6473:     }
 6474:     return $resulttext;
 6475: }
 6476: 
 6477: sub modify_contacts {
 6478:     my ($dom,%domconfig) = @_;
 6479:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6480:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6481:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6482:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6483:         }
 6484:     }
 6485:     my (%others,%to,%bcc);
 6486:     my @contacts = ('supportemail','adminemail');
 6487:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6488:                     'requestsmail');
 6489:     foreach my $type (@mailings) {
 6490:         @{$newsetting{$type}} = 
 6491:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6492:         foreach my $item (@contacts) {
 6493:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6494:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6495:             } else {
 6496:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6497:             }
 6498:         }  
 6499:         $others{$type} = $env{'form.'.$type.'_others'};
 6500:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6501:         if ($type eq 'helpdeskmail') {
 6502:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6503:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6504:         }
 6505:     }
 6506:     foreach my $item (@contacts) {
 6507:         $to{$item} = $env{'form.'.$item};
 6508:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6509:     }
 6510:     if (keys(%currsetting) > 0) {
 6511:         foreach my $item (@contacts) {
 6512:             if ($to{$item} ne $currsetting{$item}) {
 6513:                 $changes{$item} = 1;
 6514:             }
 6515:         }
 6516:         foreach my $type (@mailings) {
 6517:             foreach my $item (@contacts) {
 6518:                 if (ref($currsetting{$type}) eq 'HASH') {
 6519:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6520:                         push(@{$changes{$type}},$item);
 6521:                     }
 6522:                 } else {
 6523:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6524:                 }
 6525:             }
 6526:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6527:                 push(@{$changes{$type}},'others');
 6528:             }
 6529:             if ($type eq 'helpdeskmail') {   
 6530:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6531:                     push(@{$changes{$type}},'bcc'); 
 6532:                 }
 6533:             }
 6534:         }
 6535:     } else {
 6536:         my %default;
 6537:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6538:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6539:         $default{'errormail'} = 'adminemail';
 6540:         $default{'packagesmail'} = 'adminemail';
 6541:         $default{'helpdeskmail'} = 'supportemail';
 6542:         $default{'lonstatusmail'} = 'adminemail';
 6543:         $default{'requestsmail'} = 'adminemail';
 6544:         foreach my $item (@contacts) {
 6545:            if ($to{$item} ne $default{$item}) {
 6546:               $changes{$item} = 1;
 6547:            } 
 6548:         }
 6549:         foreach my $type (@mailings) {
 6550:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6551:                
 6552:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6553:             }
 6554:             if ($others{$type} ne '') {
 6555:                 push(@{$changes{$type}},'others');
 6556:             }
 6557:             if ($type eq 'helpdeskmail') {
 6558:                 if ($bcc{$type} ne '') {
 6559:                     push(@{$changes{$type}},'bcc');
 6560:                 }
 6561:             }
 6562:         }
 6563:     }
 6564:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6565:                                              $dom);
 6566:     if ($putresult eq 'ok') {
 6567:         if (keys(%changes) > 0) {
 6568:             my ($titles,$short_titles)  = &contact_titles();
 6569:             $resulttext = &mt('Changes made:').'<ul>';
 6570:             foreach my $item (@contacts) {
 6571:                 if ($changes{$item}) {
 6572:                     $resulttext .= '<li>'.$titles->{$item}.
 6573:                                     &mt(' set to: ').
 6574:                                     '<span class="LC_cusr_emph">'.
 6575:                                     $to{$item}.'</span></li>';
 6576:                 }
 6577:             }
 6578:             foreach my $type (@mailings) {
 6579:                 if (ref($changes{$type}) eq 'ARRAY') {
 6580:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6581:                     my @text;
 6582:                     foreach my $item (@{$newsetting{$type}}) {
 6583:                         push(@text,$short_titles->{$item});
 6584:                     }
 6585:                     if ($others{$type} ne '') {
 6586:                         push(@text,$others{$type});
 6587:                     }
 6588:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6589:                                    join(', ',@text).'</span>';
 6590:                     if ($type eq 'helpdeskmail') {
 6591:                         if ($bcc{$type} ne '') {
 6592:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6593:                         }
 6594:                     }
 6595:                     $resulttext .= '</li>';
 6596:                 }
 6597:             }
 6598:             $resulttext .= '</ul>';
 6599:         } else {
 6600:             $resulttext = &mt('No changes made to contact information');
 6601:         }
 6602:     } else {
 6603:         $resulttext = '<span class="LC_error">'.
 6604:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6605:     }
 6606:     return $resulttext;
 6607: }
 6608: 
 6609: sub modify_usercreation {
 6610:     my ($dom,%domconfig) = @_;
 6611:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6612:     my $warningmsg;
 6613:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6614:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6615:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6616:         }
 6617:     }
 6618:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6619:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6620:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6621:     my @contexts = ('author','course','requestcrs','selfcreate');
 6622:     foreach my $item(@contexts) {
 6623:         if ($item eq 'selfcreate') {
 6624:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6625:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6626:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6627:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6628:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6629:                         $warningmsg = &mt('Although account creation has been set to be available for institutional logins, currently default authentication in this domain has not been set to support this.').' '.&mt('You need to set the default authentication type to Kerberos 4 or 5 (with a Kerberos domain specified), or to Local authentication, if the localauth module has been customized in your domain to authenticate institutional logins.');   
 6630:                     }
 6631:                 }
 6632:             }
 6633:         } else {
 6634:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6635:         }
 6636:     }
 6637:     my ($othertitle,$usertypes,$types) = 
 6638:         &Apache::loncommon::sorted_inst_types($dom);
 6639:     if (ref($types) eq 'ARRAY') {
 6640:         if (@{$types} > 0) {
 6641:             @{$cancreate{'statustocreate'}} = 
 6642:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6643:         } else {
 6644:             @{$cancreate{'statustocreate'}} = ();
 6645:         }
 6646:         push(@contexts,'statustocreate');
 6647:     }
 6648:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6649:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6650:         foreach my $item (@contexts) {
 6651:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6652:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6653:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6654:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6655:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6656:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6657:                                     push(@{$changes{'cancreate'}},$item);
 6658:                                 }
 6659:                             }
 6660:                         }
 6661:                     }
 6662:                 } else {
 6663:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6664:                         if (@{$cancreate{$item}} > 0) {
 6665:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6666:                                 push(@{$changes{'cancreate'}},$item);
 6667:                             }
 6668:                         }
 6669:                     } else {
 6670:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6671:                             if (@{$cancreate{$item}} < 3) {
 6672:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6673:                                     push(@{$changes{'cancreate'}},$item);
 6674:                                 }
 6675:                             }
 6676:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6677:                             if (@{$cancreate{$item}} > 0) {
 6678:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6679:                                     push(@{$changes{'cancreate'}},$item);
 6680:                                 }
 6681:                             }
 6682:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6683:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6684:                                 push(@{$changes{'cancreate'}},$item);
 6685:                             }
 6686:                         }
 6687:                     }
 6688:                 }
 6689:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6690:                     foreach my $type (@{$cancreate{$item}}) {
 6691:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6692:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6693:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6694:                                     push(@{$changes{'cancreate'}},$item);
 6695:                                 }
 6696:                             }
 6697:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6698:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6699:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6700:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6701:                                     push(@{$changes{'cancreate'}},$item);
 6702:                                 }
 6703:                             }
 6704:                         }
 6705:                     }
 6706:                 }
 6707:             } else {
 6708:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6709:                     push(@{$changes{'cancreate'}},$item);
 6710:                 }
 6711:             }
 6712:         }
 6713:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6714:         foreach my $item (@contexts) {
 6715:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6716:                 if ($cancreate{$item} ne 'any') {
 6717:                     push(@{$changes{'cancreate'}},$item);
 6718:                 }
 6719:             } else {
 6720:                 if ($cancreate{$item} ne 'none') {
 6721:                     push(@{$changes{'cancreate'}},$item);
 6722:                 }
 6723:             }
 6724:         }
 6725:     } else {
 6726:         foreach my $item (@contexts)  {
 6727:             push(@{$changes{'cancreate'}},$item);
 6728:         }
 6729:     }
 6730: 
 6731:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6732:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6733:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6734:                 push(@{$changes{'username_rule'}},$type);
 6735:             }
 6736:         }
 6737:         foreach my $type (@username_rule) {
 6738:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6739:                 push(@{$changes{'username_rule'}},$type);
 6740:             }
 6741:         }
 6742:     } else {
 6743:         push(@{$changes{'username_rule'}},@username_rule);
 6744:     }
 6745: 
 6746:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6747:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 6748:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 6749:                 push(@{$changes{'id_rule'}},$type);
 6750:             }
 6751:         }
 6752:         foreach my $type (@id_rule) {
 6753:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 6754:                 push(@{$changes{'id_rule'}},$type);
 6755:             }
 6756:         }
 6757:     } else {
 6758:         push(@{$changes{'id_rule'}},@id_rule);
 6759:     }
 6760: 
 6761:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 6762:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 6763:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 6764:                 push(@{$changes{'email_rule'}},$type);
 6765:             }
 6766:         }
 6767:         foreach my $type (@email_rule) {
 6768:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 6769:                 push(@{$changes{'email_rule'}},$type);
 6770:             }
 6771:         }
 6772:     } else {
 6773:         push(@{$changes{'email_rule'}},@email_rule);
 6774:     }
 6775: 
 6776:     my @authen_contexts = ('author','course','domain');
 6777:     my @authtypes = ('int','krb4','krb5','loc');
 6778:     my %authhash;
 6779:     foreach my $item (@authen_contexts) {
 6780:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 6781:         foreach my $auth (@authtypes) {
 6782:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 6783:                 $authhash{$item}{$auth} = 1;
 6784:             } else {
 6785:                 $authhash{$item}{$auth} = 0;
 6786:             }
 6787:         }
 6788:     }
 6789:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 6790:         foreach my $item (@authen_contexts) {
 6791:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 6792:                 foreach my $auth (@authtypes) {
 6793:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 6794:                         push(@{$changes{'authtypes'}},$item);
 6795:                         last;
 6796:                     }
 6797:                 }
 6798:             }
 6799:         }
 6800:     } else {
 6801:         foreach my $item (@authen_contexts) {
 6802:             push(@{$changes{'authtypes'}},$item);
 6803:         }
 6804:     }
 6805: 
 6806:     my %usercreation_hash =  (
 6807:             usercreation => {
 6808:                               cancreate     => \%cancreate,
 6809:                               username_rule => \@username_rule,
 6810:                               id_rule       => \@id_rule,
 6811:                               email_rule    => \@email_rule,
 6812:                               authtypes     => \%authhash,
 6813:                             }
 6814:             );
 6815: 
 6816:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 6817:                                              $dom);
 6818: 
 6819:     my %selfcreatetypes = (
 6820:                              sso   => 'users authenticated by institutional single sign on',
 6821:                              login => 'users authenticated by institutional log-in',
 6822:                              email => 'users who provide a valid e-mail address for use as the username',
 6823:                           );
 6824:     if ($putresult eq 'ok') {
 6825:         if (keys(%changes) > 0) {
 6826:             $resulttext = &mt('Changes made:').'<ul>';
 6827:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 6828:                 my %lt = &usercreation_types();
 6829:                 foreach my $type (@{$changes{'cancreate'}}) {
 6830:                     my $chgtext;
 6831:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 6832:                         $chgtext = $lt{$type}.', ';
 6833:                     }
 6834:                     if ($type eq 'selfcreate') {
 6835:                         if (@{$cancreate{$type}} == 0) {
 6836:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 6837:                         } else {
 6838:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 6839:                             foreach my $case (@{$cancreate{$type}}) {
 6840:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 6841:                             }
 6842:                             $chgtext .= '</ul>';
 6843:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 6844:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 6845:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 6846:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 6847:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6848:                                         }
 6849:                                     }
 6850:                                 }
 6851:                             }
 6852:                         }
 6853:                     } elsif ($type eq 'statustocreate') {
 6854:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 6855:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 6856:                             if (@{$cancreate{'selfcreate'}} > 0) {
 6857:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 6858: 
 6859:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 6860:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 6861:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6862:                                     } 
 6863:                                 } elsif (ref($usertypes) eq 'HASH') {
 6864:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6865:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 6866:                                     } else {
 6867:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 6868:                                     }
 6869:                                     $chgtext .= '<ul>';
 6870:                                     foreach my $case (@{$cancreate{$type}}) {
 6871:                                         if ($case eq 'default') {
 6872:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 6873:                                         } else {
 6874:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 6875:                                         }
 6876:                                     }
 6877:                                     $chgtext .= '</ul>';
 6878:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6879:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 6880:                                     }
 6881:                                 }
 6882:                             } else {
 6883:                                 if (@{$cancreate{$type}} == 0) {
 6884:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 6885:                                 } else {
 6886:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 6887:                                 }
 6888:                             }
 6889:                         }
 6890:                     } elsif ($type eq 'captcha') {
 6891:                         if ($cancreate{$type} eq 'notused') {
 6892:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 6893:                         } else {
 6894:                             my %captchas = &captcha_phrases();
 6895:                             if ($captchas{$cancreate{$type}}) {
 6896:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 6897:                             } else {
 6898:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 6899:                             }
 6900:                         }
 6901:                     } elsif ($type eq 'recaptchakeys') {
 6902:                         my ($privkey,$pubkey);
 6903:                         if (ref($cancreate{$type}) eq 'HASH') {
 6904:                             $pubkey = $cancreate{$type}{'public'};
 6905:                             $privkey = $cancreate{$type}{'private'};
 6906:                         }
 6907:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 6908:                         if (!$pubkey) {
 6909:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 6910:                         } else {
 6911:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6912:                         }
 6913:                         if (!$privkey) {
 6914:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 6915:                         } else {
 6916:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6917:                         }
 6918:                         $chgtext .= '</ul>';
 6919:                     } else {
 6920:                         if ($cancreate{$type} eq 'none') {
 6921:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 6922:                         } elsif ($cancreate{$type} eq 'any') {
 6923:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 6924:                         } elsif ($cancreate{$type} eq 'official') {
 6925:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 6926:                         } elsif ($cancreate{$type} eq 'unofficial') {
 6927:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 6928:                         }
 6929:                     }
 6930:                     $resulttext .= '<li>'.$chgtext.'</li>';
 6931:                 }
 6932:             }
 6933:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 6934:                 my ($rules,$ruleorder) = 
 6935:                     &Apache::lonnet::inst_userrules($dom,'username');
 6936:                 my $chgtext = '<ul>';
 6937:                 foreach my $type (@username_rule) {
 6938:                     if (ref($rules->{$type}) eq 'HASH') {
 6939:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 6940:                     }
 6941:                 }
 6942:                 $chgtext .= '</ul>';
 6943:                 if (@username_rule > 0) {
 6944:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 6945:                 } else {
 6946:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 6947:                 }
 6948:             }
 6949:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 6950:                 my ($idrules,$idruleorder) = 
 6951:                     &Apache::lonnet::inst_userrules($dom,'id');
 6952:                 my $chgtext = '<ul>';
 6953:                 foreach my $type (@id_rule) {
 6954:                     if (ref($idrules->{$type}) eq 'HASH') {
 6955:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 6956:                     }
 6957:                 }
 6958:                 $chgtext .= '</ul>';
 6959:                 if (@id_rule > 0) {
 6960:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 6961:                 } else {
 6962:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 6963:                 }
 6964:             }
 6965:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 6966:                 my ($emailrules,$emailruleorder) =
 6967:                     &Apache::lonnet::inst_userrules($dom,'email');
 6968:                 my $chgtext = '<ul>';
 6969:                 foreach my $type (@email_rule) {
 6970:                     if (ref($emailrules->{$type}) eq 'HASH') {
 6971:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 6972:                     }
 6973:                 }
 6974:                 $chgtext .= '</ul>';
 6975:                 if (@email_rule > 0) {
 6976:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 6977:                 } else {
 6978:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 6979:                 }
 6980:             }
 6981: 
 6982:             my %authname = &authtype_names();
 6983:             my %context_title = &context_names();
 6984:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 6985:                 my $chgtext = '<ul>';
 6986:                 foreach my $type (@{$changes{'authtypes'}}) {
 6987:                     my @allowed;
 6988:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 6989:                     foreach my $auth (@authtypes) {
 6990:                         if ($authhash{$type}{$auth}) {
 6991:                             push(@allowed,$authname{$auth});
 6992:                         }
 6993:                     }
 6994:                     if (@allowed > 0) {
 6995:                         $chgtext .= join(', ',@allowed).'</li>';
 6996:                     } else {
 6997:                         $chgtext .= &mt('none').'</li>';
 6998:                     }
 6999:                 }
 7000:                 $chgtext .= '</ul>';
 7001:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7002:                 $resulttext .= '</li>';
 7003:             }
 7004:             $resulttext .= '</ul>';
 7005:         } else {
 7006:             $resulttext = &mt('No changes made to user creation settings');
 7007:         }
 7008:     } else {
 7009:         $resulttext = '<span class="LC_error">'.
 7010:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7011:     }
 7012:     if ($warningmsg ne '') {
 7013:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7014:     }
 7015:     return $resulttext;
 7016: }
 7017: 
 7018: sub process_captcha {
 7019:     my ($container,$changes,$newsettings,$current) = @_;
 7020:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 7021:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 7022:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 7023:         $newsettings->{'captcha'} = 'original';
 7024:     }
 7025:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 7026:         if ($container eq 'cancreate') {
 7027:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7028:                 push(@{$changes->{'cancreate'}},'captcha');
 7029:             } elsif (!defined($changes->{'cancreate'})) {
 7030:                 $changes->{'cancreate'} = ['captcha'];
 7031:             }
 7032:         } else {
 7033:             $changes->{'captcha'} = 1;
 7034:         }
 7035:     }
 7036:     my ($newpub,$newpriv,$currpub,$currpriv);
 7037:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 7038:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 7039:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 7040:         $newpub =~ s/\W//g;
 7041:         $newpriv =~ s/\W//g;
 7042:         $newsettings->{'recaptchakeys'} = {
 7043:                                              public  => $newpub,
 7044:                                              private => $newpriv,
 7045:                                           };
 7046:     }
 7047:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 7048:         $currpub = $current->{'recaptchakeys'}{'public'};
 7049:         $currpriv = $current->{'recaptchakeys'}{'private'};
 7050:         $newsettings->{'recaptchakeys'} = {
 7051:                                              public  => '',
 7052:                                              private => '',
 7053:                                           }
 7054:     }
 7055:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 7056:         if ($container eq 'cancreate') {
 7057:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7058:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 7059:             } elsif (!defined($changes->{'cancreate'})) {
 7060:                 $changes->{'cancreate'} = ['recaptchakeys'];
 7061:             }
 7062:         } else {
 7063:             $changes->{'recaptchakeys'} = 1;
 7064:         }
 7065:     }
 7066:     return;
 7067: }
 7068: 
 7069: sub modify_usermodification {
 7070:     my ($dom,%domconfig) = @_;
 7071:     my ($resulttext,%curr_usermodification,%changes);
 7072:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7073:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7074:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 7075:         }
 7076:     }
 7077:     my @contexts = ('author','course','selfcreate');
 7078:     my %context_title = (
 7079:                            author => 'In author context',
 7080:                            course => 'In course context',
 7081:                            selfcreate => 'When self creating account', 
 7082:                         );
 7083:     my @fields = ('lastname','firstname','middlename','generation',
 7084:                   'permanentemail','id');
 7085:     my %roles = (
 7086:                   author => ['ca','aa'],
 7087:                   course => ['st','ep','ta','in','cr'],
 7088:                 );
 7089:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7090:     if (ref($types) eq 'ARRAY') {
 7091:         push(@{$types},'default');
 7092:         $usertypes->{'default'} = $othertitle;
 7093:     }
 7094:     $roles{'selfcreate'} = $types;  
 7095:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7096:     my %modifyhash;
 7097:     foreach my $context (@contexts) {
 7098:         foreach my $role (@{$roles{$context}}) {
 7099:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7100:             foreach my $item (@fields) {
 7101:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7102:                     $modifyhash{$context}{$role}{$item} = 1;
 7103:                 } else {
 7104:                     $modifyhash{$context}{$role}{$item} = 0;
 7105:                 }
 7106:             }
 7107:         }
 7108:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7109:             foreach my $role (@{$roles{$context}}) {
 7110:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7111:                     foreach my $field (@fields) {
 7112:                         if ($modifyhash{$context}{$role}{$field} ne 
 7113:                                 $curr_usermodification{$context}{$role}{$field}) {
 7114:                             push(@{$changes{$context}},$role);
 7115:                             last;
 7116:                         }
 7117:                     }
 7118:                 }
 7119:             }
 7120:         } else {
 7121:             foreach my $context (@contexts) {
 7122:                 foreach my $role (@{$roles{$context}}) {
 7123:                     push(@{$changes{$context}},$role);
 7124:                 }
 7125:             }
 7126:         }
 7127:     }
 7128:     my %usermodification_hash =  (
 7129:                                    usermodification => \%modifyhash,
 7130:                                  );
 7131:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7132:                                              \%usermodification_hash,$dom);
 7133:     if ($putresult eq 'ok') {
 7134:         if (keys(%changes) > 0) {
 7135:             $resulttext = &mt('Changes made: ').'<ul>';
 7136:             foreach my $context (@contexts) {
 7137:                 if (ref($changes{$context}) eq 'ARRAY') {
 7138:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7139:                     if (ref($changes{$context}) eq 'ARRAY') {
 7140:                         foreach my $role (@{$changes{$context}}) {
 7141:                             my $rolename;
 7142:                             if ($context eq 'selfcreate') {
 7143:                                 $rolename = $role;
 7144:                                 if (ref($usertypes) eq 'HASH') {
 7145:                                     if ($usertypes->{$role} ne '') {
 7146:                                         $rolename = $usertypes->{$role};
 7147:                                     }
 7148:                                 }
 7149:                             } else {
 7150:                                 if ($role eq 'cr') {
 7151:                                     $rolename = &mt('Custom');
 7152:                                 } else {
 7153:                                     $rolename = &Apache::lonnet::plaintext($role);
 7154:                                 }
 7155:                             }
 7156:                             my @modifiable;
 7157:                             if ($context eq 'selfcreate') {
 7158:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Self-creation of account by users with status: [_1]',$rolename).'</span> - '.&mt('modifiable fields (if institutional data blank): ');
 7159:                             } else {
 7160:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7161:                             }
 7162:                             foreach my $field (@fields) {
 7163:                                 if ($modifyhash{$context}{$role}{$field}) {
 7164:                                     push(@modifiable,$fieldtitles{$field});
 7165:                                 }
 7166:                             }
 7167:                             if (@modifiable > 0) {
 7168:                                 $resulttext .= join(', ',@modifiable);
 7169:                             } else {
 7170:                                 $resulttext .= &mt('none'); 
 7171:                             }
 7172:                             $resulttext .= '</li>';
 7173:                         }
 7174:                         $resulttext .= '</ul></li>';
 7175:                     }
 7176:                 }
 7177:             }
 7178:             $resulttext .= '</ul>';
 7179:         } else {
 7180:             $resulttext = &mt('No changes made to user modification settings');
 7181:         }
 7182:     } else {
 7183:         $resulttext = '<span class="LC_error">'.
 7184:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7185:     }
 7186:     return $resulttext;
 7187: }
 7188: 
 7189: sub modify_defaults {
 7190:     my ($dom,$r) = @_;
 7191:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7192:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7193:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7194:     my @authtypes = ('internal','krb4','krb5','localauth');
 7195:     foreach my $item (@items) {
 7196:         $newvalues{$item} = $env{'form.'.$item};
 7197:         if ($item eq 'auth_def') {
 7198:             if ($newvalues{$item} ne '') {
 7199:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7200:                     push(@errors,$item);
 7201:                 }
 7202:             }
 7203:         } elsif ($item eq 'lang_def') {
 7204:             if ($newvalues{$item} ne '') {
 7205:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7206:                     my $langcode = $1;
 7207:                     if ($langcode ne 'x_chef') {
 7208:                         if (code2language($langcode) eq '') {
 7209:                             push(@errors,$item);
 7210:                         }
 7211:                     }
 7212:                 } else {
 7213:                     push(@errors,$item);
 7214:                 }
 7215:             }
 7216:         } elsif ($item eq 'timezone_def') {
 7217:             if ($newvalues{$item} ne '') {
 7218:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7219:                     push(@errors,$item);   
 7220:                 }
 7221:             }
 7222:         } elsif ($item eq 'datelocale_def') {
 7223:             if ($newvalues{$item} ne '') {
 7224:                 my @datelocale_ids = DateTime::Locale->ids();
 7225:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7226:                     push(@errors,$item);
 7227:                 }
 7228:             }
 7229:         } elsif ($item eq 'portal_def') {
 7230:             if ($newvalues{$item} ne '') {
 7231:                 unless ($newvalues{$item} =~ /^https?\:\/\/(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])\/?$/) {
 7232:                     push(@errors,$item);
 7233:                 }
 7234:             }
 7235:         }
 7236:         if (grep(/^\Q$item\E$/,@errors)) {
 7237:             $newvalues{$item} = $domdefaults{$item};
 7238:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7239:             $changes{$item} = 1;
 7240:         }
 7241:         $domdefaults{$item} = $newvalues{$item};
 7242:     }
 7243:     my %defaults_hash = (
 7244:                          defaults => \%newvalues,
 7245:                         );
 7246:     my $title = &defaults_titles();
 7247:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7248:                                              $dom);
 7249:     if ($putresult eq 'ok') {
 7250:         if (keys(%changes) > 0) {
 7251:             $resulttext = &mt('Changes made:').'<ul>';
 7252:             my $version = $r->dir_config('lonVersion');
 7253:             my $mailmsgtext = "Changes made to domain settings in a LON-CAPA installation - domain: $dom (running version: $version) - dns_domain.tab needs to be updated with the following changes, to support legacy 2.4, 2.5 and 2.6 versions of LON-CAPA.\n\n";
 7254:             foreach my $item (sort(keys(%changes))) {
 7255:                 my $value = $env{'form.'.$item};
 7256:                 if ($value eq '') {
 7257:                     $value = &mt('none');
 7258:                 } elsif ($item eq 'auth_def') {
 7259:                     my %authnames = &authtype_names();
 7260:                     my %shortauth = (
 7261:                              internal => 'int',
 7262:                              krb4 => 'krb4',
 7263:                              krb5 => 'krb5',
 7264:                              localauth  => 'loc',
 7265:                     );
 7266:                     $value = $authnames{$shortauth{$value}};
 7267:                 }
 7268:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7269:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7270:             }
 7271:             $resulttext .= '</ul>';
 7272:             $mailmsgtext .= "\n";
 7273:             my $cachetime = 24*60*60;
 7274:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7275:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7276:                 my $sysmail = $r->dir_config('lonSysEMail');
 7277:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 7278:             }
 7279:         } else {
 7280:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 7281:         }
 7282:     } else {
 7283:         $resulttext = '<span class="LC_error">'.
 7284:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7285:     }
 7286:     if (@errors > 0) {
 7287:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7288:         foreach my $item (@errors) {
 7289:             $resulttext .= ' "'.$title->{$item}.'",';
 7290:         }
 7291:         $resulttext =~ s/,$//;
 7292:     }
 7293:     return $resulttext;
 7294: }
 7295: 
 7296: sub modify_scantron {
 7297:     my ($r,$dom,$confname,%domconfig) = @_;
 7298:     my ($resulttext,%confhash,%changes,$errors);
 7299:     my $custom = 'custom.tab';
 7300:     my $default = 'default.tab';
 7301:     my $servadm = $r->dir_config('lonAdmEMail');
 7302:     my ($configuserok,$author_ok,$switchserver) = 
 7303:         &config_check($dom,$confname,$servadm);
 7304:     if ($env{'form.scantronformat.filename'} ne '') {
 7305:         my $error;
 7306:         if ($configuserok eq 'ok') {
 7307:             if ($switchserver) {
 7308:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7309:             } else {
 7310:                 if ($author_ok eq 'ok') {
 7311:                     my ($result,$scantronurl) =
 7312:                         &publishlogo($r,'upload','scantronformat',$dom,
 7313:                                      $confname,'scantron','','',$custom);
 7314:                     if ($result eq 'ok') {
 7315:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7316:                         $changes{'scantronformat'} = 1;
 7317:                     } else {
 7318:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7319:                     }
 7320:                 } else {
 7321:                     $error = &mt("Upload of [_1] failed because an author role could not be assigned to a Domain Configuration user ([_2]) in domain: [_3].  Error was: [_4].",$custom,$confname,$dom,$author_ok);
 7322:                 }
 7323:             }
 7324:         } else {
 7325:             $error = &mt("Upload of [_1] failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$custom,$confname,$dom,$configuserok);
 7326:         }
 7327:         if ($error) {
 7328:             &Apache::lonnet::logthis($error);
 7329:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7330:         }
 7331:     }
 7332:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7333:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7334:             if ($env{'form.scantronformat_del'}) {
 7335:                 $confhash{'scantron'}{'scantronformat'} = '';
 7336:                 $changes{'scantronformat'} = 1;
 7337:             }
 7338:         }
 7339:     }
 7340:     if (keys(%confhash) > 0) {
 7341:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7342:                                                  $dom);
 7343:         if ($putresult eq 'ok') {
 7344:             if (keys(%changes) > 0) {
 7345:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7346:                     $resulttext = &mt('Changes made:').'<ul>';
 7347:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7348:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7349:                     } else {
 7350:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7351:                     }
 7352:                     $resulttext .= '</ul>';
 7353:                 } else {
 7354:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7355:                 }
 7356:                 $resulttext .= '</ul>';
 7357:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7358:             } else {
 7359:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7360:             }
 7361:         } else {
 7362:             $resulttext = '<span class="LC_error">'.
 7363:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7364:         }
 7365:     } else {
 7366:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7367:     }
 7368:     if ($errors) {
 7369:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7370:                        $errors.'</ul>';
 7371:     }
 7372:     return $resulttext;
 7373: }
 7374: 
 7375: sub modify_coursecategories {
 7376:     my ($dom,%domconfig) = @_;
 7377:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7378:         $cathash);
 7379:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7380:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7381:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7382:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7383:             $changes{'togglecats'} = 1;
 7384:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7385:         }
 7386:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7387:             $changes{'categorize'} = 1;
 7388:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7389:         }
 7390:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7391:             $changes{'togglecatscomm'} = 1;
 7392:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7393:         }
 7394:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7395:             $changes{'categorizecomm'} = 1;
 7396:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7397:         }
 7398:     } else {
 7399:         $changes{'togglecats'} = 1;
 7400:         $changes{'categorize'} = 1;
 7401:         $changes{'togglecatscomm'} = 1;
 7402:         $changes{'categorizecomm'} = 1;
 7403:         $domconfig{'coursecategories'} = {
 7404:                                              togglecats => $env{'form.togglecats'},
 7405:                                              categorize => $env{'form.categorize'},
 7406:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7407:                                              categorizecomm => $env{'form.categorizecomm'},
 7408:                                          };
 7409:     }
 7410:     if (ref($cathash) eq 'HASH') {
 7411:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7412:             push (@deletecategory,'instcode::0');
 7413:         }
 7414:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7415:             push(@deletecategory,'communities::0');
 7416:         }
 7417:     }
 7418:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7419:     if (ref($cathash) eq 'HASH') {
 7420:         if (@deletecategory > 0) {
 7421:             #FIXME Need to remove category from all courses using a deleted category 
 7422:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7423:             foreach my $item (@deletecategory) {
 7424:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7425:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7426:                     $deletions{$item} = 1;
 7427:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7428:                 }
 7429:             }
 7430:         }
 7431:         foreach my $item (keys(%{$cathash})) {
 7432:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7433:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7434:                 $reorderings{$item} = 1;
 7435:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7436:             }
 7437:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7438:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7439:                 my $newdepth = $depth+1;
 7440:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7441:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7442:                 $adds{$newitem} = 1; 
 7443:             }
 7444:             if ($env{'form.subcat_'.$item} ne '') {
 7445:                 my $newcat = $env{'form.subcat_'.$item};
 7446:                 my $newdepth = $depth+1;
 7447:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7448:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7449:                 $adds{$newitem} = 1;
 7450:             }
 7451:         }
 7452:     }
 7453:     if ($env{'form.instcode'} eq '1') {
 7454:         if (ref($cathash) eq 'HASH') {
 7455:             my $newitem = 'instcode::0';
 7456:             if ($cathash->{$newitem} eq '') {  
 7457:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7458:                 $adds{$newitem} = 1;
 7459:             }
 7460:         } else {
 7461:             my $newitem = 'instcode::0';
 7462:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7463:             $adds{$newitem} = 1;
 7464:         }
 7465:     }
 7466:     if ($env{'form.communities'} eq '1') {
 7467:         if (ref($cathash) eq 'HASH') {
 7468:             my $newitem = 'communities::0';
 7469:             if ($cathash->{$newitem} eq '') {
 7470:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7471:                 $adds{$newitem} = 1;
 7472:             }
 7473:         } else {
 7474:             my $newitem = 'communities::0';
 7475:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7476:             $adds{$newitem} = 1;
 7477:         }
 7478:     }
 7479:     if ($env{'form.addcategory_name'} ne '') {
 7480:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7481:             ($env{'form.addcategory_name'} ne 'communities')) {
 7482:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7483:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7484:             $adds{$newitem} = 1;
 7485:         }
 7486:     }
 7487:     my $putresult;
 7488:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7489:         if (keys(%deletions) > 0) {
 7490:             foreach my $key (keys(%deletions)) {
 7491:                 if ($predelallitems{$key} ne '') {
 7492:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7493:                 }
 7494:             }
 7495:         }
 7496:         my (@chkcats,@chktrails,%chkallitems);
 7497:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7498:         if (ref($chkcats[0]) eq 'ARRAY') {
 7499:             my $depth = 0;
 7500:             my $chg = 0;
 7501:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7502:                 my $name = $chkcats[0][$i];
 7503:                 my $item;
 7504:                 if ($name eq '') {
 7505:                     $chg ++;
 7506:                 } else {
 7507:                     $item = &escape($name).'::0';
 7508:                     if ($chg) {
 7509:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7510:                     }
 7511:                     $depth ++; 
 7512:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7513:                     $depth --;
 7514:                 }
 7515:             }
 7516:         }
 7517:     }
 7518:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7519:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7520:         if ($putresult eq 'ok') {
 7521:             my %title = (
 7522:                          togglecats     => 'Show/Hide a course in catalog',
 7523:                          categorize     => 'Assign a category to a course',
 7524:                          togglecatscomm => 'Show/Hide a community in catalog',
 7525:                          categorizecomm => 'Assign a category to a community',
 7526:                         );
 7527:             my %level = (
 7528:                          dom  => 'set in Domain ("Modify Course/Community")',
 7529:                          crs  => 'set in Course ("Course Configuration")',
 7530:                          comm => 'set in Community ("Community Configuration")',
 7531:                         );
 7532:             $resulttext = &mt('Changes made:').'<ul>';
 7533:             if ($changes{'togglecats'}) {
 7534:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7535:             }
 7536:             if ($changes{'categorize'}) {
 7537:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7538:             }
 7539:             if ($changes{'togglecatscomm'}) {
 7540:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7541:             }
 7542:             if ($changes{'categorizecomm'}) {
 7543:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7544:             }
 7545:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7546:                 my $cathash;
 7547:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7548:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7549:                 } else {
 7550:                     $cathash = {};
 7551:                 } 
 7552:                 my (@cats,@trails,%allitems);
 7553:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7554:                 if (keys(%deletions) > 0) {
 7555:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7556:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7557:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7558:                     }
 7559:                     $resulttext .= '</ul></li>';
 7560:                 }
 7561:                 if (keys(%reorderings) > 0) {
 7562:                     my %sort_by_trail;
 7563:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7564:                     foreach my $key (keys(%reorderings)) {
 7565:                         if ($allitems{$key} ne '') {
 7566:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7567:                         }
 7568:                     }
 7569:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7570:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7571:                     }
 7572:                     $resulttext .= '</ul></li>';
 7573:                 }
 7574:                 if (keys(%adds) > 0) {
 7575:                     my %sort_by_trail;
 7576:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7577:                     foreach my $key (keys(%adds)) {
 7578:                         if ($allitems{$key} ne '') {
 7579:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7580:                         }
 7581:                     }
 7582:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7583:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7584:                     }
 7585:                     $resulttext .= '</ul></li>';
 7586:                 }
 7587:             }
 7588:             $resulttext .= '</ul>';
 7589:         } else {
 7590:             $resulttext = '<span class="LC_error">'.
 7591:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7592:         }
 7593:     } else {
 7594:         $resulttext = &mt('No changes made to course and community categories');
 7595:     }
 7596:     return $resulttext;
 7597: }
 7598: 
 7599: sub modify_serverstatuses {
 7600:     my ($dom,%domconfig) = @_;
 7601:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7602:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7603:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7604:     }
 7605:     my @pages = &serverstatus_pages();
 7606:     foreach my $type (@pages) {
 7607:         $newserverstatus{$type}{'namedusers'} = '';
 7608:         $newserverstatus{$type}{'machines'} = '';
 7609:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7610:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7611:             my @okusers;
 7612:             foreach my $user (@users) {
 7613:                 my ($uname,$udom) = split(/:/,$user);
 7614:                 if (($udom =~ /^$match_domain$/) &&   
 7615:                     (&Apache::lonnet::domain($udom)) &&
 7616:                     ($uname =~ /^$match_username$/)) {
 7617:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7618:                         push(@okusers,$user);
 7619:                     }
 7620:                 }
 7621:             }
 7622:             if (@okusers > 0) {
 7623:                  @okusers = sort(@okusers);
 7624:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7625:             }
 7626:         }
 7627:         if (defined($env{'form.'.$type.'_machines'})) {
 7628:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7629:             my @okmachines;
 7630:             foreach my $ip (@machines) {
 7631:                 my @parts = split(/\./,$ip);
 7632:                 next if (@parts < 4);
 7633:                 my $badip = 0;
 7634:                 for (my $i=0; $i<4; $i++) {
 7635:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7636:                         $badip = 1;
 7637:                         last;
 7638:                     }
 7639:                 }
 7640:                 if (!$badip) {
 7641:                     push(@okmachines,$ip);     
 7642:                 }
 7643:             }
 7644:             @okmachines = sort(@okmachines);
 7645:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7646:         }
 7647:     }
 7648:     my %serverstatushash =  (
 7649:                                 serverstatuses => \%newserverstatus,
 7650:                             );
 7651:     foreach my $type (@pages) {
 7652:         foreach my $setting ('namedusers','machines') {
 7653:             my (@current,@new);
 7654:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7655:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7656:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7657:                 }
 7658:             }
 7659:             if ($newserverstatus{$type}{$setting} ne '') {
 7660:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7661:             }
 7662:             if (@current > 0) {
 7663:                 if (@new > 0) {
 7664:                     foreach my $item (@current) {
 7665:                         if (!grep(/^\Q$item\E$/,@new)) {
 7666:                             $changes{$type}{$setting} = 1;
 7667:                             last;
 7668:                         }
 7669:                     }
 7670:                     foreach my $item (@new) {
 7671:                         if (!grep(/^\Q$item\E$/,@current)) {
 7672:                             $changes{$type}{$setting} = 1;
 7673:                             last;
 7674:                         }
 7675:                     }
 7676:                 } else {
 7677:                     $changes{$type}{$setting} = 1;
 7678:                 }
 7679:             } elsif (@new > 0) {
 7680:                 $changes{$type}{$setting} = 1;
 7681:             }
 7682:         }
 7683:     }
 7684:     if (keys(%changes) > 0) {
 7685:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7686:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7687:                                                  \%serverstatushash,$dom);
 7688:         if ($putresult eq 'ok') {
 7689:             $resulttext .= &mt('Changes made:').'<ul>';
 7690:             foreach my $type (@pages) {
 7691:                 if (ref($changes{$type}) eq 'HASH') {
 7692:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7693:                     if ($changes{$type}{'namedusers'}) {
 7694:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7695:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7696:                         } else {
 7697:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7698:                         }
 7699:                     }
 7700:                     if ($changes{$type}{'machines'}) {
 7701:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7702:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7703:                         } else {
 7704:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7705:                         }
 7706: 
 7707:                     }
 7708:                     $resulttext .= '</ul></li>';
 7709:                 }
 7710:             }
 7711:             $resulttext .= '</ul>';
 7712:         } else {
 7713:             $resulttext = '<span class="LC_error">'.
 7714:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7715: 
 7716:         }
 7717:     } else {
 7718:         $resulttext = &mt('No changes made to access to server status pages');
 7719:     }
 7720:     return $resulttext;
 7721: }
 7722: 
 7723: sub modify_helpsettings {
 7724:     my ($r,$dom,$confname,%domconfig) = @_;
 7725:     my ($resulttext,$errors,%changes,%helphash);
 7726:     my %defaultchecked = ('submitbugs' => 'on');
 7727:     my @offon = ('off','on');
 7728:     my @toggles = ('submitbugs');
 7729:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7730:         foreach my $item (@toggles) {
 7731:             if ($defaultchecked{$item} eq 'on') { 
 7732:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7733:                     if ($env{'form.'.$item} eq '0') {
 7734:                         $changes{$item} = 1;
 7735:                     }
 7736:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7737:                     $changes{$item} = 1;
 7738:                 }
 7739:             } elsif ($defaultchecked{$item} eq 'off') {
 7740:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7741:                     if ($env{'form.'.$item} eq '1') {
 7742:                         $changes{$item} = 1;
 7743:                     }
 7744:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7745:                     $changes{$item} = 1;
 7746:                 }
 7747: 	    }
 7748:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 7749:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7750:             }
 7751:         }
 7752:     }
 7753:     my $putresult;
 7754:     if (keys(%changes) > 0) {
 7755:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7756:         if ($putresult eq 'ok') {
 7757:             $resulttext = &mt('Changes made:').'<ul>';
 7758:             foreach my $item (sort(keys(%changes))) {
 7759:                 if ($item eq 'submitbugs') {
 7760:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 7761:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 7762:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 7763:                 }
 7764:             }
 7765:             $resulttext .= '</ul>';
 7766:         } else {
 7767:             $resulttext = &mt('No changes made to help settings');
 7768:             $errors .= '<li><span class="LC_error">'.
 7769:                        &mt('An error occurred storing the settings: [_1]',
 7770:                            $putresult).'</span></li>';
 7771:         }
 7772:     }
 7773:     if ($errors) {
 7774:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7775:                        $errors.'</ul>';
 7776:     }
 7777:     return $resulttext;
 7778: }
 7779: 
 7780: sub modify_coursedefaults {
 7781:     my ($dom,%domconfig) = @_;
 7782:     my ($resulttext,$errors,%changes,%defaultshash);
 7783:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7784:     my @offon = ('off','on');
 7785:     my @toggles = ('canuse_pdfforms');
 7786: 
 7787:     $defaultshash{'coursedefaults'} = {};
 7788: 
 7789:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7790:         if ($domconfig{'coursedefaults'} eq '') {
 7791:             $domconfig{'coursedefaults'} = {};
 7792:         }
 7793:     }
 7794: 
 7795:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7796:         foreach my $item (@toggles) {
 7797:             if ($defaultchecked{$item} eq 'on') {
 7798:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7799:                     ($env{'form.'.$item} eq '0')) {
 7800:                     $changes{$item} = 1;
 7801:                 } elsif ($domconfig{'coursdefaults'}{$item} ne $env{'form.'.$item}) {
 7802:                     $changes{$item} = 1;
 7803:                 }
 7804:             } elsif ($defaultchecked{$item} eq 'off') {
 7805:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7806:                     ($env{'form.'.$item} eq '1')) {
 7807:                     $changes{$item} = 1;
 7808:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7809:                     $changes{$item} = 1;
 7810:                 }
 7811:             }
 7812:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7813:         }
 7814:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7815:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7816:         $newdefresponder =~ s/\D//g;
 7817:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7818:             $newdefresponder = 1;
 7819:         }
 7820:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7821:         if ($currdefresponder ne $newdefresponder) {
 7822:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7823:                 $changes{'anonsurvey_threshold'} = 1;
 7824:             }
 7825:         }
 7826:     }
 7827:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7828:                                              $dom);
 7829:     if ($putresult eq 'ok') {
 7830:         if (keys(%changes) > 0) {
 7831:             if ($changes{'canuse_pdfforms'}) {
 7832:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7833:                 $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7834:                 my $cachetime = 24*60*60;
 7835:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7836:             }
 7837:             $resulttext = &mt('Changes made:').'<ul>';
 7838:             foreach my $item (sort(keys(%changes))) {
 7839:                 if ($item eq 'canuse_pdfforms') {
 7840:                     if ($env{'form.'.$item} eq '1') {
 7841:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7842:                     } else {
 7843:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7844:                     }
 7845:                 } elsif ($item eq 'anonsurvey_threshold') {
 7846:                         $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7847:                 }
 7848:             }
 7849:             $resulttext .= '</ul>';
 7850:         } else {
 7851:             $resulttext = &mt('No changes made to course defaults');
 7852:         }
 7853:     } else {
 7854:         $resulttext = '<span class="LC_error">'.
 7855:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7856:     }
 7857:     return $resulttext;
 7858: }
 7859: 
 7860: sub modify_usersessions {
 7861:     my ($dom,%domconfig) = @_;
 7862:     my @hostingtypes = ('version','excludedomain','includedomain');
 7863:     my @offloadtypes = ('primary','default');
 7864:     my %types = (
 7865:                   remote => \@hostingtypes,
 7866:                   hosted => \@hostingtypes,
 7867:                   spares => \@offloadtypes,
 7868:                 );
 7869:     my @prefixes = ('remote','hosted','spares');
 7870:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7871:     my (%by_ip,%by_location,@intdoms);
 7872:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7873:     my @locations = sort(keys(%by_location));
 7874:     my (%defaultshash,%changes);
 7875:     foreach my $prefix (@prefixes) {
 7876:         $defaultshash{'usersessions'}{$prefix} = {};
 7877:     }
 7878:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7879:     my $resulttext;
 7880:     my %iphost = &Apache::lonnet::get_iphost();
 7881:     foreach my $prefix (@prefixes) {
 7882:         next if ($prefix eq 'spares');
 7883:         foreach my $type (@{$types{$prefix}}) {
 7884:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7885:             if ($type eq 'version') {
 7886:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7887:                 my $okvalue;
 7888:                 if ($value ne '') {
 7889:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 7890:                         $okvalue = $value;
 7891:                     }
 7892:                 }
 7893:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7894:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7895:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 7896:                             if ($inuse == 0) {
 7897:                                 $changes{$prefix}{$type} = 1;
 7898:                             } else {
 7899:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 7900:                                     $changes{$prefix}{$type} = 1;
 7901:                                 }
 7902:                                 if ($okvalue ne '') {
 7903:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7904:                                 } 
 7905:                             }
 7906:                         } else {
 7907:                             if (($inuse == 1) && ($okvalue ne '')) {
 7908:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7909:                                 $changes{$prefix}{$type} = 1;
 7910:                             }
 7911:                         }
 7912:                     } else {
 7913:                         if (($inuse == 1) && ($okvalue ne '')) {
 7914:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7915:                             $changes{$prefix}{$type} = 1;
 7916:                         }
 7917:                     }
 7918:                 } else {
 7919:                     if (($inuse == 1) && ($okvalue ne '')) {
 7920:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7921:                         $changes{$prefix}{$type} = 1;
 7922:                     }
 7923:                 }
 7924:             } else {
 7925:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 7926:                 my @okvals;
 7927:                 foreach my $val (@vals) {
 7928:                     if ($val =~ /:/) {
 7929:                         my @items = split(/:/,$val);
 7930:                         foreach my $item (@items) {
 7931:                             if (ref($by_location{$item}) eq 'ARRAY') {
 7932:                                 push(@okvals,$item);
 7933:                             }
 7934:                         }
 7935:                     } else {
 7936:                         if (ref($by_location{$val}) eq 'ARRAY') {
 7937:                             push(@okvals,$val);
 7938:                         }
 7939:                     }
 7940:                 }
 7941:                 @okvals = sort(@okvals);
 7942:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7943:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7944:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7945:                             if ($inuse == 0) {
 7946:                                 $changes{$prefix}{$type} = 1; 
 7947:                             } else {
 7948:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7949:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 7950:                                 if (@changed > 0) {
 7951:                                     $changes{$prefix}{$type} = 1;
 7952:                                 }
 7953:                             }
 7954:                         } else {
 7955:                             if ($inuse == 1) {
 7956:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7957:                                 $changes{$prefix}{$type} = 1;
 7958:                             }
 7959:                         } 
 7960:                     } else {
 7961:                         if ($inuse == 1) {
 7962:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7963:                             $changes{$prefix}{$type} = 1;
 7964:                         }
 7965:                     }
 7966:                 } else {
 7967:                     if ($inuse == 1) {
 7968:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7969:                         $changes{$prefix}{$type} = 1;
 7970:                     }
 7971:                 }
 7972:             }
 7973:         }
 7974:     }
 7975: 
 7976:     my @alldoms = &Apache::lonnet::all_domains();
 7977:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7978:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 7979:     my $savespares;
 7980: 
 7981:     foreach my $lonhost (sort(keys(%servers))) {
 7982:         my $serverhomeID =
 7983:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 7984:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 7985:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 7986:         my %spareschg;
 7987:         foreach my $type (@{$types{'spares'}}) {
 7988:             my @okspares;
 7989:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 7990:             foreach my $server (@checked) {
 7991:                 if (&Apache::lonnet::hostname($server) ne '') {
 7992:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 7993:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 7994:                             push(@okspares,$server);
 7995:                         }
 7996:                     }
 7997:                 }
 7998:             }
 7999:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 8000:             my $newspare;
 8001:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 8002:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 8003:                     $newspare = $new;
 8004:                 }
 8005:             }
 8006:             my @spares;
 8007:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 8008:                 @spares = sort(@okspares,$newspare);
 8009:             } else {
 8010:                 @spares = sort(@okspares);
 8011:             }
 8012:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 8013:             if (ref($spareid{$lonhost}) eq 'HASH') {
 8014:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 8015:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 8016:                     if (@diffs > 0) {
 8017:                         $spareschg{$type} = 1;
 8018:                     }
 8019:                 }
 8020:             }
 8021:         }
 8022:         if (keys(%spareschg) > 0) {
 8023:             $changes{'spares'}{$lonhost} = \%spareschg;
 8024:         }
 8025:     }
 8026: 
 8027:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8028:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 8029:             if (ref($changes{'spares'}) eq 'HASH') {
 8030:                 if (keys(%{$changes{'spares'}}) > 0) {
 8031:                     $savespares = 1;
 8032:                 }
 8033:             }
 8034:         } else {
 8035:             $savespares = 1;
 8036:         }
 8037:     }
 8038: 
 8039:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 8040:     if ((keys(%changes) > 0) || ($savespares)) {
 8041:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8042:                                                  $dom);
 8043:         if ($putresult eq 'ok') {
 8044:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8045:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 8046:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 8047:                 }
 8048:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 8049:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 8050:                 }
 8051:             }
 8052:             my $cachetime = 24*60*60;
 8053:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8054:             if (keys(%changes) > 0) {
 8055:                 my %lt = &usersession_titles();
 8056:                 $resulttext = &mt('Changes made:').'<ul>';
 8057:                 foreach my $prefix (@prefixes) {
 8058:                     if (ref($changes{$prefix}) eq 'HASH') {
 8059:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 8060:                         if ($prefix eq 'spares') {
 8061:                             if (ref($changes{$prefix}) eq 'HASH') {
 8062:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 8063:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 8064:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 8065:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 8066:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 8067:                                         foreach my $type (@{$types{$prefix}}) {
 8068:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 8069:                                                 my $offloadto = &mt('None');
 8070:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 8071:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 8072:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 8073:                                                     }
 8074:                                                 }
 8075:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 8076:                                             }
 8077:                                         }
 8078:                                     }
 8079:                                     $resulttext .= '</li>';
 8080:                                 }
 8081:                             }
 8082:                         } else {
 8083:                             foreach my $type (@{$types{$prefix}}) {
 8084:                                 if (defined($changes{$prefix}{$type})) {
 8085:                                     my $newvalue;
 8086:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8087:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 8088:                                             if ($type eq 'version') {
 8089:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 8090:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8091:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 8092:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 8093:                                                 }
 8094:                                             }
 8095:                                         }
 8096:                                     }
 8097:                                     if ($newvalue eq '') {
 8098:                                         if ($type eq 'version') {
 8099:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8100:                                         } else {
 8101:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8102:                                         }
 8103:                                     } else {
 8104:                                         if ($type eq 'version') {
 8105:                                             $newvalue .= ' '.&mt('(or later)'); 
 8106:                                         }
 8107:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8108:                                     }
 8109:                                 }
 8110:                             }
 8111:                         }
 8112:                         $resulttext .= '</ul>';
 8113:                     }
 8114:                 }
 8115:                 $resulttext .= '</ul>';
 8116:             } else {
 8117:                 $resulttext = $nochgmsg;
 8118:             }
 8119:         } else {
 8120:             $resulttext = '<span class="LC_error">'.
 8121:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8122:         }
 8123:     } else {
 8124:         $resulttext = $nochgmsg;
 8125:     }
 8126:     return $resulttext;
 8127: }
 8128: 
 8129: sub modify_loadbalancing {
 8130:     my ($dom,%domconfig) = @_;
 8131:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8132:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8133:     my ($othertitle,$usertypes,$types) =
 8134:         &Apache::loncommon::sorted_inst_types($dom);
 8135:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8136:     my @sparestypes = ('primary','default');
 8137:     my %typetitles = &sparestype_titles();
 8138:     my $resulttext;
 8139:     my (%currbalancer,%currtargets,%currrules,%existing);
 8140:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8141:         %existing = %{$domconfig{'loadbalancing'}};
 8142:     }
 8143:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8144:                               \%currtargets,\%currrules);
 8145:     my ($saveloadbalancing,%defaultshash,%changes);
 8146:     my ($alltypes,$othertypes,$titles) =
 8147:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8148:     my %ruletitles = &offloadtype_text();
 8149:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 8150:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 8151:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 8152:         if ($balancer eq '') {
 8153:             next;
 8154:         }
 8155:         if (!exists($servers{$balancer})) {
 8156:             if (exists($currbalancer{$balancer})) {
 8157:                 push(@{$changes{'delete'}},$balancer);
 8158:             }
 8159:             next;
 8160:         }
 8161:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 8162:             push(@{$changes{'delete'}},$balancer);
 8163:             next;
 8164:         }
 8165:         if (!exists($currbalancer{$balancer})) {
 8166:             push(@{$changes{'add'}},$balancer);
 8167:         }
 8168:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 8169:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 8170:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 8171:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8172:             $saveloadbalancing = 1;
 8173:         }
 8174:         foreach my $sparetype (@sparestypes) {
 8175:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 8176:             my @offloadto;
 8177:             foreach my $target (@targets) {
 8178:                 if (($servers{$target}) && ($target ne $balancer)) {
 8179:                     if ($sparetype eq 'default') {
 8180:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 8181:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 8182:                         }
 8183:                     }
 8184:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 8185:                         push(@offloadto,$target);
 8186:                     }
 8187:                 }
 8188:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 8189:             }
 8190:         }
 8191:         if (ref($currtargets{$balancer}) eq 'HASH') {
 8192:             foreach my $sparetype (@sparestypes) {
 8193:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 8194:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 8195:                     if (@targetdiffs > 0) {
 8196:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8197:                     }
 8198:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8199:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8200:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8201:                     }
 8202:                 }
 8203:             }
 8204:         } else {
 8205:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 8206:                 foreach my $sparetype (@sparestypes) {
 8207:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8208:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8209:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 8210:                         }
 8211:                     }
 8212:                 }
 8213:             }
 8214:         }
 8215:         my $ishomedom;
 8216:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 8217:             $ishomedom = 1;
 8218:         }
 8219:         if (ref($alltypes) eq 'ARRAY') {
 8220:             foreach my $type (@{$alltypes}) {
 8221:                 my $rule;
 8222:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
 8223:                          (!$ishomedom)) {
 8224:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 8225:                 }
 8226:                 if ($rule eq 'specific') {
 8227:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 8228:                 }
 8229:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 8230:                 if (ref($currrules{$balancer}) eq 'HASH') {
 8231:                     if ($rule ne $currrules{$balancer}{$type}) {
 8232:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8233:                     }
 8234:                 } elsif ($rule ne '') {
 8235:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8236:                 }
 8237:             }
 8238:         }
 8239:     }
 8240:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8241:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8242:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 8243:             $defaultshash{'loadbalancing'} = {};
 8244:         }
 8245:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8246:                                                  \%defaultshash,$dom);
 8247: 
 8248:         if ($putresult eq 'ok') {
 8249:             if (keys(%changes) > 0) {
 8250:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 8251:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 8252:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 8253:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8254:                     }
 8255:                 }
 8256:                 if (ref($changes{'add'}) eq 'ARRAY') {
 8257:                     foreach my $balancer (sort(@{$changes{'add'}})) {
 8258:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 8259:                     }
 8260:                 }
 8261:                 if (ref($changes{'curr'}) eq 'HASH') {
 8262:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 8263:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 8264:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 8265:                                 my %offloadstr;
 8266:                                 foreach my $sparetype (@sparestypes) {
 8267:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8268:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8269:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 8270:                                         }
 8271:                                     }
 8272:                                 }
 8273:                                 if (keys(%offloadstr) == 0) {
 8274:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8275:                                 } else {
 8276:                                     my $showoffload;
 8277:                                     foreach my $sparetype (@sparestypes) {
 8278:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8279:                                         if (defined($offloadstr{$sparetype})) {
 8280:                                             $showoffload .= $offloadstr{$sparetype};
 8281:                                         } else {
 8282:                                             $showoffload .= &mt('None');
 8283:                                         }
 8284:                                         $showoffload .= ('&nbsp;'x3);
 8285:                                     }
 8286:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 8287:                                 }
 8288:                             }
 8289:                         }
 8290:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 8291:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8292:                                 foreach my $type (@{$alltypes}) {
 8293:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 8294:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 8295:                                         my $balancetext;
 8296:                                         if ($rule eq '') {
 8297:                                             $balancetext =  $ruletitles{'default'};
 8298:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 8299:                                             $balancetext =  $ruletitles{$rule};
 8300:                                         } else {
 8301:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 8302:                                         }
 8303:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>'; 
 8304:                                     }
 8305:                                 }
 8306:                             }
 8307:                         }
 8308:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8309:                     }
 8310:                 }
 8311:                 if ($resulttext ne '') {
 8312:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8313:                 } else {
 8314:                     $resulttext = $nochgmsg;
 8315:                 }
 8316:             } else {
 8317:                 $resulttext = $nochgmsg;
 8318:             }
 8319:         } else {
 8320:             $resulttext = '<span class="LC_error">'.
 8321:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8322:         }
 8323:     } else {
 8324:         $resulttext = $nochgmsg;
 8325:     }
 8326:     return $resulttext;
 8327: }
 8328: 
 8329: sub recurse_check {
 8330:     my ($chkcats,$categories,$depth,$name) = @_;
 8331:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8332:         my $chg = 0;
 8333:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8334:             my $category = $chkcats->[$depth]{$name}[$j];
 8335:             my $item;
 8336:             if ($category eq '') {
 8337:                 $chg ++;
 8338:             } else {
 8339:                 my $deeper = $depth + 1;
 8340:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8341:                 if ($chg) {
 8342:                     $categories->{$item} -= $chg;
 8343:                 }
 8344:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8345:                 $deeper --;
 8346:             }
 8347:         }
 8348:     }
 8349:     return;
 8350: }
 8351: 
 8352: sub recurse_cat_deletes {
 8353:     my ($item,$coursecategories,$deletions) = @_;
 8354:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8355:     my $subdepth = $depth + 1;
 8356:     if (ref($coursecategories) eq 'HASH') {
 8357:         foreach my $subitem (keys(%{$coursecategories})) {
 8358:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8359:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8360:                 delete($coursecategories->{$subitem});
 8361:                 $deletions->{$subitem} = 1;
 8362:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8363:             }  
 8364:         }
 8365:     }
 8366:     return;
 8367: }
 8368: 
 8369: sub get_active_dcs {
 8370:     my ($dom) = @_;
 8371:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc']);
 8372:     my %domcoords;
 8373:     my $numdcs = 0;
 8374:     my $now = time;
 8375:     foreach my $server (keys(%dompersonnel)) {
 8376:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8377:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8378:             my ($end,$start) = split(':',$dompersonnel{$server}{$user});
 8379:             if (($end eq '') || ($end == 0) || ($end > $now)) {
 8380:                 if ($start <= $now) {
 8381:                     $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8382:                 }
 8383:             }
 8384:         }
 8385:     }
 8386:     return %domcoords;
 8387: }
 8388: 
 8389: sub active_dc_picker {
 8390:     my ($dom,$curr_dc) = @_;
 8391:     my %domcoords = &get_active_dcs($dom); 
 8392:     my @dcs = sort(keys(%domcoords));
 8393:     my $numdcs = scalar(@dcs); 
 8394:     my $datatable;
 8395:     my $numinrow = 2;
 8396:     if ($numdcs > 1) {
 8397:         $datatable = '<table>';
 8398:         for (my $i=0; $i<@dcs; $i++) {
 8399:             my $rem = $i%($numinrow);
 8400:             if ($rem == 0) {
 8401:                 if ($i > 0) {
 8402:                     $datatable .= '</tr>';
 8403:                 }
 8404:                 $datatable .= '<tr>';
 8405:             }
 8406:             my $check = ' ';
 8407:             if ($curr_dc eq '') {
 8408:                 if (!$i) { 
 8409:                     $check = ' checked="checked" ';
 8410:                 }
 8411:             } elsif ($dcs[$i] eq $curr_dc) {
 8412:                 $check = ' checked="checked" ';
 8413:             }
 8414:             if ($i == @dcs - 1) {
 8415:                 my $colsleft = $numinrow - $rem;
 8416:                 if ($colsleft > 1) {
 8417:                     $datatable .= '<td colspan="'.$colsleft.'">';
 8418:                 } else {
 8419:                     $datatable .= '<td>';
 8420:                 }
 8421:             } else {
 8422:                 $datatable .= '<td>';
 8423:             }
 8424:             my ($dcname,$dcdom) = split(':',$dcs[$i]);
 8425:             $datatable .= '<span class="LC_nobreak"><label>'.
 8426:                           '<input type="radio" name="autocreate_xmldc"'.
 8427:                           ' value="'.$dcs[$i].'"'.$check.'/>'.
 8428:                           &Apache::loncommon::plainname($dcname,$dcdom).
 8429:                           '</label></span></td>';
 8430:         }
 8431:         $datatable .= '</tr></table>';
 8432:     } elsif (@dcs) {
 8433:         $datatable .= '<input type="hidden" name="autocreate_dc" value="'.
 8434:                       $dcs[0].'" />';
 8435:     }
 8436:     return ($numdcs,$datatable);
 8437: }
 8438: 
 8439: sub usersession_titles {
 8440:     return &Apache::lonlocal::texthash(
 8441:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8442:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8443:                spares => 'Servers offloaded to, when busy',
 8444:                version => 'LON-CAPA version requirement',
 8445:                excludedomain => 'Allow all, but exclude specific domains',
 8446:                includedomain => 'Deny all, but include specific domains',
 8447:                primary => 'Primary (checked first)',
 8448:                default => 'Default',
 8449:            );
 8450: }
 8451: 
 8452: sub id_for_thisdom {
 8453:     my (%servers) = @_;
 8454:     my %altids;
 8455:     foreach my $server (keys(%servers)) {
 8456:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8457:         if ($serverhome ne $server) {
 8458:             $altids{$serverhome} = $server;
 8459:         }
 8460:     }
 8461:     return %altids;
 8462: }
 8463: 
 8464: sub count_servers {
 8465:     my ($currbalancer,%servers) = @_;
 8466:     my (@spares,$numspares);
 8467:     foreach my $lonhost (sort(keys(%servers))) {
 8468:         next if ($currbalancer eq $lonhost);
 8469:         push(@spares,$lonhost);
 8470:     }
 8471:     if ($currbalancer) {
 8472:         $numspares = scalar(@spares);
 8473:     } else {
 8474:         $numspares = scalar(@spares) - 1;
 8475:     }
 8476:     return ($numspares,@spares);
 8477: }
 8478: 
 8479: sub lonbalance_targets_js {
 8480:     my ($dom,$types,$servers,$settings) = @_;
 8481:     my $select = &mt('Select');
 8482:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8483:     if (ref($servers) eq 'HASH') {
 8484:         $alltargets = join("','",sort(keys(%{$servers})));
 8485:         my @homedoms;
 8486:         foreach my $server (sort(keys(%{$servers}))) {
 8487:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8488:                 push(@homedoms,'1');
 8489:             } else {
 8490:                 push(@homedoms,'0');
 8491:             }
 8492:         }
 8493:         $allishome = join("','",@homedoms);
 8494:     }
 8495:     if (ref($types) eq 'ARRAY') {
 8496:         if (@{$types} > 0) {
 8497:             @alltypes = @{$types};
 8498:         }
 8499:     }
 8500:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8501:     $allinsttypes = join("','",@alltypes);
 8502:     my (%currbalancer,%currtargets,%currrules,%existing);
 8503:     if (ref($settings) eq 'HASH') {
 8504:         %existing = %{$settings};
 8505:     }
 8506:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 8507:                               \%currtargets,\%currrules);
 8508:     my $balancers = join("','",sort(keys(%currbalancer)));
 8509:     return <<"END";
 8510: 
 8511: <script type="text/javascript">
 8512: // <![CDATA[
 8513: 
 8514: currBalancers = new Array('$balancers');
 8515: 
 8516: function toggleTargets(balnum) {
 8517:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8518:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 8519:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 8520:     var prevbalancer = prevhostitem.value;
 8521:     var baltotal = document.getElementById('loadbalancing_total').value;
 8522:     prevhostitem.value = balancer;
 8523:     if (prevbalancer != '') {
 8524:         var prevIdx = currBalancers.indexOf(prevbalancer);
 8525:         if (prevIdx != -1) {
 8526:             currBalancers.splice(prevIdx,1);
 8527:         }
 8528:     }
 8529:     if (balancer == '') {
 8530:         hideSpares(balnum);
 8531:     } else {
 8532:         var currIdx = currBalancers.indexOf(balancer);
 8533:         if (currIdx == -1) {
 8534:             currBalancers.push(balancer);
 8535:         }
 8536:         var homedoms = new Array('$allishome');
 8537:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 8538:         showSpares(balancer,ishomedom,balnum);
 8539:     }
 8540:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 8541:     return;
 8542: }
 8543: 
 8544: function showSpares(balancer,ishomedom,balnum) {
 8545:     var alltargets = new Array('$alltargets');
 8546:     var insttypes = new Array('$allinsttypes');
 8547:     var offloadtypes = new Array('primary','default');
 8548: 
 8549:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 8550:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 8551:  
 8552:     for (var i=0; i<offloadtypes.length; i++) {
 8553:         var count = 0;
 8554:         for (var j=0; j<alltargets.length; j++) {
 8555:             if (alltargets[j] != balancer) {
 8556:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 8557:                 item.value = alltargets[j];
 8558:                 item.style.textAlign='left';
 8559:                 item.style.textFace='normal';
 8560:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8561:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 8562:                     item.disabled = '';
 8563:                 } else {
 8564:                     item.disabled = 'disabled';
 8565:                     item.checked = false;
 8566:                 }
 8567:                 count ++;
 8568:             }
 8569:         }
 8570:     }
 8571:     for (var k=0; k<insttypes.length; k++) {
 8572:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8573:             if (ishomedom == 1) {
 8574:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8575:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8576:             } else {
 8577:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8578:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8579: 
 8580:             }
 8581:         } else {
 8582:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8583:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8584:         }
 8585:         if ((insttypes[k] != '_LC_external') && 
 8586:             ((insttypes[k] != '_LC_internetdom') ||
 8587:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8588:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 8589:             item.options.length = 0;
 8590:             item.options[0] = new Option("","",true,true);
 8591:             var idx = 0;
 8592:             for (var m=0; m<alltargets.length; m++) {
 8593:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 8594:                     idx ++;
 8595:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8596: 
 8597:                 }
 8598:             }
 8599:         }
 8600:     }
 8601:     return;
 8602: }
 8603: 
 8604: function hideSpares(balnum) {
 8605:     var alltargets = new Array('$alltargets');
 8606:     var insttypes = new Array('$allinsttypes');
 8607:     var offloadtypes = new Array('primary','default');
 8608: 
 8609:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 8610:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 8611: 
 8612:     var total = alltargets.length - 1;
 8613:     for (var i=0; i<offloadtypes; i++) {
 8614:         for (var j=0; j<total; j++) {
 8615:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 8616:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 8617:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8618:         }
 8619:     }
 8620:     for (var k=0; k<insttypes.length; k++) {
 8621:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8622:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8623:         if (insttypes[k] != '_LC_external') {
 8624:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 8625:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 8626:         }
 8627:     }
 8628:     return;
 8629: }
 8630: 
 8631: function checkOffloads(item,balnum,type) {
 8632:     var alltargets = new Array('$alltargets');
 8633:     var offloadtypes = new Array('primary','default');
 8634:     if (item.checked) {
 8635:         var total = alltargets.length - 1;
 8636:         var other;
 8637:         if (type == offloadtypes[0]) {
 8638:             other = offloadtypes[1];
 8639:         } else {
 8640:             other = offloadtypes[0];
 8641:         }
 8642:         for (var i=0; i<total; i++) {
 8643:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 8644:             if (server == item.value) {
 8645:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 8646:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 8647:                 }
 8648:             }
 8649:         }
 8650:     }
 8651:     return;
 8652: }
 8653: 
 8654: function singleServerToggle(balnum,type) {
 8655:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 8656:     if (offloadtoSelIdx == 0) {
 8657:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 8658:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8659: 
 8660:     } else {
 8661:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 8662:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8663:     }
 8664:     return;
 8665: }
 8666: 
 8667: function balanceruleChange(formname,balnum,type) {
 8668:     if (type == '_LC_external') {
 8669:         return; 
 8670:     }
 8671:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 8672:     for (var i=0; i<typesRules.length; i++) {
 8673:         if (formname.elements[typesRules[i]].checked) {
 8674:             if (formname.elements[typesRules[i]].value != 'specific') {
 8675:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 8676:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8677:             } else {
 8678:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8679:             }
 8680:         }
 8681:     }
 8682:     return;
 8683: }
 8684: 
 8685: function balancerDeleteChange(balnum) {
 8686:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8687:     var baltotal = document.getElementById('loadbalancing_total').value;
 8688:     var addtarget;
 8689:     var removetarget;
 8690:     var action = 'delete';
 8691:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 8692:         var lonhost = hostitem.value;
 8693:         var currIdx = currBalancers.indexOf(lonhost);
 8694:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 8695:             if (currIdx != -1) {
 8696:                 currBalancers.splice(currIdx,1);
 8697:             }
 8698:             addtarget = lonhost;
 8699:         } else {
 8700:             if (currIdx == -1) {
 8701:                 currBalancers.push(lonhost);
 8702:             }
 8703:             removetarget = lonhost;
 8704:             action = 'undelete';
 8705:         }
 8706:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 8707:     }
 8708:     return;
 8709: }
 8710: 
 8711: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 8712:     if (baltotal > 1) {
 8713:         var offloadtypes = new Array('primary','default');
 8714:         var alltargets = new Array('$alltargets');
 8715:         var insttypes = new Array('$allinsttypes');
 8716:         for (var i=0; i<baltotal; i++) {
 8717:             if (i != balnum) {
 8718:                 for (var j=0; j<offloadtypes.length; j++) {
 8719:                     var total = alltargets.length - 1;
 8720:                     for (var k=0; k<total; k++) {
 8721:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 8722:                         var server = serveritem.value;
 8723:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 8724:                             if (server == addtarget) {
 8725:                                 serveritem.disabled = '';
 8726:                             }
 8727:                         }
 8728:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8729:                             if (server == removetarget) {
 8730:                                 serveritem.disabled = 'disabled';
 8731:                                 serveritem.checked = false;
 8732:                             }
 8733:                         }
 8734:                     }
 8735:                 }
 8736:                 for (var j=0; j<insttypes.length; j++) {
 8737:                     if (insttypes[j] != '_LC_external') {
 8738:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 8739:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 8740:                             var currSel = singleserver.selectedIndex;
 8741:                             var currVal = singleserver.options[currSel].value;
 8742:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 8743:                                 var numoptions = singleserver.options.length;
 8744:                                 var needsnew = 1;
 8745:                                 for (var k=0; k<numoptions; k++) {
 8746:                                     if (singleserver.options[k] == addtarget) {
 8747:                                         needsnew = 0;
 8748:                                         break;
 8749:                                     }
 8750:                                 }
 8751:                                 if (needsnew == 1) {
 8752:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 8753:                                 }
 8754:                             }
 8755:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8756:                                 singleserver.options.length = 0;
 8757:                                 if ((currVal) && (currVal != removetarget)) {
 8758:                                     singleserver.options[0] = new Option("","",false,false);
 8759:                                 } else {
 8760:                                     singleserver.options[0] = new Option("","",true,true);
 8761:                                 }
 8762:                                 var idx = 0;
 8763:                                 for (var m=0; m<alltargets.length; m++) {
 8764:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
 8765:                                         idx ++;
 8766:                                         if (currVal == alltargets[m]) {
 8767:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
 8768:                                         } else {
 8769:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8770:                                         }
 8771:                                     }
 8772:                                 }
 8773:                             }
 8774:                         }
 8775:                     }
 8776:                 }
 8777:             }
 8778:         }
 8779:     }
 8780:     return;
 8781: }
 8782: 
 8783: // ]]>
 8784: </script>
 8785: 
 8786: END
 8787: }
 8788: 
 8789: sub new_spares_js {
 8790:     my @sparestypes = ('primary','default');
 8791:     my $types = join("','",@sparestypes);
 8792:     my $select = &mt('Select');
 8793:     return <<"END";
 8794: 
 8795: <script type="text/javascript">
 8796: // <![CDATA[
 8797: 
 8798: function updateNewSpares(formname,lonhost) {
 8799:     var types = new Array('$types');
 8800:     var include = new Array();
 8801:     var exclude = new Array();
 8802:     for (var i=0; i<types.length; i++) {
 8803:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8804:         for (var j=0; j<spareboxes.length; j++) {
 8805:             if (formname.elements[spareboxes[j]].checked) {
 8806:                 exclude.push(formname.elements[spareboxes[j]].value);
 8807:             } else {
 8808:                 include.push(formname.elements[spareboxes[j]].value);
 8809:             }
 8810:         }
 8811:     }
 8812:     for (var i=0; i<types.length; i++) {
 8813:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8814:         var selIdx = newSpare.selectedIndex;
 8815:         var currnew = newSpare.options[selIdx].value;
 8816:         var okSpares = new Array();
 8817:         for (var j=0; j<newSpare.options.length; j++) {
 8818:             var possible = newSpare.options[j].value;
 8819:             if (possible != '') {
 8820:                 if (exclude.indexOf(possible) == -1) {
 8821:                     okSpares.push(possible);
 8822:                 } else {
 8823:                     if (currnew == possible) {
 8824:                         selIdx = 0;
 8825:                     }
 8826:                 }
 8827:             }
 8828:         }
 8829:         for (var k=0; k<include.length; k++) {
 8830:             if (okSpares.indexOf(include[k]) == -1) {
 8831:                 okSpares.push(include[k]);
 8832:             }
 8833:         }
 8834:         okSpares.sort();
 8835:         newSpare.options.length = 0;
 8836:         if (selIdx == 0) {
 8837:             newSpare.options[0] = new Option("$select","",true,true);
 8838:         } else {
 8839:             newSpare.options[0] = new Option("$select","",false,false);
 8840:         }
 8841:         for (var m=0; m<okSpares.length; m++) {
 8842:             var idx = m+1;
 8843:             var selThis = 0;
 8844:             if (selIdx != 0) {
 8845:                 if (okSpares[m] == currnew) {
 8846:                     selThis = 1;
 8847:                 }
 8848:             }
 8849:             if (selThis == 1) {
 8850:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8851:             } else {
 8852:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8853:             }
 8854:         }
 8855:     }
 8856:     return;
 8857: }
 8858: 
 8859: function checkNewSpares(lonhost,type) {
 8860:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8861:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8862:     if (chosen != '') { 
 8863:         var othertype;
 8864:         var othernewSpare;
 8865:         if (type == 'primary') {
 8866:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 8867:         }
 8868:         if (type == 'default') {
 8869:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 8870:         }
 8871:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 8872:             othernewSpare.selectedIndex = 0;
 8873:         }
 8874:     }
 8875:     return;
 8876: }
 8877: 
 8878: // ]]>
 8879: </script>
 8880: 
 8881: END
 8882: 
 8883: }
 8884: 
 8885: sub common_domprefs_js {
 8886:     return <<"END";
 8887: 
 8888: <script type="text/javascript">
 8889: // <![CDATA[
 8890: 
 8891: function getIndicesByName(formname,item) {
 8892:     var group = new Array();
 8893:     for (var i=0;i<formname.elements.length;i++) {
 8894:         if (formname.elements[i].name == item) {
 8895:             group.push(formname.elements[i].id);
 8896:         }
 8897:     }
 8898:     return group;
 8899: }
 8900: 
 8901: // ]]>
 8902: </script>
 8903: 
 8904: END
 8905: 
 8906: }
 8907: 
 8908: sub recaptcha_js {
 8909:     my %lt = &captcha_phrases();
 8910:     return <<"END";
 8911: 
 8912: <script type="text/javascript">
 8913: // <![CDATA[
 8914: 
 8915: function updateCaptcha(caller,context) {
 8916:     var privitem;
 8917:     var pubitem;
 8918:     var privtext;
 8919:     var pubtext;
 8920:     if (document.getElementById(context+'_recaptchapub')) {
 8921:         pubitem = document.getElementById(context+'_recaptchapub');
 8922:     } else {
 8923:         return;
 8924:     }
 8925:     if (document.getElementById(context+'_recaptchapriv')) {
 8926:         privitem = document.getElementById(context+'_recaptchapriv');
 8927:     } else {
 8928:         return;
 8929:     }
 8930:     if (document.getElementById(context+'_recaptchapubtxt')) {
 8931:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 8932:     } else {
 8933:         return;
 8934:     }
 8935:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 8936:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 8937:     } else {
 8938:         return;
 8939:     }
 8940:     if (caller.checked) {
 8941:         if (caller.value == 'recaptcha') {
 8942:             pubitem.type = 'text';
 8943:             privitem.type = 'text';
 8944:             pubitem.size = '40';
 8945:             privitem.size = '40';
 8946:             pubtext.innerHTML = "$lt{'pub'}";
 8947:             privtext.innerHTML = "$lt{'priv'}";
 8948:         } else {
 8949:             pubitem.type = 'hidden';
 8950:             privitem.type = 'hidden';
 8951:             pubtext.innerHTML = '';
 8952:             privtext.innerHTML = '';
 8953:         }
 8954:     }
 8955:     return;
 8956: }
 8957: 
 8958: // ]]>
 8959: </script>
 8960: 
 8961: END
 8962: 
 8963: }
 8964: 
 8965: sub captcha_phrases {
 8966:     return &Apache::lonlocal::texthash (
 8967:                  priv => 'Private key',
 8968:                  pub  => 'Public key',
 8969:                  original  => 'original (CAPTCHA)',
 8970:                  recaptcha => 'successor (ReCAPTCHA)',
 8971:                  notused   => 'unused',
 8972:     );
 8973: }
 8974: 
 8975: 1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>