File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.191: download - view: text, annotated - select for diffs
Mon Feb 25 17:43:41 2013 UTC (11 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Make &active_dc_picker() more generic so it can be used to generate a table
  of active Domain Coordinators either as radio button items (pick 1 of N),
  or as checkboxes (pick M of N).
- Eliminate some code by including start and end args in call to
  lonnet::get_domain_roles in &get_active_dcs()
- Eliminate duplicate code in &print_requestmail() with &print_requestmail()
  call.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.191 2013/02/25 17:43:41 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','helpsettings',
  214:                 'coursedefaults','usersessions','loadbalancing',
  215:                 'requestauthor'],$dom);
  216:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  217:                        'autoupdate','autocreate','directorysrch','contacts',
  218:                        'usercreation','usermodification','scantron',
  219:                        'requestcourses','requestauthor','coursecategories',
  220:                        'serverstatuses','helpsettings',
  221:                        'coursedefaults','usersessions');
  222:     my %existing;
  223:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  224:         %existing = %{$domconfig{'loadbalancing'}};
  225:     }
  226:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  227:         push(@prefs_order,'loadbalancing');
  228:     }
  229:     my %prefs = (
  230:         'rolecolors' =>
  231:                    { text => 'Default color schemes',
  232:                      help => 'Domain_Configuration_Color_Schemes',
  233:                      header => [{col1 => 'Student Settings',
  234:                                  col2 => '',},
  235:                                 {col1 => 'Coordinator Settings',
  236:                                  col2 => '',},
  237:                                 {col1 => 'Author Settings',
  238:                                  col2 => '',},
  239:                                 {col1 => 'Administrator Settings',
  240:                                  col2 => '',}],
  241:                     },
  242:         'login' =>
  243:                     { text => 'Log-in page options',
  244:                       help => 'Domain_Configuration_Login_Page',
  245:                       header => [{col1 => 'Log-in Page Items',
  246:                                   col2 => '',},
  247:                                  {col1 => 'Log-in Help',
  248:                                   col2 => 'Value'}],
  249:                     },
  250:         'defaults' => 
  251:                     { text => 'Default authentication/language/timezone/portal',
  252:                       help => 'Domain_Configuration_LangTZAuth',
  253:                       header => [{col1 => 'Setting',
  254:                                   col2 => 'Value'}],
  255:                     },
  256:         'quotas' => 
  257:                     { text => 'Blogs, personal web pages, webDAV, portfolios',
  258:                       help => 'Domain_Configuration_Quotas',
  259:                       header => [{col1 => 'User affiliation',
  260:                                   col2 => 'Available tools',
  261:                                   col3 => 'Portfolio quota',}],
  262:                     },
  263:         'autoenroll' =>
  264:                    { text => 'Auto-enrollment settings',
  265:                      help => 'Domain_Configuration_Auto_Enrollment',
  266:                      header => [{col1 => 'Configuration setting',
  267:                                  col2 => 'Value(s)'}],
  268:                    },
  269:         'autoupdate' => 
  270:                    { text => 'Auto-update settings',
  271:                      help => 'Domain_Configuration_Auto_Updates',
  272:                      header => [{col1 => 'Setting',
  273:                                  col2 => 'Value',},
  274:                                 {col1 => 'Setting',
  275:                                  col2 => 'Affiliation'},
  276:                                 {col1 => 'User population',
  277:                                  col2 => 'Updateable user data'}],
  278:                   },
  279:         'autocreate' => 
  280:                   { text => 'Auto-course creation settings',
  281:                      help => 'Domain_Configuration_Auto_Creation',
  282:                      header => [{col1 => 'Configuration Setting',
  283:                                  col2 => 'Value',}],
  284:                   },
  285:         'directorysrch' => 
  286:                   { text => 'Institutional directory searches',
  287:                     help => 'Domain_Configuration_InstDirectory_Search',
  288:                     header => [{col1 => 'Setting',
  289:                                 col2 => 'Value',}],
  290:                   },
  291:         'contacts' =>
  292:                   { text => 'Contact Information',
  293:                     help => 'Domain_Configuration_Contact_Info',
  294:                     header => [{col1 => 'Setting',
  295:                                 col2 => 'Value',}],
  296:                   },
  297: 
  298:         'usercreation' => 
  299:                   { text => 'User creation',
  300:                     help => 'Domain_Configuration_User_Creation',
  301:                     header => [{col1 => 'Format rule type',
  302:                                 col2 => 'Format rules in force'},
  303:                                {col1 => 'User account creation',
  304:                                 col2 => 'Usernames which may be created',},
  305:                                {col1 => 'Context',
  306:                                 col2 => 'Assignable authentication types'}],
  307:                   },
  308:         'usermodification' =>
  309:                   { text => 'User modification',
  310:                     help => 'Domain_Configuration_User_Modification',
  311:                     header => [{col1 => 'Target user has role',
  312:                                 col2 => 'User information updateable in author context'},
  313:                                {col1 => 'Target user has role',
  314:                                 col2 => 'User information updateable in course context'},
  315:                                {col1 => "Status of user",
  316:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  317:                   },
  318:         'scantron' =>
  319:                   { text => 'Bubblesheet format file',
  320:                     help => 'Domain_Configuration_Scantron_Format',
  321:                     header => [ {col1 => 'Item',
  322:                                  col2 => '',
  323:                               }],
  324:                   },
  325:         'requestcourses' => 
  326:                  {text => 'Request creation of courses',
  327:                   help => 'Domain_Configuration_Request_Courses',
  328:                   header => [{col1 => 'User affiliation',
  329:                               col2 => 'Availability/Processing of requests',},
  330:                              {col1 => 'Setting',
  331:                               col2 => 'Value'}],
  332:                  },
  333:         'requestauthor' =>
  334:                  {text => 'Request authoring space',
  335:                   help => 'Domain_Configuration_Request_Author',
  336:                   header => [{col1 => 'User affiliation',
  337:                               col2 => 'Availability/Processing of requests',},
  338:                              {col1 => 'Setting',
  339:                               col2 => 'Value'}],
  340:                  },
  341:         'coursecategories' =>
  342:                   { text => 'Cataloging of courses/communities',
  343:                     help => 'Domain_Configuration_Cataloging_Courses',
  344:                     header => [{col1 => 'Category settings',
  345:                                 col2 => '',},
  346:                                {col1 => 'Categories',
  347:                                 col2 => '',
  348:                                }],
  349:                   },
  350:         'serverstatuses' =>
  351:                  {text   => 'Access to server status pages',
  352:                   help   => 'Domain_Configuration_Server_Status',
  353:                   header => [{col1 => 'Status Page',
  354:                               col2 => 'Other named users',
  355:                               col3 => 'Specific IPs',
  356:                             }],
  357:                  },
  358:         'helpsettings' =>
  359:                  {text   => 'Help page settings',
  360:                   help   => 'Domain_Configuration_Help_Settings',
  361:                   header => [{col1 => 'Help Settings (logged-in users)',
  362:                               col2 => 'Value'}],
  363:                  },
  364:         'coursedefaults' => 
  365:                  {text => 'Course/Community defaults',
  366:                   help => 'Domain_Configuration_Course_Defaults',
  367:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  368:                               col2 => 'Value',},
  369:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  370:                               col2 => 'Value',},],
  371:                  },
  372:         'privacy' => 
  373:                  {text   => 'User Privacy',
  374:                   help   => 'Domain_Configuration_User_Privacy',
  375:                   header => [{col1 => 'Setting',
  376:                               col2 => 'Value',}],
  377:                  },
  378:         'usersessions' =>
  379:                  {text  => 'User session hosting/offloading',
  380:                   help  => 'Domain_Configuration_User_Sessions',
  381:                   header => [{col1 => 'Domain server',
  382:                               col2 => 'Servers to offload sessions to when busy'},
  383:                              {col1 => 'Hosting of users from other domains',
  384:                               col2 => 'Rules'},
  385:                              {col1 => "Hosting domain's own users elsewhere",
  386:                               col2 => 'Rules'}],
  387:                  },
  388:          'loadbalancing' =>
  389:                  {text  => 'Dedicated Load Balancer(s)',
  390:                   help  => 'Domain_Configuration_Load_Balancing',
  391:                   header => [{col1 => 'Balancers',
  392:                               col2 => 'Default destinations',
  393:                               col3 => 'User affiliation',
  394:                               col4 => 'Overrides'},
  395:                             ],
  396:                  },
  397:     );
  398:     if (keys(%servers) > 1) {
  399:         $prefs{'login'}  = { text   => 'Log-in page options',
  400:                              help   => 'Domain_Configuration_Login_Page',
  401:                             header => [{col1 => 'Log-in Service',
  402:                                         col2 => 'Server Setting',},
  403:                                        {col1 => 'Log-in Page Items',
  404:                                         col2 => ''},
  405:                                        {col1 => 'Log-in Help',
  406:                                         col2 => 'Value'}],
  407:                            };
  408:     }
  409: 
  410:     my @roles = ('student','coordinator','author','admin');
  411:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  412:     &Apache::lonhtmlcommon::add_breadcrumb
  413:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  414:       text=>"Settings to display/modify"});
  415:     my $confname = $dom.'-domainconfig';
  416: 
  417:     if ($phase eq 'process') {
  418:         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
  419:     } elsif ($phase eq 'display') {
  420:         my $js = &recaptcha_js();
  421:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  422:             my ($othertitle,$usertypes,$types) =
  423:                 &Apache::loncommon::sorted_inst_types($dom);
  424:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  425:                                           $domconfig{'loadbalancing'}).
  426:                    &new_spares_js().
  427:                    &common_domprefs_js().
  428:                    &Apache::loncommon::javascript_array_indexof();
  429:         }
  430:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  431:     } else {
  432: # check if domconfig user exists for the domain.
  433:         my $servadm = $r->dir_config('lonAdmEMail');
  434:         my ($configuserok,$author_ok,$switchserver) =
  435:             &config_check($dom,$confname,$servadm);
  436:         unless ($configuserok eq 'ok') {
  437:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  438:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  439:                            $confname).
  440:                       '<br />'
  441:             );
  442:             if ($switchserver) {
  443:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  444:                           '<br />'.
  445:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  446:                           '<br />'.
  447:                           &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).
  448:                           '<br />'.
  449:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  450:                 );
  451:             } else {
  452:                 $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.').
  453:                           '<br />'.
  454:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  455:                 );
  456:             }
  457:             $r->print(&Apache::loncommon::end_page());
  458:             return OK;
  459:         }
  460:         if (keys(%domconfig) == 0) {
  461:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  462:             my @ids=&Apache::lonnet::current_machine_ids();
  463:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  464:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  465:                 my @loginimages = ('img','logo','domlogo','login');
  466:                 my $custom_img_count = 0;
  467:                 foreach my $img (@loginimages) {
  468:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  469:                         $custom_img_count ++;
  470:                     }
  471:                 }
  472:                 foreach my $role (@roles) {
  473:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  474:                         $custom_img_count ++;
  475:                     }
  476:                 }
  477:                 if ($custom_img_count > 0) {
  478:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  479:                     my $switch_server = &check_switchserver($dom,$confname);
  480:                     $r->print(
  481:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  482:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  483:     &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 />'.
  484:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  485:                     if ($switch_server) {
  486:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  487:                     }
  488:                     $r->print(&Apache::loncommon::end_page());
  489:                     return OK;
  490:                 }
  491:             }
  492:         }
  493:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  494:     }
  495:     return OK;
  496: }
  497: 
  498: sub process_changes {
  499:     my ($r,$dom,$confname,$action,$roles,$values) = @_;
  500:     my %domconfig;
  501:     if (ref($values) eq 'HASH') {
  502:         %domconfig = %{$values};
  503:     }
  504:     my $output;
  505:     if ($action eq 'login') {
  506:         $output = &modify_login($r,$dom,$confname,%domconfig);
  507:     } elsif ($action eq 'rolecolors') {
  508:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  509:                                      %domconfig);
  510:     } elsif ($action eq 'quotas') {
  511:         $output = &modify_quotas($dom,$action,%domconfig);
  512:     } elsif ($action eq 'autoenroll') {
  513:         $output = &modify_autoenroll($dom,%domconfig);
  514:     } elsif ($action eq 'autoupdate') {
  515:         $output = &modify_autoupdate($dom,%domconfig);
  516:     } elsif ($action eq 'autocreate') {
  517:         $output = &modify_autocreate($dom,%domconfig);
  518:     } elsif ($action eq 'directorysrch') {
  519:         $output = &modify_directorysrch($dom,%domconfig);
  520:     } elsif ($action eq 'usercreation') {
  521:         $output = &modify_usercreation($dom,%domconfig);
  522:     } elsif ($action eq 'usermodification') {
  523:         $output = &modify_usermodification($dom,%domconfig);
  524:     } elsif ($action eq 'contacts') {
  525:         $output = &modify_contacts($dom,%domconfig);
  526:     } elsif ($action eq 'defaults') {
  527:         $output = &modify_defaults($dom,$r);
  528:     } elsif ($action eq 'scantron') {
  529:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  530:     } elsif ($action eq 'coursecategories') {
  531:         $output = &modify_coursecategories($dom,%domconfig);
  532:     } elsif ($action eq 'serverstatuses') {
  533:         $output = &modify_serverstatuses($dom,%domconfig);
  534:     } elsif ($action eq 'requestcourses') {
  535:         $output = &modify_quotas($dom,$action,%domconfig);
  536:     } elsif ($action eq 'requestauthor') {
  537:         $output = &modify_quotas($dom,$action,%domconfig);
  538:     } elsif ($action eq 'helpsettings') {
  539:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  540:     } elsif ($action eq 'coursedefaults') {
  541:         $output = &modify_coursedefaults($dom,%domconfig);
  542:     } elsif ($action eq 'usersessions') {
  543:         $output = &modify_usersessions($dom,%domconfig);
  544:     } elsif ($action eq 'loadbalancing') {
  545:         $output = &modify_loadbalancing($dom,%domconfig);
  546:     }
  547:     return $output;
  548: }
  549: 
  550: sub print_config_box {
  551:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  552:     my $rowtotal = 0;
  553:     my $output;
  554:     if ($action eq 'coursecategories') {
  555:         $output = &coursecategories_javascript($settings);
  556:     }
  557:     $output .= 
  558:          '<table class="LC_nested_outer">
  559:           <tr>
  560:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  561:            &mt($item->{text}).'&nbsp;'.
  562:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  563:           '</tr>';
  564:     $rowtotal ++;
  565:     my $numheaders = 1;
  566:     if (ref($item->{'header'}) eq 'ARRAY') {
  567:         $numheaders = scalar(@{$item->{'header'}});
  568:     }
  569:     if ($numheaders > 1) {
  570:         my $colspan = '';
  571:         my $rightcolspan = '';
  572:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') || 
  573:             (($action eq 'login') && ($numheaders < 3))) {
  574:             $colspan = ' colspan="2"';
  575:         }
  576:         if ($action eq 'usersessions') {
  577:             $rightcolspan = ' colspan="3"'; 
  578:         }
  579:         $output .= '
  580:           <tr>
  581:            <td>
  582:             <table class="LC_nested">
  583:              <tr class="LC_info_row">
  584:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  585:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  586:              </tr>';
  587:         $rowtotal ++;
  588:         if ($action eq 'autoupdate') {
  589:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  590:         } elsif ($action eq 'usercreation') {
  591:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  592:         } elsif ($action eq 'usermodification') {
  593:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  594:         } elsif ($action eq 'coursecategories') {
  595:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  596:         } elsif ($action eq 'login') {
  597:             if ($numheaders == 3) {
  598:                 $colspan = ' colspan="2"';
  599:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  600:             } else {
  601:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  602:             }
  603:         } elsif ($action eq 'requestcourses') {
  604:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  605:         } elsif ($action eq 'requestauthor') {
  606:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  607:         } elsif ($action eq 'usersessions') {
  608:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  609:         } elsif ($action eq 'rolecolors') {
  610:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  611:         } elsif ($action eq 'coursedefaults') {
  612:             $output .= &print_coursedefaults('top',$dom,$settings,\$rowtotal);
  613:         }
  614:         $output .= '
  615:            </table>
  616:           </td>
  617:          </tr>
  618:          <tr>
  619:            <td>
  620:             <table class="LC_nested">
  621:              <tr class="LC_info_row">
  622:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  623:         $output .= '
  624:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  625:              </tr>';
  626:             $rowtotal ++;
  627:         if ($action eq 'autoupdate') {
  628:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  629:            </table>
  630:           </td>
  631:          </tr>
  632:          <tr>
  633:            <td>
  634:             <table class="LC_nested">
  635:              <tr class="LC_info_row">
  636:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  637:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  638:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  639:             $rowtotal ++;
  640:         } elsif ($action eq 'usercreation') {
  641:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  642:            </table>
  643:           </td>
  644:          </tr>
  645:          <tr>
  646:            <td>
  647:             <table class="LC_nested">
  648:              <tr class="LC_info_row">
  649:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  650:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  651:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  652:             $rowtotal ++;
  653:         } elsif ($action eq 'usermodification') {
  654:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  655:            </table>
  656:           </td>
  657:          </tr>
  658:          <tr>
  659:            <td>
  660:             <table class="LC_nested">
  661:              <tr class="LC_info_row">
  662:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  663:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  664:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  665:             $rowtotal ++;
  666:         } elsif ($action eq 'coursecategories') {
  667:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  668:         } elsif ($action eq 'login') {
  669:             if ($numheaders == 3) {
  670:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  671:            </table>
  672:           </td>
  673:          </tr>
  674:          <tr>
  675:            <td>
  676:             <table class="LC_nested">
  677:              <tr class="LC_info_row">
  678:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  679:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  680:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  681:                 $rowtotal ++;
  682:             } else {
  683:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  684:             }
  685:         } elsif ($action eq 'requestcourses') {
  686:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  687:         } elsif ($action eq 'requestauthor') {
  688:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  689:         } elsif ($action eq 'usersessions') {
  690:             $output .= &print_usersessions('middle',$dom,$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'}->[2]->{'col1'}).'</td>
  699:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  700:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  701:             $rowtotal ++;
  702:         } elsif ($action eq 'coursedefaults') {
  703:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  704:         } elsif ($action eq 'rolecolors') {
  705:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  706:            </table>
  707:           </td>
  708:          </tr>
  709:          <tr>
  710:            <td>
  711:             <table class="LC_nested">
  712:              <tr class="LC_info_row">
  713:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  714:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  715:               <td class="LC_right_item" valign="top">'.
  716:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  717:              </tr>'.
  718:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  719:            </table>
  720:           </td>
  721:          </tr>
  722:          <tr>
  723:            <td>
  724:             <table class="LC_nested">
  725:              <tr class="LC_info_row">
  726:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  727:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  728:              </tr>'.
  729:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  730:             $rowtotal += 2;
  731:         }
  732:     } else {
  733:         $output .= '
  734:           <tr>
  735:            <td>
  736:             <table class="LC_nested">
  737:              <tr class="LC_info_row">';
  738:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  739:             $output .= '  
  740:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  741:         } elsif ($action eq 'serverstatuses') {
  742:             $output .= '
  743:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  744:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  745: 
  746:         } else {
  747:             $output .= '
  748:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  749:         }
  750:         if (defined($item->{'header'}->[0]->{'col3'})) {
  751:             $output .= '<td class="LC_left_item" valign="top">'.
  752:                        &mt($item->{'header'}->[0]->{'col2'});
  753:             if ($action eq 'serverstatuses') {
  754:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  755:             } 
  756:         } else {
  757:             $output .= '<td class="LC_right_item" valign="top">'.
  758:                        &mt($item->{'header'}->[0]->{'col2'});
  759:         }
  760:         $output .= '</td>';
  761:         if ($item->{'header'}->[0]->{'col3'}) {
  762:             if (defined($item->{'header'}->[0]->{'col4'})) {
  763:                 $output .= '<td class="LC_left_item" valign="top">'.
  764:                             &mt($item->{'header'}->[0]->{'col3'});
  765:             } else {
  766:                 $output .= '<td class="LC_right_item" valign="top">'.
  767:                            &mt($item->{'header'}->[0]->{'col3'});
  768:             }
  769:             if ($action eq 'serverstatuses') {
  770:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  771:             }
  772:             $output .= '</td>';
  773:         }
  774:         if ($item->{'header'}->[0]->{'col4'}) {
  775:             $output .= '<td class="LC_right_item" valign="top">'.
  776:                        &mt($item->{'header'}->[0]->{'col4'});
  777:         }
  778:         $output .= '</tr>';
  779:         $rowtotal ++;
  780:         if ($action eq 'quotas') {
  781:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  782:         } elsif ($action eq 'autoenroll') {
  783:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  784:         } elsif ($action eq 'autocreate') {
  785:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  786:         } elsif ($action eq 'directorysrch') {
  787:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  788:         } elsif ($action eq 'contacts') {
  789:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  790:         } elsif ($action eq 'defaults') {
  791:             $output .= &print_defaults($dom,\$rowtotal);
  792:         } elsif ($action eq 'scantron') {
  793:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  794:         } elsif ($action eq 'serverstatuses') {
  795:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  796:         } elsif ($action eq 'helpsettings') {
  797:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  798:         } elsif ($action eq 'loadbalancing') {
  799:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  800:         }
  801:     }
  802:     $output .= '
  803:    </table>
  804:   </td>
  805:  </tr>
  806: </table><br />';
  807:     return ($output,$rowtotal);
  808: }
  809: 
  810: sub print_login {
  811:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  812:     my ($css_class,$datatable);
  813:     my %choices = &login_choices();
  814: 
  815:     if ($caller eq 'service') {
  816:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  817:         my $choice = $choices{'disallowlogin'};
  818:         $css_class = ' class="LC_odd_row"';
  819:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  820:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  821:                       '<th>'.$choices{'server'}.'</th>'.
  822:                       '<th>'.$choices{'serverpath'}.'</th>'.
  823:                       '<th>'.$choices{'custompath'}.'</th>'.
  824:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  825:         my %disallowed;
  826:         if (ref($settings) eq 'HASH') {
  827:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  828:                %disallowed = %{$settings->{'loginvia'}};
  829:             }
  830:         }
  831:         foreach my $lonhost (sort(keys(%servers))) {
  832:             my $direct = 'selected="selected"';
  833:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  834:                 if ($disallowed{$lonhost}{'server'} ne '') {
  835:                     $direct = '';
  836:                 }
  837:             }
  838:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  839:                           '<td><select name="'.$lonhost.'_server">'.
  840:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  841:                           '</option>';
  842:             foreach my $hostid (sort(keys(%servers))) {
  843:                 next if ($servers{$hostid} eq $servers{$lonhost});
  844:                 my $selected = '';
  845:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  846:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  847:                         $selected = 'selected="selected"';
  848:                     }
  849:                 }
  850:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  851:                               $servers{$hostid}.'</option>';
  852:             }
  853:             $datatable .= '</select></td>'.
  854:                           '<td><select name="'.$lonhost.'_serverpath">';
  855:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  856:                 my $pathname = $path;
  857:                 if ($path eq 'custom') {
  858:                     $pathname = &mt('Custom Path').' ->';
  859:                 }
  860:                 my $selected = '';
  861:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  862:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  863:                         $selected = 'selected="selected"';
  864:                     }
  865:                 } elsif ($path eq '') {
  866:                     $selected = 'selected="selected"';
  867:                 }
  868:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  869:             }
  870:             $datatable .= '</select></td>';
  871:             my ($custom,$exempt);
  872:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  873:                 $custom = $disallowed{$lonhost}{'custompath'};
  874:                 $exempt = $disallowed{$lonhost}{'exempt'};
  875:             }
  876:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  877:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  878:                           '</tr>';
  879:         }
  880:         $datatable .= '</table></td></tr>';
  881:         return $datatable;
  882:     } elsif ($caller eq 'page') {
  883:         my %defaultchecked = ( 
  884:                                'coursecatalog' => 'on',
  885:                                'helpdesk'      => 'on',
  886:                                'adminmail'     => 'off',
  887:                                'newuser'       => 'off',
  888:                              );
  889:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  890:         my (%checkedon,%checkedoff);
  891:         foreach my $item (@toggles) {
  892:             if ($defaultchecked{$item} eq 'on') { 
  893:                 $checkedon{$item} = ' checked="checked" ';
  894:                 $checkedoff{$item} = ' ';
  895:             } elsif ($defaultchecked{$item} eq 'off') {
  896:                 $checkedoff{$item} = ' checked="checked" ';
  897:                 $checkedon{$item} = ' ';
  898:             }
  899:         }
  900:         my @images = ('img','logo','domlogo','login');
  901:         my @logintext = ('textcol','bgcol');
  902:         my @bgs = ('pgbg','mainbg','sidebg');
  903:         my @links = ('link','alink','vlink');
  904:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  905:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  906:         my (%is_custom,%designs);
  907:         my %defaults = (
  908:                        font => $defaultdesign{'login.font'},
  909:                        );
  910:         foreach my $item (@images) {
  911:             $defaults{$item} = $defaultdesign{'login.'.$item};
  912:             $defaults{'showlogo'}{$item} = 1;
  913:         }
  914:         foreach my $item (@bgs) {
  915:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  916:         }
  917:         foreach my $item (@logintext) {
  918:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  919:         }
  920:         foreach my $item (@links) {
  921:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  922:         }
  923:         if (ref($settings) eq 'HASH') {
  924:             foreach my $item (@toggles) {
  925:                 if ($settings->{$item} eq '1') {
  926:                     $checkedon{$item} =  ' checked="checked" ';
  927:                     $checkedoff{$item} = ' ';
  928:                 } elsif ($settings->{$item} eq '0') {
  929:                     $checkedoff{$item} =  ' checked="checked" ';
  930:                     $checkedon{$item} = ' ';
  931:                 }
  932:             }
  933:             foreach my $item (@images) {
  934:                 if (defined($settings->{$item})) {
  935:                     $designs{$item} = $settings->{$item};
  936:                     $is_custom{$item} = 1;
  937:                 }
  938:                 if (defined($settings->{'showlogo'}{$item})) {
  939:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  940:                 }
  941:             }
  942:             foreach my $item (@logintext) {
  943:                 if ($settings->{$item} ne '') {
  944:                     $designs{'logintext'}{$item} = $settings->{$item};
  945:                     $is_custom{$item} = 1;
  946:                 }
  947:             }
  948:             if ($settings->{'font'} ne '') {
  949:                 $designs{'font'} = $settings->{'font'};
  950:                 $is_custom{'font'} = 1;
  951:             }
  952:             foreach my $item (@bgs) {
  953:                 if ($settings->{$item} ne '') {
  954:                     $designs{'bgs'}{$item} = $settings->{$item};
  955:                     $is_custom{$item} = 1;
  956:                 }
  957:             }
  958:             foreach my $item (@links) {
  959:                 if ($settings->{$item} ne '') {
  960:                     $designs{'links'}{$item} = $settings->{$item};
  961:                     $is_custom{$item} = 1;
  962:                 }
  963:             }
  964:         } else {
  965:             if ($designhash{$dom.'.login.font'} ne '') {
  966:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  967:                 $is_custom{'font'} = 1;
  968:             }
  969:             foreach my $item (@images) {
  970:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  971:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  972:                     $is_custom{$item} = 1;
  973:                 }
  974:             }
  975:             foreach my $item (@bgs) {
  976:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  977:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  978:                     $is_custom{$item} = 1;
  979:                 }
  980:             }
  981:             foreach my $item (@links) {
  982:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  983:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  984:                     $is_custom{$item} = 1;
  985:                 }
  986:             }
  987:         }
  988:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  989:                                                       logo => 'Institution Logo',
  990:                                                       domlogo => 'Domain Logo',
  991:                                                       login => 'Login box');
  992:         my $itemcount = 1;
  993:         foreach my $item (@toggles) {
  994:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
  995:             $datatable .=  
  996:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  997:                 '</td><td>'.
  998:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
  999:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1000:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1001:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1002:                 '</tr>';
 1003:             $itemcount ++;
 1004:         }
 1005:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1006:         $datatable .= '</tr></table></td></tr>';
 1007:     } elsif ($caller eq 'help') {
 1008:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1009:         my $switchserver = &check_switchserver($dom,$confname);
 1010:         my $itemcount = 1;
 1011:         $defaulturl = '/adm/loginproblems.html';
 1012:         $defaulttype = 'default';
 1013:         %lt = &Apache::lonlocal::texthash (
 1014:                      del     => 'Delete?',
 1015:                      rep     => 'Replace:',
 1016:                      upl     => 'Upload:',
 1017:                      default => 'Default',
 1018:                      custom  => 'Custom',
 1019:                                              );
 1020:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1021:         my @currlangs;
 1022:         if (ref($settings) eq 'HASH') {
 1023:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1024:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1025:                     next if ($settings->{'helpurl'}{$key} eq '');
 1026:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1027:                     $type{$key} = 'custom';
 1028:                     unless ($key eq 'nolang') {
 1029:                         push(@currlangs,$key);
 1030:                     }
 1031:                 }
 1032:             } elsif ($settings->{'helpurl'} ne '') {
 1033:                 $type{'nolang'} = 'custom';
 1034:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1035:             }
 1036:         }
 1037:         foreach my $lang ('nolang',sort(@currlangs)) {
 1038:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1039:             $datatable .= '<tr'.$css_class.'>';
 1040:             if ($url{$lang} eq '') {
 1041:                 $url{$lang} = $defaulturl;
 1042:             }
 1043:             if ($type{$lang} eq '') {
 1044:                 $type{$lang} = $defaulttype;
 1045:             }
 1046:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1047:             if ($lang eq 'nolang') {
 1048:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1049:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1050:             } else {
 1051:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1052:                                   $langchoices{$lang},
 1053:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1054:             }
 1055:             $datatable .= '</span></td>'."\n".
 1056:                           '<td class="LC_left_item">';
 1057:             if ($type{$lang} eq 'custom') {
 1058:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1059:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1060:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1061:             } else {
 1062:                 $datatable .= $lt{'upl'};
 1063:             }
 1064:             $datatable .='<br />';
 1065:             if ($switchserver) {
 1066:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1067:             } else {
 1068:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1069:             }
 1070:             $datatable .= '</td></tr>';
 1071:             $itemcount ++;
 1072:         }
 1073:         my @addlangs;
 1074:         foreach my $lang (sort(keys(%langchoices))) {
 1075:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1076:             push(@addlangs,$lang);
 1077:         }
 1078:         if (@addlangs > 0) {
 1079:             my %toadd;
 1080:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1081:             $toadd{''} = &mt('Select');
 1082:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1083:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1084:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1085:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1086:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1087:             if ($switchserver) {
 1088:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1089:             } else {
 1090:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1091:             }
 1092:             $datatable .= '</td></tr>';
 1093:             $itemcount ++;
 1094:         }
 1095:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1096:     }
 1097:     return $datatable;
 1098: }
 1099: 
 1100: sub login_choices {
 1101:     my %choices =
 1102:         &Apache::lonlocal::texthash (
 1103:             coursecatalog => 'Display Course/Community Catalog link?',
 1104:             adminmail     => "Display Administrator's E-mail Address?",
 1105:             helpdesk      => 'Display "Contact Helpdesk" link',
 1106:             disallowlogin => "Login page requests redirected",
 1107:             hostid        => "Server",
 1108:             server        => "Redirect to:",
 1109:             serverpath    => "Path",
 1110:             custompath    => "Custom", 
 1111:             exempt        => "Exempt IP(s)",
 1112:             directlogin   => "No redirect",
 1113:             newuser       => "Link to create a user account",
 1114:             img           => "Header",
 1115:             logo          => "Main Logo",
 1116:             domlogo       => "Domain Logo",
 1117:             login         => "Log-in Header", 
 1118:             textcol       => "Text color",
 1119:             bgcol         => "Box color",
 1120:             bgs           => "Background colors",
 1121:             links         => "Link colors",
 1122:             font          => "Font color",
 1123:             pgbg          => "Header",
 1124:             mainbg        => "Page",
 1125:             sidebg        => "Login box",
 1126:             link          => "Link",
 1127:             alink         => "Active link",
 1128:             vlink         => "Visited link",
 1129:         );
 1130:     return %choices;
 1131: }
 1132: 
 1133: sub print_rolecolors {
 1134:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1135:     my %choices = &color_font_choices();
 1136:     my @bgs = ('pgbg','tabbg','sidebg');
 1137:     my @links = ('link','alink','vlink');
 1138:     my @images = ('img');
 1139:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1140:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1141:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1142:     my (%is_custom,%designs);
 1143:     my %defaults = (
 1144:                    img => $defaultdesign{$role.'.img'},
 1145:                    font => $defaultdesign{$role.'.font'},
 1146: 		   fontmenu => $defaultdesign{$role.'.fontmenu'},
 1147:                    );
 1148:     foreach my $item (@bgs) {
 1149:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1150:     }
 1151:     foreach my $item (@links) {
 1152:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1153:     }
 1154:     if (ref($settings) eq 'HASH') {
 1155:         if (ref($settings->{$role}) eq 'HASH') {
 1156:             if ($settings->{$role}->{'img'} ne '') {
 1157:                 $designs{'img'} = $settings->{$role}->{'img'};
 1158:                 $is_custom{'img'} = 1;
 1159:             }
 1160:             if ($settings->{$role}->{'font'} ne '') {
 1161:                 $designs{'font'} = $settings->{$role}->{'font'};
 1162:                 $is_custom{'font'} = 1;
 1163:             }
 1164:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1165:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1166:                 $is_custom{'fontmenu'} = 1;
 1167:             }
 1168:             foreach my $item (@bgs) {
 1169:                 if ($settings->{$role}->{$item} ne '') {
 1170:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1171:                     $is_custom{$item} = 1;
 1172:                 }
 1173:             }
 1174:             foreach my $item (@links) {
 1175:                 if ($settings->{$role}->{$item} ne '') {
 1176:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1177:                     $is_custom{$item} = 1;
 1178:                 }
 1179:             }
 1180:         }
 1181:     } else {
 1182:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1183:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1184:             $is_custom{'img'} = 1;
 1185:         }
 1186:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1187:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1188:             $is_custom{'fontmenu'} = 1; 
 1189:         }
 1190:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1191:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1192:             $is_custom{'font'} = 1;
 1193:         }
 1194:         foreach my $item (@bgs) {
 1195:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1196:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1197:                 $is_custom{$item} = 1;
 1198:             
 1199:             }
 1200:         }
 1201:         foreach my $item (@links) {
 1202:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1203:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1204:                 $is_custom{$item} = 1;
 1205:             }
 1206:         }
 1207:     }
 1208:     my $itemcount = 1;
 1209:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1210:     $datatable .= '</tr></table></td></tr>';
 1211:     return $datatable;
 1212: }
 1213: 
 1214: sub display_color_options {
 1215:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1216:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1217:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1218:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1219:     my $datatable = '<tr'.$css_class.'>'.
 1220:         '<td>'.$choices->{'font'}.'</td>';
 1221:     if (!$is_custom->{'font'}) {
 1222:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1223:     } else {
 1224:         $datatable .= '<td>&nbsp;</td>';
 1225:     }
 1226:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1227: 
 1228:     $datatable .= '<td><span class="LC_nobreak">'.
 1229:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1230:                   ' value="'.$current_color.'" />&nbsp;'. 
 1231:                   '&nbsp;</td></tr>';
 1232:     unless ($role eq 'login') { 
 1233:         $datatable .= '<tr'.$css_class.'>'.
 1234:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1235:         if (!$is_custom->{'fontmenu'}) {
 1236:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1237:         } else {
 1238:             $datatable .= '<td>&nbsp;</td>';
 1239:         }
 1240: 	$current_color = $designs->{'fontmenu'} ? 
 1241: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1242:         $datatable .= '<td><span class="LC_nobreak">'.
 1243:                       '<input class="colorchooser" type="text" size="10" name="'
 1244: 		      .$role.'_fontmenu"'.
 1245:                       ' value="'.$current_color.'" />&nbsp;'.
 1246:                       '&nbsp;</td></tr>';
 1247:     }
 1248:     my $switchserver = &check_switchserver($dom,$confname);
 1249:     foreach my $img (@{$images}) {
 1250: 	$itemcount ++;
 1251:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1252:         $datatable .= '<tr'.$css_class.'>'.
 1253:                       '<td>'.$choices->{$img};
 1254:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1255:         if ($role eq 'login') {
 1256:             if ($img eq 'login') {
 1257:                 $login_hdr_pick =
 1258:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1259:                 $logincolors =
 1260:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1261:                                             $designs);
 1262:             } elsif ($img ne 'domlogo') {
 1263:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1264:             }
 1265:         }
 1266:         $datatable .= '</td>';
 1267:         if ($designs->{$img} ne '') {
 1268:             $imgfile = $designs->{$img};
 1269: 	    $img_import = ($imgfile =~ m{^/adm/});
 1270:         } else {
 1271:             $imgfile = $defaults->{$img};
 1272:         }
 1273:         if ($imgfile) {
 1274:             my ($showfile,$fullsize);
 1275:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1276:                 my $urldir = $1;
 1277:                 my $filename = $2;
 1278:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1279:                 if (@info) {
 1280:                     my $thumbfile = 'tn-'.$filename;
 1281:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1282:                     if (@thumb) {
 1283:                         $showfile = $urldir.'/'.$thumbfile;
 1284:                     } else {
 1285:                         $showfile = $imgfile;
 1286:                     }
 1287:                 } else {
 1288:                     $showfile = '';
 1289:                 }
 1290:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1291:                 $showfile = $imgfile;
 1292:                 my $imgdir = $1;
 1293:                 my $filename = $2;
 1294:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1295:                     $showfile = "/$imgdir/tn-".$filename;
 1296:                 } else {
 1297:                     my $input = $londocroot.$imgfile;
 1298:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1299:                     if (!-e $output) {
 1300:                         my ($width,$height) = &thumb_dimensions();
 1301:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1302:                         if ($fullwidth ne '' && $fullheight ne '') {
 1303:                             if ($fullwidth > $width && $fullheight > $height) { 
 1304:                                 my $size = $width.'x'.$height;
 1305:                                 system("convert -sample $size $input $output");
 1306:                                 $showfile = "/$imgdir/tn-".$filename;
 1307:                             }
 1308:                         }
 1309:                     }
 1310:                 }
 1311:             }
 1312:             if ($showfile) {
 1313:                 if ($showfile =~ m{^/(adm|res)/}) {
 1314:                     if ($showfile =~ m{^/res/}) {
 1315:                         my $local_showfile =
 1316:                             &Apache::lonnet::filelocation('',$showfile);
 1317:                         &Apache::lonnet::repcopy($local_showfile);
 1318:                     }
 1319:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1320:                 }
 1321:                 if ($imgfile) {
 1322:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1323:                         if ($imgfile =~ m{^/res/}) {
 1324:                             my $local_imgfile =
 1325:                                 &Apache::lonnet::filelocation('',$imgfile);
 1326:                             &Apache::lonnet::repcopy($local_imgfile);
 1327:                         }
 1328:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1329:                     } else {
 1330:                         $fullsize = $imgfile;
 1331:                     }
 1332:                 }
 1333:                 $datatable .= '<td>';
 1334:                 if ($img eq 'login') {
 1335:                     $datatable .= $login_hdr_pick;
 1336:                 } 
 1337:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1338:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1339:             } else {
 1340:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1341:                               &mt('Upload:');
 1342:             }
 1343:         } else {
 1344:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1345:                           &mt('Upload:');
 1346:         }
 1347:         if ($switchserver) {
 1348:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1349:         } else {
 1350:             if ($img ne 'login') { # suppress file selection for Log-in header
 1351:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1352:             }
 1353:         }
 1354:         $datatable .= '</td></tr>';
 1355:     }
 1356:     $itemcount ++;
 1357:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1358:     $datatable .= '<tr'.$css_class.'>'.
 1359:                   '<td>'.$choices->{'bgs'}.'</td>';
 1360:     my $bgs_def;
 1361:     foreach my $item (@{$bgs}) {
 1362:         if (!$is_custom->{$item}) {
 1363:             $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>';
 1364:         }
 1365:     }
 1366:     if ($bgs_def) {
 1367:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1368:     } else {
 1369:         $datatable .= '<td>&nbsp;</td>';
 1370:     }
 1371:     $datatable .= '<td class="LC_right_item">'.
 1372:                   '<table border="0"><tr>';
 1373: 
 1374:     foreach my $item (@{$bgs}) {
 1375:         $datatable .= '<td align="center">';
 1376: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1377:         if ($designs->{'bgs'}{$item}) {
 1378:             $datatable .= '&nbsp;';
 1379:         }
 1380:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1381:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1382:     }
 1383:     $datatable .= '</tr></table></td></tr>';
 1384:     $itemcount ++;
 1385:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1386:     $datatable .= '<tr'.$css_class.'>'.
 1387:                   '<td>'.$choices->{'links'}.'</td>';
 1388:     my $links_def;
 1389:     foreach my $item (@{$links}) {
 1390:         if (!$is_custom->{$item}) {
 1391:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1392:         }
 1393:     }
 1394:     if ($links_def) {
 1395:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1396:     } else {
 1397:         $datatable .= '<td>&nbsp;</td>';
 1398:     }
 1399:     $datatable .= '<td class="LC_right_item">'.
 1400:                   '<table border="0"><tr>';
 1401:     foreach my $item (@{$links}) {
 1402: 	my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1403:         $datatable .= '<td align="center">'."\n";
 1404: 
 1405:         if ($designs->{'links'}{$item}) {
 1406:             $datatable.='&nbsp;';
 1407:         }
 1408:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1409:                       '" /></td>';
 1410:     }
 1411:     $$rowtotal += $itemcount;
 1412:     return $datatable;
 1413: }
 1414: 
 1415: sub logo_display_options {
 1416:     my ($img,$defaults,$designs) = @_;
 1417:     my $checkedon;
 1418:     if (ref($defaults) eq 'HASH') {
 1419:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1420:             if ($defaults->{'showlogo'}{$img}) {
 1421:                 $checkedon = 'checked="checked" ';     
 1422:             }
 1423:         } 
 1424:     }
 1425:     if (ref($designs) eq 'HASH') {
 1426:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1427:             if (defined($designs->{'showlogo'}{$img})) {
 1428:                 if ($designs->{'showlogo'}{$img} == 0) {
 1429:                     $checkedon = '';
 1430:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1431:                     $checkedon = 'checked="checked" ';
 1432:                 }
 1433:             }
 1434:         }
 1435:     }
 1436:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1437:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1438:            &mt('show').'</label>'."\n";
 1439: }
 1440: 
 1441: sub login_header_options  {
 1442:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1443:     my $output = '';
 1444:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1445:         $output .= &mt('Text default(s):').'<br />';
 1446:         if (!$is_custom->{'textcol'}) {
 1447:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1448:                        '&nbsp;&nbsp;&nbsp;';
 1449:         }
 1450:         if (!$is_custom->{'bgcol'}) {
 1451:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1452:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1453:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1454:         }
 1455:         $output .= '<br />';
 1456:     }
 1457:     $output .='<br />';
 1458:     return $output;
 1459: }
 1460: 
 1461: sub login_text_colors {
 1462:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1463:     my $color_menu = '<table border="0"><tr>';
 1464:     foreach my $item (@{$logintext}) {
 1465:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1466:         $color_menu .= '<td align="center">'.$link;
 1467:         if ($designs->{'logintext'}{$item}) {
 1468:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1469:         }
 1470:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1471:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1472:                        '<td>&nbsp;</td>';
 1473:     }
 1474:     $color_menu .= '</tr></table><br />';
 1475:     return $color_menu;
 1476: }
 1477: 
 1478: sub image_changes {
 1479:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1480:     my $output;
 1481:     if ($img eq 'login') {
 1482:             # suppress image for Log-in header
 1483:     } elsif (!$is_custom) {
 1484:         if ($img ne 'domlogo') {
 1485:             $output .= &mt('Default image:').'<br />';
 1486:         } else {
 1487:             $output .= &mt('Default in use:').'<br />';
 1488:         }
 1489:     }
 1490:     if ($img eq 'login') { # suppress image for Log-in header
 1491:         $output .= '<td>'.$logincolors;
 1492:     } else {
 1493:         if ($img_import) {
 1494:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1495:         }
 1496:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1497:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1498:         if ($is_custom) {
 1499:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1500:                        '<input type="checkbox" name="'.
 1501:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1502:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1503:         } else {
 1504:             $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1505:         }
 1506:     }
 1507:     return $output;
 1508: }
 1509: 
 1510: sub color_pick {
 1511:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1512:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1513:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1514:                ');">'.$desc.'</a>';
 1515:     return $link;
 1516: }
 1517: 
 1518: sub print_quotas {
 1519:     my ($dom,$settings,$rowtotal,$action) = @_;
 1520:     my $context;
 1521:     if ($action eq 'quotas') {
 1522:         $context = 'tools';
 1523:     } else {
 1524:         $context = $action;
 1525:     }
 1526:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 1527:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1528:     my $typecount = 0;
 1529:     my ($css_class,%titles);
 1530:     if ($context eq 'requestcourses') {
 1531:         @usertools = ('official','unofficial','community');
 1532:         @options =('norequest','approval','validate','autolimit');
 1533:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1534:         %titles = &courserequest_titles();
 1535:     } elsif ($context eq 'requestauthor') {
 1536:         @usertools = ('author');
 1537:         @options = ('norequest','approval','automatic');
 1538:         %titles = &authorrequest_titles(); 
 1539:     } else {
 1540:         @usertools = ('aboutme','blog','webdav','portfolio');
 1541:         %titles = &tool_titles();
 1542:     }
 1543:     if (ref($types) eq 'ARRAY') {
 1544:         foreach my $type (@{$types}) {
 1545:             my $currdefquota;
 1546:             unless (($context eq 'requestcourses') ||
 1547:                     ($context eq 'requestauthor')) {
 1548:                 if (ref($settings) eq 'HASH') {
 1549:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1550:                         $currdefquota = $settings->{defaultquota}->{$type}; 
 1551:                     } else {
 1552:                         $currdefquota = $settings->{$type};
 1553:                     }
 1554:                 }
 1555:             }
 1556:             if (defined($usertypes->{$type})) {
 1557:                 $typecount ++;
 1558:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1559:                 $datatable .= '<tr'.$css_class.'>'.
 1560:                               '<td>'.$usertypes->{$type}.'</td>'.
 1561:                               '<td class="LC_left_item">';
 1562:                 if ($context eq 'requestcourses') {
 1563:                     $datatable .= '<table><tr>';
 1564:                 }
 1565:                 my %cell;  
 1566:                 foreach my $item (@usertools) {
 1567:                     if ($context eq 'requestcourses') {
 1568:                         my ($curroption,$currlimit);
 1569:                         if (ref($settings) eq 'HASH') {
 1570:                             if (ref($settings->{$item}) eq 'HASH') {
 1571:                                 $curroption = $settings->{$item}->{$type};
 1572:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1573:                                     $currlimit = $1; 
 1574:                                 }
 1575:                             }
 1576:                         }
 1577:                         if (!$curroption) {
 1578:                             $curroption = 'norequest';
 1579:                         }
 1580:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1581:                         foreach my $option (@options) {
 1582:                             my $val = $option;
 1583:                             if ($option eq 'norequest') {
 1584:                                 $val = 0;  
 1585:                             }
 1586:                             if ($option eq 'validate') {
 1587:                                 my $canvalidate = 0;
 1588:                                 if (ref($validations{$item}) eq 'HASH') { 
 1589:                                     if ($validations{$item}{$type}) {
 1590:                                         $canvalidate = 1;
 1591:                                     }
 1592:                                 }
 1593:                                 next if (!$canvalidate);
 1594:                             }
 1595:                             my $checked = '';
 1596:                             if ($option eq $curroption) {
 1597:                                 $checked = ' checked="checked"';
 1598:                             } elsif ($option eq 'autolimit') {
 1599:                                 if ($curroption =~ /^autolimit/) {
 1600:                                     $checked = ' checked="checked"';
 1601:                                 }                       
 1602:                             } 
 1603:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1604:                                   '<input type="radio" name="crsreq_'.$item.
 1605:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1606:                                   $titles{$option}.'</label>';
 1607:                             if ($option eq 'autolimit') {
 1608:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1609:                                                 $item.'_limit_'.$type.'" size="1" '.
 1610:                                                 'value="'.$currlimit.'" />';
 1611:                             }
 1612:                             $cell{$item} .= '</span> ';
 1613:                             if ($option eq 'autolimit') {
 1614:                                 $cell{$item} .= $titles{'unlimited'};
 1615:                             }
 1616:                         }
 1617:                     } elsif ($context eq 'requestauthor') {
 1618:                         my $curroption;
 1619:                         if (ref($settings) eq 'HASH') {
 1620:                             $curroption = $settings->{$type};
 1621:                         }
 1622:                         if (!$curroption) {
 1623:                             $curroption = 'norequest';
 1624:                         }
 1625:                         foreach my $option (@options) {
 1626:                             my $val = $option;
 1627:                             if ($option eq 'norequest') {
 1628:                                 $val = 0;
 1629:                             }
 1630:                             my $checked = '';
 1631:                             if ($option eq $curroption) {
 1632:                                 $checked = ' checked="checked"';
 1633:                             }
 1634:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1635:                                   '<input type="radio" name="authorreq_'.$type.
 1636:                                   '" value="'.$val.'"'.$checked.' />'.
 1637:                                   $titles{$option}.'</label></span>&nbsp; ';
 1638:                         }
 1639:                     } else {
 1640:                         my $checked = 'checked="checked" ';
 1641:                         if (ref($settings) eq 'HASH') {
 1642:                             if (ref($settings->{$item}) eq 'HASH') {
 1643:                                 if ($settings->{$item}->{$type} == 0) {
 1644:                                     $checked = '';
 1645:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1646:                                     $checked =  'checked="checked" ';
 1647:                                 }
 1648:                             }
 1649:                         }
 1650:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1651:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1652:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1653:                                       '</label></span>&nbsp; ';
 1654:                     }
 1655:                 }
 1656:                 if ($context eq 'requestcourses') {
 1657:                     $datatable .= '</tr><tr>';
 1658:                     foreach my $item (@usertools) {
 1659:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1660:                     }
 1661:                     $datatable .= '</tr></table>';
 1662:                 }
 1663:                 $datatable .= '</td>';
 1664:                 unless (($context eq 'requestcourses') ||
 1665:                         ($context eq 'requestauthor')) {
 1666:                     $datatable .= 
 1667:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1668:                               '<input type="text" name="quota_'.$type.
 1669:                               '" value="'.$currdefquota.
 1670:                               '" size="5" /> Mb</span></td>';
 1671:                 }
 1672:                 $datatable .= '</tr>';
 1673:             }
 1674:         }
 1675:     }
 1676:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1677:         $defaultquota = '20';
 1678:         if (ref($settings) eq 'HASH') {
 1679:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1680:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1681:             } elsif (defined($settings->{'default'})) {
 1682:                 $defaultquota = $settings->{'default'};
 1683:             }
 1684:         }
 1685:     }
 1686:     $typecount ++;
 1687:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1688:     $datatable .= '<tr'.$css_class.'>'.
 1689:                   '<td>'.$othertitle.'</td>'.
 1690:                   '<td class="LC_left_item">';
 1691:     if ($context eq 'requestcourses') {
 1692:         $datatable .= '<table><tr>';
 1693:     }
 1694:     my %defcell;
 1695:     foreach my $item (@usertools) {
 1696:         if ($context eq 'requestcourses') {
 1697:             my ($curroption,$currlimit);
 1698:             if (ref($settings) eq 'HASH') {
 1699:                 if (ref($settings->{$item}) eq 'HASH') {
 1700:                     $curroption = $settings->{$item}->{'default'};
 1701:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1702:                         $currlimit = $1;
 1703:                     }
 1704:                 }
 1705:             }
 1706:             if (!$curroption) {
 1707:                 $curroption = 'norequest';
 1708:             }
 1709:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1710:             foreach my $option (@options) {
 1711:                 my $val = $option;
 1712:                 if ($option eq 'norequest') {
 1713:                     $val = 0;
 1714:                 }
 1715:                 if ($option eq 'validate') {
 1716:                     my $canvalidate = 0;
 1717:                     if (ref($validations{$item}) eq 'HASH') {
 1718:                         if ($validations{$item}{'default'}) {
 1719:                             $canvalidate = 1;
 1720:                         }
 1721:                     }
 1722:                     next if (!$canvalidate);
 1723:                 }
 1724:                 my $checked = '';
 1725:                 if ($option eq $curroption) {
 1726:                     $checked = ' checked="checked"';
 1727:                 } elsif ($option eq 'autolimit') {
 1728:                     if ($curroption =~ /^autolimit/) {
 1729:                         $checked = ' checked="checked"';
 1730:                     }
 1731:                 }
 1732:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1733:                                   '<input type="radio" name="crsreq_'.$item.
 1734:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1735:                                   $titles{$option}.'</label>';
 1736:                 if ($option eq 'autolimit') {
 1737:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1738:                                        $item.'_limit_default" size="1" '.
 1739:                                        'value="'.$currlimit.'" />';
 1740:                 }
 1741:                 $defcell{$item} .= '</span> ';
 1742:                 if ($option eq 'autolimit') {
 1743:                     $defcell{$item} .= $titles{'unlimited'};
 1744:                 }
 1745:             }
 1746:         } elsif ($context eq 'requestauthor') {
 1747:             my $curroption;
 1748:             if (ref($settings) eq 'HASH') {
 1749:                 $curroption = $settings->{'default'};
 1750:             }
 1751:             if (!$curroption) {
 1752:                 $curroption = 'norequest';
 1753:             }
 1754:             foreach my $option (@options) {
 1755:                 my $val = $option;
 1756:                 if ($option eq 'norequest') {
 1757:                     $val = 0;
 1758:                 }
 1759:                 my $checked = '';
 1760:                 if ($option eq $curroption) {
 1761:                     $checked = ' checked="checked"';
 1762:                 }
 1763:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1764:                               '<input type="radio" name="authorreq_default"'.
 1765:                               ' value="'.$val.'"'.$checked.' />'.
 1766:                               $titles{$option}.'</label></span>&nbsp; ';
 1767:             }
 1768:         } else {
 1769:             my $checked = 'checked="checked" ';
 1770:             if (ref($settings) eq 'HASH') {
 1771:                 if (ref($settings->{$item}) eq 'HASH') {
 1772:                     if ($settings->{$item}->{'default'} == 0) {
 1773:                         $checked = '';
 1774:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1775:                         $checked = 'checked="checked" ';
 1776:                     }
 1777:                 }
 1778:             }
 1779:             $datatable .= '<span class="LC_nobreak"><label>'.
 1780:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1781:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1782:                           '</label></span>&nbsp; ';
 1783:         }
 1784:     }
 1785:     if ($context eq 'requestcourses') {
 1786:         $datatable .= '</tr><tr>';
 1787:         foreach my $item (@usertools) {
 1788:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1789:         }
 1790:         $datatable .= '</tr></table>';
 1791:     }
 1792:     $datatable .= '</td>';
 1793:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1794:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1795:                       '<input type="text" name="defaultquota" value="'.
 1796:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1797:     }
 1798:     $datatable .= '</tr>';
 1799:     $typecount ++;
 1800:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1801:     $datatable .= '<tr'.$css_class.'>'.
 1802:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1803:     if ($context eq 'requestcourses') {
 1804:         $datatable .= &mt('(overrides affiliation, if set)').
 1805:                       '</td>'.
 1806:                       '<td class="LC_left_item">'.
 1807:                       '<table><tr>';
 1808:     } else {
 1809:         $datatable .= &mt('(overrides affiliation, if checked)').
 1810:                       '</td>'.
 1811:                       '<td class="LC_left_item" colspan="2">'.
 1812:                       '<br />';
 1813:     }
 1814:     my %advcell;
 1815:     foreach my $item (@usertools) {
 1816:         if ($context eq 'requestcourses') {
 1817:             my ($curroption,$currlimit);
 1818:             if (ref($settings) eq 'HASH') {
 1819:                 if (ref($settings->{$item}) eq 'HASH') {
 1820:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1821:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1822:                         $currlimit = $1;
 1823:                     }
 1824:                 }
 1825:             }
 1826:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1827:             my $checked = '';
 1828:             if ($curroption eq '') {
 1829:                 $checked = ' checked="checked"';
 1830:             }
 1831:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1832:                                '<input type="radio" name="crsreq_'.$item.
 1833:                                '__LC_adv" value=""'.$checked.' />'.
 1834:                                &mt('No override set').'</label></span>&nbsp; ';
 1835:             foreach my $option (@options) {
 1836:                 my $val = $option;
 1837:                 if ($option eq 'norequest') {
 1838:                     $val = 0;
 1839:                 }
 1840:                 if ($option eq 'validate') {
 1841:                     my $canvalidate = 0;
 1842:                     if (ref($validations{$item}) eq 'HASH') {
 1843:                         if ($validations{$item}{'_LC_adv'}) {
 1844:                             $canvalidate = 1;
 1845:                         }
 1846:                     }
 1847:                     next if (!$canvalidate);
 1848:                 }
 1849:                 my $checked = '';
 1850:                 if ($val eq $curroption) {
 1851:                     $checked = ' checked="checked"';
 1852:                 } elsif ($option eq 'autolimit') {
 1853:                     if ($curroption =~ /^autolimit/) {
 1854:                         $checked = ' checked="checked"';
 1855:                     }
 1856:                 }
 1857:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1858:                                   '<input type="radio" name="crsreq_'.$item.
 1859:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1860:                                   $titles{$option}.'</label>';
 1861:                 if ($option eq 'autolimit') {
 1862:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1863:                                        $item.'_limit__LC_adv" size="1" '.
 1864:                                        'value="'.$currlimit.'" />';
 1865:                 }
 1866:                 $advcell{$item} .= '</span> ';
 1867:                 if ($option eq 'autolimit') {
 1868:                     $advcell{$item} .= $titles{'unlimited'};
 1869:                 }
 1870:             }
 1871:         } elsif ($context eq 'requestauthor') {
 1872:             my $curroption;
 1873:             if (ref($settings) eq 'HASH') {
 1874:                 $curroption = $settings->{'_LC_adv'};
 1875:             }
 1876:             my $checked = '';
 1877:             if ($curroption eq '') {
 1878:                 $checked = ' checked="checked"';
 1879:             }
 1880:             $datatable .= '<span class="LC_nobreak"><label>'.
 1881:                           '<input type="radio" name="authorreq__LC_adv"'.
 1882:                           ' value=""'.$checked.' />'.
 1883:                           &mt('No override set').'</label></span>&nbsp; ';
 1884:             foreach my $option (@options) {
 1885:                 my $val = $option;
 1886:                 if ($option eq 'norequest') {
 1887:                     $val = 0;
 1888:                 }
 1889:                 my $checked = '';
 1890:                 if ($val eq $curroption) {
 1891:                     $checked = ' checked="checked"';
 1892:                 }
 1893:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1894:                               '<input type="radio" name="authorreq__LC_adv"'.
 1895:                               ' value="'.$val.'"'.$checked.' />'.
 1896:                               $titles{$option}.'</label></span>&nbsp; ';
 1897:             }
 1898:         } else {
 1899:             my $checked = 'checked="checked" ';
 1900:             if (ref($settings) eq 'HASH') {
 1901:                 if (ref($settings->{$item}) eq 'HASH') {
 1902:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1903:                         $checked = '';
 1904:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1905:                         $checked = 'checked="checked" ';
 1906:                     }
 1907:                 }
 1908:             }
 1909:             $datatable .= '<span class="LC_nobreak"><label>'.
 1910:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1911:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1912:                           '</label></span>&nbsp; ';
 1913:         }
 1914:     }
 1915:     if ($context eq 'requestcourses') {
 1916:         $datatable .= '</tr><tr>';
 1917:         foreach my $item (@usertools) {
 1918:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1919:         }
 1920:         $datatable .= '</tr></table>';
 1921:     }
 1922:     $datatable .= '</td></tr>';
 1923:     $$rowtotal += $typecount;
 1924:     return $datatable;
 1925: }
 1926: 
 1927: sub print_requestmail {
 1928:     my ($dom,$action,$settings,$rowtotal) = @_;
 1929:     my ($now,$datatable,%currapp,$rows);
 1930:     $now = time;
 1931:     if (ref($settings) eq 'HASH') {
 1932:         if (ref($settings->{'notify'}) eq 'HASH') {
 1933:             if ($settings->{'notify'}{'approval'} ne '') {
 1934:                map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 1935:             }
 1936:         }
 1937:     }
 1938:     my $numinrow = 2;
 1939:     my $css_class = 'class="LC_odd_row"';
 1940:     my $text;
 1941:     if ($action eq 'requestcourses') {
 1942:         $text = &mt('Receive notification of course requests requiring approval');
 1943:     } else {
 1944:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1945:     }
 1946:     $datatable = '<tr '.$css_class.'>'.
 1947:                  ' <td>'.$text.'</td>'.
 1948:                  ' <td class="LC_left_item">';
 1949:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 1950:                                                  'reqapprovalnotify',%currapp);
 1951:     if ($numdc > 0) {
 1952:         $datatable .= $table;
 1953:     } else {
 1954:         $datatable .= &mt('There are no active Domain Coordinators');
 1955:     }
 1956:     $datatable .='</td></tr>';
 1957:     $$rowtotal += $rows;
 1958:     return $datatable;
 1959: }
 1960: 
 1961: sub print_autoenroll {
 1962:     my ($dom,$settings,$rowtotal) = @_;
 1963:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1964:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1965:     if (ref($settings) eq 'HASH') {
 1966:         if (exists($settings->{'run'})) {
 1967:             if ($settings->{'run'} eq '0') {
 1968:                 $runoff = ' checked="checked" ';
 1969:                 $runon = ' ';
 1970:             } else {
 1971:                 $runon = ' checked="checked" ';
 1972:                 $runoff = ' ';
 1973:             }
 1974:         } else {
 1975:             if ($autorun) {
 1976:                 $runon = ' checked="checked" ';
 1977:                 $runoff = ' ';
 1978:             } else {
 1979:                 $runoff = ' checked="checked" ';
 1980:                 $runon = ' ';
 1981:             }
 1982:         }
 1983:         if (exists($settings->{'co-owners'})) {
 1984:             if ($settings->{'co-owners'} eq '0') {
 1985:                 $coownersoff = ' checked="checked" ';
 1986:                 $coownerson = ' ';
 1987:             } else {
 1988:                 $coownerson = ' checked="checked" ';
 1989:                 $coownersoff = ' ';
 1990:             }
 1991:         } else {
 1992:             $coownersoff = ' checked="checked" ';
 1993:             $coownerson = ' ';
 1994:         }
 1995:         if (exists($settings->{'sender_domain'})) {
 1996:             $defdom = $settings->{'sender_domain'};
 1997:         }
 1998:     } else {
 1999:         if ($autorun) {
 2000:             $runon = ' checked="checked" ';
 2001:             $runoff = ' ';
 2002:         } else {
 2003:             $runoff = ' checked="checked" ';
 2004:             $runon = ' ';
 2005:         }
 2006:     }
 2007:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2008:     my $notif_sender;
 2009:     if (ref($settings) eq 'HASH') {
 2010:         $notif_sender = $settings->{'sender_uname'};
 2011:     }
 2012:     my $datatable='<tr class="LC_odd_row">'.
 2013:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2014:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2015:                   '<input type="radio" name="autoenroll_run"'.
 2016:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2017:                   '<label><input type="radio" name="autoenroll_run"'.
 2018:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2019:                   '</tr><tr>'.
 2020:                   '<td>'.&mt('Notification messages - sender').
 2021:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2022:                   &mt('username').':&nbsp;'.
 2023:                   '<input type="text" name="sender_uname" value="'.
 2024:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2025:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2026:                   '<tr class="LC_odd_row">'.
 2027:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2028:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2029:                   '<input type="radio" name="autoassign_coowners"'.
 2030:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2031:                   '<label><input type="radio" name="autoassign_coowners"'.
 2032:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2033:                   '</tr>';
 2034:     $$rowtotal += 3;
 2035:     return $datatable;
 2036: }
 2037: 
 2038: sub print_autoupdate {
 2039:     my ($position,$dom,$settings,$rowtotal) = @_;
 2040:     my $datatable;
 2041:     if ($position eq 'top') {
 2042:         my $updateon = ' ';
 2043:         my $updateoff = ' checked="checked" ';
 2044:         my $classlistson = ' ';
 2045:         my $classlistsoff = ' checked="checked" ';
 2046:         if (ref($settings) eq 'HASH') {
 2047:             if ($settings->{'run'} eq '1') {
 2048:                 $updateon = $updateoff;
 2049:                 $updateoff = ' ';
 2050:             }
 2051:             if ($settings->{'classlists'} eq '1') {
 2052:                 $classlistson = $classlistsoff;
 2053:                 $classlistsoff = ' ';
 2054:             }
 2055:         }
 2056:         my %title = (
 2057:                    run => 'Auto-update active?',
 2058:                    classlists => 'Update information in classlists?',
 2059:                     );
 2060:         $datatable = '<tr class="LC_odd_row">'. 
 2061:                   '<td>'.&mt($title{'run'}).'</td>'.
 2062:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2063:                   '<input type="radio" name="autoupdate_run"'.
 2064:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2065:                   '<label><input type="radio" name="autoupdate_run"'.
 2066:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2067:                   '</tr><tr>'.
 2068:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2069:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2070:                   '<label><input type="radio" name="classlists"'.
 2071:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2072:                   '<label><input type="radio" name="classlists"'.
 2073:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2074:                   '</tr>';
 2075:         $$rowtotal += 2;
 2076:     } elsif ($position eq 'middle') {
 2077:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2078:         my $numinrow = 3;
 2079:         my $locknamesettings;
 2080:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2081:                                      $dom,$numinrow,$othertitle,
 2082:                                     'lockablenames');
 2083:         $$rowtotal ++;
 2084:     } else {
 2085:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2086:         my @fields = ('lastname','firstname','middlename','generation',
 2087:                       'permanentemail','id');
 2088:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2089:         my $numrows = 0;
 2090:         if (ref($types) eq 'ARRAY') {
 2091:             if (@{$types} > 0) {
 2092:                 $datatable = 
 2093:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2094:                                          \@fields,$types,\$numrows);
 2095:                     $$rowtotal += @{$types}; 
 2096:             }
 2097:         }
 2098:         $datatable .= 
 2099:             &usertype_update_row($settings,{'default' => $othertitle},
 2100:                                  \%fieldtitles,\@fields,['default'],
 2101:                                  \$numrows);
 2102:         $$rowtotal ++;     
 2103:     }
 2104:     return $datatable;
 2105: }
 2106: 
 2107: sub print_autocreate {
 2108:     my ($dom,$settings,$rowtotal) = @_;
 2109:     my (%createon,%createoff,%currhash);
 2110:     my @types = ('xml','req');
 2111:     if (ref($settings) eq 'HASH') {
 2112:         foreach my $item (@types) {
 2113:             $createoff{$item} = ' checked="checked" ';
 2114:             $createon{$item} = ' ';
 2115:             if (exists($settings->{$item})) {
 2116:                 if ($settings->{$item}) {
 2117:                     $createon{$item} = ' checked="checked" ';
 2118:                     $createoff{$item} = ' ';
 2119:                 }
 2120:             }
 2121:         }
 2122:         if ($settings->{'xmldc'} ne '') { 
 2123:             $currhash{$settings->{'xmldc'}} = 1;
 2124:         }
 2125:     } else {
 2126:         foreach my $item (@types) {
 2127:             $createoff{$item} = ' checked="checked" ';
 2128:             $createon{$item} = ' ';
 2129:         }
 2130:     }
 2131:     $$rowtotal += 2;
 2132:     my $numinrow = 2;
 2133:     my $datatable='<tr class="LC_odd_row">'.
 2134:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2135:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2136:                   '<input type="radio" name="autocreate_xml"'.
 2137:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2138:                   '<label><input type="radio" name="autocreate_xml"'.
 2139:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2140:                   '</td></tr><tr>'.
 2141:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2142:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2143:                   '<input type="radio" name="autocreate_req"'.
 2144:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2145:                   '<label><input type="radio" name="autocreate_req"'.
 2146:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2147:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2148:                                                    'autocreate_xmldc',%currhash);
 2149:     if ($numdc > 1) {
 2150:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2151:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2152:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2153:     } else {
 2154:         $datatable .= $dctable.'</td></tr>';
 2155:     }
 2156:     $$rowtotal += $rows;
 2157:     return $datatable;
 2158: }
 2159: 
 2160: sub print_directorysrch {
 2161:     my ($dom,$settings,$rowtotal) = @_;
 2162:     my $srchon = ' ';
 2163:     my $srchoff = ' checked="checked" ';
 2164:     my ($exacton,$containson,$beginson);
 2165:     my $localon = ' ';
 2166:     my $localoff = ' checked="checked" ';
 2167:     if (ref($settings) eq 'HASH') {
 2168:         if ($settings->{'available'} eq '1') {
 2169:             $srchon = $srchoff;
 2170:             $srchoff = ' ';
 2171:         }
 2172:         if ($settings->{'localonly'} eq '1') {
 2173:             $localon = $localoff;
 2174:             $localoff = ' ';
 2175:         }
 2176:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2177:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2178:                 if ($type eq 'exact') {
 2179:                     $exacton = ' checked="checked" ';
 2180:                 } elsif ($type eq 'contains') {
 2181:                     $containson = ' checked="checked" ';
 2182:                 } elsif ($type eq 'begins') {
 2183:                     $beginson = ' checked="checked" ';
 2184:                 }
 2185:             }
 2186:         } else {
 2187:             if ($settings->{'searchtypes'} eq 'exact') {
 2188:                 $exacton = ' checked="checked" ';
 2189:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2190:                 $containson = ' checked="checked" ';
 2191:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2192:                 $exacton = ' checked="checked" ';
 2193:                 $containson = ' checked="checked" ';
 2194:             }
 2195:         }
 2196:     }
 2197:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2198:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2199: 
 2200:     my $numinrow = 4;
 2201:     my $cansrchrow = 0;
 2202:     my $datatable='<tr class="LC_odd_row">'.
 2203:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2204:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2205:                   '<input type="radio" name="dirsrch_available"'.
 2206:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2207:                   '<label><input type="radio" name="dirsrch_available"'.
 2208:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2209:                   '</tr><tr>'.
 2210:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2211:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2212:                   '<input type="radio" name="dirsrch_localonly"'.
 2213:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2214:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2215:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2216:                   '</tr>';
 2217:     $$rowtotal += 2;
 2218:     if (ref($usertypes) eq 'HASH') {
 2219:         if (keys(%{$usertypes}) > 0) {
 2220:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2221:                                          $numinrow,$othertitle,'cansearch');
 2222:             $cansrchrow = 1;
 2223:         }
 2224:     }
 2225:     if ($cansrchrow) {
 2226:         $$rowtotal ++;
 2227:         $datatable .= '<tr>';
 2228:     } else {
 2229:         $datatable .= '<tr class="LC_odd_row">';
 2230:     }
 2231:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2232:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2233:     foreach my $title (@{$titleorder}) {
 2234:         if (defined($searchtitles->{$title})) {
 2235:             my $check = ' ';
 2236:             if (ref($settings) eq 'HASH') {
 2237:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2238:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2239:                         $check = ' checked="checked" ';
 2240:                     }
 2241:                 }
 2242:             }
 2243:             $datatable .= '<td class="LC_left_item">'.
 2244:                           '<span class="LC_nobreak"><label>'.
 2245:                           '<input type="checkbox" name="searchby" '.
 2246:                           'value="'.$title.'"'.$check.'/>'.
 2247:                           $searchtitles->{$title}.'</label></span></td>';
 2248:         }
 2249:     }
 2250:     $datatable .= '</tr></table></td></tr>';
 2251:     $$rowtotal ++;
 2252:     if ($cansrchrow) {
 2253:         $datatable .= '<tr class="LC_odd_row">';
 2254:     } else {
 2255:         $datatable .= '<tr>';
 2256:     }
 2257:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2258:                   '<td class="LC_left_item" colspan="2">'.
 2259:                   '<span class="LC_nobreak"><label>'.
 2260:                   '<input type="checkbox" name="searchtypes" '.
 2261:                   $exacton.' value="exact" />'.&mt('Exact match').
 2262:                   '</label>&nbsp;'.
 2263:                   '<label><input type="checkbox" name="searchtypes" '.
 2264:                   $beginson.' value="begins" />'.&mt('Begins with').
 2265:                   '</label>&nbsp;'.
 2266:                   '<label><input type="checkbox" name="searchtypes" '.
 2267:                   $containson.' value="contains" />'.&mt('Contains').
 2268:                   '</label></span></td></tr>';
 2269:     $$rowtotal ++;
 2270:     return $datatable;
 2271: }
 2272: 
 2273: sub print_contacts {
 2274:     my ($dom,$settings,$rowtotal) = @_;
 2275:     my $datatable;
 2276:     my @contacts = ('adminemail','supportemail');
 2277:     my (%checked,%to,%otheremails,%bccemails);
 2278:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2279:                     'requestsmail','updatesmail');
 2280:     foreach my $type (@mailings) {
 2281:         $otheremails{$type} = '';
 2282:     }
 2283:     $bccemails{'helpdeskmail'} = '';
 2284:     if (ref($settings) eq 'HASH') {
 2285:         foreach my $item (@contacts) {
 2286:             if (exists($settings->{$item})) {
 2287:                 $to{$item} = $settings->{$item};
 2288:             }
 2289:         }
 2290:         foreach my $type (@mailings) {
 2291:             if (exists($settings->{$type})) {
 2292:                 if (ref($settings->{$type}) eq 'HASH') {
 2293:                     foreach my $item (@contacts) {
 2294:                         if ($settings->{$type}{$item}) {
 2295:                             $checked{$type}{$item} = ' checked="checked" ';
 2296:                         }
 2297:                     }
 2298:                     $otheremails{$type} = $settings->{$type}{'others'};
 2299:                     if ($type eq 'helpdeskmail') {
 2300:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2301:                     }
 2302:                 }
 2303:             } elsif ($type eq 'lonstatusmail') {
 2304:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2305:             }
 2306:         }
 2307:     } else {
 2308:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2309:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2310:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2311:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2312:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2313:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2314:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2315:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2316:     }
 2317:     my ($titles,$short_titles) = &contact_titles();
 2318:     my $rownum = 0;
 2319:     my $css_class;
 2320:     foreach my $item (@contacts) {
 2321:         $rownum ++;
 2322:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2323:         $datatable .= '<tr'.$css_class.'>'. 
 2324:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2325:                   '</span></td><td class="LC_right_item">'.
 2326:                   '<input type="text" name="'.$item.'" value="'.
 2327:                   $to{$item}.'" /></td></tr>';
 2328:     }
 2329:     foreach my $type (@mailings) {
 2330:         $rownum ++;
 2331:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2332:         $datatable .= '<tr'.$css_class.'>'.
 2333:                       '<td><span class="LC_nobreak">'.
 2334:                       $titles->{$type}.': </span></td>'.
 2335:                       '<td class="LC_left_item">'.
 2336:                       '<span class="LC_nobreak">';
 2337:         foreach my $item (@contacts) {
 2338:             $datatable .= '<label>'.
 2339:                           '<input type="checkbox" name="'.$type.'"'.
 2340:                           $checked{$type}{$item}.
 2341:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2342:                           '</label>&nbsp;';
 2343:         }
 2344:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2345:                       '<input type="text" name="'.$type.'_others" '.
 2346:                       'value="'.$otheremails{$type}.'"  />';
 2347:         if ($type eq 'helpdeskmail') {
 2348:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2349:                           '<input type="text" name="'.$type.'_bcc" '.
 2350:                           'value="'.$bccemails{$type}.'"  />';
 2351:         }
 2352:         $datatable .= '</td></tr>'."\n";
 2353:     }
 2354:     $$rowtotal += $rownum;
 2355:     return $datatable;
 2356: }
 2357: 
 2358: sub print_helpsettings {
 2359:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2360:     my ($datatable,$itemcount);
 2361:     $itemcount = 1;
 2362:     my (%choices,%defaultchecked,@toggles);
 2363:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2364:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2365:                                  &mt('LON-CAPA bug tracker'),600,500));
 2366:     %defaultchecked = ('submitbugs' => 'on');
 2367:     @toggles = ('submitbugs',);
 2368: 
 2369:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2370:                                                  \%choices,$itemcount);
 2371:     return $datatable;
 2372: }
 2373: 
 2374: sub radiobutton_prefs {
 2375:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount) = @_;
 2376:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2377:                    (ref($choices) eq 'HASH'));
 2378: 
 2379:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2380: 
 2381:     foreach my $item (@{$toggles}) {
 2382:         if ($defaultchecked->{$item} eq 'on') {
 2383:             $checkedon{$item} = ' checked="checked" ';
 2384:             $checkedoff{$item} = ' ';
 2385:         } elsif ($defaultchecked->{$item} eq 'off') {
 2386:             $checkedoff{$item} = ' checked="checked" ';
 2387:             $checkedon{$item} = ' ';
 2388:         }
 2389:     }
 2390:     if (ref($settings) eq 'HASH') {
 2391:         foreach my $item (@{$toggles}) {
 2392:             if ($settings->{$item} eq '1') {
 2393:                 $checkedon{$item} =  ' checked="checked" ';
 2394:                 $checkedoff{$item} = ' ';
 2395:             } elsif ($settings->{$item} eq '0') {
 2396:                 $checkedoff{$item} =  ' checked="checked" ';
 2397:                 $checkedon{$item} = ' ';
 2398:             }
 2399:         }
 2400:     }
 2401:     foreach my $item (@{$toggles}) {
 2402:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2403:         $datatable .=
 2404:             '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices->{$item}.
 2405:             '</span></td>'.
 2406:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2407:             '<label><input type="radio" name="'.
 2408:             $item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').
 2409:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2410:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2411:             '</span></td>'.
 2412:             '</tr>';
 2413:         $itemcount ++;
 2414:     }
 2415:     return ($datatable,$itemcount);
 2416: }
 2417: 
 2418: sub print_coursedefaults {
 2419:     my ($position,$dom,$settings,$rowtotal) = @_;
 2420:     my ($css_class,$datatable);
 2421:     my $itemcount = 1;
 2422:     if ($position eq 'top') {
 2423:         my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2424:         %choices =
 2425:             &Apache::lonlocal::texthash (
 2426:                 canuse_pdfforms => 'Course/Community users can create/upload PDF forms',
 2427:         );
 2428:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2429:         @toggles = ('canuse_pdfforms',);
 2430:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2431:                                                  \%choices,$itemcount);
 2432:         $$rowtotal += $itemcount;
 2433:     } else {
 2434:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2435:         my %choices =
 2436:             &Apache::lonlocal::texthash (
 2437:                 anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2438:         );
 2439:         my $currdefresponder;
 2440:         if (ref($settings) eq 'HASH') {
 2441:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2442:         }
 2443:         if (!$currdefresponder) {
 2444:             $currdefresponder = 10;
 2445:         } elsif ($currdefresponder < 1) {
 2446:             $currdefresponder = 1;
 2447:         }
 2448:         $datatable .=
 2449:                '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices{'anonsurvey_threshold'}.
 2450:                 '</span></td>'.
 2451:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2452:                 '<input type="text" name="anonsurvey_threshold"'.
 2453:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2454:                 '</td></tr>';
 2455:     }
 2456:     return $datatable;
 2457: }
 2458: 
 2459: sub print_usersessions {
 2460:     my ($position,$dom,$settings,$rowtotal) = @_;
 2461:     my ($css_class,$datatable,%checked,%choices);
 2462:     my (%by_ip,%by_location,@intdoms);
 2463:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2464: 
 2465:     my @alldoms = &Apache::lonnet::all_domains();
 2466:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2467:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2468:     my %altids = &id_for_thisdom(%servers);
 2469:     my $itemcount = 1;
 2470:     if ($position eq 'top') {
 2471:         if (keys(%serverhomes) > 1) {
 2472:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2473:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2474:         } else {
 2475:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2476:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2477:         }
 2478:     } else {
 2479:         if (keys(%by_location) == 0) {
 2480:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2481:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2482:         } else {
 2483:             my %lt = &usersession_titles();
 2484:             my $numinrow = 5;
 2485:             my $prefix;
 2486:             my @types;
 2487:             if ($position eq 'bottom') {
 2488:                 $prefix = 'remote';
 2489:                 @types = ('version','excludedomain','includedomain');
 2490:             } else {
 2491:                 $prefix = 'hosted';
 2492:                 @types = ('excludedomain','includedomain');
 2493:             }
 2494:             my (%current,%checkedon,%checkedoff);
 2495:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2496:             my @locations = sort(keys(%by_location));
 2497:             foreach my $type (@types) {
 2498:                 $checkedon{$type} = '';
 2499:                 $checkedoff{$type} = ' checked="checked"';
 2500:             }
 2501:             if (ref($settings) eq 'HASH') {
 2502:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2503:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2504:                         $current{$key} = $settings->{$prefix}{$key};
 2505:                         if ($key eq 'version') {
 2506:                             if ($current{$key} ne '') {
 2507:                                 $checkedon{$key} = ' checked="checked"';
 2508:                                 $checkedoff{$key} = '';
 2509:                             }
 2510:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2511:                             $checkedon{$key} = ' checked="checked"';
 2512:                             $checkedoff{$key} = '';
 2513:                         }
 2514:                     }
 2515:                 }
 2516:             }
 2517:             foreach my $type (@types) {
 2518:                 next if ($type ne 'version' && !@locations);
 2519:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2520:                 $datatable .= '<tr'.$css_class.'>
 2521:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2522:                                <span class="LC_nobreak">&nbsp;
 2523:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2524:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2525:                 if ($type eq 'version') {
 2526:                     my $selector = '<select name="'.$prefix.'_version">';
 2527:                     foreach my $version (@lcversions) {
 2528:                         my $selected = '';
 2529:                         if ($current{'version'} eq $version) {
 2530:                             $selected = ' selected="selected"';
 2531:                         }
 2532:                         $selector .= ' <option value="'.$version.'"'.
 2533:                                      $selected.'>'.$version.'</option>';
 2534:                     }
 2535:                     $selector .= '</select> ';
 2536:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2537:                 } else {
 2538:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2539:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2540:                                  ' />'.('&nbsp;'x2).
 2541:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2542:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2543:                                  "\n".
 2544:                                  '</div><div><table>';
 2545:                     my $rem;
 2546:                     for (my $i=0; $i<@locations; $i++) {
 2547:                         my ($showloc,$value,$checkedtype);
 2548:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2549:                             my $ip = $by_location{$locations[$i]}->[0];
 2550:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2551:                                  $value = join(':',@{$by_ip{$ip}});
 2552:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2553:                                 if (ref($current{$type}) eq 'ARRAY') {
 2554:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2555:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2556:                                             $checkedtype = ' checked="checked"';
 2557:                                             last;
 2558:                                         }
 2559:                                     }
 2560:                                 }
 2561:                             }
 2562:                         }
 2563:                         $rem = $i%($numinrow);
 2564:                         if ($rem == 0) {
 2565:                             if ($i > 0) {
 2566:                                 $datatable .= '</tr>';
 2567:                             }
 2568:                             $datatable .= '<tr>';
 2569:                         }
 2570:                         $datatable .= '<td class="LC_left_item">'.
 2571:                                       '<span class="LC_nobreak"><label>'.
 2572:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2573:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2574:                                       '</label></span></td>';
 2575:                     }
 2576:                     $rem = @locations%($numinrow);
 2577:                     my $colsleft = $numinrow - $rem;
 2578:                     if ($colsleft > 1 ) {
 2579:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2580:                                       '&nbsp;</td>';
 2581:                     } elsif ($colsleft == 1) {
 2582:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2583:                     }
 2584:                     $datatable .= '</tr></table>';
 2585:                 }
 2586:                 $datatable .= '</td></tr>';
 2587:                 $itemcount ++;
 2588:             }
 2589:         }
 2590:     }
 2591:     $$rowtotal += $itemcount;
 2592:     return $datatable;
 2593: }
 2594: 
 2595: sub build_location_hashes {
 2596:     my ($intdoms,$by_ip,$by_location) = @_;
 2597:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2598:                   (ref($by_location) eq 'HASH')); 
 2599:     my %iphost = &Apache::lonnet::get_iphost();
 2600:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2601:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2602:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2603:         foreach my $id (@{$iphost{$primary_ip}}) {
 2604:             my $intdom = &Apache::lonnet::internet_dom($id);
 2605:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2606:                 push(@{$intdoms},$intdom);
 2607:             }
 2608:         }
 2609:     }
 2610:     foreach my $ip (keys(%iphost)) {
 2611:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2612:             foreach my $id (@{$iphost{$ip}}) {
 2613:                 my $location = &Apache::lonnet::internet_dom($id);
 2614:                 if ($location) {
 2615:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2616:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2617:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2618:                             push(@{$by_ip->{$ip}},$location);
 2619:                         }
 2620:                     } else {
 2621:                         $by_ip->{$ip} = [$location];
 2622:                     }
 2623:                 }
 2624:             }
 2625:         }
 2626:     }
 2627:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2628:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2629:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2630:             my $first = $by_ip->{$ip}->[0];
 2631:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2632:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2633:                     push(@{$by_location->{$first}},$ip);
 2634:                 }
 2635:             } else {
 2636:                 $by_location->{$first} = [$ip];
 2637:             }
 2638:         }
 2639:     }
 2640:     return;
 2641: }
 2642: 
 2643: sub current_offloads_to {
 2644:     my ($dom,$settings,$servers) = @_;
 2645:     my (%spareid,%otherdomconfigs);
 2646:     if (ref($servers) eq 'HASH') {
 2647:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2648:             my $gotspares;
 2649:             if (ref($settings) eq 'HASH') {
 2650:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2651:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2652:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2653:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2654:                         $gotspares = 1;
 2655:                     }
 2656:                 }
 2657:             }
 2658:             unless ($gotspares) {
 2659:                 my $gotspares;
 2660:                 my $serverhomeID =
 2661:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2662:                 my $serverhomedom =
 2663:                     &Apache::lonnet::host_domain($serverhomeID);
 2664:                 if ($serverhomedom ne $dom) {
 2665:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2666:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2667:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2668:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2669:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2670:                                 $gotspares = 1;
 2671:                             }
 2672:                         }
 2673:                     } else {
 2674:                         $otherdomconfigs{$serverhomedom} =
 2675:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2676:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2677:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2678:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2679:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2680:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2681:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2682:                                         $gotspares = 1;
 2683:                                     }
 2684:                                 }
 2685:                             }
 2686:                         }
 2687:                     }
 2688:                 }
 2689:             }
 2690:             unless ($gotspares) {
 2691:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2692:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2693:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2694:                } else {
 2695:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2696:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2697:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2698:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2699:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2700:                     } else {
 2701:                         my %what = (
 2702:                              spareid => 1,
 2703:                         );
 2704:                         my ($result,$returnhash) = 
 2705:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2706:                         if ($result eq 'ok') { 
 2707:                             if (ref($returnhash) eq 'HASH') {
 2708:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2709:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2710:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2711:                                 }
 2712:                             }
 2713:                         }
 2714:                     }
 2715:                 }
 2716:             }
 2717:         }
 2718:     }
 2719:     return %spareid;
 2720: }
 2721: 
 2722: sub spares_row {
 2723:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2724:     my $css_class;
 2725:     my $numinrow = 4;
 2726:     my $itemcount = 1;
 2727:     my $datatable;
 2728:     my %typetitles = &sparestype_titles();
 2729:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2730:         foreach my $server (sort(keys(%{$servers}))) {
 2731:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2732:             my ($othercontrol,$serverdom);
 2733:             if ($serverhome ne $server) {
 2734:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2735:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2736:             } else {
 2737:                 $serverdom = &Apache::lonnet::host_domain($server);
 2738:                 if ($serverdom ne $dom) {
 2739:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2740:                 }
 2741:             }
 2742:             next unless (ref($spareid->{$server}) eq 'HASH');
 2743:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2744:             $datatable .= '<tr'.$css_class.'>
 2745:                            <td rowspan="2">
 2746:                             <span class="LC_nobreak">'.
 2747:                           &mt('[_1] when busy, offloads to:'
 2748:                               ,'<b>'.$server.'</b>').
 2749:                           "\n";
 2750:             my (%current,%canselect);
 2751:             my @choices = 
 2752:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2753:             foreach my $type ('primary','default') {
 2754:                 if (ref($spareid->{$server}) eq 'HASH') {
 2755:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2756:                         my @spares = @{$spareid->{$server}{$type}};
 2757:                         if (@spares > 0) {
 2758:                             if ($othercontrol) {
 2759:                                 $current{$type} = join(', ',@spares);
 2760:                             } else {
 2761:                                 $current{$type} .= '<table>';
 2762:                                 my $numspares = scalar(@spares);
 2763:                                 for (my $i=0;  $i<@spares; $i++) {
 2764:                                     my $rem = $i%($numinrow);
 2765:                                     if ($rem == 0) {
 2766:                                         if ($i > 0) {
 2767:                                             $current{$type} .= '</tr>';
 2768:                                         }
 2769:                                         $current{$type} .= '<tr>';
 2770:                                     }
 2771:                                     $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;'.
 2772:                                                        $spareid->{$server}{$type}[$i].
 2773:                                                        '</label></td>'."\n";
 2774:                                 }
 2775:                                 my $rem = @spares%($numinrow);
 2776:                                 my $colsleft = $numinrow - $rem;
 2777:                                 if ($colsleft > 1 ) {
 2778:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2779:                                                        '" class="LC_left_item">'.
 2780:                                                        '&nbsp;</td>';
 2781:                                 } elsif ($colsleft == 1) {
 2782:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2783:                                 }
 2784:                                 $current{$type} .= '</tr></table>';
 2785:                             }
 2786:                         }
 2787:                     }
 2788:                     if ($current{$type} eq '') {
 2789:                         $current{$type} = &mt('None specified');
 2790:                     }
 2791:                     if ($othercontrol) {
 2792:                         if ($type eq 'primary') {
 2793:                             $canselect{$type} = $othercontrol;
 2794:                         }
 2795:                     } else {
 2796:                         $canselect{$type} = 
 2797:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2798:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2799:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2800:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2801:                         if (@choices > 0) {
 2802:                             foreach my $lonhost (@choices) {
 2803:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2804:                             }
 2805:                         }
 2806:                         $canselect{$type} .= '</select>'."\n";
 2807:                     }
 2808:                 } else {
 2809:                     $current{$type} = &mt('Could not be determined');
 2810:                     if ($type eq 'primary') {
 2811:                         $canselect{$type} =  $othercontrol;
 2812:                     }
 2813:                 }
 2814:                 if ($type eq 'default') {
 2815:                     $datatable .= '<tr'.$css_class.'>';
 2816:                 }
 2817:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2818:                               '<td>'.$current{$type}.'</td>'."\n".
 2819:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2820:             }
 2821:             $itemcount ++;
 2822:         }
 2823:     }
 2824:     $$rowtotal += $itemcount;
 2825:     return $datatable;
 2826: }
 2827: 
 2828: sub possible_newspares {
 2829:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2830:     my $serverhostname = &Apache::lonnet::hostname($server);
 2831:     my %excluded;
 2832:     if ($serverhostname ne '') {
 2833:         %excluded = (
 2834:                        $serverhostname => 1,
 2835:                     );
 2836:     }
 2837:     if (ref($currspares) eq 'HASH') {
 2838:         foreach my $type (keys(%{$currspares})) {
 2839:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2840:                 if (@{$currspares->{$type}} > 0) {
 2841:                     foreach my $curr (@{$currspares->{$type}}) {
 2842:                         my $hostname = &Apache::lonnet::hostname($curr);
 2843:                         $excluded{$hostname} = 1;
 2844:                     }
 2845:                 }
 2846:             }
 2847:         }
 2848:     }
 2849:     my @choices;
 2850:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2851:         if (keys(%{$serverhomes}) > 1) {
 2852:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2853:                 unless ($excluded{$name}) {
 2854:                     if (exists($altids->{$serverhomes->{$name}})) {
 2855:                         push(@choices,$altids->{$serverhomes->{$name}});
 2856:                     } else {
 2857:                         push(@choices,$serverhomes->{$name});
 2858:                     }
 2859:                 }
 2860:             }
 2861:         }
 2862:     }
 2863:     return sort(@choices);
 2864: }
 2865: 
 2866: sub print_loadbalancing {
 2867:     my ($dom,$settings,$rowtotal) = @_;
 2868:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2869:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2870:     my $numinrow = 1;
 2871:     my $datatable;
 2872:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2873:     my (%currbalancer,%currtargets,%currrules,%existing);
 2874:     if (ref($settings) eq 'HASH') {
 2875:         %existing = %{$settings};
 2876:     }
 2877:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 2878:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 2879:                                   \%currtargets,\%currrules);
 2880:     } else {
 2881:         return;
 2882:     }
 2883:     my ($othertitle,$usertypes,$types) =
 2884:         &Apache::loncommon::sorted_inst_types($dom);
 2885:     my $rownum = 6;
 2886:     if (ref($types) eq 'ARRAY') {
 2887:         $rownum += scalar(@{$types});
 2888:     }
 2889:     my @css_class = ('LC_odd_row','LC_even_row');
 2890:     my $balnum = 0;
 2891:     my $islast;
 2892:     my (@toshow,$disabledtext);
 2893:     if (keys(%currbalancer) > 0) {
 2894:         @toshow = sort(keys(%currbalancer));
 2895:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 2896:             push(@toshow,'');
 2897:         }
 2898:     } else {
 2899:         @toshow = ('');
 2900:         $disabledtext = &mt('No existing load balancer');
 2901:     }
 2902:     foreach my $lonhost (@toshow) {
 2903:         if ($balnum == scalar(@toshow)-1) {
 2904:             $islast = 1;
 2905:         } else {
 2906:             $islast = 0;
 2907:         }
 2908:         my $cssidx = $balnum%2;
 2909:         my $targets_div_style = 'display: none';
 2910:         my $disabled_div_style = 'display: block';
 2911:         my $homedom_div_style = 'display: none';
 2912:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 2913:                       '<td rowspan="'.$rownum.'" valign="top">'.
 2914:                       '<p>';
 2915:         if ($lonhost eq '') {
 2916:             $datatable .= '<span class="LC_nobreak">'; 
 2917:             if (keys(%currbalancer) > 0) {
 2918:                 $datatable .= &mt('Add balancer:');
 2919:             } else {
 2920:                 $datatable .= &mt('Enable balancer:');
 2921:             }
 2922:             $datatable .= '&nbsp;'.
 2923:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 2924:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 2925:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 2926:                           '<option value="" selected="selected">'.&mt('None').
 2927:                           '</option>'."\n";
 2928:             foreach my $server (sort(keys(%servers))) {
 2929:                 next if ($currbalancer{$server});
 2930:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 2931:             }
 2932:             $datatable .= 
 2933:                 '</select>'."\n".
 2934:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 2935:         } else {
 2936:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 2937:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 2938:                            &mt('Stop balancing').'</label>'.
 2939:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 2940:             $targets_div_style = 'display: block';
 2941:             $disabled_div_style = 'display: none';
 2942:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 2943:                 $homedom_div_style = 'display: block';
 2944:             }
 2945:         }
 2946:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 2947:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 2948:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 2949:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2950:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 2951:         my @sparestypes = ('primary','default');
 2952:         my %typetitles = &sparestype_titles();
 2953:         foreach my $sparetype (@sparestypes) {
 2954:             my $targettable;
 2955:             for (my $i=0; $i<$numspares; $i++) {
 2956:                 my $checked;
 2957:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 2958:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 2959:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 2960:                             $checked = ' checked="checked"';
 2961:                         }
 2962:                     }
 2963:                 }
 2964:                 my ($chkboxval,$disabled);
 2965:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 2966:                     $chkboxval = $spares[$i];
 2967:                 }
 2968:                 if (exists($currbalancer{$spares[$i]})) {
 2969:                     $disabled = ' disabled="disabled"';
 2970:                 }
 2971:                 $targettable .= 
 2972:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 2973:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 2974:                     '</span></label></td>';
 2975:                 my $rem = $i%($numinrow);
 2976:                 if ($rem == 0) {
 2977:                     if (($i > 0) && ($i < $numspares-1)) {
 2978:                         $targettable .= '</tr>';
 2979:                     }
 2980:                     if ($i < $numspares-1) {
 2981:                         $targettable .= '<tr>';
 2982:                     }
 2983:                 }
 2984:             }
 2985:             if ($targettable ne '') {
 2986:                 my $rem = $numspares%($numinrow);
 2987:                 my $colsleft = $numinrow - $rem;
 2988:                 if ($colsleft > 1 ) {
 2989:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2990:                                     '&nbsp;</td>';
 2991:                 } elsif ($colsleft == 1) {
 2992:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 2993:                 }
 2994:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 2995:                                '<table><tr>'.$targettable.'</tr></table><br />';
 2996:             }
 2997:         }
 2998:         $datatable .= '</div></td></tr>'.
 2999:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3000:                                            $othertitle,$usertypes,$types,\%servers,
 3001:                                            \%currbalancer,$lonhost,
 3002:                                            $targets_div_style,$homedom_div_style,
 3003:                                            $css_class[$cssidx],$balnum,$islast);
 3004:         $$rowtotal += $rownum;
 3005:         $balnum ++;
 3006:     }
 3007:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3008:     return $datatable;
 3009: }
 3010: 
 3011: sub get_loadbalancers_config {
 3012:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3013:     return unless ((ref($servers) eq 'HASH') &&
 3014:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3015:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3016:     if (keys(%{$existing}) > 0) {
 3017:         my $oldlonhost;
 3018:         foreach my $key (sort(keys(%{$existing}))) {
 3019:             if ($key eq 'lonhost') {
 3020:                 $oldlonhost = $existing->{'lonhost'};
 3021:                 $currbalancer->{$oldlonhost} = 1;
 3022:             } elsif ($key eq 'targets') {
 3023:                 if ($oldlonhost) {
 3024:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3025:                 }
 3026:             } elsif ($key eq 'rules') {
 3027:                 if ($oldlonhost) {
 3028:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3029:                 }
 3030:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3031:                 $currbalancer->{$key} = 1;
 3032:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3033:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3034:             }
 3035:         }
 3036:     } else {
 3037:         my ($balancerref,$targetsref) =
 3038:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3039:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3040:             foreach my $server (sort(keys(%{$balancerref}))) {
 3041:                 $currbalancer->{$server} = 1;
 3042:                 $currtargets->{$server} = $targetsref->{$server};
 3043:             }
 3044:         }
 3045:     }
 3046:     return;
 3047: }
 3048: 
 3049: sub loadbalancing_rules {
 3050:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3051:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3052:         $css_class,$balnum,$islast) = @_;
 3053:     my $output;
 3054:     my $num = 0;
 3055:     my ($alltypes,$othertypes,$titles) = 
 3056:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3057:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3058:         foreach my $type (@{$alltypes}) {
 3059:             $num ++;
 3060:             my $current;
 3061:             if (ref($currrules) eq 'HASH') {
 3062:                 $current = $currrules->{$type};
 3063:             }
 3064:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3065:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3066:                     $current = '';
 3067:                 }
 3068:             }
 3069:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3070:                                              $servers,$currbalancer,$lonhost,$dom,
 3071:                                              $targets_div_style,$homedom_div_style,
 3072:                                              $css_class,$balnum,$num,$islast);
 3073:         }
 3074:     }
 3075:     return $output;
 3076: }
 3077: 
 3078: sub loadbalancing_titles {
 3079:     my ($dom,$intdom,$usertypes,$types) = @_;
 3080:     my %othertypes = (
 3081:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3082:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3083:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3084:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3085:                      );
 3086:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 3087:     if (ref($types) eq 'ARRAY') {
 3088:         unshift(@alltypes,@{$types},'default');
 3089:     }
 3090:     my %titles;
 3091:     foreach my $type (@alltypes) {
 3092:         if ($type =~ /^_LC_/) {
 3093:             $titles{$type} = $othertypes{$type};
 3094:         } elsif ($type eq 'default') {
 3095:             $titles{$type} = &mt('All users from [_1]',$dom);
 3096:             if (ref($types) eq 'ARRAY') {
 3097:                 if (@{$types} > 0) {
 3098:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3099:                 }
 3100:             }
 3101:         } elsif (ref($usertypes) eq 'HASH') {
 3102:             $titles{$type} = $usertypes->{$type};
 3103:         }
 3104:     }
 3105:     return (\@alltypes,\%othertypes,\%titles);
 3106: }
 3107: 
 3108: sub loadbalance_rule_row {
 3109:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3110:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3111:     my @rulenames = ('default','homeserver');
 3112:     my %ruletitles = &offloadtype_text();
 3113:     if ($type eq '_LC_external') {
 3114:         push(@rulenames,'externalbalancer');
 3115:     } else {
 3116:         push(@rulenames,'specific');
 3117:     }
 3118:     push(@rulenames,'none');
 3119:     my $style = $targets_div_style;
 3120:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3121:         $style = $homedom_div_style;
 3122:     }
 3123:     my $space;
 3124:     if ($islast && $num == 1) {
 3125:         $space = '<div display="inline-block">&nbsp;</div>';
 3126:     }
 3127:     my $output = 
 3128:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3129:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3130:         '<td valaign="top">'.$space.
 3131:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3132:     for (my $i=0; $i<@rulenames; $i++) {
 3133:         my $rule = $rulenames[$i];
 3134:         my ($checked,$extra);
 3135:         if ($rulenames[$i] eq 'default') {
 3136:             $rule = '';
 3137:         }
 3138:         if ($rulenames[$i] eq 'specific') {
 3139:             if (ref($servers) eq 'HASH') {
 3140:                 my $default;
 3141:                 if (($current ne '') && (exists($servers->{$current}))) {
 3142:                     $checked = ' checked="checked"';
 3143:                 }
 3144:                 unless ($checked) {
 3145:                     $default = ' selected="selected"';
 3146:                 }
 3147:                 $extra = 
 3148:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3149:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3150:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3151:                     '<option value=""'.$default.'></option>'."\n";
 3152:                 foreach my $server (sort(keys(%{$servers}))) {
 3153:                     if (ref($currbalancer) eq 'HASH') {
 3154:                         next if (exists($currbalancer->{$server}));
 3155:                     }
 3156:                     my $selected;
 3157:                     if ($server eq $current) {
 3158:                         $selected = ' selected="selected"';
 3159:                     }
 3160:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3161:                 }
 3162:                 $extra .= '</select>';
 3163:             }
 3164:         } elsif ($rule eq $current) {
 3165:             $checked = ' checked="checked"';
 3166:         }
 3167:         $output .= '<span class="LC_nobreak"><label>'.
 3168:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3169:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3170:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3171:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3172:                    '</label>'.$extra.'</span><br />'."\n";
 3173:     }
 3174:     $output .= '</div></td></tr>'."\n";
 3175:     return $output;
 3176: }
 3177: 
 3178: sub offloadtype_text {
 3179:     my %ruletitles = &Apache::lonlocal::texthash (
 3180:            'default'          => 'Offloads to default destinations',
 3181:            'homeserver'       => "Offloads to user's home server",
 3182:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3183:            'specific'         => 'Offloads to specific server',
 3184:            'none'             => 'No offload',
 3185:     );
 3186:     return %ruletitles;
 3187: }
 3188: 
 3189: sub sparestype_titles {
 3190:     my %typestitles = &Apache::lonlocal::texthash (
 3191:                           'primary' => 'primary',
 3192:                           'default' => 'default',
 3193:                       );
 3194:     return %typestitles;
 3195: }
 3196: 
 3197: sub contact_titles {
 3198:     my %titles = &Apache::lonlocal::texthash (
 3199:                    'supportemail' => 'Support E-mail address',
 3200:                    'adminemail'   => 'Default Server Admin E-mail address',
 3201:                    'errormail'    => 'Error reports to be e-mailed to',
 3202:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3203:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3204:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3205:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3206:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3207:                  );
 3208:     my %short_titles = &Apache::lonlocal::texthash (
 3209:                            adminemail   => 'Admin E-mail address',
 3210:                            supportemail => 'Support E-mail',
 3211:                        );   
 3212:     return (\%titles,\%short_titles);
 3213: }
 3214: 
 3215: sub tool_titles {
 3216:     my %titles = &Apache::lonlocal::texthash (
 3217:                      aboutme    => 'Personal web page',
 3218:                      blog       => 'Blog',
 3219:                      webdav     => 'WebDAV',
 3220:                      portfolio  => 'Portfolio',
 3221:                      official   => 'Official courses (with institutional codes)',
 3222:                      unofficial => 'Unofficial courses',
 3223:                      community  => 'Communities',
 3224:                  );
 3225:     return %titles;
 3226: }
 3227: 
 3228: sub courserequest_titles {
 3229:     my %titles = &Apache::lonlocal::texthash (
 3230:                                    official   => 'Official',
 3231:                                    unofficial => 'Unofficial',
 3232:                                    community  => 'Communities',
 3233:                                    norequest  => 'Not allowed',
 3234:                                    approval   => 'Approval by Dom. Coord.',
 3235:                                    validate   => 'With validation',
 3236:                                    autolimit  => 'Numerical limit',
 3237:                                    unlimited  => '(blank for unlimited)',
 3238:                  );
 3239:     return %titles;
 3240: }
 3241: 
 3242: sub authorrequest_titles {
 3243:     my %titles = &Apache::lonlocal::texthash (
 3244:                                    norequest  => 'Not allowed',
 3245:                                    approval   => 'Approval by Dom. Coord.',
 3246:                                    automatic  => 'Automatic approval',
 3247:                  );
 3248:     return %titles;
 3249: } 
 3250: 
 3251: sub courserequest_conditions {
 3252:     my %conditions = &Apache::lonlocal::texthash (
 3253:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3254:        validate   => '(Processing of request subject to instittutional validation).',
 3255:                  );
 3256:     return %conditions;
 3257: }
 3258: 
 3259: 
 3260: sub print_usercreation {
 3261:     my ($position,$dom,$settings,$rowtotal) = @_;
 3262:     my $numinrow = 4;
 3263:     my $datatable;
 3264:     if ($position eq 'top') {
 3265:         $$rowtotal ++;
 3266:         my $rowcount = 0;
 3267:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3268:         if (ref($rules) eq 'HASH') {
 3269:             if (keys(%{$rules}) > 0) {
 3270:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3271:                                                 $ruleorder,$numinrow,$rowcount);
 3272:                 $$rowtotal ++;
 3273:                 $rowcount ++;
 3274:             }
 3275:         }
 3276:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3277:         if (ref($idrules) eq 'HASH') {
 3278:             if (keys(%{$idrules}) > 0) {
 3279:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3280:                                                 $idruleorder,$numinrow,$rowcount);
 3281:                 $$rowtotal ++;
 3282:                 $rowcount ++;
 3283:             }
 3284:         }
 3285:         my ($emailrules,$emailruleorder) = 
 3286:             &Apache::lonnet::inst_userrules($dom,'email');
 3287:         if (ref($emailrules) eq 'HASH') {
 3288:             if (keys(%{$emailrules}) > 0) {
 3289:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3290:                                                 $emailruleorder,$numinrow,$rowcount);
 3291:                 $$rowtotal ++;
 3292:                 $rowcount ++;
 3293:             }
 3294:         }
 3295:         if ($rowcount == 0) {
 3296:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3297:             $$rowtotal ++;
 3298:             $rowcount ++;
 3299:         }
 3300:     } elsif ($position eq 'middle') {
 3301:         my @creators = ('author','course','requestcrs','selfcreate');
 3302:         my ($rules,$ruleorder) =
 3303:             &Apache::lonnet::inst_userrules($dom,'username');
 3304:         my %lt = &usercreation_types();
 3305:         my %checked;
 3306:         my @selfcreate; 
 3307:         if (ref($settings) eq 'HASH') {
 3308:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3309:                 foreach my $item (@creators) {
 3310:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3311:                 }
 3312:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3313:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3314:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3315:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3316:                         @selfcreate = ('email','login','sso');
 3317:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3318:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3319:                     }
 3320:                 }
 3321:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3322:                 foreach my $item (@creators) {
 3323:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3324:                         $checked{$item} = 'none';
 3325:                     }
 3326:                 }
 3327:             }
 3328:         }
 3329:         my $rownum = 0;
 3330:         foreach my $item (@creators) {
 3331:             $rownum ++;
 3332:             if ($item ne 'selfcreate') {  
 3333:                 if ($checked{$item} eq '') {
 3334:                     $checked{$item} = 'any';
 3335:                 }
 3336:             }
 3337:             my $css_class;
 3338:             if ($rownum%2) {
 3339:                 $css_class = '';
 3340:             } else {
 3341:                 $css_class = ' class="LC_odd_row" ';
 3342:             }
 3343:             $datatable .= '<tr'.$css_class.'>'.
 3344:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3345:                          '</span></td><td align="right">';
 3346:             my @options;
 3347:             if ($item eq 'selfcreate') {
 3348:                 push(@options,('email','login','sso'));
 3349:             } else {
 3350:                 @options = ('any');
 3351:                 if (ref($rules) eq 'HASH') {
 3352:                     if (keys(%{$rules}) > 0) {
 3353:                         push(@options,('official','unofficial'));
 3354:                     }
 3355:                 }
 3356:                 push(@options,'none');
 3357:             }
 3358:             foreach my $option (@options) {
 3359:                 my $type = 'radio';
 3360:                 my $check = ' ';
 3361:                 if ($item eq 'selfcreate') {
 3362:                     $type = 'checkbox';
 3363:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3364:                         $check = ' checked="checked" ';
 3365:                     }
 3366:                 } else {
 3367:                     if ($checked{$item} eq $option) {
 3368:                         $check = ' checked="checked" ';
 3369:                     }
 3370:                 } 
 3371:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3372:                               '<input type="'.$type.'" name="can_createuser_'.
 3373:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3374:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3375:             }
 3376:             $datatable .= '</td></tr>';
 3377:         }
 3378:         my ($othertitle,$usertypes,$types) =
 3379:             &Apache::loncommon::sorted_inst_types($dom);
 3380:         my $createsettings;
 3381:         if (ref($settings) eq 'HASH') {
 3382:             $createsettings = $settings->{cancreate};
 3383:         }
 3384:         if (ref($usertypes) eq 'HASH') {
 3385:             if (keys(%{$usertypes}) > 0) {
 3386:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3387:                                              $dom,$numinrow,$othertitle,
 3388:                                              'statustocreate');
 3389:                 $$rowtotal ++;
 3390:                 $rownum ++;
 3391:             }
 3392:         }
 3393:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3394:     } else {
 3395:         my @contexts = ('author','course','domain');
 3396:         my @authtypes = ('int','krb4','krb5','loc');
 3397:         my %checked;
 3398:         if (ref($settings) eq 'HASH') {
 3399:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3400:                 foreach my $item (@contexts) {
 3401:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3402:                         foreach my $auth (@authtypes) {
 3403:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3404:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3405:                             }
 3406:                         }
 3407:                     }
 3408:                 }
 3409:             }
 3410:         } else {
 3411:             foreach my $item (@contexts) {
 3412:                 foreach my $auth (@authtypes) {
 3413:                     $checked{$item}{$auth} = ' checked="checked" ';
 3414:                 }
 3415:             }
 3416:         }
 3417:         my %title = &context_names();
 3418:         my %authname = &authtype_names();
 3419:         my $rownum = 0;
 3420:         my $css_class; 
 3421:         foreach my $item (@contexts) {
 3422:             if ($rownum%2) {
 3423:                 $css_class = '';
 3424:             } else {
 3425:                 $css_class = ' class="LC_odd_row" ';
 3426:             }
 3427:             $datatable .=   '<tr'.$css_class.'>'.
 3428:                             '<td>'.$title{$item}.
 3429:                             '</td><td class="LC_left_item">'.
 3430:                             '<span class="LC_nobreak">';
 3431:             foreach my $auth (@authtypes) {
 3432:                 $datatable .= '<label>'. 
 3433:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3434:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3435:                               $authname{$auth}.'</label>&nbsp;';
 3436:             }
 3437:             $datatable .= '</span></td></tr>';
 3438:             $rownum ++;
 3439:         }
 3440:         $$rowtotal += $rownum;
 3441:     }
 3442:     return $datatable;
 3443: }
 3444: 
 3445: sub captcha_choice {
 3446:     my ($context,$settings,$itemcount) = @_;
 3447:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3448:     my %lt = &captcha_phrases();
 3449:     $keyentry = 'hidden';
 3450:     if ($context eq 'cancreate') {
 3451:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3452:     } elsif ($context eq 'login') {
 3453:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3454:     }
 3455:     if (ref($settings) eq 'HASH') {
 3456:         if ($settings->{'captcha'}) {
 3457:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3458:         } else {
 3459:             $checked{'original'} = ' checked="checked"';
 3460:         }
 3461:         if ($settings->{'captcha'} eq 'recaptcha') {
 3462:             $pubtext = $lt{'pub'};
 3463:             $privtext = $lt{'priv'};
 3464:             $keyentry = 'text';
 3465:         }
 3466:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3467:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3468:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3469:         }
 3470:     } else {
 3471:         $checked{'original'} = ' checked="checked"';
 3472:     }
 3473:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3474:     my $output = '<tr'.$css_class.'>'.
 3475:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3476:                  '<table><tr><td>'."\n";
 3477:     foreach my $option ('original','recaptcha','notused') {
 3478:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3479:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3480:                    $lt{$option}.'</label></span>';
 3481:         unless ($option eq 'notused') {
 3482:             $output .= ('&nbsp;'x2)."\n";
 3483:         }
 3484:     }
 3485: #
 3486: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3487: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3488: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu) 
 3489: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3490: #  
 3491:     $output .= '</td></tr>'."\n".
 3492:                '<tr><td>'."\n".
 3493:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3494:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3495:                $currpub.'" size="40" /></span><br />'."\n".
 3496:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3497:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3498:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3499:                '</td></tr>';
 3500:     return $output;
 3501: }
 3502: 
 3503: sub user_formats_row {
 3504:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3505:     my $output;
 3506:     my %text = (
 3507:                    'username' => 'new usernames',
 3508:                    'id'       => 'IDs',
 3509:                    'email'    => 'self-created accounts (e-mail)',
 3510:                );
 3511:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3512:     $output = '<tr '.$css_class.'>'.
 3513:               '<td><span class="LC_nobreak">';
 3514:     if ($type eq 'email') {
 3515:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3516:     } else {
 3517:         $output .= &mt("Format rules to check for $text{$type}: ");
 3518:     }
 3519:     $output .= '</span></td>'.
 3520:                '<td class="LC_left_item" colspan="2"><table>';
 3521:     my $rem;
 3522:     if (ref($ruleorder) eq 'ARRAY') {
 3523:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3524:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3525:                 my $rem = $i%($numinrow);
 3526:                 if ($rem == 0) {
 3527:                     if ($i > 0) {
 3528:                         $output .= '</tr>';
 3529:                     }
 3530:                     $output .= '<tr>';
 3531:                 }
 3532:                 my $check = ' ';
 3533:                 if (ref($settings) eq 'HASH') {
 3534:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3535:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3536:                             $check = ' checked="checked" ';
 3537:                         }
 3538:                     }
 3539:                 }
 3540:                 $output .= '<td class="LC_left_item">'.
 3541:                            '<span class="LC_nobreak"><label>'.
 3542:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3543:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3544:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3545:             }
 3546:         }
 3547:         $rem = @{$ruleorder}%($numinrow);
 3548:     }
 3549:     my $colsleft = $numinrow - $rem;
 3550:     if ($colsleft > 1 ) {
 3551:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3552:                    '&nbsp;</td>';
 3553:     } elsif ($colsleft == 1) {
 3554:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3555:     }
 3556:     $output .= '</tr></table></td></tr>';
 3557:     return $output;
 3558: }
 3559: 
 3560: sub usercreation_types {
 3561:     my %lt = &Apache::lonlocal::texthash (
 3562:                     author     => 'When adding a co-author',
 3563:                     course     => 'When adding a user to a course',
 3564:                     requestcrs => 'When requesting a course',
 3565:                     selfcreate => 'User creates own account', 
 3566:                     any        => 'Any',
 3567:                     official   => 'Institutional only ',
 3568:                     unofficial => 'Non-institutional only',
 3569:                     email      => 'E-mail address',
 3570:                     login      => 'Institutional Login',
 3571:                     sso        => 'SSO', 
 3572:                     none       => 'None',
 3573:     );
 3574:     return %lt;
 3575: }
 3576: 
 3577: sub authtype_names {
 3578:     my %lt = &Apache::lonlocal::texthash(
 3579:                       int    => 'Internal',
 3580:                       krb4   => 'Kerberos 4',
 3581:                       krb5   => 'Kerberos 5',
 3582:                       loc    => 'Local',
 3583:                   );
 3584:     return %lt;
 3585: }
 3586: 
 3587: sub context_names {
 3588:     my %context_title = &Apache::lonlocal::texthash(
 3589:        author => 'Creating users when an Author',
 3590:        course => 'Creating users when in a course',
 3591:        domain => 'Creating users when a Domain Coordinator',
 3592:     );
 3593:     return %context_title;
 3594: }
 3595: 
 3596: sub print_usermodification {
 3597:     my ($position,$dom,$settings,$rowtotal) = @_;
 3598:     my $numinrow = 4;
 3599:     my ($context,$datatable,$rowcount);
 3600:     if ($position eq 'top') {
 3601:         $rowcount = 0;
 3602:         $context = 'author'; 
 3603:         foreach my $role ('ca','aa') {
 3604:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3605:                                                    $numinrow,$rowcount);
 3606:             $$rowtotal ++;
 3607:             $rowcount ++;
 3608:         }
 3609:     } elsif ($position eq 'middle') {
 3610:         $context = 'course';
 3611:         $rowcount = 0;
 3612:         foreach my $role ('st','ep','ta','in','cr') {
 3613:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3614:                                                    $numinrow,$rowcount);
 3615:             $$rowtotal ++;
 3616:             $rowcount ++;
 3617:         }
 3618:     } elsif ($position eq 'bottom') {
 3619:         $context = 'selfcreate';
 3620:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3621:         $usertypes->{'default'} = $othertitle;
 3622:         if (ref($types) eq 'ARRAY') {
 3623:             push(@{$types},'default');
 3624:             $usertypes->{'default'} = $othertitle;
 3625:             foreach my $status (@{$types}) {
 3626:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3627:                                                        $numinrow,$rowcount,$usertypes);
 3628:                 $$rowtotal ++;
 3629:                 $rowcount ++;
 3630:             }
 3631:         }
 3632:     }
 3633:     return $datatable;
 3634: }
 3635: 
 3636: sub print_defaults {
 3637:     my ($dom,$rowtotal) = @_;
 3638:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3639:                  'datelocale_def','portal_def');
 3640:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3641:     my $titles = &defaults_titles($dom);
 3642:     my $rownum = 0;
 3643:     my ($datatable,$css_class);
 3644:     foreach my $item (@items) {
 3645:         if ($rownum%2) {
 3646:             $css_class = '';
 3647:         } else {
 3648:             $css_class = ' class="LC_odd_row" ';
 3649:         }
 3650:         $datatable .= '<tr'.$css_class.'>'.
 3651:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3652:                   '</span></td><td class="LC_right_item">';
 3653:         if ($item eq 'auth_def') {
 3654:             my @authtypes = ('internal','krb4','krb5','localauth');
 3655:             my %shortauth = (
 3656:                              internal => 'int',
 3657:                              krb4 => 'krb4',
 3658:                              krb5 => 'krb5',
 3659:                              localauth  => 'loc'
 3660:                            );
 3661:             my %authnames = &authtype_names();
 3662:             foreach my $auth (@authtypes) {
 3663:                 my $checked = ' ';
 3664:                 if ($domdefaults{$item} eq $auth) {
 3665:                     $checked = ' checked="checked" ';
 3666:                 }
 3667:                 $datatable .= '<label><input type="radio" name="'.$item.
 3668:                               '" value="'.$auth.'"'.$checked.'/>'.
 3669:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3670:             }
 3671:         } elsif ($item eq 'timezone_def') {
 3672:             my $includeempty = 1;
 3673:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3674:         } elsif ($item eq 'datelocale_def') {
 3675:             my $includeempty = 1;
 3676:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3677:         } elsif ($item eq 'lang_def') {
 3678:             my %langchoices = &get_languages_hash();
 3679:             $langchoices{''} = 'No language preference';
 3680:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 3681:             $datatable .= &Apache::loncommon::select_form($domdefaults{$item},$item,
 3682:                                                           \%langchoices);
 3683:         } else {
 3684:             my $size;
 3685:             if ($item eq 'portal_def') {
 3686:                 $size = ' size="25"';
 3687:             }
 3688:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3689:                           $domdefaults{$item}.'"'.$size.' />';
 3690:         }
 3691:         $datatable .= '</td></tr>';
 3692:         $rownum ++;
 3693:     }
 3694:     $$rowtotal += $rownum;
 3695:     return $datatable;
 3696: }
 3697: 
 3698: sub get_languages_hash {
 3699:     my %langchoices;
 3700:     foreach my $id (&Apache::loncommon::languageids()) {
 3701:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 3702:         if ($code ne '') {
 3703:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 3704:         }
 3705:     }
 3706:     return %langchoices;
 3707: }
 3708: 
 3709: sub defaults_titles {
 3710:     my ($dom) = @_;
 3711:     my %titles = &Apache::lonlocal::texthash (
 3712:                    'auth_def'      => 'Default authentication type',
 3713:                    'auth_arg_def'  => 'Default authentication argument',
 3714:                    'lang_def'      => 'Default language',
 3715:                    'timezone_def'  => 'Default timezone',
 3716:                    'datelocale_def' => 'Default locale for dates',
 3717:                    'portal_def'     => 'Portal/Default URL',
 3718:                  );
 3719:     if ($dom) {
 3720:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3721:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3722:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3723:         $protocol = 'http' if ($protocol ne 'https');
 3724:         if ($uint_dom) {
 3725:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3726:                                          $uint_dom);
 3727:         }
 3728:     }
 3729:     return (\%titles);
 3730: }
 3731: 
 3732: sub print_scantronformat {
 3733:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3734:     my $itemcount = 1;
 3735:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3736:         %confhash);
 3737:     my $switchserver = &check_switchserver($dom,$confname);
 3738:     my %lt = &Apache::lonlocal::texthash (
 3739:                 default => 'Default bubblesheet format file error',
 3740:                 custom  => 'Custom bubblesheet format file error',
 3741:              );
 3742:     my %scantronfiles = (
 3743:         default => 'default.tab',
 3744:         custom => 'custom.tab',
 3745:     );
 3746:     foreach my $key (keys(%scantronfiles)) {
 3747:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3748:                               .$scantronfiles{$key};
 3749:     }
 3750:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3751:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3752:         if (!$switchserver) {
 3753:             my $servadm = $r->dir_config('lonAdmEMail');
 3754:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3755:             if ($configuserok eq 'ok') {
 3756:                 if ($author_ok eq 'ok') {
 3757:                     my %legacyfile = (
 3758:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3759:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3760:                     );
 3761:                     my %md5chk;
 3762:                     foreach my $type (keys(%legacyfile)) {
 3763:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3764:                         chomp($md5chk{$type});
 3765:                     }
 3766:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3767:                         foreach my $type (keys(%legacyfile)) {
 3768:                             ($scantronurls{$type},my $error) = 
 3769:                                 &legacy_scantronformat($r,$dom,$confname,
 3770:                                                  $type,$legacyfile{$type},
 3771:                                                  $scantronurls{$type},
 3772:                                                  $scantronfiles{$type});
 3773:                             if ($error ne '') {
 3774:                                 $error{$type} = $error;
 3775:                             }
 3776:                         }
 3777:                         if (keys(%error) == 0) {
 3778:                             $is_custom = 1;
 3779:                             $confhash{'scantron'}{'scantronformat'} = 
 3780:                                 $scantronurls{'custom'};
 3781:                             my $putresult = 
 3782:                                 &Apache::lonnet::put_dom('configuration',
 3783:                                                          \%confhash,$dom);
 3784:                             if ($putresult ne 'ok') {
 3785:                                 $error{'custom'} = 
 3786:                                     '<span class="LC_error">'.
 3787:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3788:                             }
 3789:                         }
 3790:                     } else {
 3791:                         ($scantronurls{'default'},my $error) =
 3792:                             &legacy_scantronformat($r,$dom,$confname,
 3793:                                           'default',$legacyfile{'default'},
 3794:                                           $scantronurls{'default'},
 3795:                                           $scantronfiles{'default'});
 3796:                         if ($error eq '') {
 3797:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3798:                             my $putresult =
 3799:                                 &Apache::lonnet::put_dom('configuration',
 3800:                                                          \%confhash,$dom);
 3801:                             if ($putresult ne 'ok') {
 3802:                                 $error{'default'} =
 3803:                                     '<span class="LC_error">'.
 3804:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3805:                             }
 3806:                         } else {
 3807:                             $error{'default'} = $error;
 3808:                         }
 3809:                     }
 3810:                 }
 3811:             }
 3812:         } else {
 3813:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3814:         }
 3815:     }
 3816:     if (ref($settings) eq 'HASH') {
 3817:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3818:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3819:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3820:                 $scantronurl = '';
 3821:             } else {
 3822:                 $scantronurl = $settings->{'scantronformat'};
 3823:             }
 3824:             $is_custom = 1;
 3825:         } else {
 3826:             $scantronurl = $scantronurls{'default'};
 3827:         }
 3828:     } else {
 3829:         if ($is_custom) {
 3830:             $scantronurl = $scantronurls{'custom'};
 3831:         } else {
 3832:             $scantronurl = $scantronurls{'default'};
 3833:         }
 3834:     }
 3835:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3836:     $datatable .= '<tr'.$css_class.'>';
 3837:     if (!$is_custom) {
 3838:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3839:                       '<span class="LC_nobreak">';
 3840:         if ($scantronurl) {
 3841:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3842:                           &mt('Default bubblesheet format file').'</a>';
 3843:         } else {
 3844:             $datatable = &mt('File unavailable for display');
 3845:         }
 3846:         $datatable .= '</span></td>';
 3847:         if (keys(%error) == 0) { 
 3848:             $datatable .= '<td valign="bottom">';
 3849:             if (!$switchserver) {
 3850:                 $datatable .= &mt('Upload:').'<br />';
 3851:             }
 3852:         } else {
 3853:             my $errorstr;
 3854:             foreach my $key (sort(keys(%error))) {
 3855:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3856:             }
 3857:             $datatable .= '<td>'.$errorstr;
 3858:         }
 3859:     } else {
 3860:         if (keys(%error) > 0) {
 3861:             my $errorstr;
 3862:             foreach my $key (sort(keys(%error))) {
 3863:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3864:             } 
 3865:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3866:         } elsif ($scantronurl) {
 3867:             $datatable .= '<td><span class="LC_nobreak">'.
 3868:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3869:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3870:                           '<input type="checkbox" name="scantronformat_del"'.
 3871:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3872:                           '<td><span class="LC_nobreak">&nbsp;'.
 3873:                           &mt('Replace:').'</span><br />';
 3874:         }
 3875:     }
 3876:     if (keys(%error) == 0) {
 3877:         if ($switchserver) {
 3878:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3879:         } else {
 3880:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3881:                          '<input type="file" name="scantronformat" /></span>';
 3882:         }
 3883:     }
 3884:     $datatable .= '</td></tr>';
 3885:     $$rowtotal ++;
 3886:     return $datatable;
 3887: }
 3888: 
 3889: sub legacy_scantronformat {
 3890:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3891:     my ($url,$error);
 3892:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3893:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3894:         (my $result,$url) =
 3895:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3896:                          '','',$newfile);
 3897:         if ($result ne 'ok') {
 3898:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3899:         }
 3900:     }
 3901:     return ($url,$error);
 3902: }
 3903: 
 3904: sub print_coursecategories {
 3905:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3906:     my $datatable;
 3907:     if ($position eq 'top') {
 3908:         my $toggle_cats_crs = ' ';
 3909:         my $toggle_cats_dom = ' checked="checked" ';
 3910:         my $can_cat_crs = ' ';
 3911:         my $can_cat_dom = ' checked="checked" ';
 3912:         my $toggle_catscomm_comm = ' ';
 3913:         my $toggle_catscomm_dom = ' checked="checked" ';
 3914:         my $can_catcomm_comm = ' ';
 3915:         my $can_catcomm_dom = ' checked="checked" ';
 3916: 
 3917:         if (ref($settings) eq 'HASH') {
 3918:             if ($settings->{'togglecats'} eq 'crs') {
 3919:                 $toggle_cats_crs = $toggle_cats_dom;
 3920:                 $toggle_cats_dom = ' ';
 3921:             }
 3922:             if ($settings->{'categorize'} eq 'crs') {
 3923:                 $can_cat_crs = $can_cat_dom;
 3924:                 $can_cat_dom = ' ';
 3925:             }
 3926:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3927:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3928:                 $toggle_catscomm_dom = ' ';
 3929:             }
 3930:             if ($settings->{'categorizecomm'} eq 'comm') {
 3931:                 $can_catcomm_comm = $can_catcomm_dom;
 3932:                 $can_catcomm_dom = ' ';
 3933:             }
 3934:         }
 3935:         my %title = &Apache::lonlocal::texthash (
 3936:                      togglecats     => 'Show/Hide a course in catalog',
 3937:                      togglecatscomm => 'Show/Hide a community in catalog',
 3938:                      categorize     => 'Assign a category to a course',
 3939:                      categorizecomm => 'Assign a category to a community',
 3940:                     );
 3941:         my %level = &Apache::lonlocal::texthash (
 3942:                      dom  => 'Set in Domain',
 3943:                      crs  => 'Set in Course',
 3944:                      comm => 'Set in Community',
 3945:                     );
 3946:         $datatable = '<tr class="LC_odd_row">'.
 3947:                   '<td>'.$title{'togglecats'}.'</td>'.
 3948:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3949:                   '<input type="radio" name="togglecats"'.
 3950:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3951:                   '<label><input type="radio" name="togglecats"'.
 3952:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3953:                   '</tr><tr>'.
 3954:                   '<td>'.$title{'categorize'}.'</td>'.
 3955:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3956:                   '<label><input type="radio" name="categorize"'.
 3957:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3958:                   '<label><input type="radio" name="categorize"'.
 3959:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3960:                   '</tr><tr class="LC_odd_row">'.
 3961:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 3962:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3963:                   '<input type="radio" name="togglecatscomm"'.
 3964:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3965:                   '<label><input type="radio" name="togglecatscomm"'.
 3966:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3967:                   '</tr><tr>'.
 3968:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 3969:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3970:                   '<label><input type="radio" name="categorizecomm"'.
 3971:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3972:                   '<label><input type="radio" name="categorizecomm"'.
 3973:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3974:                   '</tr>';
 3975:         $$rowtotal += 4;
 3976:     } else {
 3977:         my $css_class;
 3978:         my $itemcount = 1;
 3979:         my $cathash; 
 3980:         if (ref($settings) eq 'HASH') {
 3981:             $cathash = $settings->{'cats'};
 3982:         }
 3983:         if (ref($cathash) eq 'HASH') {
 3984:             my (@cats,@trails,%allitems,%idx,@jsarray);
 3985:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 3986:                                                    \%allitems,\%idx,\@jsarray);
 3987:             my $maxdepth = scalar(@cats);
 3988:             my $colattrib = '';
 3989:             if ($maxdepth > 2) {
 3990:                 $colattrib = ' colspan="2" ';
 3991:             }
 3992:             my @path;
 3993:             if (@cats > 0) {
 3994:                 if (ref($cats[0]) eq 'ARRAY') {
 3995:                     my $numtop = @{$cats[0]};
 3996:                     my $maxnum = $numtop;
 3997:                     my %default_names = (
 3998:                           instcode    => &mt('Official courses'),
 3999:                           communities => &mt('Communities'),
 4000:                     );
 4001: 
 4002:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4003:                         ($cathash->{'instcode::0'} eq '') ||
 4004:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4005:                         ($cathash->{'communities::0'} eq '')) {
 4006:                         $maxnum ++;
 4007:                     }
 4008:                     my $lastidx;
 4009:                     for (my $i=0; $i<$numtop; $i++) {
 4010:                         my $parent = $cats[0][$i];
 4011:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4012:                         my $item = &escape($parent).'::0';
 4013:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4014:                         $lastidx = $idx{$item};
 4015:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4016:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4017:                         for (my $k=0; $k<=$maxnum; $k++) {
 4018:                             my $vpos = $k+1;
 4019:                             my $selstr;
 4020:                             if ($k == $i) {
 4021:                                 $selstr = ' selected="selected" ';
 4022:                             }
 4023:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4024:                         }
 4025:                         $datatable .= '</select></td><td>';
 4026:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4027:                             $datatable .=  '<span class="LC_nobreak">'
 4028:                                            .$default_names{$parent}.'</span>';
 4029:                             if ($parent eq 'instcode') {
 4030:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4031:                                               .&mt('with institutional codes')
 4032:                                               .')</span></td><td'.$colattrib.'>';
 4033:                             } else {
 4034:                                 $datatable .= '<table><tr><td>';
 4035:                             }
 4036:                             $datatable .= '<span class="LC_nobreak">'
 4037:                                           .'<label><input type="radio" name="'
 4038:                                           .$parent.'" value="1" checked="checked" />'
 4039:                                           .&mt('Display').'</label>';
 4040:                             if ($parent eq 'instcode') {
 4041:                                 $datatable .= '&nbsp;';
 4042:                             } else {
 4043:                                 $datatable .= '</span></td></tr><tr><td>'
 4044:                                               .'<span class="LC_nobreak">';
 4045:                             }
 4046:                             $datatable .= '<label><input type="radio" name="'
 4047:                                           .$parent.'" value="0" />'
 4048:                                           .&mt('Do not display').'</label></span>';
 4049:                             if ($parent eq 'communities') {
 4050:                                 $datatable .= '</td></tr></table>';
 4051:                             }
 4052:                             $datatable .= '</td>';
 4053:                         } else {
 4054:                             $datatable .= $parent
 4055:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 4056:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4057:                         }
 4058:                         my $depth = 1;
 4059:                         push(@path,$parent);
 4060:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4061:                         pop(@path);
 4062:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4063:                         $itemcount ++;
 4064:                     }
 4065:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4066:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4067:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4068:                     for (my $k=0; $k<=$maxnum; $k++) {
 4069:                         my $vpos = $k+1;
 4070:                         my $selstr;
 4071:                         if ($k == $numtop) {
 4072:                             $selstr = ' selected="selected" ';
 4073:                         }
 4074:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4075:                     }
 4076:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4077:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4078:                                   .'</tr>'."\n";
 4079:                     $itemcount ++;
 4080:                     foreach my $default ('instcode','communities') {
 4081:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4082:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4083:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4084:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4085:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4086:                             for (my $k=0; $k<=$maxnum; $k++) {
 4087:                                 my $vpos = $k+1;
 4088:                                 my $selstr;
 4089:                                 if ($k == $maxnum) {
 4090:                                     $selstr = ' selected="selected" ';
 4091:                                 }
 4092:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4093:                             }
 4094:                             $datatable .= '</select></span></td>'.
 4095:                                           '<td><span class="LC_nobreak">'.
 4096:                                           $default_names{$default}.'</span>';
 4097:                             if ($default eq 'instcode') {
 4098:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4099:                                               .&mt('with institutional codes').')</span>';
 4100:                             }
 4101:                             $datatable .= '</td>'
 4102:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4103:                                           .&mt('Display').'</label>&nbsp;'
 4104:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4105:                                           .&mt('Do not display').'</label></span></td></tr>';
 4106:                         }
 4107:                     }
 4108:                 }
 4109:             } else {
 4110:                 $datatable .= &initialize_categories($itemcount);
 4111:             }
 4112:         } else {
 4113:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4114:                           .&initialize_categories($itemcount);
 4115:         }
 4116:         $$rowtotal += $itemcount;
 4117:     }
 4118:     return $datatable;
 4119: }
 4120: 
 4121: sub print_serverstatuses {
 4122:     my ($dom,$settings,$rowtotal) = @_;
 4123:     my $datatable;
 4124:     my @pages = &serverstatus_pages();
 4125:     my (%namedaccess,%machineaccess);
 4126:     foreach my $type (@pages) {
 4127:         $namedaccess{$type} = '';
 4128:         $machineaccess{$type}= '';
 4129:     }
 4130:     if (ref($settings) eq 'HASH') {
 4131:         foreach my $type (@pages) {
 4132:             if (exists($settings->{$type})) {
 4133:                 if (ref($settings->{$type}) eq 'HASH') {
 4134:                     foreach my $key (keys(%{$settings->{$type}})) {
 4135:                         if ($key eq 'namedusers') {
 4136:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4137:                         } elsif ($key eq 'machines') {
 4138:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4139:                         }
 4140:                     }
 4141:                 }
 4142:             }
 4143:         }
 4144:     }
 4145:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4146:     my $rownum = 0;
 4147:     my $css_class;
 4148:     foreach my $type (@pages) {
 4149:         $rownum ++;
 4150:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4151:         $datatable .= '<tr'.$css_class.'>'.
 4152:                       '<td><span class="LC_nobreak">'.
 4153:                       $titles->{$type}.'</span></td>'.
 4154:                       '<td class="LC_left_item">'.
 4155:                       '<input type="text" name="'.$type.'_namedusers" '.
 4156:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4157:                       '<td class="LC_right_item">'.
 4158:                       '<span class="LC_nobreak">'.
 4159:                       '<input type="text" name="'.$type.'_machines" '.
 4160:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4161:                       '</td></tr>'."\n";
 4162:     }
 4163:     $$rowtotal += $rownum;
 4164:     return $datatable;
 4165: }
 4166: 
 4167: sub serverstatus_pages {
 4168:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4169:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4170:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4171: }
 4172: 
 4173: sub coursecategories_javascript {
 4174:     my ($settings) = @_;
 4175:     my ($output,$jstext,$cathash);
 4176:     if (ref($settings) eq 'HASH') {
 4177:         $cathash = $settings->{'cats'};
 4178:     }
 4179:     if (ref($cathash) eq 'HASH') {
 4180:         my (@cats,@jsarray,%idx);
 4181:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4182:         if (@jsarray > 0) {
 4183:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4184:             for (my $i=0; $i<@jsarray; $i++) {
 4185:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4186:                     my $catstr = join('","',@{$jsarray[$i]});
 4187:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4188:                 }
 4189:             }
 4190:         }
 4191:     } else {
 4192:         $jstext  = '    var categories = Array(1);'."\n".
 4193:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4194:     }
 4195:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4196:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4197:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4198:     $output = <<"ENDSCRIPT";
 4199: <script type="text/javascript">
 4200: // <![CDATA[
 4201: function reorderCats(form,parent,item,idx) {
 4202:     var changedVal;
 4203: $jstext
 4204:     var newpos = 'addcategory_pos';
 4205:     var current = new Array;
 4206:     if (parent == '') {
 4207:         var has_instcode = 0;
 4208:         var maxtop = categories[idx].length;
 4209:         for (var j=0; j<maxtop; j++) {
 4210:             if (categories[idx][j] == 'instcode::0') {
 4211:                 has_instcode == 1;
 4212:             }
 4213:         }
 4214:         if (has_instcode == 0) {
 4215:             categories[idx][maxtop] = 'instcode_pos';
 4216:         }
 4217:     } else {
 4218:         newpos += '_'+parent;
 4219:     }
 4220:     var maxh = 1 + categories[idx].length;
 4221:     var current = new Array;
 4222:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4223:     if (item == newpos) {
 4224:         changedVal = newitemVal;
 4225:     } else {
 4226:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4227:         current[newitemVal] = newpos;
 4228:     }
 4229:     for (var i=0; i<categories[idx].length; i++) {
 4230:         var elementName = categories[idx][i];
 4231:         if (elementName != item) {
 4232:             if (form.elements[elementName]) {
 4233:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4234:                 current[currVal] = elementName;
 4235:             }
 4236:         }
 4237:     }
 4238:     var oldVal;
 4239:     for (var j=0; j<maxh; j++) {
 4240:         if (current[j] == undefined) {
 4241:             oldVal = j;
 4242:         }
 4243:     }
 4244:     if (oldVal < changedVal) {
 4245:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4246:            var elementName = current[k];
 4247:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4248:         }
 4249:     } else {
 4250:         for (var k=changedVal; k<oldVal; k++) {
 4251:             var elementName = current[k];
 4252:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4253:         }
 4254:     }
 4255:     return;
 4256: }
 4257: 
 4258: function categoryCheck(form) {
 4259:     if (form.elements['addcategory_name'].value == 'instcode') {
 4260:         alert('$instcode_reserved\\n$choose_again');
 4261:         return false;
 4262:     }
 4263:     if (form.elements['addcategory_name'].value == 'communities') {
 4264:         alert('$communities_reserved\\n$choose_again');
 4265:         return false;
 4266:     }
 4267:     return true;
 4268: }
 4269: 
 4270: // ]]>
 4271: </script>
 4272: 
 4273: ENDSCRIPT
 4274:     return $output;
 4275: }
 4276: 
 4277: sub initialize_categories {
 4278:     my ($itemcount) = @_;
 4279:     my ($datatable,$css_class,$chgstr);
 4280:     my %default_names = (
 4281:                       instcode    => 'Official courses (with institutional codes)',
 4282:                       communities => 'Communities',
 4283:                         );
 4284:     my $select0 = ' selected="selected"';
 4285:     my $select1 = '';
 4286:     foreach my $default ('instcode','communities') {
 4287:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4288:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4289:         if ($default eq 'communities') {
 4290:             $select1 = $select0;
 4291:             $select0 = '';
 4292:         }
 4293:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4294:                      .'<select name="'.$default.'_pos">'
 4295:                      .'<option value="0"'.$select0.'>1</option>'
 4296:                      .'<option value="1"'.$select1.'>2</option>'
 4297:                      .'<option value="2">3</option></select>&nbsp;'
 4298:                      .$default_names{$default}
 4299:                      .'</span></td><td><span class="LC_nobreak">'
 4300:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4301:                      .&mt('Display').'</label>&nbsp;<label>'
 4302:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4303:                  .'</label></span></td></tr>';
 4304:         $itemcount ++;
 4305:     }
 4306:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4307:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4308:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4309:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4310:                   .'<option value="0">1</option>'
 4311:                   .'<option value="1">2</option>'
 4312:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4313:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4314:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4315:     return $datatable;
 4316: }
 4317: 
 4318: sub build_category_rows {
 4319:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4320:     my ($text,$name,$item,$chgstr);
 4321:     if (ref($cats) eq 'ARRAY') {
 4322:         my $maxdepth = scalar(@{$cats});
 4323:         if (ref($cats->[$depth]) eq 'HASH') {
 4324:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4325:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4326:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4327:                 $text .= '<td><table class="LC_datatable">';
 4328:                 my ($idxnum,$parent_name,$parent_item);
 4329:                 my $higher = $depth - 1;
 4330:                 if ($higher == 0) {
 4331:                     $parent_name = &escape($parent).'::'.$higher;
 4332:                 } else {
 4333:                     if (ref($path) eq 'ARRAY') {
 4334:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4335:                     }
 4336:                 }
 4337:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4338:                 for (my $j=0; $j<=$numchildren; $j++) {
 4339:                     if ($j < $numchildren) {
 4340:                         $name = $cats->[$depth]{$parent}[$j];
 4341:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4342:                         $idxnum = $idx->{$item};
 4343:                     } else {
 4344:                         $name = $parent_name;
 4345:                         $item = $parent_item;
 4346:                     }
 4347:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4348:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4349:                     for (my $i=0; $i<=$numchildren; $i++) {
 4350:                         my $vpos = $i+1;
 4351:                         my $selstr;
 4352:                         if ($j == $i) {
 4353:                             $selstr = ' selected="selected" ';
 4354:                         }
 4355:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4356:                     }
 4357:                     $text .= '</select>&nbsp;';
 4358:                     if ($j < $numchildren) {
 4359:                         my $deeper = $depth+1;
 4360:                         $text .= $name.'&nbsp;'
 4361:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4362:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4363:                         if(ref($path) eq 'ARRAY') {
 4364:                             push(@{$path},$name);
 4365:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4366:                             pop(@{$path});
 4367:                         }
 4368:                     } else {
 4369:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4370:                         if ($j == $numchildren) {
 4371:                             $text .= $name;
 4372:                         } else {
 4373:                             $text .= $item;
 4374:                         }
 4375:                         $text .= '" value="" />';
 4376:                     }
 4377:                     $text .= '</td></tr>';
 4378:                 }
 4379:                 $text .= '</table></td>';
 4380:             } else {
 4381:                 my $higher = $depth-1;
 4382:                 if ($higher == 0) {
 4383:                     $name = &escape($parent).'::'.$higher;
 4384:                 } else {
 4385:                     if (ref($path) eq 'ARRAY') {
 4386:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4387:                     }
 4388:                 }
 4389:                 my $colspan;
 4390:                 if ($parent ne 'instcode') {
 4391:                     $colspan = $maxdepth - $depth - 1;
 4392:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4393:                 }
 4394:             }
 4395:         }
 4396:     }
 4397:     return $text;
 4398: }
 4399: 
 4400: sub modifiable_userdata_row {
 4401:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4402:     my $rolename;
 4403:     if ($context eq 'selfcreate') {
 4404:         if (ref($usertypes) eq 'HASH') {
 4405:             $rolename = $usertypes->{$role};
 4406:         } else {
 4407:             $rolename = $role;
 4408:         }
 4409:     } else {
 4410:         if ($role eq 'cr') {
 4411:             $rolename = &mt('Custom role');
 4412:         } else {
 4413:             $rolename = &Apache::lonnet::plaintext($role);
 4414:         }
 4415:     }
 4416:     my @fields = ('lastname','firstname','middlename','generation',
 4417:                   'permanentemail','id');
 4418:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4419:     my $output;
 4420:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4421:     $output = '<tr '.$css_class.'>'.
 4422:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4423:               '<td class="LC_left_item" colspan="2"><table>';
 4424:     my $rem;
 4425:     my %checks;
 4426:     if (ref($settings) eq 'HASH') {
 4427:         if (ref($settings->{$context}) eq 'HASH') {
 4428:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4429:                 foreach my $field (@fields) {
 4430:                     if ($settings->{$context}->{$role}->{$field}) {
 4431:                         $checks{$field} = ' checked="checked" ';
 4432:                     }
 4433:                 }
 4434:             }
 4435:         }
 4436:     }
 4437:     for (my $i=0; $i<@fields; $i++) {
 4438:         my $rem = $i%($numinrow);
 4439:         if ($rem == 0) {
 4440:             if ($i > 0) {
 4441:                 $output .= '</tr>';
 4442:             }
 4443:             $output .= '<tr>';
 4444:         }
 4445:         my $check = ' ';
 4446:         if (exists($checks{$fields[$i]})) {
 4447:             $check = $checks{$fields[$i]}
 4448:         } else {
 4449:             if ($role eq 'st') {
 4450:                 if (ref($settings) ne 'HASH') {
 4451:                     $check = ' checked="checked" '; 
 4452:                 }
 4453:             }
 4454:         }
 4455:         $output .= '<td class="LC_left_item">'.
 4456:                    '<span class="LC_nobreak"><label>'.
 4457:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4458:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4459:                    '</label></span></td>';
 4460:         $rem = @fields%($numinrow);
 4461:     }
 4462:     my $colsleft = $numinrow - $rem;
 4463:     if ($colsleft > 1 ) {
 4464:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4465:                    '&nbsp;</td>';
 4466:     } elsif ($colsleft == 1) {
 4467:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4468:     }
 4469:     $output .= '</tr></table></td></tr>';
 4470:     return $output;
 4471: }
 4472: 
 4473: sub insttypes_row {
 4474:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4475:     my %lt = &Apache::lonlocal::texthash (
 4476:                       cansearch => 'Users allowed to search',
 4477:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4478:                       lockablenames => 'User preference to lock name',
 4479:              );
 4480:     my $showdom;
 4481:     if ($context eq 'cansearch') {
 4482:         $showdom = ' ('.$dom.')';
 4483:     }
 4484:     my $class = 'LC_left_item';
 4485:     if ($context eq 'statustocreate') {
 4486:         $class = 'LC_right_item';
 4487:     }
 4488:     my $output =  '<tr class="LC_odd_row">'.
 4489:                   '<td>'.$lt{$context}.$showdom.
 4490:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4491:     my $rem;
 4492:     if (ref($types) eq 'ARRAY') {
 4493:         for (my $i=0; $i<@{$types}; $i++) {
 4494:             if (defined($usertypes->{$types->[$i]})) {
 4495:                 my $rem = $i%($numinrow);
 4496:                 if ($rem == 0) {
 4497:                     if ($i > 0) {
 4498:                         $output .= '</tr>';
 4499:                     }
 4500:                     $output .= '<tr>';
 4501:                 }
 4502:                 my $check = ' ';
 4503:                 if (ref($settings) eq 'HASH') {
 4504:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4505:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4506:                             $check = ' checked="checked" ';
 4507:                         }
 4508:                     } elsif ($context eq 'statustocreate') {
 4509:                         $check = ' checked="checked" ';
 4510:                     }
 4511:                 }
 4512:                 $output .= '<td class="LC_left_item">'.
 4513:                            '<span class="LC_nobreak"><label>'.
 4514:                            '<input type="checkbox" name="'.$context.'" '.
 4515:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4516:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4517:             }
 4518:         }
 4519:         $rem = @{$types}%($numinrow);
 4520:     }
 4521:     my $colsleft = $numinrow - $rem;
 4522:     if (($rem == 0) && (@{$types} > 0)) {
 4523:         $output .= '<tr>';
 4524:     }
 4525:     if ($colsleft > 1) {
 4526:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4527:     } else {
 4528:         $output .= '<td class="LC_left_item">';
 4529:     }
 4530:     my $defcheck = ' ';
 4531:     if (ref($settings) eq 'HASH') {  
 4532:         if (ref($settings->{$context}) eq 'ARRAY') {
 4533:             if (grep(/^default$/,@{$settings->{$context}})) {
 4534:                 $defcheck = ' checked="checked" ';
 4535:             }
 4536:         } elsif ($context eq 'statustocreate') {
 4537:             $defcheck = ' checked="checked" ';
 4538:         }
 4539:     }
 4540:     $output .= '<span class="LC_nobreak"><label>'.
 4541:                '<input type="checkbox" name="'.$context.'" '.
 4542:                'value="default"'.$defcheck.'/>'.
 4543:                $othertitle.'</label></span></td>'.
 4544:                '</tr></table></td></tr>';
 4545:     return $output;
 4546: }
 4547: 
 4548: sub sorted_searchtitles {
 4549:     my %searchtitles = &Apache::lonlocal::texthash(
 4550:                          'uname' => 'username',
 4551:                          'lastname' => 'last name',
 4552:                          'lastfirst' => 'last name, first name',
 4553:                      );
 4554:     my @titleorder = ('uname','lastname','lastfirst');
 4555:     return (\%searchtitles,\@titleorder);
 4556: }
 4557: 
 4558: sub sorted_searchtypes {
 4559:     my %srchtypes_desc = (
 4560:                            exact    => 'is exact match',
 4561:                            contains => 'contains ..',
 4562:                            begins   => 'begins with ..',
 4563:                          );
 4564:     my @srchtypeorder = ('exact','begins','contains');
 4565:     return (\%srchtypes_desc,\@srchtypeorder);
 4566: }
 4567: 
 4568: sub usertype_update_row {
 4569:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4570:     my $datatable;
 4571:     my $numinrow = 4;
 4572:     foreach my $type (@{$types}) {
 4573:         if (defined($usertypes->{$type})) {
 4574:             $$rownums ++;
 4575:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4576:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4577:                           '</td><td class="LC_left_item"><table>';
 4578:             for (my $i=0; $i<@{$fields}; $i++) {
 4579:                 my $rem = $i%($numinrow);
 4580:                 if ($rem == 0) {
 4581:                     if ($i > 0) {
 4582:                         $datatable .= '</tr>';
 4583:                     }
 4584:                     $datatable .= '<tr>';
 4585:                 }
 4586:                 my $check = ' ';
 4587:                 if (ref($settings) eq 'HASH') {
 4588:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4589:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4590:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4591:                                 $check = ' checked="checked" ';
 4592:                             }
 4593:                         }
 4594:                     }
 4595:                 }
 4596: 
 4597:                 if ($i == @{$fields}-1) {
 4598:                     my $colsleft = $numinrow - $rem;
 4599:                     if ($colsleft > 1) {
 4600:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4601:                     } else {
 4602:                         $datatable .= '<td>';
 4603:                     }
 4604:                 } else {
 4605:                     $datatable .= '<td>';
 4606:                 }
 4607:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4608:                               '<input type="checkbox" name="updateable_'.$type.
 4609:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4610:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4611:             }
 4612:             $datatable .= '</tr></table></td></tr>';
 4613:         }
 4614:     }
 4615:     return $datatable;
 4616: }
 4617: 
 4618: sub modify_login {
 4619:     my ($r,$dom,$confname,%domconfig) = @_;
 4620:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4621:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4622:     %title = ( coursecatalog => 'Display course catalog',
 4623:                adminmail => 'Display administrator E-mail address',
 4624:                helpdesk  => 'Display "Contact Helpdesk" link',
 4625:                newuser => 'Link for visitors to create a user account',
 4626:                loginheader => 'Log-in box header');
 4627:     @offon = ('off','on');
 4628:     if (ref($domconfig{login}) eq 'HASH') {
 4629:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4630:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4631:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4632:             }
 4633:         }
 4634:     }
 4635:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4636:                                            \%domconfig,\%loginhash);
 4637:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 4638:     foreach my $item (@toggles) {
 4639:         $loginhash{login}{$item} = $env{'form.'.$item};
 4640:     }
 4641:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4642:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4643:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4644:                                          \%loginhash);
 4645:     }
 4646: 
 4647:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4648:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4649:     if (keys(%servers) > 1) {
 4650:         foreach my $lonhost (keys(%servers)) {
 4651:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4652:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4653:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4654:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4655:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4656:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4657:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4658:                         $changes{'loginvia'}{$lonhost} = 1;
 4659:                     } else {
 4660:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4661:                         $changes{'loginvia'}{$lonhost} = 1;
 4662:                     }
 4663:                 } else {
 4664:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4665:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4666:                         $changes{'loginvia'}{$lonhost} = 1;
 4667:                     }
 4668:                 }
 4669:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4670:                     foreach my $item (@loginvia_attribs) {
 4671:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4672:                     }
 4673:                 } else {
 4674:                     foreach my $item (@loginvia_attribs) {
 4675:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4676:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4677:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4678:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4679:                                 $new = '/';
 4680:                             }
 4681:                         }
 4682:                         if (($item eq 'custompath') && 
 4683:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4684:                             $new = '';
 4685:                         }
 4686:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4687:                             $changes{'loginvia'}{$lonhost} = 1;
 4688:                         }
 4689:                         if ($item eq 'exempt') {
 4690:                             $new =~ s/^\s+//;
 4691:                             $new =~ s/\s+$//;
 4692:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4693:                             my @okips;
 4694:                             foreach my $ip (@poss_ips) {
 4695:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4696:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4697:                                         push(@okips,$ip); 
 4698:                                     }
 4699:                                 }
 4700:                             }
 4701:                             if (@okips > 0) {
 4702:                                 $new = join(',',@okips); 
 4703:                             } else {
 4704:                                 $new = ''; 
 4705:                             }
 4706:                         }
 4707:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4708:                     }
 4709:                 }
 4710:             } else {
 4711:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4712:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4713:                     $changes{'loginvia'}{$lonhost} = 1;
 4714:                     foreach my $item (@loginvia_attribs) {
 4715:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4716:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4717:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4718:                                 $new = '/';
 4719:                             }
 4720:                         }
 4721:                         if (($item eq 'custompath') && 
 4722:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4723:                             $new = '';
 4724:                         }
 4725:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4726:                     }
 4727:                 }
 4728:             }
 4729:         }
 4730:     }
 4731: 
 4732:     my $servadm = $r->dir_config('lonAdmEMail');
 4733:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 4734:     if (ref($domconfig{'login'}) eq 'HASH') {
 4735:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 4736:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 4737:                 if ($lang eq 'nolang') {
 4738:                     push(@currlangs,$lang);
 4739:                 } elsif (defined($langchoices{$lang})) {
 4740:                     push(@currlangs,$lang);
 4741:                 } else {
 4742:                     next;
 4743:                 }
 4744:             }
 4745:         }
 4746:     }
 4747:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 4748:     if (@currlangs > 0) {
 4749:         foreach my $lang (@currlangs) {
 4750:             if (grep(/^\Q$lang\E$/,@delurls)) {
 4751:                 $changes{'helpurl'}{$lang} = 1;
 4752:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 4753:                 $changes{'helpurl'}{$lang} = 1;
 4754:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 4755:                 push(@newlangs,$lang);
 4756:             } else {
 4757:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4758:             }
 4759:         }
 4760:     }
 4761:     unless (grep(/^nolang$/,@currlangs)) {
 4762:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 4763:             $changes{'helpurl'}{'nolang'} = 1;
 4764:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 4765:             push(@newlangs,'nolang');
 4766:         }
 4767:     }
 4768:     if ($env{'form.loginhelpurl_add_lang'}) {
 4769:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 4770:             ($env{'form.loginhelpurl_add_file.filename'})) {
 4771:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 4772:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 4773:         }
 4774:     }
 4775:     if ((@newlangs > 0) || ($addedfile)) {
 4776:         my $error;
 4777:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 4778:         if ($configuserok eq 'ok') {
 4779:             if ($switchserver) {
 4780:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 4781:             } elsif ($author_ok eq 'ok') {
 4782:                 my @allnew = @newlangs;
 4783:                 if ($addedfile ne '') {
 4784:                     push(@allnew,$addedfile);
 4785:                 }
 4786:                 foreach my $lang (@allnew) {
 4787:                     my $formelem = 'loginhelpurl_'.$lang;
 4788:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 4789:                         $formelem = 'loginhelpurl_add_file';
 4790:                     }
 4791:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 4792:                                                                "help/$lang",'','',$newfile{$lang});
 4793:                     if ($result eq 'ok') {
 4794:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 4795:                         $changes{'helpurl'}{$lang} = 1;
 4796:                     } else {
 4797:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 4798:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 4799:                         if ((grep(/^\Q$lang\E$/,@currlangs)) && 
 4800:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 4801: 
 4802:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4803:                         }
 4804:                     }
 4805:                 }
 4806:             } else {
 4807:                 $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);
 4808:             }
 4809:         } else {
 4810:             $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);
 4811:         }
 4812:         if ($error) {
 4813:             &Apache::lonnet::logthis($error);
 4814:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4815:         }
 4816:     }
 4817:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 4818: 
 4819:     my $defaulthelpfile = '/adm/loginproblems.html';
 4820:     my $defaulttext = &mt('Default in use');
 4821: 
 4822:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4823:                                              $dom);
 4824:     if ($putresult eq 'ok') {
 4825:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 4826:         my %defaultchecked = (
 4827:                     'coursecatalog' => 'on',
 4828:                     'helpdesk'      => 'on',
 4829:                     'adminmail'     => 'off',
 4830:                     'newuser'       => 'off',
 4831:         );
 4832:         if (ref($domconfig{'login'}) eq 'HASH') {
 4833:             foreach my $item (@toggles) {
 4834:                 if ($defaultchecked{$item} eq 'on') { 
 4835:                     if (($domconfig{'login'}{$item} eq '0') &&
 4836:                         ($env{'form.'.$item} eq '1')) {
 4837:                         $changes{$item} = 1;
 4838:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4839:                               $domconfig{'login'}{$item} eq '1') &&
 4840:                              ($env{'form.'.$item} eq '0')) {
 4841:                         $changes{$item} = 1;
 4842:                     }
 4843:                 } elsif ($defaultchecked{$item} eq 'off') {
 4844:                     if (($domconfig{'login'}{$item} eq '1') &&
 4845:                         ($env{'form.'.$item} eq '0')) {
 4846:                         $changes{$item} = 1;
 4847:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4848:                               $domconfig{'login'}{$item} eq '0') &&
 4849:                              ($env{'form.'.$item} eq '1')) {
 4850:                         $changes{$item} = 1;
 4851:                     }
 4852:                 }
 4853:             }
 4854:         }
 4855:         if (keys(%changes) > 0 || $colchgtext) {
 4856:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4857:             $resulttext = &mt('Changes made:').'<ul>';
 4858:             foreach my $item (sort(keys(%changes))) {
 4859:                 if ($item eq 'loginvia') {
 4860:                     if (ref($changes{$item}) eq 'HASH') {
 4861:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4862:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4863:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4864:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4865:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4866:                                     $protocol = 'http' if ($protocol ne 'https');
 4867:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4868: 
 4869:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4870:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4871:                                     } else {
 4872:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4873:                                     }
 4874:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4875:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4876:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4877:                                     }
 4878:                                     $resulttext .= '</li>';
 4879:                                 } else {
 4880:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4881:                                 }
 4882:                             } else {
 4883:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4884:                             }
 4885:                         }
 4886:                         $resulttext .= '</ul></li>';
 4887:                     }
 4888:                 } elsif ($item eq 'helpurl') {
 4889:                     if (ref($changes{$item}) eq 'HASH') {
 4890:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 4891:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 4892:                                 my ($chg,$link);
 4893:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 4894:                                 if ($lang eq 'nolang') {
 4895:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 4896:                                 } else {
 4897:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 4898:                                 }
 4899:                                 $resulttext .= '<li>'.$chg.'</li>';
 4900:                             } else {
 4901:                                 my $chg;
 4902:                                 if ($lang eq 'nolang') {
 4903:                                     $chg = &mt('custom log-in help file for no preferred language');
 4904:                                 } else {
 4905:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 4906:                                 }
 4907:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 4908:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 4909:                                                       '?inhibitmenu=yes',$chg,600,500).
 4910:                                                '</li>';
 4911:                             }
 4912:                         }
 4913:                     }
 4914:                 } elsif ($item eq 'captcha') {
 4915:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4916:                         my $chgtxt; 
 4917:                         if ($loginhash{'login'}{$item} eq 'notused') {
 4918:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 4919:                         } else {
 4920:                             my %captchas = &captcha_phrases();
 4921:                             if ($captchas{$loginhash{'login'}{$item}}) {
 4922:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 4923:                             } else {
 4924:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 4925:                             }
 4926:                         }
 4927:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4928:                     }
 4929:                 } elsif ($item eq 'recaptchakeys') {
 4930:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4931:                         my ($privkey,$pubkey);
 4932:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 4933:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 4934:                             $privkey = $loginhash{'login'}{$item}{'private'};
 4935:                         }
 4936:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 4937:                         if (!$pubkey) {
 4938:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 4939:                         } else {
 4940:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 4941:                         }
 4942:                         if (!$privkey) {
 4943:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 4944:                         } else {
 4945:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 4946:                         }
 4947:                         $chgtxt .= '</ul>';
 4948:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4949:                     }
 4950:                 } else {
 4951:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4952:                 }
 4953:             }
 4954:             $resulttext .= $colchgtext.'</ul>';
 4955:         } else {
 4956:             $resulttext = &mt('No changes made to log-in page settings');
 4957:         }
 4958:     } else {
 4959:         $resulttext = '<span class="LC_error">'.
 4960: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4961:     }
 4962:     if ($errors) {
 4963:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 4964:                        $errors.'</ul>';
 4965:     }
 4966:     return $resulttext;
 4967: }
 4968: 
 4969: sub color_font_choices {
 4970:     my %choices =
 4971:         &Apache::lonlocal::texthash (
 4972:             img => "Header",
 4973:             bgs => "Background colors",
 4974:             links => "Link colors",
 4975:             images => "Images",
 4976:             font => "Font color",
 4977:             fontmenu => "Font Menu",
 4978:             pgbg => "Page",
 4979:             tabbg => "Header",
 4980:             sidebg => "Border",
 4981:             link => "Link",
 4982:             alink => "Active link",
 4983:             vlink => "Visited link",
 4984:         );
 4985:     return %choices;
 4986: }
 4987: 
 4988: sub modify_rolecolors {
 4989:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 4990:     my ($resulttext,%rolehash);
 4991:     $rolehash{'rolecolors'} = {};
 4992:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 4993:         if ($domconfig{'rolecolors'} eq '') {
 4994:             $domconfig{'rolecolors'} = {};
 4995:         }
 4996:     }
 4997:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 4998:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 4999:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5000:                                              $dom);
 5001:     if ($putresult eq 'ok') {
 5002:         if (keys(%changes) > 0) {
 5003:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5004:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5005:                                              $rolehash{'rolecolors'});
 5006:         } else {
 5007:             $resulttext = &mt('No changes made to default color schemes');
 5008:         }
 5009:     } else {
 5010:         $resulttext = '<span class="LC_error">'.
 5011: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5012:     }
 5013:     if ($errors) {
 5014:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5015:                        $errors.'</ul>';
 5016:     }
 5017:     return $resulttext;
 5018: }
 5019: 
 5020: sub modify_colors {
 5021:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5022:     my (%changes,%choices);
 5023:     my @bgs;
 5024:     my @links = ('link','alink','vlink');
 5025:     my @logintext;
 5026:     my @images;
 5027:     my $servadm = $r->dir_config('lonAdmEMail');
 5028:     my $errors;
 5029:     foreach my $role (@{$roles}) {
 5030:         if ($role eq 'login') {
 5031:             %choices = &login_choices();
 5032:             @logintext = ('textcol','bgcol');
 5033:         } else {
 5034:             %choices = &color_font_choices();
 5035:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5036:         }
 5037:         if ($role eq 'login') {
 5038:             @images = ('img','logo','domlogo','login');
 5039:             @bgs = ('pgbg','mainbg','sidebg');
 5040:         } else {
 5041:             @images = ('img');
 5042:             @bgs = ('pgbg','tabbg','sidebg'); 
 5043:         }
 5044:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5045:         foreach my $item (@bgs,@links,@logintext) {
 5046:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5047:         }
 5048:         my ($configuserok,$author_ok,$switchserver) = 
 5049:             &config_check($dom,$confname,$servadm);
 5050:         my ($width,$height) = &thumb_dimensions();
 5051:         if (ref($domconfig->{$role}) ne 'HASH') {
 5052:             $domconfig->{$role} = {};
 5053:         }
 5054:         foreach my $img (@images) {
 5055:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5056:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5057:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5058:                 } else { 
 5059:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5060:                 }
 5061:             } 
 5062: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5063: 		 && !defined($domconfig->{$role}{$img})
 5064: 		 && !$env{'form.'.$role.'_del_'.$img}
 5065: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5066: 		# import the old configured image from the .tab setting
 5067: 		# if they haven't provided a new one 
 5068: 		$domconfig->{$role}{$img} = 
 5069: 		    $env{'form.'.$role.'_import_'.$img};
 5070: 	    }
 5071:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5072:                 my $error;
 5073:                 if ($configuserok eq 'ok') {
 5074:                     if ($switchserver) {
 5075:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5076:                     } else {
 5077:                         if ($author_ok eq 'ok') {
 5078:                             my ($result,$logourl) = 
 5079:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5080:                                            $dom,$confname,$img,$width,$height);
 5081:                             if ($result eq 'ok') {
 5082:                                 $confhash->{$role}{$img} = $logourl;
 5083:                                 $changes{$role}{'images'}{$img} = 1;
 5084:                             } else {
 5085:                                 $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);
 5086:                             }
 5087:                         } else {
 5088:                             $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);
 5089:                         }
 5090:                     }
 5091:                 } else {
 5092:                     $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);
 5093:                 }
 5094:                 if ($error) {
 5095:                     &Apache::lonnet::logthis($error);
 5096:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5097:                 }
 5098:             } elsif ($domconfig->{$role}{$img} ne '') {
 5099:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5100:                     my $error;
 5101:                     if ($configuserok eq 'ok') {
 5102: # is confname an author?
 5103:                         if ($switchserver eq '') {
 5104:                             if ($author_ok eq 'ok') {
 5105:                                 my ($result,$logourl) = 
 5106:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5107:                                             $dom,$confname,$img,$width,$height);
 5108:                                 if ($result eq 'ok') {
 5109:                                     $confhash->{$role}{$img} = $logourl;
 5110: 				    $changes{$role}{'images'}{$img} = 1;
 5111:                                 }
 5112:                             }
 5113:                         }
 5114:                     }
 5115:                 }
 5116:             }
 5117:         }
 5118:         if (ref($domconfig) eq 'HASH') {
 5119:             if (ref($domconfig->{$role}) eq 'HASH') {
 5120:                 foreach my $img (@images) {
 5121:                     if ($domconfig->{$role}{$img} ne '') {
 5122:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5123:                             $confhash->{$role}{$img} = '';
 5124:                             $changes{$role}{'images'}{$img} = 1;
 5125:                         } else {
 5126:                             if ($confhash->{$role}{$img} eq '') {
 5127:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5128:                             }
 5129:                         }
 5130:                     } else {
 5131:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5132:                             $confhash->{$role}{$img} = '';
 5133:                             $changes{$role}{'images'}{$img} = 1;
 5134:                         } 
 5135:                     }
 5136:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5137:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5138:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5139:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5140:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5141:                             }
 5142:                         } else {
 5143:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5144:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5145:                             }
 5146:                         }
 5147:                     }
 5148:                 }
 5149:                 if ($domconfig->{$role}{'font'} ne '') {
 5150:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5151:                         $changes{$role}{'font'} = 1;
 5152:                     }
 5153:                 } else {
 5154:                     if ($confhash->{$role}{'font'}) {
 5155:                         $changes{$role}{'font'} = 1;
 5156:                     }
 5157:                 }
 5158:                 if ($role ne 'login') {
 5159:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5160:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5161:                             $changes{$role}{'fontmenu'} = 1;
 5162:                         }
 5163:                     } else {
 5164:                         if ($confhash->{$role}{'fontmenu'}) {
 5165:                             $changes{$role}{'fontmenu'} = 1;
 5166:                         }
 5167:                     }
 5168:                 }
 5169:                 foreach my $item (@bgs) {
 5170:                     if ($domconfig->{$role}{$item} ne '') {
 5171:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5172:                             $changes{$role}{'bgs'}{$item} = 1;
 5173:                         } 
 5174:                     } else {
 5175:                         if ($confhash->{$role}{$item}) {
 5176:                             $changes{$role}{'bgs'}{$item} = 1;
 5177:                         }
 5178:                     }
 5179:                 }
 5180:                 foreach my $item (@links) {
 5181:                     if ($domconfig->{$role}{$item} ne '') {
 5182:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5183:                             $changes{$role}{'links'}{$item} = 1;
 5184:                         }
 5185:                     } else {
 5186:                         if ($confhash->{$role}{$item}) {
 5187:                             $changes{$role}{'links'}{$item} = 1;
 5188:                         }
 5189:                     }
 5190:                 }
 5191:                 foreach my $item (@logintext) {
 5192:                     if ($domconfig->{$role}{$item} ne '') {
 5193:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5194:                             $changes{$role}{'logintext'}{$item} = 1;
 5195:                         }
 5196:                     } else {
 5197:                         if ($confhash->{$role}{$item}) {
 5198:                             $changes{$role}{'logintext'}{$item} = 1;
 5199:                         }
 5200:                     }
 5201:                 }
 5202:             } else {
 5203:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5204:                                         \@logintext,$confhash,\%changes); 
 5205:             }
 5206:         } else {
 5207:             &default_change_checker($role,\@images,\@links,\@bgs,
 5208:                                     \@logintext,$confhash,\%changes); 
 5209:         }
 5210:     }
 5211:     return ($errors,%changes);
 5212: }
 5213: 
 5214: sub config_check {
 5215:     my ($dom,$confname,$servadm) = @_;
 5216:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5217:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5218:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5219:                                                    $confname,$servadm);
 5220:     if ($configuserok eq 'ok') {
 5221:         $switchserver = &check_switchserver($dom,$confname);
 5222:         if ($switchserver eq '') {
 5223:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5224:         }
 5225:     }
 5226:     return ($configuserok,$author_ok,$switchserver);
 5227: }
 5228: 
 5229: sub default_change_checker {
 5230:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5231:     foreach my $item (@{$links}) {
 5232:         if ($confhash->{$role}{$item}) {
 5233:             $changes->{$role}{'links'}{$item} = 1;
 5234:         }
 5235:     }
 5236:     foreach my $item (@{$bgs}) {
 5237:         if ($confhash->{$role}{$item}) {
 5238:             $changes->{$role}{'bgs'}{$item} = 1;
 5239:         }
 5240:     }
 5241:     foreach my $item (@{$logintext}) {
 5242:         if ($confhash->{$role}{$item}) {
 5243:             $changes->{$role}{'logintext'}{$item} = 1;
 5244:         }
 5245:     }
 5246:     foreach my $img (@{$images}) {
 5247:         if ($env{'form.'.$role.'_del_'.$img}) {
 5248:             $confhash->{$role}{$img} = '';
 5249:             $changes->{$role}{'images'}{$img} = 1;
 5250:         }
 5251:         if ($role eq 'login') {
 5252:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5253:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5254:             }
 5255:         }
 5256:     }
 5257:     if ($confhash->{$role}{'font'}) {
 5258:         $changes->{$role}{'font'} = 1;
 5259:     }
 5260: }
 5261: 
 5262: sub display_colorchgs {
 5263:     my ($dom,$changes,$roles,$confhash) = @_;
 5264:     my (%choices,$resulttext);
 5265:     if (!grep(/^login$/,@{$roles})) {
 5266:         $resulttext = &mt('Changes made:').'<br />';
 5267:     }
 5268:     foreach my $role (@{$roles}) {
 5269:         if ($role eq 'login') {
 5270:             %choices = &login_choices();
 5271:         } else {
 5272:             %choices = &color_font_choices();
 5273:         }
 5274:         if (ref($changes->{$role}) eq 'HASH') {
 5275:             if ($role ne 'login') {
 5276:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5277:             }
 5278:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5279:                 if ($role ne 'login') {
 5280:                     $resulttext .= '<ul>';
 5281:                 }
 5282:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5283:                     if ($role ne 'login') {
 5284:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5285:                     }
 5286:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5287:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5288:                             if ($confhash->{$role}{$key}{$item}) {
 5289:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5290:                             } else {
 5291:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5292:                             }
 5293:                         } elsif ($confhash->{$role}{$item} eq '') {
 5294:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5295:                         } else {
 5296:                             my $newitem = $confhash->{$role}{$item};
 5297:                             if ($key eq 'images') {
 5298:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5299:                             }
 5300:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5301:                         }
 5302:                     }
 5303:                     if ($role ne 'login') {
 5304:                         $resulttext .= '</ul></li>';
 5305:                     }
 5306:                 } else {
 5307:                     if ($confhash->{$role}{$key} eq '') {
 5308:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5309:                     } else {
 5310:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5311:                     }
 5312:                 }
 5313:                 if ($role ne 'login') {
 5314:                     $resulttext .= '</ul>';
 5315:                 }
 5316:             }
 5317:         }
 5318:     }
 5319:     return $resulttext;
 5320: }
 5321: 
 5322: sub thumb_dimensions {
 5323:     return ('200','50');
 5324: }
 5325: 
 5326: sub check_dimensions {
 5327:     my ($inputfile) = @_;
 5328:     my ($fullwidth,$fullheight);
 5329:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5330:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5331:             my $imageinfo = <PIPE>;
 5332:             if (!close(PIPE)) {
 5333:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5334:             }
 5335:             chomp($imageinfo);
 5336:             my ($fullsize) = 
 5337:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5338:             if ($fullsize) {
 5339:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5340:             }
 5341:         }
 5342:     }
 5343:     return ($fullwidth,$fullheight);
 5344: }
 5345: 
 5346: sub check_configuser {
 5347:     my ($uhome,$dom,$confname,$servadm) = @_;
 5348:     my ($configuserok,%currroles);
 5349:     if ($uhome eq 'no_host') {
 5350:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5351:         my $configpass = &LONCAPA::Enrollment::create_password();
 5352:         $configuserok = 
 5353:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5354:                              $configpass,'','','','','',undef,$servadm);
 5355:     } else {
 5356:         $configuserok = 'ok';
 5357:         %currroles = 
 5358:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5359:     }
 5360:     return ($configuserok,%currroles);
 5361: }
 5362: 
 5363: sub check_authorstatus {
 5364:     my ($dom,$confname,%currroles) = @_;
 5365:     my $author_ok;
 5366:     if (!$currroles{':'.$dom.':au'}) {
 5367:         my $start = time;
 5368:         my $end = 0;
 5369:         $author_ok = 
 5370:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5371:                                         'au',$end,$start,'','','domconfig');
 5372:     } else {
 5373:         $author_ok = 'ok';
 5374:     }
 5375:     return $author_ok;
 5376: }
 5377: 
 5378: sub publishlogo {
 5379:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5380:     my ($output,$fname,$logourl);
 5381:     if ($action eq 'upload') {
 5382:         $fname=$env{'form.'.$formname.'.filename'};
 5383:         chop($env{'form.'.$formname});
 5384:     } else {
 5385:         ($fname) = ($formname =~ /([^\/]+)$/);
 5386:     }
 5387:     if ($savefileas ne '') {
 5388:         $fname = $savefileas;
 5389:     }
 5390:     $fname=&Apache::lonnet::clean_filename($fname);
 5391: # See if there is anything left
 5392:     unless ($fname) { return ('error: no uploaded file'); }
 5393:     $fname="$subdir/$fname";
 5394:     my $docroot=$r->dir_config('lonDocRoot'); 
 5395:     my $filepath="$docroot/priv";
 5396:     my $relpath = "$dom/$confname";
 5397:     my ($fnamepath,$file,$fetchthumb);
 5398:     $file=$fname;
 5399:     if ($fname=~m|/|) {
 5400:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5401:     }
 5402:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5403:     my $count;
 5404:     for ($count=5;$count<=$#parts;$count++) {
 5405:         $filepath.="/$parts[$count]";
 5406:         if ((-e $filepath)!=1) {
 5407:             mkdir($filepath,02770);
 5408:         }
 5409:     }
 5410:     # Check for bad extension and disallow upload
 5411:     if ($file=~/\.(\w+)$/ &&
 5412:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5413:         $output = 
 5414:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 5415:     } elsif ($file=~/\.(\w+)$/ &&
 5416:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5417:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5418:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5419:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5420:     } elsif (-d "$filepath/$file") {
 5421:         $output = &mt('File name is a directory name - rename the file and re-upload');
 5422:     } else {
 5423:         my $source = $filepath.'/'.$file;
 5424:         my $logfile;
 5425:         if (!open($logfile,">>$source".'.log')) {
 5426:             return (&mt('No write permission to Construction Space'));
 5427:         }
 5428:         print $logfile
 5429: "\n================= Publish ".localtime()." ================\n".
 5430: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5431: # Save the file
 5432:         if (!open(FH,'>'.$source)) {
 5433:             &Apache::lonnet::logthis('Failed to create '.$source);
 5434:             return (&mt('Failed to create file'));
 5435:         }
 5436:         if ($action eq 'upload') {
 5437:             if (!print FH ($env{'form.'.$formname})) {
 5438:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5439:                 return (&mt('Failed to write file'));
 5440:             }
 5441:         } else {
 5442:             my $original = &Apache::lonnet::filelocation('',$formname);
 5443:             if(!copy($original,$source)) {
 5444:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5445:                 return (&mt('Failed to write file'));
 5446:             }
 5447:         }
 5448:         close(FH);
 5449:         chmod(0660, $source); # Permissions to rw-rw---.
 5450: 
 5451:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5452:         my $copyfile=$targetdir.'/'.$file;
 5453: 
 5454:         my @parts=split(/\//,$targetdir);
 5455:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5456:         for (my $count=5;$count<=$#parts;$count++) {
 5457:             $path.="/$parts[$count]";
 5458:             if (!-e $path) {
 5459:                 print $logfile "\nCreating directory ".$path;
 5460:                 mkdir($path,02770);
 5461:             }
 5462:         }
 5463:         my $versionresult;
 5464:         if (-e $copyfile) {
 5465:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5466:         } else {
 5467:             $versionresult = 'ok';
 5468:         }
 5469:         if ($versionresult eq 'ok') {
 5470:             if (copy($source,$copyfile)) {
 5471:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5472:                 $output = 'ok';
 5473:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5474:                 push(@{$modified_urls},[$copyfile,$source]);
 5475:                 my $metaoutput = 
 5476:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5477:                 unless ($registered_cleanup) {
 5478:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5479:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5480:                     $registered_cleanup=1;
 5481:                 }
 5482:             } else {
 5483:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5484:                 $output = &mt('Failed to copy file to RES space').", $!";
 5485:             }
 5486:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5487:                 my $inputfile = $filepath.'/'.$file;
 5488:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5489:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5490:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5491:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5492:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5493:                         system("convert -sample $thumbsize $inputfile $outfile");
 5494:                         chmod(0660, $filepath.'/tn-'.$file);
 5495:                         if (-e $outfile) {
 5496:                             my $copyfile=$targetdir.'/tn-'.$file;
 5497:                             if (copy($outfile,$copyfile)) {
 5498:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5499:                                 my $thumb_metaoutput = 
 5500:                                     &write_metadata($dom,$confname,$formname,
 5501:                                                     $targetdir,'tn-'.$file,$logfile);
 5502:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5503:                                 unless ($registered_cleanup) {
 5504:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5505:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5506:                                     $registered_cleanup=1;
 5507:                                 }
 5508:                             } else {
 5509:                                 print $logfile "\nUnable to write ".$copyfile.
 5510:                                                ':'.$!."\n";
 5511:                             }
 5512:                         }
 5513:                     }
 5514:                 }
 5515:             }
 5516:         } else {
 5517:             $output = $versionresult;
 5518:         }
 5519:     }
 5520:     return ($output,$logourl);
 5521: }
 5522: 
 5523: sub logo_versioning {
 5524:     my ($targetdir,$file,$logfile) = @_;
 5525:     my $target = $targetdir.'/'.$file;
 5526:     my ($maxversion,$fn,$extn,$output);
 5527:     $maxversion = 0;
 5528:     if ($file =~ /^(.+)\.(\w+)$/) {
 5529:         $fn=$1;
 5530:         $extn=$2;
 5531:     }
 5532:     opendir(DIR,$targetdir);
 5533:     while (my $filename=readdir(DIR)) {
 5534:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5535:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5536:         }
 5537:     }
 5538:     $maxversion++;
 5539:     print $logfile "\nCreating old version ".$maxversion."\n";
 5540:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5541:     if (copy($target,$copyfile)) {
 5542:         print $logfile "Copied old target to ".$copyfile."\n";
 5543:         $copyfile=$copyfile.'.meta';
 5544:         if (copy($target.'.meta',$copyfile)) {
 5545:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5546:             $output = 'ok';
 5547:         } else {
 5548:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5549:             $output = &mt('Failed to copy old meta').", $!, ";
 5550:         }
 5551:     } else {
 5552:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5553:         $output = &mt('Failed to copy old target').", $!, ";
 5554:     }
 5555:     return $output;
 5556: }
 5557: 
 5558: sub write_metadata {
 5559:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5560:     my (%metadatafields,%metadatakeys,$output);
 5561:     $metadatafields{'title'}=$formname;
 5562:     $metadatafields{'creationdate'}=time;
 5563:     $metadatafields{'lastrevisiondate'}=time;
 5564:     $metadatafields{'copyright'}='public';
 5565:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5566:                                          $env{'user.domain'};
 5567:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5568:     $metadatafields{'domain'}=$dom;
 5569:     {
 5570:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5571:         my $mfh;
 5572:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5573:             foreach (sort(keys(%metadatafields))) {
 5574:                 unless ($_=~/\./) {
 5575:                     my $unikey=$_;
 5576:                     $unikey=~/^([A-Za-z]+)/;
 5577:                     my $tag=$1;
 5578:                     $tag=~tr/A-Z/a-z/;
 5579:                     print $mfh "\n\<$tag";
 5580:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5581:                         my $value=$metadatafields{$unikey.'.'.$_};
 5582:                         $value=~s/\"/\'\'/g;
 5583:                         print $mfh ' '.$_.'="'.$value.'"';
 5584:                     }
 5585:                     print $mfh '>'.
 5586:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5587:                             .'</'.$tag.'>';
 5588:                 }
 5589:             }
 5590:             $output = 'ok';
 5591:             print $logfile "\nWrote metadata";
 5592:             close($mfh);
 5593:         } else {
 5594:             print $logfile "\nFailed to open metadata file";
 5595:             $output = &mt('Could not write metadata');
 5596:         }
 5597:     }
 5598:     return $output;
 5599: }
 5600: 
 5601: sub notifysubscribed {
 5602:     foreach my $targetsource (@{$modified_urls}){
 5603:         next unless (ref($targetsource) eq 'ARRAY');
 5604:         my ($target,$source)=@{$targetsource};
 5605:         if ($source ne '') {
 5606:             if (open(my $logfh,'>>'.$source.'.log')) {
 5607:                 print $logfh "\nCleanup phase: Notifications\n";
 5608:                 my @subscribed=&subscribed_hosts($target);
 5609:                 foreach my $subhost (@subscribed) {
 5610:                     print $logfh "\nNotifying host ".$subhost.':';
 5611:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5612:                     print $logfh $reply;
 5613:                 }
 5614:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5615:                 foreach my $subhost (@subscribedmeta) {
 5616:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5617:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5618:                                                         $subhost);
 5619:                     print $logfh $reply;
 5620:                 }
 5621:                 print $logfh "\n============ Done ============\n";
 5622:                 close($logfh);
 5623:             }
 5624:         }
 5625:     }
 5626:     return OK;
 5627: }
 5628: 
 5629: sub subscribed_hosts {
 5630:     my ($target) = @_;
 5631:     my @subscribed;
 5632:     if (open(my $fh,"<$target.subscription")) {
 5633:         while (my $subline=<$fh>) {
 5634:             if ($subline =~ /^($match_lonid):/) {
 5635:                 my $host = $1;
 5636:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5637:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5638:                         push(@subscribed,$host);
 5639:                     }
 5640:                 }
 5641:             }
 5642:         }
 5643:     }
 5644:     return @subscribed;
 5645: }
 5646: 
 5647: sub check_switchserver {
 5648:     my ($dom,$confname) = @_;
 5649:     my ($allowed,$switchserver);
 5650:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5651:     if ($home eq 'no_host') {
 5652:         $home = &Apache::lonnet::domain($dom,'primary');
 5653:     }
 5654:     my @ids=&Apache::lonnet::current_machine_ids();
 5655:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5656:     if (!$allowed) {
 5657: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5658:     }
 5659:     return $switchserver;
 5660: }
 5661: 
 5662: sub modify_quotas {
 5663:     my ($dom,$action,%domconfig) = @_;
 5664:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5665:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5666:     if ($action eq 'quotas') {
 5667:         $context = 'tools'; 
 5668:     } else {
 5669:         $context = $action;
 5670:     }
 5671:     if ($context eq 'requestcourses') {
 5672:         @usertools = ('official','unofficial','community');
 5673:         @options =('norequest','approval','validate','autolimit');
 5674:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5675:         %titles = &courserequest_titles();
 5676:         $toolregexp = join('|',@usertools);
 5677:         %conditions = &courserequest_conditions();
 5678:     } elsif ($context eq 'requestauthor') {
 5679:         @usertools = ('author');
 5680:         %titles = &authorrequest_titles();
 5681:     } else {
 5682:         @usertools = ('aboutme','blog','webdav','portfolio');
 5683:         %titles = &tool_titles();
 5684:     }
 5685:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5686:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5687:     foreach my $key (keys(%env)) {
 5688:         if ($context eq 'requestcourses') {
 5689:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5690:                 my $item = $1;
 5691:                 my $type = $2;
 5692:                 if ($type =~ /^limit_(.+)/) {
 5693:                     $limithash{$item}{$1} = $env{$key};
 5694:                 } else {
 5695:                     $confhash{$item}{$type} = $env{$key};
 5696:                 }
 5697:             }
 5698:         } elsif ($context eq 'requestauthor') {
 5699:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5700:                 $confhash{$1} = $env{$key};
 5701:             }
 5702:         } else {
 5703:             if ($key =~ /^form\.quota_(.+)$/) {
 5704:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5705:             }
 5706:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5707:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5708:             }
 5709:         }
 5710:     }
 5711:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5712:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5713:         @approvalnotify = sort(@approvalnotify);
 5714:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5715:         if (ref($domconfig{$action}) eq 'HASH') {
 5716:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5717:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5718:                     $changes{'notify'}{'approval'} = 1;
 5719:                 }
 5720:             } else {
 5721:                 if ($confhash{'notify'}{'approval'}) {
 5722:                     $changes{'notify'}{'approval'} = 1;
 5723:                 }
 5724:             }
 5725:         } else {
 5726:             if ($confhash{'notify'}{'approval'}) {
 5727:                 $changes{'notify'}{'approval'} = 1;
 5728:             }
 5729:         }
 5730:     } else {
 5731:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5732:     }
 5733:     foreach my $item (@usertools) {
 5734:         foreach my $type (@{$types},'default','_LC_adv') {
 5735:             my $unset; 
 5736:             if ($context eq 'requestcourses') {
 5737:                 $unset = '0';
 5738:                 if ($type eq '_LC_adv') {
 5739:                     $unset = '';
 5740:                 }
 5741:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5742:                     $confhash{$item}{$type} .= '=';
 5743:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5744:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5745:                     }
 5746:                 }
 5747:             } elsif ($context eq 'requestauthor') {
 5748:                 $unset = '0';
 5749:                 if ($type eq '_LC_adv') {
 5750:                     $unset = '';
 5751:                 }
 5752:             } else {
 5753:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5754:                     $confhash{$item}{$type} = 1;
 5755:                 } else {
 5756:                     $confhash{$item}{$type} = 0;
 5757:                 }
 5758:             }
 5759:             if (ref($domconfig{$action}) eq 'HASH') {
 5760:                 if ($action eq 'requestauthor') {
 5761:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5762:                         $changes{$type} = 1;
 5763:                     }
 5764:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5765:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5766:                         $changes{$item}{$type} = 1;
 5767:                     }
 5768:                 } else {
 5769:                     if ($context eq 'requestcourses') {
 5770:                         if ($confhash{$item}{$type} ne $unset) {
 5771:                             $changes{$item}{$type} = 1;
 5772:                         }
 5773:                     } else {
 5774:                         if (!$confhash{$item}{$type}) {
 5775:                             $changes{$item}{$type} = 1;
 5776:                         }
 5777:                     }
 5778:                 }
 5779:             } else {
 5780:                 if ($context eq 'requestcourses') {
 5781:                     if ($confhash{$item}{$type} ne $unset) {
 5782:                         $changes{$item}{$type} = 1;
 5783:                     }
 5784:                 } elsif ($context eq 'requestauthor') {
 5785:                     if ($confhash{$type} ne $unset) {
 5786:                         $changes{$type} = 1;
 5787:                     }
 5788:                 } else {
 5789:                     if (!$confhash{$item}{$type}) {
 5790:                         $changes{$item}{$type} = 1;
 5791:                     }
 5792:                 }
 5793:             }
 5794:         }
 5795:     }
 5796:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5797:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5798:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5799:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5800:                     if (exists($confhash{'defaultquota'}{$key})) {
 5801:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5802:                             $changes{'defaultquota'}{$key} = 1;
 5803:                         }
 5804:                     } else {
 5805:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5806:                     }
 5807:                 }
 5808:             } else {
 5809:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5810:                     if (exists($confhash{'defaultquota'}{$key})) {
 5811:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5812:                             $changes{'defaultquota'}{$key} = 1;
 5813:                         }
 5814:                     } else {
 5815:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5816:                     }
 5817:                 }
 5818:             }
 5819:         }
 5820:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5821:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5822:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5823:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5824:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5825:                             $changes{'defaultquota'}{$key} = 1;
 5826:                         }
 5827:                     } else {
 5828:                         if (!exists($domconfig{'quotas'}{$key})) {
 5829:                             $changes{'defaultquota'}{$key} = 1;
 5830:                         }
 5831:                     }
 5832:                 } else {
 5833:                     $changes{'defaultquota'}{$key} = 1;
 5834:                 }
 5835:             }
 5836:         }
 5837:     }
 5838: 
 5839:     if ($context eq 'requestauthor') {
 5840:         $domdefaults{'requestauthor'} = \%confhash;
 5841:     } else {
 5842:         foreach my $key (keys(%confhash)) {
 5843:             $domdefaults{$key} = $confhash{$key};
 5844:         }
 5845:     }
 5846: 
 5847:     my %quotahash = (
 5848:                       $action => { %confhash }
 5849:                     );
 5850:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5851:                                              $dom);
 5852:     if ($putresult eq 'ok') {
 5853:         if (keys(%changes) > 0) {
 5854:             my $cachetime = 24*60*60;
 5855:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5856: 
 5857:             $resulttext = &mt('Changes made:').'<ul>';
 5858:             unless (($context eq 'requestcourses') || 
 5859:                     ($context eq 'requestauthor')) {
 5860:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5861:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5862:                     foreach my $type (@{$types},'default') {
 5863:                         if (defined($changes{'defaultquota'}{$type})) {
 5864:                             my $typetitle = $usertypes->{$type};
 5865:                             if ($type eq 'default') {
 5866:                                 $typetitle = $othertitle;
 5867:                             }
 5868:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5869:                         }
 5870:                     }
 5871:                     $resulttext .= '</ul></li>';
 5872:                 }
 5873:             }
 5874:             my %newenv;
 5875:             foreach my $item (@usertools) {
 5876:                 my (%haschgs,%inconf);
 5877:                 if ($context eq 'requestauthor') {
 5878:                     %haschgs = %changes;
 5879:                     %inconf = %confhash; 
 5880:                 } else {
 5881:                     if (ref($changes{$item}) eq 'HASH') {
 5882:                         %haschgs = %{$changes{$item}};
 5883:                     }
 5884:                     if (ref($confhash{$item}) eq 'HASH') {
 5885:                         %inconf = %{$confhash{$item}};
 5886:                     }
 5887:                 }
 5888:                 if (keys(%haschgs) > 0) {
 5889:                     my $newacc = 
 5890:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5891:                                                           $env{'user.domain'},
 5892:                                                           $item,'reload',$context);
 5893:                     if (($context eq 'requestcourses') || 
 5894:                         ($context eq 'requestauthor')) {
 5895:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5896:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5897:                         }
 5898:                     } else {
 5899:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5900:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5901:                         }
 5902:                     }
 5903:                     unless ($context eq 'requestauthor') {
 5904:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5905:                     }
 5906:                     foreach my $type (@{$types},'default','_LC_adv') {
 5907:                         if ($haschgs{$type}) {
 5908:                             my $typetitle = $usertypes->{$type};
 5909:                             if ($type eq 'default') {
 5910:                                 $typetitle = $othertitle;
 5911:                             } elsif ($type eq '_LC_adv') {
 5912:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5913:                             }
 5914:                             if ($inconf{$type}) {
 5915:                                 if ($context eq 'requestcourses') {
 5916:                                     my $cond;
 5917:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 5918:                                         if ($1 eq '') {
 5919:                                             $cond = &mt('(Automatic processing of any request).');
 5920:                                         } else {
 5921:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5922:                                         }
 5923:                                     } else { 
 5924:                                         $cond = $conditions{$inconf{$type}};
 5925:                                     }
 5926:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5927:                                 } elsif ($context eq 'requestauthor') {
 5928:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 5929:                                                              $titles{$inconf{$type}},$typetitle);
 5930: 
 5931:                                 } else {
 5932:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5933:                                 }
 5934:                             } else {
 5935:                                 if ($type eq '_LC_adv') {
 5936:                                     if ($inconf{$type} eq '0') {
 5937:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5938:                                     } else { 
 5939:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5940:                                     }
 5941:                                 } else {
 5942:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5943:                                 }
 5944:                             }
 5945:                         }
 5946:                     }
 5947:                     unless ($context eq 'requestauthor') {
 5948:                         $resulttext .= '</ul></li>';
 5949:                     }
 5950:                 }
 5951:             }
 5952:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 5953:                 if (ref($changes{'notify'}) eq 'HASH') {
 5954:                     if ($changes{'notify'}{'approval'}) {
 5955:                         if (ref($confhash{'notify'}) eq 'HASH') {
 5956:                             if ($confhash{'notify'}{'approval'}) {
 5957:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 5958:                             } else {
 5959:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 5960:                             }
 5961:                         }
 5962:                     }
 5963:                 }
 5964:             }
 5965:             $resulttext .= '</ul>';
 5966:             if (keys(%newenv)) {
 5967:                 &Apache::lonnet::appenv(\%newenv);
 5968:             }
 5969:         } else {
 5970:             if ($context eq 'requestcourses') {
 5971:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 5972:             } elsif ($context eq 'requestauthor') {
 5973:                 $resulttext = &mt('No changes made to rights to request author space.');
 5974:             } else {
 5975:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 5976:             }
 5977:         }
 5978:     } else {
 5979:         $resulttext = '<span class="LC_error">'.
 5980: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5981:     }
 5982:     return $resulttext;
 5983: }
 5984: 
 5985: sub modify_autoenroll {
 5986:     my ($dom,%domconfig) = @_;
 5987:     my ($resulttext,%changes);
 5988:     my %currautoenroll;
 5989:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 5990:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 5991:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 5992:         }
 5993:     }
 5994:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 5995:     my %title = ( run => 'Auto-enrollment active',
 5996:                   sender => 'Sender for notification messages',
 5997:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 5998:     my @offon = ('off','on');
 5999:     my $sender_uname = $env{'form.sender_uname'};
 6000:     my $sender_domain = $env{'form.sender_domain'};
 6001:     if ($sender_domain eq '') {
 6002:         $sender_uname = '';
 6003:     } elsif ($sender_uname eq '') {
 6004:         $sender_domain = '';
 6005:     }
 6006:     my $coowners = $env{'form.autoassign_coowners'};
 6007:     my %autoenrollhash =  (
 6008:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6009:                                        'sender_uname' => $sender_uname,
 6010:                                        'sender_domain' => $sender_domain,
 6011:                                        'co-owners' => $coowners,
 6012:                                 }
 6013:                      );
 6014:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6015:                                              $dom);
 6016:     if ($putresult eq 'ok') {
 6017:         if (exists($currautoenroll{'run'})) {
 6018:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6019:                  $changes{'run'} = 1;
 6020:              }
 6021:         } elsif ($autorun) {
 6022:             if ($env{'form.autoenroll_run'} ne '1') {
 6023:                  $changes{'run'} = 1;
 6024:             }
 6025:         }
 6026:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6027:             $changes{'sender'} = 1;
 6028:         }
 6029:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6030:             $changes{'sender'} = 1;
 6031:         }
 6032:         if ($currautoenroll{'co-owners'} ne '') {
 6033:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6034:                 $changes{'coowners'} = 1;
 6035:             }
 6036:         } elsif ($coowners) {
 6037:             $changes{'coowners'} = 1;
 6038:         }      
 6039:         if (keys(%changes) > 0) {
 6040:             $resulttext = &mt('Changes made:').'<ul>';
 6041:             if ($changes{'run'}) {
 6042:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6043:             }
 6044:             if ($changes{'sender'}) {
 6045:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6046:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6047:                 } else {
 6048:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6049:                 }
 6050:             }
 6051:             if ($changes{'coowners'}) {
 6052:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6053:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6054:             }
 6055:             $resulttext .= '</ul>';
 6056:         } else {
 6057:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6058:         }
 6059:     } else {
 6060:         $resulttext = '<span class="LC_error">'.
 6061: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6062:     }
 6063:     return $resulttext;
 6064: }
 6065: 
 6066: sub modify_autoupdate {
 6067:     my ($dom,%domconfig) = @_;
 6068:     my ($resulttext,%currautoupdate,%fields,%changes);
 6069:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6070:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6071:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6072:         }
 6073:     }
 6074:     my @offon = ('off','on');
 6075:     my %title = &Apache::lonlocal::texthash (
 6076:                    run => 'Auto-update:',
 6077:                    classlists => 'Updates to user information in classlists?'
 6078:                 );
 6079:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6080:     my %fieldtitles = &Apache::lonlocal::texthash (
 6081:                         id => 'Student/Employee ID',
 6082:                         permanentemail => 'E-mail address',
 6083:                         lastname => 'Last Name',
 6084:                         firstname => 'First Name',
 6085:                         middlename => 'Middle Name',
 6086:                         generation => 'Generation',
 6087:                       );
 6088:     $othertitle = &mt('All users');
 6089:     if (keys(%{$usertypes}) >  0) {
 6090:         $othertitle = &mt('Other users');
 6091:     }
 6092:     foreach my $key (keys(%env)) {
 6093:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6094:             my ($usertype,$item) = ($1,$2);
 6095:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6096:                 if ($usertype eq 'default') {   
 6097:                     push(@{$fields{$1}},$2);
 6098:                 } elsif (ref($types) eq 'ARRAY') {
 6099:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6100:                         push(@{$fields{$1}},$2);
 6101:                     }
 6102:                 }
 6103:             }
 6104:         }
 6105:     }
 6106:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6107:     @lockablenames = sort(@lockablenames);
 6108:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6109:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6110:         if (@changed) {
 6111:             $changes{'lockablenames'} = 1;
 6112:         }
 6113:     } else {
 6114:         if (@lockablenames) {
 6115:             $changes{'lockablenames'} = 1;
 6116:         }
 6117:     }
 6118:     my %updatehash = (
 6119:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6120:                                       classlists => $env{'form.classlists'},
 6121:                                       fields => {%fields},
 6122:                                       lockablenames => \@lockablenames,
 6123:                                     }
 6124:                      );
 6125:     foreach my $key (keys(%currautoupdate)) {
 6126:         if (($key eq 'run') || ($key eq 'classlists')) {
 6127:             if (exists($updatehash{autoupdate}{$key})) {
 6128:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6129:                     $changes{$key} = 1;
 6130:                 }
 6131:             }
 6132:         } elsif ($key eq 'fields') {
 6133:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6134:                 foreach my $item (@{$types},'default') {
 6135:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6136:                         my $change = 0;
 6137:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6138:                             if (!exists($fields{$item})) {
 6139:                                 $change = 1;
 6140:                                 last;
 6141:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6142:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6143:                                     $change = 1;
 6144:                                     last;
 6145:                                 }
 6146:                             }
 6147:                         }
 6148:                         if ($change) {
 6149:                             push(@{$changes{$key}},$item);
 6150:                         }
 6151:                     } 
 6152:                 }
 6153:             }
 6154:         } elsif ($key eq 'lockablenames') {
 6155:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6156:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6157:                 if (@changed) {
 6158:                     $changes{'lockablenames'} = 1;
 6159:                 }
 6160:             } else {
 6161:                 if (@lockablenames) {
 6162:                     $changes{'lockablenames'} = 1;
 6163:                 }
 6164:             }
 6165:         }
 6166:     }
 6167:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6168:         if (@lockablenames) {
 6169:             $changes{'lockablenames'} = 1;
 6170:         }
 6171:     }
 6172:     foreach my $item (@{$types},'default') {
 6173:         if (defined($fields{$item})) {
 6174:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6175:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6176:                     my $change = 0;
 6177:                     if (ref($fields{$item}) eq 'ARRAY') {
 6178:                         foreach my $type (@{$fields{$item}}) {
 6179:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6180:                                 $change = 1;
 6181:                                 last;
 6182:                             }
 6183:                         }
 6184:                     }
 6185:                     if ($change) {
 6186:                         push(@{$changes{'fields'}},$item);
 6187:                     }
 6188:                 } else {
 6189:                     push(@{$changes{'fields'}},$item);
 6190:                 }
 6191:             } else {
 6192:                 push(@{$changes{'fields'}},$item);
 6193:             }
 6194:         }
 6195:     }
 6196:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6197:                                              $dom);
 6198:     if ($putresult eq 'ok') {
 6199:         if (keys(%changes) > 0) {
 6200:             $resulttext = &mt('Changes made:').'<ul>';
 6201:             foreach my $key (sort(keys(%changes))) {
 6202:                 if ($key eq 'lockablenames') {
 6203:                     $resulttext .= '<li>';
 6204:                     if (@lockablenames) {
 6205:                         $usertypes->{'default'} = $othertitle;
 6206:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6207:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6208:                     } else {
 6209:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6210:                     }
 6211:                     $resulttext .= '</li>';
 6212:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6213:                     foreach my $item (@{$changes{$key}}) {
 6214:                         my @newvalues;
 6215:                         foreach my $type (@{$fields{$item}}) {
 6216:                             push(@newvalues,$fieldtitles{$type});
 6217:                         }
 6218:                         my $newvaluestr;
 6219:                         if (@newvalues > 0) {
 6220:                             $newvaluestr = join(', ',@newvalues);
 6221:                         } else {
 6222:                             $newvaluestr = &mt('none');
 6223:                         }
 6224:                         if ($item eq 'default') {
 6225:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6226:                         } else {
 6227:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6228:                         }
 6229:                     }
 6230:                 } else {
 6231:                     my $newvalue;
 6232:                     if ($key eq 'run') {
 6233:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6234:                     } else {
 6235:                         $newvalue = $offon[$env{'form.'.$key}];
 6236:                     }
 6237:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6238:                 }
 6239:             }
 6240:             $resulttext .= '</ul>';
 6241:         } else {
 6242:             $resulttext = &mt('No changes made to autoupdates');
 6243:         }
 6244:     } else {
 6245:         $resulttext = '<span class="LC_error">'.
 6246: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6247:     }
 6248:     return $resulttext;
 6249: }
 6250: 
 6251: sub modify_autocreate {
 6252:     my ($dom,%domconfig) = @_;
 6253:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6254:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6255:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6256:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6257:         }
 6258:     }
 6259:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6260:                  req => 'Auto-creation of validated requests for official courses',
 6261:                  xmldc => 'Identity of course creator of courses from XML files',
 6262:                );
 6263:     my @types = ('xml','req');
 6264:     foreach my $item (@types) {
 6265:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6266:         $newvals{$item} =~ s/\D//g;
 6267:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6268:     }
 6269:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6270:     my %domcoords = &get_active_dcs($dom);
 6271:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6272:         $newvals{'xmldc'} = '';
 6273:     } 
 6274:     %autocreatehash =  (
 6275:                         autocreate => { xml => $newvals{'xml'},
 6276:                                         req => $newvals{'req'},
 6277:                                       }
 6278:                        );
 6279:     if ($newvals{'xmldc'} ne '') {
 6280:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6281:     }
 6282:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6283:                                              $dom);
 6284:     if ($putresult eq 'ok') {
 6285:         my @items = @types;
 6286:         if ($newvals{'xml'}) {
 6287:             push(@items,'xmldc');
 6288:         }
 6289:         foreach my $item (@items) {
 6290:             if (exists($currautocreate{$item})) {
 6291:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6292:                     $changes{$item} = 1;
 6293:                 }
 6294:             } elsif ($newvals{$item}) {
 6295:                 $changes{$item} = 1;
 6296:             }
 6297:         }
 6298:         if (keys(%changes) > 0) {
 6299:             my @offon = ('off','on'); 
 6300:             $resulttext = &mt('Changes made:').'<ul>';
 6301:             foreach my $item (@types) {
 6302:                 if ($changes{$item}) {
 6303:                     my $newtxt = $offon[$newvals{$item}];
 6304:                     $resulttext .= '<li>'.
 6305:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 6306:                                        '<b>','</b>').
 6307:                                    '</li>';
 6308:                 }
 6309:             }
 6310:             if ($changes{'xmldc'}) {
 6311:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6312:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6313:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 6314:             }
 6315:             $resulttext .= '</ul>';
 6316:         } else {
 6317:             $resulttext = &mt('No changes made to auto-creation settings');
 6318:         }
 6319:     } else {
 6320:         $resulttext = '<span class="LC_error">'.
 6321:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6322:     }
 6323:     return $resulttext;
 6324: }
 6325: 
 6326: sub modify_directorysrch {
 6327:     my ($dom,%domconfig) = @_;
 6328:     my ($resulttext,%changes);
 6329:     my %currdirsrch;
 6330:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6331:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6332:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6333:         }
 6334:     }
 6335:     my %title = ( available => 'Directory search available',
 6336:                   localonly => 'Other domains can search',
 6337:                   searchby => 'Search types',
 6338:                   searchtypes => 'Search latitude');
 6339:     my @offon = ('off','on');
 6340:     my @otherdoms = ('Yes','No');
 6341: 
 6342:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6343:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6344:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6345: 
 6346:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6347:     if (keys(%{$usertypes}) == 0) {
 6348:         @cansearch = ('default');
 6349:     } else {
 6350:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6351:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6352:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6353:                     push(@{$changes{'cansearch'}},$type);
 6354:                 }
 6355:             }
 6356:             foreach my $type (@cansearch) {
 6357:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6358:                     push(@{$changes{'cansearch'}},$type);
 6359:                 }
 6360:             }
 6361:         } else {
 6362:             push(@{$changes{'cansearch'}},@cansearch);
 6363:         }
 6364:     }
 6365: 
 6366:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6367:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6368:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6369:                 push(@{$changes{'searchby'}},$by);
 6370:             }
 6371:         }
 6372:         foreach my $by (@searchby) {
 6373:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6374:                 push(@{$changes{'searchby'}},$by);
 6375:             }
 6376:         }
 6377:     } else {
 6378:         push(@{$changes{'searchby'}},@searchby);
 6379:     }
 6380: 
 6381:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6382:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6383:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6384:                 push(@{$changes{'searchtypes'}},$type);
 6385:             }
 6386:         }
 6387:         foreach my $type (@searchtypes) {
 6388:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6389:                 push(@{$changes{'searchtypes'}},$type);
 6390:             }
 6391:         }
 6392:     } else {
 6393:         if (exists($currdirsrch{'searchtypes'})) {
 6394:             foreach my $type (@searchtypes) {  
 6395:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6396:                     push(@{$changes{'searchtypes'}},$type);
 6397:                 }
 6398:             }
 6399:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6400:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6401:             }   
 6402:         } else {
 6403:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6404:         }
 6405:     }
 6406: 
 6407:     my %dirsrch_hash =  (
 6408:             directorysrch => { available => $env{'form.dirsrch_available'},
 6409:                                cansearch => \@cansearch,
 6410:                                localonly => $env{'form.dirsrch_localonly'},
 6411:                                searchby => \@searchby,
 6412:                                searchtypes => \@searchtypes,
 6413:                              }
 6414:             );
 6415:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6416:                                              $dom);
 6417:     if ($putresult eq 'ok') {
 6418:         if (exists($currdirsrch{'available'})) {
 6419:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6420:                  $changes{'available'} = 1;
 6421:              }
 6422:         } else {
 6423:             if ($env{'form.dirsrch_available'} eq '1') {
 6424:                 $changes{'available'} = 1;
 6425:             }
 6426:         }
 6427:         if (exists($currdirsrch{'localonly'})) {
 6428:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6429:                  $changes{'localonly'} = 1;
 6430:              }
 6431:         } else {
 6432:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6433:                 $changes{'localonly'} = 1;
 6434:             }
 6435:         }
 6436:         if (keys(%changes) > 0) {
 6437:             $resulttext = &mt('Changes made:').'<ul>';
 6438:             if ($changes{'available'}) {
 6439:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6440:             }
 6441:             if ($changes{'localonly'}) {
 6442:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6443:             }
 6444: 
 6445:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6446:                 my $chgtext;
 6447:                 if (ref($usertypes) eq 'HASH') {
 6448:                     if (keys(%{$usertypes}) > 0) {
 6449:                         foreach my $type (@{$types}) {
 6450:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6451:                                 $chgtext .= $usertypes->{$type}.'; ';
 6452:                             }
 6453:                         }
 6454:                         if (grep(/^default$/,@cansearch)) {
 6455:                             $chgtext .= $othertitle;
 6456:                         } else {
 6457:                             $chgtext =~ s/\; $//;
 6458:                         }
 6459:                         $resulttext .= 
 6460:                             '<li>'.
 6461:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 6462:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 6463:                             '</li>';
 6464:                     }
 6465:                 }
 6466:             }
 6467:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6468:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6469:                 my $chgtext;
 6470:                 foreach my $type (@{$titleorder}) {
 6471:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6472:                         if (defined($searchtitles->{$type})) {
 6473:                             $chgtext .= $searchtitles->{$type}.'; ';
 6474:                         }
 6475:                     }
 6476:                 }
 6477:                 $chgtext =~ s/\; $//;
 6478:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6479:             }
 6480:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6481:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6482:                 my $chgtext;
 6483:                 foreach my $type (@{$srchtypeorder}) {
 6484:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6485:                         if (defined($srchtypes_desc->{$type})) {
 6486:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6487:                         }
 6488:                     }
 6489:                 }
 6490:                 $chgtext =~ s/\; $//;
 6491:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 6492:             }
 6493:             $resulttext .= '</ul>';
 6494:         } else {
 6495:             $resulttext = &mt('No changes made to institution directory search settings');
 6496:         }
 6497:     } else {
 6498:         $resulttext = '<span class="LC_error">'.
 6499:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6500:     }
 6501:     return $resulttext;
 6502: }
 6503: 
 6504: sub modify_contacts {
 6505:     my ($dom,%domconfig) = @_;
 6506:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6507:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6508:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6509:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6510:         }
 6511:     }
 6512:     my (%others,%to,%bcc);
 6513:     my @contacts = ('supportemail','adminemail');
 6514:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6515:                     'requestsmail','updatesmail');
 6516:     foreach my $type (@mailings) {
 6517:         @{$newsetting{$type}} = 
 6518:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6519:         foreach my $item (@contacts) {
 6520:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6521:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6522:             } else {
 6523:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6524:             }
 6525:         }  
 6526:         $others{$type} = $env{'form.'.$type.'_others'};
 6527:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6528:         if ($type eq 'helpdeskmail') {
 6529:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6530:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6531:         }
 6532:     }
 6533:     foreach my $item (@contacts) {
 6534:         $to{$item} = $env{'form.'.$item};
 6535:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6536:     }
 6537:     if (keys(%currsetting) > 0) {
 6538:         foreach my $item (@contacts) {
 6539:             if ($to{$item} ne $currsetting{$item}) {
 6540:                 $changes{$item} = 1;
 6541:             }
 6542:         }
 6543:         foreach my $type (@mailings) {
 6544:             foreach my $item (@contacts) {
 6545:                 if (ref($currsetting{$type}) eq 'HASH') {
 6546:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6547:                         push(@{$changes{$type}},$item);
 6548:                     }
 6549:                 } else {
 6550:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6551:                 }
 6552:             }
 6553:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6554:                 push(@{$changes{$type}},'others');
 6555:             }
 6556:             if ($type eq 'helpdeskmail') {   
 6557:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6558:                     push(@{$changes{$type}},'bcc'); 
 6559:                 }
 6560:             }
 6561:         }
 6562:     } else {
 6563:         my %default;
 6564:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6565:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6566:         $default{'errormail'} = 'adminemail';
 6567:         $default{'packagesmail'} = 'adminemail';
 6568:         $default{'helpdeskmail'} = 'supportemail';
 6569:         $default{'lonstatusmail'} = 'adminemail';
 6570:         $default{'requestsmail'} = 'adminemail';
 6571:         $default{'updatesmail'} = 'adminemail';
 6572:         foreach my $item (@contacts) {
 6573:            if ($to{$item} ne $default{$item}) {
 6574:               $changes{$item} = 1;
 6575:            } 
 6576:         }
 6577:         foreach my $type (@mailings) {
 6578:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6579:                
 6580:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6581:             }
 6582:             if ($others{$type} ne '') {
 6583:                 push(@{$changes{$type}},'others');
 6584:             }
 6585:             if ($type eq 'helpdeskmail') {
 6586:                 if ($bcc{$type} ne '') {
 6587:                     push(@{$changes{$type}},'bcc');
 6588:                 }
 6589:             }
 6590:         }
 6591:     }
 6592:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6593:                                              $dom);
 6594:     if ($putresult eq 'ok') {
 6595:         if (keys(%changes) > 0) {
 6596:             my ($titles,$short_titles)  = &contact_titles();
 6597:             $resulttext = &mt('Changes made:').'<ul>';
 6598:             foreach my $item (@contacts) {
 6599:                 if ($changes{$item}) {
 6600:                     $resulttext .= '<li>'.$titles->{$item}.
 6601:                                     &mt(' set to: ').
 6602:                                     '<span class="LC_cusr_emph">'.
 6603:                                     $to{$item}.'</span></li>';
 6604:                 }
 6605:             }
 6606:             foreach my $type (@mailings) {
 6607:                 if (ref($changes{$type}) eq 'ARRAY') {
 6608:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6609:                     my @text;
 6610:                     foreach my $item (@{$newsetting{$type}}) {
 6611:                         push(@text,$short_titles->{$item});
 6612:                     }
 6613:                     if ($others{$type} ne '') {
 6614:                         push(@text,$others{$type});
 6615:                     }
 6616:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6617:                                    join(', ',@text).'</span>';
 6618:                     if ($type eq 'helpdeskmail') {
 6619:                         if ($bcc{$type} ne '') {
 6620:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6621:                         }
 6622:                     }
 6623:                     $resulttext .= '</li>';
 6624:                 }
 6625:             }
 6626:             $resulttext .= '</ul>';
 6627:         } else {
 6628:             $resulttext = &mt('No changes made to contact information');
 6629:         }
 6630:     } else {
 6631:         $resulttext = '<span class="LC_error">'.
 6632:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6633:     }
 6634:     return $resulttext;
 6635: }
 6636: 
 6637: sub modify_usercreation {
 6638:     my ($dom,%domconfig) = @_;
 6639:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6640:     my $warningmsg;
 6641:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6642:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6643:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6644:         }
 6645:     }
 6646:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6647:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6648:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6649:     my @contexts = ('author','course','requestcrs','selfcreate');
 6650:     foreach my $item(@contexts) {
 6651:         if ($item eq 'selfcreate') {
 6652:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6653:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6654:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6655:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6656:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6657:                         $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.');   
 6658:                     }
 6659:                 }
 6660:             }
 6661:         } else {
 6662:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6663:         }
 6664:     }
 6665:     my ($othertitle,$usertypes,$types) = 
 6666:         &Apache::loncommon::sorted_inst_types($dom);
 6667:     if (ref($types) eq 'ARRAY') {
 6668:         if (@{$types} > 0) {
 6669:             @{$cancreate{'statustocreate'}} = 
 6670:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6671:         } else {
 6672:             @{$cancreate{'statustocreate'}} = ();
 6673:         }
 6674:         push(@contexts,'statustocreate');
 6675:     }
 6676:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6677:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6678:         foreach my $item (@contexts) {
 6679:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6680:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6681:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6682:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6683:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6684:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6685:                                     push(@{$changes{'cancreate'}},$item);
 6686:                                 }
 6687:                             }
 6688:                         }
 6689:                     }
 6690:                 } else {
 6691:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6692:                         if (@{$cancreate{$item}} > 0) {
 6693:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6694:                                 push(@{$changes{'cancreate'}},$item);
 6695:                             }
 6696:                         }
 6697:                     } else {
 6698:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6699:                             if (@{$cancreate{$item}} < 3) {
 6700:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6701:                                     push(@{$changes{'cancreate'}},$item);
 6702:                                 }
 6703:                             }
 6704:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6705:                             if (@{$cancreate{$item}} > 0) {
 6706:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6707:                                     push(@{$changes{'cancreate'}},$item);
 6708:                                 }
 6709:                             }
 6710:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6711:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6712:                                 push(@{$changes{'cancreate'}},$item);
 6713:                             }
 6714:                         }
 6715:                     }
 6716:                 }
 6717:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6718:                     foreach my $type (@{$cancreate{$item}}) {
 6719:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6720:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6721:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6722:                                     push(@{$changes{'cancreate'}},$item);
 6723:                                 }
 6724:                             }
 6725:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6726:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6727:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6728:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6729:                                     push(@{$changes{'cancreate'}},$item);
 6730:                                 }
 6731:                             }
 6732:                         }
 6733:                     }
 6734:                 }
 6735:             } else {
 6736:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6737:                     push(@{$changes{'cancreate'}},$item);
 6738:                 }
 6739:             }
 6740:         }
 6741:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6742:         foreach my $item (@contexts) {
 6743:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6744:                 if ($cancreate{$item} ne 'any') {
 6745:                     push(@{$changes{'cancreate'}},$item);
 6746:                 }
 6747:             } else {
 6748:                 if ($cancreate{$item} ne 'none') {
 6749:                     push(@{$changes{'cancreate'}},$item);
 6750:                 }
 6751:             }
 6752:         }
 6753:     } else {
 6754:         foreach my $item (@contexts)  {
 6755:             push(@{$changes{'cancreate'}},$item);
 6756:         }
 6757:     }
 6758: 
 6759:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6760:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6761:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6762:                 push(@{$changes{'username_rule'}},$type);
 6763:             }
 6764:         }
 6765:         foreach my $type (@username_rule) {
 6766:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6767:                 push(@{$changes{'username_rule'}},$type);
 6768:             }
 6769:         }
 6770:     } else {
 6771:         push(@{$changes{'username_rule'}},@username_rule);
 6772:     }
 6773: 
 6774:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6775:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 6776:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 6777:                 push(@{$changes{'id_rule'}},$type);
 6778:             }
 6779:         }
 6780:         foreach my $type (@id_rule) {
 6781:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 6782:                 push(@{$changes{'id_rule'}},$type);
 6783:             }
 6784:         }
 6785:     } else {
 6786:         push(@{$changes{'id_rule'}},@id_rule);
 6787:     }
 6788: 
 6789:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 6790:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 6791:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 6792:                 push(@{$changes{'email_rule'}},$type);
 6793:             }
 6794:         }
 6795:         foreach my $type (@email_rule) {
 6796:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 6797:                 push(@{$changes{'email_rule'}},$type);
 6798:             }
 6799:         }
 6800:     } else {
 6801:         push(@{$changes{'email_rule'}},@email_rule);
 6802:     }
 6803: 
 6804:     my @authen_contexts = ('author','course','domain');
 6805:     my @authtypes = ('int','krb4','krb5','loc');
 6806:     my %authhash;
 6807:     foreach my $item (@authen_contexts) {
 6808:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 6809:         foreach my $auth (@authtypes) {
 6810:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 6811:                 $authhash{$item}{$auth} = 1;
 6812:             } else {
 6813:                 $authhash{$item}{$auth} = 0;
 6814:             }
 6815:         }
 6816:     }
 6817:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 6818:         foreach my $item (@authen_contexts) {
 6819:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 6820:                 foreach my $auth (@authtypes) {
 6821:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 6822:                         push(@{$changes{'authtypes'}},$item);
 6823:                         last;
 6824:                     }
 6825:                 }
 6826:             }
 6827:         }
 6828:     } else {
 6829:         foreach my $item (@authen_contexts) {
 6830:             push(@{$changes{'authtypes'}},$item);
 6831:         }
 6832:     }
 6833: 
 6834:     my %usercreation_hash =  (
 6835:             usercreation => {
 6836:                               cancreate     => \%cancreate,
 6837:                               username_rule => \@username_rule,
 6838:                               id_rule       => \@id_rule,
 6839:                               email_rule    => \@email_rule,
 6840:                               authtypes     => \%authhash,
 6841:                             }
 6842:             );
 6843: 
 6844:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 6845:                                              $dom);
 6846: 
 6847:     my %selfcreatetypes = (
 6848:                              sso   => 'users authenticated by institutional single sign on',
 6849:                              login => 'users authenticated by institutional log-in',
 6850:                              email => 'users who provide a valid e-mail address for use as the username',
 6851:                           );
 6852:     if ($putresult eq 'ok') {
 6853:         if (keys(%changes) > 0) {
 6854:             $resulttext = &mt('Changes made:').'<ul>';
 6855:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 6856:                 my %lt = &usercreation_types();
 6857:                 foreach my $type (@{$changes{'cancreate'}}) {
 6858:                     my $chgtext;
 6859:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 6860:                         $chgtext = $lt{$type}.', ';
 6861:                     }
 6862:                     if ($type eq 'selfcreate') {
 6863:                         if (@{$cancreate{$type}} == 0) {
 6864:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 6865:                         } else {
 6866:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 6867:                             foreach my $case (@{$cancreate{$type}}) {
 6868:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 6869:                             }
 6870:                             $chgtext .= '</ul>';
 6871:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 6872:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 6873:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 6874:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 6875:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6876:                                         }
 6877:                                     }
 6878:                                 }
 6879:                             }
 6880:                         }
 6881:                     } elsif ($type eq 'statustocreate') {
 6882:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 6883:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 6884:                             if (@{$cancreate{'selfcreate'}} > 0) {
 6885:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 6886: 
 6887:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 6888:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 6889:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6890:                                     } 
 6891:                                 } elsif (ref($usertypes) eq 'HASH') {
 6892:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6893:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 6894:                                     } else {
 6895:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 6896:                                     }
 6897:                                     $chgtext .= '<ul>';
 6898:                                     foreach my $case (@{$cancreate{$type}}) {
 6899:                                         if ($case eq 'default') {
 6900:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 6901:                                         } else {
 6902:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 6903:                                         }
 6904:                                     }
 6905:                                     $chgtext .= '</ul>';
 6906:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6907:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 6908:                                     }
 6909:                                 }
 6910:                             } else {
 6911:                                 if (@{$cancreate{$type}} == 0) {
 6912:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 6913:                                 } else {
 6914:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 6915:                                 }
 6916:                             }
 6917:                         }
 6918:                     } elsif ($type eq 'captcha') {
 6919:                         if ($cancreate{$type} eq 'notused') {
 6920:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 6921:                         } else {
 6922:                             my %captchas = &captcha_phrases();
 6923:                             if ($captchas{$cancreate{$type}}) {
 6924:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 6925:                             } else {
 6926:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.'); 
 6927:                             }
 6928:                         }
 6929:                     } elsif ($type eq 'recaptchakeys') {
 6930:                         my ($privkey,$pubkey);
 6931:                         if (ref($cancreate{$type}) eq 'HASH') {
 6932:                             $pubkey = $cancreate{$type}{'public'};
 6933:                             $privkey = $cancreate{$type}{'private'};
 6934:                         }
 6935:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 6936:                         if (!$pubkey) {
 6937:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 6938:                         } else {
 6939:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6940:                         }
 6941:                         if (!$privkey) {
 6942:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 6943:                         } else {
 6944:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6945:                         }
 6946:                         $chgtext .= '</ul>';
 6947:                     } else {
 6948:                         if ($cancreate{$type} eq 'none') {
 6949:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 6950:                         } elsif ($cancreate{$type} eq 'any') {
 6951:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 6952:                         } elsif ($cancreate{$type} eq 'official') {
 6953:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 6954:                         } elsif ($cancreate{$type} eq 'unofficial') {
 6955:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 6956:                         }
 6957:                     }
 6958:                     $resulttext .= '<li>'.$chgtext.'</li>';
 6959:                 }
 6960:             }
 6961:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 6962:                 my ($rules,$ruleorder) = 
 6963:                     &Apache::lonnet::inst_userrules($dom,'username');
 6964:                 my $chgtext = '<ul>';
 6965:                 foreach my $type (@username_rule) {
 6966:                     if (ref($rules->{$type}) eq 'HASH') {
 6967:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 6968:                     }
 6969:                 }
 6970:                 $chgtext .= '</ul>';
 6971:                 if (@username_rule > 0) {
 6972:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 6973:                 } else {
 6974:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 6975:                 }
 6976:             }
 6977:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 6978:                 my ($idrules,$idruleorder) = 
 6979:                     &Apache::lonnet::inst_userrules($dom,'id');
 6980:                 my $chgtext = '<ul>';
 6981:                 foreach my $type (@id_rule) {
 6982:                     if (ref($idrules->{$type}) eq 'HASH') {
 6983:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 6984:                     }
 6985:                 }
 6986:                 $chgtext .= '</ul>';
 6987:                 if (@id_rule > 0) {
 6988:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 6989:                 } else {
 6990:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 6991:                 }
 6992:             }
 6993:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 6994:                 my ($emailrules,$emailruleorder) =
 6995:                     &Apache::lonnet::inst_userrules($dom,'email');
 6996:                 my $chgtext = '<ul>';
 6997:                 foreach my $type (@email_rule) {
 6998:                     if (ref($emailrules->{$type}) eq 'HASH') {
 6999:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 7000:                     }
 7001:                 }
 7002:                 $chgtext .= '</ul>';
 7003:                 if (@email_rule > 0) {
 7004:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 7005:                 } else {
 7006:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 7007:                 }
 7008:             }
 7009: 
 7010:             my %authname = &authtype_names();
 7011:             my %context_title = &context_names();
 7012:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7013:                 my $chgtext = '<ul>';
 7014:                 foreach my $type (@{$changes{'authtypes'}}) {
 7015:                     my @allowed;
 7016:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7017:                     foreach my $auth (@authtypes) {
 7018:                         if ($authhash{$type}{$auth}) {
 7019:                             push(@allowed,$authname{$auth});
 7020:                         }
 7021:                     }
 7022:                     if (@allowed > 0) {
 7023:                         $chgtext .= join(', ',@allowed).'</li>';
 7024:                     } else {
 7025:                         $chgtext .= &mt('none').'</li>';
 7026:                     }
 7027:                 }
 7028:                 $chgtext .= '</ul>';
 7029:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7030:                 $resulttext .= '</li>';
 7031:             }
 7032:             $resulttext .= '</ul>';
 7033:         } else {
 7034:             $resulttext = &mt('No changes made to user creation settings');
 7035:         }
 7036:     } else {
 7037:         $resulttext = '<span class="LC_error">'.
 7038:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7039:     }
 7040:     if ($warningmsg ne '') {
 7041:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7042:     }
 7043:     return $resulttext;
 7044: }
 7045: 
 7046: sub process_captcha {
 7047:     my ($container,$changes,$newsettings,$current) = @_;
 7048:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 7049:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 7050:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 7051:         $newsettings->{'captcha'} = 'original';
 7052:     }
 7053:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 7054:         if ($container eq 'cancreate') { 
 7055:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7056:                 push(@{$changes->{'cancreate'}},'captcha');
 7057:             } elsif (!defined($changes->{'cancreate'})) {
 7058:                 $changes->{'cancreate'} = ['captcha'];
 7059:             }
 7060:         } else {
 7061:             $changes->{'captcha'} = 1;
 7062:         }
 7063:     }
 7064:     my ($newpub,$newpriv,$currpub,$currpriv);
 7065:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 7066:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 7067:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 7068:         $newpub =~ s/\W//g;
 7069:         $newpriv =~ s/\W//g;
 7070:         $newsettings->{'recaptchakeys'} = {
 7071:                                              public  => $newpub,
 7072:                                              private => $newpriv,
 7073:                                           };
 7074:     }
 7075:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 7076:         $currpub = $current->{'recaptchakeys'}{'public'};
 7077:         $currpriv = $current->{'recaptchakeys'}{'private'};
 7078:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 7079:             $newsettings->{'recaptchakeys'} = {
 7080:                                                  public  => '',
 7081:                                                  private => '',
 7082:                                               }
 7083:         }
 7084:     }
 7085:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 7086:         if ($container eq 'cancreate') {
 7087:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7088:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 7089:             } elsif (!defined($changes->{'cancreate'})) {
 7090:                 $changes->{'cancreate'} = ['recaptchakeys'];
 7091:             }
 7092:         } else {
 7093:             $changes->{'recaptchakeys'} = 1; 
 7094:         }
 7095:     }
 7096:     return;
 7097: }
 7098: 
 7099: sub modify_usermodification {
 7100:     my ($dom,%domconfig) = @_;
 7101:     my ($resulttext,%curr_usermodification,%changes);
 7102:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7103:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7104:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 7105:         }
 7106:     }
 7107:     my @contexts = ('author','course','selfcreate');
 7108:     my %context_title = (
 7109:                            author => 'In author context',
 7110:                            course => 'In course context',
 7111:                            selfcreate => 'When self creating account', 
 7112:                         );
 7113:     my @fields = ('lastname','firstname','middlename','generation',
 7114:                   'permanentemail','id');
 7115:     my %roles = (
 7116:                   author => ['ca','aa'],
 7117:                   course => ['st','ep','ta','in','cr'],
 7118:                 );
 7119:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7120:     if (ref($types) eq 'ARRAY') {
 7121:         push(@{$types},'default');
 7122:         $usertypes->{'default'} = $othertitle;
 7123:     }
 7124:     $roles{'selfcreate'} = $types;  
 7125:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7126:     my %modifyhash;
 7127:     foreach my $context (@contexts) {
 7128:         foreach my $role (@{$roles{$context}}) {
 7129:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7130:             foreach my $item (@fields) {
 7131:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7132:                     $modifyhash{$context}{$role}{$item} = 1;
 7133:                 } else {
 7134:                     $modifyhash{$context}{$role}{$item} = 0;
 7135:                 }
 7136:             }
 7137:         }
 7138:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7139:             foreach my $role (@{$roles{$context}}) {
 7140:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7141:                     foreach my $field (@fields) {
 7142:                         if ($modifyhash{$context}{$role}{$field} ne 
 7143:                                 $curr_usermodification{$context}{$role}{$field}) {
 7144:                             push(@{$changes{$context}},$role);
 7145:                             last;
 7146:                         }
 7147:                     }
 7148:                 }
 7149:             }
 7150:         } else {
 7151:             foreach my $context (@contexts) {
 7152:                 foreach my $role (@{$roles{$context}}) {
 7153:                     push(@{$changes{$context}},$role);
 7154:                 }
 7155:             }
 7156:         }
 7157:     }
 7158:     my %usermodification_hash =  (
 7159:                                    usermodification => \%modifyhash,
 7160:                                  );
 7161:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7162:                                              \%usermodification_hash,$dom);
 7163:     if ($putresult eq 'ok') {
 7164:         if (keys(%changes) > 0) {
 7165:             $resulttext = &mt('Changes made: ').'<ul>';
 7166:             foreach my $context (@contexts) {
 7167:                 if (ref($changes{$context}) eq 'ARRAY') {
 7168:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7169:                     if (ref($changes{$context}) eq 'ARRAY') {
 7170:                         foreach my $role (@{$changes{$context}}) {
 7171:                             my $rolename;
 7172:                             if ($context eq 'selfcreate') {
 7173:                                 $rolename = $role;
 7174:                                 if (ref($usertypes) eq 'HASH') {
 7175:                                     if ($usertypes->{$role} ne '') {
 7176:                                         $rolename = $usertypes->{$role};
 7177:                                     }
 7178:                                 }
 7179:                             } else {
 7180:                                 if ($role eq 'cr') {
 7181:                                     $rolename = &mt('Custom');
 7182:                                 } else {
 7183:                                     $rolename = &Apache::lonnet::plaintext($role);
 7184:                                 }
 7185:                             }
 7186:                             my @modifiable;
 7187:                             if ($context eq 'selfcreate') {
 7188:                                 $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): ');
 7189:                             } else {
 7190:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7191:                             }
 7192:                             foreach my $field (@fields) {
 7193:                                 if ($modifyhash{$context}{$role}{$field}) {
 7194:                                     push(@modifiable,$fieldtitles{$field});
 7195:                                 }
 7196:                             }
 7197:                             if (@modifiable > 0) {
 7198:                                 $resulttext .= join(', ',@modifiable);
 7199:                             } else {
 7200:                                 $resulttext .= &mt('none'); 
 7201:                             }
 7202:                             $resulttext .= '</li>';
 7203:                         }
 7204:                         $resulttext .= '</ul></li>';
 7205:                     }
 7206:                 }
 7207:             }
 7208:             $resulttext .= '</ul>';
 7209:         } else {
 7210:             $resulttext = &mt('No changes made to user modification settings');
 7211:         }
 7212:     } else {
 7213:         $resulttext = '<span class="LC_error">'.
 7214:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7215:     }
 7216:     return $resulttext;
 7217: }
 7218: 
 7219: sub modify_defaults {
 7220:     my ($dom,$r) = @_;
 7221:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7222:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7223:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7224:     my @authtypes = ('internal','krb4','krb5','localauth');
 7225:     foreach my $item (@items) {
 7226:         $newvalues{$item} = $env{'form.'.$item};
 7227:         if ($item eq 'auth_def') {
 7228:             if ($newvalues{$item} ne '') {
 7229:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7230:                     push(@errors,$item);
 7231:                 }
 7232:             }
 7233:         } elsif ($item eq 'lang_def') {
 7234:             if ($newvalues{$item} ne '') {
 7235:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7236:                     my $langcode = $1;
 7237:                     if ($langcode ne 'x_chef') {
 7238:                         if (code2language($langcode) eq '') {
 7239:                             push(@errors,$item);
 7240:                         }
 7241:                     }
 7242:                 } else {
 7243:                     push(@errors,$item);
 7244:                 }
 7245:             }
 7246:         } elsif ($item eq 'timezone_def') {
 7247:             if ($newvalues{$item} ne '') {
 7248:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7249:                     push(@errors,$item);   
 7250:                 }
 7251:             }
 7252:         } elsif ($item eq 'datelocale_def') {
 7253:             if ($newvalues{$item} ne '') {
 7254:                 my @datelocale_ids = DateTime::Locale->ids();
 7255:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7256:                     push(@errors,$item);
 7257:                 }
 7258:             }
 7259:         } elsif ($item eq 'portal_def') {
 7260:             if ($newvalues{$item} ne '') {
 7261:                 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])\/?$/) {
 7262:                     push(@errors,$item);
 7263:                 }
 7264:             }
 7265:         }
 7266:         if (grep(/^\Q$item\E$/,@errors)) {
 7267:             $newvalues{$item} = $domdefaults{$item};
 7268:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7269:             $changes{$item} = 1;
 7270:         }
 7271:         $domdefaults{$item} = $newvalues{$item};
 7272:     }
 7273:     my %defaults_hash = (
 7274:                          defaults => \%newvalues,
 7275:                         );
 7276:     my $title = &defaults_titles();
 7277:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7278:                                              $dom);
 7279:     if ($putresult eq 'ok') {
 7280:         if (keys(%changes) > 0) {
 7281:             $resulttext = &mt('Changes made:').'<ul>';
 7282:             my $version = $r->dir_config('lonVersion');
 7283:             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";
 7284:             foreach my $item (sort(keys(%changes))) {
 7285:                 my $value = $env{'form.'.$item};
 7286:                 if ($value eq '') {
 7287:                     $value = &mt('none');
 7288:                 } elsif ($item eq 'auth_def') {
 7289:                     my %authnames = &authtype_names();
 7290:                     my %shortauth = (
 7291:                              internal => 'int',
 7292:                              krb4 => 'krb4',
 7293:                              krb5 => 'krb5',
 7294:                              localauth  => 'loc',
 7295:                     );
 7296:                     $value = $authnames{$shortauth{$value}};
 7297:                 }
 7298:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7299:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7300:             }
 7301:             $resulttext .= '</ul>';
 7302:             $mailmsgtext .= "\n";
 7303:             my $cachetime = 24*60*60;
 7304:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7305:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7306:                 my $sysmail = $r->dir_config('lonSysEMail');
 7307:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 7308:             }
 7309:         } else {
 7310:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 7311:         }
 7312:     } else {
 7313:         $resulttext = '<span class="LC_error">'.
 7314:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7315:     }
 7316:     if (@errors > 0) {
 7317:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7318:         foreach my $item (@errors) {
 7319:             $resulttext .= ' "'.$title->{$item}.'",';
 7320:         }
 7321:         $resulttext =~ s/,$//;
 7322:     }
 7323:     return $resulttext;
 7324: }
 7325: 
 7326: sub modify_scantron {
 7327:     my ($r,$dom,$confname,%domconfig) = @_;
 7328:     my ($resulttext,%confhash,%changes,$errors);
 7329:     my $custom = 'custom.tab';
 7330:     my $default = 'default.tab';
 7331:     my $servadm = $r->dir_config('lonAdmEMail');
 7332:     my ($configuserok,$author_ok,$switchserver) = 
 7333:         &config_check($dom,$confname,$servadm);
 7334:     if ($env{'form.scantronformat.filename'} ne '') {
 7335:         my $error;
 7336:         if ($configuserok eq 'ok') {
 7337:             if ($switchserver) {
 7338:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7339:             } else {
 7340:                 if ($author_ok eq 'ok') {
 7341:                     my ($result,$scantronurl) =
 7342:                         &publishlogo($r,'upload','scantronformat',$dom,
 7343:                                      $confname,'scantron','','',$custom);
 7344:                     if ($result eq 'ok') {
 7345:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7346:                         $changes{'scantronformat'} = 1;
 7347:                     } else {
 7348:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7349:                     }
 7350:                 } else {
 7351:                     $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);
 7352:                 }
 7353:             }
 7354:         } else {
 7355:             $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);
 7356:         }
 7357:         if ($error) {
 7358:             &Apache::lonnet::logthis($error);
 7359:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7360:         }
 7361:     }
 7362:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7363:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7364:             if ($env{'form.scantronformat_del'}) {
 7365:                 $confhash{'scantron'}{'scantronformat'} = '';
 7366:                 $changes{'scantronformat'} = 1;
 7367:             }
 7368:         }
 7369:     }
 7370:     if (keys(%confhash) > 0) {
 7371:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7372:                                                  $dom);
 7373:         if ($putresult eq 'ok') {
 7374:             if (keys(%changes) > 0) {
 7375:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7376:                     $resulttext = &mt('Changes made:').'<ul>';
 7377:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7378:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7379:                     } else {
 7380:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7381:                     }
 7382:                     $resulttext .= '</ul>';
 7383:                 } else {
 7384:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7385:                 }
 7386:                 $resulttext .= '</ul>';
 7387:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7388:             } else {
 7389:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7390:             }
 7391:         } else {
 7392:             $resulttext = '<span class="LC_error">'.
 7393:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7394:         }
 7395:     } else {
 7396:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7397:     }
 7398:     if ($errors) {
 7399:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7400:                        $errors.'</ul>';
 7401:     }
 7402:     return $resulttext;
 7403: }
 7404: 
 7405: sub modify_coursecategories {
 7406:     my ($dom,%domconfig) = @_;
 7407:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7408:         $cathash);
 7409:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7410:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7411:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7412:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7413:             $changes{'togglecats'} = 1;
 7414:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7415:         }
 7416:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7417:             $changes{'categorize'} = 1;
 7418:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7419:         }
 7420:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7421:             $changes{'togglecatscomm'} = 1;
 7422:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7423:         }
 7424:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7425:             $changes{'categorizecomm'} = 1;
 7426:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7427:         }
 7428:     } else {
 7429:         $changes{'togglecats'} = 1;
 7430:         $changes{'categorize'} = 1;
 7431:         $changes{'togglecatscomm'} = 1;
 7432:         $changes{'categorizecomm'} = 1;
 7433:         $domconfig{'coursecategories'} = {
 7434:                                              togglecats => $env{'form.togglecats'},
 7435:                                              categorize => $env{'form.categorize'},
 7436:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7437:                                              categorizecomm => $env{'form.categorizecomm'},
 7438:                                          };
 7439:     }
 7440:     if (ref($cathash) eq 'HASH') {
 7441:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7442:             push (@deletecategory,'instcode::0');
 7443:         }
 7444:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7445:             push(@deletecategory,'communities::0');
 7446:         }
 7447:     }
 7448:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7449:     if (ref($cathash) eq 'HASH') {
 7450:         if (@deletecategory > 0) {
 7451:             #FIXME Need to remove category from all courses using a deleted category 
 7452:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7453:             foreach my $item (@deletecategory) {
 7454:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7455:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7456:                     $deletions{$item} = 1;
 7457:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7458:                 }
 7459:             }
 7460:         }
 7461:         foreach my $item (keys(%{$cathash})) {
 7462:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7463:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7464:                 $reorderings{$item} = 1;
 7465:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7466:             }
 7467:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7468:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7469:                 my $newdepth = $depth+1;
 7470:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7471:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7472:                 $adds{$newitem} = 1; 
 7473:             }
 7474:             if ($env{'form.subcat_'.$item} ne '') {
 7475:                 my $newcat = $env{'form.subcat_'.$item};
 7476:                 my $newdepth = $depth+1;
 7477:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7478:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7479:                 $adds{$newitem} = 1;
 7480:             }
 7481:         }
 7482:     }
 7483:     if ($env{'form.instcode'} eq '1') {
 7484:         if (ref($cathash) eq 'HASH') {
 7485:             my $newitem = 'instcode::0';
 7486:             if ($cathash->{$newitem} eq '') {  
 7487:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7488:                 $adds{$newitem} = 1;
 7489:             }
 7490:         } else {
 7491:             my $newitem = 'instcode::0';
 7492:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7493:             $adds{$newitem} = 1;
 7494:         }
 7495:     }
 7496:     if ($env{'form.communities'} eq '1') {
 7497:         if (ref($cathash) eq 'HASH') {
 7498:             my $newitem = 'communities::0';
 7499:             if ($cathash->{$newitem} eq '') {
 7500:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7501:                 $adds{$newitem} = 1;
 7502:             }
 7503:         } else {
 7504:             my $newitem = 'communities::0';
 7505:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7506:             $adds{$newitem} = 1;
 7507:         }
 7508:     }
 7509:     if ($env{'form.addcategory_name'} ne '') {
 7510:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7511:             ($env{'form.addcategory_name'} ne 'communities')) {
 7512:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7513:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7514:             $adds{$newitem} = 1;
 7515:         }
 7516:     }
 7517:     my $putresult;
 7518:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7519:         if (keys(%deletions) > 0) {
 7520:             foreach my $key (keys(%deletions)) {
 7521:                 if ($predelallitems{$key} ne '') {
 7522:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7523:                 }
 7524:             }
 7525:         }
 7526:         my (@chkcats,@chktrails,%chkallitems);
 7527:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7528:         if (ref($chkcats[0]) eq 'ARRAY') {
 7529:             my $depth = 0;
 7530:             my $chg = 0;
 7531:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7532:                 my $name = $chkcats[0][$i];
 7533:                 my $item;
 7534:                 if ($name eq '') {
 7535:                     $chg ++;
 7536:                 } else {
 7537:                     $item = &escape($name).'::0';
 7538:                     if ($chg) {
 7539:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7540:                     }
 7541:                     $depth ++; 
 7542:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7543:                     $depth --;
 7544:                 }
 7545:             }
 7546:         }
 7547:     }
 7548:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7549:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7550:         if ($putresult eq 'ok') {
 7551:             my %title = (
 7552:                          togglecats     => 'Show/Hide a course in catalog',
 7553:                          categorize     => 'Assign a category to a course',
 7554:                          togglecatscomm => 'Show/Hide a community in catalog',
 7555:                          categorizecomm => 'Assign a category to a community',
 7556:                         );
 7557:             my %level = (
 7558:                          dom  => 'set in Domain ("Modify Course/Community")',
 7559:                          crs  => 'set in Course ("Course Configuration")',
 7560:                          comm => 'set in Community ("Community Configuration")',
 7561:                         );
 7562:             $resulttext = &mt('Changes made:').'<ul>';
 7563:             if ($changes{'togglecats'}) {
 7564:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7565:             }
 7566:             if ($changes{'categorize'}) {
 7567:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7568:             }
 7569:             if ($changes{'togglecatscomm'}) {
 7570:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7571:             }
 7572:             if ($changes{'categorizecomm'}) {
 7573:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7574:             }
 7575:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7576:                 my $cathash;
 7577:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7578:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7579:                 } else {
 7580:                     $cathash = {};
 7581:                 } 
 7582:                 my (@cats,@trails,%allitems);
 7583:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7584:                 if (keys(%deletions) > 0) {
 7585:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7586:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7587:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7588:                     }
 7589:                     $resulttext .= '</ul></li>';
 7590:                 }
 7591:                 if (keys(%reorderings) > 0) {
 7592:                     my %sort_by_trail;
 7593:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7594:                     foreach my $key (keys(%reorderings)) {
 7595:                         if ($allitems{$key} ne '') {
 7596:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7597:                         }
 7598:                     }
 7599:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7600:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7601:                     }
 7602:                     $resulttext .= '</ul></li>';
 7603:                 }
 7604:                 if (keys(%adds) > 0) {
 7605:                     my %sort_by_trail;
 7606:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7607:                     foreach my $key (keys(%adds)) {
 7608:                         if ($allitems{$key} ne '') {
 7609:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7610:                         }
 7611:                     }
 7612:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7613:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7614:                     }
 7615:                     $resulttext .= '</ul></li>';
 7616:                 }
 7617:             }
 7618:             $resulttext .= '</ul>';
 7619:         } else {
 7620:             $resulttext = '<span class="LC_error">'.
 7621:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7622:         }
 7623:     } else {
 7624:         $resulttext = &mt('No changes made to course and community categories');
 7625:     }
 7626:     return $resulttext;
 7627: }
 7628: 
 7629: sub modify_serverstatuses {
 7630:     my ($dom,%domconfig) = @_;
 7631:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7632:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7633:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7634:     }
 7635:     my @pages = &serverstatus_pages();
 7636:     foreach my $type (@pages) {
 7637:         $newserverstatus{$type}{'namedusers'} = '';
 7638:         $newserverstatus{$type}{'machines'} = '';
 7639:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7640:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7641:             my @okusers;
 7642:             foreach my $user (@users) {
 7643:                 my ($uname,$udom) = split(/:/,$user);
 7644:                 if (($udom =~ /^$match_domain$/) &&   
 7645:                     (&Apache::lonnet::domain($udom)) &&
 7646:                     ($uname =~ /^$match_username$/)) {
 7647:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7648:                         push(@okusers,$user);
 7649:                     }
 7650:                 }
 7651:             }
 7652:             if (@okusers > 0) {
 7653:                  @okusers = sort(@okusers);
 7654:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7655:             }
 7656:         }
 7657:         if (defined($env{'form.'.$type.'_machines'})) {
 7658:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7659:             my @okmachines;
 7660:             foreach my $ip (@machines) {
 7661:                 my @parts = split(/\./,$ip);
 7662:                 next if (@parts < 4);
 7663:                 my $badip = 0;
 7664:                 for (my $i=0; $i<4; $i++) {
 7665:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7666:                         $badip = 1;
 7667:                         last;
 7668:                     }
 7669:                 }
 7670:                 if (!$badip) {
 7671:                     push(@okmachines,$ip);     
 7672:                 }
 7673:             }
 7674:             @okmachines = sort(@okmachines);
 7675:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7676:         }
 7677:     }
 7678:     my %serverstatushash =  (
 7679:                                 serverstatuses => \%newserverstatus,
 7680:                             );
 7681:     foreach my $type (@pages) {
 7682:         foreach my $setting ('namedusers','machines') {
 7683:             my (@current,@new);
 7684:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7685:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7686:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7687:                 }
 7688:             }
 7689:             if ($newserverstatus{$type}{$setting} ne '') {
 7690:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7691:             }
 7692:             if (@current > 0) {
 7693:                 if (@new > 0) {
 7694:                     foreach my $item (@current) {
 7695:                         if (!grep(/^\Q$item\E$/,@new)) {
 7696:                             $changes{$type}{$setting} = 1;
 7697:                             last;
 7698:                         }
 7699:                     }
 7700:                     foreach my $item (@new) {
 7701:                         if (!grep(/^\Q$item\E$/,@current)) {
 7702:                             $changes{$type}{$setting} = 1;
 7703:                             last;
 7704:                         }
 7705:                     }
 7706:                 } else {
 7707:                     $changes{$type}{$setting} = 1;
 7708:                 }
 7709:             } elsif (@new > 0) {
 7710:                 $changes{$type}{$setting} = 1;
 7711:             }
 7712:         }
 7713:     }
 7714:     if (keys(%changes) > 0) {
 7715:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7716:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7717:                                                  \%serverstatushash,$dom);
 7718:         if ($putresult eq 'ok') {
 7719:             $resulttext .= &mt('Changes made:').'<ul>';
 7720:             foreach my $type (@pages) {
 7721:                 if (ref($changes{$type}) eq 'HASH') {
 7722:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7723:                     if ($changes{$type}{'namedusers'}) {
 7724:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7725:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7726:                         } else {
 7727:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7728:                         }
 7729:                     }
 7730:                     if ($changes{$type}{'machines'}) {
 7731:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7732:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7733:                         } else {
 7734:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7735:                         }
 7736: 
 7737:                     }
 7738:                     $resulttext .= '</ul></li>';
 7739:                 }
 7740:             }
 7741:             $resulttext .= '</ul>';
 7742:         } else {
 7743:             $resulttext = '<span class="LC_error">'.
 7744:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7745: 
 7746:         }
 7747:     } else {
 7748:         $resulttext = &mt('No changes made to access to server status pages');
 7749:     }
 7750:     return $resulttext;
 7751: }
 7752: 
 7753: sub modify_helpsettings {
 7754:     my ($r,$dom,$confname,%domconfig) = @_;
 7755:     my ($resulttext,$errors,%changes,%helphash);
 7756:     my %defaultchecked = ('submitbugs' => 'on');
 7757:     my @offon = ('off','on');
 7758:     my @toggles = ('submitbugs');
 7759:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7760:         foreach my $item (@toggles) {
 7761:             if ($defaultchecked{$item} eq 'on') { 
 7762:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7763:                     if ($env{'form.'.$item} eq '0') {
 7764:                         $changes{$item} = 1;
 7765:                     }
 7766:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7767:                     $changes{$item} = 1;
 7768:                 }
 7769:             } elsif ($defaultchecked{$item} eq 'off') {
 7770:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7771:                     if ($env{'form.'.$item} eq '1') {
 7772:                         $changes{$item} = 1;
 7773:                     }
 7774:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7775:                     $changes{$item} = 1;
 7776:                 }
 7777:             }
 7778:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) { 
 7779:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7780:             }
 7781:         }
 7782:     }
 7783:     my $putresult;
 7784:     if (keys(%changes) > 0) {
 7785:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7786:         if ($putresult eq 'ok') {
 7787:             $resulttext = &mt('Changes made:').'<ul>';
 7788:             foreach my $item (sort(keys(%changes))) {
 7789:                 if ($item eq 'submitbugs') {
 7790:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 7791:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 7792:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 7793:                 }
 7794:             }
 7795:             $resulttext .= '</ul>';
 7796:         } else {
 7797:             $resulttext = &mt('No changes made to help settings');
 7798:             $errors .= '<li><span class="LC_error">'.
 7799:                        &mt('An error occurred storing the settings: [_1]',
 7800:                            $putresult).'</span></li>';
 7801:         }
 7802:     }
 7803:     if ($errors) {
 7804:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7805:                        $errors.'</ul>';
 7806:     }
 7807:     return $resulttext;
 7808: }
 7809: 
 7810: sub modify_coursedefaults {
 7811:     my ($dom,%domconfig) = @_;
 7812:     my ($resulttext,$errors,%changes,%defaultshash);
 7813:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7814:     my @offon = ('off','on');
 7815:     my @toggles = ('canuse_pdfforms');
 7816: 
 7817:     $defaultshash{'coursedefaults'} = {};
 7818: 
 7819:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7820:         if ($domconfig{'coursedefaults'} eq '') {
 7821:             $domconfig{'coursedefaults'} = {};
 7822:         }
 7823:     }
 7824: 
 7825:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7826:         foreach my $item (@toggles) {
 7827:             if ($defaultchecked{$item} eq 'on') {
 7828:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7829:                     ($env{'form.'.$item} eq '0')) {
 7830:                     $changes{$item} = 1;
 7831:                 } elsif ($domconfig{'coursdefaults'}{$item} ne $env{'form.'.$item}) {
 7832:                     $changes{$item} = 1;
 7833:                 }
 7834:             } elsif ($defaultchecked{$item} eq 'off') {
 7835:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7836:                     ($env{'form.'.$item} eq '1')) {
 7837:                     $changes{$item} = 1;
 7838:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7839:                     $changes{$item} = 1;
 7840:                 }
 7841:             }
 7842:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7843:         }
 7844:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7845:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7846:         $newdefresponder =~ s/\D//g;
 7847:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7848:             $newdefresponder = 1;
 7849:         }
 7850:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7851:         if ($currdefresponder ne $newdefresponder) {
 7852:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7853:                 $changes{'anonsurvey_threshold'} = 1;
 7854:             }
 7855:         }
 7856:     }
 7857:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7858:                                              $dom);
 7859:     if ($putresult eq 'ok') {
 7860:         if (keys(%changes) > 0) {
 7861:             if ($changes{'canuse_pdfforms'}) {
 7862:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7863:                 $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7864:                 my $cachetime = 24*60*60;
 7865:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7866:             }
 7867:             $resulttext = &mt('Changes made:').'<ul>';
 7868:             foreach my $item (sort(keys(%changes))) {
 7869:                 if ($item eq 'canuse_pdfforms') {
 7870:                     if ($env{'form.'.$item} eq '1') {
 7871:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7872:                     } else {
 7873:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7874:                     }
 7875:                 } elsif ($item eq 'anonsurvey_threshold') {
 7876:                         $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7877:                 }
 7878:             }
 7879:             $resulttext .= '</ul>';
 7880:         } else {
 7881:             $resulttext = &mt('No changes made to course defaults');
 7882:         }
 7883:     } else {
 7884:         $resulttext = '<span class="LC_error">'.
 7885:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7886:     }
 7887:     return $resulttext;
 7888: }
 7889: 
 7890: sub modify_usersessions {
 7891:     my ($dom,%domconfig) = @_;
 7892:     my @hostingtypes = ('version','excludedomain','includedomain');
 7893:     my @offloadtypes = ('primary','default');
 7894:     my %types = (
 7895:                   remote => \@hostingtypes,
 7896:                   hosted => \@hostingtypes,
 7897:                   spares => \@offloadtypes,
 7898:                 );
 7899:     my @prefixes = ('remote','hosted','spares');
 7900:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7901:     my (%by_ip,%by_location,@intdoms);
 7902:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7903:     my @locations = sort(keys(%by_location));
 7904:     my (%defaultshash,%changes);
 7905:     foreach my $prefix (@prefixes) {
 7906:         $defaultshash{'usersessions'}{$prefix} = {};
 7907:     }
 7908:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7909:     my $resulttext;
 7910:     my %iphost = &Apache::lonnet::get_iphost();
 7911:     foreach my $prefix (@prefixes) {
 7912:         next if ($prefix eq 'spares');
 7913:         foreach my $type (@{$types{$prefix}}) {
 7914:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7915:             if ($type eq 'version') {
 7916:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7917:                 my $okvalue;
 7918:                 if ($value ne '') {
 7919:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 7920:                         $okvalue = $value;
 7921:                     }
 7922:                 }
 7923:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7924:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7925:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 7926:                             if ($inuse == 0) {
 7927:                                 $changes{$prefix}{$type} = 1;
 7928:                             } else {
 7929:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 7930:                                     $changes{$prefix}{$type} = 1;
 7931:                                 }
 7932:                                 if ($okvalue ne '') {
 7933:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7934:                                 } 
 7935:                             }
 7936:                         } else {
 7937:                             if (($inuse == 1) && ($okvalue ne '')) {
 7938:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7939:                                 $changes{$prefix}{$type} = 1;
 7940:                             }
 7941:                         }
 7942:                     } else {
 7943:                         if (($inuse == 1) && ($okvalue ne '')) {
 7944:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7945:                             $changes{$prefix}{$type} = 1;
 7946:                         }
 7947:                     }
 7948:                 } else {
 7949:                     if (($inuse == 1) && ($okvalue ne '')) {
 7950:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7951:                         $changes{$prefix}{$type} = 1;
 7952:                     }
 7953:                 }
 7954:             } else {
 7955:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 7956:                 my @okvals;
 7957:                 foreach my $val (@vals) {
 7958:                     if ($val =~ /:/) {
 7959:                         my @items = split(/:/,$val);
 7960:                         foreach my $item (@items) {
 7961:                             if (ref($by_location{$item}) eq 'ARRAY') {
 7962:                                 push(@okvals,$item);
 7963:                             }
 7964:                         }
 7965:                     } else {
 7966:                         if (ref($by_location{$val}) eq 'ARRAY') {
 7967:                             push(@okvals,$val);
 7968:                         }
 7969:                     }
 7970:                 }
 7971:                 @okvals = sort(@okvals);
 7972:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7973:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7974:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7975:                             if ($inuse == 0) {
 7976:                                 $changes{$prefix}{$type} = 1; 
 7977:                             } else {
 7978:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7979:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 7980:                                 if (@changed > 0) {
 7981:                                     $changes{$prefix}{$type} = 1;
 7982:                                 }
 7983:                             }
 7984:                         } else {
 7985:                             if ($inuse == 1) {
 7986:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7987:                                 $changes{$prefix}{$type} = 1;
 7988:                             }
 7989:                         } 
 7990:                     } else {
 7991:                         if ($inuse == 1) {
 7992:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7993:                             $changes{$prefix}{$type} = 1;
 7994:                         }
 7995:                     }
 7996:                 } else {
 7997:                     if ($inuse == 1) {
 7998:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7999:                         $changes{$prefix}{$type} = 1;
 8000:                     }
 8001:                 }
 8002:             }
 8003:         }
 8004:     }
 8005: 
 8006:     my @alldoms = &Apache::lonnet::all_domains();
 8007:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8008:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 8009:     my $savespares;
 8010: 
 8011:     foreach my $lonhost (sort(keys(%servers))) {
 8012:         my $serverhomeID =
 8013:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 8014:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 8015:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 8016:         my %spareschg;
 8017:         foreach my $type (@{$types{'spares'}}) {
 8018:             my @okspares;
 8019:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 8020:             foreach my $server (@checked) {
 8021:                 if (&Apache::lonnet::hostname($server) ne '') {
 8022:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 8023:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 8024:                             push(@okspares,$server);
 8025:                         }
 8026:                     }
 8027:                 }
 8028:             }
 8029:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 8030:             my $newspare;
 8031:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 8032:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 8033:                     $newspare = $new;
 8034:                 }
 8035:             }
 8036:             my @spares;
 8037:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 8038:                 @spares = sort(@okspares,$newspare);
 8039:             } else {
 8040:                 @spares = sort(@okspares);
 8041:             }
 8042:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 8043:             if (ref($spareid{$lonhost}) eq 'HASH') {
 8044:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 8045:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 8046:                     if (@diffs > 0) {
 8047:                         $spareschg{$type} = 1;
 8048:                     }
 8049:                 }
 8050:             }
 8051:         }
 8052:         if (keys(%spareschg) > 0) {
 8053:             $changes{'spares'}{$lonhost} = \%spareschg;
 8054:         }
 8055:     }
 8056: 
 8057:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8058:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 8059:             if (ref($changes{'spares'}) eq 'HASH') {
 8060:                 if (keys(%{$changes{'spares'}}) > 0) {
 8061:                     $savespares = 1;
 8062:                 }
 8063:             }
 8064:         } else {
 8065:             $savespares = 1;
 8066:         }
 8067:     }
 8068: 
 8069:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 8070:     if ((keys(%changes) > 0) || ($savespares)) {
 8071:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8072:                                                  $dom);
 8073:         if ($putresult eq 'ok') {
 8074:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8075:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 8076:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 8077:                 }
 8078:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 8079:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 8080:                 }
 8081:             }
 8082:             my $cachetime = 24*60*60;
 8083:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8084:             if (keys(%changes) > 0) {
 8085:                 my %lt = &usersession_titles();
 8086:                 $resulttext = &mt('Changes made:').'<ul>';
 8087:                 foreach my $prefix (@prefixes) {
 8088:                     if (ref($changes{$prefix}) eq 'HASH') {
 8089:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 8090:                         if ($prefix eq 'spares') {
 8091:                             if (ref($changes{$prefix}) eq 'HASH') {
 8092:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 8093:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 8094:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 8095:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 8096:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 8097:                                         foreach my $type (@{$types{$prefix}}) {
 8098:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 8099:                                                 my $offloadto = &mt('None');
 8100:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 8101:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 8102:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 8103:                                                     }
 8104:                                                 }
 8105:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 8106:                                             }
 8107:                                         }
 8108:                                     }
 8109:                                     $resulttext .= '</li>';
 8110:                                 }
 8111:                             }
 8112:                         } else {
 8113:                             foreach my $type (@{$types{$prefix}}) {
 8114:                                 if (defined($changes{$prefix}{$type})) {
 8115:                                     my $newvalue;
 8116:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8117:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 8118:                                             if ($type eq 'version') {
 8119:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 8120:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8121:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 8122:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 8123:                                                 }
 8124:                                             }
 8125:                                         }
 8126:                                     }
 8127:                                     if ($newvalue eq '') {
 8128:                                         if ($type eq 'version') {
 8129:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8130:                                         } else {
 8131:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8132:                                         }
 8133:                                     } else {
 8134:                                         if ($type eq 'version') {
 8135:                                             $newvalue .= ' '.&mt('(or later)'); 
 8136:                                         }
 8137:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8138:                                     }
 8139:                                 }
 8140:                             }
 8141:                         }
 8142:                         $resulttext .= '</ul>';
 8143:                     }
 8144:                 }
 8145:                 $resulttext .= '</ul>';
 8146:             } else {
 8147:                 $resulttext = $nochgmsg;
 8148:             }
 8149:         } else {
 8150:             $resulttext = '<span class="LC_error">'.
 8151:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8152:         }
 8153:     } else {
 8154:         $resulttext = $nochgmsg;
 8155:     }
 8156:     return $resulttext;
 8157: }
 8158: 
 8159: sub modify_loadbalancing {
 8160:     my ($dom,%domconfig) = @_;
 8161:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8162:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8163:     my ($othertitle,$usertypes,$types) =
 8164:         &Apache::loncommon::sorted_inst_types($dom);
 8165:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8166:     my @sparestypes = ('primary','default');
 8167:     my %typetitles = &sparestype_titles();
 8168:     my $resulttext;
 8169:     my (%currbalancer,%currtargets,%currrules,%existing);
 8170:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8171:         %existing = %{$domconfig{'loadbalancing'}};
 8172:     }
 8173:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8174:                               \%currtargets,\%currrules);
 8175:     my ($saveloadbalancing,%defaultshash,%changes);
 8176:     my ($alltypes,$othertypes,$titles) =
 8177:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8178:     my %ruletitles = &offloadtype_text();
 8179:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 8180:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 8181:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 8182:         if ($balancer eq '') {
 8183:             next;
 8184:         }
 8185:         if (!exists($servers{$balancer})) { 
 8186:             if (exists($currbalancer{$balancer})) {
 8187:                 push(@{$changes{'delete'}},$balancer);
 8188:             }
 8189:             next;
 8190:         }
 8191:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 8192:             push(@{$changes{'delete'}},$balancer);
 8193:             next;
 8194:         }
 8195:         if (!exists($currbalancer{$balancer})) {
 8196:             push(@{$changes{'add'}},$balancer);
 8197:         }
 8198:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 8199:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 8200:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 8201:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8202:             $saveloadbalancing = 1;
 8203:         }
 8204:         foreach my $sparetype (@sparestypes) {
 8205:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 8206:             my @offloadto;
 8207:             foreach my $target (@targets) {
 8208:                 if (($servers{$target}) && ($target ne $balancer)) {
 8209:                     if ($sparetype eq 'default') {
 8210:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 8211:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 8212:                         }
 8213:                     }
 8214:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 8215:                         push(@offloadto,$target);
 8216:                     }
 8217:                 }
 8218:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 8219:             }
 8220:         }
 8221:         if (ref($currtargets{$balancer}) eq 'HASH') {
 8222:             foreach my $sparetype (@sparestypes) {
 8223:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 8224:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 8225:                     if (@targetdiffs > 0) {
 8226:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8227:                     }
 8228:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8229:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8230:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8231:                     }
 8232:                 }
 8233:             }
 8234:         } else {
 8235:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 8236:                 foreach my $sparetype (@sparestypes) { 
 8237:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8238:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8239:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 8240:                         }
 8241:                     }
 8242:                 }
 8243:             }  
 8244:         }
 8245:         my $ishomedom;
 8246:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 8247:             $ishomedom = 1;
 8248:         }
 8249:         if (ref($alltypes) eq 'ARRAY') {
 8250:             foreach my $type (@{$alltypes}) {
 8251:                 my $rule;
 8252:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) && 
 8253:                          (!$ishomedom)) {
 8254:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 8255:                 }
 8256:                 if ($rule eq 'specific') {
 8257:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 8258:                 }
 8259:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 8260:                 if (ref($currrules{$balancer}) eq 'HASH') {
 8261:                     if ($rule ne $currrules{$balancer}{$type}) {
 8262:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8263:                     }
 8264:                 } elsif ($rule ne '') {
 8265:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8266:                 }
 8267:             }
 8268:         }
 8269:     }
 8270:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8271:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8272:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 8273:             $defaultshash{'loadbalancing'} = {};
 8274:         }
 8275:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8276:                                                  \%defaultshash,$dom);
 8277:  
 8278:         if ($putresult eq 'ok') {
 8279:             if (keys(%changes) > 0) {
 8280:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 8281:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 8282:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 8283:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8284:                     }
 8285:                 }
 8286:                 if (ref($changes{'add'}) eq 'ARRAY') {
 8287:                     foreach my $balancer (sort(@{$changes{'add'}})) {   
 8288:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 8289:                     }
 8290:                 }
 8291:                 if (ref($changes{'curr'}) eq 'HASH') {
 8292:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 8293:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 8294:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 8295:                                 my %offloadstr;
 8296:                                 foreach my $sparetype (@sparestypes) {
 8297:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8298:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8299:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 8300:                                         }
 8301:                                     }
 8302:                                 }
 8303:                                 if (keys(%offloadstr) == 0) {
 8304:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8305:                                 } else {
 8306:                                     my $showoffload;
 8307:                                     foreach my $sparetype (@sparestypes) {
 8308:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8309:                                         if (defined($offloadstr{$sparetype})) {
 8310:                                             $showoffload .= $offloadstr{$sparetype};
 8311:                                         } else {
 8312:                                             $showoffload .= &mt('None');
 8313:                                         }
 8314:                                         $showoffload .= ('&nbsp;'x3);
 8315:                                     }
 8316:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 8317:                                 }
 8318:                             }
 8319:                         }
 8320:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 8321:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8322:                                 foreach my $type (@{$alltypes}) {
 8323:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 8324:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 8325:                                         my $balancetext;
 8326:                                         if ($rule eq '') {
 8327:                                             $balancetext =  $ruletitles{'default'};
 8328:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 8329:                                             $balancetext =  $ruletitles{$rule};
 8330:                                         } else {
 8331:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 8332:                                         }
 8333:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';     
 8334:                                     }
 8335:                                 }
 8336:                             }
 8337:                         }
 8338:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8339:                     }
 8340:                 }
 8341:                 if ($resulttext ne '') {
 8342:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8343:                 } else {
 8344:                     $resulttext = $nochgmsg;
 8345:                 }
 8346:             } else {
 8347:                 $resulttext = $nochgmsg;
 8348:             }
 8349:         } else {
 8350:             $resulttext = '<span class="LC_error">'.
 8351:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8352:         }
 8353:     } else {
 8354:         $resulttext = $nochgmsg;
 8355:     }
 8356:     return $resulttext;
 8357: }
 8358: 
 8359: sub recurse_check {
 8360:     my ($chkcats,$categories,$depth,$name) = @_;
 8361:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8362:         my $chg = 0;
 8363:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8364:             my $category = $chkcats->[$depth]{$name}[$j];
 8365:             my $item;
 8366:             if ($category eq '') {
 8367:                 $chg ++;
 8368:             } else {
 8369:                 my $deeper = $depth + 1;
 8370:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8371:                 if ($chg) {
 8372:                     $categories->{$item} -= $chg;
 8373:                 }
 8374:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8375:                 $deeper --;
 8376:             }
 8377:         }
 8378:     }
 8379:     return;
 8380: }
 8381: 
 8382: sub recurse_cat_deletes {
 8383:     my ($item,$coursecategories,$deletions) = @_;
 8384:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8385:     my $subdepth = $depth + 1;
 8386:     if (ref($coursecategories) eq 'HASH') {
 8387:         foreach my $subitem (keys(%{$coursecategories})) {
 8388:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8389:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8390:                 delete($coursecategories->{$subitem});
 8391:                 $deletions->{$subitem} = 1;
 8392:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8393:             }
 8394:         }
 8395:     }
 8396:     return;
 8397: }
 8398: 
 8399: sub get_active_dcs {
 8400:     my ($dom) = @_;
 8401:     my $now = time;
 8402:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 8403:     my %domcoords;
 8404:     my $numdcs = 0;
 8405:     my $now = time;
 8406:     foreach my $server (keys(%dompersonnel)) {
 8407:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8408:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8409:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8410:         }
 8411:     }
 8412:     return %domcoords;
 8413: }
 8414: 
 8415: sub active_dc_picker {
 8416:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
 8417:     my %domcoords = &get_active_dcs($dom); 
 8418:     my @domcoord = keys(%domcoords);
 8419:     if (keys(%currhash)) {
 8420:         foreach my $dc (keys(%currhash)) {
 8421:             unless (exists($domcoords{$dc})) {
 8422:                 push(@domcoord,$dc);
 8423:             }
 8424:         }
 8425:     }
 8426:     @domcoord = sort(@domcoord);
 8427:     my $numdcs = scalar(@domcoord); 
 8428:     my $rows = 0;
 8429:     my $table;
 8430:     if ($numdcs > 1) {
 8431:         $table = '<table>';
 8432:         for (my $i=0; $i<@domcoord; $i++) {
 8433:             my $rem = $i%($numinrow);
 8434:             if ($rem == 0) {
 8435:                 if ($i > 0) {
 8436:                     $table .= '</tr>';
 8437:                 }
 8438:                 $table .= '<tr>';
 8439:                 $rows ++;
 8440:             }
 8441:             my $check = '';
 8442:             if ($inputtype eq 'radio') {
 8443:                 if (keys(%currhash) == 0) {
 8444:                     if (!$i) {
 8445:                         $check = ' checked="checked"';
 8446:                     }
 8447:                 } elsif (exists($currhash{$domcoord[$i]})) {
 8448:                     $check = ' checked="checked"';
 8449:                 }
 8450:             } else {
 8451:                 if (exists($currhash{$domcoord[$i]})) {
 8452:                     $check = ' checked="checked"';
 8453:                 }
 8454:             }
 8455:             if ($i == @domcoord - 1) {
 8456:                 my $colsleft = $numinrow - $rem;
 8457:                 if ($colsleft > 1) {
 8458:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
 8459:                 } else {
 8460:                     $table .= '<td class="LC_left_item">';
 8461:                 }
 8462:             } else {
 8463:                 $table .= '<td class="LC_left_item">';
 8464:             }
 8465:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
 8466:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
 8467:             $table .= '<span class="LC_nobreak"><label>'.
 8468:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
 8469:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
 8470:             if ($user ne $dcname.':'.$dcdom) {
 8471:                 $table .=  ' ('.$dcname.':'.$dcdom.')'.
 8472:                            '</label></span></td>';
 8473:             }
 8474:         }
 8475:         $table .= '</tr></table>';
 8476:     } elsif ($numdcs == 1) {
 8477:         if ($inputtype eq 'radio') {
 8478:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />';
 8479:         } else {
 8480:             my $check;
 8481:             if (exists($currhash{$domcoord[0]})) {
 8482:                 $check = ' checked="checked"';
 8483:             }
 8484:             $table .= '<input type="checkbox" name="'.$name.'" '.
 8485:                       'value="'.$domcoord[0].'"'.$check.' />';
 8486:             $rows ++;
 8487:         }
 8488:     }
 8489:     return ($numdcs,$table,$rows);
 8490: }
 8491: 
 8492: sub usersession_titles {
 8493:     return &Apache::lonlocal::texthash(
 8494:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8495:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8496:                spares => 'Servers offloaded to, when busy',
 8497:                version => 'LON-CAPA version requirement',
 8498:                excludedomain => 'Allow all, but exclude specific domains',
 8499:                includedomain => 'Deny all, but include specific domains',
 8500:                primary => 'Primary (checked first)',
 8501:                default => 'Default',
 8502:            );
 8503: }
 8504: 
 8505: sub id_for_thisdom {
 8506:     my (%servers) = @_;
 8507:     my %altids;
 8508:     foreach my $server (keys(%servers)) {
 8509:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8510:         if ($serverhome ne $server) {
 8511:             $altids{$serverhome} = $server;
 8512:         }
 8513:     }
 8514:     return %altids;
 8515: }
 8516: 
 8517: sub count_servers {
 8518:     my ($currbalancer,%servers) = @_;
 8519:     my (@spares,$numspares);
 8520:     foreach my $lonhost (sort(keys(%servers))) {
 8521:         next if ($currbalancer eq $lonhost);
 8522:         push(@spares,$lonhost);
 8523:     }
 8524:     if ($currbalancer) {
 8525:         $numspares = scalar(@spares);
 8526:     } else {
 8527:         $numspares = scalar(@spares) - 1;
 8528:     }
 8529:     return ($numspares,@spares);
 8530: }
 8531: 
 8532: sub lonbalance_targets_js {
 8533:     my ($dom,$types,$servers,$settings) = @_;
 8534:     my $select = &mt('Select');
 8535:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8536:     if (ref($servers) eq 'HASH') {
 8537:         $alltargets = join("','",sort(keys(%{$servers})));
 8538:         my @homedoms;
 8539:         foreach my $server (sort(keys(%{$servers}))) {
 8540:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8541:                 push(@homedoms,'1');
 8542:             } else {
 8543:                 push(@homedoms,'0');
 8544:             }
 8545:         }
 8546:         $allishome = join("','",@homedoms);
 8547:     }
 8548:     if (ref($types) eq 'ARRAY') {
 8549:         if (@{$types} > 0) {
 8550:             @alltypes = @{$types};
 8551:         }
 8552:     }
 8553:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8554:     $allinsttypes = join("','",@alltypes);
 8555:     my (%currbalancer,%currtargets,%currrules,%existing);
 8556:     if (ref($settings) eq 'HASH') {
 8557:         %existing = %{$settings};
 8558:     }
 8559:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 8560:                               \%currtargets,\%currrules);
 8561:     my $balancers = join("','",sort(keys(%currbalancer))); 
 8562:     return <<"END";
 8563: 
 8564: <script type="text/javascript">
 8565: // <![CDATA[
 8566: 
 8567: currBalancers = new Array('$balancers');
 8568: 
 8569: function toggleTargets(balnum) {
 8570:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8571:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 8572:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 8573:     var prevbalancer = prevhostitem.value;
 8574:     var baltotal = document.getElementById('loadbalancing_total').value;
 8575:     prevhostitem.value = balancer;
 8576:     if (prevbalancer != '') {
 8577:         var prevIdx = currBalancers.indexOf(prevbalancer);
 8578:         if (prevIdx != -1) {
 8579:             currBalancers.splice(prevIdx,1);
 8580:         }
 8581:     }
 8582:     if (balancer == '') {
 8583:         hideSpares(balnum);
 8584:     } else {
 8585:         var currIdx = currBalancers.indexOf(balancer);
 8586:         if (currIdx == -1) {
 8587:             currBalancers.push(balancer);
 8588:         }
 8589:         var homedoms = new Array('$allishome');
 8590:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 8591:         showSpares(balancer,ishomedom,balnum);
 8592:     }
 8593:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 8594:     return;
 8595: }
 8596: 
 8597: function showSpares(balancer,ishomedom,balnum) {
 8598:     var alltargets = new Array('$alltargets');
 8599:     var insttypes = new Array('$allinsttypes');
 8600:     var offloadtypes = new Array('primary','default');
 8601: 
 8602:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 8603:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 8604:  
 8605:     for (var i=0; i<offloadtypes.length; i++) {
 8606:         var count = 0;
 8607:         for (var j=0; j<alltargets.length; j++) {
 8608:             if (alltargets[j] != balancer) {
 8609:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 8610:                 item.value = alltargets[j];
 8611:                 item.style.textAlign='left';
 8612:                 item.style.textFace='normal';
 8613:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8614:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 8615:                     item.disabled = '';
 8616:                 } else {
 8617:                     item.disabled = 'disabled';
 8618:                     item.checked = false;
 8619:                 }
 8620:                 count ++;
 8621:             }
 8622:         }
 8623:     }
 8624:     for (var k=0; k<insttypes.length; k++) {
 8625:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8626:             if (ishomedom == 1) {
 8627:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8628:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8629:             } else {
 8630:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8631:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8632: 
 8633:             }
 8634:         } else {
 8635:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8636:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8637:         }
 8638:         if ((insttypes[k] != '_LC_external') && 
 8639:             ((insttypes[k] != '_LC_internetdom') ||
 8640:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8641:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 8642:             item.options.length = 0;
 8643:             item.options[0] = new Option("","",true,true);
 8644:             var idx = 0;  
 8645:             for (var m=0; m<alltargets.length; m++) {
 8646:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 8647:                     idx ++;
 8648:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8649:                     
 8650:                 }
 8651:             }
 8652:         }
 8653:     }
 8654:     return;
 8655: }
 8656: 
 8657: function hideSpares(balnum) {
 8658:     var alltargets = new Array('$alltargets');
 8659:     var insttypes = new Array('$allinsttypes');
 8660:     var offloadtypes = new Array('primary','default');
 8661: 
 8662:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 8663:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 8664: 
 8665:     var total = alltargets.length - 1;
 8666:     for (var i=0; i<offloadtypes; i++) {
 8667:         for (var j=0; j<total; j++) {
 8668:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 8669:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 8670:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8671:         }
 8672:     }
 8673:     for (var k=0; k<insttypes.length; k++) {
 8674:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8675:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8676:         if (insttypes[k] != '_LC_external') {
 8677:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 8678:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 8679:         }
 8680:     }
 8681:     return;
 8682: }
 8683: 
 8684: function checkOffloads(item,balnum,type) {
 8685:     var alltargets = new Array('$alltargets');
 8686:     var offloadtypes = new Array('primary','default');
 8687:     if (item.checked) {
 8688:         var total = alltargets.length - 1;
 8689:         var other;
 8690:         if (type == offloadtypes[0]) {
 8691:             other = offloadtypes[1];
 8692:         } else {
 8693:             other = offloadtypes[0];
 8694:         }
 8695:         for (var i=0; i<total; i++) {
 8696:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 8697:             if (server == item.value) {
 8698:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 8699:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 8700:                 }
 8701:             }
 8702:         }
 8703:     }
 8704:     return;
 8705: }
 8706: 
 8707: function singleServerToggle(balnum,type) {
 8708:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 8709:     if (offloadtoSelIdx == 0) {
 8710:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 8711:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8712: 
 8713:     } else {
 8714:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 8715:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8716:     }
 8717:     return;
 8718: }
 8719: 
 8720: function balanceruleChange(formname,balnum,type) {
 8721:     if (type == '_LC_external') {
 8722:         return;
 8723:     }
 8724:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 8725:     for (var i=0; i<typesRules.length; i++) {
 8726:         if (formname.elements[typesRules[i]].checked) {
 8727:             if (formname.elements[typesRules[i]].value != 'specific') {
 8728:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 8729:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8730:             } else {
 8731:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8732:             }
 8733:         }
 8734:     }
 8735:     return;
 8736: }
 8737: 
 8738: function balancerDeleteChange(balnum) {
 8739:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8740:     var baltotal = document.getElementById('loadbalancing_total').value;
 8741:     var addtarget;
 8742:     var removetarget;
 8743:     var action = 'delete';
 8744:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 8745:         var lonhost = hostitem.value;
 8746:         var currIdx = currBalancers.indexOf(lonhost);
 8747:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 8748:             if (currIdx != -1) {
 8749:                 currBalancers.splice(currIdx,1);
 8750:             }
 8751:             addtarget = lonhost;
 8752:         } else {
 8753:             if (currIdx == -1) {
 8754:                 currBalancers.push(lonhost);
 8755:             }
 8756:             removetarget = lonhost;
 8757:             action = 'undelete';
 8758:         }
 8759:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 8760:     }
 8761:     return;
 8762: }
 8763: 
 8764: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 8765:     if (baltotal > 1) {
 8766:         var offloadtypes = new Array('primary','default');
 8767:         var alltargets = new Array('$alltargets');
 8768:         var insttypes = new Array('$allinsttypes');
 8769:         for (var i=0; i<baltotal; i++) {
 8770:             if (i != balnum) {
 8771:                 for (var j=0; j<offloadtypes.length; j++) {
 8772:                     var total = alltargets.length - 1;
 8773:                     for (var k=0; k<total; k++) {
 8774:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 8775:                         var server = serveritem.value;
 8776:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 8777:                             if (server == addtarget) {
 8778:                                 serveritem.disabled = '';
 8779:                             }
 8780:                         }
 8781:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8782:                             if (server == removetarget) {
 8783:                                 serveritem.disabled = 'disabled';
 8784:                                 serveritem.checked = false;
 8785:                             }
 8786:                         }
 8787:                     }
 8788:                 }
 8789:                 for (var j=0; j<insttypes.length; j++) {
 8790:                     if (insttypes[j] != '_LC_external') {
 8791:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 8792:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 8793:                             var currSel = singleserver.selectedIndex;
 8794:                             var currVal = singleserver.options[currSel].value;
 8795:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 8796:                                 var numoptions = singleserver.options.length;
 8797:                                 var needsnew = 1;
 8798:                                 for (var k=0; k<numoptions; k++) {
 8799:                                     if (singleserver.options[k] == addtarget) {
 8800:                                         needsnew = 0;
 8801:                                         break;
 8802:                                     }
 8803:                                 }
 8804:                                 if (needsnew == 1) {
 8805:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 8806:                                 }
 8807:                             }
 8808:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8809:                                 singleserver.options.length = 0;
 8810:                                 if ((currVal) && (currVal != removetarget)) {
 8811:                                     singleserver.options[0] = new Option("","",false,false);
 8812:                                 } else {
 8813:                                     singleserver.options[0] = new Option("","",true,true);
 8814:                                 }
 8815:                                 var idx = 0;
 8816:                                 for (var m=0; m<alltargets.length; m++) {
 8817:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
 8818:                                         idx ++;
 8819:                                         if (currVal == alltargets[m]) {
 8820:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
 8821:                                         } else {
 8822:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8823:                                         }
 8824:                                     }
 8825:                                 }
 8826:                             }
 8827:                         }
 8828:                     }
 8829:                 }
 8830:             }
 8831:         }
 8832:     }
 8833:     return;
 8834: }
 8835: 
 8836: // ]]>
 8837: </script>
 8838: 
 8839: END
 8840: }
 8841: 
 8842: sub new_spares_js {
 8843:     my @sparestypes = ('primary','default');
 8844:     my $types = join("','",@sparestypes);
 8845:     my $select = &mt('Select');
 8846:     return <<"END";
 8847: 
 8848: <script type="text/javascript">
 8849: // <![CDATA[
 8850: 
 8851: function updateNewSpares(formname,lonhost) {
 8852:     var types = new Array('$types');
 8853:     var include = new Array();
 8854:     var exclude = new Array();
 8855:     for (var i=0; i<types.length; i++) {
 8856:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8857:         for (var j=0; j<spareboxes.length; j++) {
 8858:             if (formname.elements[spareboxes[j]].checked) {
 8859:                 exclude.push(formname.elements[spareboxes[j]].value);
 8860:             } else {
 8861:                 include.push(formname.elements[spareboxes[j]].value);
 8862:             }
 8863:         }
 8864:     }
 8865:     for (var i=0; i<types.length; i++) {
 8866:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8867:         var selIdx = newSpare.selectedIndex;
 8868:         var currnew = newSpare.options[selIdx].value;
 8869:         var okSpares = new Array();
 8870:         for (var j=0; j<newSpare.options.length; j++) {
 8871:             var possible = newSpare.options[j].value;
 8872:             if (possible != '') {
 8873:                 if (exclude.indexOf(possible) == -1) {
 8874:                     okSpares.push(possible);
 8875:                 } else {
 8876:                     if (currnew == possible) {
 8877:                         selIdx = 0;
 8878:                     }
 8879:                 }
 8880:             }
 8881:         }
 8882:         for (var k=0; k<include.length; k++) {
 8883:             if (okSpares.indexOf(include[k]) == -1) {
 8884:                 okSpares.push(include[k]);
 8885:             }
 8886:         }
 8887:         okSpares.sort();
 8888:         newSpare.options.length = 0;
 8889:         if (selIdx == 0) {
 8890:             newSpare.options[0] = new Option("$select","",true,true);
 8891:         } else {
 8892:             newSpare.options[0] = new Option("$select","",false,false);
 8893:         }
 8894:         for (var m=0; m<okSpares.length; m++) {
 8895:             var idx = m+1;
 8896:             var selThis = 0;
 8897:             if (selIdx != 0) {
 8898:                 if (okSpares[m] == currnew) {
 8899:                     selThis = 1;
 8900:                 }
 8901:             }
 8902:             if (selThis == 1) {
 8903:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8904:             } else {
 8905:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8906:             }
 8907:         }
 8908:     }
 8909:     return;
 8910: }
 8911: 
 8912: function checkNewSpares(lonhost,type) {
 8913:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8914:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8915:     if (chosen != '') { 
 8916:         var othertype;
 8917:         var othernewSpare;
 8918:         if (type == 'primary') {
 8919:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 8920:         }
 8921:         if (type == 'default') {
 8922:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 8923:         }
 8924:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 8925:             othernewSpare.selectedIndex = 0;
 8926:         }
 8927:     }
 8928:     return;
 8929: }
 8930: 
 8931: // ]]>
 8932: </script>
 8933: 
 8934: END
 8935: 
 8936: }
 8937: 
 8938: sub common_domprefs_js {
 8939:     return <<"END";
 8940: 
 8941: <script type="text/javascript">
 8942: // <![CDATA[
 8943: 
 8944: function getIndicesByName(formname,item) {
 8945:     var group = new Array();
 8946:     for (var i=0;i<formname.elements.length;i++) {
 8947:         if (formname.elements[i].name == item) {
 8948:             group.push(formname.elements[i].id);
 8949:         }
 8950:     }
 8951:     return group;
 8952: }
 8953: 
 8954: // ]]>
 8955: </script>
 8956: 
 8957: END
 8958: 
 8959: }
 8960: 
 8961: sub recaptcha_js {
 8962:     my %lt = &captcha_phrases();
 8963:     return <<"END";
 8964: 
 8965: <script type="text/javascript">
 8966: // <![CDATA[
 8967: 
 8968: function updateCaptcha(caller,context) {
 8969:     var privitem;
 8970:     var pubitem;
 8971:     var privtext;
 8972:     var pubtext;
 8973:     if (document.getElementById(context+'_recaptchapub')) {
 8974:         pubitem = document.getElementById(context+'_recaptchapub');
 8975:     } else {
 8976:         return;
 8977:     }
 8978:     if (document.getElementById(context+'_recaptchapriv')) {
 8979:         privitem = document.getElementById(context+'_recaptchapriv');
 8980:     } else {
 8981:         return;
 8982:     }
 8983:     if (document.getElementById(context+'_recaptchapubtxt')) {
 8984:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 8985:     } else {
 8986:         return;
 8987:     }
 8988:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 8989:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 8990:     } else {
 8991:         return;
 8992:     }
 8993:     if (caller.checked) {
 8994:         if (caller.value == 'recaptcha') {
 8995:             pubitem.type = 'text';
 8996:             privitem.type = 'text';
 8997:             pubitem.size = '40';
 8998:             privitem.size = '40';
 8999:             pubtext.innerHTML = "$lt{'pub'}";
 9000:             privtext.innerHTML = "$lt{'priv'}";
 9001:         } else {
 9002:             pubitem.type = 'hidden';
 9003:             privitem.type = 'hidden';
 9004:             pubtext.innerHTML = '';
 9005:             privtext.innerHTML = '';
 9006:         }
 9007:     }
 9008:     return;
 9009: }
 9010: 
 9011: // ]]>
 9012: </script>
 9013: 
 9014: END
 9015: 
 9016: }
 9017: 
 9018: sub captcha_phrases {
 9019:     return &Apache::lonlocal::texthash (
 9020:                  priv => 'Private key',
 9021:                  pub  => 'Public key',
 9022:                  original  => 'original (CAPTCHA)',
 9023:                  recaptcha => 'successor (ReCAPTCHA)',
 9024:                  notused   => 'unused',
 9025:     );
 9026: }
 9027: 
 9028: 1;

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