File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.13: download - view: text, annotated - select for diffs
Wed Jan 9 21:37:04 2013 UTC (11 years, 5 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  - Backport 1.175, 1.177, 1.178, 1.183, 1.184, 1.185.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.13 2013/01/09 21:37:04 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 affiliated 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 affiliation',
  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: 
  388:     my @roles = ('student','coordinator','author','admin');
  389:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  390:     &Apache::lonhtmlcommon::add_breadcrumb
  391:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  392:       text=>"Settings to display/modify"});
  393:     my $confname = $dom.'-domainconfig';
  394: 
  395:     if ($phase eq 'process') {
  396:         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
  397:     } elsif ($phase eq 'display') {
  398:         my $js = &recaptcha_js();
  399:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  400:             my ($othertitle,$usertypes,$types) =
  401:                 &Apache::loncommon::sorted_inst_types($dom);
  402:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  403:                                           $domconfig{'loadbalancing'}).
  404:                    &new_spares_js().
  405:                    &common_domprefs_js().
  406:                    &Apache::loncommon::javascript_array_indexof();
  407:         }
  408:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  409:     } else {
  410: # check if domconfig user exists for the domain.
  411:         my $servadm = $r->dir_config('lonAdmEMail');
  412:         my ($configuserok,$author_ok,$switchserver) = 
  413:             &config_check($dom,$confname,$servadm);
  414:         unless ($configuserok eq 'ok') {
  415:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  416:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  417:                           $confname).
  418:                       '<br />'
  419:             );
  420:             if ($switchserver) {
  421:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  422:                           '<br />'.
  423:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  424:                           '<br />'.
  425:                           &mt('The "[_1]" user can be created automatically when a Domain Coordinator visits the web-based "Set domain configuration" screen, in a session hosted on the primary library server.',$confname).
  426:                           '<br />'.
  427:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  428:                 );
  429:             } else {
  430:                 $r->print(&mt('To create that user from the command line run the ./UPDATE script found in the top level directory of the extracted LON-CAPA tarball.').
  431:                           '<br />'.
  432:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  433:                 );
  434:             }
  435:             $r->print(&Apache::loncommon::end_page());
  436:             return OK;
  437:         }
  438:         if (keys(%domconfig) == 0) {
  439:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  440:             my @ids=&Apache::lonnet::current_machine_ids();
  441:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  442:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  443:                 my @loginimages = ('img','logo','domlogo','login');
  444:                 my $custom_img_count = 0;
  445:                 foreach my $img (@loginimages) {
  446:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  447:                         $custom_img_count ++;
  448:                     }
  449:                 }
  450:                 foreach my $role (@roles) {
  451:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  452:                         $custom_img_count ++;
  453:                     }
  454:                 }
  455:                 if ($custom_img_count > 0) {
  456:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  457:                     my $switch_server = &check_switchserver($dom,$confname);
  458:                     $r->print(
  459:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  460:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  461:     &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 />'.
  462:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  463:                     if ($switch_server) {
  464:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  465:                     }
  466:                     $r->print(&Apache::loncommon::end_page());
  467:                     return OK;
  468:                 }
  469:             }
  470:         }
  471:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  472:     }
  473:     return OK;
  474: }
  475: 
  476: sub process_changes {
  477:     my ($r,$dom,$confname,$action,$roles,$values) = @_;
  478:     my %domconfig;
  479:     if (ref($values) eq 'HASH') {
  480:         %domconfig = %{$values};
  481:     }
  482:     my $output;
  483:     if ($action eq 'login') {
  484:         $output = &modify_login($r,$dom,$confname,%domconfig);
  485:     } elsif ($action eq 'rolecolors') {
  486:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  487:                                      %domconfig);
  488:     } elsif ($action eq 'quotas') {
  489:         $output = &modify_quotas($dom,$action,%domconfig);
  490:     } elsif ($action eq 'autoenroll') {
  491:         $output = &modify_autoenroll($dom,%domconfig);
  492:     } elsif ($action eq 'autoupdate') {
  493:         $output = &modify_autoupdate($dom,%domconfig);
  494:     } elsif ($action eq 'autocreate') {
  495:         $output = &modify_autocreate($dom,%domconfig);
  496:     } elsif ($action eq 'directorysrch') {
  497:         $output = &modify_directorysrch($dom,%domconfig);
  498:     } elsif ($action eq 'usercreation') {
  499:         $output = &modify_usercreation($dom,%domconfig);
  500:     } elsif ($action eq 'usermodification') {
  501:         $output = &modify_usermodification($dom,%domconfig);
  502:     } elsif ($action eq 'contacts') {
  503:         $output = &modify_contacts($dom,%domconfig);
  504:     } elsif ($action eq 'defaults') {
  505:         $output = &modify_defaults($dom,$r);
  506:     } elsif ($action eq 'scantron') {
  507:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  508:     } elsif ($action eq 'coursecategories') {
  509:         $output = &modify_coursecategories($dom,%domconfig);
  510:     } elsif ($action eq 'serverstatuses') {
  511:         $output = &modify_serverstatuses($dom,%domconfig);
  512:     } elsif ($action eq 'requestcourses') {
  513:         $output = &modify_quotas($dom,$action,%domconfig);
  514:     } elsif ($action eq 'requestauthor') {
  515:         $output = &modify_quotas($dom,$action,%domconfig);
  516:     } elsif ($action eq 'usersessions') {
  517:         $output = &modify_usersessions($dom,%domconfig);
  518:     } elsif ($action eq 'loadbalancing') {
  519:         $output = &modify_loadbalancing($dom,%domconfig);
  520:     }
  521:     return $output;
  522: }
  523: 
  524: sub print_config_box {
  525:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  526:     my $rowtotal = 0;
  527:     my $output;
  528:     if ($action eq 'coursecategories') {
  529:         $output = &coursecategories_javascript($settings);
  530:     }
  531:     $output .= 
  532:          '<table class="LC_nested_outer">
  533:           <tr>
  534:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  535:            &mt($item->{text}).'&nbsp;'.
  536:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  537:           '</tr>';
  538:     $rowtotal ++;
  539:     my $numheaders = 1;
  540:     if (ref($item->{'header'}) eq 'ARRAY') {
  541:         $numheaders = scalar(@{$item->{'header'}});
  542:     }
  543:     if ($numheaders > 1) {
  544:         my $colspan = '';
  545:         my $rightcolspan = '';
  546:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  547:             (($action eq 'login') && ($numheaders < 3))) {
  548:             $colspan = ' colspan="2"';
  549:         }
  550:         if ($action eq 'usersessions') {
  551:             $rightcolspan = ' colspan="3"'; 
  552:         }
  553:         $output .= '
  554:           <tr>
  555:            <td>
  556:             <table class="LC_nested">
  557:              <tr class="LC_info_row">
  558:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  559:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  560:              </tr>';
  561:         $rowtotal ++;
  562:         if ($action eq 'autoupdate') {
  563:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  564:         } elsif ($action eq 'usercreation') {
  565:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  566:         } elsif ($action eq 'usermodification') {
  567:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  568:         } elsif ($action eq 'coursecategories') {
  569:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  570:         } elsif ($action eq 'login') {
  571:             if ($numheaders == 3) {
  572:                 $colspan = ' colspan="2"';
  573:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  574:             } else {
  575:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  576:             }
  577:         } elsif ($action eq 'requestcourses') {
  578:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  579:         } elsif ($action eq 'requestauthor') {
  580:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  581:         } elsif ($action eq 'usersessions') {
  582:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  583:         } elsif ($action eq 'rolecolors') {
  584:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  585:         }
  586:         $output .= '
  587:            </table>
  588:           </td>
  589:          </tr>
  590:          <tr>
  591:            <td>
  592:             <table class="LC_nested">
  593:              <tr class="LC_info_row">
  594:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  595:         $output .= '
  596:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  597:              </tr>';
  598:             $rowtotal ++;
  599:         if ($action eq 'autoupdate') {
  600:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  601:            </table>
  602:           </td>
  603:          </tr>
  604:          <tr>
  605:            <td>
  606:             <table class="LC_nested">
  607:              <tr class="LC_info_row">
  608:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  609:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  610:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  611:             $rowtotal ++;
  612:         } elsif ($action eq 'usercreation') {
  613:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  614:            </table>
  615:           </td>
  616:          </tr>
  617:          <tr>
  618:            <td>
  619:             <table class="LC_nested">
  620:              <tr class="LC_info_row">
  621:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  622:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  623:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  624:             $rowtotal ++;
  625:         } elsif ($action eq 'usermodification') {
  626:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  627:            </table>
  628:           </td>
  629:          </tr>
  630:          <tr>
  631:            <td>
  632:             <table class="LC_nested">
  633:              <tr class="LC_info_row">
  634:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  635:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  636:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  637:             $rowtotal ++;
  638:         } elsif ($action eq 'coursecategories') {
  639:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  640:         } elsif ($action eq 'login') {
  641:             if ($numheaders == 3) {
  642:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  643:            </table>
  644:           </td>
  645:          </tr>
  646:          <tr>
  647:            <td>
  648:             <table class="LC_nested">
  649:              <tr class="LC_info_row">
  650:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  651:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  652:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  653:                 $rowtotal ++;
  654:             } else {
  655:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  656:             }
  657:         } elsif ($action eq 'requestcourses') {
  658:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  659:         } elsif ($action eq 'requestauthor') {
  660:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  661:         } elsif ($action eq 'usersessions') {
  662:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  663:            </table>
  664:           </td>
  665:          </tr>
  666:          <tr>
  667:            <td>
  668:             <table class="LC_nested">
  669:              <tr class="LC_info_row">
  670:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  671:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  672:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  673:             $rowtotal ++;
  674:         } elsif ($action eq 'coursedefaults') {
  675:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  676:         } elsif ($action eq 'rolecolors') {
  677:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  678:            </table>
  679:           </td>
  680:          </tr>
  681:          <tr>
  682:            <td>
  683:             <table class="LC_nested">
  684:              <tr class="LC_info_row">
  685:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  686:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  687:               <td class="LC_right_item" valign="top">'.
  688:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  689:              </tr>'.
  690:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  691:            </table>
  692:           </td>
  693:          </tr>
  694:          <tr>
  695:            <td>
  696:             <table class="LC_nested">
  697:              <tr class="LC_info_row">
  698:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  699:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  700:              </tr>'.
  701:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  702:             $rowtotal += 2;
  703:         }
  704:     } else {
  705:         $output .= '
  706:           <tr>
  707:            <td>
  708:             <table class="LC_nested">
  709:              <tr class="LC_info_row">';
  710:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  711:             $output .= '  
  712:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  713:         } elsif ($action eq 'serverstatuses') {
  714:             $output .= '
  715:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  716:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  717: 
  718:         } else {
  719:             $output .= '
  720:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  721:         }
  722:         if (defined($item->{'header'}->[0]->{'col3'})) {
  723:             $output .= '<td class="LC_left_item" valign="top">'.
  724:                        &mt($item->{'header'}->[0]->{'col2'});
  725:             if ($action eq 'serverstatuses') {
  726:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  727:             } 
  728:         } else {
  729:             $output .= '<td class="LC_right_item" valign="top">'.
  730:                        &mt($item->{'header'}->[0]->{'col2'});
  731:         }
  732:         $output .= '</td>';
  733:         if ($item->{'header'}->[0]->{'col3'}) {
  734:             if (defined($item->{'header'}->[0]->{'col4'})) {
  735:                 $output .= '<td class="LC_left_item" valign="top">'.
  736:                             &mt($item->{'header'}->[0]->{'col3'});
  737:             } else {
  738:                 $output .= '<td class="LC_right_item" valign="top">'.
  739:                            &mt($item->{'header'}->[0]->{'col3'});
  740:             }
  741:             if ($action eq 'serverstatuses') {
  742:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  743:             }
  744:             $output .= '</td>';
  745:         }
  746:         if ($item->{'header'}->[0]->{'col4'}) {
  747:             $output .= '<td class="LC_right_item" valign="top">'.
  748:                        &mt($item->{'header'}->[0]->{'col4'});
  749:         }
  750:         $output .= '</tr>';
  751:         $rowtotal ++;
  752:         if ($action eq 'quotas') {
  753:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  754:         } elsif ($action eq 'autoenroll') {
  755:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  756:         } elsif ($action eq 'autocreate') {
  757:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  758:         } elsif ($action eq 'directorysrch') {
  759:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  760:         } elsif ($action eq 'contacts') {
  761:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  762:         } elsif ($action eq 'defaults') {
  763:             $output .= &print_defaults($dom,\$rowtotal);
  764:         } elsif ($action eq 'scantron') {
  765:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  766:         } elsif ($action eq 'serverstatuses') {
  767:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  768:         } elsif ($action eq 'helpsettings') {
  769:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  770:         } elsif ($action eq 'loadbalancing') {
  771:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  772:         }
  773:     }
  774:     $output .= '
  775:    </table>
  776:   </td>
  777:  </tr>
  778: </table><br />';
  779:     return ($output,$rowtotal);
  780: }
  781: 
  782: sub print_login {
  783:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  784:     my ($css_class,$datatable);
  785:     my %choices = &login_choices();
  786: 
  787:     if ($caller eq 'service') {
  788:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  789:         my $choice = $choices{'disallowlogin'};
  790:         $css_class = ' class="LC_odd_row"';
  791:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  792:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  793:                       '<th>'.$choices{'server'}.'</th>'.
  794:                       '<th>'.$choices{'serverpath'}.'</th>'.
  795:                       '<th>'.$choices{'custompath'}.'</th>'.
  796:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  797:         my %disallowed;
  798:         if (ref($settings) eq 'HASH') {
  799:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  800:                %disallowed = %{$settings->{'loginvia'}};
  801:             }
  802:         }
  803:         foreach my $lonhost (sort(keys(%servers))) {
  804:             my $direct = 'selected="selected"';
  805:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  806:                 if ($disallowed{$lonhost}{'server'} ne '') {
  807:                     $direct = '';
  808:                 }
  809:             }
  810:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  811:                           '<td><select name="'.$lonhost.'_server">'.
  812:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  813:                           '</option>';
  814:             foreach my $hostid (sort(keys(%servers))) {
  815:                 next if ($servers{$hostid} eq $servers{$lonhost});
  816:                 my $selected = '';
  817:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  818:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  819:                         $selected = 'selected="selected"';
  820:                     }
  821:                 }
  822:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  823:                               $servers{$hostid}.'</option>';
  824:             }
  825:             $datatable .= '</select></td>'.
  826:                           '<td><select name="'.$lonhost.'_serverpath">';
  827:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  828:                 my $pathname = $path;
  829:                 if ($path eq 'custom') {
  830:                     $pathname = &mt('Custom Path').' ->';
  831:                 }
  832:                 my $selected = '';
  833:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  834:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  835:                         $selected = 'selected="selected"';
  836:                     }
  837:                 } elsif ($path eq '') {
  838:                     $selected = 'selected="selected"';
  839:                 }
  840:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  841:             }
  842:             $datatable .= '</select></td>';
  843:             my ($custom,$exempt);
  844:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  845:                 $custom = $disallowed{$lonhost}{'custompath'};
  846:                 $exempt = $disallowed{$lonhost}{'exempt'};
  847:             }
  848:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  849:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  850:                           '</tr>';
  851:         }
  852:         $datatable .= '</table></td></tr>';
  853:         return $datatable;
  854:     } elsif ($caller eq 'page') {
  855:         my %defaultchecked = ( 
  856:                                'coursecatalog' => 'on',
  857:                                'adminmail'     => 'off',
  858:                                'newuser'       => 'off',
  859:                              );
  860:         my @toggles = ('coursecatalog','adminmail','newuser');
  861:         my (%checkedon,%checkedoff);
  862:         foreach my $item (@toggles) {
  863:             if ($defaultchecked{$item} eq 'on') { 
  864:                 $checkedon{$item} = ' checked="checked" ';
  865:                 $checkedoff{$item} = ' ';
  866:             } elsif ($defaultchecked{$item} eq 'off') {
  867:                 $checkedoff{$item} = ' checked="checked" ';
  868:                 $checkedon{$item} = ' ';
  869:             }
  870:         }
  871:         my @images = ('img','logo','domlogo','login');
  872:         my @logintext = ('textcol','bgcol');
  873:         my @bgs = ('pgbg','mainbg','sidebg');
  874:         my @links = ('link','alink','vlink');
  875:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  876:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  877:         my (%is_custom,%designs);
  878:         my %defaults = (
  879:                        font => $defaultdesign{'login.font'},
  880:                        );
  881:         foreach my $item (@images) {
  882:             $defaults{$item} = $defaultdesign{'login.'.$item};
  883:             $defaults{'showlogo'}{$item} = 1;
  884:         }
  885:         foreach my $item (@bgs) {
  886:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  887:         }
  888:         foreach my $item (@logintext) {
  889:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  890:         }
  891:         foreach my $item (@links) {
  892:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  893:         }
  894:         if (ref($settings) eq 'HASH') {
  895:             foreach my $item (@toggles) {
  896:                 if ($settings->{$item} eq '1') {
  897:                     $checkedon{$item} =  ' checked="checked" ';
  898:                     $checkedoff{$item} = ' ';
  899:                 } elsif ($settings->{$item} eq '0') {
  900:                     $checkedoff{$item} =  ' checked="checked" ';
  901:                     $checkedon{$item} = ' ';
  902:                 }
  903:             }
  904:             foreach my $item (@images) {
  905:                 if (defined($settings->{$item})) {
  906:                     $designs{$item} = $settings->{$item};
  907:                     $is_custom{$item} = 1;
  908:                 }
  909:                 if (defined($settings->{'showlogo'}{$item})) {
  910:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  911:                 }
  912:             }
  913:             foreach my $item (@logintext) {
  914:                 if ($settings->{$item} ne '') {
  915:                     $designs{'logintext'}{$item} = $settings->{$item};
  916:                     $is_custom{$item} = 1;
  917:                 }
  918:             }
  919:             if ($settings->{'font'} ne '') {
  920:                 $designs{'font'} = $settings->{'font'};
  921:                 $is_custom{'font'} = 1;
  922:             }
  923:             foreach my $item (@bgs) {
  924:                 if ($settings->{$item} ne '') {
  925:                     $designs{'bgs'}{$item} = $settings->{$item};
  926:                     $is_custom{$item} = 1;
  927:                 }
  928:             }
  929:             foreach my $item (@links) {
  930:                 if ($settings->{$item} ne '') {
  931:                     $designs{'links'}{$item} = $settings->{$item};
  932:                     $is_custom{$item} = 1;
  933:                 }
  934:             }
  935:         } else {
  936:             if ($designhash{$dom.'.login.font'} ne '') {
  937:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  938:                 $is_custom{'font'} = 1;
  939:             }
  940:             foreach my $item (@images) {
  941:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  942:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  943:                     $is_custom{$item} = 1;
  944:                 }
  945:             }
  946:             foreach my $item (@bgs) {
  947:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  948:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  949:                     $is_custom{$item} = 1;
  950:                 }
  951:             }
  952:             foreach my $item (@links) {
  953:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  954:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  955:                     $is_custom{$item} = 1;
  956:                 }
  957:             }
  958:         }
  959:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  960:                                                       logo => 'Institution Logo',
  961:                                                       domlogo => 'Domain Logo',
  962:                                                       login => 'Login box');
  963:         my $itemcount = 1;
  964:         foreach my $item (@toggles) {
  965:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
  966:             $datatable .=  
  967:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  968:                 '</td><td>'.
  969:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
  970:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
  971:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
  972:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
  973:                 '</tr>';
  974:             $itemcount ++;
  975:         }
  976:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
  977:         $datatable .= '</tr></table></td></tr>';
  978:     } elsif ($caller eq 'help') {
  979:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
  980:         my $switchserver = &check_switchserver($dom,$confname);
  981:         my $itemcount = 1;
  982:         $defaulturl = '/adm/loginproblems.html';
  983:         $defaulttype = 'default';
  984:         %lt = &Apache::lonlocal::texthash (
  985:                      del     => 'Delete?',
  986:                      rep     => 'Replace:',
  987:                      upl     => 'Upload:',
  988:                      default => 'Default',
  989:                      custom  => 'Custom',
  990:                                              );
  991:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
  992:         my @currlangs;
  993:         if (ref($settings) eq 'HASH') {
  994:             if (ref($settings->{'helpurl'}) eq 'HASH') {
  995:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
  996:                     next if ($settings->{'helpurl'}{$key} eq '');
  997:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
  998:                     $type{$key} = 'custom';
  999:                     unless ($key eq 'nolang') {
 1000:                         push(@currlangs,$key);
 1001:                     }
 1002:                 }
 1003:             } elsif ($settings->{'helpurl'} ne '') {
 1004:                 $type{'nolang'} = 'custom';
 1005:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1006:             }
 1007:         }
 1008:         foreach my $lang ('nolang',sort(@currlangs)) {
 1009:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1010:             $datatable .= '<tr'.$css_class.'>';
 1011:             if ($url{$lang} eq '') {
 1012:                 $url{$lang} = $defaulturl;
 1013:             }
 1014:             if ($type{$lang} eq '') {
 1015:                 $type{$lang} = $defaulttype;
 1016:             }
 1017:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1018:             if ($lang eq 'nolang') {
 1019:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1020:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1021:             } else {
 1022:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1023:                                   $langchoices{$lang},
 1024:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1025:             }
 1026:             $datatable .= '</span></td>'."\n".
 1027:                           '<td class="LC_left_item">';
 1028:             if ($type{$lang} eq 'custom') {
 1029:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1030:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1031:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1032:             } else {
 1033:                 $datatable .= $lt{'upl'};
 1034:             }
 1035:             $datatable .='<br />';
 1036:             if ($switchserver) {
 1037:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1038:             } else {
 1039:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1040:             }
 1041:             $datatable .= '</td></tr>';
 1042:             $itemcount ++;
 1043:         }
 1044:         my @addlangs;
 1045:         foreach my $lang (sort(keys(%langchoices))) {
 1046:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1047:             push(@addlangs,$lang);
 1048:         }
 1049:         if (@addlangs > 0) {
 1050:             my %toadd;
 1051:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1052:             $toadd{''} = &mt('Select');
 1053:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1054:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1055:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1056:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1057:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1058:             if ($switchserver) {
 1059:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1060:             } else {
 1061:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1062:             }
 1063:             $datatable .= '</td></tr>';
 1064:             $itemcount ++;
 1065:         }
 1066:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1067:     }
 1068:     return $datatable;
 1069: }
 1070: 
 1071: sub login_choices {
 1072:     my %choices =
 1073:         &Apache::lonlocal::texthash (
 1074:             coursecatalog => 'Display Course/Community Catalog link?',
 1075:             adminmail     => "Display Administrator's E-mail Address?",
 1076:             disallowlogin => "Login page requests redirected",
 1077:             hostid        => "Server",
 1078:             server        => "Redirect to:",
 1079:             serverpath    => "Path",
 1080:             custompath    => "Custom", 
 1081:             exempt        => "Exempt IP(s)",
 1082:             directlogin   => "No redirect",
 1083:             newuser       => "Link to create a user account",
 1084:             img           => "Header",
 1085:             logo          => "Main Logo",
 1086:             domlogo       => "Domain Logo",
 1087:             login         => "Log-in Header", 
 1088:             textcol       => "Text color",
 1089:             bgcol         => "Box color",
 1090:             bgs           => "Background colors",
 1091:             links         => "Link colors",
 1092:             font          => "Font color",
 1093:             pgbg          => "Header",
 1094:             mainbg        => "Page",
 1095:             sidebg        => "Login box",
 1096:             link          => "Link",
 1097:             alink         => "Active link",
 1098:             vlink         => "Visited link",
 1099:         );
 1100:     return %choices;
 1101: }
 1102: 
 1103: sub print_rolecolors {
 1104:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1105:     my %choices = &color_font_choices();
 1106:     my @bgs = ('pgbg','tabbg','sidebg');
 1107:     my @links = ('link','alink','vlink');
 1108:     my @images = ('img');
 1109:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1110:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1111:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1112:     my (%is_custom,%designs);
 1113:     my %defaults = (
 1114:                    img => $defaultdesign{$role.'.img'},
 1115:                    font => $defaultdesign{$role.'.font'},
 1116: 		   fontmenu => $defaultdesign{$role.'.fontmenu'},
 1117:                    );
 1118:     foreach my $item (@bgs) {
 1119:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1120:     }
 1121:     foreach my $item (@links) {
 1122:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1123:     }
 1124:     if (ref($settings) eq 'HASH') {
 1125:         if (ref($settings->{$role}) eq 'HASH') {
 1126:             if ($settings->{$role}->{'img'} ne '') {
 1127:                 $designs{'img'} = $settings->{$role}->{'img'};
 1128:                 $is_custom{'img'} = 1;
 1129:             }
 1130:             if ($settings->{$role}->{'font'} ne '') {
 1131:                 $designs{'font'} = $settings->{$role}->{'font'};
 1132:                 $is_custom{'font'} = 1;
 1133:             }
 1134:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1135:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1136:                 $is_custom{'fontmenu'} = 1;
 1137:             }
 1138:             foreach my $item (@bgs) {
 1139:                 if ($settings->{$role}->{$item} ne '') {
 1140:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1141:                     $is_custom{$item} = 1;
 1142:                 }
 1143:             }
 1144:             foreach my $item (@links) {
 1145:                 if ($settings->{$role}->{$item} ne '') {
 1146:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1147:                     $is_custom{$item} = 1;
 1148:                 }
 1149:             }
 1150:         }
 1151:     } else {
 1152:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1153:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1154:             $is_custom{'img'} = 1;
 1155:         }
 1156:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1157:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1158:             $is_custom{'fontmenu'} = 1; 
 1159:         }
 1160:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1161:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1162:             $is_custom{'font'} = 1;
 1163:         }
 1164:         foreach my $item (@bgs) {
 1165:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1166:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1167:                 $is_custom{$item} = 1;
 1168:             
 1169:             }
 1170:         }
 1171:         foreach my $item (@links) {
 1172:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1173:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1174:                 $is_custom{$item} = 1;
 1175:             }
 1176:         }
 1177:     }
 1178:     my $itemcount = 1;
 1179:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1180:     $datatable .= '</tr></table></td></tr>';
 1181:     return $datatable;
 1182: }
 1183: 
 1184: sub display_color_options {
 1185:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1186:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1187:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1188:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1189:     my $datatable = '<tr'.$css_class.'>'.
 1190:         '<td>'.$choices->{'font'}.'</td>';
 1191:     if (!$is_custom->{'font'}) {
 1192:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1193:     } else {
 1194:         $datatable .= '<td>&nbsp;</td>';
 1195:     }
 1196:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1197: 
 1198:     $datatable .= '<td><span class="LC_nobreak">'.
 1199:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1200:                   ' value="'.$current_color.'" />&nbsp;'.
 1201:                   '&nbsp;</td></tr>';
 1202:     unless ($role eq 'login') { 
 1203:         $datatable .= '<tr'.$css_class.'>'.
 1204:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1205:         if (!$is_custom->{'fontmenu'}) {
 1206:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1207:         } else {
 1208:             $datatable .= '<td>&nbsp;</td>';
 1209:         }
 1210:         $current_color = $designs->{'fontmenu'} ?
 1211:             $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1212:         $datatable .= '<td><span class="LC_nobreak">'.
 1213:                       '<input class="colorchooser" type="text" size="10" name="'
 1214:                       .$role.'_fontmenu"'.
 1215:                       ' value="'.$current_color.'" />&nbsp;'.
 1216:                       '&nbsp;</td></tr>';
 1217:     }
 1218:     my $switchserver = &check_switchserver($dom,$confname);
 1219:     foreach my $img (@{$images}) {
 1220: 	$itemcount ++;
 1221:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1222:         $datatable .= '<tr'.$css_class.'>'.
 1223:                       '<td>'.$choices->{$img};
 1224:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1225:         if ($role eq 'login') {
 1226:             if ($img eq 'login') {
 1227:                 $login_hdr_pick =
 1228:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1229:                 $logincolors =
 1230:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1231:                                             $designs);
 1232:             } elsif ($img ne 'domlogo') {
 1233:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1234:             }
 1235:         }
 1236:         $datatable .= '</td>';
 1237:         if ($designs->{$img} ne '') {
 1238:             $imgfile = $designs->{$img};
 1239: 	    $img_import = ($imgfile =~ m{^/adm/});
 1240:         } else {
 1241:             $imgfile = $defaults->{$img};
 1242:         }
 1243:         if ($imgfile) {
 1244:             my ($showfile,$fullsize);
 1245:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1246:                 my $urldir = $1;
 1247:                 my $filename = $2;
 1248:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1249:                 if (@info) {
 1250:                     my $thumbfile = 'tn-'.$filename;
 1251:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1252:                     if (@thumb) {
 1253:                         $showfile = $urldir.'/'.$thumbfile;
 1254:                     } else {
 1255:                         $showfile = $imgfile;
 1256:                     }
 1257:                 } else {
 1258:                     $showfile = '';
 1259:                 }
 1260:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1261:                 $showfile = $imgfile;
 1262:                 my $imgdir = $1;
 1263:                 my $filename = $2;
 1264:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1265:                     $showfile = "/$imgdir/tn-".$filename;
 1266:                 } else {
 1267:                     my $input = $londocroot.$imgfile;
 1268:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1269:                     if (!-e $output) {
 1270:                         my ($width,$height) = &thumb_dimensions();
 1271:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1272:                         if ($fullwidth ne '' && $fullheight ne '') {
 1273:                             if ($fullwidth > $width && $fullheight > $height) { 
 1274:                                 my $size = $width.'x'.$height;
 1275:                                 system("convert -sample $size $input $output");
 1276:                                 $showfile = "/$imgdir/tn-".$filename;
 1277:                             }
 1278:                         }
 1279:                     }
 1280:                 }
 1281:             }
 1282:             if ($showfile) {
 1283:                 if ($showfile =~ m{^/(adm|res)/}) {
 1284:                     if ($showfile =~ m{^/res/}) {
 1285:                         my $local_showfile =
 1286:                             &Apache::lonnet::filelocation('',$showfile);
 1287:                         &Apache::lonnet::repcopy($local_showfile);
 1288:                     }
 1289:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1290:                 }
 1291:                 if ($imgfile) {
 1292:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1293:                         if ($imgfile =~ m{^/res/}) {
 1294:                             my $local_imgfile =
 1295:                                 &Apache::lonnet::filelocation('',$imgfile);
 1296:                             &Apache::lonnet::repcopy($local_imgfile);
 1297:                         }
 1298:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1299:                     } else {
 1300:                         $fullsize = $imgfile;
 1301:                     }
 1302:                 }
 1303:                 $datatable .= '<td>';
 1304:                 if ($img eq 'login') {
 1305:                     $datatable .= $login_hdr_pick;
 1306:                 } 
 1307:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1308:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1309:             } else {
 1310:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1311:                               &mt('Upload:');
 1312:             }
 1313:         } else {
 1314:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1315:                           &mt('Upload:');
 1316:         }
 1317:         if ($switchserver) {
 1318:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1319:         } else {
 1320:             if ($img ne 'login') { # suppress file selection for Log-in header
 1321:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1322:             }
 1323:         }
 1324:         $datatable .= '</td></tr>';
 1325:     }
 1326:     $itemcount ++;
 1327:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1328:     $datatable .= '<tr'.$css_class.'>'.
 1329:                   '<td>'.$choices->{'bgs'}.'</td>';
 1330:     my $bgs_def;
 1331:     foreach my $item (@{$bgs}) {
 1332:         if (!$is_custom->{$item}) {
 1333:             $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>';
 1334:         }
 1335:     }
 1336:     if ($bgs_def) {
 1337:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1338:     } else {
 1339:         $datatable .= '<td>&nbsp;</td>';
 1340:     }
 1341:     $datatable .= '<td class="LC_right_item">'.
 1342:                   '<table border="0"><tr>';
 1343: 
 1344:     foreach my $item (@{$bgs}) {
 1345:         $datatable .= '<td align="center">';
 1346:         my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1347:         if ($designs->{'bgs'}{$item}) {
 1348:             $datatable .= '&nbsp;';
 1349:         }
 1350:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1351:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1352:     }
 1353:     $datatable .= '</tr></table></td></tr>';
 1354:     $itemcount ++;
 1355:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1356:     $datatable .= '<tr'.$css_class.'>'.
 1357:                   '<td>'.$choices->{'links'}.'</td>';
 1358:     my $links_def;
 1359:     foreach my $item (@{$links}) {
 1360:         if (!$is_custom->{$item}) {
 1361:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1362:         }
 1363:     }
 1364:     if ($links_def) {
 1365:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1366:     } else {
 1367:         $datatable .= '<td>&nbsp;</td>';
 1368:     }
 1369:     $datatable .= '<td class="LC_right_item">'.
 1370:                   '<table border="0"><tr>';
 1371:     foreach my $item (@{$links}) {
 1372:         my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1373:         $datatable .= '<td align="center">'."\n";
 1374: 
 1375:         if ($designs->{'links'}{$item}) {
 1376:             $datatable.='&nbsp;';
 1377:         }
 1378:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1379:                       '" /></td>';
 1380:     }
 1381:     $$rowtotal += $itemcount;
 1382:     return $datatable;
 1383: }
 1384: 
 1385: sub logo_display_options {
 1386:     my ($img,$defaults,$designs) = @_;
 1387:     my $checkedon;
 1388:     if (ref($defaults) eq 'HASH') {
 1389:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1390:             if ($defaults->{'showlogo'}{$img}) {
 1391:                 $checkedon = 'checked="checked" ';     
 1392:             }
 1393:         } 
 1394:     }
 1395:     if (ref($designs) eq 'HASH') {
 1396:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1397:             if (defined($designs->{'showlogo'}{$img})) {
 1398:                 if ($designs->{'showlogo'}{$img} == 0) {
 1399:                     $checkedon = '';
 1400:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1401:                     $checkedon = 'checked="checked" ';
 1402:                 }
 1403:             }
 1404:         }
 1405:     }
 1406:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1407:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1408:            &mt('show').'</label>'."\n";
 1409: }
 1410: 
 1411: sub login_header_options  {
 1412:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1413:     my $output = '';
 1414:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1415:         $output .= &mt('Text default(s):').'<br />';
 1416:         if (!$is_custom->{'textcol'}) {
 1417:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1418:                        '&nbsp;&nbsp;&nbsp;';
 1419:         }
 1420:         if (!$is_custom->{'bgcol'}) {
 1421:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1422:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1423:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1424:         }
 1425:         $output .= '<br />';
 1426:     }
 1427:     $output .='<br />';
 1428:     return $output;
 1429: }
 1430: 
 1431: sub login_text_colors {
 1432:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1433:     my $color_menu = '<table border="0"><tr>';
 1434:     foreach my $item (@{$logintext}) {
 1435:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1436:         $color_menu .= '<td align="center">'.$link;
 1437:         if ($designs->{'logintext'}{$item}) {
 1438:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1439:         }
 1440:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1441:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1442:                        '<td>&nbsp;</td>';
 1443:     }
 1444:     $color_menu .= '</tr></table><br />';
 1445:     return $color_menu;
 1446: }
 1447: 
 1448: sub image_changes {
 1449:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1450:     my $output;
 1451:     if ($img eq 'login') {
 1452:             # suppress image for Log-in header
 1453:     } elsif (!$is_custom) {
 1454:         if ($img ne 'domlogo') {
 1455:             $output .= &mt('Default image:').'<br />';
 1456:         } else {
 1457:             $output .= &mt('Default in use:').'<br />';
 1458:         }
 1459:     }
 1460:     if ($img eq 'login') { # suppress image for Log-in header
 1461:         $output .= '<td>'.$logincolors;
 1462:     } else {
 1463:         if ($img_import) {
 1464:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1465:         }
 1466:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1467:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1468:         if ($is_custom) {
 1469:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1470:                        '<input type="checkbox" name="'.
 1471:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1472:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1473:         } else {
 1474:             $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1475:         }
 1476:     }
 1477:     return $output;
 1478: }
 1479: 
 1480: sub color_pick {
 1481:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1482:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1483:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1484:                ');">'.$desc.'</a>';
 1485:     return $link;
 1486: }
 1487: 
 1488: sub print_quotas {
 1489:     my ($dom,$settings,$rowtotal,$action) = @_;
 1490:     my $context;
 1491:     if ($action eq 'quotas') {
 1492:         $context = 'tools';
 1493:     } else {
 1494:         $context = $action;
 1495:     }
 1496:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 1497:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1498:     my $typecount = 0;
 1499:     my ($css_class,%titles);
 1500:     if ($context eq 'requestcourses') {
 1501:         @usertools = ('official','unofficial','community');
 1502:         @options =('norequest','approval','validate','autolimit');
 1503:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1504:         %titles = &courserequest_titles();
 1505:     } elsif ($context eq 'requestauthor') {
 1506:         @usertools = ('author');
 1507:         @options = ('norequest','approval','automatic');
 1508:         %titles = &authorrequest_titles();
 1509:     } else {
 1510:         @usertools = ('aboutme','blog','webdav','portfolio');
 1511:         %titles = &tool_titles();
 1512:     }
 1513:     if (ref($types) eq 'ARRAY') {
 1514:         foreach my $type (@{$types}) {
 1515:             my $currdefquota;
 1516:             unless (($context eq 'requestcourses') ||
 1517:                     ($context eq 'requestauthor')) {
 1518:                 if (ref($settings) eq 'HASH') {
 1519:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1520:                         $currdefquota = $settings->{defaultquota}->{$type}; 
 1521:                     } else {
 1522:                         $currdefquota = $settings->{$type};
 1523:                     }
 1524:                 }
 1525:             }
 1526:             if (defined($usertypes->{$type})) {
 1527:                 $typecount ++;
 1528:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1529:                 $datatable .= '<tr'.$css_class.'>'.
 1530:                               '<td>'.$usertypes->{$type}.'</td>'.
 1531:                               '<td class="LC_left_item">';
 1532:                 if ($context eq 'requestcourses') {
 1533:                     $datatable .= '<table><tr>';
 1534:                 }
 1535:                 my %cell;  
 1536:                 foreach my $item (@usertools) {
 1537:                     if ($context eq 'requestcourses') {
 1538:                         my ($curroption,$currlimit);
 1539:                         if (ref($settings) eq 'HASH') {
 1540:                             if (ref($settings->{$item}) eq 'HASH') {
 1541:                                 $curroption = $settings->{$item}->{$type};
 1542:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1543:                                     $currlimit = $1; 
 1544:                                 }
 1545:                             }
 1546:                         }
 1547:                         if (!$curroption) {
 1548:                             $curroption = 'norequest';
 1549:                         }
 1550:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1551:                         foreach my $option (@options) {
 1552:                             my $val = $option;
 1553:                             if ($option eq 'norequest') {
 1554:                                 $val = 0;  
 1555:                             }
 1556:                             if ($option eq 'validate') {
 1557:                                 my $canvalidate = 0;
 1558:                                 if (ref($validations{$item}) eq 'HASH') { 
 1559:                                     if ($validations{$item}{$type}) {
 1560:                                         $canvalidate = 1;
 1561:                                     }
 1562:                                 }
 1563:                                 next if (!$canvalidate);
 1564:                             }
 1565:                             my $checked = '';
 1566:                             if ($option eq $curroption) {
 1567:                                 $checked = ' checked="checked"';
 1568:                             } elsif ($option eq 'autolimit') {
 1569:                                 if ($curroption =~ /^autolimit/) {
 1570:                                     $checked = ' checked="checked"';
 1571:                                 }                       
 1572:                             } 
 1573:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1574:                                   '<input type="radio" name="crsreq_'.$item.
 1575:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1576:                                   $titles{$option}.'</label>';
 1577:                             if ($option eq 'autolimit') {
 1578:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1579:                                                 $item.'_limit_'.$type.'" size="1" '.
 1580:                                                 'value="'.$currlimit.'" />';
 1581:                             }
 1582:                             $cell{$item} .= '</span> ';
 1583:                             if ($option eq 'autolimit') {
 1584:                                 $cell{$item} .= $titles{'unlimited'};
 1585:                             }
 1586:                         }
 1587:                     } elsif ($context eq 'requestauthor') {
 1588:                         my $curroption;
 1589:                         if (ref($settings) eq 'HASH') {
 1590:                             $curroption = $settings->{$type};
 1591:                         }
 1592:                         if (!$curroption) {
 1593:                             $curroption = 'norequest';
 1594:                         }
 1595:                         foreach my $option (@options) {
 1596:                             my $val = $option;
 1597:                             if ($option eq 'norequest') {
 1598:                                 $val = 0;
 1599:                             }
 1600:                             my $checked = '';
 1601:                             if ($option eq $curroption) {
 1602:                                 $checked = ' checked="checked"';
 1603:                             }
 1604:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1605:                                   '<input type="radio" name="authorreq_'.$type.
 1606:                                   '" value="'.$val.'"'.$checked.' />'.
 1607:                                   $titles{$option}.'</label></span>&nbsp; ';
 1608:                         }
 1609:                     } else {
 1610:                         my $checked = 'checked="checked" ';
 1611:                         if (ref($settings) eq 'HASH') {
 1612:                             if (ref($settings->{$item}) eq 'HASH') {
 1613:                                 if ($settings->{$item}->{$type} == 0) {
 1614:                                     $checked = '';
 1615:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1616:                                     $checked =  'checked="checked" ';
 1617:                                 }
 1618:                             }
 1619:                         }
 1620:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1621:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1622:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1623:                                       '</label></span>&nbsp; ';
 1624:                     }
 1625:                 }
 1626:                 if ($context eq 'requestcourses') {
 1627:                     $datatable .= '</tr><tr>';
 1628:                     foreach my $item (@usertools) {
 1629:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1630:                     }
 1631:                     $datatable .= '</tr></table>';
 1632:                 }
 1633:                 $datatable .= '</td>';
 1634:                 unless (($context eq 'requestcourses') ||
 1635:                         ($context eq 'requestauthor')) {
 1636:                     $datatable .= 
 1637:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1638:                               '<input type="text" name="quota_'.$type.
 1639:                               '" value="'.$currdefquota.
 1640:                               '" size="5" /> Mb</span></td>';
 1641:                 }
 1642:                 $datatable .= '</tr>';
 1643:             }
 1644:         }
 1645:     }
 1646:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1647:         $defaultquota = '20';
 1648:         if (ref($settings) eq 'HASH') {
 1649:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1650:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1651:             } elsif (defined($settings->{'default'})) {
 1652:                 $defaultquota = $settings->{'default'};
 1653:             }
 1654:         }
 1655:     }
 1656:     $typecount ++;
 1657:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1658:     $datatable .= '<tr'.$css_class.'>'.
 1659:                   '<td>'.$othertitle.'</td>'.
 1660:                   '<td class="LC_left_item">';
 1661:     if ($context eq 'requestcourses') {
 1662:         $datatable .= '<table><tr>';
 1663:     }
 1664:     my %defcell;
 1665:     foreach my $item (@usertools) {
 1666:         if ($context eq 'requestcourses') {
 1667:             my ($curroption,$currlimit);
 1668:             if (ref($settings) eq 'HASH') {
 1669:                 if (ref($settings->{$item}) eq 'HASH') {
 1670:                     $curroption = $settings->{$item}->{'default'};
 1671:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1672:                         $currlimit = $1;
 1673:                     }
 1674:                 }
 1675:             }
 1676:             if (!$curroption) {
 1677:                 $curroption = 'norequest';
 1678:             }
 1679:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1680:             foreach my $option (@options) {
 1681:                 my $val = $option;
 1682:                 if ($option eq 'norequest') {
 1683:                     $val = 0;
 1684:                 }
 1685:                 if ($option eq 'validate') {
 1686:                     my $canvalidate = 0;
 1687:                     if (ref($validations{$item}) eq 'HASH') {
 1688:                         if ($validations{$item}{'default'}) {
 1689:                             $canvalidate = 1;
 1690:                         }
 1691:                     }
 1692:                     next if (!$canvalidate);
 1693:                 }
 1694:                 my $checked = '';
 1695:                 if ($option eq $curroption) {
 1696:                     $checked = ' checked="checked"';
 1697:                 } elsif ($option eq 'autolimit') {
 1698:                     if ($curroption =~ /^autolimit/) {
 1699:                         $checked = ' checked="checked"';
 1700:                     }
 1701:                 }
 1702:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1703:                                   '<input type="radio" name="crsreq_'.$item.
 1704:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1705:                                   $titles{$option}.'</label>';
 1706:                 if ($option eq 'autolimit') {
 1707:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1708:                                        $item.'_limit_default" size="1" '.
 1709:                                        'value="'.$currlimit.'" />';
 1710:                 }
 1711:                 $defcell{$item} .= '</span> ';
 1712:                 if ($option eq 'autolimit') {
 1713:                     $defcell{$item} .= $titles{'unlimited'};
 1714:                 }
 1715:             }
 1716:         } elsif ($context eq 'requestauthor') {
 1717:             my $curroption;
 1718:             if (ref($settings) eq 'HASH') {
 1719:                 $curroption = $settings->{'default'};
 1720:             }
 1721:             if (!$curroption) {
 1722:                 $curroption = 'norequest';
 1723:             }
 1724:             foreach my $option (@options) {
 1725:                 my $val = $option;
 1726:                 if ($option eq 'norequest') {
 1727:                     $val = 0;
 1728:                 }
 1729:                 my $checked = '';
 1730:                 if ($option eq $curroption) {
 1731:                     $checked = ' checked="checked"';
 1732:                 }
 1733:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1734:                               '<input type="radio" name="authorreq_default"'.
 1735:                               ' value="'.$val.'"'.$checked.' />'.
 1736:                               $titles{$option}.'</label></span>&nbsp; ';
 1737:             }
 1738:         } else {
 1739:             my $checked = 'checked="checked" ';
 1740:             if (ref($settings) eq 'HASH') {
 1741:                 if (ref($settings->{$item}) eq 'HASH') {
 1742:                     if ($settings->{$item}->{'default'} == 0) {
 1743:                         $checked = '';
 1744:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1745:                         $checked = 'checked="checked" ';
 1746:                     }
 1747:                 }
 1748:             }
 1749:             $datatable .= '<span class="LC_nobreak"><label>'.
 1750:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1751:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1752:                           '</label></span>&nbsp; ';
 1753:         }
 1754:     }
 1755:     if ($context eq 'requestcourses') {
 1756:         $datatable .= '</tr><tr>';
 1757:         foreach my $item (@usertools) {
 1758:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1759:         }
 1760:         $datatable .= '</tr></table>';
 1761:     }
 1762:     $datatable .= '</td>';
 1763:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1764:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1765:                       '<input type="text" name="defaultquota" value="'.
 1766:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1767:     }
 1768:     $datatable .= '</tr>';
 1769:     $typecount ++;
 1770:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1771:     $datatable .= '<tr'.$css_class.'>'.
 1772:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1773:     if ($context eq 'requestcourses') {
 1774:         $datatable .= &mt('(overrides affiliation, if set)').
 1775:                       '</td>'.
 1776:                       '<td class="LC_left_item">'.
 1777:                       '<table><tr>';
 1778:     } else {
 1779:         $datatable .= &mt('(overrides affiliation, if checked)').
 1780:                       '</td>'.
 1781:                       '<td class="LC_left_item" colspan="2">'.
 1782:                       '<br />';
 1783:     }
 1784:     my %advcell;
 1785:     foreach my $item (@usertools) {
 1786:         if ($context eq 'requestcourses') {
 1787:             my ($curroption,$currlimit);
 1788:             if (ref($settings) eq 'HASH') {
 1789:                 if (ref($settings->{$item}) eq 'HASH') {
 1790:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1791:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1792:                         $currlimit = $1;
 1793:                     }
 1794:                 }
 1795:             }
 1796:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1797:             my $checked = '';
 1798:             if ($curroption eq '') {
 1799:                 $checked = ' checked="checked"';
 1800:             }
 1801:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1802:                                '<input type="radio" name="crsreq_'.$item.
 1803:                                '__LC_adv" value=""'.$checked.' />'.
 1804:                                &mt('No override set').'</label></span>&nbsp; ';
 1805:             foreach my $option (@options) {
 1806:                 my $val = $option;
 1807:                 if ($option eq 'norequest') {
 1808:                     $val = 0;
 1809:                 }
 1810:                 if ($option eq 'validate') {
 1811:                     my $canvalidate = 0;
 1812:                     if (ref($validations{$item}) eq 'HASH') {
 1813:                         if ($validations{$item}{'_LC_adv'}) {
 1814:                             $canvalidate = 1;
 1815:                         }
 1816:                     }
 1817:                     next if (!$canvalidate);
 1818:                 }
 1819:                 my $checked = '';
 1820:                 if ($val eq $curroption) {
 1821:                     $checked = ' checked="checked"';
 1822:                 } elsif ($option eq 'autolimit') {
 1823:                     if ($curroption =~ /^autolimit/) {
 1824:                         $checked = ' checked="checked"';
 1825:                     }
 1826:                 }
 1827:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1828:                                   '<input type="radio" name="crsreq_'.$item.
 1829:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1830:                                   $titles{$option}.'</label>';
 1831:                 if ($option eq 'autolimit') {
 1832:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1833:                                        $item.'_limit__LC_adv" size="1" '.
 1834:                                        'value="'.$currlimit.'" />';
 1835:                 }
 1836:                 $advcell{$item} .= '</span> ';
 1837:                 if ($option eq 'autolimit') {
 1838:                     $advcell{$item} .= $titles{'unlimited'};
 1839:                 }
 1840:             }
 1841:         } elsif ($context eq 'requestauthor') {
 1842:             my $curroption;
 1843:             if (ref($settings) eq 'HASH') {
 1844:                 $curroption = $settings->{'_LC_adv'};
 1845:             }
 1846:             my $checked = '';
 1847:             if ($curroption eq '') {
 1848:                 $checked = ' checked="checked"';
 1849:             }
 1850:             $datatable .= '<span class="LC_nobreak"><label>'.
 1851:                           '<input type="radio" name="authorreq__LC_adv"'.
 1852:                           ' value=""'.$checked.' />'.
 1853:                           &mt('No override set').'</label></span>&nbsp; ';
 1854:             foreach my $option (@options) {
 1855:                 my $val = $option;
 1856:                 if ($option eq 'norequest') {
 1857:                     $val = 0;
 1858:                 }
 1859:                 my $checked = '';
 1860:                 if ($val eq $curroption) {
 1861:                     $checked = ' checked="checked"';
 1862:                 }
 1863:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1864:                               '<input type="radio" name="authorreq__LC_adv"'.
 1865:                               ' value="'.$val.'"'.$checked.' />'.
 1866:                               $titles{$option}.'</label></span>&nbsp; ';
 1867:             }
 1868:         } else {
 1869:             my $checked = 'checked="checked" ';
 1870:             if (ref($settings) eq 'HASH') {
 1871:                 if (ref($settings->{$item}) eq 'HASH') {
 1872:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1873:                         $checked = '';
 1874:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1875:                         $checked = 'checked="checked" ';
 1876:                     }
 1877:                 }
 1878:             }
 1879:             $datatable .= '<span class="LC_nobreak"><label>'.
 1880:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1881:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1882:                           '</label></span>&nbsp; ';
 1883:         }
 1884:     }
 1885:     if ($context eq 'requestcourses') {
 1886:         $datatable .= '</tr><tr>';
 1887:         foreach my $item (@usertools) {
 1888:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1889:         }
 1890:         $datatable .= '</tr></table>';
 1891:     }
 1892:     $datatable .= '</td></tr>';
 1893:     $$rowtotal += $typecount;
 1894:     return $datatable;
 1895: }
 1896: 
 1897: sub print_requestmail {
 1898:     my ($dom,$action,$settings,$rowtotal) = @_;
 1899:     my ($now,$datatable,%dompersonnel,@domcoord,@currapproval,$rows);
 1900:     $now = time;
 1901:     $rows = 0;
 1902:     %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 1903:     foreach my $server (keys(%dompersonnel)) {
 1904:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 1905:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 1906:             if (!grep(/^$uname:$udom$/,@domcoord)) {
 1907:                 push(@domcoord,$uname.':'.$udom);
 1908:             }
 1909:         }
 1910:     }
 1911:     if (ref($settings) eq 'HASH') {
 1912:         if (ref($settings->{'notify'}) eq 'HASH') {
 1913:             if ($settings->{'notify'}{'approval'} ne '') {
 1914:                @currapproval = split(',',$settings->{'notify'}{'approval'});
 1915:             }
 1916:         }
 1917:     }
 1918:     if (@currapproval) {
 1919:         foreach my $dc (@currapproval) {
 1920:             unless (grep(/^\Q$dc\E$/,@domcoord)) {
 1921:                 push(@domcoord,$dc);
 1922:             }
 1923:         }
 1924:     }
 1925:     @domcoord = sort(@domcoord);
 1926:     my $numinrow = 4;
 1927:     my $numdc = @domcoord;
 1928:     my $css_class = 'class="LC_odd_row"';
 1929:     my $text;
 1930:     if ($action eq 'requestcourses') {
 1931:         $text = &mt('Receive notification of course requests requiring approval');
 1932:     } else {
 1933:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1934:     }
 1935:     $datatable = '<tr '.$css_class.'>'.
 1936:                  ' <td>'.$text.'</td>'.
 1937:                  ' <td class="LC_left_item">';
 1938:     if (@domcoord > 0) {
 1939:         $datatable .= '<table>';
 1940:         for (my $i=0; $i<$numdc; $i++) {
 1941:             my $rem = $i%($numinrow);
 1942:             if ($rem == 0) {
 1943:                 if ($i > 0) {
 1944:                     $datatable .= '</tr>';
 1945:                 }
 1946:                 $datatable .= '<tr>';
 1947:                 $rows ++;
 1948:             }
 1949:             my $check = ' ';
 1950:             if (grep(/^\Q$domcoord[$i]\E$/,@currapproval)) {
 1951:                 $check = ' checked="checked" ';
 1952:             }
 1953:             my ($uname,$udom) = split(':',$domcoord[$i]);
 1954:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
 1955:             if ($i == $numdc-1) {
 1956:                 my $colsleft = $numinrow-$rem;
 1957:                 if ($colsleft > 1) {
 1958:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 1959:                 } else {
 1960:                     $datatable .= '<td class="LC_left_item">';
 1961:                 }
 1962:             } else {
 1963:                 $datatable .= '<td class="LC_left_item">';
 1964:             }
 1965:             $datatable .= '<span class="LC_nobreak"><label>'.
 1966:                           '<input type="checkbox" name="reqapprovalnotify" '.
 1967:                           'value="'.$domcoord[$i].'"'.$check.'/>'.
 1968:                           $fullname.' ('.$uname.':'.$udom.')</label></span></td>';
 1969:         }
 1970:         $datatable .= '</tr></table>';
 1971:     } else {
 1972:         $datatable .= &mt('There are no active Domain Coordinators');
 1973:         $rows ++;
 1974:     }
 1975:     $datatable .='</td></tr>';
 1976:     $$rowtotal += $rows;
 1977:     return $datatable;
 1978: }
 1979: 
 1980: sub print_autoenroll {
 1981:     my ($dom,$settings,$rowtotal) = @_;
 1982:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1983:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1984:     if (ref($settings) eq 'HASH') {
 1985:         if (exists($settings->{'run'})) {
 1986:             if ($settings->{'run'} eq '0') {
 1987:                 $runoff = ' checked="checked" ';
 1988:                 $runon = ' ';
 1989:             } else {
 1990:                 $runon = ' checked="checked" ';
 1991:                 $runoff = ' ';
 1992:             }
 1993:         } else {
 1994:             if ($autorun) {
 1995:                 $runon = ' checked="checked" ';
 1996:                 $runoff = ' ';
 1997:             } else {
 1998:                 $runoff = ' checked="checked" ';
 1999:                 $runon = ' ';
 2000:             }
 2001:         }
 2002:         if (exists($settings->{'co-owners'})) {
 2003:             if ($settings->{'co-owners'} eq '0') {
 2004:                 $coownersoff = ' checked="checked" ';
 2005:                 $coownerson = ' ';
 2006:             } else {
 2007:                 $coownerson = ' checked="checked" ';
 2008:                 $coownersoff = ' ';
 2009:             }
 2010:         } else {
 2011:             $coownersoff = ' checked="checked" ';
 2012:             $coownerson = ' ';
 2013:         }
 2014:         if (exists($settings->{'sender_domain'})) {
 2015:             $defdom = $settings->{'sender_domain'};
 2016:         }
 2017:     } else {
 2018:         if ($autorun) {
 2019:             $runon = ' checked="checked" ';
 2020:             $runoff = ' ';
 2021:         } else {
 2022:             $runoff = ' checked="checked" ';
 2023:             $runon = ' ';
 2024:         }
 2025:     }
 2026:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2027:     my $notif_sender;
 2028:     if (ref($settings) eq 'HASH') {
 2029:         $notif_sender = $settings->{'sender_uname'};
 2030:     }
 2031:     my $datatable='<tr class="LC_odd_row">'.
 2032:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2033:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2034:                   '<input type="radio" name="autoenroll_run"'.
 2035:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2036:                   '<label><input type="radio" name="autoenroll_run"'.
 2037:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2038:                   '</tr><tr>'.
 2039:                   '<td>'.&mt('Notification messages - sender').
 2040:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2041:                   &mt('username').':&nbsp;'.
 2042:                   '<input type="text" name="sender_uname" value="'.
 2043:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2044:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2045:                   '<tr class="LC_odd_row">'.
 2046:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2047:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2048:                   '<input type="radio" name="autoassign_coowners"'.
 2049:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2050:                   '<label><input type="radio" name="autoassign_coowners"'.
 2051:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2052:                   '</tr>';
 2053:     $$rowtotal += 3;
 2054:     return $datatable;
 2055: }
 2056: 
 2057: sub print_autoupdate {
 2058:     my ($position,$dom,$settings,$rowtotal) = @_;
 2059:     my $datatable;
 2060:     if ($position eq 'top') {
 2061:         my $updateon = ' ';
 2062:         my $updateoff = ' checked="checked" ';
 2063:         my $classlistson = ' ';
 2064:         my $classlistsoff = ' checked="checked" ';
 2065:         if (ref($settings) eq 'HASH') {
 2066:             if ($settings->{'run'} eq '1') {
 2067:                 $updateon = $updateoff;
 2068:                 $updateoff = ' ';
 2069:             }
 2070:             if ($settings->{'classlists'} eq '1') {
 2071:                 $classlistson = $classlistsoff;
 2072:                 $classlistsoff = ' ';
 2073:             }
 2074:         }
 2075:         my %title = (
 2076:                    run => 'Auto-update active?',
 2077:                    classlists => 'Update information in classlists?',
 2078:                     );
 2079:         $datatable = '<tr class="LC_odd_row">'. 
 2080:                   '<td>'.&mt($title{'run'}).'</td>'.
 2081:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2082:                   '<input type="radio" name="autoupdate_run"'.
 2083:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2084:                   '<label><input type="radio" name="autoupdate_run"'.
 2085:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2086:                   '</tr><tr>'.
 2087:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2088:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2089:                   '<label><input type="radio" name="classlists"'.
 2090:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2091:                   '<label><input type="radio" name="classlists"'.
 2092:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2093:                   '</tr>';
 2094:         $$rowtotal += 2;
 2095:     } elsif ($position eq 'middle') {
 2096:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2097:         my $numinrow = 3;
 2098:         my $locknamesettings;
 2099:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2100:                                      $dom,$numinrow,$othertitle,
 2101:                                     'lockablenames');
 2102:         $$rowtotal ++;
 2103:     } else {
 2104:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2105:         my @fields = ('lastname','firstname','middlename','generation',
 2106:                       'permanentemail','id');
 2107:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2108:         my $numrows = 0;
 2109:         if (ref($types) eq 'ARRAY') {
 2110:             if (@{$types} > 0) {
 2111:                 $datatable = 
 2112:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2113:                                          \@fields,$types,\$numrows);
 2114:                     $$rowtotal += @{$types}; 
 2115:             }
 2116:         }
 2117:         $datatable .= 
 2118:             &usertype_update_row($settings,{'default' => $othertitle},
 2119:                                  \%fieldtitles,\@fields,['default'],
 2120:                                  \$numrows);
 2121:         $$rowtotal ++;     
 2122:     }
 2123:     return $datatable;
 2124: }
 2125: 
 2126: sub print_autocreate {
 2127:     my ($dom,$settings,$rowtotal) = @_;
 2128:     my (%createon,%createoff);
 2129:     my $curr_dc;
 2130:     my @types = ('xml','req');
 2131:     if (ref($settings) eq 'HASH') {
 2132:         foreach my $item (@types) {
 2133:             $createoff{$item} = ' checked="checked" ';
 2134:             $createon{$item} = ' ';
 2135:             if (exists($settings->{$item})) {
 2136:                 if ($settings->{$item}) {
 2137:                     $createon{$item} = ' checked="checked" ';
 2138:                     $createoff{$item} = ' ';
 2139:                 }
 2140:             }
 2141:         }
 2142:         $curr_dc = $settings->{'xmldc'};
 2143:     } else {
 2144:         foreach my $item (@types) {
 2145:             $createoff{$item} = ' checked="checked" ';
 2146:             $createon{$item} = ' ';
 2147:         }
 2148:     }
 2149:     $$rowtotal += 2;
 2150:     my $datatable='<tr class="LC_odd_row">'.
 2151:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2152:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2153:                   '<input type="radio" name="autocreate_xml"'.
 2154:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2155:                   '<label><input type="radio" name="autocreate_xml"'.
 2156:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2157:                   '</td></tr><tr>'.
 2158:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2159:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2160:                   '<input type="radio" name="autocreate_req"'.
 2161:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2162:                   '<label><input type="radio" name="autocreate_req"'.
 2163:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2164:     my ($numdc,$dctable) = &active_dc_picker($dom,$curr_dc);
 2165:     if ($numdc > 1) {
 2166:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2167:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2168:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2169:         $$rowtotal ++ ;
 2170:     } else {
 2171:         $datatable .= $dctable.'</td></tr>';
 2172:     }
 2173:     return $datatable;
 2174: }
 2175: 
 2176: sub print_directorysrch {
 2177:     my ($dom,$settings,$rowtotal) = @_;
 2178:     my $srchon = ' ';
 2179:     my $srchoff = ' checked="checked" ';
 2180:     my ($exacton,$containson,$beginson);
 2181:     my $localon = ' ';
 2182:     my $localoff = ' checked="checked" ';
 2183:     if (ref($settings) eq 'HASH') {
 2184:         if ($settings->{'available'} eq '1') {
 2185:             $srchon = $srchoff;
 2186:             $srchoff = ' ';
 2187:         }
 2188:         if ($settings->{'localonly'} eq '1') {
 2189:             $localon = $localoff;
 2190:             $localoff = ' ';
 2191:         }
 2192:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2193:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2194:                 if ($type eq 'exact') {
 2195:                     $exacton = ' checked="checked" ';
 2196:                 } elsif ($type eq 'contains') {
 2197:                     $containson = ' checked="checked" ';
 2198:                 } elsif ($type eq 'begins') {
 2199:                     $beginson = ' checked="checked" ';
 2200:                 }
 2201:             }
 2202:         } else {
 2203:             if ($settings->{'searchtypes'} eq 'exact') {
 2204:                 $exacton = ' checked="checked" ';
 2205:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2206:                 $containson = ' checked="checked" ';
 2207:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2208:                 $exacton = ' checked="checked" ';
 2209:                 $containson = ' checked="checked" ';
 2210:             }
 2211:         }
 2212:     }
 2213:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2214:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2215: 
 2216:     my $numinrow = 4;
 2217:     my $cansrchrow = 0;
 2218:     my $datatable='<tr class="LC_odd_row">'.
 2219:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2220:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2221:                   '<input type="radio" name="dirsrch_available"'.
 2222:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2223:                   '<label><input type="radio" name="dirsrch_available"'.
 2224:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2225:                   '</tr><tr>'.
 2226:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2227:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2228:                   '<input type="radio" name="dirsrch_localonly"'.
 2229:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2230:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2231:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2232:                   '</tr>';
 2233:     $$rowtotal += 2;
 2234:     if (ref($usertypes) eq 'HASH') {
 2235:         if (keys(%{$usertypes}) > 0) {
 2236:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2237:                                          $numinrow,$othertitle,'cansearch');
 2238:             $cansrchrow = 1;
 2239:         }
 2240:     }
 2241:     if ($cansrchrow) {
 2242:         $$rowtotal ++;
 2243:         $datatable .= '<tr>';
 2244:     } else {
 2245:         $datatable .= '<tr class="LC_odd_row">';
 2246:     }
 2247:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2248:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2249:     foreach my $title (@{$titleorder}) {
 2250:         if (defined($searchtitles->{$title})) {
 2251:             my $check = ' ';
 2252:             if (ref($settings) eq 'HASH') {
 2253:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2254:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2255:                         $check = ' checked="checked" ';
 2256:                     }
 2257:                 }
 2258:             }
 2259:             $datatable .= '<td class="LC_left_item">'.
 2260:                           '<span class="LC_nobreak"><label>'.
 2261:                           '<input type="checkbox" name="searchby" '.
 2262:                           'value="'.$title.'"'.$check.'/>'.
 2263:                           $searchtitles->{$title}.'</label></span></td>';
 2264:         }
 2265:     }
 2266:     $datatable .= '</tr></table></td></tr>';
 2267:     $$rowtotal ++;
 2268:     if ($cansrchrow) {
 2269:         $datatable .= '<tr class="LC_odd_row">';
 2270:     } else {
 2271:         $datatable .= '<tr>';
 2272:     }
 2273:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2274:                   '<td class="LC_left_item" colspan="2">'.
 2275:                   '<span class="LC_nobreak"><label>'.
 2276:                   '<input type="checkbox" name="searchtypes" '.
 2277:                   $exacton.' value="exact" />'.&mt('Exact match').
 2278:                   '</label>&nbsp;'.
 2279:                   '<label><input type="checkbox" name="searchtypes" '.
 2280:                   $beginson.' value="begins" />'.&mt('Begins with').
 2281:                   '</label>&nbsp;'.
 2282:                   '<label><input type="checkbox" name="searchtypes" '.
 2283:                   $containson.' value="contains" />'.&mt('Contains').
 2284:                   '</label></span></td></tr>';
 2285:     $$rowtotal ++;
 2286:     return $datatable;
 2287: }
 2288: 
 2289: sub print_contacts {
 2290:     my ($dom,$settings,$rowtotal) = @_;
 2291:     my $datatable;
 2292:     my @contacts = ('adminemail','supportemail');
 2293:     my (%checked,%to,%otheremails,%bccemails);
 2294:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2295:                     'requestsmail');
 2296:     foreach my $type (@mailings) {
 2297:         $otheremails{$type} = '';
 2298:     }
 2299:     $bccemails{'helpdeskmail'} = '';
 2300:     if (ref($settings) eq 'HASH') {
 2301:         foreach my $item (@contacts) {
 2302:             if (exists($settings->{$item})) {
 2303:                 $to{$item} = $settings->{$item};
 2304:             }
 2305:         }
 2306:         foreach my $type (@mailings) {
 2307:             if (exists($settings->{$type})) {
 2308:                 if (ref($settings->{$type}) eq 'HASH') {
 2309:                     foreach my $item (@contacts) {
 2310:                         if ($settings->{$type}{$item}) {
 2311:                             $checked{$type}{$item} = ' checked="checked" ';
 2312:                         }
 2313:                     }
 2314:                     $otheremails{$type} = $settings->{$type}{'others'};
 2315:                     if ($type eq 'helpdeskmail') {
 2316:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2317:                     }
 2318:                 }
 2319:             } elsif ($type eq 'lonstatusmail') {
 2320:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2321:             }
 2322:         }
 2323:     } else {
 2324:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2325:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2326:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2327:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2328:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2329:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2330:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2331:     }
 2332:     my ($titles,$short_titles) = &contact_titles();
 2333:     my $rownum = 0;
 2334:     my $css_class;
 2335:     foreach my $item (@contacts) {
 2336:         $rownum ++;
 2337:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2338:         $datatable .= '<tr'.$css_class.'>'. 
 2339:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2340:                   '</span></td><td class="LC_right_item">'.
 2341:                   '<input type="text" name="'.$item.'" value="'.
 2342:                   $to{$item}.'" /></td></tr>';
 2343:     }
 2344:     foreach my $type (@mailings) {
 2345:         $rownum ++;
 2346:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2347:         $datatable .= '<tr'.$css_class.'>'.
 2348:                       '<td><span class="LC_nobreak">'.
 2349:                       $titles->{$type}.': </span></td>'.
 2350:                       '<td class="LC_left_item">'.
 2351:                       '<span class="LC_nobreak">';
 2352:         foreach my $item (@contacts) {
 2353:             $datatable .= '<label>'.
 2354:                           '<input type="checkbox" name="'.$type.'"'.
 2355:                           $checked{$type}{$item}.
 2356:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2357:                           '</label>&nbsp;';
 2358:         }
 2359:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2360:                       '<input type="text" name="'.$type.'_others" '.
 2361:                       'value="'.$otheremails{$type}.'"  />';
 2362:         if ($type eq 'helpdeskmail') {
 2363:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2364:                           '<input type="text" name="'.$type.'_bcc" '.
 2365:                           'value="'.$bccemails{$type}.'"  />';
 2366:         }
 2367:         $datatable .= '</td></tr>'."\n";
 2368:     }
 2369:     $$rowtotal += $rownum;
 2370:     return $datatable;
 2371: }
 2372: 
 2373: sub print_helpsettings {
 2374:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2375:     my ($datatable,$itemcount);
 2376:     $itemcount = 1;
 2377:     my (%choices,%defaultchecked,@toggles);
 2378:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2379:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2380:                                  &mt('LON-CAPA bug tracker'),600,500));
 2381:     %defaultchecked = ('submitbugs' => 'on');
 2382:     @toggles = ('submitbugs',);
 2383: 
 2384:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2385:                                                  \%choices,$itemcount);
 2386:     return $datatable;
 2387: }
 2388: 
 2389: sub radiobutton_prefs {
 2390:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount) = @_;
 2391:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2392:                    (ref($choices) eq 'HASH'));
 2393: 
 2394:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2395: 
 2396:     foreach my $item (@{$toggles}) {
 2397:         if ($defaultchecked->{$item} eq 'on') {
 2398:             $checkedon{$item} = ' checked="checked" ';
 2399:             $checkedoff{$item} = ' ';
 2400:         } elsif ($defaultchecked->{$item} eq 'off') {
 2401:             $checkedoff{$item} = ' checked="checked" ';
 2402:             $checkedon{$item} = ' ';
 2403:         }
 2404:     }
 2405:     if (ref($settings) eq 'HASH') {
 2406:         foreach my $item (@{$toggles}) {
 2407:             if ($settings->{$item} eq '1') {
 2408:                 $checkedon{$item} =  ' checked="checked" ';
 2409:                 $checkedoff{$item} = ' ';
 2410:             } elsif ($settings->{$item} eq '0') {
 2411:                 $checkedoff{$item} =  ' checked="checked" ';
 2412:                 $checkedon{$item} = ' ';
 2413:             }
 2414:         }
 2415:     }
 2416:     foreach my $item (@{$toggles}) {
 2417:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2418:         $datatable .=
 2419:             '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices->{$item}.
 2420:             '</span></td>'.
 2421:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2422:             '<label><input type="radio" name="'.
 2423:             $item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').
 2424:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2425:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2426:             '</span></td>'.
 2427:             '</tr>';
 2428:         $itemcount ++;
 2429:     }
 2430:     return ($datatable,$itemcount);
 2431: }
 2432: 
 2433: sub print_coursedefaults {
 2434:     my ($position,$dom,$settings,$rowtotal) = @_;
 2435:     my ($css_class,$datatable);
 2436:     my $itemcount = 1;
 2437:     if ($position eq 'top') {
 2438:         my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2439:         %choices =
 2440:             &Apache::lonlocal::texthash (
 2441:                 canuse_pdfforms => 'Course/Community users can create/upload PDF forms',
 2442:         );
 2443:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2444:         @toggles = ('canuse_pdfforms',);
 2445:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2446:                                                  \%choices,$itemcount);
 2447:         $$rowtotal += $itemcount;
 2448:     } else {
 2449:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2450:         my %choices =
 2451:             &Apache::lonlocal::texthash (
 2452:                 anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2453:         );
 2454:         my $currdefresponder;
 2455:         if (ref($settings) eq 'HASH') {
 2456:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2457:         }
 2458:         if (!$currdefresponder) {
 2459:             $currdefresponder = 10;
 2460:         } elsif ($currdefresponder < 1) {
 2461:             $currdefresponder = 1;
 2462:         }
 2463:         $datatable .=
 2464:                '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices{'anonsurvey_threshold'}.
 2465:                 '</span></td>'.
 2466:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2467:                 '<input type="text" name="anonsurvey_threshold"'.
 2468:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2469:                 '</td></tr>';
 2470:     }
 2471:     return $datatable;
 2472: }
 2473: 
 2474: sub print_usersessions {
 2475:     my ($position,$dom,$settings,$rowtotal) = @_;
 2476:     my ($css_class,$datatable,%checked,%choices);
 2477:     my (%by_ip,%by_location,@intdoms);
 2478:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2479: 
 2480:     my @alldoms = &Apache::lonnet::all_domains();
 2481:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2482:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2483:     my %altids = &id_for_thisdom(%servers);
 2484:     my $itemcount = 1;
 2485:     if ($position eq 'top') {
 2486:         if (keys(%serverhomes) > 1) {
 2487:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2488:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2489:         } else {
 2490:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2491:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2492:         }
 2493:     } else {
 2494:         if (keys(%by_location) == 0) {
 2495:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2496:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2497:         } else {
 2498:             my %lt = &usersession_titles();
 2499:             my $numinrow = 5;
 2500:             my $prefix;
 2501:             my @types;
 2502:             if ($position eq 'bottom') {
 2503:                 $prefix = 'remote';
 2504:                 @types = ('version','excludedomain','includedomain');
 2505:             } else {
 2506:                 $prefix = 'hosted';
 2507:                 @types = ('excludedomain','includedomain');
 2508:             }
 2509:             my (%current,%checkedon,%checkedoff);
 2510:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2511:             my @locations = sort(keys(%by_location));
 2512:             foreach my $type (@types) {
 2513:                 $checkedon{$type} = '';
 2514:                 $checkedoff{$type} = ' checked="checked"';
 2515:             }
 2516:             if (ref($settings) eq 'HASH') {
 2517:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2518:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2519:                         $current{$key} = $settings->{$prefix}{$key};
 2520:                         if ($key eq 'version') {
 2521:                             if ($current{$key} ne '') {
 2522:                                 $checkedon{$key} = ' checked="checked"';
 2523:                                 $checkedoff{$key} = '';
 2524:                             }
 2525:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2526:                             $checkedon{$key} = ' checked="checked"';
 2527:                             $checkedoff{$key} = '';
 2528:                         }
 2529:                     }
 2530:                 }
 2531:             }
 2532:             foreach my $type (@types) {
 2533:                 next if ($type ne 'version' && !@locations);
 2534:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2535:                 $datatable .= '<tr'.$css_class.'>
 2536:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2537:                                <span class="LC_nobreak">&nbsp;
 2538:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2539:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2540:                 if ($type eq 'version') {
 2541:                     my $selector = '<select name="'.$prefix.'_version">';
 2542:                     foreach my $version (@lcversions) {
 2543:                         my $selected = '';
 2544:                         if ($current{'version'} eq $version) {
 2545:                             $selected = ' selected="selected"';
 2546:                         }
 2547:                         $selector .= ' <option value="'.$version.'"'.
 2548:                                      $selected.'>'.$version.'</option>';
 2549:                     }
 2550:                     $selector .= '</select> ';
 2551:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2552:                 } else {
 2553:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2554:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2555:                                  ' />'.('&nbsp;'x2).
 2556:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2557:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2558:                                  "\n".
 2559:                                  '</div><div><table>';
 2560:                     my $rem;
 2561:                     for (my $i=0; $i<@locations; $i++) {
 2562:                         my ($showloc,$value,$checkedtype);
 2563:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2564:                             my $ip = $by_location{$locations[$i]}->[0];
 2565:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2566:                                  $value = join(':',@{$by_ip{$ip}});
 2567:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2568:                                 if (ref($current{$type}) eq 'ARRAY') {
 2569:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2570:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2571:                                             $checkedtype = ' checked="checked"';
 2572:                                             last;
 2573:                                         }
 2574:                                     }
 2575:                                 }
 2576:                             }
 2577:                         }
 2578:                         $rem = $i%($numinrow);
 2579:                         if ($rem == 0) {
 2580:                             if ($i > 0) {
 2581:                                 $datatable .= '</tr>';
 2582:                             }
 2583:                             $datatable .= '<tr>';
 2584:                         }
 2585:                         $datatable .= '<td class="LC_left_item">'.
 2586:                                       '<span class="LC_nobreak"><label>'.
 2587:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2588:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2589:                                       '</label></span></td>';
 2590:                     }
 2591:                     $rem = @locations%($numinrow);
 2592:                     my $colsleft = $numinrow - $rem;
 2593:                     if ($colsleft > 1 ) {
 2594:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2595:                                       '&nbsp;</td>';
 2596:                     } elsif ($colsleft == 1) {
 2597:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2598:                     }
 2599:                     $datatable .= '</tr></table>';
 2600:                 }
 2601:                 $datatable .= '</td></tr>';
 2602:                 $itemcount ++;
 2603:             }
 2604:         }
 2605:     }
 2606:     $$rowtotal += $itemcount;
 2607:     return $datatable;
 2608: }
 2609: 
 2610: sub build_location_hashes {
 2611:     my ($intdoms,$by_ip,$by_location) = @_;
 2612:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2613:                   (ref($by_location) eq 'HASH')); 
 2614:     my %iphost = &Apache::lonnet::get_iphost();
 2615:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2616:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2617:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2618:         foreach my $id (@{$iphost{$primary_ip}}) {
 2619:             my $intdom = &Apache::lonnet::internet_dom($id);
 2620:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2621:                 push(@{$intdoms},$intdom);
 2622:             }
 2623:         }
 2624:     }
 2625:     foreach my $ip (keys(%iphost)) {
 2626:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2627:             foreach my $id (@{$iphost{$ip}}) {
 2628:                 my $location = &Apache::lonnet::internet_dom($id);
 2629:                 if ($location) {
 2630:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2631:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2632:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2633:                             push(@{$by_ip->{$ip}},$location);
 2634:                         }
 2635:                     } else {
 2636:                         $by_ip->{$ip} = [$location];
 2637:                     }
 2638:                 }
 2639:             }
 2640:         }
 2641:     }
 2642:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2643:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2644:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2645:             my $first = $by_ip->{$ip}->[0];
 2646:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2647:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2648:                     push(@{$by_location->{$first}},$ip);
 2649:                 }
 2650:             } else {
 2651:                 $by_location->{$first} = [$ip];
 2652:             }
 2653:         }
 2654:     }
 2655:     return;
 2656: }
 2657: 
 2658: sub current_offloads_to {
 2659:     my ($dom,$settings,$servers) = @_;
 2660:     my (%spareid,%otherdomconfigs);
 2661:     if (ref($servers) eq 'HASH') {
 2662:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2663:             my $gotspares;
 2664:             if (ref($settings) eq 'HASH') {
 2665:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2666:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2667:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2668:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2669:                         $gotspares = 1;
 2670:                     }
 2671:                 }
 2672:             }
 2673:             unless ($gotspares) {
 2674:                 my $gotspares;
 2675:                 my $serverhomeID =
 2676:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2677:                 my $serverhomedom =
 2678:                     &Apache::lonnet::host_domain($serverhomeID);
 2679:                 if ($serverhomedom ne $dom) {
 2680:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2681:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2682:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2683:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2684:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2685:                                 $gotspares = 1;
 2686:                             }
 2687:                         }
 2688:                     } else {
 2689:                         $otherdomconfigs{$serverhomedom} =
 2690:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2691:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2692:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2693:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2694:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2695:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2696:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2697:                                         $gotspares = 1;
 2698:                                     }
 2699:                                 }
 2700:                             }
 2701:                         }
 2702:                     }
 2703:                 }
 2704:             }
 2705:             unless ($gotspares) {
 2706:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2707:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2708:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2709:                } else {
 2710:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2711:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2712:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2713:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2714:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2715:                     } else {
 2716:                         my %what = (
 2717:                              spareid => 1,
 2718:                         );
 2719:                         my ($result,$returnhash) = 
 2720:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2721:                         if ($result eq 'ok') { 
 2722:                             if (ref($returnhash) eq 'HASH') {
 2723:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2724:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2725:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2726:                                 }
 2727:                             }
 2728:                         }
 2729:                     }
 2730:                 }
 2731:             }
 2732:         }
 2733:     }
 2734:     return %spareid;
 2735: }
 2736: 
 2737: sub spares_row {
 2738:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2739:     my $css_class;
 2740:     my $numinrow = 4;
 2741:     my $itemcount = 1;
 2742:     my $datatable;
 2743:     my %typetitles = &sparestype_titles();
 2744:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2745:         foreach my $server (sort(keys(%{$servers}))) {
 2746:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2747:             my ($othercontrol,$serverdom);
 2748:             if ($serverhome ne $server) {
 2749:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2750:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2751:             } else {
 2752:                 $serverdom = &Apache::lonnet::host_domain($server);
 2753:                 if ($serverdom ne $dom) {
 2754:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2755:                 }
 2756:             }
 2757:             next unless (ref($spareid->{$server}) eq 'HASH');
 2758:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2759:             $datatable .= '<tr'.$css_class.'>
 2760:                            <td rowspan="2">
 2761:                             <span class="LC_nobreak">'.
 2762:                           &mt('[_1] when busy, offloads to:'
 2763:                               ,'<b>'.$server.'</b>').
 2764:                           "\n";
 2765:             my (%current,%canselect);
 2766:             my @choices = 
 2767:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2768:             foreach my $type ('primary','default') {
 2769:                 if (ref($spareid->{$server}) eq 'HASH') {
 2770:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2771:                         my @spares = @{$spareid->{$server}{$type}};
 2772:                         if (@spares > 0) {
 2773:                             if ($othercontrol) {
 2774:                                 $current{$type} = join(', ',@spares);
 2775:                             } else {
 2776:                                 $current{$type} .= '<table>';
 2777:                                 my $numspares = scalar(@spares);
 2778:                                 for (my $i=0;  $i<@spares; $i++) {
 2779:                                     my $rem = $i%($numinrow);
 2780:                                     if ($rem == 0) {
 2781:                                         if ($i > 0) {
 2782:                                             $current{$type} .= '</tr>';
 2783:                                         }
 2784:                                         $current{$type} .= '<tr>';
 2785:                                     }
 2786:                                     $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;'.
 2787:                                                        $spareid->{$server}{$type}[$i].
 2788:                                                        '</label></td>'."\n";
 2789:                                 }
 2790:                                 my $rem = @spares%($numinrow);
 2791:                                 my $colsleft = $numinrow - $rem;
 2792:                                 if ($colsleft > 1 ) {
 2793:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2794:                                                        '" class="LC_left_item">'.
 2795:                                                        '&nbsp;</td>';
 2796:                                 } elsif ($colsleft == 1) {
 2797:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2798:                                 }
 2799:                                 $current{$type} .= '</tr></table>';
 2800:                             }
 2801:                         }
 2802:                     }
 2803:                     if ($current{$type} eq '') {
 2804:                         $current{$type} = &mt('None specified');
 2805:                     }
 2806:                     if ($othercontrol) {
 2807:                         if ($type eq 'primary') {
 2808:                             $canselect{$type} = $othercontrol;
 2809:                         }
 2810:                     } else {
 2811:                         $canselect{$type} = 
 2812:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2813:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2814:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2815:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2816:                         if (@choices > 0) {
 2817:                             foreach my $lonhost (@choices) {
 2818:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2819:                             }
 2820:                         }
 2821:                         $canselect{$type} .= '</select>'."\n";
 2822:                     }
 2823:                 } else {
 2824:                     $current{$type} = &mt('Could not be determined');
 2825:                     if ($type eq 'primary') {
 2826:                         $canselect{$type} =  $othercontrol;
 2827:                     }
 2828:                 }
 2829:                 if ($type eq 'default') {
 2830:                     $datatable .= '<tr'.$css_class.'>';
 2831:                 }
 2832:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2833:                               '<td>'.$current{$type}.'</td>'."\n".
 2834:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2835:             }
 2836:             $itemcount ++;
 2837:         }
 2838:     }
 2839:     $$rowtotal += $itemcount;
 2840:     return $datatable;
 2841: }
 2842: 
 2843: sub possible_newspares {
 2844:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2845:     my $serverhostname = &Apache::lonnet::hostname($server);
 2846:     my %excluded;
 2847:     if ($serverhostname ne '') {
 2848:         %excluded = (
 2849:                        $serverhostname => 1,
 2850:                     );
 2851:     }
 2852:     if (ref($currspares) eq 'HASH') {
 2853:         foreach my $type (keys(%{$currspares})) {
 2854:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2855:                 if (@{$currspares->{$type}} > 0) {
 2856:                     foreach my $curr (@{$currspares->{$type}}) {
 2857:                         my $hostname = &Apache::lonnet::hostname($curr);
 2858:                         $excluded{$hostname} = 1;
 2859:                     }
 2860:                 }
 2861:             }
 2862:         }
 2863:     }
 2864:     my @choices;
 2865:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2866:         if (keys(%{$serverhomes}) > 1) {
 2867:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2868:                 unless ($excluded{$name}) {
 2869:                     if (exists($altids->{$serverhomes->{$name}})) {
 2870:                         push(@choices,$altids->{$serverhomes->{$name}});
 2871:                     } else {
 2872:                         push(@choices,$serverhomes->{$name});
 2873:                     }
 2874:                 }
 2875:             }
 2876:         }
 2877:     }
 2878:     return sort(@choices);
 2879: }
 2880: 
 2881: sub print_loadbalancing {
 2882:     my ($dom,$settings,$rowtotal) = @_;
 2883:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2884:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2885:     my $numinrow = 1;
 2886:     my $datatable;
 2887:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2888:     my (%currbalancer,%currtargets,%currrules,%existing);
 2889:     if (ref($settings) eq 'HASH') {
 2890:         %existing = %{$settings};
 2891:     }
 2892:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 2893:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 2894:                                   \%currtargets,\%currrules);
 2895:     } else {
 2896:         return;
 2897:     }
 2898:     my ($othertitle,$usertypes,$types) =
 2899:         &Apache::loncommon::sorted_inst_types($dom);
 2900:     my $rownum = 6;
 2901:     if (ref($types) eq 'ARRAY') {
 2902:         $rownum += scalar(@{$types});
 2903:     }
 2904:     my @css_class = ('LC_odd_row','LC_even_row');
 2905:     my $balnum = 0;
 2906:     my $islast;
 2907:     my (@toshow,$disabledtext);
 2908:     if (keys(%currbalancer) > 0) {
 2909:         @toshow = sort(keys(%currbalancer));
 2910:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 2911:             push(@toshow,'');
 2912:         }
 2913:     } else {
 2914:         @toshow = ('');
 2915:         $disabledtext = &mt('No existing load balancer');
 2916:     }
 2917:     foreach my $lonhost (@toshow) {
 2918:         if ($balnum == scalar(@toshow)-1) {
 2919:             $islast = 1;
 2920:         } else {
 2921:             $islast = 0;
 2922:         }
 2923:         my $cssidx = $balnum%2;
 2924:         my $targets_div_style = 'display: none';
 2925:         my $disabled_div_style = 'display: block';
 2926:         my $homedom_div_style = 'display: none';
 2927:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 2928:                       '<td rowspan="'.$rownum.'" valign="top">'.
 2929:                       '<p>';
 2930:         if ($lonhost eq '') {
 2931:             $datatable .= '<span class="LC_nobreak">';
 2932:             if (keys(%currbalancer) > 0) {
 2933:                 $datatable .= &mt('Add balancer:');
 2934:             } else {
 2935:                 $datatable .= &mt('Enable balancer:');
 2936:             }
 2937:             $datatable .= '&nbsp;'.
 2938:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 2939:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 2940:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 2941:                           '<option value="" selected="selected">'.&mt('None').
 2942:                           '</option>'."\n";
 2943:             foreach my $server (sort(keys(%servers))) {
 2944:                 next if ($currbalancer{$server});
 2945:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 2946:             }
 2947:             $datatable .=
 2948:                 '</select>'."\n".
 2949:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 2950:         } else {
 2951:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 2952:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 2953:                            &mt('Stop balancing').'</label>'.
 2954:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 2955:             $targets_div_style = 'display: block';
 2956:             $disabled_div_style = 'display: none';
 2957:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 2958:                 $homedom_div_style = 'display: block';
 2959:             }
 2960:         }
 2961:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 2962:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 2963:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 2964:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2965:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 2966:         my @sparestypes = ('primary','default');
 2967:         my %typetitles = &sparestype_titles();
 2968:         foreach my $sparetype (@sparestypes) {
 2969:             my $targettable;
 2970:             for (my $i=0; $i<$numspares; $i++) {
 2971:                 my $checked;
 2972:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 2973:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 2974:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 2975:                             $checked = ' checked="checked"';
 2976:                         }
 2977:                     }
 2978:                 }
 2979:                 my ($chkboxval,$disabled);
 2980:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 2981:                     $chkboxval = $spares[$i];
 2982:                 }
 2983:                 if (exists($currbalancer{$spares[$i]})) {
 2984:                     $disabled = ' disabled="disabled"';
 2985:                 }
 2986:                 $targettable .=
 2987:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 2988:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 2989:                     '</span></label></td>';
 2990:                 my $rem = $i%($numinrow);
 2991:                 if ($rem == 0) {
 2992:                     if (($i > 0) && ($i < $numspares-1)) {
 2993:                         $targettable .= '</tr>';
 2994:                     }
 2995:                     if ($i < $numspares-1) {
 2996:                         $targettable .= '<tr>';
 2997:                     }
 2998:                 }
 2999:             }
 3000:             if ($targettable ne '') {
 3001:                 my $rem = $numspares%($numinrow);
 3002:                 my $colsleft = $numinrow - $rem;
 3003:                 if ($colsleft > 1 ) {
 3004:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3005:                                     '&nbsp;</td>';
 3006:                 } elsif ($colsleft == 1) {
 3007:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3008:                 }
 3009:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3010:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3011:             }
 3012:         }
 3013:         $datatable .= '</div></td></tr>'.
 3014:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3015:                                            $othertitle,$usertypes,$types,\%servers,
 3016:                                            \%currbalancer,$lonhost,
 3017:                                            $targets_div_style,$homedom_div_style,
 3018:                                            $css_class[$cssidx],$balnum,$islast);
 3019:         $$rowtotal += $rownum;
 3020:         $balnum ++;
 3021:     }
 3022:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3023:     return $datatable;
 3024: }
 3025: 
 3026: sub get_loadbalancers_config {
 3027:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3028:     return unless ((ref($servers) eq 'HASH') &&
 3029:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3030:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3031:     if (keys(%{$existing}) > 0) {
 3032:         my $oldlonhost;
 3033:         foreach my $key (sort(keys(%{$existing}))) {
 3034:             if ($key eq 'lonhost') {
 3035:                 $oldlonhost = $existing->{'lonhost'};
 3036:                 $currbalancer->{$oldlonhost} = 1;
 3037:             } elsif ($key eq 'targets') {
 3038:                 if ($oldlonhost) {
 3039:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3040:                 }
 3041:             } elsif ($key eq 'rules') {
 3042:                 if ($oldlonhost) {
 3043:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3044:                 }
 3045:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3046:                 $currbalancer->{$key} = 1;
 3047:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3048:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3049:             }
 3050:         }
 3051:     } else {
 3052:         my ($balancerref,$targetsref) =
 3053:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3054:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3055:             foreach my $server (sort(keys(%{$balancerref}))) {
 3056:                 $currbalancer->{$server} = 1;
 3057:                 $currtargets->{$server} = $targetsref->{$server};
 3058:             }
 3059:         }
 3060:     }
 3061:     return;
 3062: }
 3063: 
 3064: sub loadbalancing_rules {
 3065:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3066:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3067:         $css_class,$balnum,$islast) = @_;
 3068:     my $output;
 3069:     my $num = 0;
 3070:     my ($alltypes,$othertypes,$titles) =
 3071:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3072:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3073:         foreach my $type (@{$alltypes}) {
 3074:             $num ++;
 3075:             my $current;
 3076:             if (ref($currrules) eq 'HASH') {
 3077:                 $current = $currrules->{$type};
 3078:             }
 3079:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3080:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3081:                     $current = '';
 3082:                 }
 3083:             }
 3084:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3085:                                              $servers,$currbalancer,$lonhost,$dom,
 3086:                                              $targets_div_style,$homedom_div_style,
 3087:                                              $css_class,$balnum,$num,$islast);
 3088:         }
 3089:     }
 3090:     return $output;
 3091: }
 3092: 
 3093: sub loadbalancing_titles {
 3094:     my ($dom,$intdom,$usertypes,$types) = @_;
 3095:     my %othertypes = (
 3096:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3097:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3098:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3099:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3100:                      );
 3101:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 3102:     if (ref($types) eq 'ARRAY') {
 3103:         unshift(@alltypes,@{$types},'default');
 3104:     }
 3105:     my %titles;
 3106:     foreach my $type (@alltypes) {
 3107:         if ($type =~ /^_LC_/) {
 3108:             $titles{$type} = $othertypes{$type};
 3109:         } elsif ($type eq 'default') {
 3110:             $titles{$type} = &mt('All users from [_1]',$dom);
 3111:             if (ref($types) eq 'ARRAY') {
 3112:                 if (@{$types} > 0) {
 3113:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3114:                 }
 3115:             }
 3116:         } elsif (ref($usertypes) eq 'HASH') {
 3117:             $titles{$type} = $usertypes->{$type};
 3118:         }
 3119:     }
 3120:     return (\@alltypes,\%othertypes,\%titles);
 3121: }
 3122: 
 3123: sub loadbalance_rule_row {
 3124:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3125:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3126:     my @rulenames = ('default','homeserver');
 3127:     my %ruletitles = &offloadtype_text();
 3128:     if ($type eq '_LC_external') {
 3129:         push(@rulenames,'externalbalancer');
 3130:     } else {
 3131:         push(@rulenames,'specific');
 3132:     }
 3133:     push(@rulenames,'none');
 3134:     my $style = $targets_div_style;
 3135:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3136:         $style = $homedom_div_style;
 3137:     }
 3138:     my $space;
 3139:     if ($islast && $num == 1) {
 3140:         $space = '<div display="inline-block">&nbsp;</div>';
 3141:     }
 3142:     my $output =
 3143:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3144:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3145:         '<td valaign="top">'.$space.
 3146:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3147:     for (my $i=0; $i<@rulenames; $i++) {
 3148:         my $rule = $rulenames[$i];
 3149:         my ($checked,$extra);
 3150:         if ($rulenames[$i] eq 'default') {
 3151:             $rule = '';
 3152:         }
 3153:         if ($rulenames[$i] eq 'specific') {
 3154:             if (ref($servers) eq 'HASH') {
 3155:                 my $default;
 3156:                 if (($current ne '') && (exists($servers->{$current}))) {
 3157:                     $checked = ' checked="checked"';
 3158:                 }
 3159:                 unless ($checked) {
 3160:                     $default = ' selected="selected"';
 3161:                 }
 3162:                 $extra =
 3163:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3164:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3165:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3166:                     '<option value=""'.$default.'></option>'."\n";
 3167:                 foreach my $server (sort(keys(%{$servers}))) {
 3168:                     if (ref($currbalancer) eq 'HASH') {
 3169:                         next if (exists($currbalancer->{$server}));
 3170:                     }
 3171:                     my $selected;
 3172:                     if ($server eq $current) {
 3173:                         $selected = ' selected="selected"';
 3174:                     }
 3175:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3176:                 }
 3177:                 $extra .= '</select>';
 3178:             }
 3179:         } elsif ($rule eq $current) {
 3180:             $checked = ' checked="checked"';
 3181:         }
 3182:         $output .= '<span class="LC_nobreak"><label>'.
 3183:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3184:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3185:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3186:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3187:                    '</label>'.$extra.'</span><br />'."\n";
 3188:     }
 3189:     $output .= '</div></td></tr>'."\n";
 3190:     return $output;
 3191: }
 3192: 
 3193: sub offloadtype_text {
 3194:     my %ruletitles = &Apache::lonlocal::texthash (
 3195:            'default'          => 'Offloads to default destinations',
 3196:            'homeserver'       => "Offloads to user's home server",
 3197:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3198:            'specific'         => 'Offloads to specific server',
 3199:            'none'             => 'No offload',
 3200:     );
 3201:     return %ruletitles;
 3202: }
 3203: 
 3204: sub sparestype_titles {
 3205:     my %typestitles = &Apache::lonlocal::texthash (
 3206:                           'primary' => 'primary',
 3207:                           'default' => 'default',
 3208:                       );
 3209:     return %typestitles;
 3210: }
 3211: 
 3212: sub contact_titles {
 3213:     my %titles = &Apache::lonlocal::texthash (
 3214:                    'supportemail' => 'Support E-mail address',
 3215:                    'adminemail'   => 'Default Server Admin E-mail address',
 3216:                    'errormail'    => 'Error reports to be e-mailed to',
 3217:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3218:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3219:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3220:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3221:                  );
 3222:     my %short_titles = &Apache::lonlocal::texthash (
 3223:                            adminemail   => 'Admin E-mail address',
 3224:                            supportemail => 'Support E-mail',
 3225:                        );   
 3226:     return (\%titles,\%short_titles);
 3227: }
 3228: 
 3229: sub tool_titles {
 3230:     my %titles = &Apache::lonlocal::texthash (
 3231:                      aboutme    => 'Personal web page',
 3232:                      blog       => 'Blog',
 3233:                      webdav     => 'WebDAV',
 3234:                      portfolio  => 'Portfolio',
 3235:                      official   => 'Official courses (with institutional codes)',
 3236:                      unofficial => 'Unofficial courses',
 3237:                      community  => 'Communities',
 3238:                  );
 3239:     return %titles;
 3240: }
 3241: 
 3242: sub courserequest_titles {
 3243:     my %titles = &Apache::lonlocal::texthash (
 3244:                                    official   => 'Official',
 3245:                                    unofficial => 'Unofficial',
 3246:                                    community  => 'Communities',
 3247:                                    norequest  => 'Not allowed',
 3248:                                    approval   => 'Approval by Dom. Coord.',
 3249:                                    validate   => 'With validation',
 3250:                                    autolimit  => 'Numerical limit',
 3251:                                    unlimited  => '(blank for unlimited)',
 3252:                  );
 3253:     return %titles;
 3254: }
 3255: 
 3256: sub authorrequest_titles {
 3257:     my %titles = &Apache::lonlocal::texthash (
 3258:                                    norequest  => 'Not allowed',
 3259:                                    approval   => 'Approval by Dom. Coord.',
 3260:                                    automatic  => 'Automatic approval',
 3261:                  );
 3262:     return %titles;
 3263: }
 3264: 
 3265: sub courserequest_conditions {
 3266:     my %conditions = &Apache::lonlocal::texthash (
 3267:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3268:        validate   => '(Processing of request subject to instittutional validation).',
 3269:                  );
 3270:     return %conditions;
 3271: }
 3272: 
 3273: 
 3274: sub print_usercreation {
 3275:     my ($position,$dom,$settings,$rowtotal) = @_;
 3276:     my $numinrow = 4;
 3277:     my $datatable;
 3278:     if ($position eq 'top') {
 3279:         $$rowtotal ++;
 3280:         my $rowcount = 0;
 3281:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3282:         if (ref($rules) eq 'HASH') {
 3283:             if (keys(%{$rules}) > 0) {
 3284:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3285:                                                 $ruleorder,$numinrow,$rowcount);
 3286:                 $$rowtotal ++;
 3287:                 $rowcount ++;
 3288:             }
 3289:         }
 3290:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3291:         if (ref($idrules) eq 'HASH') {
 3292:             if (keys(%{$idrules}) > 0) {
 3293:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3294:                                                 $idruleorder,$numinrow,$rowcount);
 3295:                 $$rowtotal ++;
 3296:                 $rowcount ++;
 3297:             }
 3298:         }
 3299:         my ($emailrules,$emailruleorder) = 
 3300:             &Apache::lonnet::inst_userrules($dom,'email');
 3301:         if (ref($emailrules) eq 'HASH') {
 3302:             if (keys(%{$emailrules}) > 0) {
 3303:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3304:                                                 $emailruleorder,$numinrow,$rowcount);
 3305:                 $$rowtotal ++;
 3306:                 $rowcount ++;
 3307:             }
 3308:         }
 3309:         if ($rowcount == 0) {
 3310:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3311:             $$rowtotal ++;
 3312:             $rowcount ++;
 3313:         }
 3314:     } elsif ($position eq 'middle') {
 3315:         my @creators = ('author','course','requestcrs','selfcreate');
 3316:         my ($rules,$ruleorder) =
 3317:             &Apache::lonnet::inst_userrules($dom,'username');
 3318:         my %lt = &usercreation_types();
 3319:         my %checked;
 3320:         my @selfcreate; 
 3321:         if (ref($settings) eq 'HASH') {
 3322:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3323:                 foreach my $item (@creators) {
 3324:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3325:                 }
 3326:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3327:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3328:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3329:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3330:                         @selfcreate = ('email','login','sso');
 3331:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3332:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3333:                     }
 3334:                 }
 3335:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3336:                 foreach my $item (@creators) {
 3337:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3338:                         $checked{$item} = 'none';
 3339:                     }
 3340:                 }
 3341:             }
 3342:         }
 3343:         my $rownum = 0;
 3344:         foreach my $item (@creators) {
 3345:             $rownum ++;
 3346:             if ($item ne 'selfcreate') {  
 3347:                 if ($checked{$item} eq '') {
 3348:                     $checked{$item} = 'any';
 3349:                 }
 3350:             }
 3351:             my $css_class;
 3352:             if ($rownum%2) {
 3353:                 $css_class = '';
 3354:             } else {
 3355:                 $css_class = ' class="LC_odd_row" ';
 3356:             }
 3357:             $datatable .= '<tr'.$css_class.'>'.
 3358:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3359:                          '</span></td><td align="right">';
 3360:             my @options;
 3361:             if ($item eq 'selfcreate') {
 3362:                 push(@options,('email','login','sso'));
 3363:             } else {
 3364:                 @options = ('any');
 3365:                 if (ref($rules) eq 'HASH') {
 3366:                     if (keys(%{$rules}) > 0) {
 3367:                         push(@options,('official','unofficial'));
 3368:                     }
 3369:                 }
 3370:                 push(@options,'none');
 3371:             }
 3372:             foreach my $option (@options) {
 3373:                 my $type = 'radio';
 3374:                 my $check = ' ';
 3375:                 if ($item eq 'selfcreate') {
 3376:                     $type = 'checkbox';
 3377:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3378:                         $check = ' checked="checked" ';
 3379:                     }
 3380:                 } else {
 3381:                     if ($checked{$item} eq $option) {
 3382:                         $check = ' checked="checked" ';
 3383:                     }
 3384:                 } 
 3385:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3386:                               '<input type="'.$type.'" name="can_createuser_'.
 3387:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3388:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3389:             }
 3390:             $datatable .= '</td></tr>';
 3391:         }
 3392:         my ($othertitle,$usertypes,$types) =
 3393:             &Apache::loncommon::sorted_inst_types($dom);
 3394:         my $createsettings;
 3395:         if (ref($settings) eq 'HASH') {
 3396:             $createsettings = $settings->{cancreate};
 3397:         }
 3398:         if (ref($usertypes) eq 'HASH') {
 3399:             if (keys(%{$usertypes}) > 0) {
 3400:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3401:                                              $dom,$numinrow,$othertitle,
 3402:                                              'statustocreate');
 3403:                 $$rowtotal ++;
 3404:                 $rownum ++;
 3405:             }
 3406:         }
 3407:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3408:     } else {
 3409:         my @contexts = ('author','course','domain');
 3410:         my @authtypes = ('int','krb4','krb5','loc');
 3411:         my %checked;
 3412:         if (ref($settings) eq 'HASH') {
 3413:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3414:                 foreach my $item (@contexts) {
 3415:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3416:                         foreach my $auth (@authtypes) {
 3417:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3418:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3419:                             }
 3420:                         }
 3421:                     }
 3422:                 }
 3423:             }
 3424:         } else {
 3425:             foreach my $item (@contexts) {
 3426:                 foreach my $auth (@authtypes) {
 3427:                     $checked{$item}{$auth} = ' checked="checked" ';
 3428:                 }
 3429:             }
 3430:         }
 3431:         my %title = &context_names();
 3432:         my %authname = &authtype_names();
 3433:         my $rownum = 0;
 3434:         my $css_class; 
 3435:         foreach my $item (@contexts) {
 3436:             if ($rownum%2) {
 3437:                 $css_class = '';
 3438:             } else {
 3439:                 $css_class = ' class="LC_odd_row" ';
 3440:             }
 3441:             $datatable .=   '<tr'.$css_class.'>'.
 3442:                             '<td>'.$title{$item}.
 3443:                             '</td><td class="LC_left_item">'.
 3444:                             '<span class="LC_nobreak">';
 3445:             foreach my $auth (@authtypes) {
 3446:                 $datatable .= '<label>'. 
 3447:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3448:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3449:                               $authname{$auth}.'</label>&nbsp;';
 3450:             }
 3451:             $datatable .= '</span></td></tr>';
 3452:             $rownum ++;
 3453:         }
 3454:         $$rowtotal += $rownum;
 3455:     }
 3456:     return $datatable;
 3457: }
 3458: 
 3459: sub captcha_choice {
 3460:     my ($context,$settings,$itemcount) = @_;
 3461:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3462:     my %lt = &captcha_phrases();
 3463:     $keyentry = 'hidden';
 3464:     if ($context eq 'cancreate') {
 3465:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3466:     } elsif ($context eq 'login') {
 3467:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3468:     }
 3469:     if (ref($settings) eq 'HASH') {
 3470:         if ($settings->{'captcha'}) {
 3471:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3472:         } else {
 3473:             $checked{'original'} = ' checked="checked"';
 3474:         }
 3475:         if ($settings->{'captcha'} eq 'recaptcha') {
 3476:             $pubtext = $lt{'pub'};
 3477:             $privtext = $lt{'priv'};
 3478:             $keyentry = 'text';
 3479:         }
 3480:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3481:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3482:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3483:         }
 3484:     } else {
 3485:         $checked{'original'} = ' checked="checked"';
 3486:     }
 3487:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3488:     my $output = '<tr'.$css_class.'>'.
 3489:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3490:                  '<table><tr><td>'."\n";
 3491:     foreach my $option ('original','recaptcha','notused') {
 3492:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3493:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3494:                    $lt{$option}.'</label></span>';
 3495:         unless ($option eq 'notused') {
 3496:             $output .= ('&nbsp;'x2)."\n";
 3497:         }
 3498:     }
 3499: #
 3500: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3501: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3502: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 3503: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3504: #
 3505:     $output .= '</td></tr>'."\n".
 3506:                '<tr><td>'."\n".
 3507:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3508:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3509:                $currpub.'" size="40" /></span><br />'."\n".
 3510:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3511:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3512:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3513:                '</td></tr>';
 3514:     return $output;
 3515: }
 3516: 
 3517: sub user_formats_row {
 3518:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3519:     my $output;
 3520:     my %text = (
 3521:                    'username' => 'new usernames',
 3522:                    'id'       => 'IDs',
 3523:                    'email'    => 'self-created accounts (e-mail)',
 3524:                );
 3525:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3526:     $output = '<tr '.$css_class.'>'.
 3527:               '<td><span class="LC_nobreak">';
 3528:     if ($type eq 'email') {
 3529:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3530:     } else {
 3531:         $output .= &mt("Format rules to check for $text{$type}: ");
 3532:     }
 3533:     $output .= '</span></td>'.
 3534:                '<td class="LC_left_item" colspan="2"><table>';
 3535:     my $rem;
 3536:     if (ref($ruleorder) eq 'ARRAY') {
 3537:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3538:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3539:                 my $rem = $i%($numinrow);
 3540:                 if ($rem == 0) {
 3541:                     if ($i > 0) {
 3542:                         $output .= '</tr>';
 3543:                     }
 3544:                     $output .= '<tr>';
 3545:                 }
 3546:                 my $check = ' ';
 3547:                 if (ref($settings) eq 'HASH') {
 3548:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3549:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3550:                             $check = ' checked="checked" ';
 3551:                         }
 3552:                     }
 3553:                 }
 3554:                 $output .= '<td class="LC_left_item">'.
 3555:                            '<span class="LC_nobreak"><label>'.
 3556:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3557:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3558:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3559:             }
 3560:         }
 3561:         $rem = @{$ruleorder}%($numinrow);
 3562:     }
 3563:     my $colsleft = $numinrow - $rem;
 3564:     if ($colsleft > 1 ) {
 3565:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3566:                    '&nbsp;</td>';
 3567:     } elsif ($colsleft == 1) {
 3568:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3569:     }
 3570:     $output .= '</tr></table></td></tr>';
 3571:     return $output;
 3572: }
 3573: 
 3574: sub usercreation_types {
 3575:     my %lt = &Apache::lonlocal::texthash (
 3576:                     author     => 'When adding a co-author',
 3577:                     course     => 'When adding a user to a course',
 3578:                     requestcrs => 'When requesting a course',
 3579:                     selfcreate => 'User creates own account', 
 3580:                     any        => 'Any',
 3581:                     official   => 'Institutional only ',
 3582:                     unofficial => 'Non-institutional only',
 3583:                     email      => 'E-mail address',
 3584:                     login      => 'Institutional Login',
 3585:                     sso        => 'SSO', 
 3586:                     none       => 'None',
 3587:     );
 3588:     return %lt;
 3589: }
 3590: 
 3591: sub authtype_names {
 3592:     my %lt = &Apache::lonlocal::texthash(
 3593:                       int    => 'Internal',
 3594:                       krb4   => 'Kerberos 4',
 3595:                       krb5   => 'Kerberos 5',
 3596:                       loc    => 'Local',
 3597:                   );
 3598:     return %lt;
 3599: }
 3600: 
 3601: sub context_names {
 3602:     my %context_title = &Apache::lonlocal::texthash(
 3603:        author => 'Creating users when an Author',
 3604:        course => 'Creating users when in a course',
 3605:        domain => 'Creating users when a Domain Coordinator',
 3606:     );
 3607:     return %context_title;
 3608: }
 3609: 
 3610: sub print_usermodification {
 3611:     my ($position,$dom,$settings,$rowtotal) = @_;
 3612:     my $numinrow = 4;
 3613:     my ($context,$datatable,$rowcount);
 3614:     if ($position eq 'top') {
 3615:         $rowcount = 0;
 3616:         $context = 'author'; 
 3617:         foreach my $role ('ca','aa') {
 3618:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3619:                                                    $numinrow,$rowcount);
 3620:             $$rowtotal ++;
 3621:             $rowcount ++;
 3622:         }
 3623:     } elsif ($position eq 'middle') {
 3624:         $context = 'course';
 3625:         $rowcount = 0;
 3626:         foreach my $role ('st','ep','ta','in','cr') {
 3627:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3628:                                                    $numinrow,$rowcount);
 3629:             $$rowtotal ++;
 3630:             $rowcount ++;
 3631:         }
 3632:     } elsif ($position eq 'bottom') {
 3633:         $context = 'selfcreate';
 3634:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3635:         $usertypes->{'default'} = $othertitle;
 3636:         if (ref($types) eq 'ARRAY') {
 3637:             push(@{$types},'default');
 3638:             $usertypes->{'default'} = $othertitle;
 3639:             foreach my $status (@{$types}) {
 3640:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3641:                                                        $numinrow,$rowcount,$usertypes);
 3642:                 $$rowtotal ++;
 3643:                 $rowcount ++;
 3644:             }
 3645:         }
 3646:     }
 3647:     return $datatable;
 3648: }
 3649: 
 3650: sub print_defaults {
 3651:     my ($dom,$rowtotal) = @_;
 3652:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3653:                  'datelocale_def','portal_def');
 3654:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3655:     my $titles = &defaults_titles($dom);
 3656:     my $rownum = 0;
 3657:     my ($datatable,$css_class);
 3658:     foreach my $item (@items) {
 3659:         if ($rownum%2) {
 3660:             $css_class = '';
 3661:         } else {
 3662:             $css_class = ' class="LC_odd_row" ';
 3663:         }
 3664:         $datatable .= '<tr'.$css_class.'>'.
 3665:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3666:                   '</span></td><td class="LC_right_item">';
 3667:         if ($item eq 'auth_def') {
 3668:             my @authtypes = ('internal','krb4','krb5','localauth');
 3669:             my %shortauth = (
 3670:                              internal => 'int',
 3671:                              krb4 => 'krb4',
 3672:                              krb5 => 'krb5',
 3673:                              localauth  => 'loc'
 3674:                            );
 3675:             my %authnames = &authtype_names();
 3676:             foreach my $auth (@authtypes) {
 3677:                 my $checked = ' ';
 3678:                 if ($domdefaults{$item} eq $auth) {
 3679:                     $checked = ' checked="checked" ';
 3680:                 }
 3681:                 $datatable .= '<label><input type="radio" name="'.$item.
 3682:                               '" value="'.$auth.'"'.$checked.'/>'.
 3683:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3684:             }
 3685:         } elsif ($item eq 'timezone_def') {
 3686:             my $includeempty = 1;
 3687:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3688:         } elsif ($item eq 'datelocale_def') {
 3689:             my $includeempty = 1;
 3690:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3691:         } elsif ($item eq 'lang_def') {
 3692:             my %langchoices = &get_languages_hash();
 3693:             $langchoices{''} = 'No language preference';
 3694:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 3695:             $datatable .= &Apache::loncommon::select_form($domdefaults{$item},$item,
 3696:                                                           \%langchoices);
 3697:         } else {
 3698:             my $size;
 3699:             if ($item eq 'portal_def') {
 3700:                 $size = ' size="25"';
 3701:             }
 3702:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3703:                           $domdefaults{$item}.'"'.$size.' />';
 3704:         }
 3705:         $datatable .= '</td></tr>';
 3706:         $rownum ++;
 3707:     }
 3708:     $$rowtotal += $rownum;
 3709:     return $datatable;
 3710: }
 3711: 
 3712: sub get_languages_hash {
 3713:     my %langchoices;
 3714:     foreach my $id (&Apache::loncommon::languageids()) {
 3715:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 3716:         if ($code ne '') {
 3717:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 3718:         }
 3719:     }
 3720:     return %langchoices;
 3721: }
 3722: 
 3723: sub defaults_titles {
 3724:     my ($dom) = @_;
 3725:     my %titles = &Apache::lonlocal::texthash (
 3726:                    'auth_def'      => 'Default authentication type',
 3727:                    'auth_arg_def'  => 'Default authentication argument',
 3728:                    'lang_def'      => 'Default language',
 3729:                    'timezone_def'  => 'Default timezone',
 3730:                    'datelocale_def' => 'Default locale for dates',
 3731:                    'portal_def'     => 'Portal/Default URL',
 3732:                  );
 3733:     if ($dom) {
 3734:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3735:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3736:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3737:         $protocol = 'http' if ($protocol ne 'https');
 3738:         if ($uint_dom) {
 3739:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3740:                                          $uint_dom);
 3741:         }
 3742:     }
 3743:     return (\%titles);
 3744: }
 3745: 
 3746: sub print_scantronformat {
 3747:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3748:     my $itemcount = 1;
 3749:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3750:         %confhash);
 3751:     my $switchserver = &check_switchserver($dom,$confname);
 3752:     my %lt = &Apache::lonlocal::texthash (
 3753:                 default => 'Default bubblesheet format file error',
 3754:                 custom  => 'Custom bubblesheet format file error',
 3755:              );
 3756:     my %scantronfiles = (
 3757:         default => 'default.tab',
 3758:         custom => 'custom.tab',
 3759:     );
 3760:     foreach my $key (keys(%scantronfiles)) {
 3761:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3762:                               .$scantronfiles{$key};
 3763:     }
 3764:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3765:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3766:         if (!$switchserver) {
 3767:             my $servadm = $r->dir_config('lonAdmEMail');
 3768:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3769:             if ($configuserok eq 'ok') {
 3770:                 if ($author_ok eq 'ok') {
 3771:                     my %legacyfile = (
 3772:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3773:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3774:                     );
 3775:                     my %md5chk;
 3776:                     foreach my $type (keys(%legacyfile)) {
 3777:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3778:                         chomp($md5chk{$type});
 3779:                     }
 3780:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3781:                         foreach my $type (keys(%legacyfile)) {
 3782:                             ($scantronurls{$type},my $error) = 
 3783:                                 &legacy_scantronformat($r,$dom,$confname,
 3784:                                                  $type,$legacyfile{$type},
 3785:                                                  $scantronurls{$type},
 3786:                                                  $scantronfiles{$type});
 3787:                             if ($error ne '') {
 3788:                                 $error{$type} = $error;
 3789:                             }
 3790:                         }
 3791:                         if (keys(%error) == 0) {
 3792:                             $is_custom = 1;
 3793:                             $confhash{'scantron'}{'scantronformat'} = 
 3794:                                 $scantronurls{'custom'};
 3795:                             my $putresult = 
 3796:                                 &Apache::lonnet::put_dom('configuration',
 3797:                                                          \%confhash,$dom);
 3798:                             if ($putresult ne 'ok') {
 3799:                                 $error{'custom'} = 
 3800:                                     '<span class="LC_error">'.
 3801:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3802:                             }
 3803:                         }
 3804:                     } else {
 3805:                         ($scantronurls{'default'},my $error) =
 3806:                             &legacy_scantronformat($r,$dom,$confname,
 3807:                                           'default',$legacyfile{'default'},
 3808:                                           $scantronurls{'default'},
 3809:                                           $scantronfiles{'default'});
 3810:                         if ($error eq '') {
 3811:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3812:                             my $putresult =
 3813:                                 &Apache::lonnet::put_dom('configuration',
 3814:                                                          \%confhash,$dom);
 3815:                             if ($putresult ne 'ok') {
 3816:                                 $error{'default'} =
 3817:                                     '<span class="LC_error">'.
 3818:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3819:                             }
 3820:                         } else {
 3821:                             $error{'default'} = $error;
 3822:                         }
 3823:                     }
 3824:                 }
 3825:             }
 3826:         } else {
 3827:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3828:         }
 3829:     }
 3830:     if (ref($settings) eq 'HASH') {
 3831:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3832:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3833:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3834:                 $scantronurl = '';
 3835:             } else {
 3836:                 $scantronurl = $settings->{'scantronformat'};
 3837:             }
 3838:             $is_custom = 1;
 3839:         } else {
 3840:             $scantronurl = $scantronurls{'default'};
 3841:         }
 3842:     } else {
 3843:         if ($is_custom) {
 3844:             $scantronurl = $scantronurls{'custom'};
 3845:         } else {
 3846:             $scantronurl = $scantronurls{'default'};
 3847:         }
 3848:     }
 3849:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3850:     $datatable .= '<tr'.$css_class.'>';
 3851:     if (!$is_custom) {
 3852:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3853:                       '<span class="LC_nobreak">';
 3854:         if ($scantronurl) {
 3855:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3856:                           &mt('Default bubblesheet format file').'</a>';
 3857:         } else {
 3858:             $datatable = &mt('File unavailable for display');
 3859:         }
 3860:         $datatable .= '</span></td>';
 3861:         if (keys(%error) == 0) { 
 3862:             $datatable .= '<td valign="bottom">';
 3863:             if (!$switchserver) {
 3864:                 $datatable .= &mt('Upload:').'<br />';
 3865:             }
 3866:         } else {
 3867:             my $errorstr;
 3868:             foreach my $key (sort(keys(%error))) {
 3869:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3870:             }
 3871:             $datatable .= '<td>'.$errorstr;
 3872:         }
 3873:     } else {
 3874:         if (keys(%error) > 0) {
 3875:             my $errorstr;
 3876:             foreach my $key (sort(keys(%error))) {
 3877:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3878:             } 
 3879:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3880:         } elsif ($scantronurl) {
 3881:             $datatable .= '<td><span class="LC_nobreak">'.
 3882:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3883:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3884:                           '<input type="checkbox" name="scantronformat_del"'.
 3885:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3886:                           '<td><span class="LC_nobreak">&nbsp;'.
 3887:                           &mt('Replace:').'</span><br />';
 3888:         }
 3889:     }
 3890:     if (keys(%error) == 0) {
 3891:         if ($switchserver) {
 3892:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3893:         } else {
 3894:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3895:                          '<input type="file" name="scantronformat" /></span>';
 3896:         }
 3897:     }
 3898:     $datatable .= '</td></tr>';
 3899:     $$rowtotal ++;
 3900:     return $datatable;
 3901: }
 3902: 
 3903: sub legacy_scantronformat {
 3904:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3905:     my ($url,$error);
 3906:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3907:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3908:         (my $result,$url) =
 3909:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3910:                          '','',$newfile);
 3911:         if ($result ne 'ok') {
 3912:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3913:         }
 3914:     }
 3915:     return ($url,$error);
 3916: }
 3917: 
 3918: sub print_coursecategories {
 3919:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3920:     my $datatable;
 3921:     if ($position eq 'top') {
 3922:         my $toggle_cats_crs = ' ';
 3923:         my $toggle_cats_dom = ' checked="checked" ';
 3924:         my $can_cat_crs = ' ';
 3925:         my $can_cat_dom = ' checked="checked" ';
 3926:         my $toggle_catscomm_comm = ' ';
 3927:         my $toggle_catscomm_dom = ' checked="checked" ';
 3928:         my $can_catcomm_comm = ' ';
 3929:         my $can_catcomm_dom = ' checked="checked" ';
 3930: 
 3931:         if (ref($settings) eq 'HASH') {
 3932:             if ($settings->{'togglecats'} eq 'crs') {
 3933:                 $toggle_cats_crs = $toggle_cats_dom;
 3934:                 $toggle_cats_dom = ' ';
 3935:             }
 3936:             if ($settings->{'categorize'} eq 'crs') {
 3937:                 $can_cat_crs = $can_cat_dom;
 3938:                 $can_cat_dom = ' ';
 3939:             }
 3940:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3941:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3942:                 $toggle_catscomm_dom = ' ';
 3943:             }
 3944:             if ($settings->{'categorizecomm'} eq 'comm') {
 3945:                 $can_catcomm_comm = $can_catcomm_dom;
 3946:                 $can_catcomm_dom = ' ';
 3947:             }
 3948:         }
 3949:         my %title = &Apache::lonlocal::texthash (
 3950:                      togglecats     => 'Show/Hide a course in catalog',
 3951:                      togglecatscomm => 'Show/Hide a community in catalog',
 3952:                      categorize     => 'Assign a category to a course',
 3953:                      categorizecomm => 'Assign a category to a community',
 3954:                     );
 3955:         my %level = &Apache::lonlocal::texthash (
 3956:                      dom  => 'Set in Domain',
 3957:                      crs  => 'Set in Course',
 3958:                      comm => 'Set in Community',
 3959:                     );
 3960:         $datatable = '<tr class="LC_odd_row">'.
 3961:                   '<td>'.$title{'togglecats'}.'</td>'.
 3962:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3963:                   '<input type="radio" name="togglecats"'.
 3964:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3965:                   '<label><input type="radio" name="togglecats"'.
 3966:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3967:                   '</tr><tr>'.
 3968:                   '<td>'.$title{'categorize'}.'</td>'.
 3969:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3970:                   '<label><input type="radio" name="categorize"'.
 3971:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3972:                   '<label><input type="radio" name="categorize"'.
 3973:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3974:                   '</tr><tr class="LC_odd_row">'.
 3975:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 3976:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3977:                   '<input type="radio" name="togglecatscomm"'.
 3978:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3979:                   '<label><input type="radio" name="togglecatscomm"'.
 3980:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3981:                   '</tr><tr>'.
 3982:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 3983:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3984:                   '<label><input type="radio" name="categorizecomm"'.
 3985:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3986:                   '<label><input type="radio" name="categorizecomm"'.
 3987:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3988:                   '</tr>';
 3989:         $$rowtotal += 4;
 3990:     } else {
 3991:         my $css_class;
 3992:         my $itemcount = 1;
 3993:         my $cathash; 
 3994:         if (ref($settings) eq 'HASH') {
 3995:             $cathash = $settings->{'cats'};
 3996:         }
 3997:         if (ref($cathash) eq 'HASH') {
 3998:             my (@cats,@trails,%allitems,%idx,@jsarray);
 3999:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4000:                                                    \%allitems,\%idx,\@jsarray);
 4001:             my $maxdepth = scalar(@cats);
 4002:             my $colattrib = '';
 4003:             if ($maxdepth > 2) {
 4004:                 $colattrib = ' colspan="2" ';
 4005:             }
 4006:             my @path;
 4007:             if (@cats > 0) {
 4008:                 if (ref($cats[0]) eq 'ARRAY') {
 4009:                     my $numtop = @{$cats[0]};
 4010:                     my $maxnum = $numtop;
 4011:                     my %default_names = (
 4012:                           instcode    => &mt('Official courses'),
 4013:                           communities => &mt('Communities'),
 4014:                     );
 4015: 
 4016:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4017:                         ($cathash->{'instcode::0'} eq '') ||
 4018:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4019:                         ($cathash->{'communities::0'} eq '')) {
 4020:                         $maxnum ++;
 4021:                     }
 4022:                     my $lastidx;
 4023:                     for (my $i=0; $i<$numtop; $i++) {
 4024:                         my $parent = $cats[0][$i];
 4025:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4026:                         my $item = &escape($parent).'::0';
 4027:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4028:                         $lastidx = $idx{$item};
 4029:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4030:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4031:                         for (my $k=0; $k<=$maxnum; $k++) {
 4032:                             my $vpos = $k+1;
 4033:                             my $selstr;
 4034:                             if ($k == $i) {
 4035:                                 $selstr = ' selected="selected" ';
 4036:                             }
 4037:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4038:                         }
 4039:                         $datatable .= '</select></td><td>';
 4040:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4041:                             $datatable .=  '<span class="LC_nobreak">'
 4042:                                            .$default_names{$parent}.'</span>';
 4043:                             if ($parent eq 'instcode') {
 4044:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4045:                                               .&mt('with institutional codes')
 4046:                                               .')</span></td><td'.$colattrib.'>';
 4047:                             } else {
 4048:                                 $datatable .= '<table><tr><td>';
 4049:                             }
 4050:                             $datatable .= '<span class="LC_nobreak">'
 4051:                                           .'<label><input type="radio" name="'
 4052:                                           .$parent.'" value="1" checked="checked" />'
 4053:                                           .&mt('Display').'</label>';
 4054:                             if ($parent eq 'instcode') {
 4055:                                 $datatable .= '&nbsp;';
 4056:                             } else {
 4057:                                 $datatable .= '</span></td></tr><tr><td>'
 4058:                                               .'<span class="LC_nobreak">';
 4059:                             }
 4060:                             $datatable .= '<label><input type="radio" name="'
 4061:                                           .$parent.'" value="0" />'
 4062:                                           .&mt('Do not display').'</label></span>';
 4063:                             if ($parent eq 'communities') {
 4064:                                 $datatable .= '</td></tr></table>';
 4065:                             }
 4066:                             $datatable .= '</td>';
 4067:                         } else {
 4068:                             $datatable .= $parent
 4069:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 4070:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4071:                         }
 4072:                         my $depth = 1;
 4073:                         push(@path,$parent);
 4074:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4075:                         pop(@path);
 4076:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4077:                         $itemcount ++;
 4078:                     }
 4079:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4080:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4081:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4082:                     for (my $k=0; $k<=$maxnum; $k++) {
 4083:                         my $vpos = $k+1;
 4084:                         my $selstr;
 4085:                         if ($k == $numtop) {
 4086:                             $selstr = ' selected="selected" ';
 4087:                         }
 4088:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4089:                     }
 4090:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4091:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4092:                                   .'</tr>'."\n";
 4093:                     $itemcount ++;
 4094:                     foreach my $default ('instcode','communities') {
 4095:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4096:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4097:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4098:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4099:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4100:                             for (my $k=0; $k<=$maxnum; $k++) {
 4101:                                 my $vpos = $k+1;
 4102:                                 my $selstr;
 4103:                                 if ($k == $maxnum) {
 4104:                                     $selstr = ' selected="selected" ';
 4105:                                 }
 4106:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4107:                             }
 4108:                             $datatable .= '</select></span></td>'.
 4109:                                           '<td><span class="LC_nobreak">'.
 4110:                                           $default_names{$default}.'</span>';
 4111:                             if ($default eq 'instcode') {
 4112:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4113:                                               .&mt('with institutional codes').')</span>';
 4114:                             }
 4115:                             $datatable .= '</td>'
 4116:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4117:                                           .&mt('Display').'</label>&nbsp;'
 4118:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4119:                                           .&mt('Do not display').'</label></span></td></tr>';
 4120:                         }
 4121:                     }
 4122:                 }
 4123:             } else {
 4124:                 $datatable .= &initialize_categories($itemcount);
 4125:             }
 4126:         } else {
 4127:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4128:                           .&initialize_categories($itemcount);
 4129:         }
 4130:         $$rowtotal += $itemcount;
 4131:     }
 4132:     return $datatable;
 4133: }
 4134: 
 4135: sub print_serverstatuses {
 4136:     my ($dom,$settings,$rowtotal) = @_;
 4137:     my $datatable;
 4138:     my @pages = &serverstatus_pages();
 4139:     my (%namedaccess,%machineaccess);
 4140:     foreach my $type (@pages) {
 4141:         $namedaccess{$type} = '';
 4142:         $machineaccess{$type}= '';
 4143:     }
 4144:     if (ref($settings) eq 'HASH') {
 4145:         foreach my $type (@pages) {
 4146:             if (exists($settings->{$type})) {
 4147:                 if (ref($settings->{$type}) eq 'HASH') {
 4148:                     foreach my $key (keys(%{$settings->{$type}})) {
 4149:                         if ($key eq 'namedusers') {
 4150:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4151:                         } elsif ($key eq 'machines') {
 4152:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4153:                         }
 4154:                     }
 4155:                 }
 4156:             }
 4157:         }
 4158:     }
 4159:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4160:     my $rownum = 0;
 4161:     my $css_class;
 4162:     foreach my $type (@pages) {
 4163:         $rownum ++;
 4164:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4165:         $datatable .= '<tr'.$css_class.'>'.
 4166:                       '<td><span class="LC_nobreak">'.
 4167:                       $titles->{$type}.'</span></td>'.
 4168:                       '<td class="LC_left_item">'.
 4169:                       '<input type="text" name="'.$type.'_namedusers" '.
 4170:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4171:                       '<td class="LC_right_item">'.
 4172:                       '<span class="LC_nobreak">'.
 4173:                       '<input type="text" name="'.$type.'_machines" '.
 4174:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4175:                       '</td></tr>'."\n";
 4176:     }
 4177:     $$rowtotal += $rownum;
 4178:     return $datatable;
 4179: }
 4180: 
 4181: sub serverstatus_pages {
 4182:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4183:             'clusterstatus','metadata_keywords','metadata_harvest',
 4184:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4185: }
 4186: 
 4187: sub coursecategories_javascript {
 4188:     my ($settings) = @_;
 4189:     my ($output,$jstext,$cathash);
 4190:     if (ref($settings) eq 'HASH') {
 4191:         $cathash = $settings->{'cats'};
 4192:     }
 4193:     if (ref($cathash) eq 'HASH') {
 4194:         my (@cats,@jsarray,%idx);
 4195:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4196:         if (@jsarray > 0) {
 4197:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4198:             for (my $i=0; $i<@jsarray; $i++) {
 4199:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4200:                     my $catstr = join('","',@{$jsarray[$i]});
 4201:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4202:                 }
 4203:             }
 4204:         }
 4205:     } else {
 4206:         $jstext  = '    var categories = Array(1);'."\n".
 4207:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4208:     }
 4209:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4210:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4211:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4212:     $output = <<"ENDSCRIPT";
 4213: <script type="text/javascript">
 4214: // <![CDATA[
 4215: function reorderCats(form,parent,item,idx) {
 4216:     var changedVal;
 4217: $jstext
 4218:     var newpos = 'addcategory_pos';
 4219:     var current = new Array;
 4220:     if (parent == '') {
 4221:         var has_instcode = 0;
 4222:         var maxtop = categories[idx].length;
 4223:         for (var j=0; j<maxtop; j++) {
 4224:             if (categories[idx][j] == 'instcode::0') {
 4225:                 has_instcode == 1;
 4226:             }
 4227:         }
 4228:         if (has_instcode == 0) {
 4229:             categories[idx][maxtop] = 'instcode_pos';
 4230:         }
 4231:     } else {
 4232:         newpos += '_'+parent;
 4233:     }
 4234:     var maxh = 1 + categories[idx].length;
 4235:     var current = new Array;
 4236:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4237:     if (item == newpos) {
 4238:         changedVal = newitemVal;
 4239:     } else {
 4240:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4241:         current[newitemVal] = newpos;
 4242:     }
 4243:     for (var i=0; i<categories[idx].length; i++) {
 4244:         var elementName = categories[idx][i];
 4245:         if (elementName != item) {
 4246:             if (form.elements[elementName]) {
 4247:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4248:                 current[currVal] = elementName;
 4249:             }
 4250:         }
 4251:     }
 4252:     var oldVal;
 4253:     for (var j=0; j<maxh; j++) {
 4254:         if (current[j] == undefined) {
 4255:             oldVal = j;
 4256:         }
 4257:     }
 4258:     if (oldVal < changedVal) {
 4259:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4260:            var elementName = current[k];
 4261:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4262:         }
 4263:     } else {
 4264:         for (var k=changedVal; k<oldVal; k++) {
 4265:             var elementName = current[k];
 4266:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4267:         }
 4268:     }
 4269:     return;
 4270: }
 4271: 
 4272: function categoryCheck(form) {
 4273:     if (form.elements['addcategory_name'].value == 'instcode') {
 4274:         alert('$instcode_reserved\\n$choose_again');
 4275:         return false;
 4276:     }
 4277:     if (form.elements['addcategory_name'].value == 'communities') {
 4278:         alert('$communities_reserved\\n$choose_again');
 4279:         return false;
 4280:     }
 4281:     return true;
 4282: }
 4283: 
 4284: // ]]>
 4285: </script>
 4286: 
 4287: ENDSCRIPT
 4288:     return $output;
 4289: }
 4290: 
 4291: sub initialize_categories {
 4292:     my ($itemcount) = @_;
 4293:     my ($datatable,$css_class,$chgstr);
 4294:     my %default_names = (
 4295:                       instcode    => 'Official courses (with institutional codes)',
 4296:                       communities => 'Communities',
 4297:                         );
 4298:     my $select0 = ' selected="selected"';
 4299:     my $select1 = '';
 4300:     foreach my $default ('instcode','communities') {
 4301:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4302:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4303:         if ($default eq 'communities') {
 4304:             $select1 = $select0;
 4305:             $select0 = '';
 4306:         }
 4307:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4308:                      .'<select name="'.$default.'_pos">'
 4309:                      .'<option value="0"'.$select0.'>1</option>'
 4310:                      .'<option value="1"'.$select1.'>2</option>'
 4311:                      .'<option value="2">3</option></select>&nbsp;'
 4312:                      .$default_names{$default}
 4313:                      .'</span></td><td><span class="LC_nobreak">'
 4314:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4315:                      .&mt('Display').'</label>&nbsp;<label>'
 4316:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4317:                  .'</label></span></td></tr>';
 4318:         $itemcount ++;
 4319:     }
 4320:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4321:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4322:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4323:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4324:                   .'<option value="0">1</option>'
 4325:                   .'<option value="1">2</option>'
 4326:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4327:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4328:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4329:     return $datatable;
 4330: }
 4331: 
 4332: sub build_category_rows {
 4333:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4334:     my ($text,$name,$item,$chgstr);
 4335:     if (ref($cats) eq 'ARRAY') {
 4336:         my $maxdepth = scalar(@{$cats});
 4337:         if (ref($cats->[$depth]) eq 'HASH') {
 4338:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4339:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4340:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4341:                 $text .= '<td><table class="LC_datatable">';
 4342:                 my ($idxnum,$parent_name,$parent_item);
 4343:                 my $higher = $depth - 1;
 4344:                 if ($higher == 0) {
 4345:                     $parent_name = &escape($parent).'::'.$higher;
 4346:                 } else {
 4347:                     if (ref($path) eq 'ARRAY') {
 4348:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4349:                     }
 4350:                 }
 4351:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4352:                 for (my $j=0; $j<=$numchildren; $j++) {
 4353:                     if ($j < $numchildren) {
 4354:                         $name = $cats->[$depth]{$parent}[$j];
 4355:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4356:                         $idxnum = $idx->{$item};
 4357:                     } else {
 4358:                         $name = $parent_name;
 4359:                         $item = $parent_item;
 4360:                     }
 4361:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4362:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4363:                     for (my $i=0; $i<=$numchildren; $i++) {
 4364:                         my $vpos = $i+1;
 4365:                         my $selstr;
 4366:                         if ($j == $i) {
 4367:                             $selstr = ' selected="selected" ';
 4368:                         }
 4369:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4370:                     }
 4371:                     $text .= '</select>&nbsp;';
 4372:                     if ($j < $numchildren) {
 4373:                         my $deeper = $depth+1;
 4374:                         $text .= $name.'&nbsp;'
 4375:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4376:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4377:                         if(ref($path) eq 'ARRAY') {
 4378:                             push(@{$path},$name);
 4379:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4380:                             pop(@{$path});
 4381:                         }
 4382:                     } else {
 4383:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4384:                         if ($j == $numchildren) {
 4385:                             $text .= $name;
 4386:                         } else {
 4387:                             $text .= $item;
 4388:                         }
 4389:                         $text .= '" value="" />';
 4390:                     }
 4391:                     $text .= '</td></tr>';
 4392:                 }
 4393:                 $text .= '</table></td>';
 4394:             } else {
 4395:                 my $higher = $depth-1;
 4396:                 if ($higher == 0) {
 4397:                     $name = &escape($parent).'::'.$higher;
 4398:                 } else {
 4399:                     if (ref($path) eq 'ARRAY') {
 4400:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4401:                     }
 4402:                 }
 4403:                 my $colspan;
 4404:                 if ($parent ne 'instcode') {
 4405:                     $colspan = $maxdepth - $depth - 1;
 4406:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4407:                 }
 4408:             }
 4409:         }
 4410:     }
 4411:     return $text;
 4412: }
 4413: 
 4414: sub modifiable_userdata_row {
 4415:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4416:     my $rolename;
 4417:     if ($context eq 'selfcreate') {
 4418:         if (ref($usertypes) eq 'HASH') {
 4419:             $rolename = $usertypes->{$role};
 4420:         } else {
 4421:             $rolename = $role;
 4422:         }
 4423:     } else {
 4424:         if ($role eq 'cr') {
 4425:             $rolename = &mt('Custom role');
 4426:         } else {
 4427:             $rolename = &Apache::lonnet::plaintext($role);
 4428:         }
 4429:     }
 4430:     my @fields = ('lastname','firstname','middlename','generation',
 4431:                   'permanentemail','id');
 4432:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4433:     my $output;
 4434:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4435:     $output = '<tr '.$css_class.'>'.
 4436:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4437:               '<td class="LC_left_item" colspan="2"><table>';
 4438:     my $rem;
 4439:     my %checks;
 4440:     if (ref($settings) eq 'HASH') {
 4441:         if (ref($settings->{$context}) eq 'HASH') {
 4442:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4443:                 foreach my $field (@fields) {
 4444:                     if ($settings->{$context}->{$role}->{$field}) {
 4445:                         $checks{$field} = ' checked="checked" ';
 4446:                     }
 4447:                 }
 4448:             }
 4449:         }
 4450:     }
 4451:     for (my $i=0; $i<@fields; $i++) {
 4452:         my $rem = $i%($numinrow);
 4453:         if ($rem == 0) {
 4454:             if ($i > 0) {
 4455:                 $output .= '</tr>';
 4456:             }
 4457:             $output .= '<tr>';
 4458:         }
 4459:         my $check = ' ';
 4460:         if (exists($checks{$fields[$i]})) {
 4461:             $check = $checks{$fields[$i]}
 4462:         } else {
 4463:             if ($role eq 'st') {
 4464:                 if (ref($settings) ne 'HASH') {
 4465:                     $check = ' checked="checked" '; 
 4466:                 }
 4467:             }
 4468:         }
 4469:         $output .= '<td class="LC_left_item">'.
 4470:                    '<span class="LC_nobreak"><label>'.
 4471:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4472:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4473:                    '</label></span></td>';
 4474:         $rem = @fields%($numinrow);
 4475:     }
 4476:     my $colsleft = $numinrow - $rem;
 4477:     if ($colsleft > 1 ) {
 4478:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4479:                    '&nbsp;</td>';
 4480:     } elsif ($colsleft == 1) {
 4481:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4482:     }
 4483:     $output .= '</tr></table></td></tr>';
 4484:     return $output;
 4485: }
 4486: 
 4487: sub insttypes_row {
 4488:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4489:     my %lt = &Apache::lonlocal::texthash (
 4490:                       cansearch => 'Users allowed to search',
 4491:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4492:                       lockablenames => 'User preference to lock name',
 4493:              );
 4494:     my $showdom;
 4495:     if ($context eq 'cansearch') {
 4496:         $showdom = ' ('.$dom.')';
 4497:     }
 4498:     my $class = 'LC_left_item';
 4499:     if ($context eq 'statustocreate') {
 4500:         $class = 'LC_right_item';
 4501:     }
 4502:     my $output =  '<tr class="LC_odd_row">'.
 4503:                   '<td>'.$lt{$context}.$showdom.
 4504:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4505:     my $rem;
 4506:     if (ref($types) eq 'ARRAY') {
 4507:         for (my $i=0; $i<@{$types}; $i++) {
 4508:             if (defined($usertypes->{$types->[$i]})) {
 4509:                 my $rem = $i%($numinrow);
 4510:                 if ($rem == 0) {
 4511:                     if ($i > 0) {
 4512:                         $output .= '</tr>';
 4513:                     }
 4514:                     $output .= '<tr>';
 4515:                 }
 4516:                 my $check = ' ';
 4517:                 if (ref($settings) eq 'HASH') {
 4518:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4519:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4520:                             $check = ' checked="checked" ';
 4521:                         }
 4522:                     } elsif ($context eq 'statustocreate') {
 4523:                         $check = ' checked="checked" ';
 4524:                     }
 4525:                 }
 4526:                 $output .= '<td class="LC_left_item">'.
 4527:                            '<span class="LC_nobreak"><label>'.
 4528:                            '<input type="checkbox" name="'.$context.'" '.
 4529:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4530:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4531:             }
 4532:         }
 4533:         $rem = @{$types}%($numinrow);
 4534:     }
 4535:     my $colsleft = $numinrow - $rem;
 4536:     if (($rem == 0) && (@{$types} > 0)) {
 4537:         $output .= '<tr>';
 4538:     }
 4539:     if ($colsleft > 1) {
 4540:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4541:     } else {
 4542:         $output .= '<td class="LC_left_item">';
 4543:     }
 4544:     my $defcheck = ' ';
 4545:     if (ref($settings) eq 'HASH') {  
 4546:         if (ref($settings->{$context}) eq 'ARRAY') {
 4547:             if (grep(/^default$/,@{$settings->{$context}})) {
 4548:                 $defcheck = ' checked="checked" ';
 4549:             }
 4550:         } elsif ($context eq 'statustocreate') {
 4551:             $defcheck = ' checked="checked" ';
 4552:         }
 4553:     }
 4554:     $output .= '<span class="LC_nobreak"><label>'.
 4555:                '<input type="checkbox" name="'.$context.'" '.
 4556:                'value="default"'.$defcheck.'/>'.
 4557:                $othertitle.'</label></span></td>'.
 4558:                '</tr></table></td></tr>';
 4559:     return $output;
 4560: }
 4561: 
 4562: sub sorted_searchtitles {
 4563:     my %searchtitles = &Apache::lonlocal::texthash(
 4564:                          'uname' => 'username',
 4565:                          'lastname' => 'last name',
 4566:                          'lastfirst' => 'last name, first name',
 4567:                      );
 4568:     my @titleorder = ('uname','lastname','lastfirst');
 4569:     return (\%searchtitles,\@titleorder);
 4570: }
 4571: 
 4572: sub sorted_searchtypes {
 4573:     my %srchtypes_desc = (
 4574:                            exact    => 'is exact match',
 4575:                            contains => 'contains ..',
 4576:                            begins   => 'begins with ..',
 4577:                          );
 4578:     my @srchtypeorder = ('exact','begins','contains');
 4579:     return (\%srchtypes_desc,\@srchtypeorder);
 4580: }
 4581: 
 4582: sub usertype_update_row {
 4583:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4584:     my $datatable;
 4585:     my $numinrow = 4;
 4586:     foreach my $type (@{$types}) {
 4587:         if (defined($usertypes->{$type})) {
 4588:             $$rownums ++;
 4589:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4590:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4591:                           '</td><td class="LC_left_item"><table>';
 4592:             for (my $i=0; $i<@{$fields}; $i++) {
 4593:                 my $rem = $i%($numinrow);
 4594:                 if ($rem == 0) {
 4595:                     if ($i > 0) {
 4596:                         $datatable .= '</tr>';
 4597:                     }
 4598:                     $datatable .= '<tr>';
 4599:                 }
 4600:                 my $check = ' ';
 4601:                 if (ref($settings) eq 'HASH') {
 4602:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4603:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4604:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4605:                                 $check = ' checked="checked" ';
 4606:                             }
 4607:                         }
 4608:                     }
 4609:                 }
 4610: 
 4611:                 if ($i == @{$fields}-1) {
 4612:                     my $colsleft = $numinrow - $rem;
 4613:                     if ($colsleft > 1) {
 4614:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4615:                     } else {
 4616:                         $datatable .= '<td>';
 4617:                     }
 4618:                 } else {
 4619:                     $datatable .= '<td>';
 4620:                 }
 4621:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4622:                               '<input type="checkbox" name="updateable_'.$type.
 4623:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4624:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4625:             }
 4626:             $datatable .= '</tr></table></td></tr>';
 4627:         }
 4628:     }
 4629:     return $datatable;
 4630: }
 4631: 
 4632: sub modify_login {
 4633:     my ($r,$dom,$confname,%domconfig) = @_;
 4634:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4635:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4636:     %title = ( coursecatalog => 'Display course catalog',
 4637:                adminmail => 'Display administrator E-mail address',
 4638:                newuser => 'Link for visitors to create a user account',
 4639:                loginheader => 'Log-in box header');
 4640:     @offon = ('off','on');
 4641:     if (ref($domconfig{login}) eq 'HASH') {
 4642:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4643:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4644:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4645:             }
 4646:         }
 4647:     }
 4648:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4649:                                            \%domconfig,\%loginhash);
 4650:     my @toggles = ('coursecatalog','adminmail','newuser');
 4651:     foreach my $item (@toggles) {
 4652:         $loginhash{login}{$item} = $env{'form.'.$item};
 4653:     }
 4654:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4655:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4656:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4657:                                          \%loginhash);
 4658:     }
 4659: 
 4660:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4661:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4662:     if (keys(%servers) > 1) {
 4663:         foreach my $lonhost (keys(%servers)) {
 4664:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4665:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4666:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4667:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4668:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4669:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4670:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4671:                         $changes{'loginvia'}{$lonhost} = 1;
 4672:                     } else {
 4673:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4674:                         $changes{'loginvia'}{$lonhost} = 1;
 4675:                     }
 4676:                 } else {
 4677:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4678:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4679:                         $changes{'loginvia'}{$lonhost} = 1;
 4680:                     }
 4681:                 }
 4682:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4683:                     foreach my $item (@loginvia_attribs) {
 4684:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4685:                     }
 4686:                 } else {
 4687:                     foreach my $item (@loginvia_attribs) {
 4688:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4689:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4690:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4691:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4692:                                 $new = '/';
 4693:                             }
 4694:                         }
 4695:                         if (($item eq 'custompath') && 
 4696:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4697:                             $new = '';
 4698:                         }
 4699:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4700:                             $changes{'loginvia'}{$lonhost} = 1;
 4701:                         }
 4702:                         if ($item eq 'exempt') {
 4703:                             $new =~ s/^\s+//;
 4704:                             $new =~ s/\s+$//;
 4705:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4706:                             my @okips;
 4707:                             foreach my $ip (@poss_ips) {
 4708:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4709:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4710:                                         push(@okips,$ip); 
 4711:                                     }
 4712:                                 }
 4713:                             }
 4714:                             if (@okips > 0) {
 4715:                                 $new = join(',',@okips); 
 4716:                             } else {
 4717:                                 $new = ''; 
 4718:                             }
 4719:                         }
 4720:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4721:                     }
 4722:                 }
 4723:             } else {
 4724:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4725:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4726:                     $changes{'loginvia'}{$lonhost} = 1;
 4727:                     foreach my $item (@loginvia_attribs) {
 4728:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4729:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4730:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4731:                                 $new = '/';
 4732:                             }
 4733:                         }
 4734:                         if (($item eq 'custompath') && 
 4735:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4736:                             $new = '';
 4737:                         }
 4738:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4739:                     }
 4740:                 }
 4741:             }
 4742:         }
 4743:     }
 4744: 
 4745:     my $servadm = $r->dir_config('lonAdmEMail');
 4746:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 4747:     if (ref($domconfig{'login'}) eq 'HASH') {
 4748:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 4749:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 4750:                 if ($lang eq 'nolang') {
 4751:                     push(@currlangs,$lang);
 4752:                 } elsif (defined($langchoices{$lang})) {
 4753:                     push(@currlangs,$lang);
 4754:                 } else {
 4755:                     next;
 4756:                 }
 4757:             }
 4758:         }
 4759:     }
 4760:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 4761:     if (@currlangs > 0) {
 4762:         foreach my $lang (@currlangs) {
 4763:             if (grep(/^\Q$lang\E$/,@delurls)) {
 4764:                 $changes{'helpurl'}{$lang} = 1;
 4765:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 4766:                 $changes{'helpurl'}{$lang} = 1;
 4767:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 4768:                 push(@newlangs,$lang);
 4769:             } else {
 4770:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4771:             }
 4772:         }
 4773:     }
 4774:     unless (grep(/^nolang$/,@currlangs)) {
 4775:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 4776:             $changes{'helpurl'}{'nolang'} = 1;
 4777:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 4778:             push(@newlangs,'nolang');
 4779:         }
 4780:     }
 4781:     if ($env{'form.loginhelpurl_add_lang'}) {
 4782:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 4783:             ($env{'form.loginhelpurl_add_file.filename'})) {
 4784:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 4785:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 4786:         }
 4787:     }
 4788:     if ((@newlangs > 0) || ($addedfile)) {
 4789:         my $error;
 4790:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 4791:         if ($configuserok eq 'ok') {
 4792:             if ($switchserver) {
 4793:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 4794:             } elsif ($author_ok eq 'ok') {
 4795:                 my @allnew = @newlangs;
 4796:                 if ($addedfile ne '') {
 4797:                     push(@allnew,$addedfile);
 4798:                 }
 4799:                 foreach my $lang (@allnew) {
 4800:                     my $formelem = 'loginhelpurl_'.$lang;
 4801:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 4802:                         $formelem = 'loginhelpurl_add_file';
 4803:                     }
 4804:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 4805:                                                                "help/$lang",'','',$newfile{$lang});
 4806:                     if ($result eq 'ok') {
 4807:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 4808:                         $changes{'helpurl'}{$lang} = 1;
 4809:                     } else {
 4810:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 4811:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 4812:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 4813:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 4814: 
 4815:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4816:                         }
 4817:                     }
 4818:                 }
 4819:             } else {
 4820:                 $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);
 4821:             }
 4822:         } else {
 4823:             $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);
 4824:         }
 4825:         if ($error) {
 4826:             &Apache::lonnet::logthis($error);
 4827:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4828:         }
 4829:     }
 4830:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 4831: 
 4832:     my $defaulthelpfile = '/adm/loginproblems.html';
 4833:     my $defaulttext = &mt('Default in use');
 4834: 
 4835:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4836:                                              $dom);
 4837:     if ($putresult eq 'ok') {
 4838:         my @toggles = ('coursecatalog','adminmail','newuser');
 4839:         my %defaultchecked = (
 4840:                     'coursecatalog' => 'on',
 4841:                     'adminmail'     => 'off',
 4842:                     'newuser'       => 'off',
 4843:         );
 4844:         if (ref($domconfig{'login'}) eq 'HASH') {
 4845:             foreach my $item (@toggles) {
 4846:                 if ($defaultchecked{$item} eq 'on') { 
 4847:                     if (($domconfig{'login'}{$item} eq '0') &&
 4848:                         ($env{'form.'.$item} eq '1')) {
 4849:                         $changes{$item} = 1;
 4850:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4851:                               $domconfig{'login'}{$item} eq '1') &&
 4852:                              ($env{'form.'.$item} eq '0')) {
 4853:                         $changes{$item} = 1;
 4854:                     }
 4855:                 } elsif ($defaultchecked{$item} eq 'off') {
 4856:                     if (($domconfig{'login'}{$item} eq '1') &&
 4857:                         ($env{'form.'.$item} eq '0')) {
 4858:                         $changes{$item} = 1;
 4859:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4860:                               $domconfig{'login'}{$item} eq '0') &&
 4861:                              ($env{'form.'.$item} eq '1')) {
 4862:                         $changes{$item} = 1;
 4863:                     }
 4864:                 }
 4865:             }
 4866:         }
 4867:         if (keys(%changes) > 0 || $colchgtext) {
 4868:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4869:             $resulttext = &mt('Changes made:').'<ul>';
 4870:             foreach my $item (sort(keys(%changes))) {
 4871:                 if ($item eq 'loginvia') {
 4872:                     if (ref($changes{$item}) eq 'HASH') {
 4873:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4874:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4875:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4876:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4877:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4878:                                     $protocol = 'http' if ($protocol ne 'https');
 4879:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4880: 
 4881:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4882:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4883:                                     } else {
 4884:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4885:                                     }
 4886:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4887:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4888:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4889:                                     }
 4890:                                     $resulttext .= '</li>';
 4891:                                 } else {
 4892:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4893:                                 }
 4894:                             } else {
 4895:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4896:                             }
 4897:                         }
 4898:                         $resulttext .= '</ul></li>';
 4899:                     }
 4900:                 } elsif ($item eq 'helpurl') {
 4901:                     if (ref($changes{$item}) eq 'HASH') {
 4902:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 4903:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 4904:                                 my ($chg,$link);
 4905:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 4906:                                 if ($lang eq 'nolang') {
 4907:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 4908:                                 } else {
 4909:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 4910:                                 }
 4911:                                 $resulttext .= '<li>'.$chg.'</li>';
 4912:                             } else {
 4913:                                 my $chg;
 4914:                                 if ($lang eq 'nolang') {
 4915:                                     $chg = &mt('custom log-in help file for no preferred language');
 4916:                                 } else {
 4917:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 4918:                                 }
 4919:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 4920:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 4921:                                                       '?inhibitmenu=yes',$chg,600,500).
 4922:                                                '</li>';
 4923:                             }
 4924:                         }
 4925:                     }
 4926:                 } elsif ($item eq 'captcha') {
 4927:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4928:                         my $chgtxt;
 4929:                         if ($loginhash{'login'}{$item} eq 'notused') {
 4930:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 4931:                         } else {
 4932:                             my %captchas = &captcha_phrases();
 4933:                             if ($captchas{$loginhash{'login'}{$item}}) {
 4934:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 4935:                             } else {
 4936:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 4937:                             }
 4938:                         }
 4939:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4940:                     }
 4941:                 } elsif ($item eq 'recaptchakeys') {
 4942:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4943:                         my ($privkey,$pubkey);
 4944:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 4945:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 4946:                             $privkey = $loginhash{'login'}{$item}{'private'};
 4947:                         }
 4948:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 4949:                         if (!$pubkey) {
 4950:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 4951:                         } else {
 4952:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 4953:                         }
 4954:                         if (!$privkey) {
 4955:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 4956:                         } else {
 4957:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 4958:                         }
 4959:                         $chgtxt .= '</ul>';
 4960:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4961:                     }
 4962:                 } else {
 4963:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4964:                 }
 4965:             }
 4966:             $resulttext .= $colchgtext.'</ul>';
 4967:         } else {
 4968:             $resulttext = &mt('No changes made to log-in page settings');
 4969:         }
 4970:     } else {
 4971:         $resulttext = '<span class="LC_error">'.
 4972: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4973:     }
 4974:     if ($errors) {
 4975:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 4976:                        $errors.'</ul>';
 4977:     }
 4978:     return $resulttext;
 4979: }
 4980: 
 4981: sub color_font_choices {
 4982:     my %choices =
 4983:         &Apache::lonlocal::texthash (
 4984:             img => "Header",
 4985:             bgs => "Background colors",
 4986:             links => "Link colors",
 4987:             images => "Images",
 4988:             font => "Font color",
 4989:             fontmenu => "Font Menu",
 4990:             pgbg => "Page",
 4991:             tabbg => "Header",
 4992:             sidebg => "Border",
 4993:             link => "Link",
 4994:             alink => "Active link",
 4995:             vlink => "Visited link",
 4996:         );
 4997:     return %choices;
 4998: }
 4999: 
 5000: sub modify_rolecolors {
 5001:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 5002:     my ($resulttext,%rolehash);
 5003:     $rolehash{'rolecolors'} = {};
 5004:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5005:         if ($domconfig{'rolecolors'} eq '') {
 5006:             $domconfig{'rolecolors'} = {};
 5007:         }
 5008:     }
 5009:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5010:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5011:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5012:                                              $dom);
 5013:     if ($putresult eq 'ok') {
 5014:         if (keys(%changes) > 0) {
 5015:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5016:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5017:                                              $rolehash{'rolecolors'});
 5018:         } else {
 5019:             $resulttext = &mt('No changes made to default color schemes');
 5020:         }
 5021:     } else {
 5022:         $resulttext = '<span class="LC_error">'.
 5023: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5024:     }
 5025:     if ($errors) {
 5026:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5027:                        $errors.'</ul>';
 5028:     }
 5029:     return $resulttext;
 5030: }
 5031: 
 5032: sub modify_colors {
 5033:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5034:     my (%changes,%choices);
 5035:     my @bgs;
 5036:     my @links = ('link','alink','vlink');
 5037:     my @logintext;
 5038:     my @images;
 5039:     my $servadm = $r->dir_config('lonAdmEMail');
 5040:     my $errors;
 5041:     foreach my $role (@{$roles}) {
 5042:         if ($role eq 'login') {
 5043:             %choices = &login_choices();
 5044:             @logintext = ('textcol','bgcol');
 5045:         } else {
 5046:             %choices = &color_font_choices();
 5047:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5048:         }
 5049:         if ($role eq 'login') {
 5050:             @images = ('img','logo','domlogo','login');
 5051:             @bgs = ('pgbg','mainbg','sidebg');
 5052:         } else {
 5053:             @images = ('img');
 5054:             @bgs = ('pgbg','tabbg','sidebg'); 
 5055:         }
 5056:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5057:         foreach my $item (@bgs,@links,@logintext) {
 5058:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5059:         }
 5060:         my ($configuserok,$author_ok,$switchserver) = 
 5061:             &config_check($dom,$confname,$servadm);
 5062:         my ($width,$height) = &thumb_dimensions();
 5063:         if (ref($domconfig->{$role}) ne 'HASH') {
 5064:             $domconfig->{$role} = {};
 5065:         }
 5066:         foreach my $img (@images) {
 5067:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5068:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5069:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5070:                 } else { 
 5071:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5072:                 }
 5073:             } 
 5074: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5075: 		 && !defined($domconfig->{$role}{$img})
 5076: 		 && !$env{'form.'.$role.'_del_'.$img}
 5077: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5078: 		# import the old configured image from the .tab setting
 5079: 		# if they haven't provided a new one 
 5080: 		$domconfig->{$role}{$img} = 
 5081: 		    $env{'form.'.$role.'_import_'.$img};
 5082: 	    }
 5083:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5084:                 my $error;
 5085:                 if ($configuserok eq 'ok') {
 5086:                     if ($switchserver) {
 5087:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5088:                     } else {
 5089:                         if ($author_ok eq 'ok') {
 5090:                             my ($result,$logourl) = 
 5091:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5092:                                            $dom,$confname,$img,$width,$height);
 5093:                             if ($result eq 'ok') {
 5094:                                 $confhash->{$role}{$img} = $logourl;
 5095:                                 $changes{$role}{'images'}{$img} = 1;
 5096:                             } else {
 5097:                                 $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);
 5098:                             }
 5099:                         } else {
 5100:                             $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);
 5101:                         }
 5102:                     }
 5103:                 } else {
 5104:                     $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);
 5105:                 }
 5106:                 if ($error) {
 5107:                     &Apache::lonnet::logthis($error);
 5108:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5109:                 }
 5110:             } elsif ($domconfig->{$role}{$img} ne '') {
 5111:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5112:                     my $error;
 5113:                     if ($configuserok eq 'ok') {
 5114: # is confname an author?
 5115:                         if ($switchserver eq '') {
 5116:                             if ($author_ok eq 'ok') {
 5117:                                 my ($result,$logourl) = 
 5118:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5119:                                             $dom,$confname,$img,$width,$height);
 5120:                                 if ($result eq 'ok') {
 5121:                                     $confhash->{$role}{$img} = $logourl;
 5122: 				    $changes{$role}{'images'}{$img} = 1;
 5123:                                 }
 5124:                             }
 5125:                         }
 5126:                     }
 5127:                 }
 5128:             }
 5129:         }
 5130:         if (ref($domconfig) eq 'HASH') {
 5131:             if (ref($domconfig->{$role}) eq 'HASH') {
 5132:                 foreach my $img (@images) {
 5133:                     if ($domconfig->{$role}{$img} ne '') {
 5134:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5135:                             $confhash->{$role}{$img} = '';
 5136:                             $changes{$role}{'images'}{$img} = 1;
 5137:                         } else {
 5138:                             if ($confhash->{$role}{$img} eq '') {
 5139:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5140:                             }
 5141:                         }
 5142:                     } else {
 5143:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5144:                             $confhash->{$role}{$img} = '';
 5145:                             $changes{$role}{'images'}{$img} = 1;
 5146:                         } 
 5147:                     }
 5148:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5149:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5150:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5151:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5152:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5153:                             }
 5154:                         } else {
 5155:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5156:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5157:                             }
 5158:                         }
 5159:                     }
 5160:                 }
 5161:                 if ($domconfig->{$role}{'font'} ne '') {
 5162:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5163:                         $changes{$role}{'font'} = 1;
 5164:                     }
 5165:                 } else {
 5166:                     if ($confhash->{$role}{'font'}) {
 5167:                         $changes{$role}{'font'} = 1;
 5168:                     }
 5169:                 }
 5170:                 if ($role ne 'login') {
 5171:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5172:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5173:                             $changes{$role}{'fontmenu'} = 1;
 5174:                         }
 5175:                     } else {
 5176:                         if ($confhash->{$role}{'fontmenu'}) {
 5177:                             $changes{$role}{'fontmenu'} = 1;
 5178:                         }
 5179:                     }
 5180:                 }
 5181:                 foreach my $item (@bgs) {
 5182:                     if ($domconfig->{$role}{$item} ne '') {
 5183:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5184:                             $changes{$role}{'bgs'}{$item} = 1;
 5185:                         } 
 5186:                     } else {
 5187:                         if ($confhash->{$role}{$item}) {
 5188:                             $changes{$role}{'bgs'}{$item} = 1;
 5189:                         }
 5190:                     }
 5191:                 }
 5192:                 foreach my $item (@links) {
 5193:                     if ($domconfig->{$role}{$item} ne '') {
 5194:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5195:                             $changes{$role}{'links'}{$item} = 1;
 5196:                         }
 5197:                     } else {
 5198:                         if ($confhash->{$role}{$item}) {
 5199:                             $changes{$role}{'links'}{$item} = 1;
 5200:                         }
 5201:                     }
 5202:                 }
 5203:                 foreach my $item (@logintext) {
 5204:                     if ($domconfig->{$role}{$item} ne '') {
 5205:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5206:                             $changes{$role}{'logintext'}{$item} = 1;
 5207:                         }
 5208:                     } else {
 5209:                         if ($confhash->{$role}{$item}) {
 5210:                             $changes{$role}{'logintext'}{$item} = 1;
 5211:                         }
 5212:                     }
 5213:                 }
 5214:             } else {
 5215:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5216:                                         \@logintext,$confhash,\%changes); 
 5217:             }
 5218:         } else {
 5219:             &default_change_checker($role,\@images,\@links,\@bgs,
 5220:                                     \@logintext,$confhash,\%changes); 
 5221:         }
 5222:     }
 5223:     return ($errors,%changes);
 5224: }
 5225: 
 5226: sub config_check {
 5227:     my ($dom,$confname,$servadm) = @_;
 5228:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5229:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5230:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5231:                                                    $confname,$servadm);
 5232:     if ($configuserok eq 'ok') {
 5233:         $switchserver = &check_switchserver($dom,$confname);
 5234:         if ($switchserver eq '') {
 5235:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5236:         }
 5237:     }
 5238:     return ($configuserok,$author_ok,$switchserver);
 5239: }
 5240: 
 5241: sub default_change_checker {
 5242:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5243:     foreach my $item (@{$links}) {
 5244:         if ($confhash->{$role}{$item}) {
 5245:             $changes->{$role}{'links'}{$item} = 1;
 5246:         }
 5247:     }
 5248:     foreach my $item (@{$bgs}) {
 5249:         if ($confhash->{$role}{$item}) {
 5250:             $changes->{$role}{'bgs'}{$item} = 1;
 5251:         }
 5252:     }
 5253:     foreach my $item (@{$logintext}) {
 5254:         if ($confhash->{$role}{$item}) {
 5255:             $changes->{$role}{'logintext'}{$item} = 1;
 5256:         }
 5257:     }
 5258:     foreach my $img (@{$images}) {
 5259:         if ($env{'form.'.$role.'_del_'.$img}) {
 5260:             $confhash->{$role}{$img} = '';
 5261:             $changes->{$role}{'images'}{$img} = 1;
 5262:         }
 5263:         if ($role eq 'login') {
 5264:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5265:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5266:             }
 5267:         }
 5268:     }
 5269:     if ($confhash->{$role}{'font'}) {
 5270:         $changes->{$role}{'font'} = 1;
 5271:     }
 5272: }
 5273: 
 5274: sub display_colorchgs {
 5275:     my ($dom,$changes,$roles,$confhash) = @_;
 5276:     my (%choices,$resulttext);
 5277:     if (!grep(/^login$/,@{$roles})) {
 5278:         $resulttext = &mt('Changes made:').'<br />';
 5279:     }
 5280:     foreach my $role (@{$roles}) {
 5281:         if ($role eq 'login') {
 5282:             %choices = &login_choices();
 5283:         } else {
 5284:             %choices = &color_font_choices();
 5285:         }
 5286:         if (ref($changes->{$role}) eq 'HASH') {
 5287:             if ($role ne 'login') {
 5288:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5289:             }
 5290:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5291:                 if ($role ne 'login') {
 5292:                     $resulttext .= '<ul>';
 5293:                 }
 5294:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5295:                     if ($role ne 'login') {
 5296:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5297:                     }
 5298:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5299:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5300:                             if ($confhash->{$role}{$key}{$item}) {
 5301:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5302:                             } else {
 5303:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5304:                             }
 5305:                         } elsif ($confhash->{$role}{$item} eq '') {
 5306:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5307:                         } else {
 5308:                             my $newitem = $confhash->{$role}{$item};
 5309:                             if ($key eq 'images') {
 5310:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5311:                             }
 5312:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5313:                         }
 5314:                     }
 5315:                     if ($role ne 'login') {
 5316:                         $resulttext .= '</ul></li>';
 5317:                     }
 5318:                 } else {
 5319:                     if ($confhash->{$role}{$key} eq '') {
 5320:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5321:                     } else {
 5322:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5323:                     }
 5324:                 }
 5325:                 if ($role ne 'login') {
 5326:                     $resulttext .= '</ul>';
 5327:                 }
 5328:             }
 5329:         }
 5330:     }
 5331:     return $resulttext;
 5332: }
 5333: 
 5334: sub thumb_dimensions {
 5335:     return ('200','50');
 5336: }
 5337: 
 5338: sub check_dimensions {
 5339:     my ($inputfile) = @_;
 5340:     my ($fullwidth,$fullheight);
 5341:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5342:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5343:             my $imageinfo = <PIPE>;
 5344:             if (!close(PIPE)) {
 5345:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5346:             }
 5347:             chomp($imageinfo);
 5348:             my ($fullsize) = 
 5349:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5350:             if ($fullsize) {
 5351:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5352:             }
 5353:         }
 5354:     }
 5355:     return ($fullwidth,$fullheight);
 5356: }
 5357: 
 5358: sub check_configuser {
 5359:     my ($uhome,$dom,$confname,$servadm) = @_;
 5360:     my ($configuserok,%currroles);
 5361:     if ($uhome eq 'no_host') {
 5362:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5363:         my $configpass = &LONCAPA::Enrollment::create_password();
 5364:         $configuserok = 
 5365:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5366:                              $configpass,'','','','','',undef,$servadm);
 5367:     } else {
 5368:         $configuserok = 'ok';
 5369:         %currroles = 
 5370:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5371:     }
 5372:     return ($configuserok,%currroles);
 5373: }
 5374: 
 5375: sub check_authorstatus {
 5376:     my ($dom,$confname,%currroles) = @_;
 5377:     my $author_ok;
 5378:     if (!$currroles{':'.$dom.':au'}) {
 5379:         my $start = time;
 5380:         my $end = 0;
 5381:         $author_ok = 
 5382:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5383:                                         'au',$end,$start,'','','domconfig');
 5384:     } else {
 5385:         $author_ok = 'ok';
 5386:     }
 5387:     return $author_ok;
 5388: }
 5389: 
 5390: sub publishlogo {
 5391:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5392:     my ($output,$fname,$logourl);
 5393:     if ($action eq 'upload') {
 5394:         $fname=$env{'form.'.$formname.'.filename'};
 5395:         chop($env{'form.'.$formname});
 5396:     } else {
 5397:         ($fname) = ($formname =~ /([^\/]+)$/);
 5398:     }
 5399:     if ($savefileas ne '') {
 5400:         $fname = $savefileas;
 5401:     }
 5402:     $fname=&Apache::lonnet::clean_filename($fname);
 5403: # See if there is anything left
 5404:     unless ($fname) { return ('error: no uploaded file'); }
 5405:     $fname="$subdir/$fname";
 5406:     my $docroot=$r->dir_config('lonDocRoot');
 5407:     my $filepath="$docroot/priv";
 5408:     my $relpath = "$dom/$confname";
 5409:     my ($fnamepath,$file,$fetchthumb);
 5410:     $file=$fname;
 5411:     if ($fname=~m|/|) {
 5412:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5413:     }
 5414:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");  
 5415:     my $count;
 5416:     for ($count=5;$count<=$#parts;$count++) {
 5417:         $filepath.="/$parts[$count]";
 5418:         if ((-e $filepath)!=1) {
 5419:             mkdir($filepath,02770);
 5420:         }
 5421:     }
 5422:     # Check for bad extension and disallow upload
 5423:     if ($file=~/\.(\w+)$/ &&
 5424:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5425:         $output = 
 5426:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 5427:     } elsif ($file=~/\.(\w+)$/ &&
 5428:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5429:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5430:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5431:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5432:     } elsif (-d "$filepath/$file") {
 5433:         $output = &mt('File name is a directory name - rename the file and re-upload');
 5434:     } else {
 5435:         my $source = $filepath.'/'.$file;
 5436:         my $logfile;
 5437:         if (!open($logfile,">>$source".'.log')) {
 5438:             return (&mt('No write permission to Construction Space'));
 5439:         }
 5440:         print $logfile
 5441: "\n================= Publish ".localtime()." ================\n".
 5442: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5443: # Save the file
 5444:         if (!open(FH,'>'.$source)) {
 5445:             &Apache::lonnet::logthis('Failed to create '.$source);
 5446:             return (&mt('Failed to create file'));
 5447:         }
 5448:         if ($action eq 'upload') {
 5449:             if (!print FH ($env{'form.'.$formname})) {
 5450:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5451:                 return (&mt('Failed to write file'));
 5452:             }
 5453:         } else {
 5454:             my $original = &Apache::lonnet::filelocation('',$formname);
 5455:             if(!copy($original,$source)) {
 5456:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5457:                 return (&mt('Failed to write file'));
 5458:             }
 5459:         }
 5460:         close(FH);
 5461:         chmod(0660, $source); # Permissions to rw-rw---.
 5462: 
 5463:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5464:         my $copyfile=$targetdir.'/'.$file;
 5465: 
 5466:         my @parts=split(/\//,$targetdir);
 5467:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5468:         for (my $count=5;$count<=$#parts;$count++) {
 5469:             $path.="/$parts[$count]";
 5470:             if (!-e $path) {
 5471:                 print $logfile "\nCreating directory ".$path;
 5472:                 mkdir($path,02770);
 5473:             }
 5474:         }
 5475:         my $versionresult;
 5476:         if (-e $copyfile) {
 5477:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5478:         } else {
 5479:             $versionresult = 'ok';
 5480:         }
 5481:         if ($versionresult eq 'ok') {
 5482:             if (copy($source,$copyfile)) {
 5483:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5484:                 $output = 'ok';
 5485:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5486:                 push(@{$modified_urls},[$copyfile,$source]);
 5487:                 my $metaoutput = 
 5488:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5489:                 unless ($registered_cleanup) {
 5490:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5491:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5492:                     $registered_cleanup=1;
 5493:                 }
 5494:             } else {
 5495:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5496:                 $output = &mt('Failed to copy file to RES space').", $!";
 5497:             }
 5498:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5499:                 my $inputfile = $filepath.'/'.$file;
 5500:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5501:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5502:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5503:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5504:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5505:                         system("convert -sample $thumbsize $inputfile $outfile");
 5506:                         chmod(0660, $filepath.'/tn-'.$file);
 5507:                         if (-e $outfile) {
 5508:                             my $copyfile=$targetdir.'/tn-'.$file;
 5509:                             if (copy($outfile,$copyfile)) {
 5510:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5511:                                 my $thumb_metaoutput = 
 5512:                                     &write_metadata($dom,$confname,$formname,
 5513:                                                     $targetdir,'tn-'.$file,$logfile);
 5514:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5515:                                 unless ($registered_cleanup) {
 5516:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5517:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5518:                                     $registered_cleanup=1;
 5519:                                 }
 5520:                             } else {
 5521:                                 print $logfile "\nUnable to write ".$copyfile.
 5522:                                                ':'.$!."\n";
 5523:                             }
 5524:                         }
 5525:                     }
 5526:                 }
 5527:             }
 5528:         } else {
 5529:             $output = $versionresult;
 5530:         }
 5531:     }
 5532:     return ($output,$logourl);
 5533: }
 5534: 
 5535: sub logo_versioning {
 5536:     my ($targetdir,$file,$logfile) = @_;
 5537:     my $target = $targetdir.'/'.$file;
 5538:     my ($maxversion,$fn,$extn,$output);
 5539:     $maxversion = 0;
 5540:     if ($file =~ /^(.+)\.(\w+)$/) {
 5541:         $fn=$1;
 5542:         $extn=$2;
 5543:     }
 5544:     opendir(DIR,$targetdir);
 5545:     while (my $filename=readdir(DIR)) {
 5546:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5547:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5548:         }
 5549:     }
 5550:     $maxversion++;
 5551:     print $logfile "\nCreating old version ".$maxversion."\n";
 5552:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5553:     if (copy($target,$copyfile)) {
 5554:         print $logfile "Copied old target to ".$copyfile."\n";
 5555:         $copyfile=$copyfile.'.meta';
 5556:         if (copy($target.'.meta',$copyfile)) {
 5557:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5558:             $output = 'ok';
 5559:         } else {
 5560:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5561:             $output = &mt('Failed to copy old meta').", $!, ";
 5562:         }
 5563:     } else {
 5564:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5565:         $output = &mt('Failed to copy old target').", $!, ";
 5566:     }
 5567:     return $output;
 5568: }
 5569: 
 5570: sub write_metadata {
 5571:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5572:     my (%metadatafields,%metadatakeys,$output);
 5573:     $metadatafields{'title'}=$formname;
 5574:     $metadatafields{'creationdate'}=time;
 5575:     $metadatafields{'lastrevisiondate'}=time;
 5576:     $metadatafields{'copyright'}='public';
 5577:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5578:                                          $env{'user.domain'};
 5579:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5580:     $metadatafields{'domain'}=$dom;
 5581:     {
 5582:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5583:         my $mfh;
 5584:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5585:             foreach (sort(keys(%metadatafields))) {
 5586:                 unless ($_=~/\./) {
 5587:                     my $unikey=$_;
 5588:                     $unikey=~/^([A-Za-z]+)/;
 5589:                     my $tag=$1;
 5590:                     $tag=~tr/A-Z/a-z/;
 5591:                     print $mfh "\n\<$tag";
 5592:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5593:                         my $value=$metadatafields{$unikey.'.'.$_};
 5594:                         $value=~s/\"/\'\'/g;
 5595:                         print $mfh ' '.$_.'="'.$value.'"';
 5596:                     }
 5597:                     print $mfh '>'.
 5598:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5599:                             .'</'.$tag.'>';
 5600:                 }
 5601:             }
 5602:             $output = 'ok';
 5603:             print $logfile "\nWrote metadata";
 5604:             close($mfh);
 5605:         } else {
 5606:             print $logfile "\nFailed to open metadata file";
 5607:             $output = &mt('Could not write metadata');
 5608:         }
 5609:     }
 5610:     return $output;
 5611: }
 5612: 
 5613: sub notifysubscribed {
 5614:     foreach my $targetsource (@{$modified_urls}){
 5615:         next unless (ref($targetsource) eq 'ARRAY');
 5616:         my ($target,$source)=@{$targetsource};
 5617:         if ($source ne '') {
 5618:             if (open(my $logfh,'>>'.$source.'.log')) {
 5619:                 print $logfh "\nCleanup phase: Notifications\n";
 5620:                 my @subscribed=&subscribed_hosts($target);
 5621:                 foreach my $subhost (@subscribed) {
 5622:                     print $logfh "\nNotifying host ".$subhost.':';
 5623:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5624:                     print $logfh $reply;
 5625:                 }
 5626:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5627:                 foreach my $subhost (@subscribedmeta) {
 5628:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5629:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5630:                                                         $subhost);
 5631:                     print $logfh $reply;
 5632:                 }
 5633:                 print $logfh "\n============ Done ============\n";
 5634:                 close($logfh);
 5635:             }
 5636:         }
 5637:     }
 5638:     return OK;
 5639: }
 5640: 
 5641: sub subscribed_hosts {
 5642:     my ($target) = @_;
 5643:     my @subscribed;
 5644:     if (open(my $fh,"<$target.subscription")) {
 5645:         while (my $subline=<$fh>) {
 5646:             if ($subline =~ /^($match_lonid):/) {
 5647:                 my $host = $1;
 5648:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5649:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5650:                         push(@subscribed,$host);
 5651:                     }
 5652:                 }
 5653:             }
 5654:         }
 5655:     }
 5656:     return @subscribed;
 5657: }
 5658: 
 5659: sub check_switchserver {
 5660:     my ($dom,$confname) = @_;
 5661:     my ($allowed,$switchserver);
 5662:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5663:     if ($home eq 'no_host') {
 5664:         $home = &Apache::lonnet::domain($dom,'primary');
 5665:     }
 5666:     my @ids=&Apache::lonnet::current_machine_ids();
 5667:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5668:     if (!$allowed) {
 5669: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5670:     }
 5671:     return $switchserver;
 5672: }
 5673: 
 5674: sub modify_quotas {
 5675:     my ($dom,$action,%domconfig) = @_;
 5676:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5677:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5678:     if ($action eq 'quotas') {
 5679:         $context = 'tools'; 
 5680:     } else { 
 5681:         $context = $action;
 5682:     }
 5683:     if ($context eq 'requestcourses') {
 5684:         @usertools = ('official','unofficial','community');
 5685:         @options =('norequest','approval','validate','autolimit');
 5686:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5687:         %titles = &courserequest_titles();
 5688:         $toolregexp = join('|',@usertools);
 5689:         %conditions = &courserequest_conditions();
 5690:     } elsif ($context eq 'requestauthor') {
 5691:         @usertools = ('author');
 5692:         %titles = &authorrequest_titles();
 5693:     } else {
 5694:         @usertools = ('aboutme','blog','webdav','portfolio');
 5695:         %titles = &tool_titles();
 5696:     }
 5697:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5698:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5699:     foreach my $key (keys(%env)) {
 5700:         if ($context eq 'requestcourses') {
 5701:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5702:                 my $item = $1;
 5703:                 my $type = $2;
 5704:                 if ($type =~ /^limit_(.+)/) {
 5705:                     $limithash{$item}{$1} = $env{$key};
 5706:                 } else {
 5707:                     $confhash{$item}{$type} = $env{$key};
 5708:                 }
 5709:             }
 5710:         } elsif ($context eq 'requestauthor') {
 5711:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5712:                 $confhash{$1} = $env{$key};
 5713:             }
 5714:         } else {
 5715:             if ($key =~ /^form\.quota_(.+)$/) {
 5716:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5717:             }
 5718:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5719:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5720:             }
 5721:         }
 5722:     }
 5723:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5724:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5725:         @approvalnotify = sort(@approvalnotify);
 5726:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5727:         if (ref($domconfig{$action}) eq 'HASH') {
 5728:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5729:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5730:                     $changes{'notify'}{'approval'} = 1;
 5731:                 }
 5732:             } else {
 5733:                 if ($confhash{'notify'}{'approval'}) {
 5734:                     $changes{'notify'}{'approval'} = 1;
 5735:                 }
 5736:             }
 5737:         } else {
 5738:             if ($confhash{'notify'}{'approval'}) {
 5739:                 $changes{'notify'}{'approval'} = 1;
 5740:             }
 5741:         }
 5742:     } else {
 5743:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5744:     }
 5745:     foreach my $item (@usertools) {
 5746:         foreach my $type (@{$types},'default','_LC_adv') {
 5747:             my $unset; 
 5748:             if ($context eq 'requestcourses') {
 5749:                 $unset = '0';
 5750:                 if ($type eq '_LC_adv') {
 5751:                     $unset = '';
 5752:                 }
 5753:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5754:                     $confhash{$item}{$type} .= '=';
 5755:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5756:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5757:                     }
 5758:                 }
 5759:             } elsif ($context eq 'requestauthor') {
 5760:                 $unset = '0';
 5761:                 if ($type eq '_LC_adv') {
 5762:                     $unset = '';
 5763:                 }
 5764:             } else {
 5765:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5766:                     $confhash{$item}{$type} = 1;
 5767:                 } else {
 5768:                     $confhash{$item}{$type} = 0;
 5769:                 }
 5770:             }
 5771:             if (ref($domconfig{$action}) eq 'HASH') {
 5772:                 if ($action eq 'requestauthor') {
 5773:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5774:                         $changes{$type} = 1;
 5775:                     }
 5776:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5777:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5778:                         $changes{$item}{$type} = 1;
 5779:                     }
 5780:                 } else {
 5781:                     if ($context eq 'requestcourses') {
 5782:                         if ($confhash{$item}{$type} ne $unset) {
 5783:                             $changes{$item}{$type} = 1;
 5784:                         }
 5785:                     } else {
 5786:                         if (!$confhash{$item}{$type}) {
 5787:                             $changes{$item}{$type} = 1;
 5788:                         }
 5789:                     }
 5790:                 }
 5791:             } else {
 5792:                 if ($context eq 'requestcourses') {
 5793:                     if ($confhash{$item}{$type} ne $unset) {
 5794:                         $changes{$item}{$type} = 1;
 5795:                     }
 5796:                 } elsif ($context eq 'requestauthor') {
 5797:                     if ($confhash{$type} ne $unset) {
 5798:                         $changes{$type} = 1;
 5799:                     }
 5800:                 } else {
 5801:                     if (!$confhash{$item}{$type}) {
 5802:                         $changes{$item}{$type} = 1;
 5803:                     }
 5804:                 }
 5805:             }
 5806:         }
 5807:     }
 5808:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5809:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5810:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5811:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5812:                     if (exists($confhash{'defaultquota'}{$key})) {
 5813:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5814:                             $changes{'defaultquota'}{$key} = 1;
 5815:                         }
 5816:                     } else {
 5817:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5818:                     }
 5819:                 }
 5820:             } else {
 5821:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5822:                     if (exists($confhash{'defaultquota'}{$key})) {
 5823:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5824:                             $changes{'defaultquota'}{$key} = 1;
 5825:                         }
 5826:                     } else {
 5827:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5828:                     }
 5829:                 }
 5830:             }
 5831:         }
 5832:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5833:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5834:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5835:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5836:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5837:                             $changes{'defaultquota'}{$key} = 1;
 5838:                         }
 5839:                     } else {
 5840:                         if (!exists($domconfig{'quotas'}{$key})) {
 5841:                             $changes{'defaultquota'}{$key} = 1;
 5842:                         }
 5843:                     }
 5844:                 } else {
 5845:                     $changes{'defaultquota'}{$key} = 1;
 5846:                 }
 5847:             }
 5848:         }
 5849:     }
 5850: 
 5851:     if ($context eq 'requestauthor') {
 5852:         $domdefaults{'requestauthor'} = \%confhash;
 5853:     } else {
 5854:         foreach my $key (keys(%confhash)) {
 5855:             $domdefaults{$key} = $confhash{$key};
 5856:         }
 5857:     }
 5858: 
 5859:     my %quotahash = (
 5860:                       $action => { %confhash }
 5861:                     );
 5862:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5863:                                              $dom);
 5864:     if ($putresult eq 'ok') {
 5865:         if (keys(%changes) > 0) {
 5866:             my $cachetime = 24*60*60;
 5867:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5868: 
 5869:             $resulttext = &mt('Changes made:').'<ul>';
 5870:             unless (($context eq 'requestcourses') ||
 5871:                     ($context eq 'requestauthor')) {
 5872:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5873:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5874:                     foreach my $type (@{$types},'default') {
 5875:                         if (defined($changes{'defaultquota'}{$type})) {
 5876:                             my $typetitle = $usertypes->{$type};
 5877:                             if ($type eq 'default') {
 5878:                                 $typetitle = $othertitle;
 5879:                             }
 5880:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5881:                         }
 5882:                     }
 5883:                     $resulttext .= '</ul></li>';
 5884:                 }
 5885:             }
 5886:             my %newenv;
 5887:             foreach my $item (@usertools) {
 5888:                 my (%haschgs,%inconf);
 5889:                 if ($context eq 'requestauthor') {
 5890:                     %haschgs = %changes;
 5891:                     %inconf = %confhash;
 5892:                 } else {
 5893:                     if (ref($changes{$item}) eq 'HASH') {
 5894:                         %haschgs = %{$changes{$item}};
 5895:                     }
 5896:                     if (ref($confhash{$item}) eq 'HASH') {
 5897:                         %inconf = %{$confhash{$item}};
 5898:                     }
 5899:                 }
 5900:                 if (keys(%haschgs) > 0) {
 5901:                     my $newacc = 
 5902:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5903:                                                           $env{'user.domain'},
 5904:                                                           $item,'reload',$context);
 5905:                     if (($context eq 'requestcourses') ||
 5906:                         ($context eq 'requestauthor')) {
 5907:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5908:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5909:                         }
 5910:                     } else {
 5911:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5912:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5913:                         }
 5914:                     }
 5915:                     unless ($context eq 'requestauthor') {
 5916:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5917:                     }
 5918:                     foreach my $type (@{$types},'default','_LC_adv') {
 5919:                         if ($haschgs{$type}) {
 5920:                             my $typetitle = $usertypes->{$type};
 5921:                             if ($type eq 'default') {
 5922:                                 $typetitle = $othertitle;
 5923:                             } elsif ($type eq '_LC_adv') {
 5924:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5925:                             }
 5926:                             if ($inconf{$type}) {
 5927:                                 if ($context eq 'requestcourses') {
 5928:                                     my $cond;
 5929:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 5930:                                         if ($1 eq '') {
 5931:                                             $cond = &mt('(Automatic processing of any request).');
 5932:                                         } else {
 5933:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5934:                                         }
 5935:                                     } else { 
 5936:                                         $cond = $conditions{$inconf{$type}};
 5937:                                     }
 5938:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5939:                                 } elsif ($context eq 'requestauthor') {
 5940:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 5941:                                                              $titles{$inconf{$type}},$typetitle);
 5942: 
 5943:                                 } else {
 5944:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5945:                                 }
 5946:                             } else {
 5947:                                 if ($type eq '_LC_adv') {
 5948:                                     if ($inconf{$type} eq '0') {
 5949:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5950:                                     } else { 
 5951:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5952:                                     }
 5953:                                 } else {
 5954:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5955:                                 }
 5956:                             }
 5957:                         }
 5958:                     }
 5959:                     unless ($context eq 'requestauthor') {
 5960:                         $resulttext .= '</ul></li>';
 5961:                     }
 5962:                 }
 5963:             }
 5964:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 5965:                 if (ref($changes{'notify'}) eq 'HASH') {
 5966:                     if ($changes{'notify'}{'approval'}) {
 5967:                         if (ref($confhash{'notify'}) eq 'HASH') {
 5968:                             if ($confhash{'notify'}{'approval'}) {
 5969:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 5970:                             } else {
 5971:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 5972:                             }
 5973:                         }
 5974:                     }
 5975:                 }
 5976:             }
 5977:             $resulttext .= '</ul>';
 5978:             if (keys(%newenv)) {
 5979:                 &Apache::lonnet::appenv(\%newenv);
 5980:             }
 5981:         } else {
 5982:             if ($context eq 'requestcourses') {
 5983:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 5984:             } elsif ($context eq 'requestauthor') {
 5985:                 $resulttext = &mt('No changes made to rights to request author space.');
 5986:             } else {
 5987:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 5988:             }
 5989:         }
 5990:     } else {
 5991:         $resulttext = '<span class="LC_error">'.
 5992: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5993:     }
 5994:     return $resulttext;
 5995: }
 5996: 
 5997: sub modify_autoenroll {
 5998:     my ($dom,%domconfig) = @_;
 5999:     my ($resulttext,%changes);
 6000:     my %currautoenroll;
 6001:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 6002:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 6003:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 6004:         }
 6005:     }
 6006:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6007:     my %title = ( run => 'Auto-enrollment active',
 6008:                   sender => 'Sender for notification messages',
 6009:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6010:     my @offon = ('off','on');
 6011:     my $sender_uname = $env{'form.sender_uname'};
 6012:     my $sender_domain = $env{'form.sender_domain'};
 6013:     if ($sender_domain eq '') {
 6014:         $sender_uname = '';
 6015:     } elsif ($sender_uname eq '') {
 6016:         $sender_domain = '';
 6017:     }
 6018:     my $coowners = $env{'form.autoassign_coowners'};
 6019:     my %autoenrollhash =  (
 6020:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6021:                                        'sender_uname' => $sender_uname,
 6022:                                        'sender_domain' => $sender_domain,
 6023:                                        'co-owners' => $coowners,
 6024:                                 }
 6025:                      );
 6026:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6027:                                              $dom);
 6028:     if ($putresult eq 'ok') {
 6029:         if (exists($currautoenroll{'run'})) {
 6030:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6031:                  $changes{'run'} = 1;
 6032:              }
 6033:         } elsif ($autorun) {
 6034:             if ($env{'form.autoenroll_run'} ne '1') {
 6035:                  $changes{'run'} = 1;
 6036:             }
 6037:         }
 6038:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6039:             $changes{'sender'} = 1;
 6040:         }
 6041:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6042:             $changes{'sender'} = 1;
 6043:         }
 6044:         if ($currautoenroll{'co-owners'} ne '') {
 6045:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6046:                 $changes{'coowners'} = 1;
 6047:             }
 6048:         } elsif ($coowners) {
 6049:             $changes{'coowners'} = 1;
 6050:         }      
 6051:         if (keys(%changes) > 0) {
 6052:             $resulttext = &mt('Changes made:').'<ul>';
 6053:             if ($changes{'run'}) {
 6054:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6055:             }
 6056:             if ($changes{'sender'}) {
 6057:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6058:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6059:                 } else {
 6060:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6061:                 }
 6062:             }
 6063:             if ($changes{'coowners'}) {
 6064:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6065:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6066:             }
 6067:             $resulttext .= '</ul>';
 6068:         } else {
 6069:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6070:         }
 6071:     } else {
 6072:         $resulttext = '<span class="LC_error">'.
 6073: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6074:     }
 6075:     return $resulttext;
 6076: }
 6077: 
 6078: sub modify_autoupdate {
 6079:     my ($dom,%domconfig) = @_;
 6080:     my ($resulttext,%currautoupdate,%fields,%changes);
 6081:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6082:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6083:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6084:         }
 6085:     }
 6086:     my @offon = ('off','on');
 6087:     my %title = &Apache::lonlocal::texthash (
 6088:                    run => 'Auto-update:',
 6089:                    classlists => 'Updates to user information in classlists?'
 6090:                 );
 6091:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6092:     my %fieldtitles = &Apache::lonlocal::texthash (
 6093:                         id => 'Student/Employee ID',
 6094:                         permanentemail => 'E-mail address',
 6095:                         lastname => 'Last Name',
 6096:                         firstname => 'First Name',
 6097:                         middlename => 'Middle Name',
 6098:                         generation => 'Generation',
 6099:                       );
 6100:     $othertitle = &mt('All users');
 6101:     if (keys(%{$usertypes}) >  0) {
 6102:         $othertitle = &mt('Other users');
 6103:     }
 6104:     foreach my $key (keys(%env)) {
 6105:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6106:             my ($usertype,$item) = ($1,$2);
 6107:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6108:                 if ($usertype eq 'default') {   
 6109:                     push(@{$fields{$1}},$2);
 6110:                 } elsif (ref($types) eq 'ARRAY') {
 6111:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6112:                         push(@{$fields{$1}},$2);
 6113:                     }
 6114:                 }
 6115:             }
 6116:         }
 6117:     }
 6118:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6119:     @lockablenames = sort(@lockablenames);
 6120:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6121:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6122:         if (@changed) {
 6123:             $changes{'lockablenames'} = 1;
 6124:         }
 6125:     } else {
 6126:         if (@lockablenames) {
 6127:             $changes{'lockablenames'} = 1;
 6128:         }
 6129:     }
 6130:     my %updatehash = (
 6131:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6132:                                       classlists => $env{'form.classlists'},
 6133:                                       fields => {%fields},
 6134:                                       lockablenames => \@lockablenames,
 6135:                                     }
 6136:                      );
 6137:     foreach my $key (keys(%currautoupdate)) {
 6138:         if (($key eq 'run') || ($key eq 'classlists')) {
 6139:             if (exists($updatehash{autoupdate}{$key})) {
 6140:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6141:                     $changes{$key} = 1;
 6142:                 }
 6143:             }
 6144:         } elsif ($key eq 'fields') {
 6145:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6146:                 foreach my $item (@{$types},'default') {
 6147:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6148:                         my $change = 0;
 6149:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6150:                             if (!exists($fields{$item})) {
 6151:                                 $change = 1;
 6152:                                 last;
 6153:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6154:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6155:                                     $change = 1;
 6156:                                     last;
 6157:                                 }
 6158:                             }
 6159:                         }
 6160:                         if ($change) {
 6161:                             push(@{$changes{$key}},$item);
 6162:                         }
 6163:                     } 
 6164:                 }
 6165:             }
 6166:         } elsif ($key eq 'lockablenames') {
 6167:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6168:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6169:                 if (@changed) {
 6170:                     $changes{'lockablenames'} = 1;
 6171:                 }
 6172:             } else {
 6173:                 if (@lockablenames) {
 6174:                     $changes{'lockablenames'} = 1;
 6175:                 }
 6176:             }
 6177:         }
 6178:     }
 6179:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6180:         if (@lockablenames) {
 6181:             $changes{'lockablenames'} = 1;
 6182:         }
 6183:     }
 6184:     foreach my $item (@{$types},'default') {
 6185:         if (defined($fields{$item})) {
 6186:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6187:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6188:                     my $change = 0;
 6189:                     if (ref($fields{$item}) eq 'ARRAY') {
 6190:                         foreach my $type (@{$fields{$item}}) {
 6191:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6192:                                 $change = 1;
 6193:                                 last;
 6194:                             }
 6195:                         }
 6196:                     }
 6197:                     if ($change) {
 6198:                         push(@{$changes{'fields'}},$item);
 6199:                     }
 6200:                 } else {
 6201:                     push(@{$changes{'fields'}},$item);
 6202:                 }
 6203:             } else {
 6204:                 push(@{$changes{'fields'}},$item);
 6205:             }
 6206:         }
 6207:     }
 6208:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6209:                                              $dom);
 6210:     if ($putresult eq 'ok') {
 6211:         if (keys(%changes) > 0) {
 6212:             $resulttext = &mt('Changes made:').'<ul>';
 6213:             foreach my $key (sort(keys(%changes))) {
 6214:                 if ($key eq 'lockablenames') {
 6215:                     $resulttext .= '<li>';
 6216:                     if (@lockablenames) {
 6217:                         $usertypes->{'default'} = $othertitle;
 6218:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6219:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6220:                     } else {
 6221:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6222:                     }
 6223:                     $resulttext .= '</li>';
 6224:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6225:                     foreach my $item (@{$changes{$key}}) {
 6226:                         my @newvalues;
 6227:                         foreach my $type (@{$fields{$item}}) {
 6228:                             push(@newvalues,$fieldtitles{$type});
 6229:                         }
 6230:                         my $newvaluestr;
 6231:                         if (@newvalues > 0) {
 6232:                             $newvaluestr = join(', ',@newvalues);
 6233:                         } else {
 6234:                             $newvaluestr = &mt('none');
 6235:                         }
 6236:                         if ($item eq 'default') {
 6237:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6238:                         } else {
 6239:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6240:                         }
 6241:                     }
 6242:                 } else {
 6243:                     my $newvalue;
 6244:                     if ($key eq 'run') {
 6245:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6246:                     } else {
 6247:                         $newvalue = $offon[$env{'form.'.$key}];
 6248:                     }
 6249:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6250:                 }
 6251:             }
 6252:             $resulttext .= '</ul>';
 6253:         } else {
 6254:             $resulttext = &mt('No changes made to autoupdates');
 6255:         }
 6256:     } else {
 6257:         $resulttext = '<span class="LC_error">'.
 6258: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6259:     }
 6260:     return $resulttext;
 6261: }
 6262: 
 6263: sub modify_autocreate {
 6264:     my ($dom,%domconfig) = @_;
 6265:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6266:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6267:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6268:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6269:         }
 6270:     }
 6271:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6272:                  req => 'Auto-creation of validated requests for official courses',
 6273:                  xmldc => 'Identity of course creator of courses from XML files',
 6274:                );
 6275:     my @types = ('xml','req');
 6276:     foreach my $item (@types) {
 6277:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6278:         $newvals{$item} =~ s/\D//g;
 6279:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6280:     }
 6281:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6282:     my %domcoords = &get_active_dcs($dom);
 6283:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6284:         $newvals{'xmldc'} = '';
 6285:     } 
 6286:     %autocreatehash =  (
 6287:                         autocreate => { xml => $newvals{'xml'},
 6288:                                         req => $newvals{'req'},
 6289:                                       }
 6290:                        );
 6291:     if ($newvals{'xmldc'} ne '') {
 6292:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6293:     }
 6294:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6295:                                              $dom);
 6296:     if ($putresult eq 'ok') {
 6297:         my @items = @types;
 6298:         if ($newvals{'xml'}) {
 6299:             push(@items,'xmldc');
 6300:         }
 6301:         foreach my $item (@items) {
 6302:             if (exists($currautocreate{$item})) {
 6303:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6304:                     $changes{$item} = 1;
 6305:                 }
 6306:             } elsif ($newvals{$item}) {
 6307:                 $changes{$item} = 1;
 6308:             }
 6309:         }
 6310:         if (keys(%changes) > 0) {
 6311:             my @offon = ('off','on'); 
 6312:             $resulttext = &mt('Changes made:').'<ul>';
 6313:             foreach my $item (@types) {
 6314:                 if ($changes{$item}) {
 6315:                     my $newtxt = $offon[$newvals{$item}];
 6316:                     $resulttext .= '<li>'.
 6317:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 6318:                                        '<b>','</b>').
 6319:                                    '</li>';
 6320:                 }
 6321:             }
 6322:             if ($changes{'xmldc'}) {
 6323:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6324:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6325:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 6326:             }
 6327:             $resulttext .= '</ul>';
 6328:         } else {
 6329:             $resulttext = &mt('No changes made to auto-creation settings');
 6330:         }
 6331:     } else {
 6332:         $resulttext = '<span class="LC_error">'.
 6333:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6334:     }
 6335:     return $resulttext;
 6336: }
 6337: 
 6338: sub modify_directorysrch {
 6339:     my ($dom,%domconfig) = @_;
 6340:     my ($resulttext,%changes);
 6341:     my %currdirsrch;
 6342:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6343:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6344:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6345:         }
 6346:     }
 6347:     my %title = ( available => 'Directory search available',
 6348:                   localonly => 'Other domains can search',
 6349:                   searchby => 'Search types',
 6350:                   searchtypes => 'Search latitude');
 6351:     my @offon = ('off','on');
 6352:     my @otherdoms = ('Yes','No');
 6353: 
 6354:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6355:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6356:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6357: 
 6358:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6359:     if (keys(%{$usertypes}) == 0) {
 6360:         @cansearch = ('default');
 6361:     } else {
 6362:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6363:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6364:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6365:                     push(@{$changes{'cansearch'}},$type);
 6366:                 }
 6367:             }
 6368:             foreach my $type (@cansearch) {
 6369:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6370:                     push(@{$changes{'cansearch'}},$type);
 6371:                 }
 6372:             }
 6373:         } else {
 6374:             push(@{$changes{'cansearch'}},@cansearch);
 6375:         }
 6376:     }
 6377: 
 6378:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6379:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6380:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6381:                 push(@{$changes{'searchby'}},$by);
 6382:             }
 6383:         }
 6384:         foreach my $by (@searchby) {
 6385:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6386:                 push(@{$changes{'searchby'}},$by);
 6387:             }
 6388:         }
 6389:     } else {
 6390:         push(@{$changes{'searchby'}},@searchby);
 6391:     }
 6392: 
 6393:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6394:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6395:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6396:                 push(@{$changes{'searchtypes'}},$type);
 6397:             }
 6398:         }
 6399:         foreach my $type (@searchtypes) {
 6400:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6401:                 push(@{$changes{'searchtypes'}},$type);
 6402:             }
 6403:         }
 6404:     } else {
 6405:         if (exists($currdirsrch{'searchtypes'})) {
 6406:             foreach my $type (@searchtypes) {  
 6407:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6408:                     push(@{$changes{'searchtypes'}},$type);
 6409:                 }
 6410:             }
 6411:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6412:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6413:             }   
 6414:         } else {
 6415:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6416:         }
 6417:     }
 6418: 
 6419:     my %dirsrch_hash =  (
 6420:             directorysrch => { available => $env{'form.dirsrch_available'},
 6421:                                cansearch => \@cansearch,
 6422:                                localonly => $env{'form.dirsrch_localonly'},
 6423:                                searchby => \@searchby,
 6424:                                searchtypes => \@searchtypes,
 6425:                              }
 6426:             );
 6427:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6428:                                              $dom);
 6429:     if ($putresult eq 'ok') {
 6430:         if (exists($currdirsrch{'available'})) {
 6431:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6432:                  $changes{'available'} = 1;
 6433:              }
 6434:         } else {
 6435:             if ($env{'form.dirsrch_available'} eq '1') {
 6436:                 $changes{'available'} = 1;
 6437:             }
 6438:         }
 6439:         if (exists($currdirsrch{'localonly'})) {
 6440:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6441:                  $changes{'localonly'} = 1;
 6442:              }
 6443:         } else {
 6444:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6445:                 $changes{'localonly'} = 1;
 6446:             }
 6447:         }
 6448:         if (keys(%changes) > 0) {
 6449:             $resulttext = &mt('Changes made:').'<ul>';
 6450:             if ($changes{'available'}) {
 6451:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6452:             }
 6453:             if ($changes{'localonly'}) {
 6454:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6455:             }
 6456: 
 6457:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6458:                 my $chgtext;
 6459:                 if (ref($usertypes) eq 'HASH') {
 6460:                     if (keys(%{$usertypes}) > 0) {
 6461:                         foreach my $type (@{$types}) {
 6462:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6463:                                 $chgtext .= $usertypes->{$type}.'; ';
 6464:                             }
 6465:                         }
 6466:                         if (grep(/^default$/,@cansearch)) {
 6467:                             $chgtext .= $othertitle;
 6468:                         } else {
 6469:                             $chgtext =~ s/\; $//;
 6470:                         }
 6471:                         $resulttext .=
 6472:                             '<li>'.
 6473:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 6474:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 6475:                             '</li>';
 6476:                     }
 6477:                 }
 6478:             }
 6479:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6480:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6481:                 my $chgtext;
 6482:                 foreach my $type (@{$titleorder}) {
 6483:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6484:                         if (defined($searchtitles->{$type})) {
 6485:                             $chgtext .= $searchtitles->{$type}.'; ';
 6486:                         }
 6487:                     }
 6488:                 }
 6489:                 $chgtext =~ s/\; $//;
 6490:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6491:             }
 6492:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6493:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6494:                 my $chgtext;
 6495:                 foreach my $type (@{$srchtypeorder}) {
 6496:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6497:                         if (defined($srchtypes_desc->{$type})) {
 6498:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6499:                         }
 6500:                     }
 6501:                 }
 6502:                 $chgtext =~ s/\; $//;
 6503:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 6504:             }
 6505:             $resulttext .= '</ul>';
 6506:         } else {
 6507:             $resulttext = &mt('No changes made to institution directory search settings');
 6508:         }
 6509:     } else {
 6510:         $resulttext = '<span class="LC_error">'.
 6511:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6512:     }
 6513:     return $resulttext;
 6514: }
 6515: 
 6516: sub modify_contacts {
 6517:     my ($dom,%domconfig) = @_;
 6518:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6519:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6520:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6521:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6522:         }
 6523:     }
 6524:     my (%others,%to,%bcc);
 6525:     my @contacts = ('supportemail','adminemail');
 6526:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6527:                     'requestsmail');
 6528:     foreach my $type (@mailings) {
 6529:         @{$newsetting{$type}} = 
 6530:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6531:         foreach my $item (@contacts) {
 6532:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6533:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6534:             } else {
 6535:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6536:             }
 6537:         }  
 6538:         $others{$type} = $env{'form.'.$type.'_others'};
 6539:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6540:         if ($type eq 'helpdeskmail') {
 6541:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6542:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6543:         }
 6544:     }
 6545:     foreach my $item (@contacts) {
 6546:         $to{$item} = $env{'form.'.$item};
 6547:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6548:     }
 6549:     if (keys(%currsetting) > 0) {
 6550:         foreach my $item (@contacts) {
 6551:             if ($to{$item} ne $currsetting{$item}) {
 6552:                 $changes{$item} = 1;
 6553:             }
 6554:         }
 6555:         foreach my $type (@mailings) {
 6556:             foreach my $item (@contacts) {
 6557:                 if (ref($currsetting{$type}) eq 'HASH') {
 6558:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6559:                         push(@{$changes{$type}},$item);
 6560:                     }
 6561:                 } else {
 6562:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6563:                 }
 6564:             }
 6565:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6566:                 push(@{$changes{$type}},'others');
 6567:             }
 6568:             if ($type eq 'helpdeskmail') {   
 6569:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6570:                     push(@{$changes{$type}},'bcc'); 
 6571:                 }
 6572:             }
 6573:         }
 6574:     } else {
 6575:         my %default;
 6576:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6577:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6578:         $default{'errormail'} = 'adminemail';
 6579:         $default{'packagesmail'} = 'adminemail';
 6580:         $default{'helpdeskmail'} = 'supportemail';
 6581:         $default{'lonstatusmail'} = 'adminemail';
 6582:         $default{'requestsmail'} = 'adminemail';
 6583:         foreach my $item (@contacts) {
 6584:            if ($to{$item} ne $default{$item}) {
 6585:               $changes{$item} = 1;
 6586:            } 
 6587:         }
 6588:         foreach my $type (@mailings) {
 6589:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6590:                
 6591:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6592:             }
 6593:             if ($others{$type} ne '') {
 6594:                 push(@{$changes{$type}},'others');
 6595:             }
 6596:             if ($type eq 'helpdeskmail') {
 6597:                 if ($bcc{$type} ne '') {
 6598:                     push(@{$changes{$type}},'bcc');
 6599:                 }
 6600:             }
 6601:         }
 6602:     }
 6603:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6604:                                              $dom);
 6605:     if ($putresult eq 'ok') {
 6606:         if (keys(%changes) > 0) {
 6607:             my ($titles,$short_titles)  = &contact_titles();
 6608:             $resulttext = &mt('Changes made:').'<ul>';
 6609:             foreach my $item (@contacts) {
 6610:                 if ($changes{$item}) {
 6611:                     $resulttext .= '<li>'.$titles->{$item}.
 6612:                                     &mt(' set to: ').
 6613:                                     '<span class="LC_cusr_emph">'.
 6614:                                     $to{$item}.'</span></li>';
 6615:                 }
 6616:             }
 6617:             foreach my $type (@mailings) {
 6618:                 if (ref($changes{$type}) eq 'ARRAY') {
 6619:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6620:                     my @text;
 6621:                     foreach my $item (@{$newsetting{$type}}) {
 6622:                         push(@text,$short_titles->{$item});
 6623:                     }
 6624:                     if ($others{$type} ne '') {
 6625:                         push(@text,$others{$type});
 6626:                     }
 6627:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6628:                                    join(', ',@text).'</span>';
 6629:                     if ($type eq 'helpdeskmail') {
 6630:                         if ($bcc{$type} ne '') {
 6631:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6632:                         }
 6633:                     }
 6634:                     $resulttext .= '</li>';
 6635:                 }
 6636:             }
 6637:             $resulttext .= '</ul>';
 6638:         } else {
 6639:             $resulttext = &mt('No changes made to contact information');
 6640:         }
 6641:     } else {
 6642:         $resulttext = '<span class="LC_error">'.
 6643:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6644:     }
 6645:     return $resulttext;
 6646: }
 6647: 
 6648: sub modify_usercreation {
 6649:     my ($dom,%domconfig) = @_;
 6650:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6651:     my $warningmsg;
 6652:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6653:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6654:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6655:         }
 6656:     }
 6657:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6658:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6659:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6660:     my @contexts = ('author','course','requestcrs','selfcreate');
 6661:     foreach my $item(@contexts) {
 6662:         if ($item eq 'selfcreate') {
 6663:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6664:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6665:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6666:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6667:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6668:                         $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.');   
 6669:                     }
 6670:                 }
 6671:             }
 6672:         } else {
 6673:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6674:         }
 6675:     }
 6676:     my ($othertitle,$usertypes,$types) = 
 6677:         &Apache::loncommon::sorted_inst_types($dom);
 6678:     if (ref($types) eq 'ARRAY') {
 6679:         if (@{$types} > 0) {
 6680:             @{$cancreate{'statustocreate'}} = 
 6681:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6682:         } else {
 6683:             @{$cancreate{'statustocreate'}} = ();
 6684:         }
 6685:         push(@contexts,'statustocreate');
 6686:     }
 6687:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6688:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6689:         foreach my $item (@contexts) {
 6690:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6691:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6692:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6693:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6694:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6695:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6696:                                     push(@{$changes{'cancreate'}},$item);
 6697:                                 }
 6698:                             }
 6699:                         }
 6700:                     }
 6701:                 } else {
 6702:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6703:                         if (@{$cancreate{$item}} > 0) {
 6704:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6705:                                 push(@{$changes{'cancreate'}},$item);
 6706:                             }
 6707:                         }
 6708:                     } else {
 6709:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6710:                             if (@{$cancreate{$item}} < 3) {
 6711:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6712:                                     push(@{$changes{'cancreate'}},$item);
 6713:                                 }
 6714:                             }
 6715:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6716:                             if (@{$cancreate{$item}} > 0) {
 6717:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6718:                                     push(@{$changes{'cancreate'}},$item);
 6719:                                 }
 6720:                             }
 6721:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6722:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6723:                                 push(@{$changes{'cancreate'}},$item);
 6724:                             }
 6725:                         }
 6726:                     }
 6727:                 }
 6728:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6729:                     foreach my $type (@{$cancreate{$item}}) {
 6730:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6731:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6732:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6733:                                     push(@{$changes{'cancreate'}},$item);
 6734:                                 }
 6735:                             }
 6736:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6737:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6738:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6739:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6740:                                     push(@{$changes{'cancreate'}},$item);
 6741:                                 }
 6742:                             }
 6743:                         }
 6744:                     }
 6745:                 }
 6746:             } else {
 6747:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6748:                     push(@{$changes{'cancreate'}},$item);
 6749:                 }
 6750:             }
 6751:         }
 6752:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6753:         foreach my $item (@contexts) {
 6754:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6755:                 if ($cancreate{$item} ne 'any') {
 6756:                     push(@{$changes{'cancreate'}},$item);
 6757:                 }
 6758:             } else {
 6759:                 if ($cancreate{$item} ne 'none') {
 6760:                     push(@{$changes{'cancreate'}},$item);
 6761:                 }
 6762:             }
 6763:         }
 6764:     } else {
 6765:         foreach my $item (@contexts)  {
 6766:             push(@{$changes{'cancreate'}},$item);
 6767:         }
 6768:     }
 6769: 
 6770:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6771:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6772:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6773:                 push(@{$changes{'username_rule'}},$type);
 6774:             }
 6775:         }
 6776:         foreach my $type (@username_rule) {
 6777:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6778:                 push(@{$changes{'username_rule'}},$type);
 6779:             }
 6780:         }
 6781:     } else {
 6782:         push(@{$changes{'username_rule'}},@username_rule);
 6783:     }
 6784: 
 6785:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6786:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 6787:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 6788:                 push(@{$changes{'id_rule'}},$type);
 6789:             }
 6790:         }
 6791:         foreach my $type (@id_rule) {
 6792:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 6793:                 push(@{$changes{'id_rule'}},$type);
 6794:             }
 6795:         }
 6796:     } else {
 6797:         push(@{$changes{'id_rule'}},@id_rule);
 6798:     }
 6799: 
 6800:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 6801:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 6802:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 6803:                 push(@{$changes{'email_rule'}},$type);
 6804:             }
 6805:         }
 6806:         foreach my $type (@email_rule) {
 6807:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 6808:                 push(@{$changes{'email_rule'}},$type);
 6809:             }
 6810:         }
 6811:     } else {
 6812:         push(@{$changes{'email_rule'}},@email_rule);
 6813:     }
 6814: 
 6815:     my @authen_contexts = ('author','course','domain');
 6816:     my @authtypes = ('int','krb4','krb5','loc');
 6817:     my %authhash;
 6818:     foreach my $item (@authen_contexts) {
 6819:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 6820:         foreach my $auth (@authtypes) {
 6821:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 6822:                 $authhash{$item}{$auth} = 1;
 6823:             } else {
 6824:                 $authhash{$item}{$auth} = 0;
 6825:             }
 6826:         }
 6827:     }
 6828:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 6829:         foreach my $item (@authen_contexts) {
 6830:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 6831:                 foreach my $auth (@authtypes) {
 6832:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 6833:                         push(@{$changes{'authtypes'}},$item);
 6834:                         last;
 6835:                     }
 6836:                 }
 6837:             }
 6838:         }
 6839:     } else {
 6840:         foreach my $item (@authen_contexts) {
 6841:             push(@{$changes{'authtypes'}},$item);
 6842:         }
 6843:     }
 6844: 
 6845:     my %usercreation_hash =  (
 6846:             usercreation => {
 6847:                               cancreate     => \%cancreate,
 6848:                               username_rule => \@username_rule,
 6849:                               id_rule       => \@id_rule,
 6850:                               email_rule    => \@email_rule,
 6851:                               authtypes     => \%authhash,
 6852:                             }
 6853:             );
 6854: 
 6855:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 6856:                                              $dom);
 6857: 
 6858:     my %selfcreatetypes = (
 6859:                              sso   => 'users authenticated by institutional single sign on',
 6860:                              login => 'users authenticated by institutional log-in',
 6861:                              email => 'users who provide a valid e-mail address for use as the username',
 6862:                           );
 6863:     if ($putresult eq 'ok') {
 6864:         if (keys(%changes) > 0) {
 6865:             $resulttext = &mt('Changes made:').'<ul>';
 6866:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 6867:                 my %lt = &usercreation_types();
 6868:                 foreach my $type (@{$changes{'cancreate'}}) {
 6869:                     my $chgtext;
 6870:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 6871:                         $chgtext = $lt{$type}.', ';
 6872:                     }
 6873:                     if ($type eq 'selfcreate') {
 6874:                         if (@{$cancreate{$type}} == 0) {
 6875:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 6876:                         } else {
 6877:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 6878:                             foreach my $case (@{$cancreate{$type}}) {
 6879:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 6880:                             }
 6881:                             $chgtext .= '</ul>';
 6882:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 6883:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 6884:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 6885:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 6886:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6887:                                         }
 6888:                                     }
 6889:                                 }
 6890:                             }
 6891:                         }
 6892:                     } elsif ($type eq 'statustocreate') {
 6893:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 6894:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 6895:                             if (@{$cancreate{'selfcreate'}} > 0) {
 6896:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 6897: 
 6898:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 6899:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 6900:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6901:                                     } 
 6902:                                 } elsif (ref($usertypes) eq 'HASH') {
 6903:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6904:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 6905:                                     } else {
 6906:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 6907:                                     }
 6908:                                     $chgtext .= '<ul>';
 6909:                                     foreach my $case (@{$cancreate{$type}}) {
 6910:                                         if ($case eq 'default') {
 6911:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 6912:                                         } else {
 6913:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 6914:                                         }
 6915:                                     }
 6916:                                     $chgtext .= '</ul>';
 6917:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6918:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 6919:                                     }
 6920:                                 }
 6921:                             } else {
 6922:                                 if (@{$cancreate{$type}} == 0) {
 6923:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 6924:                                 } else {
 6925:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 6926:                                 }
 6927:                             }
 6928:                         }
 6929:                     } elsif ($type eq 'captcha') {
 6930:                         if ($cancreate{$type} eq 'notused') {
 6931:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 6932:                         } else {
 6933:                             my %captchas = &captcha_phrases();
 6934:                             if ($captchas{$cancreate{$type}}) {
 6935:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 6936:                             } else {
 6937:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 6938:                             }
 6939:                         }
 6940:                     } elsif ($type eq 'recaptchakeys') {
 6941:                         my ($privkey,$pubkey);
 6942:                         if (ref($cancreate{$type}) eq 'HASH') {
 6943:                             $pubkey = $cancreate{$type}{'public'};
 6944:                             $privkey = $cancreate{$type}{'private'};
 6945:                         }
 6946:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 6947:                         if (!$pubkey) {
 6948:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 6949:                         } else {
 6950:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6951:                         }
 6952:                         if (!$privkey) {
 6953:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 6954:                         } else {
 6955:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6956:                         }
 6957:                         $chgtext .= '</ul>';
 6958:                     } else {
 6959:                         if ($cancreate{$type} eq 'none') {
 6960:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 6961:                         } elsif ($cancreate{$type} eq 'any') {
 6962:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 6963:                         } elsif ($cancreate{$type} eq 'official') {
 6964:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 6965:                         } elsif ($cancreate{$type} eq 'unofficial') {
 6966:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 6967:                         }
 6968:                     }
 6969:                     $resulttext .= '<li>'.$chgtext.'</li>';
 6970:                 }
 6971:             }
 6972:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 6973:                 my ($rules,$ruleorder) = 
 6974:                     &Apache::lonnet::inst_userrules($dom,'username');
 6975:                 my $chgtext = '<ul>';
 6976:                 foreach my $type (@username_rule) {
 6977:                     if (ref($rules->{$type}) eq 'HASH') {
 6978:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 6979:                     }
 6980:                 }
 6981:                 $chgtext .= '</ul>';
 6982:                 if (@username_rule > 0) {
 6983:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 6984:                 } else {
 6985:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 6986:                 }
 6987:             }
 6988:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 6989:                 my ($idrules,$idruleorder) = 
 6990:                     &Apache::lonnet::inst_userrules($dom,'id');
 6991:                 my $chgtext = '<ul>';
 6992:                 foreach my $type (@id_rule) {
 6993:                     if (ref($idrules->{$type}) eq 'HASH') {
 6994:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 6995:                     }
 6996:                 }
 6997:                 $chgtext .= '</ul>';
 6998:                 if (@id_rule > 0) {
 6999:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 7000:                 } else {
 7001:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 7002:                 }
 7003:             }
 7004:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 7005:                 my ($emailrules,$emailruleorder) =
 7006:                     &Apache::lonnet::inst_userrules($dom,'email');
 7007:                 my $chgtext = '<ul>';
 7008:                 foreach my $type (@email_rule) {
 7009:                     if (ref($emailrules->{$type}) eq 'HASH') {
 7010:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 7011:                     }
 7012:                 }
 7013:                 $chgtext .= '</ul>';
 7014:                 if (@email_rule > 0) {
 7015:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 7016:                 } else {
 7017:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 7018:                 }
 7019:             }
 7020: 
 7021:             my %authname = &authtype_names();
 7022:             my %context_title = &context_names();
 7023:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7024:                 my $chgtext = '<ul>';
 7025:                 foreach my $type (@{$changes{'authtypes'}}) {
 7026:                     my @allowed;
 7027:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7028:                     foreach my $auth (@authtypes) {
 7029:                         if ($authhash{$type}{$auth}) {
 7030:                             push(@allowed,$authname{$auth});
 7031:                         }
 7032:                     }
 7033:                     if (@allowed > 0) {
 7034:                         $chgtext .= join(', ',@allowed).'</li>';
 7035:                     } else {
 7036:                         $chgtext .= &mt('none').'</li>';
 7037:                     }
 7038:                 }
 7039:                 $chgtext .= '</ul>';
 7040:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7041:                 $resulttext .= '</li>';
 7042:             }
 7043:             $resulttext .= '</ul>';
 7044:         } else {
 7045:             $resulttext = &mt('No changes made to user creation settings');
 7046:         }
 7047:     } else {
 7048:         $resulttext = '<span class="LC_error">'.
 7049:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7050:     }
 7051:     if ($warningmsg ne '') {
 7052:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7053:     }
 7054:     return $resulttext;
 7055: }
 7056: 
 7057: sub process_captcha {
 7058:     my ($container,$changes,$newsettings,$current) = @_;
 7059:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 7060:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 7061:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 7062:         $newsettings->{'captcha'} = 'original';
 7063:     }
 7064:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 7065:         if ($container eq 'cancreate') {
 7066:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7067:                 push(@{$changes->{'cancreate'}},'captcha');
 7068:             } elsif (!defined($changes->{'cancreate'})) {
 7069:                 $changes->{'cancreate'} = ['captcha'];
 7070:             }
 7071:         } else {
 7072:             $changes->{'captcha'} = 1;
 7073:         }
 7074:     }
 7075:     my ($newpub,$newpriv,$currpub,$currpriv);
 7076:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 7077:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 7078:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 7079:         $newpub =~ s/\W//g;
 7080:         $newpriv =~ s/\W//g;
 7081:         $newsettings->{'recaptchakeys'} = {
 7082:                                              public  => $newpub,
 7083:                                              private => $newpriv,
 7084:                                           };
 7085:     }
 7086:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 7087:         $currpub = $current->{'recaptchakeys'}{'public'};
 7088:         $currpriv = $current->{'recaptchakeys'}{'private'};
 7089:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 7090:             $newsettings->{'recaptchakeys'} = {
 7091:                                                  public  => '',
 7092:                                                  private => '',
 7093:                                               }
 7094:         }
 7095:     }
 7096:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 7097:         if ($container eq 'cancreate') {
 7098:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7099:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 7100:             } elsif (!defined($changes->{'cancreate'})) {
 7101:                 $changes->{'cancreate'} = ['recaptchakeys'];
 7102:             }
 7103:         } else {
 7104:             $changes->{'recaptchakeys'} = 1;
 7105:         }
 7106:     }
 7107:     return;
 7108: }
 7109: 
 7110: sub modify_usermodification {
 7111:     my ($dom,%domconfig) = @_;
 7112:     my ($resulttext,%curr_usermodification,%changes);
 7113:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7114:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7115:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 7116:         }
 7117:     }
 7118:     my @contexts = ('author','course','selfcreate');
 7119:     my %context_title = (
 7120:                            author => 'In author context',
 7121:                            course => 'In course context',
 7122:                            selfcreate => 'When self creating account', 
 7123:                         );
 7124:     my @fields = ('lastname','firstname','middlename','generation',
 7125:                   'permanentemail','id');
 7126:     my %roles = (
 7127:                   author => ['ca','aa'],
 7128:                   course => ['st','ep','ta','in','cr'],
 7129:                 );
 7130:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7131:     if (ref($types) eq 'ARRAY') {
 7132:         push(@{$types},'default');
 7133:         $usertypes->{'default'} = $othertitle;
 7134:     }
 7135:     $roles{'selfcreate'} = $types;  
 7136:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7137:     my %modifyhash;
 7138:     foreach my $context (@contexts) {
 7139:         foreach my $role (@{$roles{$context}}) {
 7140:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7141:             foreach my $item (@fields) {
 7142:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7143:                     $modifyhash{$context}{$role}{$item} = 1;
 7144:                 } else {
 7145:                     $modifyhash{$context}{$role}{$item} = 0;
 7146:                 }
 7147:             }
 7148:         }
 7149:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7150:             foreach my $role (@{$roles{$context}}) {
 7151:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7152:                     foreach my $field (@fields) {
 7153:                         if ($modifyhash{$context}{$role}{$field} ne 
 7154:                                 $curr_usermodification{$context}{$role}{$field}) {
 7155:                             push(@{$changes{$context}},$role);
 7156:                             last;
 7157:                         }
 7158:                     }
 7159:                 }
 7160:             }
 7161:         } else {
 7162:             foreach my $context (@contexts) {
 7163:                 foreach my $role (@{$roles{$context}}) {
 7164:                     push(@{$changes{$context}},$role);
 7165:                 }
 7166:             }
 7167:         }
 7168:     }
 7169:     my %usermodification_hash =  (
 7170:                                    usermodification => \%modifyhash,
 7171:                                  );
 7172:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7173:                                              \%usermodification_hash,$dom);
 7174:     if ($putresult eq 'ok') {
 7175:         if (keys(%changes) > 0) {
 7176:             $resulttext = &mt('Changes made: ').'<ul>';
 7177:             foreach my $context (@contexts) {
 7178:                 if (ref($changes{$context}) eq 'ARRAY') {
 7179:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7180:                     if (ref($changes{$context}) eq 'ARRAY') {
 7181:                         foreach my $role (@{$changes{$context}}) {
 7182:                             my $rolename;
 7183:                             if ($context eq 'selfcreate') {
 7184:                                 $rolename = $role;
 7185:                                 if (ref($usertypes) eq 'HASH') {
 7186:                                     if ($usertypes->{$role} ne '') {
 7187:                                         $rolename = $usertypes->{$role};
 7188:                                     }
 7189:                                 }
 7190:                             } else {
 7191:                                 if ($role eq 'cr') {
 7192:                                     $rolename = &mt('Custom');
 7193:                                 } else {
 7194:                                     $rolename = &Apache::lonnet::plaintext($role);
 7195:                                 }
 7196:                             }
 7197:                             my @modifiable;
 7198:                             if ($context eq 'selfcreate') {
 7199:                                 $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): ');
 7200:                             } else {
 7201:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7202:                             }
 7203:                             foreach my $field (@fields) {
 7204:                                 if ($modifyhash{$context}{$role}{$field}) {
 7205:                                     push(@modifiable,$fieldtitles{$field});
 7206:                                 }
 7207:                             }
 7208:                             if (@modifiable > 0) {
 7209:                                 $resulttext .= join(', ',@modifiable);
 7210:                             } else {
 7211:                                 $resulttext .= &mt('none'); 
 7212:                             }
 7213:                             $resulttext .= '</li>';
 7214:                         }
 7215:                         $resulttext .= '</ul></li>';
 7216:                     }
 7217:                 }
 7218:             }
 7219:             $resulttext .= '</ul>';
 7220:         } else {
 7221:             $resulttext = &mt('No changes made to user modification settings');
 7222:         }
 7223:     } else {
 7224:         $resulttext = '<span class="LC_error">'.
 7225:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7226:     }
 7227:     return $resulttext;
 7228: }
 7229: 
 7230: sub modify_defaults {
 7231:     my ($dom,$r) = @_;
 7232:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7233:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7234:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7235:     my @authtypes = ('internal','krb4','krb5','localauth');
 7236:     foreach my $item (@items) {
 7237:         $newvalues{$item} = $env{'form.'.$item};
 7238:         if ($item eq 'auth_def') {
 7239:             if ($newvalues{$item} ne '') {
 7240:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7241:                     push(@errors,$item);
 7242:                 }
 7243:             }
 7244:         } elsif ($item eq 'lang_def') {
 7245:             if ($newvalues{$item} ne '') {
 7246:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7247:                     my $langcode = $1;
 7248:                     if ($langcode ne 'x_chef') {
 7249:                         if (code2language($langcode) eq '') {
 7250:                             push(@errors,$item);
 7251:                         }
 7252:                     }
 7253:                 } else {
 7254:                     push(@errors,$item);
 7255:                 }
 7256:             }
 7257:         } elsif ($item eq 'timezone_def') {
 7258:             if ($newvalues{$item} ne '') {
 7259:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7260:                     push(@errors,$item);   
 7261:                 }
 7262:             }
 7263:         } elsif ($item eq 'datelocale_def') {
 7264:             if ($newvalues{$item} ne '') {
 7265:                 my @datelocale_ids = DateTime::Locale->ids();
 7266:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7267:                     push(@errors,$item);
 7268:                 }
 7269:             }
 7270:         } elsif ($item eq 'portal_def') {
 7271:             if ($newvalues{$item} ne '') {
 7272:                 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])\/?$/) {
 7273:                     push(@errors,$item);
 7274:                 }
 7275:             }
 7276:         }
 7277:         if (grep(/^\Q$item\E$/,@errors)) {
 7278:             $newvalues{$item} = $domdefaults{$item};
 7279:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7280:             $changes{$item} = 1;
 7281:         }
 7282:         $domdefaults{$item} = $newvalues{$item};
 7283:     }
 7284:     my %defaults_hash = (
 7285:                          defaults => \%newvalues,
 7286:                         );
 7287:     my $title = &defaults_titles();
 7288:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7289:                                              $dom);
 7290:     if ($putresult eq 'ok') {
 7291:         if (keys(%changes) > 0) {
 7292:             $resulttext = &mt('Changes made:').'<ul>';
 7293:             my $version = $r->dir_config('lonVersion');
 7294:             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";
 7295:             foreach my $item (sort(keys(%changes))) {
 7296:                 my $value = $env{'form.'.$item};
 7297:                 if ($value eq '') {
 7298:                     $value = &mt('none');
 7299:                 } elsif ($item eq 'auth_def') {
 7300:                     my %authnames = &authtype_names();
 7301:                     my %shortauth = (
 7302:                              internal => 'int',
 7303:                              krb4 => 'krb4',
 7304:                              krb5 => 'krb5',
 7305:                              localauth  => 'loc',
 7306:                     );
 7307:                     $value = $authnames{$shortauth{$value}};
 7308:                 }
 7309:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7310:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7311:             }
 7312:             $resulttext .= '</ul>';
 7313:             $mailmsgtext .= "\n";
 7314:             my $cachetime = 24*60*60;
 7315:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7316:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7317:                 my $sysmail = $r->dir_config('lonSysEMail');
 7318:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 7319:             }
 7320:         } else {
 7321:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 7322:         }
 7323:     } else {
 7324:         $resulttext = '<span class="LC_error">'.
 7325:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7326:     }
 7327:     if (@errors > 0) {
 7328:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7329:         foreach my $item (@errors) {
 7330:             $resulttext .= ' "'.$title->{$item}.'",';
 7331:         }
 7332:         $resulttext =~ s/,$//;
 7333:     }
 7334:     return $resulttext;
 7335: }
 7336: 
 7337: sub modify_scantron {
 7338:     my ($r,$dom,$confname,%domconfig) = @_;
 7339:     my ($resulttext,%confhash,%changes,$errors);
 7340:     my $custom = 'custom.tab';
 7341:     my $default = 'default.tab';
 7342:     my $servadm = $r->dir_config('lonAdmEMail');
 7343:     my ($configuserok,$author_ok,$switchserver) = 
 7344:         &config_check($dom,$confname,$servadm);
 7345:     if ($env{'form.scantronformat.filename'} ne '') {
 7346:         my $error;
 7347:         if ($configuserok eq 'ok') {
 7348:             if ($switchserver) {
 7349:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7350:             } else {
 7351:                 if ($author_ok eq 'ok') {
 7352:                     my ($result,$scantronurl) =
 7353:                         &publishlogo($r,'upload','scantronformat',$dom,
 7354:                                      $confname,'scantron','','',$custom);
 7355:                     if ($result eq 'ok') {
 7356:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7357:                         $changes{'scantronformat'} = 1;
 7358:                     } else {
 7359:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7360:                     }
 7361:                 } else {
 7362:                     $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);
 7363:                 }
 7364:             }
 7365:         } else {
 7366:             $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);
 7367:         }
 7368:         if ($error) {
 7369:             &Apache::lonnet::logthis($error);
 7370:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7371:         }
 7372:     }
 7373:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7374:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7375:             if ($env{'form.scantronformat_del'}) {
 7376:                 $confhash{'scantron'}{'scantronformat'} = '';
 7377:                 $changes{'scantronformat'} = 1;
 7378:             }
 7379:         }
 7380:     }
 7381:     if (keys(%confhash) > 0) {
 7382:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7383:                                                  $dom);
 7384:         if ($putresult eq 'ok') {
 7385:             if (keys(%changes) > 0) {
 7386:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7387:                     $resulttext = &mt('Changes made:').'<ul>';
 7388:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7389:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7390:                     } else {
 7391:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7392:                     }
 7393:                     $resulttext .= '</ul>';
 7394:                 } else {
 7395:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7396:                 }
 7397:                 $resulttext .= '</ul>';
 7398:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7399:             } else {
 7400:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7401:             }
 7402:         } else {
 7403:             $resulttext = '<span class="LC_error">'.
 7404:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7405:         }
 7406:     } else {
 7407:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7408:     }
 7409:     if ($errors) {
 7410:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7411:                        $errors.'</ul>';
 7412:     }
 7413:     return $resulttext;
 7414: }
 7415: 
 7416: sub modify_coursecategories {
 7417:     my ($dom,%domconfig) = @_;
 7418:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7419:         $cathash);
 7420:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7421:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7422:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7423:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7424:             $changes{'togglecats'} = 1;
 7425:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7426:         }
 7427:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7428:             $changes{'categorize'} = 1;
 7429:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7430:         }
 7431:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7432:             $changes{'togglecatscomm'} = 1;
 7433:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7434:         }
 7435:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7436:             $changes{'categorizecomm'} = 1;
 7437:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7438:         }
 7439:     } else {
 7440:         $changes{'togglecats'} = 1;
 7441:         $changes{'categorize'} = 1;
 7442:         $changes{'togglecatscomm'} = 1;
 7443:         $changes{'categorizecomm'} = 1;
 7444:         $domconfig{'coursecategories'} = {
 7445:                                              togglecats => $env{'form.togglecats'},
 7446:                                              categorize => $env{'form.categorize'},
 7447:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7448:                                              categorizecomm => $env{'form.categorizecomm'},
 7449:                                          };
 7450:     }
 7451:     if (ref($cathash) eq 'HASH') {
 7452:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7453:             push (@deletecategory,'instcode::0');
 7454:         }
 7455:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7456:             push(@deletecategory,'communities::0');
 7457:         }
 7458:     }
 7459:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7460:     if (ref($cathash) eq 'HASH') {
 7461:         if (@deletecategory > 0) {
 7462:             #FIXME Need to remove category from all courses using a deleted category 
 7463:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7464:             foreach my $item (@deletecategory) {
 7465:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7466:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7467:                     $deletions{$item} = 1;
 7468:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7469:                 }
 7470:             }
 7471:         }
 7472:         foreach my $item (keys(%{$cathash})) {
 7473:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7474:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7475:                 $reorderings{$item} = 1;
 7476:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7477:             }
 7478:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7479:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7480:                 my $newdepth = $depth+1;
 7481:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7482:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7483:                 $adds{$newitem} = 1; 
 7484:             }
 7485:             if ($env{'form.subcat_'.$item} ne '') {
 7486:                 my $newcat = $env{'form.subcat_'.$item};
 7487:                 my $newdepth = $depth+1;
 7488:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7489:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7490:                 $adds{$newitem} = 1;
 7491:             }
 7492:         }
 7493:     }
 7494:     if ($env{'form.instcode'} eq '1') {
 7495:         if (ref($cathash) eq 'HASH') {
 7496:             my $newitem = 'instcode::0';
 7497:             if ($cathash->{$newitem} eq '') {  
 7498:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7499:                 $adds{$newitem} = 1;
 7500:             }
 7501:         } else {
 7502:             my $newitem = 'instcode::0';
 7503:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7504:             $adds{$newitem} = 1;
 7505:         }
 7506:     }
 7507:     if ($env{'form.communities'} eq '1') {
 7508:         if (ref($cathash) eq 'HASH') {
 7509:             my $newitem = 'communities::0';
 7510:             if ($cathash->{$newitem} eq '') {
 7511:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7512:                 $adds{$newitem} = 1;
 7513:             }
 7514:         } else {
 7515:             my $newitem = 'communities::0';
 7516:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7517:             $adds{$newitem} = 1;
 7518:         }
 7519:     }
 7520:     if ($env{'form.addcategory_name'} ne '') {
 7521:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7522:             ($env{'form.addcategory_name'} ne 'communities')) {
 7523:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7524:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7525:             $adds{$newitem} = 1;
 7526:         }
 7527:     }
 7528:     my $putresult;
 7529:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7530:         if (keys(%deletions) > 0) {
 7531:             foreach my $key (keys(%deletions)) {
 7532:                 if ($predelallitems{$key} ne '') {
 7533:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7534:                 }
 7535:             }
 7536:         }
 7537:         my (@chkcats,@chktrails,%chkallitems);
 7538:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7539:         if (ref($chkcats[0]) eq 'ARRAY') {
 7540:             my $depth = 0;
 7541:             my $chg = 0;
 7542:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7543:                 my $name = $chkcats[0][$i];
 7544:                 my $item;
 7545:                 if ($name eq '') {
 7546:                     $chg ++;
 7547:                 } else {
 7548:                     $item = &escape($name).'::0';
 7549:                     if ($chg) {
 7550:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7551:                     }
 7552:                     $depth ++; 
 7553:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7554:                     $depth --;
 7555:                 }
 7556:             }
 7557:         }
 7558:     }
 7559:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7560:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7561:         if ($putresult eq 'ok') {
 7562:             my %title = (
 7563:                          togglecats     => 'Show/Hide a course in catalog',
 7564:                          categorize     => 'Assign a category to a course',
 7565:                          togglecatscomm => 'Show/Hide a community in catalog',
 7566:                          categorizecomm => 'Assign a category to a community',
 7567:                         );
 7568:             my %level = (
 7569:                          dom  => 'set in Domain ("Modify Course/Community")',
 7570:                          crs  => 'set in Course ("Course Configuration")',
 7571:                          comm => 'set in Community ("Community Configuration")',
 7572:                         );
 7573:             $resulttext = &mt('Changes made:').'<ul>';
 7574:             if ($changes{'togglecats'}) {
 7575:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7576:             }
 7577:             if ($changes{'categorize'}) {
 7578:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7579:             }
 7580:             if ($changes{'togglecatscomm'}) {
 7581:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7582:             }
 7583:             if ($changes{'categorizecomm'}) {
 7584:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7585:             }
 7586:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7587:                 my $cathash;
 7588:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7589:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7590:                 } else {
 7591:                     $cathash = {};
 7592:                 } 
 7593:                 my (@cats,@trails,%allitems);
 7594:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7595:                 if (keys(%deletions) > 0) {
 7596:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7597:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7598:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7599:                     }
 7600:                     $resulttext .= '</ul></li>';
 7601:                 }
 7602:                 if (keys(%reorderings) > 0) {
 7603:                     my %sort_by_trail;
 7604:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7605:                     foreach my $key (keys(%reorderings)) {
 7606:                         if ($allitems{$key} ne '') {
 7607:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7608:                         }
 7609:                     }
 7610:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7611:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7612:                     }
 7613:                     $resulttext .= '</ul></li>';
 7614:                 }
 7615:                 if (keys(%adds) > 0) {
 7616:                     my %sort_by_trail;
 7617:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7618:                     foreach my $key (keys(%adds)) {
 7619:                         if ($allitems{$key} ne '') {
 7620:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7621:                         }
 7622:                     }
 7623:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7624:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7625:                     }
 7626:                     $resulttext .= '</ul></li>';
 7627:                 }
 7628:             }
 7629:             $resulttext .= '</ul>';
 7630:         } else {
 7631:             $resulttext = '<span class="LC_error">'.
 7632:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7633:         }
 7634:     } else {
 7635:         $resulttext = &mt('No changes made to course and community categories');
 7636:     }
 7637:     return $resulttext;
 7638: }
 7639: 
 7640: sub modify_serverstatuses {
 7641:     my ($dom,%domconfig) = @_;
 7642:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7643:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7644:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7645:     }
 7646:     my @pages = &serverstatus_pages();
 7647:     foreach my $type (@pages) {
 7648:         $newserverstatus{$type}{'namedusers'} = '';
 7649:         $newserverstatus{$type}{'machines'} = '';
 7650:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7651:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7652:             my @okusers;
 7653:             foreach my $user (@users) {
 7654:                 my ($uname,$udom) = split(/:/,$user);
 7655:                 if (($udom =~ /^$match_domain$/) &&   
 7656:                     (&Apache::lonnet::domain($udom)) &&
 7657:                     ($uname =~ /^$match_username$/)) {
 7658:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7659:                         push(@okusers,$user);
 7660:                     }
 7661:                 }
 7662:             }
 7663:             if (@okusers > 0) {
 7664:                  @okusers = sort(@okusers);
 7665:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7666:             }
 7667:         }
 7668:         if (defined($env{'form.'.$type.'_machines'})) {
 7669:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7670:             my @okmachines;
 7671:             foreach my $ip (@machines) {
 7672:                 my @parts = split(/\./,$ip);
 7673:                 next if (@parts < 4);
 7674:                 my $badip = 0;
 7675:                 for (my $i=0; $i<4; $i++) {
 7676:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7677:                         $badip = 1;
 7678:                         last;
 7679:                     }
 7680:                 }
 7681:                 if (!$badip) {
 7682:                     push(@okmachines,$ip);     
 7683:                 }
 7684:             }
 7685:             @okmachines = sort(@okmachines);
 7686:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7687:         }
 7688:     }
 7689:     my %serverstatushash =  (
 7690:                                 serverstatuses => \%newserverstatus,
 7691:                             );
 7692:     foreach my $type (@pages) {
 7693:         foreach my $setting ('namedusers','machines') {
 7694:             my (@current,@new);
 7695:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7696:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7697:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7698:                 }
 7699:             }
 7700:             if ($newserverstatus{$type}{$setting} ne '') {
 7701:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7702:             }
 7703:             if (@current > 0) {
 7704:                 if (@new > 0) {
 7705:                     foreach my $item (@current) {
 7706:                         if (!grep(/^\Q$item\E$/,@new)) {
 7707:                             $changes{$type}{$setting} = 1;
 7708:                             last;
 7709:                         }
 7710:                     }
 7711:                     foreach my $item (@new) {
 7712:                         if (!grep(/^\Q$item\E$/,@current)) {
 7713:                             $changes{$type}{$setting} = 1;
 7714:                             last;
 7715:                         }
 7716:                     }
 7717:                 } else {
 7718:                     $changes{$type}{$setting} = 1;
 7719:                 }
 7720:             } elsif (@new > 0) {
 7721:                 $changes{$type}{$setting} = 1;
 7722:             }
 7723:         }
 7724:     }
 7725:     if (keys(%changes) > 0) {
 7726:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7727:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7728:                                                  \%serverstatushash,$dom);
 7729:         if ($putresult eq 'ok') {
 7730:             $resulttext .= &mt('Changes made:').'<ul>';
 7731:             foreach my $type (@pages) {
 7732:                 if (ref($changes{$type}) eq 'HASH') {
 7733:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7734:                     if ($changes{$type}{'namedusers'}) {
 7735:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7736:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7737:                         } else {
 7738:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7739:                         }
 7740:                     }
 7741:                     if ($changes{$type}{'machines'}) {
 7742:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7743:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7744:                         } else {
 7745:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7746:                         }
 7747: 
 7748:                     }
 7749:                     $resulttext .= '</ul></li>';
 7750:                 }
 7751:             }
 7752:             $resulttext .= '</ul>';
 7753:         } else {
 7754:             $resulttext = '<span class="LC_error">'.
 7755:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7756: 
 7757:         }
 7758:     } else {
 7759:         $resulttext = &mt('No changes made to access to server status pages');
 7760:     }
 7761:     return $resulttext;
 7762: }
 7763: 
 7764: sub modify_helpsettings {
 7765:     my ($r,$dom,$confname,%domconfig) = @_;
 7766:     my ($resulttext,$errors,%changes,%helphash);
 7767:     my %defaultchecked = ('submitbugs' => 'on');
 7768:     my @offon = ('off','on');
 7769:     my @toggles = ('submitbugs');
 7770:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7771:         foreach my $item (@toggles) {
 7772:             if ($defaultchecked{$item} eq 'on') { 
 7773:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7774:                     if ($env{'form.'.$item} eq '0') {
 7775:                         $changes{$item} = 1;
 7776:                     }
 7777:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7778:                     $changes{$item} = 1;
 7779:                 }
 7780:             } elsif ($defaultchecked{$item} eq 'off') {
 7781:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7782:                     if ($env{'form.'.$item} eq '1') {
 7783:                         $changes{$item} = 1;
 7784:                     }
 7785:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7786:                     $changes{$item} = 1;
 7787:                 }
 7788: 	    }
 7789:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 7790:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7791:             }
 7792:         }
 7793:     }
 7794:     my $putresult;
 7795:     if (keys(%changes) > 0) {
 7796:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7797:         if ($putresult eq 'ok') {
 7798:             $resulttext = &mt('Changes made:').'<ul>';
 7799:             foreach my $item (sort(keys(%changes))) {
 7800:                 if ($item eq 'submitbugs') {
 7801:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 7802:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 7803:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 7804:                 }
 7805:             }
 7806:             $resulttext .= '</ul>';
 7807:         } else {
 7808:             $resulttext = &mt('No changes made to help settings');
 7809:             $errors .= '<li><span class="LC_error">'.
 7810:                        &mt('An error occurred storing the settings: [_1]',
 7811:                            $putresult).'</span></li>';
 7812:         }
 7813:     }
 7814:     if ($errors) {
 7815:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7816:                        $errors.'</ul>';
 7817:     }
 7818:     return $resulttext;
 7819: }
 7820: 
 7821: sub modify_coursedefaults {
 7822:     my ($dom,%domconfig) = @_;
 7823:     my ($resulttext,$errors,%changes,%defaultshash);
 7824:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7825:     my @offon = ('off','on');
 7826:     my @toggles = ('canuse_pdfforms');
 7827: 
 7828:     $defaultshash{'coursedefaults'} = {};
 7829: 
 7830:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7831:         if ($domconfig{'coursedefaults'} eq '') {
 7832:             $domconfig{'coursedefaults'} = {};
 7833:         }
 7834:     }
 7835: 
 7836:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7837:         foreach my $item (@toggles) {
 7838:             if ($defaultchecked{$item} eq 'on') {
 7839:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7840:                     ($env{'form.'.$item} eq '0')) {
 7841:                     $changes{$item} = 1;
 7842:                 } elsif ($domconfig{'coursdefaults'}{$item} ne $env{'form.'.$item}) {
 7843:                     $changes{$item} = 1;
 7844:                 }
 7845:             } elsif ($defaultchecked{$item} eq 'off') {
 7846:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7847:                     ($env{'form.'.$item} eq '1')) {
 7848:                     $changes{$item} = 1;
 7849:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7850:                     $changes{$item} = 1;
 7851:                 }
 7852:             }
 7853:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7854:         }
 7855:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7856:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7857:         $newdefresponder =~ s/\D//g;
 7858:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7859:             $newdefresponder = 1;
 7860:         }
 7861:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7862:         if ($currdefresponder ne $newdefresponder) {
 7863:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7864:                 $changes{'anonsurvey_threshold'} = 1;
 7865:             }
 7866:         }
 7867:     }
 7868:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7869:                                              $dom);
 7870:     if ($putresult eq 'ok') {
 7871:         if (keys(%changes) > 0) {
 7872:             if ($changes{'canuse_pdfforms'}) {
 7873:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7874:                 $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7875:                 my $cachetime = 24*60*60;
 7876:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7877:             }
 7878:             $resulttext = &mt('Changes made:').'<ul>';
 7879:             foreach my $item (sort(keys(%changes))) {
 7880:                 if ($item eq 'canuse_pdfforms') {
 7881:                     if ($env{'form.'.$item} eq '1') {
 7882:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7883:                     } else {
 7884:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7885:                     }
 7886:                 } elsif ($item eq 'anonsurvey_threshold') {
 7887:                         $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7888:                 }
 7889:             }
 7890:             $resulttext .= '</ul>';
 7891:         } else {
 7892:             $resulttext = &mt('No changes made to course defaults');
 7893:         }
 7894:     } else {
 7895:         $resulttext = '<span class="LC_error">'.
 7896:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7897:     }
 7898:     return $resulttext;
 7899: }
 7900: 
 7901: sub modify_usersessions {
 7902:     my ($dom,%domconfig) = @_;
 7903:     my @hostingtypes = ('version','excludedomain','includedomain');
 7904:     my @offloadtypes = ('primary','default');
 7905:     my %types = (
 7906:                   remote => \@hostingtypes,
 7907:                   hosted => \@hostingtypes,
 7908:                   spares => \@offloadtypes,
 7909:                 );
 7910:     my @prefixes = ('remote','hosted','spares');
 7911:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7912:     my (%by_ip,%by_location,@intdoms);
 7913:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7914:     my @locations = sort(keys(%by_location));
 7915:     my (%defaultshash,%changes);
 7916:     foreach my $prefix (@prefixes) {
 7917:         $defaultshash{'usersessions'}{$prefix} = {};
 7918:     }
 7919:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7920:     my $resulttext;
 7921:     my %iphost = &Apache::lonnet::get_iphost();
 7922:     foreach my $prefix (@prefixes) {
 7923:         next if ($prefix eq 'spares');
 7924:         foreach my $type (@{$types{$prefix}}) {
 7925:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7926:             if ($type eq 'version') {
 7927:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7928:                 my $okvalue;
 7929:                 if ($value ne '') {
 7930:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 7931:                         $okvalue = $value;
 7932:                     }
 7933:                 }
 7934:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7935:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7936:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 7937:                             if ($inuse == 0) {
 7938:                                 $changes{$prefix}{$type} = 1;
 7939:                             } else {
 7940:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 7941:                                     $changes{$prefix}{$type} = 1;
 7942:                                 }
 7943:                                 if ($okvalue ne '') {
 7944:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7945:                                 } 
 7946:                             }
 7947:                         } else {
 7948:                             if (($inuse == 1) && ($okvalue ne '')) {
 7949:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7950:                                 $changes{$prefix}{$type} = 1;
 7951:                             }
 7952:                         }
 7953:                     } else {
 7954:                         if (($inuse == 1) && ($okvalue ne '')) {
 7955:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7956:                             $changes{$prefix}{$type} = 1;
 7957:                         }
 7958:                     }
 7959:                 } else {
 7960:                     if (($inuse == 1) && ($okvalue ne '')) {
 7961:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7962:                         $changes{$prefix}{$type} = 1;
 7963:                     }
 7964:                 }
 7965:             } else {
 7966:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 7967:                 my @okvals;
 7968:                 foreach my $val (@vals) {
 7969:                     if ($val =~ /:/) {
 7970:                         my @items = split(/:/,$val);
 7971:                         foreach my $item (@items) {
 7972:                             if (ref($by_location{$item}) eq 'ARRAY') {
 7973:                                 push(@okvals,$item);
 7974:                             }
 7975:                         }
 7976:                     } else {
 7977:                         if (ref($by_location{$val}) eq 'ARRAY') {
 7978:                             push(@okvals,$val);
 7979:                         }
 7980:                     }
 7981:                 }
 7982:                 @okvals = sort(@okvals);
 7983:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7984:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7985:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7986:                             if ($inuse == 0) {
 7987:                                 $changes{$prefix}{$type} = 1; 
 7988:                             } else {
 7989:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7990:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 7991:                                 if (@changed > 0) {
 7992:                                     $changes{$prefix}{$type} = 1;
 7993:                                 }
 7994:                             }
 7995:                         } else {
 7996:                             if ($inuse == 1) {
 7997:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7998:                                 $changes{$prefix}{$type} = 1;
 7999:                             }
 8000:                         } 
 8001:                     } else {
 8002:                         if ($inuse == 1) {
 8003:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8004:                             $changes{$prefix}{$type} = 1;
 8005:                         }
 8006:                     }
 8007:                 } else {
 8008:                     if ($inuse == 1) {
 8009:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8010:                         $changes{$prefix}{$type} = 1;
 8011:                     }
 8012:                 }
 8013:             }
 8014:         }
 8015:     }
 8016: 
 8017:     my @alldoms = &Apache::lonnet::all_domains();
 8018:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8019:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 8020:     my $savespares;
 8021: 
 8022:     foreach my $lonhost (sort(keys(%servers))) {
 8023:         my $serverhomeID =
 8024:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 8025:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 8026:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 8027:         my %spareschg;
 8028:         foreach my $type (@{$types{'spares'}}) {
 8029:             my @okspares;
 8030:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 8031:             foreach my $server (@checked) {
 8032:                 if (&Apache::lonnet::hostname($server) ne '') {
 8033:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 8034:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 8035:                             push(@okspares,$server);
 8036:                         }
 8037:                     }
 8038:                 }
 8039:             }
 8040:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 8041:             my $newspare;
 8042:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 8043:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 8044:                     $newspare = $new;
 8045:                 }
 8046:             }
 8047:             my @spares;
 8048:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 8049:                 @spares = sort(@okspares,$newspare);
 8050:             } else {
 8051:                 @spares = sort(@okspares);
 8052:             }
 8053:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 8054:             if (ref($spareid{$lonhost}) eq 'HASH') {
 8055:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 8056:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 8057:                     if (@diffs > 0) {
 8058:                         $spareschg{$type} = 1;
 8059:                     }
 8060:                 }
 8061:             }
 8062:         }
 8063:         if (keys(%spareschg) > 0) {
 8064:             $changes{'spares'}{$lonhost} = \%spareschg;
 8065:         }
 8066:     }
 8067: 
 8068:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8069:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 8070:             if (ref($changes{'spares'}) eq 'HASH') {
 8071:                 if (keys(%{$changes{'spares'}}) > 0) {
 8072:                     $savespares = 1;
 8073:                 }
 8074:             }
 8075:         } else {
 8076:             $savespares = 1;
 8077:         }
 8078:     }
 8079: 
 8080:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 8081:     if ((keys(%changes) > 0) || ($savespares)) {
 8082:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8083:                                                  $dom);
 8084:         if ($putresult eq 'ok') {
 8085:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8086:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 8087:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 8088:                 }
 8089:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 8090:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 8091:                 }
 8092:             }
 8093:             my $cachetime = 24*60*60;
 8094:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8095:             if (keys(%changes) > 0) {
 8096:                 my %lt = &usersession_titles();
 8097:                 $resulttext = &mt('Changes made:').'<ul>';
 8098:                 foreach my $prefix (@prefixes) {
 8099:                     if (ref($changes{$prefix}) eq 'HASH') {
 8100:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 8101:                         if ($prefix eq 'spares') {
 8102:                             if (ref($changes{$prefix}) eq 'HASH') {
 8103:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 8104:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 8105:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 8106:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 8107:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 8108:                                         foreach my $type (@{$types{$prefix}}) {
 8109:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 8110:                                                 my $offloadto = &mt('None');
 8111:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 8112:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 8113:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 8114:                                                     }
 8115:                                                 }
 8116:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 8117:                                             }
 8118:                                         }
 8119:                                     }
 8120:                                     $resulttext .= '</li>';
 8121:                                 }
 8122:                             }
 8123:                         } else {
 8124:                             foreach my $type (@{$types{$prefix}}) {
 8125:                                 if (defined($changes{$prefix}{$type})) {
 8126:                                     my $newvalue;
 8127:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8128:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 8129:                                             if ($type eq 'version') {
 8130:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 8131:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8132:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 8133:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 8134:                                                 }
 8135:                                             }
 8136:                                         }
 8137:                                     }
 8138:                                     if ($newvalue eq '') {
 8139:                                         if ($type eq 'version') {
 8140:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8141:                                         } else {
 8142:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8143:                                         }
 8144:                                     } else {
 8145:                                         if ($type eq 'version') {
 8146:                                             $newvalue .= ' '.&mt('(or later)'); 
 8147:                                         }
 8148:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8149:                                     }
 8150:                                 }
 8151:                             }
 8152:                         }
 8153:                         $resulttext .= '</ul>';
 8154:                     }
 8155:                 }
 8156:                 $resulttext .= '</ul>';
 8157:             } else {
 8158:                 $resulttext = $nochgmsg;
 8159:             }
 8160:         } else {
 8161:             $resulttext = '<span class="LC_error">'.
 8162:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8163:         }
 8164:     } else {
 8165:         $resulttext = $nochgmsg;
 8166:     }
 8167:     return $resulttext;
 8168: }
 8169: 
 8170: sub modify_loadbalancing {
 8171:     my ($dom,%domconfig) = @_;
 8172:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8173:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8174:     my ($othertitle,$usertypes,$types) =
 8175:         &Apache::loncommon::sorted_inst_types($dom);
 8176:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8177:     my @sparestypes = ('primary','default');
 8178:     my %typetitles = &sparestype_titles();
 8179:     my $resulttext;
 8180:     my (%currbalancer,%currtargets,%currrules,%existing);
 8181:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8182:         %existing = %{$domconfig{'loadbalancing'}};
 8183:     }
 8184:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8185:                               \%currtargets,\%currrules);
 8186:     my ($saveloadbalancing,%defaultshash,%changes);
 8187:     my ($alltypes,$othertypes,$titles) =
 8188:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8189:     my %ruletitles = &offloadtype_text();
 8190:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 8191:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 8192:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 8193:         if ($balancer eq '') {
 8194:             next;
 8195:         }
 8196:         if (!exists($servers{$balancer})) {
 8197:             if (exists($currbalancer{$balancer})) {
 8198:                 push(@{$changes{'delete'}},$balancer);
 8199:             }
 8200:             next;
 8201:         }
 8202:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 8203:             push(@{$changes{'delete'}},$balancer);
 8204:             next;
 8205:         }
 8206:         if (!exists($currbalancer{$balancer})) {
 8207:             push(@{$changes{'add'}},$balancer);
 8208:         }
 8209:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 8210:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 8211:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 8212:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8213:             $saveloadbalancing = 1;
 8214:         }
 8215:         foreach my $sparetype (@sparestypes) {
 8216:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 8217:             my @offloadto;
 8218:             foreach my $target (@targets) {
 8219:                 if (($servers{$target}) && ($target ne $balancer)) {
 8220:                     if ($sparetype eq 'default') {
 8221:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 8222:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 8223:                         }
 8224:                     }
 8225:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 8226:                         push(@offloadto,$target);
 8227:                     }
 8228:                 }
 8229:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 8230:             }
 8231:         }
 8232:         if (ref($currtargets{$balancer}) eq 'HASH') {
 8233:             foreach my $sparetype (@sparestypes) {
 8234:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 8235:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 8236:                     if (@targetdiffs > 0) {
 8237:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8238:                     }
 8239:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8240:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8241:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8242:                     }
 8243:                 }
 8244:             }
 8245:         } else {
 8246:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 8247:                 foreach my $sparetype (@sparestypes) {
 8248:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8249:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8250:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 8251:                         }
 8252:                     }
 8253:                 }
 8254:             }
 8255:         }
 8256:         my $ishomedom;
 8257:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 8258:             $ishomedom = 1;
 8259:         }
 8260:         if (ref($alltypes) eq 'ARRAY') {
 8261:             foreach my $type (@{$alltypes}) {
 8262:                 my $rule;
 8263:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
 8264:                          (!$ishomedom)) {
 8265:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 8266:                 }
 8267:                 if ($rule eq 'specific') {
 8268:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 8269:                 }
 8270:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 8271:                 if (ref($currrules{$balancer}) eq 'HASH') {
 8272:                     if ($rule ne $currrules{$balancer}{$type}) {
 8273:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8274:                     }
 8275:                 } elsif ($rule ne '') {
 8276:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8277:                 }
 8278:             }
 8279:         }
 8280:     }
 8281:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8282:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8283:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 8284:             $defaultshash{'loadbalancing'} = {};
 8285:         }
 8286:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8287:                                                  \%defaultshash,$dom);
 8288: 
 8289:         if ($putresult eq 'ok') {
 8290:             if (keys(%changes) > 0) {
 8291:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 8292:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 8293:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 8294:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8295:                     }
 8296:                 }
 8297:                 if (ref($changes{'add'}) eq 'ARRAY') {
 8298:                     foreach my $balancer (sort(@{$changes{'add'}})) {
 8299:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 8300:                     }
 8301:                 }
 8302:                 if (ref($changes{'curr'}) eq 'HASH') {
 8303:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 8304:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 8305:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 8306:                                 my %offloadstr;
 8307:                                 foreach my $sparetype (@sparestypes) {
 8308:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8309:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8310:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 8311:                                         }
 8312:                                     }
 8313:                                 }
 8314:                                 if (keys(%offloadstr) == 0) {
 8315:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8316:                                 } else {
 8317:                                     my $showoffload;
 8318:                                     foreach my $sparetype (@sparestypes) {
 8319:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8320:                                         if (defined($offloadstr{$sparetype})) {
 8321:                                             $showoffload .= $offloadstr{$sparetype};
 8322:                                         } else {
 8323:                                             $showoffload .= &mt('None');
 8324:                                         }
 8325:                                         $showoffload .= ('&nbsp;'x3);
 8326:                                     }
 8327:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 8328:                                 }
 8329:                             }
 8330:                         }
 8331:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 8332:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8333:                                 foreach my $type (@{$alltypes}) {
 8334:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 8335:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 8336:                                         my $balancetext;
 8337:                                         if ($rule eq '') {
 8338:                                             $balancetext =  $ruletitles{'default'};
 8339:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 8340:                                             $balancetext =  $ruletitles{$rule};
 8341:                                         } else {
 8342:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 8343:                                         }
 8344:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>'; 
 8345:                                     }
 8346:                                 }
 8347:                             }
 8348:                         }
 8349:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8350:                     }
 8351:                 }
 8352:                 if ($resulttext ne '') {
 8353:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8354:                 } else {
 8355:                     $resulttext = $nochgmsg;
 8356:                 }
 8357:             } else {
 8358:                 $resulttext = $nochgmsg;
 8359:             }
 8360:         } else {
 8361:             $resulttext = '<span class="LC_error">'.
 8362:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8363:         }
 8364:     } else {
 8365:         $resulttext = $nochgmsg;
 8366:     }
 8367:     return $resulttext;
 8368: }
 8369: 
 8370: sub recurse_check {
 8371:     my ($chkcats,$categories,$depth,$name) = @_;
 8372:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8373:         my $chg = 0;
 8374:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8375:             my $category = $chkcats->[$depth]{$name}[$j];
 8376:             my $item;
 8377:             if ($category eq '') {
 8378:                 $chg ++;
 8379:             } else {
 8380:                 my $deeper = $depth + 1;
 8381:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8382:                 if ($chg) {
 8383:                     $categories->{$item} -= $chg;
 8384:                 }
 8385:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8386:                 $deeper --;
 8387:             }
 8388:         }
 8389:     }
 8390:     return;
 8391: }
 8392: 
 8393: sub recurse_cat_deletes {
 8394:     my ($item,$coursecategories,$deletions) = @_;
 8395:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8396:     my $subdepth = $depth + 1;
 8397:     if (ref($coursecategories) eq 'HASH') {
 8398:         foreach my $subitem (keys(%{$coursecategories})) {
 8399:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8400:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8401:                 delete($coursecategories->{$subitem});
 8402:                 $deletions->{$subitem} = 1;
 8403:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8404:             }  
 8405:         }
 8406:     }
 8407:     return;
 8408: }
 8409: 
 8410: sub get_active_dcs {
 8411:     my ($dom) = @_;
 8412:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc']);
 8413:     my %domcoords;
 8414:     my $numdcs = 0;
 8415:     my $now = time;
 8416:     foreach my $server (keys(%dompersonnel)) {
 8417:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8418:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8419:             my ($end,$start) = split(':',$dompersonnel{$server}{$user});
 8420:             if (($end eq '') || ($end == 0) || ($end > $now)) {
 8421:                 if ($start <= $now) {
 8422:                     $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8423:                 }
 8424:             }
 8425:         }
 8426:     }
 8427:     return %domcoords;
 8428: }
 8429: 
 8430: sub active_dc_picker {
 8431:     my ($dom,$curr_dc) = @_;
 8432:     my %domcoords = &get_active_dcs($dom); 
 8433:     my @dcs = sort(keys(%domcoords));
 8434:     my $numdcs = scalar(@dcs); 
 8435:     my $datatable;
 8436:     my $numinrow = 2;
 8437:     if ($numdcs > 1) {
 8438:         $datatable = '<table>';
 8439:         for (my $i=0; $i<@dcs; $i++) {
 8440:             my $rem = $i%($numinrow);
 8441:             if ($rem == 0) {
 8442:                 if ($i > 0) {
 8443:                     $datatable .= '</tr>';
 8444:                 }
 8445:                 $datatable .= '<tr>';
 8446:             }
 8447:             my $check = ' ';
 8448:             if ($curr_dc eq '') {
 8449:                 if (!$i) { 
 8450:                     $check = ' checked="checked" ';
 8451:                 }
 8452:             } elsif ($dcs[$i] eq $curr_dc) {
 8453:                 $check = ' checked="checked" ';
 8454:             }
 8455:             if ($i == @dcs - 1) {
 8456:                 my $colsleft = $numinrow - $rem;
 8457:                 if ($colsleft > 1) {
 8458:                     $datatable .= '<td colspan="'.$colsleft.'">';
 8459:                 } else {
 8460:                     $datatable .= '<td>';
 8461:                 }
 8462:             } else {
 8463:                 $datatable .= '<td>';
 8464:             }
 8465:             my ($dcname,$dcdom) = split(':',$dcs[$i]);
 8466:             $datatable .= '<span class="LC_nobreak"><label>'.
 8467:                           '<input type="radio" name="autocreate_xmldc"'.
 8468:                           ' value="'.$dcs[$i].'"'.$check.'/>'.
 8469:                           &Apache::loncommon::plainname($dcname,$dcdom).
 8470:                           ' ('.$dcname.':'.$dcdom.')'.
 8471:                           '</label></span></td>';
 8472:         }
 8473:         $datatable .= '</tr></table>';
 8474:     } elsif (@dcs) {
 8475:         $datatable .= '<input type="hidden" name="autocreate_dc" value="'.
 8476:                       $dcs[0].'" />';
 8477:     }
 8478:     return ($numdcs,$datatable);
 8479: }
 8480: 
 8481: sub usersession_titles {
 8482:     return &Apache::lonlocal::texthash(
 8483:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8484:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8485:                spares => 'Servers offloaded to, when busy',
 8486:                version => 'LON-CAPA version requirement',
 8487:                excludedomain => 'Allow all, but exclude specific domains',
 8488:                includedomain => 'Deny all, but include specific domains',
 8489:                primary => 'Primary (checked first)',
 8490:                default => 'Default',
 8491:            );
 8492: }
 8493: 
 8494: sub id_for_thisdom {
 8495:     my (%servers) = @_;
 8496:     my %altids;
 8497:     foreach my $server (keys(%servers)) {
 8498:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8499:         if ($serverhome ne $server) {
 8500:             $altids{$serverhome} = $server;
 8501:         }
 8502:     }
 8503:     return %altids;
 8504: }
 8505: 
 8506: sub count_servers {
 8507:     my ($currbalancer,%servers) = @_;
 8508:     my (@spares,$numspares);
 8509:     foreach my $lonhost (sort(keys(%servers))) {
 8510:         next if ($currbalancer eq $lonhost);
 8511:         push(@spares,$lonhost);
 8512:     }
 8513:     if ($currbalancer) {
 8514:         $numspares = scalar(@spares);
 8515:     } else {
 8516:         $numspares = scalar(@spares) - 1;
 8517:     }
 8518:     return ($numspares,@spares);
 8519: }
 8520: 
 8521: sub lonbalance_targets_js {
 8522:     my ($dom,$types,$servers,$settings) = @_;
 8523:     my $select = &mt('Select');
 8524:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8525:     if (ref($servers) eq 'HASH') {
 8526:         $alltargets = join("','",sort(keys(%{$servers})));
 8527:         my @homedoms;
 8528:         foreach my $server (sort(keys(%{$servers}))) {
 8529:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8530:                 push(@homedoms,'1');
 8531:             } else {
 8532:                 push(@homedoms,'0');
 8533:             }
 8534:         }
 8535:         $allishome = join("','",@homedoms);
 8536:     }
 8537:     if (ref($types) eq 'ARRAY') {
 8538:         if (@{$types} > 0) {
 8539:             @alltypes = @{$types};
 8540:         }
 8541:     }
 8542:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8543:     $allinsttypes = join("','",@alltypes);
 8544:     my (%currbalancer,%currtargets,%currrules,%existing);
 8545:     if (ref($settings) eq 'HASH') {
 8546:         %existing = %{$settings};
 8547:     }
 8548:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 8549:                               \%currtargets,\%currrules);
 8550:     my $balancers = join("','",sort(keys(%currbalancer)));
 8551:     return <<"END";
 8552: 
 8553: <script type="text/javascript">
 8554: // <![CDATA[
 8555: 
 8556: currBalancers = new Array('$balancers');
 8557: 
 8558: function toggleTargets(balnum) {
 8559:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8560:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 8561:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 8562:     var prevbalancer = prevhostitem.value;
 8563:     var baltotal = document.getElementById('loadbalancing_total').value;
 8564:     prevhostitem.value = balancer;
 8565:     if (prevbalancer != '') {
 8566:         var prevIdx = currBalancers.indexOf(prevbalancer);
 8567:         if (prevIdx != -1) {
 8568:             currBalancers.splice(prevIdx,1);
 8569:         }
 8570:     }
 8571:     if (balancer == '') {
 8572:         hideSpares(balnum);
 8573:     } else {
 8574:         var currIdx = currBalancers.indexOf(balancer);
 8575:         if (currIdx == -1) {
 8576:             currBalancers.push(balancer);
 8577:         }
 8578:         var homedoms = new Array('$allishome');
 8579:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 8580:         showSpares(balancer,ishomedom,balnum);
 8581:     }
 8582:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 8583:     return;
 8584: }
 8585: 
 8586: function showSpares(balancer,ishomedom,balnum) {
 8587:     var alltargets = new Array('$alltargets');
 8588:     var insttypes = new Array('$allinsttypes');
 8589:     var offloadtypes = new Array('primary','default');
 8590: 
 8591:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 8592:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 8593:  
 8594:     for (var i=0; i<offloadtypes.length; i++) {
 8595:         var count = 0;
 8596:         for (var j=0; j<alltargets.length; j++) {
 8597:             if (alltargets[j] != balancer) {
 8598:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 8599:                 item.value = alltargets[j];
 8600:                 item.style.textAlign='left';
 8601:                 item.style.textFace='normal';
 8602:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8603:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 8604:                     item.disabled = '';
 8605:                 } else {
 8606:                     item.disabled = 'disabled';
 8607:                     item.checked = false;
 8608:                 }
 8609:                 count ++;
 8610:             }
 8611:         }
 8612:     }
 8613:     for (var k=0; k<insttypes.length; k++) {
 8614:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8615:             if (ishomedom == 1) {
 8616:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8617:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8618:             } else {
 8619:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8620:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8621: 
 8622:             }
 8623:         } else {
 8624:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8625:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8626:         }
 8627:         if ((insttypes[k] != '_LC_external') && 
 8628:             ((insttypes[k] != '_LC_internetdom') ||
 8629:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8630:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 8631:             item.options.length = 0;
 8632:             item.options[0] = new Option("","",true,true);
 8633:             var idx = 0;
 8634:             for (var m=0; m<alltargets.length; m++) {
 8635:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 8636:                     idx ++;
 8637:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8638: 
 8639:                 }
 8640:             }
 8641:         }
 8642:     }
 8643:     return;
 8644: }
 8645: 
 8646: function hideSpares(balnum) {
 8647:     var alltargets = new Array('$alltargets');
 8648:     var insttypes = new Array('$allinsttypes');
 8649:     var offloadtypes = new Array('primary','default');
 8650: 
 8651:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 8652:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 8653: 
 8654:     var total = alltargets.length - 1;
 8655:     for (var i=0; i<offloadtypes; i++) {
 8656:         for (var j=0; j<total; j++) {
 8657:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 8658:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 8659:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8660:         }
 8661:     }
 8662:     for (var k=0; k<insttypes.length; k++) {
 8663:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8664:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8665:         if (insttypes[k] != '_LC_external') {
 8666:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 8667:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 8668:         }
 8669:     }
 8670:     return;
 8671: }
 8672: 
 8673: function checkOffloads(item,balnum,type) {
 8674:     var alltargets = new Array('$alltargets');
 8675:     var offloadtypes = new Array('primary','default');
 8676:     if (item.checked) {
 8677:         var total = alltargets.length - 1;
 8678:         var other;
 8679:         if (type == offloadtypes[0]) {
 8680:             other = offloadtypes[1];
 8681:         } else {
 8682:             other = offloadtypes[0];
 8683:         }
 8684:         for (var i=0; i<total; i++) {
 8685:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 8686:             if (server == item.value) {
 8687:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 8688:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 8689:                 }
 8690:             }
 8691:         }
 8692:     }
 8693:     return;
 8694: }
 8695: 
 8696: function singleServerToggle(balnum,type) {
 8697:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 8698:     if (offloadtoSelIdx == 0) {
 8699:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 8700:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8701: 
 8702:     } else {
 8703:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 8704:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8705:     }
 8706:     return;
 8707: }
 8708: 
 8709: function balanceruleChange(formname,balnum,type) {
 8710:     if (type == '_LC_external') {
 8711:         return; 
 8712:     }
 8713:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 8714:     for (var i=0; i<typesRules.length; i++) {
 8715:         if (formname.elements[typesRules[i]].checked) {
 8716:             if (formname.elements[typesRules[i]].value != 'specific') {
 8717:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 8718:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8719:             } else {
 8720:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8721:             }
 8722:         }
 8723:     }
 8724:     return;
 8725: }
 8726: 
 8727: function balancerDeleteChange(balnum) {
 8728:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8729:     var baltotal = document.getElementById('loadbalancing_total').value;
 8730:     var addtarget;
 8731:     var removetarget;
 8732:     var action = 'delete';
 8733:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 8734:         var lonhost = hostitem.value;
 8735:         var currIdx = currBalancers.indexOf(lonhost);
 8736:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 8737:             if (currIdx != -1) {
 8738:                 currBalancers.splice(currIdx,1);
 8739:             }
 8740:             addtarget = lonhost;
 8741:         } else {
 8742:             if (currIdx == -1) {
 8743:                 currBalancers.push(lonhost);
 8744:             }
 8745:             removetarget = lonhost;
 8746:             action = 'undelete';
 8747:         }
 8748:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 8749:     }
 8750:     return;
 8751: }
 8752: 
 8753: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 8754:     if (baltotal > 1) {
 8755:         var offloadtypes = new Array('primary','default');
 8756:         var alltargets = new Array('$alltargets');
 8757:         var insttypes = new Array('$allinsttypes');
 8758:         for (var i=0; i<baltotal; i++) {
 8759:             if (i != balnum) {
 8760:                 for (var j=0; j<offloadtypes.length; j++) {
 8761:                     var total = alltargets.length - 1;
 8762:                     for (var k=0; k<total; k++) {
 8763:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 8764:                         var server = serveritem.value;
 8765:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 8766:                             if (server == addtarget) {
 8767:                                 serveritem.disabled = '';
 8768:                             }
 8769:                         }
 8770:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8771:                             if (server == removetarget) {
 8772:                                 serveritem.disabled = 'disabled';
 8773:                                 serveritem.checked = false;
 8774:                             }
 8775:                         }
 8776:                     }
 8777:                 }
 8778:                 for (var j=0; j<insttypes.length; j++) {
 8779:                     if (insttypes[j] != '_LC_external') {
 8780:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 8781:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 8782:                             var currSel = singleserver.selectedIndex;
 8783:                             var currVal = singleserver.options[currSel].value;
 8784:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 8785:                                 var numoptions = singleserver.options.length;
 8786:                                 var needsnew = 1;
 8787:                                 for (var k=0; k<numoptions; k++) {
 8788:                                     if (singleserver.options[k] == addtarget) {
 8789:                                         needsnew = 0;
 8790:                                         break;
 8791:                                     }
 8792:                                 }
 8793:                                 if (needsnew == 1) {
 8794:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 8795:                                 }
 8796:                             }
 8797:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8798:                                 singleserver.options.length = 0;
 8799:                                 if ((currVal) && (currVal != removetarget)) {
 8800:                                     singleserver.options[0] = new Option("","",false,false);
 8801:                                 } else {
 8802:                                     singleserver.options[0] = new Option("","",true,true);
 8803:                                 }
 8804:                                 var idx = 0;
 8805:                                 for (var m=0; m<alltargets.length; m++) {
 8806:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
 8807:                                         idx ++;
 8808:                                         if (currVal == alltargets[m]) {
 8809:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
 8810:                                         } else {
 8811:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8812:                                         }
 8813:                                     }
 8814:                                 }
 8815:                             }
 8816:                         }
 8817:                     }
 8818:                 }
 8819:             }
 8820:         }
 8821:     }
 8822:     return;
 8823: }
 8824: 
 8825: // ]]>
 8826: </script>
 8827: 
 8828: END
 8829: }
 8830: 
 8831: sub new_spares_js {
 8832:     my @sparestypes = ('primary','default');
 8833:     my $types = join("','",@sparestypes);
 8834:     my $select = &mt('Select');
 8835:     return <<"END";
 8836: 
 8837: <script type="text/javascript">
 8838: // <![CDATA[
 8839: 
 8840: function updateNewSpares(formname,lonhost) {
 8841:     var types = new Array('$types');
 8842:     var include = new Array();
 8843:     var exclude = new Array();
 8844:     for (var i=0; i<types.length; i++) {
 8845:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8846:         for (var j=0; j<spareboxes.length; j++) {
 8847:             if (formname.elements[spareboxes[j]].checked) {
 8848:                 exclude.push(formname.elements[spareboxes[j]].value);
 8849:             } else {
 8850:                 include.push(formname.elements[spareboxes[j]].value);
 8851:             }
 8852:         }
 8853:     }
 8854:     for (var i=0; i<types.length; i++) {
 8855:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8856:         var selIdx = newSpare.selectedIndex;
 8857:         var currnew = newSpare.options[selIdx].value;
 8858:         var okSpares = new Array();
 8859:         for (var j=0; j<newSpare.options.length; j++) {
 8860:             var possible = newSpare.options[j].value;
 8861:             if (possible != '') {
 8862:                 if (exclude.indexOf(possible) == -1) {
 8863:                     okSpares.push(possible);
 8864:                 } else {
 8865:                     if (currnew == possible) {
 8866:                         selIdx = 0;
 8867:                     }
 8868:                 }
 8869:             }
 8870:         }
 8871:         for (var k=0; k<include.length; k++) {
 8872:             if (okSpares.indexOf(include[k]) == -1) {
 8873:                 okSpares.push(include[k]);
 8874:             }
 8875:         }
 8876:         okSpares.sort();
 8877:         newSpare.options.length = 0;
 8878:         if (selIdx == 0) {
 8879:             newSpare.options[0] = new Option("$select","",true,true);
 8880:         } else {
 8881:             newSpare.options[0] = new Option("$select","",false,false);
 8882:         }
 8883:         for (var m=0; m<okSpares.length; m++) {
 8884:             var idx = m+1;
 8885:             var selThis = 0;
 8886:             if (selIdx != 0) {
 8887:                 if (okSpares[m] == currnew) {
 8888:                     selThis = 1;
 8889:                 }
 8890:             }
 8891:             if (selThis == 1) {
 8892:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8893:             } else {
 8894:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8895:             }
 8896:         }
 8897:     }
 8898:     return;
 8899: }
 8900: 
 8901: function checkNewSpares(lonhost,type) {
 8902:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8903:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8904:     if (chosen != '') { 
 8905:         var othertype;
 8906:         var othernewSpare;
 8907:         if (type == 'primary') {
 8908:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 8909:         }
 8910:         if (type == 'default') {
 8911:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 8912:         }
 8913:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 8914:             othernewSpare.selectedIndex = 0;
 8915:         }
 8916:     }
 8917:     return;
 8918: }
 8919: 
 8920: // ]]>
 8921: </script>
 8922: 
 8923: END
 8924: 
 8925: }
 8926: 
 8927: sub common_domprefs_js {
 8928:     return <<"END";
 8929: 
 8930: <script type="text/javascript">
 8931: // <![CDATA[
 8932: 
 8933: function getIndicesByName(formname,item) {
 8934:     var group = new Array();
 8935:     for (var i=0;i<formname.elements.length;i++) {
 8936:         if (formname.elements[i].name == item) {
 8937:             group.push(formname.elements[i].id);
 8938:         }
 8939:     }
 8940:     return group;
 8941: }
 8942: 
 8943: // ]]>
 8944: </script>
 8945: 
 8946: END
 8947: 
 8948: }
 8949: 
 8950: sub recaptcha_js {
 8951:     my %lt = &captcha_phrases();
 8952:     return <<"END";
 8953: 
 8954: <script type="text/javascript">
 8955: // <![CDATA[
 8956: 
 8957: function updateCaptcha(caller,context) {
 8958:     var privitem;
 8959:     var pubitem;
 8960:     var privtext;
 8961:     var pubtext;
 8962:     if (document.getElementById(context+'_recaptchapub')) {
 8963:         pubitem = document.getElementById(context+'_recaptchapub');
 8964:     } else {
 8965:         return;
 8966:     }
 8967:     if (document.getElementById(context+'_recaptchapriv')) {
 8968:         privitem = document.getElementById(context+'_recaptchapriv');
 8969:     } else {
 8970:         return;
 8971:     }
 8972:     if (document.getElementById(context+'_recaptchapubtxt')) {
 8973:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 8974:     } else {
 8975:         return;
 8976:     }
 8977:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 8978:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 8979:     } else {
 8980:         return;
 8981:     }
 8982:     if (caller.checked) {
 8983:         if (caller.value == 'recaptcha') {
 8984:             pubitem.type = 'text';
 8985:             privitem.type = 'text';
 8986:             pubitem.size = '40';
 8987:             privitem.size = '40';
 8988:             pubtext.innerHTML = "$lt{'pub'}";
 8989:             privtext.innerHTML = "$lt{'priv'}";
 8990:         } else {
 8991:             pubitem.type = 'hidden';
 8992:             privitem.type = 'hidden';
 8993:             pubtext.innerHTML = '';
 8994:             privtext.innerHTML = '';
 8995:         }
 8996:     }
 8997:     return;
 8998: }
 8999: 
 9000: // ]]>
 9001: </script>
 9002: 
 9003: END
 9004: 
 9005: }
 9006: 
 9007: sub captcha_phrases {
 9008:     return &Apache::lonlocal::texthash (
 9009:                  priv => 'Private key',
 9010:                  pub  => 'Public key',
 9011:                  original  => 'original (CAPTCHA)',
 9012:                  recaptcha => 'successor (ReCAPTCHA)',
 9013:                  notused   => 'unused',
 9014:     );
 9015: }
 9016: 
 9017: 1;

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