File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.172: download - view: text, annotated - select for diffs
Tue Oct 2 18:35:19 2012 UTC (11 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Domain config option to allow requests for author spaces.
  - fix retrieval of current default.
  - more detail in message about what changes were made.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.172 2012/10/02 18:35:19 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas,  
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course.
   98: 
   99: Outputs: 1
  100: 
  101: $datatable  - HTML containing form elements which allow settings to be changed. 
  102: 
  103: In the case of course requests, radio buttons are displayed for each institutional
  104: affiliate type (and also default, and _LC_adv) for each of the course types 
  105: (official, unofficial and community).  In each case the radio buttons allow the 
  106: selection of one of four values:
  107: 
  108: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  109: which have the following effects:
  110: 
  111: 0
  112: 
  113: =over
  114: 
  115: - course requests are not allowed for this course types/affiliation
  116: 
  117: =back
  118: 
  119: approval 
  120: 
  121: =over 
  122: 
  123: - course requests must be approved by a Doman Coordinator in the 
  124: course's domain
  125: 
  126: =back
  127: 
  128: validate 
  129: 
  130: =over
  131: 
  132: - an institutional validation (e.g., check requestor is instructor
  133: of record) needs to be passed before the course will be created.  The required
  134: validation is in localenroll.pm on the primary library server for the course 
  135: domain.
  136: 
  137: =back
  138: 
  139: autolimit 
  140: 
  141: =over
  142:  
  143: - course requests will be processed automatically up to a limit of
  144: N requests for the course type for the particular requestor.
  145: If N is undefined, there is no limit to the number of course requests
  146: which a course owner may submit and have processed automatically. 
  147: 
  148: =back
  149: 
  150: =item modify_quotas() 
  151: 
  152: =back
  153: 
  154: =cut
  155: 
  156: package Apache::domainprefs;
  157: 
  158: use strict;
  159: use Apache::Constants qw(:common :http);
  160: use Apache::lonnet;
  161: use Apache::loncommon();
  162: use Apache::lonhtmlcommon();
  163: use Apache::lonlocal;
  164: use Apache::lonmsg();
  165: use Apache::lonconfigsettings;
  166: use LONCAPA qw(:DEFAULT :match);
  167: use LONCAPA::Enrollment;
  168: use LONCAPA::lonauthcgi();
  169: use File::Copy;
  170: use Locale::Language;
  171: use DateTime::TimeZone;
  172: use DateTime::Locale;
  173: 
  174: my $registered_cleanup;
  175: my $modified_urls;
  176: 
  177: sub handler {
  178:     my $r=shift;
  179:     if ($r->header_only) {
  180:         &Apache::loncommon::content_type($r,'text/html');
  181:         $r->send_http_header;
  182:         return OK;
  183:     }
  184: 
  185:     my $context = 'domain';
  186:     my $dom = $env{'request.role.domain'};
  187:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  188:     if (&Apache::lonnet::allowed('mau',$dom)) {
  189:         &Apache::loncommon::content_type($r,'text/html');
  190:         $r->send_http_header;
  191:     } else {
  192:         $env{'user.error.msg'}=
  193:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  194:         return HTTP_NOT_ACCEPTABLE;
  195:     }
  196: 
  197:     $registered_cleanup=0;
  198:     @{$modified_urls}=();
  199: 
  200:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  201:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  202:                                             ['phase','actions']);
  203:     my $phase = 'pickactions';
  204:     if ( exists($env{'form.phase'}) ) {
  205:         $phase = $env{'form.phase'};
  206:     }
  207:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  208:     my %domconfig =
  209:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  210:                 'quotas','autoenroll','autoupdate','autocreate',
  211:                 'directorysrch','usercreation','usermodification',
  212:                 'contacts','defaults','scantron','coursecategories',
  213:                 'serverstatuses','requestcourses','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',
  390:                   help  => 'Domain_Configuration_Load_Balancing',
  391:                   header => [{col1 => 'Balancers',
  392:                               col2 => 'Default destinations',
  393:                               col3 => 'User affliation',
  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:     my @roles = ('student','coordinator','author','admin');
  410:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  411:     &Apache::lonhtmlcommon::add_breadcrumb
  412:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  413:       text=>"Settings to display/modify"});
  414:     my $confname = $dom.'-domainconfig';
  415:     if ($phase eq 'process') {
  416:         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
  417:     } elsif ($phase eq 'display') {
  418:         my $js = &recaptcha_js();
  419:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  420:             my ($othertitle,$usertypes,$types) =
  421:                 &Apache::loncommon::sorted_inst_types($dom);
  422:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  423:                                           $domconfig{'loadbalancing'}).
  424:                    &new_spares_js().
  425:                    &common_domprefs_js().
  426:                    &Apache::loncommon::javascript_array_indexof();
  427:         }
  428:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  429:     } else {
  430:         if (keys(%domconfig) == 0) {
  431:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  432:             my @ids=&Apache::lonnet::current_machine_ids();
  433:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  434:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  435:                 my @loginimages = ('img','logo','domlogo','login');
  436:                 my $custom_img_count = 0;
  437:                 foreach my $img (@loginimages) {
  438:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  439:                         $custom_img_count ++;
  440:                     }
  441:                 }
  442:                 foreach my $role (@roles) {
  443:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  444:                         $custom_img_count ++;
  445:                     }
  446:                 }
  447:                 if ($custom_img_count > 0) {
  448:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  449:                     my $switch_server = &check_switchserver($dom,$confname);
  450:                     $r->print(
  451:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  452:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  453:     &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 />'.
  454:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  455:                     if ($switch_server) {
  456:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  457:                     }
  458:                     $r->print(&Apache::loncommon::end_page());
  459:                     return OK;
  460:                 }
  461:             }
  462:         }
  463:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  464:     }
  465:     return OK;
  466: }
  467: 
  468: sub process_changes {
  469:     my ($r,$dom,$confname,$action,$roles,$values) = @_;
  470:     my %domconfig;
  471:     if (ref($values) eq 'HASH') {
  472:         %domconfig = %{$values};
  473:     }
  474:     my $output;
  475:     if ($action eq 'login') {
  476:         $output = &modify_login($r,$dom,$confname,%domconfig);
  477:     } elsif ($action eq 'rolecolors') {
  478:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  479:                                      %domconfig);
  480:     } elsif ($action eq 'quotas') {
  481:         $output = &modify_quotas($dom,$action,%domconfig);
  482:     } elsif ($action eq 'autoenroll') {
  483:         $output = &modify_autoenroll($dom,%domconfig);
  484:     } elsif ($action eq 'autoupdate') {
  485:         $output = &modify_autoupdate($dom,%domconfig);
  486:     } elsif ($action eq 'autocreate') {
  487:         $output = &modify_autocreate($dom,%domconfig);
  488:     } elsif ($action eq 'directorysrch') {
  489:         $output = &modify_directorysrch($dom,%domconfig);
  490:     } elsif ($action eq 'usercreation') {
  491:         $output = &modify_usercreation($dom,%domconfig);
  492:     } elsif ($action eq 'usermodification') {
  493:         $output = &modify_usermodification($dom,%domconfig);
  494:     } elsif ($action eq 'contacts') {
  495:         $output = &modify_contacts($dom,%domconfig);
  496:     } elsif ($action eq 'defaults') {
  497:         $output = &modify_defaults($dom,$r);
  498:     } elsif ($action eq 'scantron') {
  499:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  500:     } elsif ($action eq 'coursecategories') {
  501:         $output = &modify_coursecategories($dom,%domconfig);
  502:     } elsif ($action eq 'serverstatuses') {
  503:         $output = &modify_serverstatuses($dom,%domconfig);
  504:     } elsif ($action eq 'requestcourses') {
  505:         $output = &modify_quotas($dom,$action,%domconfig);
  506:     } elsif ($action eq 'requestauthor') {
  507:         $output = &modify_quotas($dom,$action,%domconfig);
  508:     } elsif ($action eq 'helpsettings') {
  509:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  510:     } elsif ($action eq 'coursedefaults') {
  511:         $output = &modify_coursedefaults($dom,%domconfig);
  512:     } elsif ($action eq 'usersessions') {
  513:         $output = &modify_usersessions($dom,%domconfig);
  514:     } elsif ($action eq 'loadbalancing') {
  515:         $output = &modify_loadbalancing($dom,%domconfig);
  516:     }
  517:     return $output;
  518: }
  519: 
  520: sub print_config_box {
  521:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  522:     my $rowtotal = 0;
  523:     my $output;
  524:     if ($action eq 'coursecategories') {
  525:         $output = &coursecategories_javascript($settings);
  526:     }
  527:     $output .= 
  528:          '<table class="LC_nested_outer">
  529:           <tr>
  530:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  531:            &mt($item->{text}).'&nbsp;'.
  532:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  533:           '</tr>';
  534:     $rowtotal ++;
  535:     my $numheaders = 1;
  536:     if (ref($item->{'header'}) eq 'ARRAY') {
  537:         $numheaders = scalar(@{$item->{'header'}});
  538:     }
  539:     if ($numheaders > 1) {
  540:         my $colspan = '';
  541:         my $rightcolspan = '';
  542:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') || 
  543:             (($action eq 'login') && ($numheaders < 3))) {
  544:             $colspan = ' colspan="2"';
  545:         }
  546:         if ($action eq 'usersessions') {
  547:             $rightcolspan = ' colspan="3"'; 
  548:         }
  549:         $output .= '
  550:           <tr>
  551:            <td>
  552:             <table class="LC_nested">
  553:              <tr class="LC_info_row">
  554:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  555:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  556:              </tr>';
  557:         $rowtotal ++;
  558:         if ($action eq 'autoupdate') {
  559:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  560:         } elsif ($action eq 'usercreation') {
  561:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  562:         } elsif ($action eq 'usermodification') {
  563:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  564:         } elsif ($action eq 'coursecategories') {
  565:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  566:         } elsif ($action eq 'login') {
  567:             if ($numheaders == 3) {
  568:                 $colspan = ' colspan="2"';
  569:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  570:             } else {
  571:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  572:             }
  573:         } elsif ($action eq 'requestcourses') {
  574:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  575:         } elsif ($action eq 'requestauthor') {
  576:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  577:         } elsif ($action eq 'usersessions') {
  578:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  579:         } elsif ($action eq 'rolecolors') {
  580:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  581:         } elsif ($action eq 'coursedefaults') {
  582:             $output .= &print_coursedefaults('top',$dom,$settings,\$rowtotal);
  583:         }
  584:         $output .= '
  585:            </table>
  586:           </td>
  587:          </tr>
  588:          <tr>
  589:            <td>
  590:             <table class="LC_nested">
  591:              <tr class="LC_info_row">
  592:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  593:         $output .= '
  594:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  595:              </tr>';
  596:             $rowtotal ++;
  597:         if ($action eq 'autoupdate') {
  598:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  599:            </table>
  600:           </td>
  601:          </tr>
  602:          <tr>
  603:            <td>
  604:             <table class="LC_nested">
  605:              <tr class="LC_info_row">
  606:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  607:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  608:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  609:             $rowtotal ++;
  610:         } elsif ($action eq 'usercreation') {
  611:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  612:            </table>
  613:           </td>
  614:          </tr>
  615:          <tr>
  616:            <td>
  617:             <table class="LC_nested">
  618:              <tr class="LC_info_row">
  619:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  620:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  621:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  622:             $rowtotal ++;
  623:         } elsif ($action eq 'usermodification') {
  624:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  625:            </table>
  626:           </td>
  627:          </tr>
  628:          <tr>
  629:            <td>
  630:             <table class="LC_nested">
  631:              <tr class="LC_info_row">
  632:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  633:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  634:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  635:             $rowtotal ++;
  636:         } elsif ($action eq 'coursecategories') {
  637:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  638:         } elsif ($action eq 'login') {
  639:             if ($numheaders == 3) {
  640:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  641:            </table>
  642:           </td>
  643:          </tr>
  644:          <tr>
  645:            <td>
  646:             <table class="LC_nested">
  647:              <tr class="LC_info_row">
  648:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  649:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  650:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  651:                 $rowtotal ++;
  652:             } else {
  653:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  654:             }
  655:         } elsif ($action eq 'requestcourses') {
  656:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  657:         } elsif ($action eq 'requestauthor') {
  658:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  659:         } elsif ($action eq 'usersessions') {
  660:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  661:            </table>
  662:           </td>
  663:          </tr>
  664:          <tr>
  665:            <td>
  666:             <table class="LC_nested">
  667:              <tr class="LC_info_row">
  668:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  669:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  670:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  671:             $rowtotal ++;
  672:         } elsif ($action eq 'coursedefaults') {
  673:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  674:         } elsif ($action eq 'rolecolors') {
  675:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  676:            </table>
  677:           </td>
  678:          </tr>
  679:          <tr>
  680:            <td>
  681:             <table class="LC_nested">
  682:              <tr class="LC_info_row">
  683:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  684:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  685:               <td class="LC_right_item" valign="top">'.
  686:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  687:              </tr>'.
  688:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  689:            </table>
  690:           </td>
  691:          </tr>
  692:          <tr>
  693:            <td>
  694:             <table class="LC_nested">
  695:              <tr class="LC_info_row">
  696:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  697:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  698:              </tr>'.
  699:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  700:             $rowtotal += 2;
  701:         }
  702:     } else {
  703:         $output .= '
  704:           <tr>
  705:            <td>
  706:             <table class="LC_nested">
  707:              <tr class="LC_info_row">';
  708:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  709:             $output .= '  
  710:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  711:         } elsif ($action eq 'serverstatuses') {
  712:             $output .= '
  713:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  714:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  715: 
  716:         } else {
  717:             $output .= '
  718:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  719:         }
  720:         if (defined($item->{'header'}->[0]->{'col3'})) {
  721:             $output .= '<td class="LC_left_item" valign="top">'.
  722:                        &mt($item->{'header'}->[0]->{'col2'});
  723:             if ($action eq 'serverstatuses') {
  724:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  725:             } 
  726:         } else {
  727:             $output .= '<td class="LC_right_item" valign="top">'.
  728:                        &mt($item->{'header'}->[0]->{'col2'});
  729:         }
  730:         $output .= '</td>';
  731:         if ($item->{'header'}->[0]->{'col3'}) {
  732:             if (defined($item->{'header'}->[0]->{'col4'})) {
  733:                 $output .= '<td class="LC_left_item" valign="top">'.
  734:                             &mt($item->{'header'}->[0]->{'col3'});
  735:             } else {
  736:                 $output .= '<td class="LC_right_item" valign="top">'.
  737:                            &mt($item->{'header'}->[0]->{'col3'});
  738:             }
  739:             if ($action eq 'serverstatuses') {
  740:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  741:             }
  742:             $output .= '</td>';
  743:         }
  744:         if ($item->{'header'}->[0]->{'col4'}) {
  745:             $output .= '<td class="LC_right_item" valign="top">'.
  746:                        &mt($item->{'header'}->[0]->{'col4'});
  747:         }
  748:         $output .= '</tr>';
  749:         $rowtotal ++;
  750:         if ($action eq 'quotas') {
  751:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  752:         } elsif ($action eq 'autoenroll') {
  753:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  754:         } elsif ($action eq 'autocreate') {
  755:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  756:         } elsif ($action eq 'directorysrch') {
  757:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  758:         } elsif ($action eq 'contacts') {
  759:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  760:         } elsif ($action eq 'defaults') {
  761:             $output .= &print_defaults($dom,\$rowtotal);
  762:         } elsif ($action eq 'scantron') {
  763:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  764:         } elsif ($action eq 'serverstatuses') {
  765:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  766:         } elsif ($action eq 'helpsettings') {
  767:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  768:         } elsif ($action eq 'loadbalancing') {
  769:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  770:         }
  771:     }
  772:     $output .= '
  773:    </table>
  774:   </td>
  775:  </tr>
  776: </table><br />';
  777:     return ($output,$rowtotal);
  778: }
  779: 
  780: sub print_login {
  781:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  782:     my ($css_class,$datatable);
  783:     my %choices = &login_choices();
  784: 
  785:     if ($caller eq 'service') {
  786:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  787:         my $choice = $choices{'disallowlogin'};
  788:         $css_class = ' class="LC_odd_row"';
  789:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  790:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  791:                       '<th>'.$choices{'server'}.'</th>'.
  792:                       '<th>'.$choices{'serverpath'}.'</th>'.
  793:                       '<th>'.$choices{'custompath'}.'</th>'.
  794:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  795:         my %disallowed;
  796:         if (ref($settings) eq 'HASH') {
  797:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  798:                %disallowed = %{$settings->{'loginvia'}};
  799:             }
  800:         }
  801:         foreach my $lonhost (sort(keys(%servers))) {
  802:             my $direct = 'selected="selected"';
  803:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  804:                 if ($disallowed{$lonhost}{'server'} ne '') {
  805:                     $direct = '';
  806:                 }
  807:             }
  808:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  809:                           '<td><select name="'.$lonhost.'_server">'.
  810:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  811:                           '</option>';
  812:             foreach my $hostid (keys(%servers)) {
  813:                 next if ($servers{$hostid} eq $servers{$lonhost});
  814:                 my $selected = '';
  815:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  816:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  817:                         $selected = 'selected="selected"';
  818:                     }
  819:                 }
  820:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  821:                               $servers{$hostid}.'</option>';
  822:             }
  823:             $datatable .= '</select></td>'.
  824:                           '<td><select name="'.$lonhost.'_serverpath">';
  825:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  826:                 my $pathname = $path;
  827:                 if ($path eq 'custom') {
  828:                     $pathname = &mt('Custom Path').' ->';
  829:                 }
  830:                 my $selected = '';
  831:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  832:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  833:                         $selected = 'selected="selected"';
  834:                     }
  835:                 } elsif ($path eq '') {
  836:                     $selected = 'selected="selected"';
  837:                 }
  838:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  839:             }
  840:             $datatable .= '</select></td>';
  841:             my ($custom,$exempt);
  842:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  843:                 $custom = $disallowed{$lonhost}{'custompath'};
  844:                 $exempt = $disallowed{$lonhost}{'exempt'};
  845:             }
  846:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  847:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  848:                           '</tr>';
  849:         }
  850:         $datatable .= '</table></td></tr>';
  851:         return $datatable;
  852:     } elsif ($caller eq 'page') {
  853:         my %defaultchecked = ( 
  854:                                'coursecatalog' => 'on',
  855:                                'adminmail'     => 'off',
  856:                                'newuser'       => 'off',
  857:                              );
  858:         my @toggles = ('coursecatalog','adminmail','newuser');
  859:         my (%checkedon,%checkedoff);
  860:         foreach my $item (@toggles) {
  861:             if ($defaultchecked{$item} eq 'on') { 
  862:                 $checkedon{$item} = ' checked="checked" ';
  863:                 $checkedoff{$item} = ' ';
  864:             } elsif ($defaultchecked{$item} eq 'off') {
  865:                 $checkedoff{$item} = ' checked="checked" ';
  866:                 $checkedon{$item} = ' ';
  867:             }
  868:         }
  869:         my @images = ('img','logo','domlogo','login');
  870:         my @logintext = ('textcol','bgcol');
  871:         my @bgs = ('pgbg','mainbg','sidebg');
  872:         my @links = ('link','alink','vlink');
  873:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  874:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  875:         my (%is_custom,%designs);
  876:         my %defaults = (
  877:                        font => $defaultdesign{'login.font'},
  878:                        );
  879:         foreach my $item (@images) {
  880:             $defaults{$item} = $defaultdesign{'login.'.$item};
  881:             $defaults{'showlogo'}{$item} = 1;
  882:         }
  883:         foreach my $item (@bgs) {
  884:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  885:         }
  886:         foreach my $item (@logintext) {
  887:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  888:         }
  889:         foreach my $item (@links) {
  890:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  891:         }
  892:         if (ref($settings) eq 'HASH') {
  893:             foreach my $item (@toggles) {
  894:                 if ($settings->{$item} eq '1') {
  895:                     $checkedon{$item} =  ' checked="checked" ';
  896:                     $checkedoff{$item} = ' ';
  897:                 } elsif ($settings->{$item} eq '0') {
  898:                     $checkedoff{$item} =  ' checked="checked" ';
  899:                     $checkedon{$item} = ' ';
  900:                 }
  901:             }
  902:             foreach my $item (@images) {
  903:                 if (defined($settings->{$item})) {
  904:                     $designs{$item} = $settings->{$item};
  905:                     $is_custom{$item} = 1;
  906:                 }
  907:                 if (defined($settings->{'showlogo'}{$item})) {
  908:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  909:                 }
  910:             }
  911:             foreach my $item (@logintext) {
  912:                 if ($settings->{$item} ne '') {
  913:                     $designs{'logintext'}{$item} = $settings->{$item};
  914:                     $is_custom{$item} = 1;
  915:                 }
  916:             }
  917:             if ($settings->{'font'} ne '') {
  918:                 $designs{'font'} = $settings->{'font'};
  919:                 $is_custom{'font'} = 1;
  920:             }
  921:             foreach my $item (@bgs) {
  922:                 if ($settings->{$item} ne '') {
  923:                     $designs{'bgs'}{$item} = $settings->{$item};
  924:                     $is_custom{$item} = 1;
  925:                 }
  926:             }
  927:             foreach my $item (@links) {
  928:                 if ($settings->{$item} ne '') {
  929:                     $designs{'links'}{$item} = $settings->{$item};
  930:                     $is_custom{$item} = 1;
  931:                 }
  932:             }
  933:         } else {
  934:             if ($designhash{$dom.'.login.font'} ne '') {
  935:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  936:                 $is_custom{'font'} = 1;
  937:             }
  938:             foreach my $item (@images) {
  939:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  940:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  941:                     $is_custom{$item} = 1;
  942:                 }
  943:             }
  944:             foreach my $item (@bgs) {
  945:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  946:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  947:                     $is_custom{$item} = 1;
  948:                 }
  949:             }
  950:             foreach my $item (@links) {
  951:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  952:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  953:                     $is_custom{$item} = 1;
  954:                 }
  955:             }
  956:         }
  957:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  958:                                                       logo => 'Institution Logo',
  959:                                                       domlogo => 'Domain Logo',
  960:                                                       login => 'Login box');
  961:         my $itemcount = 1;
  962:         foreach my $item (@toggles) {
  963:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
  964:             $datatable .=  
  965:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  966:                 '</td><td>'.
  967:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
  968:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
  969:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
  970:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
  971:                 '</tr>';
  972:             $itemcount ++;
  973:         }
  974:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
  975:         $datatable .= '</tr></table></td></tr>';
  976:     } elsif ($caller eq 'help') {
  977:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
  978:         my $switchserver = &check_switchserver($dom,$confname);
  979:         my $itemcount = 1;
  980:         $defaulturl = '/adm/loginproblems.html';
  981:         $defaulttype = 'default';
  982:         %lt = &Apache::lonlocal::texthash (
  983:                      del     => 'Delete?',
  984:                      rep     => 'Replace:',
  985:                      upl     => 'Upload:',
  986:                      default => 'Default',
  987:                      custom  => 'Custom',
  988:                                              );
  989:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
  990:         my @currlangs;
  991:         if (ref($settings) eq 'HASH') {
  992:             if (ref($settings->{'helpurl'}) eq 'HASH') {
  993:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
  994:                     next if ($settings->{'helpurl'}{$key} eq '');
  995:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
  996:                     $type{$key} = 'custom';
  997:                     unless ($key eq 'nolang') {
  998:                         push(@currlangs,$key);
  999:                     }
 1000:                 }
 1001:             } elsif ($settings->{'helpurl'} ne '') {
 1002:                 $type{'nolang'} = 'custom';
 1003:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1004:             }
 1005:         }
 1006:         foreach my $lang ('nolang',sort(@currlangs)) {
 1007:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1008:             $datatable .= '<tr'.$css_class.'>';
 1009:             if ($url{$lang} eq '') {
 1010:                 $url{$lang} = $defaulturl;
 1011:             }
 1012:             if ($type{$lang} eq '') {
 1013:                 $type{$lang} = $defaulttype;
 1014:             }
 1015:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1016:             if ($lang eq 'nolang') {
 1017:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1018:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1019:             } else {
 1020:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1021:                                   $langchoices{$lang},
 1022:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1023:             }
 1024:             $datatable .= '</span></td>'."\n".
 1025:                           '<td class="LC_left_item">';
 1026:             if ($type{$lang} eq 'custom') {
 1027:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1028:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1029:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1030:             } else {
 1031:                 $datatable .= $lt{'upl'};
 1032:             }
 1033:             $datatable .='<br />';
 1034:             if ($switchserver) {
 1035:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1036:             } else {
 1037:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1038:             }
 1039:             $datatable .= '</td></tr>';
 1040:             $itemcount ++;
 1041:         }
 1042:         my @addlangs;
 1043:         foreach my $lang (sort(keys(%langchoices))) {
 1044:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1045:             push(@addlangs,$lang);
 1046:         }
 1047:         if (@addlangs > 0) {
 1048:             my %toadd;
 1049:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1050:             $toadd{''} = &mt('Select');
 1051:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1052:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1053:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1054:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1055:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1056:             if ($switchserver) {
 1057:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1058:             } else {
 1059:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1060:             }
 1061:             $datatable .= '</td></tr>';
 1062:             $itemcount ++;
 1063:         }
 1064:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1065:     }
 1066:     return $datatable;
 1067: }
 1068: 
 1069: sub login_choices {
 1070:     my %choices =
 1071:         &Apache::lonlocal::texthash (
 1072:             coursecatalog => 'Display Course/Community Catalog link?',
 1073:             adminmail     => "Display Administrator's E-mail Address?",
 1074:             disallowlogin => "Login page requests redirected",
 1075:             hostid        => "Server",
 1076:             server        => "Redirect to:",
 1077:             serverpath    => "Path",
 1078:             custompath    => "Custom", 
 1079:             exempt        => "Exempt IP(s)",
 1080:             directlogin   => "No redirect",
 1081:             newuser       => "Link to create a user account",
 1082:             img           => "Header",
 1083:             logo          => "Main Logo",
 1084:             domlogo       => "Domain Logo",
 1085:             login         => "Log-in Header", 
 1086:             textcol       => "Text color",
 1087:             bgcol         => "Box color",
 1088:             bgs           => "Background colors",
 1089:             links         => "Link colors",
 1090:             font          => "Font color",
 1091:             pgbg          => "Header",
 1092:             mainbg        => "Page",
 1093:             sidebg        => "Login box",
 1094:             link          => "Link",
 1095:             alink         => "Active link",
 1096:             vlink         => "Visited link",
 1097:         );
 1098:     return %choices;
 1099: }
 1100: 
 1101: sub print_rolecolors {
 1102:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1103:     my %choices = &color_font_choices();
 1104:     my @bgs = ('pgbg','tabbg','sidebg');
 1105:     my @links = ('link','alink','vlink');
 1106:     my @images = ('img');
 1107:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1108:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1109:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1110:     my (%is_custom,%designs);
 1111:     my %defaults = (
 1112:                    img => $defaultdesign{$role.'.img'},
 1113:                    font => $defaultdesign{$role.'.font'},
 1114: 		   fontmenu => $defaultdesign{$role.'.fontmenu'},
 1115:                    );
 1116:     foreach my $item (@bgs) {
 1117:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1118:     }
 1119:     foreach my $item (@links) {
 1120:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1121:     }
 1122:     if (ref($settings) eq 'HASH') {
 1123:         if (ref($settings->{$role}) eq 'HASH') {
 1124:             if ($settings->{$role}->{'img'} ne '') {
 1125:                 $designs{'img'} = $settings->{$role}->{'img'};
 1126:                 $is_custom{'img'} = 1;
 1127:             }
 1128:             if ($settings->{$role}->{'font'} ne '') {
 1129:                 $designs{'font'} = $settings->{$role}->{'font'};
 1130:                 $is_custom{'font'} = 1;
 1131:             }
 1132:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1133:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1134:                 $is_custom{'fontmenu'} = 1;
 1135:             }
 1136:             foreach my $item (@bgs) {
 1137:                 if ($settings->{$role}->{$item} ne '') {
 1138:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1139:                     $is_custom{$item} = 1;
 1140:                 }
 1141:             }
 1142:             foreach my $item (@links) {
 1143:                 if ($settings->{$role}->{$item} ne '') {
 1144:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1145:                     $is_custom{$item} = 1;
 1146:                 }
 1147:             }
 1148:         }
 1149:     } else {
 1150:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1151:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1152:             $is_custom{'img'} = 1;
 1153:         }
 1154:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1155:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1156:             $is_custom{'fontmenu'} = 1; 
 1157:         }
 1158:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1159:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1160:             $is_custom{'font'} = 1;
 1161:         }
 1162:         foreach my $item (@bgs) {
 1163:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1164:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1165:                 $is_custom{$item} = 1;
 1166:             
 1167:             }
 1168:         }
 1169:         foreach my $item (@links) {
 1170:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1171:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1172:                 $is_custom{$item} = 1;
 1173:             }
 1174:         }
 1175:     }
 1176:     my $itemcount = 1;
 1177:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1178:     $datatable .= '</tr></table></td></tr>';
 1179:     return $datatable;
 1180: }
 1181: 
 1182: sub display_color_options {
 1183:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1184:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1185:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1186:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1187:     my $datatable = '<tr'.$css_class.'>'.
 1188:         '<td>'.$choices->{'font'}.'</td>';
 1189:     if (!$is_custom->{'font'}) {
 1190:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1191:     } else {
 1192:         $datatable .= '<td>&nbsp;</td>';
 1193:     }
 1194:     my $fontlink = &color_pick($phase,$role,'font',$choices->{'font'},$designs->{'font'});
 1195:     $datatable .= '<td><span class="LC_nobreak">'.
 1196:                   '<input type="text" size="10" name="'.$role.'_font"'.
 1197:                   ' value="'.$designs->{'font'}.'" />&nbsp;'.$fontlink.
 1198:                   '&nbsp;<span id="css_'.$role.'_font" style="background-color: '.
 1199:                   $designs->{'font'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1200:                   '</span></td></tr>';
 1201:     unless ($role eq 'login') { 
 1202:         $datatable .= '<tr'.$css_class.'>'.
 1203:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1204:         if (!$is_custom->{'fontmenu'}) {
 1205:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1206:         } else {
 1207:             $datatable .= '<td>&nbsp;</td>';
 1208:         }
 1209:         $fontlink = &color_pick($phase,$role,'fontmenu',$choices->{'fontmenu'},$designs->{'fontmenu'});
 1210:         $datatable .= '<td><span class="LC_nobreak">'.
 1211:                       '<input type="text" size="10" name="'.$role.'_fontmenu"'.
 1212:                       ' value="'.$designs->{'fontmenu'}.'" />&nbsp;'.$fontlink.
 1213:                       '&nbsp;<span id="css_'.$role.'_fontmenu" style="background-color: '.
 1214:                       $designs->{'fontmenu'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1215:                       '</span></td></tr>';
 1216:     }
 1217:     my $switchserver = &check_switchserver($dom,$confname);
 1218:     foreach my $img (@{$images}) {
 1219: 	$itemcount ++;
 1220:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1221:         $datatable .= '<tr'.$css_class.'>'.
 1222:                       '<td>'.$choices->{$img};
 1223:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1224:         if ($role eq 'login') {
 1225:             if ($img eq 'login') {
 1226:                 $login_hdr_pick =
 1227:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1228:                 $logincolors =
 1229:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1230:                                             $designs);
 1231:             } elsif ($img ne 'domlogo') {
 1232:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1233:             }
 1234:         }
 1235:         $datatable .= '</td>';
 1236:         if ($designs->{$img} ne '') {
 1237:             $imgfile = $designs->{$img};
 1238: 	    $img_import = ($imgfile =~ m{^/adm/});
 1239:         } else {
 1240:             $imgfile = $defaults->{$img};
 1241:         }
 1242:         if ($imgfile) {
 1243:             my ($showfile,$fullsize);
 1244:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1245:                 my $urldir = $1;
 1246:                 my $filename = $2;
 1247:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1248:                 if (@info) {
 1249:                     my $thumbfile = 'tn-'.$filename;
 1250:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1251:                     if (@thumb) {
 1252:                         $showfile = $urldir.'/'.$thumbfile;
 1253:                     } else {
 1254:                         $showfile = $imgfile;
 1255:                     }
 1256:                 } else {
 1257:                     $showfile = '';
 1258:                 }
 1259:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1260:                 $showfile = $imgfile;
 1261:                 my $imgdir = $1;
 1262:                 my $filename = $2;
 1263:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1264:                     $showfile = "/$imgdir/tn-".$filename;
 1265:                 } else {
 1266:                     my $input = $londocroot.$imgfile;
 1267:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1268:                     if (!-e $output) {
 1269:                         my ($width,$height) = &thumb_dimensions();
 1270:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1271:                         if ($fullwidth ne '' && $fullheight ne '') {
 1272:                             if ($fullwidth > $width && $fullheight > $height) { 
 1273:                                 my $size = $width.'x'.$height;
 1274:                                 system("convert -sample $size $input $output");
 1275:                                 $showfile = "/$imgdir/tn-".$filename;
 1276:                             }
 1277:                         }
 1278:                     }
 1279:                 }
 1280:             }
 1281:             if ($showfile) {
 1282:                 if ($showfile =~ m{^/(adm|res)/}) {
 1283:                     if ($showfile =~ m{^/res/}) {
 1284:                         my $local_showfile =
 1285:                             &Apache::lonnet::filelocation('',$showfile);
 1286:                         &Apache::lonnet::repcopy($local_showfile);
 1287:                     }
 1288:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1289:                 }
 1290:                 if ($imgfile) {
 1291:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1292:                         if ($imgfile =~ m{^/res/}) {
 1293:                             my $local_imgfile =
 1294:                                 &Apache::lonnet::filelocation('',$imgfile);
 1295:                             &Apache::lonnet::repcopy($local_imgfile);
 1296:                         }
 1297:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1298:                     } else {
 1299:                         $fullsize = $imgfile;
 1300:                     }
 1301:                 }
 1302:                 $datatable .= '<td>';
 1303:                 if ($img eq 'login') {
 1304:                     $datatable .= $login_hdr_pick;
 1305:                 } 
 1306:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1307:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1308:             } else {
 1309:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1310:                               &mt('Upload:');
 1311:             }
 1312:         } else {
 1313:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1314:                           &mt('Upload:');
 1315:         }
 1316:         if ($switchserver) {
 1317:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1318:         } else {
 1319:             if ($img ne 'login') { # suppress file selection for Log-in header
 1320:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1321:             }
 1322:         }
 1323:         $datatable .= '</td></tr>';
 1324:     }
 1325:     $itemcount ++;
 1326:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1327:     $datatable .= '<tr'.$css_class.'>'.
 1328:                   '<td>'.$choices->{'bgs'}.'</td>';
 1329:     my $bgs_def;
 1330:     foreach my $item (@{$bgs}) {
 1331:         if (!$is_custom->{$item}) {
 1332:             $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>';
 1333:         }
 1334:     }
 1335:     if ($bgs_def) {
 1336:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1337:     } else {
 1338:         $datatable .= '<td>&nbsp;</td>';
 1339:     }
 1340:     $datatable .= '<td class="LC_right_item">'.
 1341:                   '<table border="0"><tr>';
 1342:     foreach my $item (@{$bgs}) {
 1343:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'bgs'}{$item});
 1344:         $datatable .= '<td align="center">'.$link;
 1345:         if ($designs->{'bgs'}{$item}) {
 1346:             $datatable .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1347:         }
 1348:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'bgs'}{$item}.
 1349:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1350:     }
 1351:     $datatable .= '</tr></table></td></tr>';
 1352:     $itemcount ++;
 1353:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1354:     $datatable .= '<tr'.$css_class.'>'.
 1355:                   '<td>'.$choices->{'links'}.'</td>';
 1356:     my $links_def;
 1357:     foreach my $item (@{$links}) {
 1358:         if (!$is_custom->{$item}) {
 1359:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1360:         }
 1361:     }
 1362:     if ($links_def) {
 1363:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1364:     } else {
 1365:         $datatable .= '<td>&nbsp;</td>';
 1366:     }
 1367:     $datatable .= '<td class="LC_right_item">'.
 1368:                   '<table border="0"><tr>';
 1369:     foreach my $item (@{$links}) {
 1370:         $datatable .= '<td align="center">'."\n".
 1371:                       &color_pick($phase,$role,$item,$choices->{$item},
 1372:                                   $designs->{'links'}{$item});
 1373:         if ($designs->{'links'}{$item}) {
 1374:             $datatable.='&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'links'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1375:         }
 1376:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'links'}{$item}.
 1377:                       '" /></td>';
 1378:     }
 1379:     $$rowtotal += $itemcount;
 1380:     return $datatable;
 1381: }
 1382: 
 1383: sub logo_display_options {
 1384:     my ($img,$defaults,$designs) = @_;
 1385:     my $checkedon;
 1386:     if (ref($defaults) eq 'HASH') {
 1387:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1388:             if ($defaults->{'showlogo'}{$img}) {
 1389:                 $checkedon = 'checked="checked" ';     
 1390:             }
 1391:         } 
 1392:     }
 1393:     if (ref($designs) eq 'HASH') {
 1394:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1395:             if (defined($designs->{'showlogo'}{$img})) {
 1396:                 if ($designs->{'showlogo'}{$img} == 0) {
 1397:                     $checkedon = '';
 1398:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1399:                     $checkedon = 'checked="checked" ';
 1400:                 }
 1401:             }
 1402:         }
 1403:     }
 1404:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1405:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1406:            &mt('show').'</label>'."\n";
 1407: }
 1408: 
 1409: sub login_header_options  {
 1410:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1411:     my $output = '';
 1412:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1413:         $output .= &mt('Text default(s):').'<br />';
 1414:         if (!$is_custom->{'textcol'}) {
 1415:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1416:                        '&nbsp;&nbsp;&nbsp;';
 1417:         }
 1418:         if (!$is_custom->{'bgcol'}) {
 1419:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1420:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1421:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1422:         }
 1423:         $output .= '<br />';
 1424:     }
 1425:     $output .='<br />';
 1426:     return $output;
 1427: }
 1428: 
 1429: sub login_text_colors {
 1430:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1431:     my $color_menu = '<table border="0"><tr>';
 1432:     foreach my $item (@{$logintext}) {
 1433:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1434:         $color_menu .= '<td align="center">'.$link;
 1435:         if ($designs->{'logintext'}{$item}) {
 1436:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1437:         }
 1438:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1439:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1440:                        '<td>&nbsp;</td>';
 1441:     }
 1442:     $color_menu .= '</tr></table><br />';
 1443:     return $color_menu;
 1444: }
 1445: 
 1446: sub image_changes {
 1447:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1448:     my $output;
 1449:     if ($img eq 'login') {
 1450:             # suppress image for Log-in header
 1451:     } elsif (!$is_custom) {
 1452:         if ($img ne 'domlogo') {
 1453:             $output .= &mt('Default image:').'<br />';
 1454:         } else {
 1455:             $output .= &mt('Default in use:').'<br />';
 1456:         }
 1457:     }
 1458:     if ($img eq 'login') { # suppress image for Log-in header
 1459:         $output .= '<td>'.$logincolors;
 1460:     } else {
 1461:         if ($img_import) {
 1462:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1463:         }
 1464:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1465:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1466:         if ($is_custom) {
 1467:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1468:                        '<input type="checkbox" name="'.
 1469:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1470:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1471:         } else {
 1472:             $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1473:         }
 1474:     }
 1475:     return $output;
 1476: }
 1477: 
 1478: sub color_pick {
 1479:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1480:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1481:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1482:                ');">'.$desc.'</a>';
 1483:     return $link;
 1484: }
 1485: 
 1486: sub print_quotas {
 1487:     my ($dom,$settings,$rowtotal,$action) = @_;
 1488:     my $context;
 1489:     if ($action eq 'quotas') {
 1490:         $context = 'tools';
 1491:     } else {
 1492:         $context = $action;
 1493:     }
 1494:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 1495:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1496:     my $typecount = 0;
 1497:     my ($css_class,%titles);
 1498:     if ($context eq 'requestcourses') {
 1499:         @usertools = ('official','unofficial','community');
 1500:         @options =('norequest','approval','validate','autolimit');
 1501:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1502:         %titles = &courserequest_titles();
 1503:     } elsif ($context eq 'requestauthor') {
 1504:         @usertools = ('author');
 1505:         @options = ('norequest','approval','automatic');
 1506:         %titles = &authorrequest_titles(); 
 1507:     } else {
 1508:         @usertools = ('aboutme','blog','webdav','portfolio');
 1509:         %titles = &tool_titles();
 1510:     }
 1511:     if (ref($types) eq 'ARRAY') {
 1512:         foreach my $type (@{$types}) {
 1513:             my $currdefquota;
 1514:             unless (($context eq 'requestcourses') ||
 1515:                     ($context eq 'requestauthor')) {
 1516:                 if (ref($settings) eq 'HASH') {
 1517:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1518:                         $currdefquota = $settings->{defaultquota}->{$type}; 
 1519:                     } else {
 1520:                         $currdefquota = $settings->{$type};
 1521:                     }
 1522:                 }
 1523:             }
 1524:             if (defined($usertypes->{$type})) {
 1525:                 $typecount ++;
 1526:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1527:                 $datatable .= '<tr'.$css_class.'>'.
 1528:                               '<td>'.$usertypes->{$type}.'</td>'.
 1529:                               '<td class="LC_left_item">';
 1530:                 if ($context eq 'requestcourses') {
 1531:                     $datatable .= '<table><tr>';
 1532:                 }
 1533:                 my %cell;  
 1534:                 foreach my $item (@usertools) {
 1535:                     if ($context eq 'requestcourses') {
 1536:                         my ($curroption,$currlimit);
 1537:                         if (ref($settings) eq 'HASH') {
 1538:                             if (ref($settings->{$item}) eq 'HASH') {
 1539:                                 $curroption = $settings->{$item}->{$type};
 1540:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1541:                                     $currlimit = $1; 
 1542:                                 }
 1543:                             }
 1544:                         }
 1545:                         if (!$curroption) {
 1546:                             $curroption = 'norequest';
 1547:                         }
 1548:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1549:                         foreach my $option (@options) {
 1550:                             my $val = $option;
 1551:                             if ($option eq 'norequest') {
 1552:                                 $val = 0;  
 1553:                             }
 1554:                             if ($option eq 'validate') {
 1555:                                 my $canvalidate = 0;
 1556:                                 if (ref($validations{$item}) eq 'HASH') { 
 1557:                                     if ($validations{$item}{$type}) {
 1558:                                         $canvalidate = 1;
 1559:                                     }
 1560:                                 }
 1561:                                 next if (!$canvalidate);
 1562:                             }
 1563:                             my $checked = '';
 1564:                             if ($option eq $curroption) {
 1565:                                 $checked = ' checked="checked"';
 1566:                             } elsif ($option eq 'autolimit') {
 1567:                                 if ($curroption =~ /^autolimit/) {
 1568:                                     $checked = ' checked="checked"';
 1569:                                 }                       
 1570:                             } 
 1571:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1572:                                   '<input type="radio" name="crsreq_'.$item.
 1573:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1574:                                   $titles{$option}.'</label>';
 1575:                             if ($option eq 'autolimit') {
 1576:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1577:                                                 $item.'_limit_'.$type.'" size="1" '.
 1578:                                                 'value="'.$currlimit.'" />';
 1579:                             }
 1580:                             $cell{$item} .= '</span> ';
 1581:                             if ($option eq 'autolimit') {
 1582:                                 $cell{$item} .= $titles{'unlimited'};
 1583:                             }
 1584:                         }
 1585:                     } elsif ($context eq 'requestauthor') {
 1586:                         my $curroption;
 1587:                         if (ref($settings) eq 'HASH') {
 1588:                             $curroption = $settings->{$type};
 1589:                         }
 1590:                         if (!$curroption) {
 1591:                             $curroption = 'norequest';
 1592:                         }
 1593:                         foreach my $option (@options) {
 1594:                             my $val = $option;
 1595:                             if ($option eq 'norequest') {
 1596:                                 $val = 0;
 1597:                             }
 1598:                             my $checked = '';
 1599:                             if ($option eq $curroption) {
 1600:                                 $checked = ' checked="checked"';
 1601:                             }
 1602:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1603:                                   '<input type="radio" name="authorreq_'.$type.
 1604:                                   '" value="'.$val.'"'.$checked.' />'.
 1605:                                   $titles{$option}.'</label></span>&nbsp; ';
 1606:                         }
 1607:                     } else {
 1608:                         my $checked = 'checked="checked" ';
 1609:                         if (ref($settings) eq 'HASH') {
 1610:                             if (ref($settings->{$item}) eq 'HASH') {
 1611:                                 if ($settings->{$item}->{$type} == 0) {
 1612:                                     $checked = '';
 1613:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1614:                                     $checked =  'checked="checked" ';
 1615:                                 }
 1616:                             }
 1617:                         }
 1618:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1619:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1620:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1621:                                       '</label></span>&nbsp; ';
 1622:                     }
 1623:                 }
 1624:                 if ($context eq 'requestcourses') {
 1625:                     $datatable .= '</tr><tr>';
 1626:                     foreach my $item (@usertools) {
 1627:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1628:                     }
 1629:                     $datatable .= '</tr></table>';
 1630:                 }
 1631:                 $datatable .= '</td>';
 1632:                 unless (($context eq 'requestcourses') ||
 1633:                         ($context eq 'requestauthor')) {
 1634:                     $datatable .= 
 1635:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1636:                               '<input type="text" name="quota_'.$type.
 1637:                               '" value="'.$currdefquota.
 1638:                               '" size="5" /> Mb</span></td>';
 1639:                 }
 1640:                 $datatable .= '</tr>';
 1641:             }
 1642:         }
 1643:     }
 1644:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1645:         $defaultquota = '20';
 1646:         if (ref($settings) eq 'HASH') {
 1647:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1648:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1649:             } elsif (defined($settings->{'default'})) {
 1650:                 $defaultquota = $settings->{'default'};
 1651:             }
 1652:         }
 1653:     }
 1654:     $typecount ++;
 1655:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1656:     $datatable .= '<tr'.$css_class.'>'.
 1657:                   '<td>'.$othertitle.'</td>'.
 1658:                   '<td class="LC_left_item">';
 1659:     if ($context eq 'requestcourses') {
 1660:         $datatable .= '<table><tr>';
 1661:     }
 1662:     my %defcell;
 1663:     foreach my $item (@usertools) {
 1664:         if ($context eq 'requestcourses') {
 1665:             my ($curroption,$currlimit);
 1666:             if (ref($settings) eq 'HASH') {
 1667:                 if (ref($settings->{$item}) eq 'HASH') {
 1668:                     $curroption = $settings->{$item}->{'default'};
 1669:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1670:                         $currlimit = $1;
 1671:                     }
 1672:                 }
 1673:             }
 1674:             if (!$curroption) {
 1675:                 $curroption = 'norequest';
 1676:             }
 1677:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1678:             foreach my $option (@options) {
 1679:                 my $val = $option;
 1680:                 if ($option eq 'norequest') {
 1681:                     $val = 0;
 1682:                 }
 1683:                 if ($option eq 'validate') {
 1684:                     my $canvalidate = 0;
 1685:                     if (ref($validations{$item}) eq 'HASH') {
 1686:                         if ($validations{$item}{'default'}) {
 1687:                             $canvalidate = 1;
 1688:                         }
 1689:                     }
 1690:                     next if (!$canvalidate);
 1691:                 }
 1692:                 my $checked = '';
 1693:                 if ($option eq $curroption) {
 1694:                     $checked = ' checked="checked"';
 1695:                 } elsif ($option eq 'autolimit') {
 1696:                     if ($curroption =~ /^autolimit/) {
 1697:                         $checked = ' checked="checked"';
 1698:                     }
 1699:                 }
 1700:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1701:                                   '<input type="radio" name="crsreq_'.$item.
 1702:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1703:                                   $titles{$option}.'</label>';
 1704:                 if ($option eq 'autolimit') {
 1705:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1706:                                        $item.'_limit_default" size="1" '.
 1707:                                        'value="'.$currlimit.'" />';
 1708:                 }
 1709:                 $defcell{$item} .= '</span> ';
 1710:                 if ($option eq 'autolimit') {
 1711:                     $defcell{$item} .= $titles{'unlimited'};
 1712:                 }
 1713:             }
 1714:         } elsif ($context eq 'requestauthor') {
 1715:             my $curroption;
 1716:             if (ref($settings) eq 'HASH') {
 1717:                 $curroption = $settings->{'default'};
 1718:             }
 1719:             if (!$curroption) {
 1720:                 $curroption = 'norequest';
 1721:             }
 1722:             foreach my $option (@options) {
 1723:                 my $val = $option;
 1724:                 if ($option eq 'norequest') {
 1725:                     $val = 0;
 1726:                 }
 1727:                 my $checked = '';
 1728:                 if ($option eq $curroption) {
 1729:                     $checked = ' checked="checked"';
 1730:                 }
 1731:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1732:                               '<input type="radio" name="authorreq_default"'.
 1733:                               ' value="'.$val.'"'.$checked.' />'.
 1734:                               $titles{$option}.'</label></span>&nbsp; ';
 1735:             }
 1736:         } else {
 1737:             my $checked = 'checked="checked" ';
 1738:             if (ref($settings) eq 'HASH') {
 1739:                 if (ref($settings->{$item}) eq 'HASH') {
 1740:                     if ($settings->{$item}->{'default'} == 0) {
 1741:                         $checked = '';
 1742:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1743:                         $checked = 'checked="checked" ';
 1744:                     }
 1745:                 }
 1746:             }
 1747:             $datatable .= '<span class="LC_nobreak"><label>'.
 1748:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1749:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1750:                           '</label></span>&nbsp; ';
 1751:         }
 1752:     }
 1753:     if ($context eq 'requestcourses') {
 1754:         $datatable .= '</tr><tr>';
 1755:         foreach my $item (@usertools) {
 1756:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1757:         }
 1758:         $datatable .= '</tr></table>';
 1759:     }
 1760:     $datatable .= '</td>';
 1761:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1762:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1763:                       '<input type="text" name="defaultquota" value="'.
 1764:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1765:     }
 1766:     $datatable .= '</tr>';
 1767:     $typecount ++;
 1768:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1769:     $datatable .= '<tr'.$css_class.'>'.
 1770:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1771:     if ($context eq 'requestcourses') {
 1772:         $datatable .= &mt('(overrides affiliation, if set)').
 1773:                       '</td>'.
 1774:                       '<td class="LC_left_item">'.
 1775:                       '<table><tr>';
 1776:     } else {
 1777:         $datatable .= &mt('(overrides affiliation, if checked)').
 1778:                       '</td>'.
 1779:                       '<td class="LC_left_item" colspan="2">'.
 1780:                       '<br />';
 1781:     }
 1782:     my %advcell;
 1783:     foreach my $item (@usertools) {
 1784:         if ($context eq 'requestcourses') {
 1785:             my ($curroption,$currlimit);
 1786:             if (ref($settings) eq 'HASH') {
 1787:                 if (ref($settings->{$item}) eq 'HASH') {
 1788:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1789:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1790:                         $currlimit = $1;
 1791:                     }
 1792:                 }
 1793:             }
 1794:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1795:             my $checked = '';
 1796:             if ($curroption eq '') {
 1797:                 $checked = ' checked="checked"';
 1798:             }
 1799:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1800:                                '<input type="radio" name="crsreq_'.$item.
 1801:                                '__LC_adv" value=""'.$checked.' />'.
 1802:                                &mt('No override set').'</label></span>&nbsp; ';
 1803:             foreach my $option (@options) {
 1804:                 my $val = $option;
 1805:                 if ($option eq 'norequest') {
 1806:                     $val = 0;
 1807:                 }
 1808:                 if ($option eq 'validate') {
 1809:                     my $canvalidate = 0;
 1810:                     if (ref($validations{$item}) eq 'HASH') {
 1811:                         if ($validations{$item}{'_LC_adv'}) {
 1812:                             $canvalidate = 1;
 1813:                         }
 1814:                     }
 1815:                     next if (!$canvalidate);
 1816:                 }
 1817:                 my $checked = '';
 1818:                 if ($val eq $curroption) {
 1819:                     $checked = ' checked="checked"';
 1820:                 } elsif ($option eq 'autolimit') {
 1821:                     if ($curroption =~ /^autolimit/) {
 1822:                         $checked = ' checked="checked"';
 1823:                     }
 1824:                 }
 1825:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1826:                                   '<input type="radio" name="crsreq_'.$item.
 1827:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1828:                                   $titles{$option}.'</label>';
 1829:                 if ($option eq 'autolimit') {
 1830:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1831:                                        $item.'_limit__LC_adv" size="1" '.
 1832:                                        'value="'.$currlimit.'" />';
 1833:                 }
 1834:                 $advcell{$item} .= '</span> ';
 1835:                 if ($option eq 'autolimit') {
 1836:                     $advcell{$item} .= $titles{'unlimited'};
 1837:                 }
 1838:             }
 1839:         } elsif ($context eq 'requestauthor') {
 1840:             my $curroption;
 1841:             if (ref($settings) eq 'HASH') {
 1842:                 $curroption = $settings->{'_LC_adv'};
 1843:             }
 1844:             my $checked = '';
 1845:             if ($curroption eq '') {
 1846:                 $checked = ' checked="checked"';
 1847:             }
 1848:             $datatable .= '<span class="LC_nobreak"><label>'.
 1849:                           '<input type="radio" name="authorreq__LC_adv"'.
 1850:                           ' value=""'.$checked.' />'.
 1851:                           &mt('No override set').'</label></span>&nbsp; ';
 1852:             foreach my $option (@options) {
 1853:                 my $val = $option;
 1854:                 if ($option eq 'norequest') {
 1855:                     $val = 0;
 1856:                 }
 1857:                 my $checked = '';
 1858:                 if ($val eq $curroption) {
 1859:                     $checked = ' checked="checked"';
 1860:                 }
 1861:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1862:                               '<input type="radio" name="crsreq_'.$item.
 1863:                               '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1864:                               $titles{$option}.'</label></span>&nbsp; ';
 1865:             }
 1866:         } else {
 1867:             my $checked = 'checked="checked" ';
 1868:             if (ref($settings) eq 'HASH') {
 1869:                 if (ref($settings->{$item}) eq 'HASH') {
 1870:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1871:                         $checked = '';
 1872:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1873:                         $checked = 'checked="checked" ';
 1874:                     }
 1875:                 }
 1876:             }
 1877:             $datatable .= '<span class="LC_nobreak"><label>'.
 1878:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1879:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1880:                           '</label></span>&nbsp; ';
 1881:         }
 1882:     }
 1883:     if ($context eq 'requestcourses') {
 1884:         $datatable .= '</tr><tr>';
 1885:         foreach my $item (@usertools) {
 1886:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1887:         }
 1888:         $datatable .= '</tr></table>';
 1889:     }
 1890:     $datatable .= '</td></tr>';
 1891:     $$rowtotal += $typecount;
 1892:     return $datatable;
 1893: }
 1894: 
 1895: sub print_requestmail {
 1896:     my ($dom,$action,$settings,$rowtotal) = @_;
 1897:     my ($now,$datatable,%dompersonnel,@domcoord,@currapproval,$rows);
 1898:     $now = time;
 1899:     $rows = 0;
 1900:     %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 1901:     foreach my $server (keys(%dompersonnel)) {
 1902:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 1903:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 1904:             if (!grep(/^$uname:$udom$/,@domcoord)) {
 1905:                 push(@domcoord,$uname.':'.$udom);
 1906:             }
 1907:         }
 1908:     }
 1909:     if (ref($settings) eq 'HASH') {
 1910:         if (ref($settings->{'notify'}) eq 'HASH') {
 1911:             if ($settings->{'notify'}{'approval'} ne '') {
 1912:                @currapproval = split(',',$settings->{'notify'}{'approval'});
 1913:             }
 1914:         }
 1915:     }
 1916:     if (@currapproval) {
 1917:         foreach my $dc (@currapproval) {
 1918:             unless (grep(/^\Q$dc\E$/,@domcoord)) {
 1919:                 push(@domcoord,$dc);
 1920:             }
 1921:         }
 1922:     }
 1923:     @domcoord = sort(@domcoord);
 1924:     my $numinrow = 4;
 1925:     my $numdc = @domcoord;
 1926:     my $css_class = 'class="LC_odd_row"';
 1927:     my $text;
 1928:     if ($action eq 'requestcourses') {
 1929:         $text = &mt('Receive notification of course requests requiring approval');
 1930:     } else {
 1931:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1932:     }
 1933:     $datatable = '<tr '.$css_class.'>'.
 1934:                  ' <td>'.$text.'</td>'.
 1935:                  ' <td class="LC_left_item">';
 1936:     if (@domcoord > 0) {
 1937:         $datatable .= '<table>';
 1938:         for (my $i=0; $i<$numdc; $i++) {
 1939:             my $rem = $i%($numinrow);
 1940:             if ($rem == 0) {
 1941:                 if ($i > 0) {
 1942:                     $datatable .= '</tr>';
 1943:                 }
 1944:                 $datatable .= '<tr>';
 1945:                 $rows ++;
 1946:             }
 1947:             my $check = ' ';
 1948:             if (grep(/^\Q$domcoord[$i]\E$/,@currapproval)) {
 1949:                 $check = ' checked="checked" ';
 1950:             }
 1951:             my ($uname,$udom) = split(':',$domcoord[$i]);
 1952:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
 1953:             if ($i == $numdc-1) {
 1954:                 my $colsleft = $numinrow-$rem;
 1955:                 if ($colsleft > 1) {
 1956:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 1957:                 } else {
 1958:                     $datatable .= '<td class="LC_left_item">';
 1959:                 }
 1960:             } else {
 1961:                 $datatable .= '<td class="LC_left_item">';
 1962:             }
 1963:             $datatable .= '<span class="LC_nobreak"><label>'.
 1964:                           '<input type="checkbox" name="reqapprovalnotify" '.
 1965:                           'value="'.$domcoord[$i].'"'.$check.'/>'.
 1966:                           $fullname.'</label></span></td>';
 1967:         }
 1968:         $datatable .= '</tr></table>';
 1969:     } else {
 1970:         $datatable .= &mt('There are no active Domain Coordinators');
 1971:         $rows ++;
 1972:     }
 1973:     $datatable .='</td></tr>';
 1974:     $$rowtotal += $rows;
 1975:     return $datatable;
 1976: }
 1977: 
 1978: sub print_autoenroll {
 1979:     my ($dom,$settings,$rowtotal) = @_;
 1980:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1981:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1982:     if (ref($settings) eq 'HASH') {
 1983:         if (exists($settings->{'run'})) {
 1984:             if ($settings->{'run'} eq '0') {
 1985:                 $runoff = ' checked="checked" ';
 1986:                 $runon = ' ';
 1987:             } else {
 1988:                 $runon = ' checked="checked" ';
 1989:                 $runoff = ' ';
 1990:             }
 1991:         } else {
 1992:             if ($autorun) {
 1993:                 $runon = ' checked="checked" ';
 1994:                 $runoff = ' ';
 1995:             } else {
 1996:                 $runoff = ' checked="checked" ';
 1997:                 $runon = ' ';
 1998:             }
 1999:         }
 2000:         if (exists($settings->{'co-owners'})) {
 2001:             if ($settings->{'co-owners'} eq '0') {
 2002:                 $coownersoff = ' checked="checked" ';
 2003:                 $coownerson = ' ';
 2004:             } else {
 2005:                 $coownerson = ' checked="checked" ';
 2006:                 $coownersoff = ' ';
 2007:             }
 2008:         } else {
 2009:             $coownersoff = ' checked="checked" ';
 2010:             $coownerson = ' ';
 2011:         }
 2012:         if (exists($settings->{'sender_domain'})) {
 2013:             $defdom = $settings->{'sender_domain'};
 2014:         }
 2015:     } else {
 2016:         if ($autorun) {
 2017:             $runon = ' checked="checked" ';
 2018:             $runoff = ' ';
 2019:         } else {
 2020:             $runoff = ' checked="checked" ';
 2021:             $runon = ' ';
 2022:         }
 2023:     }
 2024:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2025:     my $notif_sender;
 2026:     if (ref($settings) eq 'HASH') {
 2027:         $notif_sender = $settings->{'sender_uname'};
 2028:     }
 2029:     my $datatable='<tr class="LC_odd_row">'.
 2030:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2031:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2032:                   '<input type="radio" name="autoenroll_run"'.
 2033:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2034:                   '<label><input type="radio" name="autoenroll_run"'.
 2035:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2036:                   '</tr><tr>'.
 2037:                   '<td>'.&mt('Notification messages - sender').
 2038:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2039:                   &mt('username').':&nbsp;'.
 2040:                   '<input type="text" name="sender_uname" value="'.
 2041:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2042:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2043:                   '<tr class="LC_odd_row">'.
 2044:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2045:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2046:                   '<input type="radio" name="autoassign_coowners"'.
 2047:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2048:                   '<label><input type="radio" name="autoassign_coowners"'.
 2049:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2050:                   '</tr>';
 2051:     $$rowtotal += 3;
 2052:     return $datatable;
 2053: }
 2054: 
 2055: sub print_autoupdate {
 2056:     my ($position,$dom,$settings,$rowtotal) = @_;
 2057:     my $datatable;
 2058:     if ($position eq 'top') {
 2059:         my $updateon = ' ';
 2060:         my $updateoff = ' checked="checked" ';
 2061:         my $classlistson = ' ';
 2062:         my $classlistsoff = ' checked="checked" ';
 2063:         if (ref($settings) eq 'HASH') {
 2064:             if ($settings->{'run'} eq '1') {
 2065:                 $updateon = $updateoff;
 2066:                 $updateoff = ' ';
 2067:             }
 2068:             if ($settings->{'classlists'} eq '1') {
 2069:                 $classlistson = $classlistsoff;
 2070:                 $classlistsoff = ' ';
 2071:             }
 2072:         }
 2073:         my %title = (
 2074:                    run => 'Auto-update active?',
 2075:                    classlists => 'Update information in classlists?',
 2076:                     );
 2077:         $datatable = '<tr class="LC_odd_row">'. 
 2078:                   '<td>'.&mt($title{'run'}).'</td>'.
 2079:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2080:                   '<input type="radio" name="autoupdate_run"'.
 2081:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2082:                   '<label><input type="radio" name="autoupdate_run"'.
 2083:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2084:                   '</tr><tr>'.
 2085:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2086:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2087:                   '<label><input type="radio" name="classlists"'.
 2088:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2089:                   '<label><input type="radio" name="classlists"'.
 2090:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2091:                   '</tr>';
 2092:         $$rowtotal += 2;
 2093:     } elsif ($position eq 'middle') {
 2094:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2095:         my $numinrow = 3;
 2096:         my $locknamesettings;
 2097:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2098:                                      $dom,$numinrow,$othertitle,
 2099:                                     'lockablenames');
 2100:         $$rowtotal ++;
 2101:     } else {
 2102:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2103:         my @fields = ('lastname','firstname','middlename','generation',
 2104:                       'permanentemail','id');
 2105:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2106:         my $numrows = 0;
 2107:         if (ref($types) eq 'ARRAY') {
 2108:             if (@{$types} > 0) {
 2109:                 $datatable = 
 2110:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2111:                                          \@fields,$types,\$numrows);
 2112:                     $$rowtotal += @{$types}; 
 2113:             }
 2114:         }
 2115:         $datatable .= 
 2116:             &usertype_update_row($settings,{'default' => $othertitle},
 2117:                                  \%fieldtitles,\@fields,['default'],
 2118:                                  \$numrows);
 2119:         $$rowtotal ++;     
 2120:     }
 2121:     return $datatable;
 2122: }
 2123: 
 2124: sub print_autocreate {
 2125:     my ($dom,$settings,$rowtotal) = @_;
 2126:     my (%createon,%createoff);
 2127:     my $curr_dc;
 2128:     my @types = ('xml','req');
 2129:     if (ref($settings) eq 'HASH') {
 2130:         foreach my $item (@types) {
 2131:             $createoff{$item} = ' checked="checked" ';
 2132:             $createon{$item} = ' ';
 2133:             if (exists($settings->{$item})) {
 2134:                 if ($settings->{$item}) {
 2135:                     $createon{$item} = ' checked="checked" ';
 2136:                     $createoff{$item} = ' ';
 2137:                 }
 2138:             }
 2139:         }
 2140:         $curr_dc = $settings->{'xmldc'};
 2141:     } else {
 2142:         foreach my $item (@types) {
 2143:             $createoff{$item} = ' checked="checked" ';
 2144:             $createon{$item} = ' ';
 2145:         }
 2146:     }
 2147:     $$rowtotal += 2;
 2148:     my $datatable='<tr class="LC_odd_row">'.
 2149:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2150:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2151:                   '<input type="radio" name="autocreate_xml"'.
 2152:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2153:                   '<label><input type="radio" name="autocreate_xml"'.
 2154:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2155:                   '</td></tr><tr>'.
 2156:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2157:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2158:                   '<input type="radio" name="autocreate_req"'.
 2159:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2160:                   '<label><input type="radio" name="autocreate_req"'.
 2161:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2162:     my ($numdc,$dctable) = &active_dc_picker($dom,$curr_dc);
 2163:     if ($numdc > 1) {
 2164:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2165:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2166:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2167:         $$rowtotal ++ ;
 2168:     } else {
 2169:         $datatable .= $dctable.'</td></tr>';
 2170:     }
 2171:     return $datatable;
 2172: }
 2173: 
 2174: sub print_directorysrch {
 2175:     my ($dom,$settings,$rowtotal) = @_;
 2176:     my $srchon = ' ';
 2177:     my $srchoff = ' checked="checked" ';
 2178:     my ($exacton,$containson,$beginson);
 2179:     my $localon = ' ';
 2180:     my $localoff = ' checked="checked" ';
 2181:     if (ref($settings) eq 'HASH') {
 2182:         if ($settings->{'available'} eq '1') {
 2183:             $srchon = $srchoff;
 2184:             $srchoff = ' ';
 2185:         }
 2186:         if ($settings->{'localonly'} eq '1') {
 2187:             $localon = $localoff;
 2188:             $localoff = ' ';
 2189:         }
 2190:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2191:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2192:                 if ($type eq 'exact') {
 2193:                     $exacton = ' checked="checked" ';
 2194:                 } elsif ($type eq 'contains') {
 2195:                     $containson = ' checked="checked" ';
 2196:                 } elsif ($type eq 'begins') {
 2197:                     $beginson = ' checked="checked" ';
 2198:                 }
 2199:             }
 2200:         } else {
 2201:             if ($settings->{'searchtypes'} eq 'exact') {
 2202:                 $exacton = ' checked="checked" ';
 2203:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2204:                 $containson = ' checked="checked" ';
 2205:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2206:                 $exacton = ' checked="checked" ';
 2207:                 $containson = ' checked="checked" ';
 2208:             }
 2209:         }
 2210:     }
 2211:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2212:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2213: 
 2214:     my $numinrow = 4;
 2215:     my $cansrchrow = 0;
 2216:     my $datatable='<tr class="LC_odd_row">'.
 2217:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2218:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2219:                   '<input type="radio" name="dirsrch_available"'.
 2220:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2221:                   '<label><input type="radio" name="dirsrch_available"'.
 2222:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2223:                   '</tr><tr>'.
 2224:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2225:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2226:                   '<input type="radio" name="dirsrch_localonly"'.
 2227:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2228:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2229:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2230:                   '</tr>';
 2231:     $$rowtotal += 2;
 2232:     if (ref($usertypes) eq 'HASH') {
 2233:         if (keys(%{$usertypes}) > 0) {
 2234:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2235:                                          $numinrow,$othertitle,'cansearch');
 2236:             $cansrchrow = 1;
 2237:         }
 2238:     }
 2239:     if ($cansrchrow) {
 2240:         $$rowtotal ++;
 2241:         $datatable .= '<tr>';
 2242:     } else {
 2243:         $datatable .= '<tr class="LC_odd_row">';
 2244:     }
 2245:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2246:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2247:     foreach my $title (@{$titleorder}) {
 2248:         if (defined($searchtitles->{$title})) {
 2249:             my $check = ' ';
 2250:             if (ref($settings) eq 'HASH') {
 2251:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2252:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2253:                         $check = ' checked="checked" ';
 2254:                     }
 2255:                 }
 2256:             }
 2257:             $datatable .= '<td class="LC_left_item">'.
 2258:                           '<span class="LC_nobreak"><label>'.
 2259:                           '<input type="checkbox" name="searchby" '.
 2260:                           'value="'.$title.'"'.$check.'/>'.
 2261:                           $searchtitles->{$title}.'</label></span></td>';
 2262:         }
 2263:     }
 2264:     $datatable .= '</tr></table></td></tr>';
 2265:     $$rowtotal ++;
 2266:     if ($cansrchrow) {
 2267:         $datatable .= '<tr class="LC_odd_row">';
 2268:     } else {
 2269:         $datatable .= '<tr>';
 2270:     }
 2271:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2272:                   '<td class="LC_left_item" colspan="2">'.
 2273:                   '<span class="LC_nobreak"><label>'.
 2274:                   '<input type="checkbox" name="searchtypes" '.
 2275:                   $exacton.' value="exact" />'.&mt('Exact match').
 2276:                   '</label>&nbsp;'.
 2277:                   '<label><input type="checkbox" name="searchtypes" '.
 2278:                   $beginson.' value="begins" />'.&mt('Begins with').
 2279:                   '</label>&nbsp;'.
 2280:                   '<label><input type="checkbox" name="searchtypes" '.
 2281:                   $containson.' value="contains" />'.&mt('Contains').
 2282:                   '</label></span></td></tr>';
 2283:     $$rowtotal ++;
 2284:     return $datatable;
 2285: }
 2286: 
 2287: sub print_contacts {
 2288:     my ($dom,$settings,$rowtotal) = @_;
 2289:     my $datatable;
 2290:     my @contacts = ('adminemail','supportemail');
 2291:     my (%checked,%to,%otheremails,%bccemails);
 2292:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2293:                     'requestsmail');
 2294:     foreach my $type (@mailings) {
 2295:         $otheremails{$type} = '';
 2296:     }
 2297:     $bccemails{'helpdeskmail'} = '';
 2298:     if (ref($settings) eq 'HASH') {
 2299:         foreach my $item (@contacts) {
 2300:             if (exists($settings->{$item})) {
 2301:                 $to{$item} = $settings->{$item};
 2302:             }
 2303:         }
 2304:         foreach my $type (@mailings) {
 2305:             if (exists($settings->{$type})) {
 2306:                 if (ref($settings->{$type}) eq 'HASH') {
 2307:                     foreach my $item (@contacts) {
 2308:                         if ($settings->{$type}{$item}) {
 2309:                             $checked{$type}{$item} = ' checked="checked" ';
 2310:                         }
 2311:                     }
 2312:                     $otheremails{$type} = $settings->{$type}{'others'};
 2313:                     if ($type eq 'helpdeskmail') {
 2314:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2315:                     }
 2316:                 }
 2317:             } elsif ($type eq 'lonstatusmail') {
 2318:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2319:             }
 2320:         }
 2321:     } else {
 2322:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2323:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2324:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2325:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2326:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2327:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2328:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2329:     }
 2330:     my ($titles,$short_titles) = &contact_titles();
 2331:     my $rownum = 0;
 2332:     my $css_class;
 2333:     foreach my $item (@contacts) {
 2334:         $rownum ++;
 2335:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2336:         $datatable .= '<tr'.$css_class.'>'. 
 2337:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2338:                   '</span></td><td class="LC_right_item">'.
 2339:                   '<input type="text" name="'.$item.'" value="'.
 2340:                   $to{$item}.'" /></td></tr>';
 2341:     }
 2342:     foreach my $type (@mailings) {
 2343:         $rownum ++;
 2344:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2345:         $datatable .= '<tr'.$css_class.'>'.
 2346:                       '<td><span class="LC_nobreak">'.
 2347:                       $titles->{$type}.': </span></td>'.
 2348:                       '<td class="LC_left_item">'.
 2349:                       '<span class="LC_nobreak">';
 2350:         foreach my $item (@contacts) {
 2351:             $datatable .= '<label>'.
 2352:                           '<input type="checkbox" name="'.$type.'"'.
 2353:                           $checked{$type}{$item}.
 2354:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2355:                           '</label>&nbsp;';
 2356:         }
 2357:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2358:                       '<input type="text" name="'.$type.'_others" '.
 2359:                       'value="'.$otheremails{$type}.'"  />';
 2360:         if ($type eq 'helpdeskmail') {
 2361:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2362:                           '<input type="text" name="'.$type.'_bcc" '.
 2363:                           'value="'.$bccemails{$type}.'"  />';
 2364:         }
 2365:         $datatable .= '</td></tr>'."\n";
 2366:     }
 2367:     $$rowtotal += $rownum;
 2368:     return $datatable;
 2369: }
 2370: 
 2371: sub print_helpsettings {
 2372:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2373:     my ($datatable,$itemcount);
 2374:     $itemcount = 1;
 2375:     my (%choices,%defaultchecked,@toggles);
 2376:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2377:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2378:                                  &mt('LON-CAPA bug tracker'),600,500));
 2379:     %defaultchecked = ('submitbugs' => 'on');
 2380:     @toggles = ('submitbugs',);
 2381: 
 2382:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2383:                                                  \%choices,$itemcount);
 2384:     return $datatable;
 2385: }
 2386: 
 2387: sub radiobutton_prefs {
 2388:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount) = @_;
 2389:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2390:                    (ref($choices) eq 'HASH'));
 2391: 
 2392:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2393: 
 2394:     foreach my $item (@{$toggles}) {
 2395:         if ($defaultchecked->{$item} eq 'on') {
 2396:             $checkedon{$item} = ' checked="checked" ';
 2397:             $checkedoff{$item} = ' ';
 2398:         } elsif ($defaultchecked->{$item} eq 'off') {
 2399:             $checkedoff{$item} = ' checked="checked" ';
 2400:             $checkedon{$item} = ' ';
 2401:         }
 2402:     }
 2403:     if (ref($settings) eq 'HASH') {
 2404:         foreach my $item (@{$toggles}) {
 2405:             if ($settings->{$item} eq '1') {
 2406:                 $checkedon{$item} =  ' checked="checked" ';
 2407:                 $checkedoff{$item} = ' ';
 2408:             } elsif ($settings->{$item} eq '0') {
 2409:                 $checkedoff{$item} =  ' checked="checked" ';
 2410:                 $checkedon{$item} = ' ';
 2411:             }
 2412:         }
 2413:     }
 2414:     foreach my $item (@{$toggles}) {
 2415:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2416:         $datatable .=
 2417:             '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices->{$item}.
 2418:             '</span></td>'.
 2419:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2420:             '<label><input type="radio" name="'.
 2421:             $item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').
 2422:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2423:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2424:             '</span></td>'.
 2425:             '</tr>';
 2426:         $itemcount ++;
 2427:     }
 2428:     return ($datatable,$itemcount);
 2429: }
 2430: 
 2431: sub print_coursedefaults {
 2432:     my ($position,$dom,$settings,$rowtotal) = @_;
 2433:     my ($css_class,$datatable);
 2434:     my $itemcount = 1;
 2435:     if ($position eq 'top') {
 2436:         my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2437:         %choices =
 2438:             &Apache::lonlocal::texthash (
 2439:                 canuse_pdfforms => 'Course/Community users can create/upload PDF forms',
 2440:         );
 2441:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2442:         @toggles = ('canuse_pdfforms',);
 2443:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2444:                                                  \%choices,$itemcount);
 2445:         $$rowtotal += $itemcount;
 2446:     } else {
 2447:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2448:         my %choices =
 2449:             &Apache::lonlocal::texthash (
 2450:                 anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2451:         );
 2452:         my $currdefresponder;
 2453:         if (ref($settings) eq 'HASH') {
 2454:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2455:         }
 2456:         if (!$currdefresponder) {
 2457:             $currdefresponder = 10;
 2458:         } elsif ($currdefresponder < 1) {
 2459:             $currdefresponder = 1;
 2460:         }
 2461:         $datatable .=
 2462:                '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices{'anonsurvey_threshold'}.
 2463:                 '</span></td>'.
 2464:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2465:                 '<input type="text" name="anonsurvey_threshold"'.
 2466:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2467:                 '</td></tr>';
 2468:     }
 2469:     return $datatable;
 2470: }
 2471: 
 2472: sub print_usersessions {
 2473:     my ($position,$dom,$settings,$rowtotal) = @_;
 2474:     my ($css_class,$datatable,%checked,%choices);
 2475:     my (%by_ip,%by_location,@intdoms);
 2476:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2477: 
 2478:     my @alldoms = &Apache::lonnet::all_domains();
 2479:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2480:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2481:     my %altids = &id_for_thisdom(%servers);
 2482:     my $itemcount = 1;
 2483:     if ($position eq 'top') {
 2484:         if (keys(%serverhomes) > 1) {
 2485:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2486:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2487:         } else {
 2488:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2489:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2490:         }
 2491:     } else {
 2492:         if (keys(%by_location) == 0) {
 2493:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2494:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2495:         } else {
 2496:             my %lt = &usersession_titles();
 2497:             my $numinrow = 5;
 2498:             my $prefix;
 2499:             my @types;
 2500:             if ($position eq 'bottom') {
 2501:                 $prefix = 'remote';
 2502:                 @types = ('version','excludedomain','includedomain');
 2503:             } else {
 2504:                 $prefix = 'hosted';
 2505:                 @types = ('excludedomain','includedomain');
 2506:             }
 2507:             my (%current,%checkedon,%checkedoff);
 2508:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2509:             my @locations = sort(keys(%by_location));
 2510:             foreach my $type (@types) {
 2511:                 $checkedon{$type} = '';
 2512:                 $checkedoff{$type} = ' checked="checked"';
 2513:             }
 2514:             if (ref($settings) eq 'HASH') {
 2515:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2516:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2517:                         $current{$key} = $settings->{$prefix}{$key};
 2518:                         if ($key eq 'version') {
 2519:                             if ($current{$key} ne '') {
 2520:                                 $checkedon{$key} = ' checked="checked"';
 2521:                                 $checkedoff{$key} = '';
 2522:                             }
 2523:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2524:                             $checkedon{$key} = ' checked="checked"';
 2525:                             $checkedoff{$key} = '';
 2526:                         }
 2527:                     }
 2528:                 }
 2529:             }
 2530:             foreach my $type (@types) {
 2531:                 next if ($type ne 'version' && !@locations);
 2532:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2533:                 $datatable .= '<tr'.$css_class.'>
 2534:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2535:                                <span class="LC_nobreak">&nbsp;
 2536:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2537:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2538:                 if ($type eq 'version') {
 2539:                     my $selector = '<select name="'.$prefix.'_version">';
 2540:                     foreach my $version (@lcversions) {
 2541:                         my $selected = '';
 2542:                         if ($current{'version'} eq $version) {
 2543:                             $selected = ' selected="selected"';
 2544:                         }
 2545:                         $selector .= ' <option value="'.$version.'"'.
 2546:                                      $selected.'>'.$version.'</option>';
 2547:                     }
 2548:                     $selector .= '</select> ';
 2549:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2550:                 } else {
 2551:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2552:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2553:                                  ' />'.('&nbsp;'x2).
 2554:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2555:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2556:                                  "\n".
 2557:                                  '</div><div><table>';
 2558:                     my $rem;
 2559:                     for (my $i=0; $i<@locations; $i++) {
 2560:                         my ($showloc,$value,$checkedtype);
 2561:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2562:                             my $ip = $by_location{$locations[$i]}->[0];
 2563:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2564:                                  $value = join(':',@{$by_ip{$ip}});
 2565:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2566:                                 if (ref($current{$type}) eq 'ARRAY') {
 2567:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2568:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2569:                                             $checkedtype = ' checked="checked"';
 2570:                                             last;
 2571:                                         }
 2572:                                     }
 2573:                                 }
 2574:                             }
 2575:                         }
 2576:                         $rem = $i%($numinrow);
 2577:                         if ($rem == 0) {
 2578:                             if ($i > 0) {
 2579:                                 $datatable .= '</tr>';
 2580:                             }
 2581:                             $datatable .= '<tr>';
 2582:                         }
 2583:                         $datatable .= '<td class="LC_left_item">'.
 2584:                                       '<span class="LC_nobreak"><label>'.
 2585:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2586:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2587:                                       '</label></span></td>';
 2588:                     }
 2589:                     $rem = @locations%($numinrow);
 2590:                     my $colsleft = $numinrow - $rem;
 2591:                     if ($colsleft > 1 ) {
 2592:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2593:                                       '&nbsp;</td>';
 2594:                     } elsif ($colsleft == 1) {
 2595:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2596:                     }
 2597:                     $datatable .= '</tr></table>';
 2598:                 }
 2599:                 $datatable .= '</td></tr>';
 2600:                 $itemcount ++;
 2601:             }
 2602:         }
 2603:     }
 2604:     $$rowtotal += $itemcount;
 2605:     return $datatable;
 2606: }
 2607: 
 2608: sub build_location_hashes {
 2609:     my ($intdoms,$by_ip,$by_location) = @_;
 2610:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2611:                   (ref($by_location) eq 'HASH')); 
 2612:     my %iphost = &Apache::lonnet::get_iphost();
 2613:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2614:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2615:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2616:         foreach my $id (@{$iphost{$primary_ip}}) {
 2617:             my $intdom = &Apache::lonnet::internet_dom($id);
 2618:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2619:                 push(@{$intdoms},$intdom);
 2620:             }
 2621:         }
 2622:     }
 2623:     foreach my $ip (keys(%iphost)) {
 2624:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2625:             foreach my $id (@{$iphost{$ip}}) {
 2626:                 my $location = &Apache::lonnet::internet_dom($id);
 2627:                 if ($location) {
 2628:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2629:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2630:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2631:                             push(@{$by_ip->{$ip}},$location);
 2632:                         }
 2633:                     } else {
 2634:                         $by_ip->{$ip} = [$location];
 2635:                     }
 2636:                 }
 2637:             }
 2638:         }
 2639:     }
 2640:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2641:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2642:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2643:             my $first = $by_ip->{$ip}->[0];
 2644:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2645:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2646:                     push(@{$by_location->{$first}},$ip);
 2647:                 }
 2648:             } else {
 2649:                 $by_location->{$first} = [$ip];
 2650:             }
 2651:         }
 2652:     }
 2653:     return;
 2654: }
 2655: 
 2656: sub current_offloads_to {
 2657:     my ($dom,$settings,$servers) = @_;
 2658:     my (%spareid,%otherdomconfigs);
 2659:     if (ref($servers) eq 'HASH') {
 2660:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2661:             my $gotspares;
 2662:             if (ref($settings) eq 'HASH') {
 2663:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2664:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2665:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2666:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2667:                         $gotspares = 1;
 2668:                     }
 2669:                 }
 2670:             }
 2671:             unless ($gotspares) {
 2672:                 my $gotspares;
 2673:                 my $serverhomeID =
 2674:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2675:                 my $serverhomedom =
 2676:                     &Apache::lonnet::host_domain($serverhomeID);
 2677:                 if ($serverhomedom ne $dom) {
 2678:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2679:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2680:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2681:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2682:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2683:                                 $gotspares = 1;
 2684:                             }
 2685:                         }
 2686:                     } else {
 2687:                         $otherdomconfigs{$serverhomedom} =
 2688:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2689:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2690:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2691:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2692:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2693:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2694:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2695:                                         $gotspares = 1;
 2696:                                     }
 2697:                                 }
 2698:                             }
 2699:                         }
 2700:                     }
 2701:                 }
 2702:             }
 2703:             unless ($gotspares) {
 2704:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2705:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2706:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2707:                } else {
 2708:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2709:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2710:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2711:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2712:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2713:                     } else {
 2714:                         my %what = (
 2715:                              spareid => 1,
 2716:                         );
 2717:                         my ($result,$returnhash) = 
 2718:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2719:                         if ($result eq 'ok') { 
 2720:                             if (ref($returnhash) eq 'HASH') {
 2721:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2722:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2723:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2724:                                 }
 2725:                             }
 2726:                         }
 2727:                     }
 2728:                 }
 2729:             }
 2730:         }
 2731:     }
 2732:     return %spareid;
 2733: }
 2734: 
 2735: sub spares_row {
 2736:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2737:     my $css_class;
 2738:     my $numinrow = 4;
 2739:     my $itemcount = 1;
 2740:     my $datatable;
 2741:     my %typetitles = &sparestype_titles();
 2742:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2743:         foreach my $server (sort(keys(%{$servers}))) {
 2744:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2745:             my ($othercontrol,$serverdom);
 2746:             if ($serverhome ne $server) {
 2747:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2748:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2749:             } else {
 2750:                 $serverdom = &Apache::lonnet::host_domain($server);
 2751:                 if ($serverdom ne $dom) {
 2752:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2753:                 }
 2754:             }
 2755:             next unless (ref($spareid->{$server}) eq 'HASH');
 2756:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2757:             $datatable .= '<tr'.$css_class.'>
 2758:                            <td rowspan="2">
 2759:                             <span class="LC_nobreak"><b>'.$server.'</b> when busy, offloads to:</span></td>'."\n";
 2760:             my (%current,%canselect);
 2761:             my @choices = 
 2762:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2763:             foreach my $type ('primary','default') {
 2764:                 if (ref($spareid->{$server}) eq 'HASH') {
 2765:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2766:                         my @spares = @{$spareid->{$server}{$type}};
 2767:                         if (@spares > 0) {
 2768:                             if ($othercontrol) {
 2769:                                 $current{$type} = join(', ',@spares);
 2770:                             } else {
 2771:                                 $current{$type} .= '<table>';
 2772:                                 my $numspares = scalar(@spares);
 2773:                                 for (my $i=0;  $i<@spares; $i++) {
 2774:                                     my $rem = $i%($numinrow);
 2775:                                     if ($rem == 0) {
 2776:                                         if ($i > 0) {
 2777:                                             $current{$type} .= '</tr>';
 2778:                                         }
 2779:                                         $current{$type} .= '<tr>';
 2780:                                     }
 2781:                                     $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;'.
 2782:                                                        $spareid->{$server}{$type}[$i].
 2783:                                                        '</label></td>'."\n";
 2784:                                 }
 2785:                                 my $rem = @spares%($numinrow);
 2786:                                 my $colsleft = $numinrow - $rem;
 2787:                                 if ($colsleft > 1 ) {
 2788:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2789:                                                        '" class="LC_left_item">'.
 2790:                                                        '&nbsp;</td>';
 2791:                                 } elsif ($colsleft == 1) {
 2792:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2793:                                 }
 2794:                                 $current{$type} .= '</tr></table>';
 2795:                             }
 2796:                         }
 2797:                     }
 2798:                     if ($current{$type} eq '') {
 2799:                         $current{$type} = &mt('None specified');
 2800:                     }
 2801:                     if ($othercontrol) {
 2802:                         if ($type eq 'primary') {
 2803:                             $canselect{$type} = $othercontrol;
 2804:                         }
 2805:                     } else {
 2806:                         $canselect{$type} = 
 2807:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2808:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2809:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2810:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2811:                         if (@choices > 0) {
 2812:                             foreach my $lonhost (@choices) {
 2813:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2814:                             }
 2815:                         }
 2816:                         $canselect{$type} .= '</select>'."\n";
 2817:                     }
 2818:                 } else {
 2819:                     $current{$type} = &mt('Could not be determined');
 2820:                     if ($type eq 'primary') {
 2821:                         $canselect{$type} =  $othercontrol;
 2822:                     }
 2823:                 }
 2824:                 if ($type eq 'default') {
 2825:                     $datatable .= '<tr'.$css_class.'>';
 2826:                 }
 2827:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2828:                               '<td>'.$current{$type}.'</td>'."\n".
 2829:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2830:             }
 2831:             $itemcount ++;
 2832:         }
 2833:     }
 2834:     $$rowtotal += $itemcount;
 2835:     return $datatable;
 2836: }
 2837: 
 2838: sub possible_newspares {
 2839:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2840:     my $serverhostname = &Apache::lonnet::hostname($server);
 2841:     my %excluded;
 2842:     if ($serverhostname ne '') {
 2843:         %excluded = (
 2844:                        $serverhostname => 1,
 2845:                     );
 2846:     }
 2847:     if (ref($currspares) eq 'HASH') {
 2848:         foreach my $type (keys(%{$currspares})) {
 2849:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2850:                 if (@{$currspares->{$type}} > 0) {
 2851:                     foreach my $curr (@{$currspares->{$type}}) {
 2852:                         my $hostname = &Apache::lonnet::hostname($curr);
 2853:                         $excluded{$hostname} = 1;
 2854:                     }
 2855:                 }
 2856:             }
 2857:         }
 2858:     }
 2859:     my @choices;
 2860:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2861:         if (keys(%{$serverhomes}) > 1) {
 2862:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2863:                 unless ($excluded{$name}) {
 2864:                     if (exists($altids->{$serverhomes->{$name}})) {
 2865:                         push(@choices,$altids->{$serverhomes->{$name}});
 2866:                     } else {
 2867:                         push(@choices,$serverhomes->{$name});
 2868:                     }
 2869:                 }
 2870:             }
 2871:         }
 2872:     }
 2873:     return sort(@choices);
 2874: }
 2875: 
 2876: sub print_loadbalancing {
 2877:     my ($dom,$settings,$rowtotal) = @_;
 2878:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2879:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2880:     my $numinrow = 1;
 2881:     my $datatable;
 2882:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2883:     my (%currbalancer,%currtargets,%currrules,%existing);
 2884:     if (ref($settings) eq 'HASH') {
 2885:         %existing = %{$settings};
 2886:     }
 2887:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 2888:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 2889:                                   \%currtargets,\%currrules);
 2890:     } else {
 2891:         return;
 2892:     }
 2893:     my ($othertitle,$usertypes,$types) =
 2894:         &Apache::loncommon::sorted_inst_types($dom);
 2895:     my $rownum = 6;
 2896:     if (ref($types) eq 'ARRAY') {
 2897:         $rownum += scalar(@{$types});
 2898:     }
 2899:     my @css_class = ('LC_odd_row','LC_even_row');
 2900:     my $balnum = 0;
 2901:     my $islast;
 2902:     my (@toshow,$disabledtext);
 2903:     if (keys(%currbalancer) > 0) {
 2904:         @toshow = sort(keys(%currbalancer));
 2905:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 2906:             push(@toshow,'');
 2907:         }
 2908:     } else {
 2909:         @toshow = ('');
 2910:         $disabledtext = &mt('No existing load balancer');
 2911:     }
 2912:     foreach my $lonhost (@toshow) {
 2913:         if ($balnum == scalar(@toshow)-1) {
 2914:             $islast = 1;
 2915:         } else {
 2916:             $islast = 0;
 2917:         }
 2918:         my $cssidx = $balnum%2;
 2919:         my $targets_div_style = 'display: none';
 2920:         my $disabled_div_style = 'display: block';
 2921:         my $homedom_div_style = 'display: none';
 2922:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 2923:                       '<td rowspan="'.$rownum.'" valign="top">'.
 2924:                       '<p>';
 2925:         if ($lonhost eq '') {
 2926:             $datatable .= '<span class="LC_nobreak">'; 
 2927:             if (keys(%currbalancer) > 0) {
 2928:                 $datatable .= &mt('Add balancer:');
 2929:             } else {
 2930:                 $datatable .= &mt('Enable balancer:');
 2931:             }
 2932:             $datatable .= '&nbsp;'.
 2933:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 2934:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 2935:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 2936:                           '<option value="" selected="selected">'.&mt('None').
 2937:                           '</option>'."\n";
 2938:             foreach my $server (sort(keys(%servers))) {
 2939:                 next if ($currbalancer{$server});
 2940:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 2941:             }
 2942:             $datatable .= 
 2943:                 '</select>'."\n".
 2944:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 2945:         } else {
 2946:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 2947:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 2948:                            &mt('Stop balancing').'</label>'.
 2949:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 2950:             $targets_div_style = 'display: block';
 2951:             $disabled_div_style = 'display: none';
 2952:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 2953:                 $homedom_div_style = 'display: block';
 2954:             }
 2955:         }
 2956:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 2957:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 2958:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 2959:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2960:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 2961:         my @sparestypes = ('primary','default');
 2962:         my %typetitles = &sparestype_titles();
 2963:         foreach my $sparetype (@sparestypes) {
 2964:             my $targettable;
 2965:             for (my $i=0; $i<$numspares; $i++) {
 2966:                 my $checked;
 2967:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 2968:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 2969:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 2970:                             $checked = ' checked="checked"';
 2971:                         }
 2972:                     }
 2973:                 }
 2974:                 my ($chkboxval,$disabled);
 2975:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 2976:                     $chkboxval = $spares[$i];
 2977:                 }
 2978:                 if (exists($currbalancer{$spares[$i]})) {
 2979:                     $disabled = ' disabled="disabled"';
 2980:                 }
 2981:                 $targettable .= 
 2982:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 2983:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 2984:                     '</span></label></td>';
 2985:                 my $rem = $i%($numinrow);
 2986:                 if ($rem == 0) {
 2987:                     if (($i > 0) && ($i < $numspares-1)) {
 2988:                         $targettable .= '</tr>';
 2989:                     }
 2990:                     if ($i < $numspares-1) {
 2991:                         $targettable .= '<tr>';
 2992:                     }
 2993:                 }
 2994:             }
 2995:             if ($targettable ne '') {
 2996:                 my $rem = $numspares%($numinrow);
 2997:                 my $colsleft = $numinrow - $rem;
 2998:                 if ($colsleft > 1 ) {
 2999:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3000:                                     '&nbsp;</td>';
 3001:                 } elsif ($colsleft == 1) {
 3002:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3003:                 }
 3004:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3005:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3006:             }
 3007:         }
 3008:         my $cssidx = $balnum%2; 
 3009:         $datatable .= '</div></td></tr>'.
 3010:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3011:                                            $othertitle,$usertypes,$types,\%servers,
 3012:                                            \%currbalancer,$lonhost,
 3013:                                            $targets_div_style,$homedom_div_style,
 3014:                                            $css_class[$cssidx],$balnum,$islast);
 3015:         $$rowtotal += $rownum;
 3016:         $balnum ++;
 3017:     }
 3018:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3019:     return $datatable;
 3020: }
 3021: 
 3022: sub get_loadbalancers_config {
 3023:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3024:     return unless ((ref($servers) eq 'HASH') &&
 3025:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3026:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3027:     if (keys(%{$existing}) > 0) {
 3028:         my $oldlonhost;
 3029:         foreach my $key (sort(keys(%{$existing}))) {
 3030:             if ($key eq 'lonhost') {
 3031:                 $oldlonhost = $existing->{'lonhost'};
 3032:                 $currbalancer->{$oldlonhost} = 1;
 3033:             } elsif ($key eq 'targets') {
 3034:                 if ($oldlonhost) {
 3035:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3036:                 }
 3037:             } elsif ($key eq 'rules') {
 3038:                 if ($oldlonhost) {
 3039:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3040:                 }
 3041:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3042:                 $currbalancer->{$key} = 1;
 3043:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3044:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3045:             }
 3046:         }
 3047:     } else {
 3048:         my ($balancerref,$targetsref) =
 3049:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3050:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3051:             foreach my $server (sort(keys(%{$balancerref}))) {
 3052:                 $currbalancer->{$server} = 1;
 3053:                 $currtargets->{$server} = $targetsref->{$server};
 3054:             }
 3055:         }
 3056:     }
 3057:     return;
 3058: }
 3059: 
 3060: sub loadbalancing_rules {
 3061:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3062:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3063:         $css_class,$balnum,$islast) = @_;
 3064:     my $output;
 3065:     my $num = 0;
 3066:     my ($alltypes,$othertypes,$titles) = 
 3067:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3068:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3069:         foreach my $type (@{$alltypes}) {
 3070:             $num ++;
 3071:             my $current;
 3072:             if (ref($currrules) eq 'HASH') {
 3073:                 $current = $currrules->{$type};
 3074:             }
 3075:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3076:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3077:                     $current = '';
 3078:                 }
 3079:             }
 3080:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3081:                                              $servers,$currbalancer,$lonhost,$dom,
 3082:                                              $targets_div_style,$homedom_div_style,
 3083:                                              $css_class,$balnum,$num,$islast);
 3084:         }
 3085:     }
 3086:     return $output;
 3087: }
 3088: 
 3089: sub loadbalancing_titles {
 3090:     my ($dom,$intdom,$usertypes,$types) = @_;
 3091:     my %othertypes = (
 3092:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3093:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3094:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3095:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3096:                      );
 3097:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 3098:     if (ref($types) eq 'ARRAY') {
 3099:         unshift(@alltypes,@{$types},'default');
 3100:     }
 3101:     my %titles;
 3102:     foreach my $type (@alltypes) {
 3103:         if ($type =~ /^_LC_/) {
 3104:             $titles{$type} = $othertypes{$type};
 3105:         } elsif ($type eq 'default') {
 3106:             $titles{$type} = &mt('All users from [_1]',$dom);
 3107:             if (ref($types) eq 'ARRAY') {
 3108:                 if (@{$types} > 0) {
 3109:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3110:                 }
 3111:             }
 3112:         } elsif (ref($usertypes) eq 'HASH') {
 3113:             $titles{$type} = $usertypes->{$type};
 3114:         }
 3115:     }
 3116:     return (\@alltypes,\%othertypes,\%titles);
 3117: }
 3118: 
 3119: sub loadbalance_rule_row {
 3120:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3121:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3122:     my @rulenames = ('default','homeserver');
 3123:     my %ruletitles = &offloadtype_text();
 3124:     if ($type eq '_LC_external') {
 3125:         push(@rulenames,'externalbalancer');
 3126:     } else {
 3127:         push(@rulenames,'specific');
 3128:     }
 3129:     push(@rulenames,'none');
 3130:     my $style = $targets_div_style;
 3131:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3132:         $style = $homedom_div_style;
 3133:     }
 3134:     my $space;
 3135:     if ($islast && $num == 1) {
 3136:         $space = '<div display="inline-block">&nbsp;</div>';
 3137:     }
 3138:     my $output = 
 3139:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3140:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3141:         '<td valaign="top">'.$space.
 3142:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3143:     for (my $i=0; $i<@rulenames; $i++) {
 3144:         my $rule = $rulenames[$i];
 3145:         my ($checked,$extra);
 3146:         if ($rulenames[$i] eq 'default') {
 3147:             $rule = '';
 3148:         }
 3149:         if ($rulenames[$i] eq 'specific') {
 3150:             if (ref($servers) eq 'HASH') {
 3151:                 my $default;
 3152:                 if (($current ne '') && (exists($servers->{$current}))) {
 3153:                     $checked = ' checked="checked"';
 3154:                 }
 3155:                 unless ($checked) {
 3156:                     $default = ' selected="selected"';
 3157:                 }
 3158:                 $extra = 
 3159:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3160:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3161:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3162:                     '<option value=""'.$default.'></option>'."\n";
 3163:                 foreach my $server (sort(keys(%{$servers}))) {
 3164:                     if (ref($currbalancer) eq 'HASH') {
 3165:                         next if (exists($currbalancer->{$server}));
 3166:                     }
 3167:                     my $selected;
 3168:                     if ($server eq $current) {
 3169:                         $selected = ' selected="selected"';
 3170:                     }
 3171:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3172:                 }
 3173:                 $extra .= '</select>';
 3174:             }
 3175:         } elsif ($rule eq $current) {
 3176:             $checked = ' checked="checked"';
 3177:         }
 3178:         $output .= '<span class="LC_nobreak"><label>'.
 3179:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3180:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3181:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3182:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3183:                    '</label>'.$extra.'</span><br />'."\n";
 3184:     }
 3185:     $output .= '</div></td></tr>'."\n";
 3186:     return $output;
 3187: }
 3188: 
 3189: sub offloadtype_text {
 3190:     my %ruletitles = &Apache::lonlocal::texthash (
 3191:            'default'          => 'Offloads to default destinations',
 3192:            'homeserver'       => "Offloads to user's home server",
 3193:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3194:            'specific'         => 'Offloads to specific server',
 3195:            'none'             => 'No offload',
 3196:     );
 3197:     return %ruletitles;
 3198: }
 3199: 
 3200: sub sparestype_titles {
 3201:     my %typestitles = &Apache::lonlocal::texthash (
 3202:                           'primary' => 'primary',
 3203:                           'default' => 'default',
 3204:                       );
 3205:     return %typestitles;
 3206: }
 3207: 
 3208: sub contact_titles {
 3209:     my %titles = &Apache::lonlocal::texthash (
 3210:                    'supportemail' => 'Support E-mail address',
 3211:                    'adminemail'   => 'Default Server Admin E-mail address',
 3212:                    'errormail'    => 'Error reports to be e-mailed to',
 3213:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3214:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3215:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3216:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3217:                  );
 3218:     my %short_titles = &Apache::lonlocal::texthash (
 3219:                            adminemail   => 'Admin E-mail address',
 3220:                            supportemail => 'Support E-mail',
 3221:                        );   
 3222:     return (\%titles,\%short_titles);
 3223: }
 3224: 
 3225: sub tool_titles {
 3226:     my %titles = &Apache::lonlocal::texthash (
 3227:                      aboutme    => 'Personal web page',
 3228:                      blog       => 'Blog',
 3229:                      webdav     => 'WebDAV',
 3230:                      portfolio  => 'Portfolio',
 3231:                      official   => 'Official courses (with institutional codes)',
 3232:                      unofficial => 'Unofficial courses',
 3233:                      community  => 'Communities',
 3234:                  );
 3235:     return %titles;
 3236: }
 3237: 
 3238: sub courserequest_titles {
 3239:     my %titles = &Apache::lonlocal::texthash (
 3240:                                    official   => 'Official',
 3241:                                    unofficial => 'Unofficial',
 3242:                                    community  => 'Communities',
 3243:                                    norequest  => 'Not allowed',
 3244:                                    approval   => 'Approval by Dom. Coord.',
 3245:                                    validate   => 'With validation',
 3246:                                    autolimit  => 'Numerical limit',
 3247:                                    unlimited  => '(blank for unlimited)',
 3248:                  );
 3249:     return %titles;
 3250: }
 3251: 
 3252: sub authorrequest_titles {
 3253:     my %titles = &Apache::lonlocal::texthash (
 3254:                                    norequest  => 'Not allowed',
 3255:                                    approval   => 'Approval by Dom. Coord.',
 3256:                                    automatic  => 'Automatic approval',
 3257:                  );
 3258:     return %titles;
 3259: } 
 3260: 
 3261: sub courserequest_conditions {
 3262:     my %conditions = &Apache::lonlocal::texthash (
 3263:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3264:        validate   => '(Processing of request subject to instittutional validation).',
 3265:                  );
 3266:     return %conditions;
 3267: }
 3268: 
 3269: 
 3270: sub print_usercreation {
 3271:     my ($position,$dom,$settings,$rowtotal) = @_;
 3272:     my $numinrow = 4;
 3273:     my $datatable;
 3274:     if ($position eq 'top') {
 3275:         $$rowtotal ++;
 3276:         my $rowcount = 0;
 3277:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3278:         if (ref($rules) eq 'HASH') {
 3279:             if (keys(%{$rules}) > 0) {
 3280:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3281:                                                 $ruleorder,$numinrow,$rowcount);
 3282:                 $$rowtotal ++;
 3283:                 $rowcount ++;
 3284:             }
 3285:         }
 3286:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3287:         if (ref($idrules) eq 'HASH') {
 3288:             if (keys(%{$idrules}) > 0) {
 3289:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3290:                                                 $idruleorder,$numinrow,$rowcount);
 3291:                 $$rowtotal ++;
 3292:                 $rowcount ++;
 3293:             }
 3294:         }
 3295:         my ($emailrules,$emailruleorder) = 
 3296:             &Apache::lonnet::inst_userrules($dom,'email');
 3297:         if (ref($emailrules) eq 'HASH') {
 3298:             if (keys(%{$emailrules}) > 0) {
 3299:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3300:                                                 $emailruleorder,$numinrow,$rowcount);
 3301:                 $$rowtotal ++;
 3302:                 $rowcount ++;
 3303:             }
 3304:         }
 3305:         if ($rowcount == 0) {
 3306:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3307:             $$rowtotal ++;
 3308:             $rowcount ++;
 3309:         }
 3310:     } elsif ($position eq 'middle') {
 3311:         my @creators = ('author','course','requestcrs','selfcreate');
 3312:         my ($rules,$ruleorder) =
 3313:             &Apache::lonnet::inst_userrules($dom,'username');
 3314:         my %lt = &usercreation_types();
 3315:         my %checked;
 3316:         my @selfcreate; 
 3317:         if (ref($settings) eq 'HASH') {
 3318:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3319:                 foreach my $item (@creators) {
 3320:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3321:                 }
 3322:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3323:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3324:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3325:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3326:                         @selfcreate = ('email','login','sso');
 3327:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3328:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3329:                     }
 3330:                 }
 3331:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3332:                 foreach my $item (@creators) {
 3333:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3334:                         $checked{$item} = 'none';
 3335:                     }
 3336:                 }
 3337:             }
 3338:         }
 3339:         my $rownum = 0;
 3340:         foreach my $item (@creators) {
 3341:             $rownum ++;
 3342:             if ($item ne 'selfcreate') {  
 3343:                 if ($checked{$item} eq '') {
 3344:                     $checked{$item} = 'any';
 3345:                 }
 3346:             }
 3347:             my $css_class;
 3348:             if ($rownum%2) {
 3349:                 $css_class = '';
 3350:             } else {
 3351:                 $css_class = ' class="LC_odd_row" ';
 3352:             }
 3353:             $datatable .= '<tr'.$css_class.'>'.
 3354:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3355:                          '</span></td><td align="right">';
 3356:             my @options;
 3357:             if ($item eq 'selfcreate') {
 3358:                 push(@options,('email','login','sso'));
 3359:             } else {
 3360:                 @options = ('any');
 3361:                 if (ref($rules) eq 'HASH') {
 3362:                     if (keys(%{$rules}) > 0) {
 3363:                         push(@options,('official','unofficial'));
 3364:                     }
 3365:                 }
 3366:                 push(@options,'none');
 3367:             }
 3368:             foreach my $option (@options) {
 3369:                 my $type = 'radio';
 3370:                 my $check = ' ';
 3371:                 if ($item eq 'selfcreate') {
 3372:                     $type = 'checkbox';
 3373:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3374:                         $check = ' checked="checked" ';
 3375:                     }
 3376:                 } else {
 3377:                     if ($checked{$item} eq $option) {
 3378:                         $check = ' checked="checked" ';
 3379:                     }
 3380:                 } 
 3381:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3382:                               '<input type="'.$type.'" name="can_createuser_'.
 3383:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3384:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3385:             }
 3386:             $datatable .= '</td></tr>';
 3387:         }
 3388:         my ($othertitle,$usertypes,$types) =
 3389:             &Apache::loncommon::sorted_inst_types($dom);
 3390:         my $createsettings;
 3391:         if (ref($settings) eq 'HASH') {
 3392:             $createsettings = $settings->{cancreate};
 3393:         }
 3394:         if (ref($usertypes) eq 'HASH') {
 3395:             if (keys(%{$usertypes}) > 0) {
 3396:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3397:                                              $dom,$numinrow,$othertitle,
 3398:                                              'statustocreate');
 3399:                 $$rowtotal ++;
 3400:                 $rownum ++;
 3401:             }
 3402:         }
 3403:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3404:     } else {
 3405:         my @contexts = ('author','course','domain');
 3406:         my @authtypes = ('int','krb4','krb5','loc');
 3407:         my %checked;
 3408:         if (ref($settings) eq 'HASH') {
 3409:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3410:                 foreach my $item (@contexts) {
 3411:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3412:                         foreach my $auth (@authtypes) {
 3413:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3414:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3415:                             }
 3416:                         }
 3417:                     }
 3418:                 }
 3419:             }
 3420:         } else {
 3421:             foreach my $item (@contexts) {
 3422:                 foreach my $auth (@authtypes) {
 3423:                     $checked{$item}{$auth} = ' checked="checked" ';
 3424:                 }
 3425:             }
 3426:         }
 3427:         my %title = &context_names();
 3428:         my %authname = &authtype_names();
 3429:         my $rownum = 0;
 3430:         my $css_class; 
 3431:         foreach my $item (@contexts) {
 3432:             if ($rownum%2) {
 3433:                 $css_class = '';
 3434:             } else {
 3435:                 $css_class = ' class="LC_odd_row" ';
 3436:             }
 3437:             $datatable .=   '<tr'.$css_class.'>'.
 3438:                             '<td>'.$title{$item}.
 3439:                             '</td><td class="LC_left_item">'.
 3440:                             '<span class="LC_nobreak">';
 3441:             foreach my $auth (@authtypes) {
 3442:                 $datatable .= '<label>'. 
 3443:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3444:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3445:                               $authname{$auth}.'</label>&nbsp;';
 3446:             }
 3447:             $datatable .= '</span></td></tr>';
 3448:             $rownum ++;
 3449:         }
 3450:         $$rowtotal += $rownum;
 3451:     }
 3452:     return $datatable;
 3453: }
 3454: 
 3455: sub captcha_choice {
 3456:     my ($context,$settings,$itemcount) = @_;
 3457:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3458:     my %lt = &captcha_phrases();
 3459:     $keyentry = 'hidden';
 3460:     if ($context eq 'cancreate') {
 3461:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3462:     } elsif ($context eq 'login') {
 3463:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3464:     }
 3465:     if (ref($settings) eq 'HASH') {
 3466:         if ($settings->{'captcha'}) {
 3467:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3468:         } else {
 3469:             $checked{'original'} = ' checked="checked"';
 3470:         }
 3471:         if ($settings->{'captcha'} eq 'recaptcha') {
 3472:             $pubtext = $lt{'pub'};
 3473:             $privtext = $lt{'priv'};
 3474:             $keyentry = 'text';
 3475:         }
 3476:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3477:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3478:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3479:         }
 3480:     } else {
 3481:         $checked{'original'} = ' checked="checked"';
 3482:     }
 3483:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3484:     my $output = '<tr'.$css_class.'>'.
 3485:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3486:                  '<table><tr><td>'."\n";
 3487:     foreach my $option ('original','recaptcha','notused') {
 3488:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3489:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3490:                    $lt{$option}.'</label></span>';
 3491:         unless ($option eq 'notused') {
 3492:             $output .= ('&nbsp;'x2)."\n";
 3493:         }
 3494:     }
 3495: #
 3496: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3497: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3498: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu) 
 3499: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3500: #  
 3501:     $output .= '</td></tr>'."\n".
 3502:                '<tr><td>'."\n".
 3503:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3504:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3505:                $currpub.'" size="40" /></span><br />'."\n".
 3506:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3507:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3508:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3509:                '</td></tr>';
 3510:     return $output;
 3511: }
 3512: 
 3513: sub user_formats_row {
 3514:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3515:     my $output;
 3516:     my %text = (
 3517:                    'username' => 'new usernames',
 3518:                    'id'       => 'IDs',
 3519:                    'email'    => 'self-created accounts (e-mail)',
 3520:                );
 3521:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3522:     $output = '<tr '.$css_class.'>'.
 3523:               '<td><span class="LC_nobreak">';
 3524:     if ($type eq 'email') {
 3525:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3526:     } else {
 3527:         $output .= &mt("Format rules to check for $text{$type}: ");
 3528:     }
 3529:     $output .= '</span></td>'.
 3530:                '<td class="LC_left_item" colspan="2"><table>';
 3531:     my $rem;
 3532:     if (ref($ruleorder) eq 'ARRAY') {
 3533:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3534:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3535:                 my $rem = $i%($numinrow);
 3536:                 if ($rem == 0) {
 3537:                     if ($i > 0) {
 3538:                         $output .= '</tr>';
 3539:                     }
 3540:                     $output .= '<tr>';
 3541:                 }
 3542:                 my $check = ' ';
 3543:                 if (ref($settings) eq 'HASH') {
 3544:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3545:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3546:                             $check = ' checked="checked" ';
 3547:                         }
 3548:                     }
 3549:                 }
 3550:                 $output .= '<td class="LC_left_item">'.
 3551:                            '<span class="LC_nobreak"><label>'.
 3552:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3553:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3554:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3555:             }
 3556:         }
 3557:         $rem = @{$ruleorder}%($numinrow);
 3558:     }
 3559:     my $colsleft = $numinrow - $rem;
 3560:     if ($colsleft > 1 ) {
 3561:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3562:                    '&nbsp;</td>';
 3563:     } elsif ($colsleft == 1) {
 3564:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3565:     }
 3566:     $output .= '</tr></table></td></tr>';
 3567:     return $output;
 3568: }
 3569: 
 3570: sub usercreation_types {
 3571:     my %lt = &Apache::lonlocal::texthash (
 3572:                     author     => 'When adding a co-author',
 3573:                     course     => 'When adding a user to a course',
 3574:                     requestcrs => 'When requesting a course',
 3575:                     selfcreate => 'User creates own account', 
 3576:                     any        => 'Any',
 3577:                     official   => 'Institutional only ',
 3578:                     unofficial => 'Non-institutional only',
 3579:                     email      => 'E-mail address',
 3580:                     login      => 'Institutional Login',
 3581:                     sso        => 'SSO', 
 3582:                     none       => 'None',
 3583:     );
 3584:     return %lt;
 3585: }
 3586: 
 3587: sub authtype_names {
 3588:     my %lt = &Apache::lonlocal::texthash(
 3589:                       int    => 'Internal',
 3590:                       krb4   => 'Kerberos 4',
 3591:                       krb5   => 'Kerberos 5',
 3592:                       loc    => 'Local',
 3593:                   );
 3594:     return %lt;
 3595: }
 3596: 
 3597: sub context_names {
 3598:     my %context_title = &Apache::lonlocal::texthash(
 3599:        author => 'Creating users when an Author',
 3600:        course => 'Creating users when in a course',
 3601:        domain => 'Creating users when a Domain Coordinator',
 3602:     );
 3603:     return %context_title;
 3604: }
 3605: 
 3606: sub print_usermodification {
 3607:     my ($position,$dom,$settings,$rowtotal) = @_;
 3608:     my $numinrow = 4;
 3609:     my ($context,$datatable,$rowcount);
 3610:     if ($position eq 'top') {
 3611:         $rowcount = 0;
 3612:         $context = 'author'; 
 3613:         foreach my $role ('ca','aa') {
 3614:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3615:                                                    $numinrow,$rowcount);
 3616:             $$rowtotal ++;
 3617:             $rowcount ++;
 3618:         }
 3619:     } elsif ($position eq 'middle') {
 3620:         $context = 'course';
 3621:         $rowcount = 0;
 3622:         foreach my $role ('st','ep','ta','in','cr') {
 3623:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3624:                                                    $numinrow,$rowcount);
 3625:             $$rowtotal ++;
 3626:             $rowcount ++;
 3627:         }
 3628:     } elsif ($position eq 'bottom') {
 3629:         $context = 'selfcreate';
 3630:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3631:         $usertypes->{'default'} = $othertitle;
 3632:         if (ref($types) eq 'ARRAY') {
 3633:             push(@{$types},'default');
 3634:             $usertypes->{'default'} = $othertitle;
 3635:             foreach my $status (@{$types}) {
 3636:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3637:                                                        $numinrow,$rowcount,$usertypes);
 3638:                 $$rowtotal ++;
 3639:                 $rowcount ++;
 3640:             }
 3641:         }
 3642:     }
 3643:     return $datatable;
 3644: }
 3645: 
 3646: sub print_defaults {
 3647:     my ($dom,$rowtotal) = @_;
 3648:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3649:                  'datelocale_def','portal_def');
 3650:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3651:     my $titles = &defaults_titles($dom);
 3652:     my $rownum = 0;
 3653:     my ($datatable,$css_class);
 3654:     foreach my $item (@items) {
 3655:         if ($rownum%2) {
 3656:             $css_class = '';
 3657:         } else {
 3658:             $css_class = ' class="LC_odd_row" ';
 3659:         }
 3660:         $datatable .= '<tr'.$css_class.'>'.
 3661:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3662:                   '</span></td><td class="LC_right_item">';
 3663:         if ($item eq 'auth_def') {
 3664:             my @authtypes = ('internal','krb4','krb5','localauth');
 3665:             my %shortauth = (
 3666:                              internal => 'int',
 3667:                              krb4 => 'krb4',
 3668:                              krb5 => 'krb5',
 3669:                              localauth  => 'loc'
 3670:                            );
 3671:             my %authnames = &authtype_names();
 3672:             foreach my $auth (@authtypes) {
 3673:                 my $checked = ' ';
 3674:                 if ($domdefaults{$item} eq $auth) {
 3675:                     $checked = ' checked="checked" ';
 3676:                 }
 3677:                 $datatable .= '<label><input type="radio" name="'.$item.
 3678:                               '" value="'.$auth.'"'.$checked.'/>'.
 3679:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3680:             }
 3681:         } elsif ($item eq 'timezone_def') {
 3682:             my $includeempty = 1;
 3683:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3684:         } elsif ($item eq 'datelocale_def') {
 3685:             my $includeempty = 1;
 3686:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3687:         } elsif ($item eq 'lang_def') {
 3688:             my %langchoices = &get_languages_hash();
 3689:             $langchoices{''} = 'No language preference';
 3690:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 3691:             $datatable .= &Apache::loncommon::select_form($domdefaults{$item},$item,
 3692:                                                           \%langchoices);
 3693:         } else {
 3694:             my $size;
 3695:             if ($item eq 'portal_def') {
 3696:                 $size = ' size="25"';
 3697:             }
 3698:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3699:                           $domdefaults{$item}.'"'.$size.' />';
 3700:         }
 3701:         $datatable .= '</td></tr>';
 3702:         $rownum ++;
 3703:     }
 3704:     $$rowtotal += $rownum;
 3705:     return $datatable;
 3706: }
 3707: 
 3708: sub get_languages_hash {
 3709:     my %langchoices;
 3710:     foreach my $id (&Apache::loncommon::languageids()) {
 3711:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 3712:         if ($code ne '') {
 3713:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 3714:         }
 3715:     }
 3716:     return %langchoices;
 3717: }
 3718: 
 3719: sub defaults_titles {
 3720:     my ($dom) = @_;
 3721:     my %titles = &Apache::lonlocal::texthash (
 3722:                    'auth_def'      => 'Default authentication type',
 3723:                    'auth_arg_def'  => 'Default authentication argument',
 3724:                    'lang_def'      => 'Default language',
 3725:                    'timezone_def'  => 'Default timezone',
 3726:                    'datelocale_def' => 'Default locale for dates',
 3727:                    'portal_def'     => 'Portal/Default URL',
 3728:                  );
 3729:     if ($dom) {
 3730:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3731:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3732:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3733:         $protocol = 'http' if ($protocol ne 'https');
 3734:         if ($uint_dom) {
 3735:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3736:                                          $uint_dom);
 3737:         }
 3738:     }
 3739:     return (\%titles);
 3740: }
 3741: 
 3742: sub print_scantronformat {
 3743:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3744:     my $itemcount = 1;
 3745:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3746:         %confhash);
 3747:     my $switchserver = &check_switchserver($dom,$confname);
 3748:     my %lt = &Apache::lonlocal::texthash (
 3749:                 default => 'Default bubblesheet format file error',
 3750:                 custom  => 'Custom bubblesheet format file error',
 3751:              );
 3752:     my %scantronfiles = (
 3753:         default => 'default.tab',
 3754:         custom => 'custom.tab',
 3755:     );
 3756:     foreach my $key (keys(%scantronfiles)) {
 3757:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3758:                               .$scantronfiles{$key};
 3759:     }
 3760:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3761:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3762:         if (!$switchserver) {
 3763:             my $servadm = $r->dir_config('lonAdmEMail');
 3764:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3765:             if ($configuserok eq 'ok') {
 3766:                 if ($author_ok eq 'ok') {
 3767:                     my %legacyfile = (
 3768:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3769:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3770:                     );
 3771:                     my %md5chk;
 3772:                     foreach my $type (keys(%legacyfile)) {
 3773:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3774:                         chomp($md5chk{$type});
 3775:                     }
 3776:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3777:                         foreach my $type (keys(%legacyfile)) {
 3778:                             ($scantronurls{$type},my $error) = 
 3779:                                 &legacy_scantronformat($r,$dom,$confname,
 3780:                                                  $type,$legacyfile{$type},
 3781:                                                  $scantronurls{$type},
 3782:                                                  $scantronfiles{$type});
 3783:                             if ($error ne '') {
 3784:                                 $error{$type} = $error;
 3785:                             }
 3786:                         }
 3787:                         if (keys(%error) == 0) {
 3788:                             $is_custom = 1;
 3789:                             $confhash{'scantron'}{'scantronformat'} = 
 3790:                                 $scantronurls{'custom'};
 3791:                             my $putresult = 
 3792:                                 &Apache::lonnet::put_dom('configuration',
 3793:                                                          \%confhash,$dom);
 3794:                             if ($putresult ne 'ok') {
 3795:                                 $error{'custom'} = 
 3796:                                     '<span class="LC_error">'.
 3797:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3798:                             }
 3799:                         }
 3800:                     } else {
 3801:                         ($scantronurls{'default'},my $error) =
 3802:                             &legacy_scantronformat($r,$dom,$confname,
 3803:                                           'default',$legacyfile{'default'},
 3804:                                           $scantronurls{'default'},
 3805:                                           $scantronfiles{'default'});
 3806:                         if ($error eq '') {
 3807:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3808:                             my $putresult =
 3809:                                 &Apache::lonnet::put_dom('configuration',
 3810:                                                          \%confhash,$dom);
 3811:                             if ($putresult ne 'ok') {
 3812:                                 $error{'default'} =
 3813:                                     '<span class="LC_error">'.
 3814:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3815:                             }
 3816:                         } else {
 3817:                             $error{'default'} = $error;
 3818:                         }
 3819:                     }
 3820:                 }
 3821:             }
 3822:         } else {
 3823:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3824:         }
 3825:     }
 3826:     if (ref($settings) eq 'HASH') {
 3827:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3828:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3829:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3830:                 $scantronurl = '';
 3831:             } else {
 3832:                 $scantronurl = $settings->{'scantronformat'};
 3833:             }
 3834:             $is_custom = 1;
 3835:         } else {
 3836:             $scantronurl = $scantronurls{'default'};
 3837:         }
 3838:     } else {
 3839:         if ($is_custom) {
 3840:             $scantronurl = $scantronurls{'custom'};
 3841:         } else {
 3842:             $scantronurl = $scantronurls{'default'};
 3843:         }
 3844:     }
 3845:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3846:     $datatable .= '<tr'.$css_class.'>';
 3847:     if (!$is_custom) {
 3848:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3849:                       '<span class="LC_nobreak">';
 3850:         if ($scantronurl) {
 3851:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3852:                           &mt('Default bubblesheet format file').'</a>';
 3853:         } else {
 3854:             $datatable = &mt('File unavailable for display');
 3855:         }
 3856:         $datatable .= '</span></td>';
 3857:         if (keys(%error) == 0) { 
 3858:             $datatable .= '<td valign="bottom">';
 3859:             if (!$switchserver) {
 3860:                 $datatable .= &mt('Upload:').'<br />';
 3861:             }
 3862:         } else {
 3863:             my $errorstr;
 3864:             foreach my $key (sort(keys(%error))) {
 3865:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3866:             }
 3867:             $datatable .= '<td>'.$errorstr;
 3868:         }
 3869:     } else {
 3870:         if (keys(%error) > 0) {
 3871:             my $errorstr;
 3872:             foreach my $key (sort(keys(%error))) {
 3873:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3874:             } 
 3875:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3876:         } elsif ($scantronurl) {
 3877:             $datatable .= '<td><span class="LC_nobreak">'.
 3878:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3879:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3880:                           '<input type="checkbox" name="scantronformat_del"'.
 3881:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3882:                           '<td><span class="LC_nobreak">&nbsp;'.
 3883:                           &mt('Replace:').'</span><br />';
 3884:         }
 3885:     }
 3886:     if (keys(%error) == 0) {
 3887:         if ($switchserver) {
 3888:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3889:         } else {
 3890:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3891:                          '<input type="file" name="scantronformat" /></span>';
 3892:         }
 3893:     }
 3894:     $datatable .= '</td></tr>';
 3895:     $$rowtotal ++;
 3896:     return $datatable;
 3897: }
 3898: 
 3899: sub legacy_scantronformat {
 3900:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3901:     my ($url,$error);
 3902:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3903:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3904:         (my $result,$url) =
 3905:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3906:                          '','',$newfile);
 3907:         if ($result ne 'ok') {
 3908:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3909:         }
 3910:     }
 3911:     return ($url,$error);
 3912: }
 3913: 
 3914: sub print_coursecategories {
 3915:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3916:     my $datatable;
 3917:     if ($position eq 'top') {
 3918:         my $toggle_cats_crs = ' ';
 3919:         my $toggle_cats_dom = ' checked="checked" ';
 3920:         my $can_cat_crs = ' ';
 3921:         my $can_cat_dom = ' checked="checked" ';
 3922:         my $toggle_catscomm_comm = ' ';
 3923:         my $toggle_catscomm_dom = ' checked="checked" ';
 3924:         my $can_catcomm_comm = ' ';
 3925:         my $can_catcomm_dom = ' checked="checked" ';
 3926: 
 3927:         if (ref($settings) eq 'HASH') {
 3928:             if ($settings->{'togglecats'} eq 'crs') {
 3929:                 $toggle_cats_crs = $toggle_cats_dom;
 3930:                 $toggle_cats_dom = ' ';
 3931:             }
 3932:             if ($settings->{'categorize'} eq 'crs') {
 3933:                 $can_cat_crs = $can_cat_dom;
 3934:                 $can_cat_dom = ' ';
 3935:             }
 3936:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3937:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3938:                 $toggle_catscomm_dom = ' ';
 3939:             }
 3940:             if ($settings->{'categorizecomm'} eq 'comm') {
 3941:                 $can_catcomm_comm = $can_catcomm_dom;
 3942:                 $can_catcomm_dom = ' ';
 3943:             }
 3944:         }
 3945:         my %title = &Apache::lonlocal::texthash (
 3946:                      togglecats     => 'Show/Hide a course in catalog',
 3947:                      togglecatscomm => 'Show/Hide a community in catalog',
 3948:                      categorize     => 'Assign a category to a course',
 3949:                      categorizecomm => 'Assign a category to a community',
 3950:                     );
 3951:         my %level = &Apache::lonlocal::texthash (
 3952:                      dom  => 'Set in Domain',
 3953:                      crs  => 'Set in Course',
 3954:                      comm => 'Set in Community',
 3955:                     );
 3956:         $datatable = '<tr class="LC_odd_row">'.
 3957:                   '<td>'.$title{'togglecats'}.'</td>'.
 3958:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3959:                   '<input type="radio" name="togglecats"'.
 3960:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3961:                   '<label><input type="radio" name="togglecats"'.
 3962:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3963:                   '</tr><tr>'.
 3964:                   '<td>'.$title{'categorize'}.'</td>'.
 3965:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3966:                   '<label><input type="radio" name="categorize"'.
 3967:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3968:                   '<label><input type="radio" name="categorize"'.
 3969:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3970:                   '</tr><tr class="LC_odd_row">'.
 3971:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 3972:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3973:                   '<input type="radio" name="togglecatscomm"'.
 3974:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3975:                   '<label><input type="radio" name="togglecatscomm"'.
 3976:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3977:                   '</tr><tr>'.
 3978:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 3979:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3980:                   '<label><input type="radio" name="categorizecomm"'.
 3981:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3982:                   '<label><input type="radio" name="categorizecomm"'.
 3983:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3984:                   '</tr>';
 3985:         $$rowtotal += 4;
 3986:     } else {
 3987:         my $css_class;
 3988:         my $itemcount = 1;
 3989:         my $cathash; 
 3990:         if (ref($settings) eq 'HASH') {
 3991:             $cathash = $settings->{'cats'};
 3992:         }
 3993:         if (ref($cathash) eq 'HASH') {
 3994:             my (@cats,@trails,%allitems,%idx,@jsarray);
 3995:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 3996:                                                    \%allitems,\%idx,\@jsarray);
 3997:             my $maxdepth = scalar(@cats);
 3998:             my $colattrib = '';
 3999:             if ($maxdepth > 2) {
 4000:                 $colattrib = ' colspan="2" ';
 4001:             }
 4002:             my @path;
 4003:             if (@cats > 0) {
 4004:                 if (ref($cats[0]) eq 'ARRAY') {
 4005:                     my $numtop = @{$cats[0]};
 4006:                     my $maxnum = $numtop;
 4007:                     my %default_names = (
 4008:                           instcode    => &mt('Official courses'),
 4009:                           communities => &mt('Communities'),
 4010:                     );
 4011: 
 4012:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4013:                         ($cathash->{'instcode::0'} eq '') ||
 4014:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4015:                         ($cathash->{'communities::0'} eq '')) {
 4016:                         $maxnum ++;
 4017:                     }
 4018:                     my $lastidx;
 4019:                     for (my $i=0; $i<$numtop; $i++) {
 4020:                         my $parent = $cats[0][$i];
 4021:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4022:                         my $item = &escape($parent).'::0';
 4023:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4024:                         $lastidx = $idx{$item};
 4025:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4026:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4027:                         for (my $k=0; $k<=$maxnum; $k++) {
 4028:                             my $vpos = $k+1;
 4029:                             my $selstr;
 4030:                             if ($k == $i) {
 4031:                                 $selstr = ' selected="selected" ';
 4032:                             }
 4033:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4034:                         }
 4035:                         $datatable .= '</select></td><td>';
 4036:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4037:                             $datatable .=  '<span class="LC_nobreak">'
 4038:                                            .$default_names{$parent}.'</span>';
 4039:                             if ($parent eq 'instcode') {
 4040:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4041:                                               .&mt('with institutional codes')
 4042:                                               .')</span></td><td'.$colattrib.'>';
 4043:                             } else {
 4044:                                 $datatable .= '<table><tr><td>';
 4045:                             }
 4046:                             $datatable .= '<span class="LC_nobreak">'
 4047:                                           .'<label><input type="radio" name="'
 4048:                                           .$parent.'" value="1" checked="checked" />'
 4049:                                           .&mt('Display').'</label>';
 4050:                             if ($parent eq 'instcode') {
 4051:                                 $datatable .= '&nbsp;';
 4052:                             } else {
 4053:                                 $datatable .= '</span></td></tr><tr><td>'
 4054:                                               .'<span class="LC_nobreak">';
 4055:                             }
 4056:                             $datatable .= '<label><input type="radio" name="'
 4057:                                           .$parent.'" value="0" />'
 4058:                                           .&mt('Do not display').'</label></span>';
 4059:                             if ($parent eq 'communities') {
 4060:                                 $datatable .= '</td></tr></table>';
 4061:                             }
 4062:                             $datatable .= '</td>';
 4063:                         } else {
 4064:                             $datatable .= $parent
 4065:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 4066:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4067:                         }
 4068:                         my $depth = 1;
 4069:                         push(@path,$parent);
 4070:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4071:                         pop(@path);
 4072:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4073:                         $itemcount ++;
 4074:                     }
 4075:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4076:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4077:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4078:                     for (my $k=0; $k<=$maxnum; $k++) {
 4079:                         my $vpos = $k+1;
 4080:                         my $selstr;
 4081:                         if ($k == $numtop) {
 4082:                             $selstr = ' selected="selected" ';
 4083:                         }
 4084:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4085:                     }
 4086:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4087:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4088:                                   .'</tr>'."\n";
 4089:                     $itemcount ++;
 4090:                     foreach my $default ('instcode','communities') {
 4091:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4092:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4093:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4094:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4095:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4096:                             for (my $k=0; $k<=$maxnum; $k++) {
 4097:                                 my $vpos = $k+1;
 4098:                                 my $selstr;
 4099:                                 if ($k == $maxnum) {
 4100:                                     $selstr = ' selected="selected" ';
 4101:                                 }
 4102:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4103:                             }
 4104:                             $datatable .= '</select></span></td>'.
 4105:                                           '<td><span class="LC_nobreak">'.
 4106:                                           $default_names{$default}.'</span>';
 4107:                             if ($default eq 'instcode') {
 4108:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4109:                                               .&mt('with institutional codes').')</span>';
 4110:                             }
 4111:                             $datatable .= '</td>'
 4112:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4113:                                           .&mt('Display').'</label>&nbsp;'
 4114:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4115:                                           .&mt('Do not display').'</label></span></td></tr>';
 4116:                         }
 4117:                     }
 4118:                 }
 4119:             } else {
 4120:                 $datatable .= &initialize_categories($itemcount);
 4121:             }
 4122:         } else {
 4123:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4124:                           .&initialize_categories($itemcount);
 4125:         }
 4126:         $$rowtotal += $itemcount;
 4127:     }
 4128:     return $datatable;
 4129: }
 4130: 
 4131: sub print_serverstatuses {
 4132:     my ($dom,$settings,$rowtotal) = @_;
 4133:     my $datatable;
 4134:     my @pages = &serverstatus_pages();
 4135:     my (%namedaccess,%machineaccess);
 4136:     foreach my $type (@pages) {
 4137:         $namedaccess{$type} = '';
 4138:         $machineaccess{$type}= '';
 4139:     }
 4140:     if (ref($settings) eq 'HASH') {
 4141:         foreach my $type (@pages) {
 4142:             if (exists($settings->{$type})) {
 4143:                 if (ref($settings->{$type}) eq 'HASH') {
 4144:                     foreach my $key (keys(%{$settings->{$type}})) {
 4145:                         if ($key eq 'namedusers') {
 4146:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4147:                         } elsif ($key eq 'machines') {
 4148:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4149:                         }
 4150:                     }
 4151:                 }
 4152:             }
 4153:         }
 4154:     }
 4155:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4156:     my $rownum = 0;
 4157:     my $css_class;
 4158:     foreach my $type (@pages) {
 4159:         $rownum ++;
 4160:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4161:         $datatable .= '<tr'.$css_class.'>'.
 4162:                       '<td><span class="LC_nobreak">'.
 4163:                       $titles->{$type}.'</span></td>'.
 4164:                       '<td class="LC_left_item">'.
 4165:                       '<input type="text" name="'.$type.'_namedusers" '.
 4166:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4167:                       '<td class="LC_right_item">'.
 4168:                       '<span class="LC_nobreak">'.
 4169:                       '<input type="text" name="'.$type.'_machines" '.
 4170:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4171:                       '</td></tr>'."\n";
 4172:     }
 4173:     $$rowtotal += $rownum;
 4174:     return $datatable;
 4175: }
 4176: 
 4177: sub serverstatus_pages {
 4178:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4179:             'clusterstatus','metadata_keywords','metadata_harvest',
 4180:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4181: }
 4182: 
 4183: sub coursecategories_javascript {
 4184:     my ($settings) = @_;
 4185:     my ($output,$jstext,$cathash);
 4186:     if (ref($settings) eq 'HASH') {
 4187:         $cathash = $settings->{'cats'};
 4188:     }
 4189:     if (ref($cathash) eq 'HASH') {
 4190:         my (@cats,@jsarray,%idx);
 4191:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4192:         if (@jsarray > 0) {
 4193:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4194:             for (my $i=0; $i<@jsarray; $i++) {
 4195:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4196:                     my $catstr = join('","',@{$jsarray[$i]});
 4197:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4198:                 }
 4199:             }
 4200:         }
 4201:     } else {
 4202:         $jstext  = '    var categories = Array(1);'."\n".
 4203:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4204:     }
 4205:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4206:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4207:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4208:     $output = <<"ENDSCRIPT";
 4209: <script type="text/javascript">
 4210: // <![CDATA[
 4211: function reorderCats(form,parent,item,idx) {
 4212:     var changedVal;
 4213: $jstext
 4214:     var newpos = 'addcategory_pos';
 4215:     var current = new Array;
 4216:     if (parent == '') {
 4217:         var has_instcode = 0;
 4218:         var maxtop = categories[idx].length;
 4219:         for (var j=0; j<maxtop; j++) {
 4220:             if (categories[idx][j] == 'instcode::0') {
 4221:                 has_instcode == 1;
 4222:             }
 4223:         }
 4224:         if (has_instcode == 0) {
 4225:             categories[idx][maxtop] = 'instcode_pos';
 4226:         }
 4227:     } else {
 4228:         newpos += '_'+parent;
 4229:     }
 4230:     var maxh = 1 + categories[idx].length;
 4231:     var current = new Array;
 4232:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4233:     if (item == newpos) {
 4234:         changedVal = newitemVal;
 4235:     } else {
 4236:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4237:         current[newitemVal] = newpos;
 4238:     }
 4239:     for (var i=0; i<categories[idx].length; i++) {
 4240:         var elementName = categories[idx][i];
 4241:         if (elementName != item) {
 4242:             if (form.elements[elementName]) {
 4243:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4244:                 current[currVal] = elementName;
 4245:             }
 4246:         }
 4247:     }
 4248:     var oldVal;
 4249:     for (var j=0; j<maxh; j++) {
 4250:         if (current[j] == undefined) {
 4251:             oldVal = j;
 4252:         }
 4253:     }
 4254:     if (oldVal < changedVal) {
 4255:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4256:            var elementName = current[k];
 4257:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4258:         }
 4259:     } else {
 4260:         for (var k=changedVal; k<oldVal; k++) {
 4261:             var elementName = current[k];
 4262:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4263:         }
 4264:     }
 4265:     return;
 4266: }
 4267: 
 4268: function categoryCheck(form) {
 4269:     if (form.elements['addcategory_name'].value == 'instcode') {
 4270:         alert('$instcode_reserved\\n$choose_again');
 4271:         return false;
 4272:     }
 4273:     if (form.elements['addcategory_name'].value == 'communities') {
 4274:         alert('$communities_reserved\\n$choose_again');
 4275:         return false;
 4276:     }
 4277:     return true;
 4278: }
 4279: 
 4280: // ]]>
 4281: </script>
 4282: 
 4283: ENDSCRIPT
 4284:     return $output;
 4285: }
 4286: 
 4287: sub initialize_categories {
 4288:     my ($itemcount) = @_;
 4289:     my ($datatable,$css_class,$chgstr);
 4290:     my %default_names = (
 4291:                       instcode    => 'Official courses (with institutional codes)',
 4292:                       communities => 'Communities',
 4293:                         );
 4294:     my $select0 = ' selected="selected"';
 4295:     my $select1 = '';
 4296:     foreach my $default ('instcode','communities') {
 4297:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4298:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4299:         if ($default eq 'communities') {
 4300:             $select1 = $select0;
 4301:             $select0 = '';
 4302:         }
 4303:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4304:                      .'<select name="'.$default.'_pos">'
 4305:                      .'<option value="0"'.$select0.'>1</option>'
 4306:                      .'<option value="1"'.$select1.'>2</option>'
 4307:                      .'<option value="2">3</option></select>&nbsp;'
 4308:                      .$default_names{$default}
 4309:                      .'</span></td><td><span class="LC_nobreak">'
 4310:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4311:                      .&mt('Display').'</label>&nbsp;<label>'
 4312:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4313:                  .'</label></span></td></tr>';
 4314:         $itemcount ++;
 4315:     }
 4316:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4317:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4318:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4319:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4320:                   .'<option value="0">1</option>'
 4321:                   .'<option value="1">2</option>'
 4322:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4323:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4324:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4325:     return $datatable;
 4326: }
 4327: 
 4328: sub build_category_rows {
 4329:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4330:     my ($text,$name,$item,$chgstr);
 4331:     if (ref($cats) eq 'ARRAY') {
 4332:         my $maxdepth = scalar(@{$cats});
 4333:         if (ref($cats->[$depth]) eq 'HASH') {
 4334:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4335:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4336:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4337:                 $text .= '<td><table class="LC_datatable">';
 4338:                 my ($idxnum,$parent_name,$parent_item);
 4339:                 my $higher = $depth - 1;
 4340:                 if ($higher == 0) {
 4341:                     $parent_name = &escape($parent).'::'.$higher;
 4342:                 } else {
 4343:                     if (ref($path) eq 'ARRAY') {
 4344:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4345:                     }
 4346:                 }
 4347:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4348:                 for (my $j=0; $j<=$numchildren; $j++) {
 4349:                     if ($j < $numchildren) {
 4350:                         $name = $cats->[$depth]{$parent}[$j];
 4351:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4352:                         $idxnum = $idx->{$item};
 4353:                     } else {
 4354:                         $name = $parent_name;
 4355:                         $item = $parent_item;
 4356:                     }
 4357:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4358:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4359:                     for (my $i=0; $i<=$numchildren; $i++) {
 4360:                         my $vpos = $i+1;
 4361:                         my $selstr;
 4362:                         if ($j == $i) {
 4363:                             $selstr = ' selected="selected" ';
 4364:                         }
 4365:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4366:                     }
 4367:                     $text .= '</select>&nbsp;';
 4368:                     if ($j < $numchildren) {
 4369:                         my $deeper = $depth+1;
 4370:                         $text .= $name.'&nbsp;'
 4371:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4372:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4373:                         if(ref($path) eq 'ARRAY') {
 4374:                             push(@{$path},$name);
 4375:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4376:                             pop(@{$path});
 4377:                         }
 4378:                     } else {
 4379:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4380:                         if ($j == $numchildren) {
 4381:                             $text .= $name;
 4382:                         } else {
 4383:                             $text .= $item;
 4384:                         }
 4385:                         $text .= '" value="" />';
 4386:                     }
 4387:                     $text .= '</td></tr>';
 4388:                 }
 4389:                 $text .= '</table></td>';
 4390:             } else {
 4391:                 my $higher = $depth-1;
 4392:                 if ($higher == 0) {
 4393:                     $name = &escape($parent).'::'.$higher;
 4394:                 } else {
 4395:                     if (ref($path) eq 'ARRAY') {
 4396:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4397:                     }
 4398:                 }
 4399:                 my $colspan;
 4400:                 if ($parent ne 'instcode') {
 4401:                     $colspan = $maxdepth - $depth - 1;
 4402:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4403:                 }
 4404:             }
 4405:         }
 4406:     }
 4407:     return $text;
 4408: }
 4409: 
 4410: sub modifiable_userdata_row {
 4411:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4412:     my $rolename;
 4413:     if ($context eq 'selfcreate') {
 4414:         if (ref($usertypes) eq 'HASH') {
 4415:             $rolename = $usertypes->{$role};
 4416:         } else {
 4417:             $rolename = $role;
 4418:         }
 4419:     } else {
 4420:         if ($role eq 'cr') {
 4421:             $rolename = &mt('Custom role');
 4422:         } else {
 4423:             $rolename = &Apache::lonnet::plaintext($role);
 4424:         }
 4425:     }
 4426:     my @fields = ('lastname','firstname','middlename','generation',
 4427:                   'permanentemail','id');
 4428:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4429:     my $output;
 4430:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4431:     $output = '<tr '.$css_class.'>'.
 4432:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4433:               '<td class="LC_left_item" colspan="2"><table>';
 4434:     my $rem;
 4435:     my %checks;
 4436:     if (ref($settings) eq 'HASH') {
 4437:         if (ref($settings->{$context}) eq 'HASH') {
 4438:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4439:                 foreach my $field (@fields) {
 4440:                     if ($settings->{$context}->{$role}->{$field}) {
 4441:                         $checks{$field} = ' checked="checked" ';
 4442:                     }
 4443:                 }
 4444:             }
 4445:         }
 4446:     }
 4447:     for (my $i=0; $i<@fields; $i++) {
 4448:         my $rem = $i%($numinrow);
 4449:         if ($rem == 0) {
 4450:             if ($i > 0) {
 4451:                 $output .= '</tr>';
 4452:             }
 4453:             $output .= '<tr>';
 4454:         }
 4455:         my $check = ' ';
 4456:         if (exists($checks{$fields[$i]})) {
 4457:             $check = $checks{$fields[$i]}
 4458:         } else {
 4459:             if ($role eq 'st') {
 4460:                 if (ref($settings) ne 'HASH') {
 4461:                     $check = ' checked="checked" '; 
 4462:                 }
 4463:             }
 4464:         }
 4465:         $output .= '<td class="LC_left_item">'.
 4466:                    '<span class="LC_nobreak"><label>'.
 4467:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4468:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4469:                    '</label></span></td>';
 4470:         $rem = @fields%($numinrow);
 4471:     }
 4472:     my $colsleft = $numinrow - $rem;
 4473:     if ($colsleft > 1 ) {
 4474:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4475:                    '&nbsp;</td>';
 4476:     } elsif ($colsleft == 1) {
 4477:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4478:     }
 4479:     $output .= '</tr></table></td></tr>';
 4480:     return $output;
 4481: }
 4482: 
 4483: sub insttypes_row {
 4484:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4485:     my %lt = &Apache::lonlocal::texthash (
 4486:                       cansearch => 'Users allowed to search',
 4487:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4488:                       lockablenames => 'User preference to lock name',
 4489:              );
 4490:     my $showdom;
 4491:     if ($context eq 'cansearch') {
 4492:         $showdom = ' ('.$dom.')';
 4493:     }
 4494:     my $class = 'LC_left_item';
 4495:     if ($context eq 'statustocreate') {
 4496:         $class = 'LC_right_item';
 4497:     }
 4498:     my $output =  '<tr class="LC_odd_row">'.
 4499:                   '<td>'.$lt{$context}.$showdom.
 4500:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4501:     my $rem;
 4502:     if (ref($types) eq 'ARRAY') {
 4503:         for (my $i=0; $i<@{$types}; $i++) {
 4504:             if (defined($usertypes->{$types->[$i]})) {
 4505:                 my $rem = $i%($numinrow);
 4506:                 if ($rem == 0) {
 4507:                     if ($i > 0) {
 4508:                         $output .= '</tr>';
 4509:                     }
 4510:                     $output .= '<tr>';
 4511:                 }
 4512:                 my $check = ' ';
 4513:                 if (ref($settings) eq 'HASH') {
 4514:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4515:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4516:                             $check = ' checked="checked" ';
 4517:                         }
 4518:                     } elsif ($context eq 'statustocreate') {
 4519:                         $check = ' checked="checked" ';
 4520:                     }
 4521:                 }
 4522:                 $output .= '<td class="LC_left_item">'.
 4523:                            '<span class="LC_nobreak"><label>'.
 4524:                            '<input type="checkbox" name="'.$context.'" '.
 4525:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4526:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4527:             }
 4528:         }
 4529:         $rem = @{$types}%($numinrow);
 4530:     }
 4531:     my $colsleft = $numinrow - $rem;
 4532:     if (($rem == 0) && (@{$types} > 0)) {
 4533:         $output .= '<tr>';
 4534:     }
 4535:     if ($colsleft > 1) {
 4536:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4537:     } else {
 4538:         $output .= '<td class="LC_left_item">';
 4539:     }
 4540:     my $defcheck = ' ';
 4541:     if (ref($settings) eq 'HASH') {  
 4542:         if (ref($settings->{$context}) eq 'ARRAY') {
 4543:             if (grep(/^default$/,@{$settings->{$context}})) {
 4544:                 $defcheck = ' checked="checked" ';
 4545:             }
 4546:         } elsif ($context eq 'statustocreate') {
 4547:             $defcheck = ' checked="checked" ';
 4548:         }
 4549:     }
 4550:     $output .= '<span class="LC_nobreak"><label>'.
 4551:                '<input type="checkbox" name="'.$context.'" '.
 4552:                'value="default"'.$defcheck.'/>'.
 4553:                $othertitle.'</label></span></td>'.
 4554:                '</tr></table></td></tr>';
 4555:     return $output;
 4556: }
 4557: 
 4558: sub sorted_searchtitles {
 4559:     my %searchtitles = &Apache::lonlocal::texthash(
 4560:                          'uname' => 'username',
 4561:                          'lastname' => 'last name',
 4562:                          'lastfirst' => 'last name, first name',
 4563:                      );
 4564:     my @titleorder = ('uname','lastname','lastfirst');
 4565:     return (\%searchtitles,\@titleorder);
 4566: }
 4567: 
 4568: sub sorted_searchtypes {
 4569:     my %srchtypes_desc = (
 4570:                            exact    => 'is exact match',
 4571:                            contains => 'contains ..',
 4572:                            begins   => 'begins with ..',
 4573:                          );
 4574:     my @srchtypeorder = ('exact','begins','contains');
 4575:     return (\%srchtypes_desc,\@srchtypeorder);
 4576: }
 4577: 
 4578: sub usertype_update_row {
 4579:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4580:     my $datatable;
 4581:     my $numinrow = 4;
 4582:     foreach my $type (@{$types}) {
 4583:         if (defined($usertypes->{$type})) {
 4584:             $$rownums ++;
 4585:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4586:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4587:                           '</td><td class="LC_left_item"><table>';
 4588:             for (my $i=0; $i<@{$fields}; $i++) {
 4589:                 my $rem = $i%($numinrow);
 4590:                 if ($rem == 0) {
 4591:                     if ($i > 0) {
 4592:                         $datatable .= '</tr>';
 4593:                     }
 4594:                     $datatable .= '<tr>';
 4595:                 }
 4596:                 my $check = ' ';
 4597:                 if (ref($settings) eq 'HASH') {
 4598:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4599:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4600:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4601:                                 $check = ' checked="checked" ';
 4602:                             }
 4603:                         }
 4604:                     }
 4605:                 }
 4606: 
 4607:                 if ($i == @{$fields}-1) {
 4608:                     my $colsleft = $numinrow - $rem;
 4609:                     if ($colsleft > 1) {
 4610:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4611:                     } else {
 4612:                         $datatable .= '<td>';
 4613:                     }
 4614:                 } else {
 4615:                     $datatable .= '<td>';
 4616:                 }
 4617:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4618:                               '<input type="checkbox" name="updateable_'.$type.
 4619:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4620:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4621:             }
 4622:             $datatable .= '</tr></table></td></tr>';
 4623:         }
 4624:     }
 4625:     return $datatable;
 4626: }
 4627: 
 4628: sub modify_login {
 4629:     my ($r,$dom,$confname,%domconfig) = @_;
 4630:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4631:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4632:     %title = ( coursecatalog => 'Display course catalog',
 4633:                adminmail => 'Display administrator E-mail address',
 4634:                newuser => 'Link for visitors to create a user account',
 4635:                loginheader => 'Log-in box header');
 4636:     @offon = ('off','on');
 4637:     if (ref($domconfig{login}) eq 'HASH') {
 4638:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4639:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4640:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4641:             }
 4642:         }
 4643:     }
 4644:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4645:                                            \%domconfig,\%loginhash);
 4646:     my @toggles = ('coursecatalog','adminmail','newuser');
 4647:     foreach my $item (@toggles) {
 4648:         $loginhash{login}{$item} = $env{'form.'.$item};
 4649:     }
 4650:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4651:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4652:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4653:                                          \%loginhash);
 4654:     }
 4655: 
 4656:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4657:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4658:     if (keys(%servers) > 1) {
 4659:         foreach my $lonhost (keys(%servers)) {
 4660:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4661:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4662:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4663:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4664:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4665:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4666:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4667:                         $changes{'loginvia'}{$lonhost} = 1;
 4668:                     } else {
 4669:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4670:                         $changes{'loginvia'}{$lonhost} = 1;
 4671:                     }
 4672:                 } else {
 4673:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4674:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4675:                         $changes{'loginvia'}{$lonhost} = 1;
 4676:                     }
 4677:                 }
 4678:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4679:                     foreach my $item (@loginvia_attribs) {
 4680:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4681:                     }
 4682:                 } else {
 4683:                     foreach my $item (@loginvia_attribs) {
 4684:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4685:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4686:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4687:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4688:                                 $new = '/';
 4689:                             }
 4690:                         }
 4691:                         if (($item eq 'custompath') && 
 4692:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4693:                             $new = '';
 4694:                         }
 4695:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4696:                             $changes{'loginvia'}{$lonhost} = 1;
 4697:                         }
 4698:                         if ($item eq 'exempt') {
 4699:                             $new =~ s/^\s+//;
 4700:                             $new =~ s/\s+$//;
 4701:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4702:                             my @okips;
 4703:                             foreach my $ip (@poss_ips) {
 4704:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4705:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4706:                                         push(@okips,$ip); 
 4707:                                     }
 4708:                                 }
 4709:                             }
 4710:                             if (@okips > 0) {
 4711:                                 $new = join(',',@okips); 
 4712:                             } else {
 4713:                                 $new = ''; 
 4714:                             }
 4715:                         }
 4716:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4717:                     }
 4718:                 }
 4719:             } else {
 4720:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4721:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4722:                     $changes{'loginvia'}{$lonhost} = 1;
 4723:                     foreach my $item (@loginvia_attribs) {
 4724:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4725:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4726:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4727:                                 $new = '/';
 4728:                             }
 4729:                         }
 4730:                         if (($item eq 'custompath') && 
 4731:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4732:                             $new = '';
 4733:                         }
 4734:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4735:                     }
 4736:                 }
 4737:             }
 4738:         }
 4739:     }
 4740: 
 4741:     my $servadm = $r->dir_config('lonAdmEMail');
 4742:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 4743:     if (ref($domconfig{'login'}) eq 'HASH') {
 4744:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 4745:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 4746:                 if ($lang eq 'nolang') {
 4747:                     push(@currlangs,$lang);
 4748:                 } elsif (defined($langchoices{$lang})) {
 4749:                     push(@currlangs,$lang);
 4750:                 } else {
 4751:                     next;
 4752:                 }
 4753:             }
 4754:         }
 4755:     }
 4756:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 4757:     if (@currlangs > 0) {
 4758:         foreach my $lang (@currlangs) {
 4759:             if (grep(/^\Q$lang\E$/,@delurls)) {
 4760:                 $changes{'helpurl'}{$lang} = 1;
 4761:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 4762:                 $changes{'helpurl'}{$lang} = 1;
 4763:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 4764:                 push(@newlangs,$lang);
 4765:             } else {
 4766:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4767:             }
 4768:         }
 4769:     }
 4770:     unless (grep(/^nolang$/,@currlangs)) {
 4771:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 4772:             $changes{'helpurl'}{'nolang'} = 1;
 4773:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 4774:             push(@newlangs,'nolang');
 4775:         }
 4776:     }
 4777:     if ($env{'form.loginhelpurl_add_lang'}) {
 4778:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 4779:             ($env{'form.loginhelpurl_add_file.filename'})) {
 4780:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 4781:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 4782:         }
 4783:     }
 4784:     if ((@newlangs > 0) || ($addedfile)) {
 4785:         my $error;
 4786:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 4787:         if ($configuserok eq 'ok') {
 4788:             if ($switchserver) {
 4789:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 4790:             } elsif ($author_ok eq 'ok') {
 4791:                 my @allnew = @newlangs;
 4792:                 if ($addedfile ne '') {
 4793:                     push(@allnew,$addedfile);
 4794:                 }
 4795:                 foreach my $lang (@allnew) {
 4796:                     my $formelem = 'loginhelpurl_'.$lang;
 4797:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 4798:                         $formelem = 'loginhelpurl_add_file';
 4799:                     }
 4800:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 4801:                                                                "help/$lang",'','',$newfile{$lang});
 4802:                     if ($result eq 'ok') {
 4803:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 4804:                         $changes{'helpurl'}{$lang} = 1;
 4805:                     } else {
 4806:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 4807:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 4808:                         if ((grep(/^\Q$lang\E$/,@currlangs)) && 
 4809:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 4810: 
 4811:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4812:                         }
 4813:                     }
 4814:                 }
 4815:             } else {
 4816:                 $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);
 4817:             }
 4818:         } else {
 4819:             $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);
 4820:         }
 4821:         if ($error) {
 4822:             &Apache::lonnet::logthis($error);
 4823:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4824:         }
 4825:     }
 4826:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 4827: 
 4828:     my $defaulthelpfile = '/adm/loginproblems.html';
 4829:     my $defaulttext = &mt('Default in use');
 4830: 
 4831:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4832:                                              $dom);
 4833:     if ($putresult eq 'ok') {
 4834:         my @toggles = ('coursecatalog','adminmail','newuser');
 4835:         my %defaultchecked = (
 4836:                     'coursecatalog' => 'on',
 4837:                     'adminmail'     => 'off',
 4838:                     'newuser'       => 'off',
 4839:         );
 4840:         if (ref($domconfig{'login'}) eq 'HASH') {
 4841:             foreach my $item (@toggles) {
 4842:                 if ($defaultchecked{$item} eq 'on') { 
 4843:                     if (($domconfig{'login'}{$item} eq '0') &&
 4844:                         ($env{'form.'.$item} eq '1')) {
 4845:                         $changes{$item} = 1;
 4846:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4847:                               $domconfig{'login'}{$item} eq '1') &&
 4848:                              ($env{'form.'.$item} eq '0')) {
 4849:                         $changes{$item} = 1;
 4850:                     }
 4851:                 } elsif ($defaultchecked{$item} eq 'off') {
 4852:                     if (($domconfig{'login'}{$item} eq '1') &&
 4853:                         ($env{'form.'.$item} eq '0')) {
 4854:                         $changes{$item} = 1;
 4855:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4856:                               $domconfig{'login'}{$item} eq '0') &&
 4857:                              ($env{'form.'.$item} eq '1')) {
 4858:                         $changes{$item} = 1;
 4859:                     }
 4860:                 }
 4861:             }
 4862:         }
 4863:         if (keys(%changes) > 0 || $colchgtext) {
 4864:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4865:             $resulttext = &mt('Changes made:').'<ul>';
 4866:             foreach my $item (sort(keys(%changes))) {
 4867:                 if ($item eq 'loginvia') {
 4868:                     if (ref($changes{$item}) eq 'HASH') {
 4869:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4870:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4871:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4872:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4873:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4874:                                     $protocol = 'http' if ($protocol ne 'https');
 4875:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4876: 
 4877:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4878:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4879:                                     } else {
 4880:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4881:                                     }
 4882:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4883:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4884:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4885:                                     }
 4886:                                     $resulttext .= '</li>';
 4887:                                 } else {
 4888:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4889:                                 }
 4890:                             } else {
 4891:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4892:                             }
 4893:                         }
 4894:                         $resulttext .= '</ul></li>';
 4895:                     }
 4896:                 } elsif ($item eq 'helpurl') {
 4897:                     if (ref($changes{$item}) eq 'HASH') {
 4898:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 4899:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 4900:                                 my ($chg,$link);
 4901:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 4902:                                 if ($lang eq 'nolang') {
 4903:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 4904:                                 } else {
 4905:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 4906:                                 }
 4907:                                 $resulttext .= '<li>'.$chg.'</li>';
 4908:                             } else {
 4909:                                 my $chg;
 4910:                                 if ($lang eq 'nolang') {
 4911:                                     $chg = &mt('custom log-in help file for no preferred language');
 4912:                                 } else {
 4913:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 4914:                                 }
 4915:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 4916:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 4917:                                                       '?inhibitmenu=yes',$chg,600,500).
 4918:                                                '</li>';
 4919:                             }
 4920:                         }
 4921:                     }
 4922:                 } elsif ($item eq 'captcha') {
 4923:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4924:                         my $chgtxt; 
 4925:                         if ($loginhash{'login'}{$item} eq 'notused') {
 4926:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 4927:                         } else {
 4928:                             my %captchas = &captcha_phrases();
 4929:                             if ($captchas{$loginhash{'login'}{$item}}) {
 4930:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 4931:                             } else {
 4932:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 4933:                             }
 4934:                         }
 4935:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4936:                     }
 4937:                 } elsif ($item eq 'recaptchakeys') {
 4938:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4939:                         my ($privkey,$pubkey);
 4940:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 4941:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 4942:                             $privkey = $loginhash{'login'}{$item}{'private'};
 4943:                         }
 4944:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 4945:                         if (!$pubkey) {
 4946:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 4947:                         } else {
 4948:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 4949:                         }
 4950:                         if (!$privkey) {
 4951:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 4952:                         } else {
 4953:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 4954:                         }
 4955:                         $chgtxt .= '</ul>';
 4956:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4957:                     }
 4958:                 } else {
 4959:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4960:                 }
 4961:             }
 4962:             $resulttext .= $colchgtext.'</ul>';
 4963:         } else {
 4964:             $resulttext = &mt('No changes made to log-in page settings');
 4965:         }
 4966:     } else {
 4967:         $resulttext = '<span class="LC_error">'.
 4968: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4969:     }
 4970:     if ($errors) {
 4971:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 4972:                        $errors.'</ul>';
 4973:     }
 4974:     return $resulttext;
 4975: }
 4976: 
 4977: sub color_font_choices {
 4978:     my %choices =
 4979:         &Apache::lonlocal::texthash (
 4980:             img => "Header",
 4981:             bgs => "Background colors",
 4982:             links => "Link colors",
 4983:             images => "Images",
 4984:             font => "Font color",
 4985:             fontmenu => "Font Menu",
 4986:             pgbg => "Page",
 4987:             tabbg => "Header",
 4988:             sidebg => "Border",
 4989:             link => "Link",
 4990:             alink => "Active link",
 4991:             vlink => "Visited link",
 4992:         );
 4993:     return %choices;
 4994: }
 4995: 
 4996: sub modify_rolecolors {
 4997:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 4998:     my ($resulttext,%rolehash);
 4999:     $rolehash{'rolecolors'} = {};
 5000:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5001:         if ($domconfig{'rolecolors'} eq '') {
 5002:             $domconfig{'rolecolors'} = {};
 5003:         }
 5004:     }
 5005:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5006:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5007:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5008:                                              $dom);
 5009:     if ($putresult eq 'ok') {
 5010:         if (keys(%changes) > 0) {
 5011:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5012:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5013:                                              $rolehash{'rolecolors'});
 5014:         } else {
 5015:             $resulttext = &mt('No changes made to default color schemes');
 5016:         }
 5017:     } else {
 5018:         $resulttext = '<span class="LC_error">'.
 5019: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5020:     }
 5021:     if ($errors) {
 5022:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5023:                        $errors.'</ul>';
 5024:     }
 5025:     return $resulttext;
 5026: }
 5027: 
 5028: sub modify_colors {
 5029:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5030:     my (%changes,%choices);
 5031:     my @bgs;
 5032:     my @links = ('link','alink','vlink');
 5033:     my @logintext;
 5034:     my @images;
 5035:     my $servadm = $r->dir_config('lonAdmEMail');
 5036:     my $errors;
 5037:     foreach my $role (@{$roles}) {
 5038:         if ($role eq 'login') {
 5039:             %choices = &login_choices();
 5040:             @logintext = ('textcol','bgcol');
 5041:         } else {
 5042:             %choices = &color_font_choices();
 5043:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5044:         }
 5045:         if ($role eq 'login') {
 5046:             @images = ('img','logo','domlogo','login');
 5047:             @bgs = ('pgbg','mainbg','sidebg');
 5048:         } else {
 5049:             @images = ('img');
 5050:             @bgs = ('pgbg','tabbg','sidebg'); 
 5051:         }
 5052:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5053:         foreach my $item (@bgs,@links,@logintext) {
 5054:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5055:         }
 5056:         my ($configuserok,$author_ok,$switchserver) = 
 5057:             &config_check($dom,$confname,$servadm);
 5058:         my ($width,$height) = &thumb_dimensions();
 5059:         if (ref($domconfig->{$role}) ne 'HASH') {
 5060:             $domconfig->{$role} = {};
 5061:         }
 5062:         foreach my $img (@images) {
 5063:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5064:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5065:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5066:                 } else { 
 5067:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5068:                 }
 5069:             } 
 5070: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5071: 		 && !defined($domconfig->{$role}{$img})
 5072: 		 && !$env{'form.'.$role.'_del_'.$img}
 5073: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5074: 		# import the old configured image from the .tab setting
 5075: 		# if they haven't provided a new one 
 5076: 		$domconfig->{$role}{$img} = 
 5077: 		    $env{'form.'.$role.'_import_'.$img};
 5078: 	    }
 5079:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5080:                 my $error;
 5081:                 if ($configuserok eq 'ok') {
 5082:                     if ($switchserver) {
 5083:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5084:                     } else {
 5085:                         if ($author_ok eq 'ok') {
 5086:                             my ($result,$logourl) = 
 5087:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5088:                                            $dom,$confname,$img,$width,$height);
 5089:                             if ($result eq 'ok') {
 5090:                                 $confhash->{$role}{$img} = $logourl;
 5091:                                 $changes{$role}{'images'}{$img} = 1;
 5092:                             } else {
 5093:                                 $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);
 5094:                             }
 5095:                         } else {
 5096:                             $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);
 5097:                         }
 5098:                     }
 5099:                 } else {
 5100:                     $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);
 5101:                 }
 5102:                 if ($error) {
 5103:                     &Apache::lonnet::logthis($error);
 5104:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5105:                 }
 5106:             } elsif ($domconfig->{$role}{$img} ne '') {
 5107:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5108:                     my $error;
 5109:                     if ($configuserok eq 'ok') {
 5110: # is confname an author?
 5111:                         if ($switchserver eq '') {
 5112:                             if ($author_ok eq 'ok') {
 5113:                                 my ($result,$logourl) = 
 5114:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5115:                                             $dom,$confname,$img,$width,$height);
 5116:                                 if ($result eq 'ok') {
 5117:                                     $confhash->{$role}{$img} = $logourl;
 5118: 				    $changes{$role}{'images'}{$img} = 1;
 5119:                                 }
 5120:                             }
 5121:                         }
 5122:                     }
 5123:                 }
 5124:             }
 5125:         }
 5126:         if (ref($domconfig) eq 'HASH') {
 5127:             if (ref($domconfig->{$role}) eq 'HASH') {
 5128:                 foreach my $img (@images) {
 5129:                     if ($domconfig->{$role}{$img} ne '') {
 5130:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5131:                             $confhash->{$role}{$img} = '';
 5132:                             $changes{$role}{'images'}{$img} = 1;
 5133:                         } else {
 5134:                             if ($confhash->{$role}{$img} eq '') {
 5135:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5136:                             }
 5137:                         }
 5138:                     } else {
 5139:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5140:                             $confhash->{$role}{$img} = '';
 5141:                             $changes{$role}{'images'}{$img} = 1;
 5142:                         } 
 5143:                     }
 5144:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5145:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5146:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5147:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5148:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5149:                             }
 5150:                         } else {
 5151:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5152:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5153:                             }
 5154:                         }
 5155:                     }
 5156:                 }
 5157:                 if ($domconfig->{$role}{'font'} ne '') {
 5158:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5159:                         $changes{$role}{'font'} = 1;
 5160:                     }
 5161:                 } else {
 5162:                     if ($confhash->{$role}{'font'}) {
 5163:                         $changes{$role}{'font'} = 1;
 5164:                     }
 5165:                 }
 5166:                 if ($role ne 'login') {
 5167:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5168:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5169:                             $changes{$role}{'fontmenu'} = 1;
 5170:                         }
 5171:                     } else {
 5172:                         if ($confhash->{$role}{'fontmenu'}) {
 5173:                             $changes{$role}{'fontmenu'} = 1;
 5174:                         }
 5175:                     }
 5176:                 }
 5177:                 foreach my $item (@bgs) {
 5178:                     if ($domconfig->{$role}{$item} ne '') {
 5179:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5180:                             $changes{$role}{'bgs'}{$item} = 1;
 5181:                         } 
 5182:                     } else {
 5183:                         if ($confhash->{$role}{$item}) {
 5184:                             $changes{$role}{'bgs'}{$item} = 1;
 5185:                         }
 5186:                     }
 5187:                 }
 5188:                 foreach my $item (@links) {
 5189:                     if ($domconfig->{$role}{$item} ne '') {
 5190:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5191:                             $changes{$role}{'links'}{$item} = 1;
 5192:                         }
 5193:                     } else {
 5194:                         if ($confhash->{$role}{$item}) {
 5195:                             $changes{$role}{'links'}{$item} = 1;
 5196:                         }
 5197:                     }
 5198:                 }
 5199:                 foreach my $item (@logintext) {
 5200:                     if ($domconfig->{$role}{$item} ne '') {
 5201:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5202:                             $changes{$role}{'logintext'}{$item} = 1;
 5203:                         }
 5204:                     } else {
 5205:                         if ($confhash->{$role}{$item}) {
 5206:                             $changes{$role}{'logintext'}{$item} = 1;
 5207:                         }
 5208:                     }
 5209:                 }
 5210:             } else {
 5211:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5212:                                         \@logintext,$confhash,\%changes); 
 5213:             }
 5214:         } else {
 5215:             &default_change_checker($role,\@images,\@links,\@bgs,
 5216:                                     \@logintext,$confhash,\%changes); 
 5217:         }
 5218:     }
 5219:     return ($errors,%changes);
 5220: }
 5221: 
 5222: sub config_check {
 5223:     my ($dom,$confname,$servadm) = @_;
 5224:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5225:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5226:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5227:                                                    $confname,$servadm);
 5228:     if ($configuserok eq 'ok') {
 5229:         $switchserver = &check_switchserver($dom,$confname);
 5230:         if ($switchserver eq '') {
 5231:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5232:         }
 5233:     }
 5234:     return ($configuserok,$author_ok,$switchserver);
 5235: }
 5236: 
 5237: sub default_change_checker {
 5238:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5239:     foreach my $item (@{$links}) {
 5240:         if ($confhash->{$role}{$item}) {
 5241:             $changes->{$role}{'links'}{$item} = 1;
 5242:         }
 5243:     }
 5244:     foreach my $item (@{$bgs}) {
 5245:         if ($confhash->{$role}{$item}) {
 5246:             $changes->{$role}{'bgs'}{$item} = 1;
 5247:         }
 5248:     }
 5249:     foreach my $item (@{$logintext}) {
 5250:         if ($confhash->{$role}{$item}) {
 5251:             $changes->{$role}{'logintext'}{$item} = 1;
 5252:         }
 5253:     }
 5254:     foreach my $img (@{$images}) {
 5255:         if ($env{'form.'.$role.'_del_'.$img}) {
 5256:             $confhash->{$role}{$img} = '';
 5257:             $changes->{$role}{'images'}{$img} = 1;
 5258:         }
 5259:         if ($role eq 'login') {
 5260:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5261:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5262:             }
 5263:         }
 5264:     }
 5265:     if ($confhash->{$role}{'font'}) {
 5266:         $changes->{$role}{'font'} = 1;
 5267:     }
 5268: }
 5269: 
 5270: sub display_colorchgs {
 5271:     my ($dom,$changes,$roles,$confhash) = @_;
 5272:     my (%choices,$resulttext);
 5273:     if (!grep(/^login$/,@{$roles})) {
 5274:         $resulttext = &mt('Changes made:').'<br />';
 5275:     }
 5276:     foreach my $role (@{$roles}) {
 5277:         if ($role eq 'login') {
 5278:             %choices = &login_choices();
 5279:         } else {
 5280:             %choices = &color_font_choices();
 5281:         }
 5282:         if (ref($changes->{$role}) eq 'HASH') {
 5283:             if ($role ne 'login') {
 5284:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5285:             }
 5286:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5287:                 if ($role ne 'login') {
 5288:                     $resulttext .= '<ul>';
 5289:                 }
 5290:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5291:                     if ($role ne 'login') {
 5292:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5293:                     }
 5294:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5295:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5296:                             if ($confhash->{$role}{$key}{$item}) {
 5297:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5298:                             } else {
 5299:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5300:                             }
 5301:                         } elsif ($confhash->{$role}{$item} eq '') {
 5302:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5303:                         } else {
 5304:                             my $newitem = $confhash->{$role}{$item};
 5305:                             if ($key eq 'images') {
 5306:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5307:                             }
 5308:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5309:                         }
 5310:                     }
 5311:                     if ($role ne 'login') {
 5312:                         $resulttext .= '</ul></li>';
 5313:                     }
 5314:                 } else {
 5315:                     if ($confhash->{$role}{$key} eq '') {
 5316:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5317:                     } else {
 5318:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5319:                     }
 5320:                 }
 5321:                 if ($role ne 'login') {
 5322:                     $resulttext .= '</ul>';
 5323:                 }
 5324:             }
 5325:         }
 5326:     }
 5327:     return $resulttext;
 5328: }
 5329: 
 5330: sub thumb_dimensions {
 5331:     return ('200','50');
 5332: }
 5333: 
 5334: sub check_dimensions {
 5335:     my ($inputfile) = @_;
 5336:     my ($fullwidth,$fullheight);
 5337:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5338:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5339:             my $imageinfo = <PIPE>;
 5340:             if (!close(PIPE)) {
 5341:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5342:             }
 5343:             chomp($imageinfo);
 5344:             my ($fullsize) = 
 5345:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5346:             if ($fullsize) {
 5347:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5348:             }
 5349:         }
 5350:     }
 5351:     return ($fullwidth,$fullheight);
 5352: }
 5353: 
 5354: sub check_configuser {
 5355:     my ($uhome,$dom,$confname,$servadm) = @_;
 5356:     my ($configuserok,%currroles);
 5357:     if ($uhome eq 'no_host') {
 5358:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5359:         my $configpass = &LONCAPA::Enrollment::create_password();
 5360:         $configuserok = 
 5361:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5362:                              $configpass,'','','','','',undef,$servadm);
 5363:     } else {
 5364:         $configuserok = 'ok';
 5365:         %currroles = 
 5366:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5367:     }
 5368:     return ($configuserok,%currroles);
 5369: }
 5370: 
 5371: sub check_authorstatus {
 5372:     my ($dom,$confname,%currroles) = @_;
 5373:     my $author_ok;
 5374:     if (!$currroles{':'.$dom.':au'}) {
 5375:         my $start = time;
 5376:         my $end = 0;
 5377:         $author_ok = 
 5378:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5379:                                         'au',$end,$start,'','','domconfig');
 5380:     } else {
 5381:         $author_ok = 'ok';
 5382:     }
 5383:     return $author_ok;
 5384: }
 5385: 
 5386: sub publishlogo {
 5387:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5388:     my ($output,$fname,$logourl);
 5389:     if ($action eq 'upload') {
 5390:         $fname=$env{'form.'.$formname.'.filename'};
 5391:         chop($env{'form.'.$formname});
 5392:     } else {
 5393:         ($fname) = ($formname =~ /([^\/]+)$/);
 5394:     }
 5395:     if ($savefileas ne '') {
 5396:         $fname = $savefileas;
 5397:     }
 5398:     $fname=&Apache::lonnet::clean_filename($fname);
 5399: # See if there is anything left
 5400:     unless ($fname) { return ('error: no uploaded file'); }
 5401:     $fname="$subdir/$fname";
 5402:     my $docroot=$r->dir_config('lonDocRoot'); 
 5403:     my $filepath="$docroot/priv";
 5404:     my $relpath = "$dom/$confname";
 5405:     my ($fnamepath,$file,$fetchthumb);
 5406:     $file=$fname;
 5407:     if ($fname=~m|/|) {
 5408:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5409:     }
 5410:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5411:     my $count;
 5412:     for ($count=5;$count<=$#parts;$count++) {
 5413:         $filepath.="/$parts[$count]";
 5414:         if ((-e $filepath)!=1) {
 5415:             mkdir($filepath,02770);
 5416:         }
 5417:     }
 5418:     # Check for bad extension and disallow upload
 5419:     if ($file=~/\.(\w+)$/ &&
 5420:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5421:         $output = 
 5422:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 5423:     } elsif ($file=~/\.(\w+)$/ &&
 5424:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5425:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5426:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5427:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5428:     } elsif (-d "$filepath/$file") {
 5429:         $output = &mt('File name is a directory name - rename the file and re-upload');
 5430:     } else {
 5431:         my $source = $filepath.'/'.$file;
 5432:         my $logfile;
 5433:         if (!open($logfile,">>$source".'.log')) {
 5434:             return (&mt('No write permission to Construction Space'));
 5435:         }
 5436:         print $logfile
 5437: "\n================= Publish ".localtime()." ================\n".
 5438: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5439: # Save the file
 5440:         if (!open(FH,'>'.$source)) {
 5441:             &Apache::lonnet::logthis('Failed to create '.$source);
 5442:             return (&mt('Failed to create file'));
 5443:         }
 5444:         if ($action eq 'upload') {
 5445:             if (!print FH ($env{'form.'.$formname})) {
 5446:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5447:                 return (&mt('Failed to write file'));
 5448:             }
 5449:         } else {
 5450:             my $original = &Apache::lonnet::filelocation('',$formname);
 5451:             if(!copy($original,$source)) {
 5452:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5453:                 return (&mt('Failed to write file'));
 5454:             }
 5455:         }
 5456:         close(FH);
 5457:         chmod(0660, $source); # Permissions to rw-rw---.
 5458: 
 5459:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5460:         my $copyfile=$targetdir.'/'.$file;
 5461: 
 5462:         my @parts=split(/\//,$targetdir);
 5463:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5464:         for (my $count=5;$count<=$#parts;$count++) {
 5465:             $path.="/$parts[$count]";
 5466:             if (!-e $path) {
 5467:                 print $logfile "\nCreating directory ".$path;
 5468:                 mkdir($path,02770);
 5469:             }
 5470:         }
 5471:         my $versionresult;
 5472:         if (-e $copyfile) {
 5473:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5474:         } else {
 5475:             $versionresult = 'ok';
 5476:         }
 5477:         if ($versionresult eq 'ok') {
 5478:             if (copy($source,$copyfile)) {
 5479:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5480:                 $output = 'ok';
 5481:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5482:                 push(@{$modified_urls},[$copyfile,$source]);
 5483:                 my $metaoutput = 
 5484:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5485:                 unless ($registered_cleanup) {
 5486:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5487:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5488:                     $registered_cleanup=1;
 5489:                 }
 5490:             } else {
 5491:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5492:                 $output = &mt('Failed to copy file to RES space').", $!";
 5493:             }
 5494:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5495:                 my $inputfile = $filepath.'/'.$file;
 5496:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5497:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5498:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5499:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5500:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5501:                         system("convert -sample $thumbsize $inputfile $outfile");
 5502:                         chmod(0660, $filepath.'/tn-'.$file);
 5503:                         if (-e $outfile) {
 5504:                             my $copyfile=$targetdir.'/tn-'.$file;
 5505:                             if (copy($outfile,$copyfile)) {
 5506:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5507:                                 my $thumb_metaoutput = 
 5508:                                     &write_metadata($dom,$confname,$formname,
 5509:                                                     $targetdir,'tn-'.$file,$logfile);
 5510:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5511:                                 unless ($registered_cleanup) {
 5512:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5513:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5514:                                     $registered_cleanup=1;
 5515:                                 }
 5516:                             } else {
 5517:                                 print $logfile "\nUnable to write ".$copyfile.
 5518:                                                ':'.$!."\n";
 5519:                             }
 5520:                         }
 5521:                     }
 5522:                 }
 5523:             }
 5524:         } else {
 5525:             $output = $versionresult;
 5526:         }
 5527:     }
 5528:     return ($output,$logourl);
 5529: }
 5530: 
 5531: sub logo_versioning {
 5532:     my ($targetdir,$file,$logfile) = @_;
 5533:     my $target = $targetdir.'/'.$file;
 5534:     my ($maxversion,$fn,$extn,$output);
 5535:     $maxversion = 0;
 5536:     if ($file =~ /^(.+)\.(\w+)$/) {
 5537:         $fn=$1;
 5538:         $extn=$2;
 5539:     }
 5540:     opendir(DIR,$targetdir);
 5541:     while (my $filename=readdir(DIR)) {
 5542:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5543:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5544:         }
 5545:     }
 5546:     $maxversion++;
 5547:     print $logfile "\nCreating old version ".$maxversion."\n";
 5548:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5549:     if (copy($target,$copyfile)) {
 5550:         print $logfile "Copied old target to ".$copyfile."\n";
 5551:         $copyfile=$copyfile.'.meta';
 5552:         if (copy($target.'.meta',$copyfile)) {
 5553:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5554:             $output = 'ok';
 5555:         } else {
 5556:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5557:             $output = &mt('Failed to copy old meta').", $!, ";
 5558:         }
 5559:     } else {
 5560:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5561:         $output = &mt('Failed to copy old target').", $!, ";
 5562:     }
 5563:     return $output;
 5564: }
 5565: 
 5566: sub write_metadata {
 5567:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5568:     my (%metadatafields,%metadatakeys,$output);
 5569:     $metadatafields{'title'}=$formname;
 5570:     $metadatafields{'creationdate'}=time;
 5571:     $metadatafields{'lastrevisiondate'}=time;
 5572:     $metadatafields{'copyright'}='public';
 5573:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5574:                                          $env{'user.domain'};
 5575:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5576:     $metadatafields{'domain'}=$dom;
 5577:     {
 5578:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5579:         my $mfh;
 5580:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5581:             foreach (sort keys %metadatafields) {
 5582:                 unless ($_=~/\./) {
 5583:                     my $unikey=$_;
 5584:                     $unikey=~/^([A-Za-z]+)/;
 5585:                     my $tag=$1;
 5586:                     $tag=~tr/A-Z/a-z/;
 5587:                     print $mfh "\n\<$tag";
 5588:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5589:                         my $value=$metadatafields{$unikey.'.'.$_};
 5590:                         $value=~s/\"/\'\'/g;
 5591:                         print $mfh ' '.$_.'="'.$value.'"';
 5592:                     }
 5593:                     print $mfh '>'.
 5594:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5595:                             .'</'.$tag.'>';
 5596:                 }
 5597:             }
 5598:             $output = 'ok';
 5599:             print $logfile "\nWrote metadata";
 5600:             close($mfh);
 5601:         } else {
 5602:             print $logfile "\nFailed to open metadata file";
 5603:             $output = &mt('Could not write metadata');
 5604:         }
 5605:     }
 5606:     return $output;
 5607: }
 5608: 
 5609: sub notifysubscribed {
 5610:     foreach my $targetsource (@{$modified_urls}){
 5611:         next unless (ref($targetsource) eq 'ARRAY');
 5612:         my ($target,$source)=@{$targetsource};
 5613:         if ($source ne '') {
 5614:             if (open(my $logfh,'>>'.$source.'.log')) {
 5615:                 print $logfh "\nCleanup phase: Notifications\n";
 5616:                 my @subscribed=&subscribed_hosts($target);
 5617:                 foreach my $subhost (@subscribed) {
 5618:                     print $logfh "\nNotifying host ".$subhost.':';
 5619:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5620:                     print $logfh $reply;
 5621:                 }
 5622:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5623:                 foreach my $subhost (@subscribedmeta) {
 5624:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5625:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5626:                                                         $subhost);
 5627:                     print $logfh $reply;
 5628:                 }
 5629:                 print $logfh "\n============ Done ============\n";
 5630:                 close($logfh);
 5631:             }
 5632:         }
 5633:     }
 5634:     return OK;
 5635: }
 5636: 
 5637: sub subscribed_hosts {
 5638:     my ($target) = @_;
 5639:     my @subscribed;
 5640:     if (open(my $fh,"<$target.subscription")) {
 5641:         while (my $subline=<$fh>) {
 5642:             if ($subline =~ /^($match_lonid):/) {
 5643:                 my $host = $1;
 5644:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5645:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5646:                         push(@subscribed,$host);
 5647:                     }
 5648:                 }
 5649:             }
 5650:         }
 5651:     }
 5652:     return @subscribed;
 5653: }
 5654: 
 5655: sub check_switchserver {
 5656:     my ($dom,$confname) = @_;
 5657:     my ($allowed,$switchserver);
 5658:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5659:     if ($home eq 'no_host') {
 5660:         $home = &Apache::lonnet::domain($dom,'primary');
 5661:     }
 5662:     my @ids=&Apache::lonnet::current_machine_ids();
 5663:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5664:     if (!$allowed) {
 5665: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&role=dc./'.$dom.'/">'.&mt('Switch Server').'</a>';
 5666:     }
 5667:     return $switchserver;
 5668: }
 5669: 
 5670: sub modify_quotas {
 5671:     my ($dom,$action,%domconfig) = @_;
 5672:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5673:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5674:     if ($action eq 'quotas') {
 5675:         $context = 'tools'; 
 5676:     } else {
 5677:         $context = $action;
 5678:     }
 5679:     if ($context eq 'requestcourses') {
 5680:         @usertools = ('official','unofficial','community');
 5681:         @options =('norequest','approval','validate','autolimit');
 5682:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5683:         %titles = &courserequest_titles();
 5684:         $toolregexp = join('|',@usertools);
 5685:         %conditions = &courserequest_conditions();
 5686:     } elsif ($context eq 'requestauthor') {
 5687:         @usertools = ('author');
 5688:         %titles = &authorrequest_titles();
 5689:     } else {
 5690:         @usertools = ('aboutme','blog','webdav','portfolio');
 5691:         %titles = &tool_titles();
 5692:     }
 5693:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5694:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5695:     foreach my $key (keys(%env)) {
 5696:         if ($context eq 'requestcourses') {
 5697:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5698:                 my $item = $1;
 5699:                 my $type = $2;
 5700:                 if ($type =~ /^limit_(.+)/) {
 5701:                     $limithash{$item}{$1} = $env{$key};
 5702:                 } else {
 5703:                     $confhash{$item}{$type} = $env{$key};
 5704:                 }
 5705:             }
 5706:         } elsif ($context eq 'requestauthor') {
 5707:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5708:                 $confhash{$1} = $env{$key};
 5709:             }
 5710:         } else {
 5711:             if ($key =~ /^form\.quota_(.+)$/) {
 5712:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5713:             }
 5714:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5715:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5716:             }
 5717:         }
 5718:     }
 5719:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5720:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5721:         @approvalnotify = sort(@approvalnotify);
 5722:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5723:         if (ref($domconfig{$action}) eq 'HASH') {
 5724:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5725:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5726:                     $changes{'notify'}{'approval'} = 1;
 5727:                 }
 5728:             } else {
 5729:                 if ($confhash{'notify'}{'approval'}) {
 5730:                     $changes{'notify'}{'approval'} = 1;
 5731:                 }
 5732:             }
 5733:         } else {
 5734:             if ($confhash{'notify'}{'approval'}) {
 5735:                 $changes{'notify'}{'approval'} = 1;
 5736:             }
 5737:         }
 5738:     } else {
 5739:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5740:     }
 5741:     foreach my $item (@usertools) {
 5742:         foreach my $type (@{$types},'default','_LC_adv') {
 5743:             my $unset; 
 5744:             if ($context eq 'requestcourses') {
 5745:                 $unset = '0';
 5746:                 if ($type eq '_LC_adv') {
 5747:                     $unset = '';
 5748:                 }
 5749:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5750:                     $confhash{$item}{$type} .= '=';
 5751:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5752:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5753:                     }
 5754:                 }
 5755:             } elsif ($context eq 'requestauthor') {
 5756:                 $unset = '0';
 5757:                 if ($type eq '_LC_adv') {
 5758:                     $unset = '';
 5759:                 }
 5760:             } else {
 5761:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5762:                     $confhash{$item}{$type} = 1;
 5763:                 } else {
 5764:                     $confhash{$item}{$type} = 0;
 5765:                 }
 5766:             }
 5767:             if (ref($domconfig{$action}) eq 'HASH') {
 5768:                 if ($action eq 'requestauthor') {
 5769:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5770:                         $changes{$type} = 1;
 5771:                     }
 5772:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5773:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5774:                         $changes{$item}{$type} = 1;
 5775:                     }
 5776:                 } else {
 5777:                     if ($context eq 'requestcourses') {
 5778:                         if ($confhash{$item}{$type} ne $unset) {
 5779:                             $changes{$item}{$type} = 1;
 5780:                         }
 5781:                     } else {
 5782:                         if (!$confhash{$item}{$type}) {
 5783:                             $changes{$item}{$type} = 1;
 5784:                         }
 5785:                     }
 5786:                 }
 5787:             } else {
 5788:                 if ($context eq 'requestcourses') {
 5789:                     if ($confhash{$item}{$type} ne $unset) {
 5790:                         $changes{$item}{$type} = 1;
 5791:                     }
 5792:                 } elsif ($context eq 'requestauthor') {
 5793:                     if ($confhash{$type} ne $unset) {
 5794:                         $changes{$type} = 1;
 5795:                     }
 5796:                 } else {
 5797:                     if (!$confhash{$item}{$type}) {
 5798:                         $changes{$item}{$type} = 1;
 5799:                     }
 5800:                 }
 5801:             }
 5802:         }
 5803:     }
 5804:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5805:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5806:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5807:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5808:                     if (exists($confhash{'defaultquota'}{$key})) {
 5809:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5810:                             $changes{'defaultquota'}{$key} = 1;
 5811:                         }
 5812:                     } else {
 5813:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5814:                     }
 5815:                 }
 5816:             } else {
 5817:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5818:                     if (exists($confhash{'defaultquota'}{$key})) {
 5819:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5820:                             $changes{'defaultquota'}{$key} = 1;
 5821:                         }
 5822:                     } else {
 5823:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5824:                     }
 5825:                 }
 5826:             }
 5827:         }
 5828:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5829:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5830:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5831:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5832:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5833:                             $changes{'defaultquota'}{$key} = 1;
 5834:                         }
 5835:                     } else {
 5836:                         if (!exists($domconfig{'quotas'}{$key})) {
 5837:                             $changes{'defaultquota'}{$key} = 1;
 5838:                         }
 5839:                     }
 5840:                 } else {
 5841:                     $changes{'defaultquota'}{$key} = 1;
 5842:                 }
 5843:             }
 5844:         }
 5845:     }
 5846: 
 5847:     if ($context eq 'requestauthor') {
 5848:         $domdefaults{'requestauthor'} = \%confhash;
 5849:     } else {
 5850:         foreach my $key (keys(%confhash)) {
 5851:             $domdefaults{$key} = $confhash{$key};
 5852:         }
 5853:     }
 5854: 
 5855:     my %quotahash = (
 5856:                       $action => { %confhash }
 5857:                     );
 5858:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5859:                                              $dom);
 5860:     if ($putresult eq 'ok') {
 5861:         if (keys(%changes) > 0) {
 5862:             my $cachetime = 24*60*60;
 5863:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5864: 
 5865:             $resulttext = &mt('Changes made:').'<ul>';
 5866:             unless (($context eq 'requestcourses') || 
 5867:                     ($context eq 'requestauthor')) {
 5868:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5869:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5870:                     foreach my $type (@{$types},'default') {
 5871:                         if (defined($changes{'defaultquota'}{$type})) {
 5872:                             my $typetitle = $usertypes->{$type};
 5873:                             if ($type eq 'default') {
 5874:                                 $typetitle = $othertitle;
 5875:                             }
 5876:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5877:                         }
 5878:                     }
 5879:                     $resulttext .= '</ul></li>';
 5880:                 }
 5881:             }
 5882:             my %newenv;
 5883:             foreach my $item (@usertools) {
 5884:                 my (%haschgs,%inconf);
 5885:                 if ($context eq 'requestauthor') {
 5886:                     %haschgs = %changes;
 5887:                     %inconf = %confhash; 
 5888:                 } else {
 5889:                     if (ref($changes{$item}) eq 'HASH') {
 5890:                         %haschgs = %{$changes{$item}};
 5891:                     }
 5892:                     if (ref($confhash{$item}) eq 'HASH') {
 5893:                         %inconf = %{$confhash{$item}};
 5894:                     }
 5895:                 }
 5896:                 if (keys(%haschgs) > 0) {
 5897:                     my $newacc = 
 5898:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5899:                                                           $env{'user.domain'},
 5900:                                                           $item,'reload',$context);
 5901:                     if (($context eq 'requestcourses') || 
 5902:                         ($context eq 'requestauthor')) {
 5903:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5904:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5905:                         }
 5906:                     } else {
 5907:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5908:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5909:                         }
 5910:                     }
 5911:                     unless ($context eq 'requestauthor') {
 5912:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5913:                     }
 5914:                     foreach my $type (@{$types},'default','_LC_adv') {
 5915:                         if ($haschgs{$type}) {
 5916:                             my $typetitle = $usertypes->{$type};
 5917:                             if ($type eq 'default') {
 5918:                                 $typetitle = $othertitle;
 5919:                             } elsif ($type eq '_LC_adv') {
 5920:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5921:                             }
 5922:                             if ($inconf{$type}) {
 5923:                                 if ($context eq 'requestcourses') {
 5924:                                     my $cond;
 5925:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 5926:                                         if ($1 eq '') {
 5927:                                             $cond = &mt('(Automatic processing of any request).');
 5928:                                         } else {
 5929:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5930:                                         }
 5931:                                     } else { 
 5932:                                         $cond = $conditions{$inconf{$type}};
 5933:                                     }
 5934:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5935:                                 } elsif ($context eq 'requestauthor') {
 5936:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 5937:                                                              $titles{$inconf{$type}},$typetitle);
 5938: 
 5939:                                 } else {
 5940:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5941:                                 }
 5942:                             } else {
 5943:                                 if ($type eq '_LC_adv') {
 5944:                                     if ($inconf{$type} eq '0') {
 5945:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5946:                                     } else { 
 5947:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5948:                                     }
 5949:                                 } else {
 5950:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5951:                                 }
 5952:                             }
 5953:                         }
 5954:                     }
 5955:                     unless ($context eq 'requestauthor') {
 5956:                         $resulttext .= '</ul></li>';
 5957:                     }
 5958:                 }
 5959:             }
 5960:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 5961:                 if (ref($changes{'notify'}) eq 'HASH') {
 5962:                     if ($changes{'notify'}{'approval'}) {
 5963:                         if (ref($confhash{'notify'}) eq 'HASH') {
 5964:                             if ($confhash{'notify'}{'approval'}) {
 5965:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 5966:                             } else {
 5967:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 5968:                             }
 5969:                         }
 5970:                     }
 5971:                 }
 5972:             }
 5973:             $resulttext .= '</ul>';
 5974:             if (keys(%newenv)) {
 5975:                 &Apache::lonnet::appenv(\%newenv);
 5976:             }
 5977:         } else {
 5978:             if ($context eq 'requestcourses') {
 5979:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 5980:             } elsif ($context eq 'requestauthor') {
 5981:                 $resulttext = &mt('No changes made to rights to request author space.');
 5982:             } else {
 5983:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 5984:             }
 5985:         }
 5986:     } else {
 5987:         $resulttext = '<span class="LC_error">'.
 5988: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5989:     }
 5990:     return $resulttext;
 5991: }
 5992: 
 5993: sub modify_autoenroll {
 5994:     my ($dom,%domconfig) = @_;
 5995:     my ($resulttext,%changes);
 5996:     my %currautoenroll;
 5997:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 5998:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 5999:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 6000:         }
 6001:     }
 6002:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6003:     my %title = ( run => 'Auto-enrollment active',
 6004:                   sender => 'Sender for notification messages',
 6005:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6006:     my @offon = ('off','on');
 6007:     my $sender_uname = $env{'form.sender_uname'};
 6008:     my $sender_domain = $env{'form.sender_domain'};
 6009:     if ($sender_domain eq '') {
 6010:         $sender_uname = '';
 6011:     } elsif ($sender_uname eq '') {
 6012:         $sender_domain = '';
 6013:     }
 6014:     my $coowners = $env{'form.autoassign_coowners'};
 6015:     my %autoenrollhash =  (
 6016:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6017:                                        'sender_uname' => $sender_uname,
 6018:                                        'sender_domain' => $sender_domain,
 6019:                                        'co-owners' => $coowners,
 6020:                                 }
 6021:                      );
 6022:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6023:                                              $dom);
 6024:     if ($putresult eq 'ok') {
 6025:         if (exists($currautoenroll{'run'})) {
 6026:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6027:                  $changes{'run'} = 1;
 6028:              }
 6029:         } elsif ($autorun) {
 6030:             if ($env{'form.autoenroll_run'} ne '1') {
 6031:                  $changes{'run'} = 1;
 6032:             }
 6033:         }
 6034:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6035:             $changes{'sender'} = 1;
 6036:         }
 6037:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6038:             $changes{'sender'} = 1;
 6039:         }
 6040:         if ($currautoenroll{'co-owners'} ne '') {
 6041:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6042:                 $changes{'coowners'} = 1;
 6043:             }
 6044:         } elsif ($coowners) {
 6045:             $changes{'coowners'} = 1;
 6046:         }      
 6047:         if (keys(%changes) > 0) {
 6048:             $resulttext = &mt('Changes made:').'<ul>';
 6049:             if ($changes{'run'}) {
 6050:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6051:             }
 6052:             if ($changes{'sender'}) {
 6053:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6054:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6055:                 } else {
 6056:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6057:                 }
 6058:             }
 6059:             if ($changes{'coowners'}) {
 6060:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6061:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6062:             }
 6063:             $resulttext .= '</ul>';
 6064:         } else {
 6065:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6066:         }
 6067:     } else {
 6068:         $resulttext = '<span class="LC_error">'.
 6069: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6070:     }
 6071:     return $resulttext;
 6072: }
 6073: 
 6074: sub modify_autoupdate {
 6075:     my ($dom,%domconfig) = @_;
 6076:     my ($resulttext,%currautoupdate,%fields,%changes);
 6077:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6078:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6079:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6080:         }
 6081:     }
 6082:     my @offon = ('off','on');
 6083:     my %title = &Apache::lonlocal::texthash (
 6084:                    run => 'Auto-update:',
 6085:                    classlists => 'Updates to user information in classlists?'
 6086:                 );
 6087:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6088:     my %fieldtitles = &Apache::lonlocal::texthash (
 6089:                         id => 'Student/Employee ID',
 6090:                         permanentemail => 'E-mail address',
 6091:                         lastname => 'Last Name',
 6092:                         firstname => 'First Name',
 6093:                         middlename => 'Middle Name',
 6094:                         generation => 'Generation',
 6095:                       );
 6096:     $othertitle = &mt('All users');
 6097:     if (keys(%{$usertypes}) >  0) {
 6098:         $othertitle = &mt('Other users');
 6099:     }
 6100:     foreach my $key (keys(%env)) {
 6101:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6102:             my ($usertype,$item) = ($1,$2);
 6103:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6104:                 if ($usertype eq 'default') {   
 6105:                     push(@{$fields{$1}},$2);
 6106:                 } elsif (ref($types) eq 'ARRAY') {
 6107:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6108:                         push(@{$fields{$1}},$2);
 6109:                     }
 6110:                 }
 6111:             }
 6112:         }
 6113:     }
 6114:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6115:     @lockablenames = sort(@lockablenames);
 6116:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6117:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6118:         if (@changed) {
 6119:             $changes{'lockablenames'} = 1;
 6120:         }
 6121:     } else {
 6122:         if (@lockablenames) {
 6123:             $changes{'lockablenames'} = 1;
 6124:         }
 6125:     }
 6126:     my %updatehash = (
 6127:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6128:                                       classlists => $env{'form.classlists'},
 6129:                                       fields => {%fields},
 6130:                                       lockablenames => \@lockablenames,
 6131:                                     }
 6132:                      );
 6133:     foreach my $key (keys(%currautoupdate)) {
 6134:         if (($key eq 'run') || ($key eq 'classlists')) {
 6135:             if (exists($updatehash{autoupdate}{$key})) {
 6136:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6137:                     $changes{$key} = 1;
 6138:                 }
 6139:             }
 6140:         } elsif ($key eq 'fields') {
 6141:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6142:                 foreach my $item (@{$types},'default') {
 6143:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6144:                         my $change = 0;
 6145:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6146:                             if (!exists($fields{$item})) {
 6147:                                 $change = 1;
 6148:                                 last;
 6149:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6150:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6151:                                     $change = 1;
 6152:                                     last;
 6153:                                 }
 6154:                             }
 6155:                         }
 6156:                         if ($change) {
 6157:                             push(@{$changes{$key}},$item);
 6158:                         }
 6159:                     } 
 6160:                 }
 6161:             }
 6162:         } elsif ($key eq 'lockablenames') {
 6163:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6164:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6165:                 if (@changed) {
 6166:                     $changes{'lockablenames'} = 1;
 6167:                 }
 6168:             } else {
 6169:                 if (@lockablenames) {
 6170:                     $changes{'lockablenames'} = 1;
 6171:                 }
 6172:             }
 6173:         }
 6174:     }
 6175:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6176:         if (@lockablenames) {
 6177:             $changes{'lockablenames'} = 1;
 6178:         }
 6179:     }
 6180:     foreach my $item (@{$types},'default') {
 6181:         if (defined($fields{$item})) {
 6182:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6183:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6184:                     my $change = 0;
 6185:                     if (ref($fields{$item}) eq 'ARRAY') {
 6186:                         foreach my $type (@{$fields{$item}}) {
 6187:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6188:                                 $change = 1;
 6189:                                 last;
 6190:                             }
 6191:                         }
 6192:                     }
 6193:                     if ($change) {
 6194:                         push(@{$changes{'fields'}},$item);
 6195:                     }
 6196:                 } else {
 6197:                     push(@{$changes{'fields'}},$item);
 6198:                 }
 6199:             } else {
 6200:                 push(@{$changes{'fields'}},$item);
 6201:             }
 6202:         }
 6203:     }
 6204:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6205:                                              $dom);
 6206:     if ($putresult eq 'ok') {
 6207:         if (keys(%changes) > 0) {
 6208:             $resulttext = &mt('Changes made:').'<ul>';
 6209:             foreach my $key (sort(keys(%changes))) {
 6210:                 if ($key eq 'lockablenames') {
 6211:                     $resulttext .= '<li>';
 6212:                     if (@lockablenames) {
 6213:                         $usertypes->{'default'} = $othertitle;
 6214:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6215:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6216:                     } else {
 6217:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6218:                     }
 6219:                     $resulttext .= '</li>';
 6220:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6221:                     foreach my $item (@{$changes{$key}}) {
 6222:                         my @newvalues;
 6223:                         foreach my $type (@{$fields{$item}}) {
 6224:                             push(@newvalues,$fieldtitles{$type});
 6225:                         }
 6226:                         my $newvaluestr;
 6227:                         if (@newvalues > 0) {
 6228:                             $newvaluestr = join(', ',@newvalues);
 6229:                         } else {
 6230:                             $newvaluestr = &mt('none');
 6231:                         }
 6232:                         if ($item eq 'default') {
 6233:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6234:                         } else {
 6235:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6236:                         }
 6237:                     }
 6238:                 } else {
 6239:                     my $newvalue;
 6240:                     if ($key eq 'run') {
 6241:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6242:                     } else {
 6243:                         $newvalue = $offon[$env{'form.'.$key}];
 6244:                     }
 6245:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6246:                 }
 6247:             }
 6248:             $resulttext .= '</ul>';
 6249:         } else {
 6250:             $resulttext = &mt('No changes made to autoupdates');
 6251:         }
 6252:     } else {
 6253:         $resulttext = '<span class="LC_error">'.
 6254: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6255:     }
 6256:     return $resulttext;
 6257: }
 6258: 
 6259: sub modify_autocreate {
 6260:     my ($dom,%domconfig) = @_;
 6261:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6262:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6263:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6264:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6265:         }
 6266:     }
 6267:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6268:                  req => 'Auto-creation of validated requests for official courses',
 6269:                  xmldc => 'Identity of course creator of courses from XML files',
 6270:                );
 6271:     my @types = ('xml','req');
 6272:     foreach my $item (@types) {
 6273:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6274:         $newvals{$item} =~ s/\D//g;
 6275:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6276:     }
 6277:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6278:     my %domcoords = &get_active_dcs($dom);
 6279:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6280:         $newvals{'xmldc'} = '';
 6281:     } 
 6282:     %autocreatehash =  (
 6283:                         autocreate => { xml => $newvals{'xml'},
 6284:                                         req => $newvals{'req'},
 6285:                                       }
 6286:                        );
 6287:     if ($newvals{'xmldc'} ne '') {
 6288:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6289:     }
 6290:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6291:                                              $dom);
 6292:     if ($putresult eq 'ok') {
 6293:         my @items = @types;
 6294:         if ($newvals{'xml'}) {
 6295:             push(@items,'xmldc');
 6296:         }
 6297:         foreach my $item (@items) {
 6298:             if (exists($currautocreate{$item})) {
 6299:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6300:                     $changes{$item} = 1;
 6301:                 }
 6302:             } elsif ($newvals{$item}) {
 6303:                 $changes{$item} = 1;
 6304:             }
 6305:         }
 6306:         if (keys(%changes) > 0) {
 6307:             my @offon = ('off','on'); 
 6308:             $resulttext = &mt('Changes made:').'<ul>';
 6309:             foreach my $item (@types) {
 6310:                 if ($changes{$item}) {
 6311:                     my $newtxt = $offon[$newvals{$item}];
 6312:                     $resulttext .= '<li>'.&mt("$title{$item} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>';
 6313:                 }
 6314:             }
 6315:             if ($changes{'xmldc'}) {
 6316:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6317:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6318:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>'; 
 6319:             }
 6320:             $resulttext .= '</ul>';
 6321:         } else {
 6322:             $resulttext = &mt('No changes made to auto-creation settings');
 6323:         }
 6324:     } else {
 6325:         $resulttext = '<span class="LC_error">'.
 6326:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6327:     }
 6328:     return $resulttext;
 6329: }
 6330: 
 6331: sub modify_directorysrch {
 6332:     my ($dom,%domconfig) = @_;
 6333:     my ($resulttext,%changes);
 6334:     my %currdirsrch;
 6335:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6336:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6337:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6338:         }
 6339:     }
 6340:     my %title = ( available => 'Directory search available',
 6341:                   localonly => 'Other domains can search',
 6342:                   searchby => 'Search types',
 6343:                   searchtypes => 'Search latitude');
 6344:     my @offon = ('off','on');
 6345:     my @otherdoms = ('Yes','No');
 6346: 
 6347:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6348:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6349:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6350: 
 6351:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6352:     if (keys(%{$usertypes}) == 0) {
 6353:         @cansearch = ('default');
 6354:     } else {
 6355:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6356:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6357:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6358:                     push(@{$changes{'cansearch'}},$type);
 6359:                 }
 6360:             }
 6361:             foreach my $type (@cansearch) {
 6362:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6363:                     push(@{$changes{'cansearch'}},$type);
 6364:                 }
 6365:             }
 6366:         } else {
 6367:             push(@{$changes{'cansearch'}},@cansearch);
 6368:         }
 6369:     }
 6370: 
 6371:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6372:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6373:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6374:                 push(@{$changes{'searchby'}},$by);
 6375:             }
 6376:         }
 6377:         foreach my $by (@searchby) {
 6378:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6379:                 push(@{$changes{'searchby'}},$by);
 6380:             }
 6381:         }
 6382:     } else {
 6383:         push(@{$changes{'searchby'}},@searchby);
 6384:     }
 6385: 
 6386:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6387:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6388:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6389:                 push(@{$changes{'searchtypes'}},$type);
 6390:             }
 6391:         }
 6392:         foreach my $type (@searchtypes) {
 6393:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6394:                 push(@{$changes{'searchtypes'}},$type);
 6395:             }
 6396:         }
 6397:     } else {
 6398:         if (exists($currdirsrch{'searchtypes'})) {
 6399:             foreach my $type (@searchtypes) {  
 6400:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6401:                     push(@{$changes{'searchtypes'}},$type);
 6402:                 }
 6403:             }
 6404:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6405:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6406:             }   
 6407:         } else {
 6408:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6409:         }
 6410:     }
 6411: 
 6412:     my %dirsrch_hash =  (
 6413:             directorysrch => { available => $env{'form.dirsrch_available'},
 6414:                                cansearch => \@cansearch,
 6415:                                localonly => $env{'form.dirsrch_localonly'},
 6416:                                searchby => \@searchby,
 6417:                                searchtypes => \@searchtypes,
 6418:                              }
 6419:             );
 6420:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6421:                                              $dom);
 6422:     if ($putresult eq 'ok') {
 6423:         if (exists($currdirsrch{'available'})) {
 6424:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6425:                  $changes{'available'} = 1;
 6426:              }
 6427:         } else {
 6428:             if ($env{'form.dirsrch_available'} eq '1') {
 6429:                 $changes{'available'} = 1;
 6430:             }
 6431:         }
 6432:         if (exists($currdirsrch{'localonly'})) {
 6433:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6434:                  $changes{'localonly'} = 1;
 6435:              }
 6436:         } else {
 6437:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6438:                 $changes{'localonly'} = 1;
 6439:             }
 6440:         }
 6441:         if (keys(%changes) > 0) {
 6442:             $resulttext = &mt('Changes made:').'<ul>';
 6443:             if ($changes{'available'}) {
 6444:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6445:             }
 6446:             if ($changes{'localonly'}) {
 6447:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6448:             }
 6449: 
 6450:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6451:                 my $chgtext;
 6452:                 if (ref($usertypes) eq 'HASH') {
 6453:                     if (keys(%{$usertypes}) > 0) {
 6454:                         foreach my $type (@{$types}) {
 6455:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6456:                                 $chgtext .= $usertypes->{$type}.'; ';
 6457:                             }
 6458:                         }
 6459:                         if (grep(/^default$/,@cansearch)) {
 6460:                             $chgtext .= $othertitle;
 6461:                         } else {
 6462:                             $chgtext =~ s/\; $//;
 6463:                         }
 6464:                         $resulttext .= '<li>'.&mt("Users from domain '<span class=\"LC_cusr_emph\">[_1]</span>' permitted to search the institutional directory set to: [_2]",$dom,$chgtext).'</li>';
 6465:                     }
 6466:                 }
 6467:             }
 6468:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6469:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6470:                 my $chgtext;
 6471:                 foreach my $type (@{$titleorder}) {
 6472:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6473:                         if (defined($searchtitles->{$type})) {
 6474:                             $chgtext .= $searchtitles->{$type}.'; ';
 6475:                         }
 6476:                     }
 6477:                 }
 6478:                 $chgtext =~ s/\; $//;
 6479:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6480:             }
 6481:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6482:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6483:                 my $chgtext;
 6484:                 foreach my $type (@{$srchtypeorder}) {
 6485:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6486:                         if (defined($srchtypes_desc->{$type})) {
 6487:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6488:                         }
 6489:                     }
 6490:                 }
 6491:                 $chgtext =~ s/\; $//;
 6492:                 $resulttext .= '<li>'.&mt("$title{'searchtypes'} set to: \"[_1]\"",$chgtext).'</li>';
 6493:             }
 6494:             $resulttext .= '</ul>';
 6495:         } else {
 6496:             $resulttext = &mt('No changes made to institution directory search settings');
 6497:         }
 6498:     } else {
 6499:         $resulttext = '<span class="LC_error">'.
 6500:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6501:     }
 6502:     return $resulttext;
 6503: }
 6504: 
 6505: sub modify_contacts {
 6506:     my ($dom,%domconfig) = @_;
 6507:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6508:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6509:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6510:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6511:         }
 6512:     }
 6513:     my (%others,%to,%bcc);
 6514:     my @contacts = ('supportemail','adminemail');
 6515:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6516:                     'requestsmail');
 6517:     foreach my $type (@mailings) {
 6518:         @{$newsetting{$type}} = 
 6519:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6520:         foreach my $item (@contacts) {
 6521:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6522:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6523:             } else {
 6524:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6525:             }
 6526:         }  
 6527:         $others{$type} = $env{'form.'.$type.'_others'};
 6528:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6529:         if ($type eq 'helpdeskmail') {
 6530:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6531:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6532:         }
 6533:     }
 6534:     foreach my $item (@contacts) {
 6535:         $to{$item} = $env{'form.'.$item};
 6536:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6537:     }
 6538:     if (keys(%currsetting) > 0) {
 6539:         foreach my $item (@contacts) {
 6540:             if ($to{$item} ne $currsetting{$item}) {
 6541:                 $changes{$item} = 1;
 6542:             }
 6543:         }
 6544:         foreach my $type (@mailings) {
 6545:             foreach my $item (@contacts) {
 6546:                 if (ref($currsetting{$type}) eq 'HASH') {
 6547:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6548:                         push(@{$changes{$type}},$item);
 6549:                     }
 6550:                 } else {
 6551:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6552:                 }
 6553:             }
 6554:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6555:                 push(@{$changes{$type}},'others');
 6556:             }
 6557:             if ($type eq 'helpdeskmail') {   
 6558:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6559:                     push(@{$changes{$type}},'bcc'); 
 6560:                 }
 6561:             }
 6562:         }
 6563:     } else {
 6564:         my %default;
 6565:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6566:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6567:         $default{'errormail'} = 'adminemail';
 6568:         $default{'packagesmail'} = 'adminemail';
 6569:         $default{'helpdeskmail'} = 'supportemail';
 6570:         $default{'lonstatusmail'} = 'adminemail';
 6571:         $default{'requestsmail'} = '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:         $newsettings->{'recaptchakeys'} = {
 7079:                                              public  => '',
 7080:                                              private => '',
 7081:                                           }
 7082:     }
 7083:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 7084:         if ($container eq 'cancreate') {
 7085:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7086:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 7087:             } elsif (!defined($changes->{'cancreate'})) {
 7088:                 $changes->{'cancreate'} = ['recaptchakeys'];
 7089:             }
 7090:         } else {
 7091:             $changes->{'recaptchakeys'} = 1; 
 7092:         }
 7093:     }
 7094:     return;
 7095: }
 7096: 
 7097: sub modify_usermodification {
 7098:     my ($dom,%domconfig) = @_;
 7099:     my ($resulttext,%curr_usermodification,%changes);
 7100:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7101:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7102:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 7103:         }
 7104:     }
 7105:     my @contexts = ('author','course','selfcreate');
 7106:     my %context_title = (
 7107:                            author => 'In author context',
 7108:                            course => 'In course context',
 7109:                            selfcreate => 'When self creating account', 
 7110:                         );
 7111:     my @fields = ('lastname','firstname','middlename','generation',
 7112:                   'permanentemail','id');
 7113:     my %roles = (
 7114:                   author => ['ca','aa'],
 7115:                   course => ['st','ep','ta','in','cr'],
 7116:                 );
 7117:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7118:     if (ref($types) eq 'ARRAY') {
 7119:         push(@{$types},'default');
 7120:         $usertypes->{'default'} = $othertitle;
 7121:     }
 7122:     $roles{'selfcreate'} = $types;  
 7123:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7124:     my %modifyhash;
 7125:     foreach my $context (@contexts) {
 7126:         foreach my $role (@{$roles{$context}}) {
 7127:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7128:             foreach my $item (@fields) {
 7129:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7130:                     $modifyhash{$context}{$role}{$item} = 1;
 7131:                 } else {
 7132:                     $modifyhash{$context}{$role}{$item} = 0;
 7133:                 }
 7134:             }
 7135:         }
 7136:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7137:             foreach my $role (@{$roles{$context}}) {
 7138:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7139:                     foreach my $field (@fields) {
 7140:                         if ($modifyhash{$context}{$role}{$field} ne 
 7141:                                 $curr_usermodification{$context}{$role}{$field}) {
 7142:                             push(@{$changes{$context}},$role);
 7143:                             last;
 7144:                         }
 7145:                     }
 7146:                 }
 7147:             }
 7148:         } else {
 7149:             foreach my $context (@contexts) {
 7150:                 foreach my $role (@{$roles{$context}}) {
 7151:                     push(@{$changes{$context}},$role);
 7152:                 }
 7153:             }
 7154:         }
 7155:     }
 7156:     my %usermodification_hash =  (
 7157:                                    usermodification => \%modifyhash,
 7158:                                  );
 7159:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7160:                                              \%usermodification_hash,$dom);
 7161:     if ($putresult eq 'ok') {
 7162:         if (keys(%changes) > 0) {
 7163:             $resulttext = &mt('Changes made: ').'<ul>';
 7164:             foreach my $context (@contexts) {
 7165:                 if (ref($changes{$context}) eq 'ARRAY') {
 7166:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7167:                     if (ref($changes{$context}) eq 'ARRAY') {
 7168:                         foreach my $role (@{$changes{$context}}) {
 7169:                             my $rolename;
 7170:                             if ($context eq 'selfcreate') {
 7171:                                 $rolename = $role;
 7172:                                 if (ref($usertypes) eq 'HASH') {
 7173:                                     if ($usertypes->{$role} ne '') {
 7174:                                         $rolename = $usertypes->{$role};
 7175:                                     }
 7176:                                 }
 7177:                             } else {
 7178:                                 if ($role eq 'cr') {
 7179:                                     $rolename = &mt('Custom');
 7180:                                 } else {
 7181:                                     $rolename = &Apache::lonnet::plaintext($role);
 7182:                                 }
 7183:                             }
 7184:                             my @modifiable;
 7185:                             if ($context eq 'selfcreate') {
 7186:                                 $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): ');
 7187:                             } else {
 7188:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7189:                             }
 7190:                             foreach my $field (@fields) {
 7191:                                 if ($modifyhash{$context}{$role}{$field}) {
 7192:                                     push(@modifiable,$fieldtitles{$field});
 7193:                                 }
 7194:                             }
 7195:                             if (@modifiable > 0) {
 7196:                                 $resulttext .= join(', ',@modifiable);
 7197:                             } else {
 7198:                                 $resulttext .= &mt('none'); 
 7199:                             }
 7200:                             $resulttext .= '</li>';
 7201:                         }
 7202:                         $resulttext .= '</ul></li>';
 7203:                     }
 7204:                 }
 7205:             }
 7206:             $resulttext .= '</ul>';
 7207:         } else {
 7208:             $resulttext = &mt('No changes made to user modification settings');
 7209:         }
 7210:     } else {
 7211:         $resulttext = '<span class="LC_error">'.
 7212:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7213:     }
 7214:     return $resulttext;
 7215: }
 7216: 
 7217: sub modify_defaults {
 7218:     my ($dom,$r) = @_;
 7219:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7220:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7221:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7222:     my @authtypes = ('internal','krb4','krb5','localauth');
 7223:     foreach my $item (@items) {
 7224:         $newvalues{$item} = $env{'form.'.$item};
 7225:         if ($item eq 'auth_def') {
 7226:             if ($newvalues{$item} ne '') {
 7227:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7228:                     push(@errors,$item);
 7229:                 }
 7230:             }
 7231:         } elsif ($item eq 'lang_def') {
 7232:             if ($newvalues{$item} ne '') {
 7233:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7234:                     my $langcode = $1;
 7235:                     if ($langcode ne 'x_chef') {
 7236:                         if (code2language($langcode) eq '') {
 7237:                             push(@errors,$item);
 7238:                         }
 7239:                     }
 7240:                 } else {
 7241:                     push(@errors,$item);
 7242:                 }
 7243:             }
 7244:         } elsif ($item eq 'timezone_def') {
 7245:             if ($newvalues{$item} ne '') {
 7246:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7247:                     push(@errors,$item);   
 7248:                 }
 7249:             }
 7250:         } elsif ($item eq 'datelocale_def') {
 7251:             if ($newvalues{$item} ne '') {
 7252:                 my @datelocale_ids = DateTime::Locale->ids();
 7253:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7254:                     push(@errors,$item);
 7255:                 }
 7256:             }
 7257:         } elsif ($item eq 'portal_def') {
 7258:             if ($newvalues{$item} ne '') {
 7259:                 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])\/?$/) {
 7260:                     push(@errors,$item);
 7261:                 }
 7262:             }
 7263:         }
 7264:         if (grep(/^\Q$item\E$/,@errors)) {
 7265:             $newvalues{$item} = $domdefaults{$item};
 7266:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7267:             $changes{$item} = 1;
 7268:         }
 7269:         $domdefaults{$item} = $newvalues{$item};
 7270:     }
 7271:     my %defaults_hash = (
 7272:                          defaults => \%newvalues,
 7273:                         );
 7274:     my $title = &defaults_titles();
 7275:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7276:                                              $dom);
 7277:     if ($putresult eq 'ok') {
 7278:         if (keys(%changes) > 0) {
 7279:             $resulttext = &mt('Changes made:').'<ul>';
 7280:             my $version = $r->dir_config('lonVersion');
 7281:             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";
 7282:             foreach my $item (sort(keys(%changes))) {
 7283:                 my $value = $env{'form.'.$item};
 7284:                 if ($value eq '') {
 7285:                     $value = &mt('none');
 7286:                 } elsif ($item eq 'auth_def') {
 7287:                     my %authnames = &authtype_names();
 7288:                     my %shortauth = (
 7289:                              internal => 'int',
 7290:                              krb4 => 'krb4',
 7291:                              krb5 => 'krb5',
 7292:                              localauth  => 'loc',
 7293:                     );
 7294:                     $value = $authnames{$shortauth{$value}};
 7295:                 }
 7296:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7297:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7298:             }
 7299:             $resulttext .= '</ul>';
 7300:             $mailmsgtext .= "\n";
 7301:             my $cachetime = 24*60*60;
 7302:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7303:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7304:                 my $sysmail = $r->dir_config('lonSysEMail');
 7305:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 7306:             }
 7307:         } else {
 7308:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 7309:         }
 7310:     } else {
 7311:         $resulttext = '<span class="LC_error">'.
 7312:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7313:     }
 7314:     if (@errors > 0) {
 7315:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7316:         foreach my $item (@errors) {
 7317:             $resulttext .= ' "'.$title->{$item}.'",';
 7318:         }
 7319:         $resulttext =~ s/,$//;
 7320:     }
 7321:     return $resulttext;
 7322: }
 7323: 
 7324: sub modify_scantron {
 7325:     my ($r,$dom,$confname,%domconfig) = @_;
 7326:     my ($resulttext,%confhash,%changes,$errors);
 7327:     my $custom = 'custom.tab';
 7328:     my $default = 'default.tab';
 7329:     my $servadm = $r->dir_config('lonAdmEMail');
 7330:     my ($configuserok,$author_ok,$switchserver) = 
 7331:         &config_check($dom,$confname,$servadm);
 7332:     if ($env{'form.scantronformat.filename'} ne '') {
 7333:         my $error;
 7334:         if ($configuserok eq 'ok') {
 7335:             if ($switchserver) {
 7336:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7337:             } else {
 7338:                 if ($author_ok eq 'ok') {
 7339:                     my ($result,$scantronurl) =
 7340:                         &publishlogo($r,'upload','scantronformat',$dom,
 7341:                                      $confname,'scantron','','',$custom);
 7342:                     if ($result eq 'ok') {
 7343:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7344:                         $changes{'scantronformat'} = 1;
 7345:                     } else {
 7346:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7347:                     }
 7348:                 } else {
 7349:                     $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);
 7350:                 }
 7351:             }
 7352:         } else {
 7353:             $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);
 7354:         }
 7355:         if ($error) {
 7356:             &Apache::lonnet::logthis($error);
 7357:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7358:         }
 7359:     }
 7360:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7361:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7362:             if ($env{'form.scantronformat_del'}) {
 7363:                 $confhash{'scantron'}{'scantronformat'} = '';
 7364:                 $changes{'scantronformat'} = 1;
 7365:             }
 7366:         }
 7367:     }
 7368:     if (keys(%confhash) > 0) {
 7369:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7370:                                                  $dom);
 7371:         if ($putresult eq 'ok') {
 7372:             if (keys(%changes) > 0) {
 7373:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7374:                     $resulttext = &mt('Changes made:').'<ul>';
 7375:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7376:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7377:                     } else {
 7378:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7379:                     }
 7380:                     $resulttext .= '</ul>';
 7381:                 } else {
 7382:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7383:                 }
 7384:                 $resulttext .= '</ul>';
 7385:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7386:             } else {
 7387:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7388:             }
 7389:         } else {
 7390:             $resulttext = '<span class="LC_error">'.
 7391:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7392:         }
 7393:     } else {
 7394:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7395:     }
 7396:     if ($errors) {
 7397:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7398:                        $errors.'</ul>';
 7399:     }
 7400:     return $resulttext;
 7401: }
 7402: 
 7403: sub modify_coursecategories {
 7404:     my ($dom,%domconfig) = @_;
 7405:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7406:         $cathash);
 7407:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7408:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7409:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7410:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7411:             $changes{'togglecats'} = 1;
 7412:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7413:         }
 7414:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7415:             $changes{'categorize'} = 1;
 7416:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7417:         }
 7418:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7419:             $changes{'togglecatscomm'} = 1;
 7420:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7421:         }
 7422:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7423:             $changes{'categorizecomm'} = 1;
 7424:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7425:         }
 7426:     } else {
 7427:         $changes{'togglecats'} = 1;
 7428:         $changes{'categorize'} = 1;
 7429:         $changes{'togglecatscomm'} = 1;
 7430:         $changes{'categorizecomm'} = 1;
 7431:         $domconfig{'coursecategories'} = {
 7432:                                              togglecats => $env{'form.togglecats'},
 7433:                                              categorize => $env{'form.categorize'},
 7434:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7435:                                              categorizecomm => $env{'form.categorizecomm'},
 7436:                                          };
 7437:     }
 7438:     if (ref($cathash) eq 'HASH') {
 7439:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7440:             push (@deletecategory,'instcode::0');
 7441:         }
 7442:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7443:             push(@deletecategory,'communities::0');
 7444:         }
 7445:     }
 7446:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7447:     if (ref($cathash) eq 'HASH') {
 7448:         if (@deletecategory > 0) {
 7449:             #FIXME Need to remove category from all courses using a deleted category 
 7450:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7451:             foreach my $item (@deletecategory) {
 7452:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7453:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7454:                     $deletions{$item} = 1;
 7455:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7456:                 }
 7457:             }
 7458:         }
 7459:         foreach my $item (keys(%{$cathash})) {
 7460:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7461:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7462:                 $reorderings{$item} = 1;
 7463:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7464:             }
 7465:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7466:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7467:                 my $newdepth = $depth+1;
 7468:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7469:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7470:                 $adds{$newitem} = 1; 
 7471:             }
 7472:             if ($env{'form.subcat_'.$item} ne '') {
 7473:                 my $newcat = $env{'form.subcat_'.$item};
 7474:                 my $newdepth = $depth+1;
 7475:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7476:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7477:                 $adds{$newitem} = 1;
 7478:             }
 7479:         }
 7480:     }
 7481:     if ($env{'form.instcode'} eq '1') {
 7482:         if (ref($cathash) eq 'HASH') {
 7483:             my $newitem = 'instcode::0';
 7484:             if ($cathash->{$newitem} eq '') {  
 7485:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7486:                 $adds{$newitem} = 1;
 7487:             }
 7488:         } else {
 7489:             my $newitem = 'instcode::0';
 7490:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7491:             $adds{$newitem} = 1;
 7492:         }
 7493:     }
 7494:     if ($env{'form.communities'} eq '1') {
 7495:         if (ref($cathash) eq 'HASH') {
 7496:             my $newitem = 'communities::0';
 7497:             if ($cathash->{$newitem} eq '') {
 7498:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7499:                 $adds{$newitem} = 1;
 7500:             }
 7501:         } else {
 7502:             my $newitem = 'communities::0';
 7503:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7504:             $adds{$newitem} = 1;
 7505:         }
 7506:     }
 7507:     if ($env{'form.addcategory_name'} ne '') {
 7508:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7509:             ($env{'form.addcategory_name'} ne 'communities')) {
 7510:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7511:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7512:             $adds{$newitem} = 1;
 7513:         }
 7514:     }
 7515:     my $putresult;
 7516:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7517:         if (keys(%deletions) > 0) {
 7518:             foreach my $key (keys(%deletions)) {
 7519:                 if ($predelallitems{$key} ne '') {
 7520:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7521:                 }
 7522:             }
 7523:         }
 7524:         my (@chkcats,@chktrails,%chkallitems);
 7525:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7526:         if (ref($chkcats[0]) eq 'ARRAY') {
 7527:             my $depth = 0;
 7528:             my $chg = 0;
 7529:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7530:                 my $name = $chkcats[0][$i];
 7531:                 my $item;
 7532:                 if ($name eq '') {
 7533:                     $chg ++;
 7534:                 } else {
 7535:                     $item = &escape($name).'::0';
 7536:                     if ($chg) {
 7537:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7538:                     }
 7539:                     $depth ++; 
 7540:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7541:                     $depth --;
 7542:                 }
 7543:             }
 7544:         }
 7545:     }
 7546:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7547:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7548:         if ($putresult eq 'ok') {
 7549:             my %title = (
 7550:                          togglecats     => 'Show/Hide a course in catalog',
 7551:                          categorize     => 'Assign a category to a course',
 7552:                          togglecatscomm => 'Show/Hide a community in catalog',
 7553:                          categorizecomm => 'Assign a category to a community',
 7554:                         );
 7555:             my %level = (
 7556:                          dom  => 'set in Domain ("Modify Course/Community")',
 7557:                          crs  => 'set in Course ("Course Configuration")',
 7558:                          comm => 'set in Community ("Community Configuration")',
 7559:                         );
 7560:             $resulttext = &mt('Changes made:').'<ul>';
 7561:             if ($changes{'togglecats'}) {
 7562:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7563:             }
 7564:             if ($changes{'categorize'}) {
 7565:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7566:             }
 7567:             if ($changes{'togglecatscomm'}) {
 7568:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7569:             }
 7570:             if ($changes{'categorizecomm'}) {
 7571:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7572:             }
 7573:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7574:                 my $cathash;
 7575:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7576:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7577:                 } else {
 7578:                     $cathash = {};
 7579:                 } 
 7580:                 my (@cats,@trails,%allitems);
 7581:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7582:                 if (keys(%deletions) > 0) {
 7583:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7584:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7585:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7586:                     }
 7587:                     $resulttext .= '</ul></li>';
 7588:                 }
 7589:                 if (keys(%reorderings) > 0) {
 7590:                     my %sort_by_trail;
 7591:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7592:                     foreach my $key (keys(%reorderings)) {
 7593:                         if ($allitems{$key} ne '') {
 7594:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7595:                         }
 7596:                     }
 7597:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7598:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7599:                     }
 7600:                     $resulttext .= '</ul></li>';
 7601:                 }
 7602:                 if (keys(%adds) > 0) {
 7603:                     my %sort_by_trail;
 7604:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7605:                     foreach my $key (keys(%adds)) {
 7606:                         if ($allitems{$key} ne '') {
 7607:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7608:                         }
 7609:                     }
 7610:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7611:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7612:                     }
 7613:                     $resulttext .= '</ul></li>';
 7614:                 }
 7615:             }
 7616:             $resulttext .= '</ul>';
 7617:         } else {
 7618:             $resulttext = '<span class="LC_error">'.
 7619:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7620:         }
 7621:     } else {
 7622:         $resulttext = &mt('No changes made to course and community categories');
 7623:     }
 7624:     return $resulttext;
 7625: }
 7626: 
 7627: sub modify_serverstatuses {
 7628:     my ($dom,%domconfig) = @_;
 7629:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7630:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7631:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7632:     }
 7633:     my @pages = &serverstatus_pages();
 7634:     foreach my $type (@pages) {
 7635:         $newserverstatus{$type}{'namedusers'} = '';
 7636:         $newserverstatus{$type}{'machines'} = '';
 7637:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7638:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7639:             my @okusers;
 7640:             foreach my $user (@users) {
 7641:                 my ($uname,$udom) = split(/:/,$user);
 7642:                 if (($udom =~ /^$match_domain$/) &&   
 7643:                     (&Apache::lonnet::domain($udom)) &&
 7644:                     ($uname =~ /^$match_username$/)) {
 7645:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7646:                         push(@okusers,$user);
 7647:                     }
 7648:                 }
 7649:             }
 7650:             if (@okusers > 0) {
 7651:                  @okusers = sort(@okusers);
 7652:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7653:             }
 7654:         }
 7655:         if (defined($env{'form.'.$type.'_machines'})) {
 7656:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7657:             my @okmachines;
 7658:             foreach my $ip (@machines) {
 7659:                 my @parts = split(/\./,$ip);
 7660:                 next if (@parts < 4);
 7661:                 my $badip = 0;
 7662:                 for (my $i=0; $i<4; $i++) {
 7663:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7664:                         $badip = 1;
 7665:                         last;
 7666:                     }
 7667:                 }
 7668:                 if (!$badip) {
 7669:                     push(@okmachines,$ip);     
 7670:                 }
 7671:             }
 7672:             @okmachines = sort(@okmachines);
 7673:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7674:         }
 7675:     }
 7676:     my %serverstatushash =  (
 7677:                                 serverstatuses => \%newserverstatus,
 7678:                             );
 7679:     foreach my $type (@pages) {
 7680:         foreach my $setting ('namedusers','machines') {
 7681:             my (@current,@new);
 7682:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7683:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7684:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7685:                 }
 7686:             }
 7687:             if ($newserverstatus{$type}{$setting} ne '') {
 7688:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7689:             }
 7690:             if (@current > 0) {
 7691:                 if (@new > 0) {
 7692:                     foreach my $item (@current) {
 7693:                         if (!grep(/^\Q$item\E$/,@new)) {
 7694:                             $changes{$type}{$setting} = 1;
 7695:                             last;
 7696:                         }
 7697:                     }
 7698:                     foreach my $item (@new) {
 7699:                         if (!grep(/^\Q$item\E$/,@current)) {
 7700:                             $changes{$type}{$setting} = 1;
 7701:                             last;
 7702:                         }
 7703:                     }
 7704:                 } else {
 7705:                     $changes{$type}{$setting} = 1;
 7706:                 }
 7707:             } elsif (@new > 0) {
 7708:                 $changes{$type}{$setting} = 1;
 7709:             }
 7710:         }
 7711:     }
 7712:     if (keys(%changes) > 0) {
 7713:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7714:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7715:                                                  \%serverstatushash,$dom);
 7716:         if ($putresult eq 'ok') {
 7717:             $resulttext .= &mt('Changes made:').'<ul>';
 7718:             foreach my $type (@pages) {
 7719:                 if (ref($changes{$type}) eq 'HASH') {
 7720:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7721:                     if ($changes{$type}{'namedusers'}) {
 7722:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7723:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7724:                         } else {
 7725:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7726:                         }
 7727:                     }
 7728:                     if ($changes{$type}{'machines'}) {
 7729:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7730:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7731:                         } else {
 7732:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7733:                         }
 7734: 
 7735:                     }
 7736:                     $resulttext .= '</ul></li>';
 7737:                 }
 7738:             }
 7739:             $resulttext .= '</ul>';
 7740:         } else {
 7741:             $resulttext = '<span class="LC_error">'.
 7742:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7743: 
 7744:         }
 7745:     } else {
 7746:         $resulttext = &mt('No changes made to access to server status pages');
 7747:     }
 7748:     return $resulttext;
 7749: }
 7750: 
 7751: sub modify_helpsettings {
 7752:     my ($r,$dom,$confname,%domconfig) = @_;
 7753:     my ($resulttext,$errors,%changes,%helphash);
 7754:     my %defaultchecked = ('submitbugs' => 'on');
 7755:     my @offon = ('off','on');
 7756:     my @toggles = ('submitbugs');
 7757:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7758:         foreach my $item (@toggles) {
 7759:             if ($defaultchecked{$item} eq 'on') { 
 7760:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7761:                     if ($env{'form.'.$item} eq '0') {
 7762:                         $changes{$item} = 1;
 7763:                     }
 7764:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7765:                     $changes{$item} = 1;
 7766:                 }
 7767:             } elsif ($defaultchecked{$item} eq 'off') {
 7768:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7769:                     if ($env{'form.'.$item} eq '1') {
 7770:                         $changes{$item} = 1;
 7771:                     }
 7772:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7773:                     $changes{$item} = 1;
 7774:                 }
 7775:             }
 7776:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) { 
 7777:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7778:             }
 7779:         }
 7780:     }
 7781:     my $putresult;
 7782:     if (keys(%changes) > 0) {
 7783:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7784:         if ($putresult eq 'ok') {
 7785:             $resulttext = &mt('Changes made:').'<ul>';
 7786:             foreach my $item (sort(keys(%changes))) {
 7787:                 if ($item eq 'submitbugs') {
 7788:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 7789:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 7790:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 7791:                 }
 7792:             }
 7793:             $resulttext .= '</ul>';
 7794:         } else {
 7795:             $resulttext = &mt('No changes made to help settings');
 7796:             $errors .= '<li><span class="LC_error">'.
 7797:                        &mt('An error occurred storing the settings: [_1]',
 7798:                            $putresult).'</span></li>';
 7799:         }
 7800:     }
 7801:     if ($errors) {
 7802:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7803:                        $errors.'</ul>';
 7804:     }
 7805:     return $resulttext;
 7806: }
 7807: 
 7808: sub modify_coursedefaults {
 7809:     my ($dom,%domconfig) = @_;
 7810:     my ($resulttext,$errors,%changes,%defaultshash);
 7811:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7812:     my @offon = ('off','on');
 7813:     my @toggles = ('canuse_pdfforms');
 7814: 
 7815:     $defaultshash{'coursedefaults'} = {};
 7816: 
 7817:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7818:         if ($domconfig{'coursedefaults'} eq '') {
 7819:             $domconfig{'coursedefaults'} = {};
 7820:         }
 7821:     }
 7822: 
 7823:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7824:         foreach my $item (@toggles) {
 7825:             if ($defaultchecked{$item} eq 'on') {
 7826:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7827:                     ($env{'form.'.$item} eq '0')) {
 7828:                     $changes{$item} = 1;
 7829:                 } elsif ($domconfig{'coursdefaults'}{$item} ne $env{'form.'.$item}) {
 7830:                     $changes{$item} = 1;
 7831:                 }
 7832:             } elsif ($defaultchecked{$item} eq 'off') {
 7833:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7834:                     ($env{'form.'.$item} eq '1')) {
 7835:                     $changes{$item} = 1;
 7836:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7837:                     $changes{$item} = 1;
 7838:                 }
 7839:             }
 7840:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7841:         }
 7842:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7843:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7844:         $newdefresponder =~ s/\D//g;
 7845:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7846:             $newdefresponder = 1;
 7847:         }
 7848:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7849:         if ($currdefresponder ne $newdefresponder) {
 7850:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7851:                 $changes{'anonsurvey_threshold'} = 1;
 7852:             }
 7853:         }
 7854:     }
 7855:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7856:                                              $dom);
 7857:     if ($putresult eq 'ok') {
 7858:         if (keys(%changes) > 0) {
 7859:             if ($changes{'canuse_pdfforms'}) {
 7860:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7861:                 $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7862:                 my $cachetime = 24*60*60;
 7863:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7864:             }
 7865:             $resulttext = &mt('Changes made:').'<ul>';
 7866:             foreach my $item (sort(keys(%changes))) {
 7867:                 if ($item eq 'canuse_pdfforms') {
 7868:                     if ($env{'form.'.$item} eq '1') {
 7869:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7870:                     } else {
 7871:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7872:                     }
 7873:                 } elsif ($item eq 'anonsurvey_threshold') {
 7874:                         $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7875:                 }
 7876:             }
 7877:             $resulttext .= '</ul>';
 7878:         } else {
 7879:             $resulttext = &mt('No changes made to course defaults');
 7880:         }
 7881:     } else {
 7882:         $resulttext = '<span class="LC_error">'.
 7883:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7884:     }
 7885:     return $resulttext;
 7886: }
 7887: 
 7888: sub modify_usersessions {
 7889:     my ($dom,%domconfig) = @_;
 7890:     my @hostingtypes = ('version','excludedomain','includedomain');
 7891:     my @offloadtypes = ('primary','default');
 7892:     my %types = (
 7893:                   remote => \@hostingtypes,
 7894:                   hosted => \@hostingtypes,
 7895:                   spares => \@offloadtypes,
 7896:                 );
 7897:     my @prefixes = ('remote','hosted','spares');
 7898:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7899:     my (%by_ip,%by_location,@intdoms);
 7900:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7901:     my @locations = sort(keys(%by_location));
 7902:     my (%defaultshash,%changes);
 7903:     foreach my $prefix (@prefixes) {
 7904:         $defaultshash{'usersessions'}{$prefix} = {};
 7905:     }
 7906:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7907:     my $resulttext;
 7908:     my %iphost = &Apache::lonnet::get_iphost();
 7909:     foreach my $prefix (@prefixes) {
 7910:         next if ($prefix eq 'spares');
 7911:         foreach my $type (@{$types{$prefix}}) {
 7912:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7913:             if ($type eq 'version') {
 7914:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7915:                 my $okvalue;
 7916:                 if ($value ne '') {
 7917:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 7918:                         $okvalue = $value;
 7919:                     }
 7920:                 }
 7921:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7922:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7923:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 7924:                             if ($inuse == 0) {
 7925:                                 $changes{$prefix}{$type} = 1;
 7926:                             } else {
 7927:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 7928:                                     $changes{$prefix}{$type} = 1;
 7929:                                 }
 7930:                                 if ($okvalue ne '') {
 7931:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7932:                                 } 
 7933:                             }
 7934:                         } else {
 7935:                             if (($inuse == 1) && ($okvalue ne '')) {
 7936:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7937:                                 $changes{$prefix}{$type} = 1;
 7938:                             }
 7939:                         }
 7940:                     } else {
 7941:                         if (($inuse == 1) && ($okvalue ne '')) {
 7942:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7943:                             $changes{$prefix}{$type} = 1;
 7944:                         }
 7945:                     }
 7946:                 } else {
 7947:                     if (($inuse == 1) && ($okvalue ne '')) {
 7948:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7949:                         $changes{$prefix}{$type} = 1;
 7950:                     }
 7951:                 }
 7952:             } else {
 7953:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 7954:                 my @okvals;
 7955:                 foreach my $val (@vals) {
 7956:                     if ($val =~ /:/) {
 7957:                         my @items = split(/:/,$val);
 7958:                         foreach my $item (@items) {
 7959:                             if (ref($by_location{$item}) eq 'ARRAY') {
 7960:                                 push(@okvals,$item);
 7961:                             }
 7962:                         }
 7963:                     } else {
 7964:                         if (ref($by_location{$val}) eq 'ARRAY') {
 7965:                             push(@okvals,$val);
 7966:                         }
 7967:                     }
 7968:                 }
 7969:                 @okvals = sort(@okvals);
 7970:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7971:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7972:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7973:                             if ($inuse == 0) {
 7974:                                 $changes{$prefix}{$type} = 1; 
 7975:                             } else {
 7976:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7977:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 7978:                                 if (@changed > 0) {
 7979:                                     $changes{$prefix}{$type} = 1;
 7980:                                 }
 7981:                             }
 7982:                         } else {
 7983:                             if ($inuse == 1) {
 7984:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7985:                                 $changes{$prefix}{$type} = 1;
 7986:                             }
 7987:                         } 
 7988:                     } else {
 7989:                         if ($inuse == 1) {
 7990:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7991:                             $changes{$prefix}{$type} = 1;
 7992:                         }
 7993:                     }
 7994:                 } else {
 7995:                     if ($inuse == 1) {
 7996:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7997:                         $changes{$prefix}{$type} = 1;
 7998:                     }
 7999:                 }
 8000:             }
 8001:         }
 8002:     }
 8003: 
 8004:     my @alldoms = &Apache::lonnet::all_domains();
 8005:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8006:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 8007:     my $savespares;
 8008: 
 8009:     foreach my $lonhost (sort(keys(%servers))) {
 8010:         my $serverhomeID =
 8011:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 8012:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 8013:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 8014:         my %spareschg;
 8015:         foreach my $type (@{$types{'spares'}}) {
 8016:             my @okspares;
 8017:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 8018:             foreach my $server (@checked) {
 8019:                 if (&Apache::lonnet::hostname($server) ne '') {
 8020:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 8021:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 8022:                             push(@okspares,$server);
 8023:                         }
 8024:                     }
 8025:                 }
 8026:             }
 8027:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 8028:             my $newspare;
 8029:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 8030:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 8031:                     $newspare = $new;
 8032:                 }
 8033:             }
 8034:             my @spares;
 8035:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 8036:                 @spares = sort(@okspares,$newspare);
 8037:             } else {
 8038:                 @spares = sort(@okspares);
 8039:             }
 8040:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 8041:             if (ref($spareid{$lonhost}) eq 'HASH') {
 8042:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 8043:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 8044:                     if (@diffs > 0) {
 8045:                         $spareschg{$type} = 1;
 8046:                     }
 8047:                 }
 8048:             }
 8049:         }
 8050:         if (keys(%spareschg) > 0) {
 8051:             $changes{'spares'}{$lonhost} = \%spareschg;
 8052:         }
 8053:     }
 8054: 
 8055:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8056:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 8057:             if (ref($changes{'spares'}) eq 'HASH') {
 8058:                 if (keys(%{$changes{'spares'}}) > 0) {
 8059:                     $savespares = 1;
 8060:                 }
 8061:             }
 8062:         } else {
 8063:             $savespares = 1;
 8064:         }
 8065:     }
 8066: 
 8067:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 8068:     if ((keys(%changes) > 0) || ($savespares)) {
 8069:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8070:                                                  $dom);
 8071:         if ($putresult eq 'ok') {
 8072:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8073:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 8074:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 8075:                 }
 8076:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 8077:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 8078:                 }
 8079:             }
 8080:             my $cachetime = 24*60*60;
 8081:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8082:             if (keys(%changes) > 0) {
 8083:                 my %lt = &usersession_titles();
 8084:                 $resulttext = &mt('Changes made:').'<ul>';
 8085:                 foreach my $prefix (@prefixes) {
 8086:                     if (ref($changes{$prefix}) eq 'HASH') {
 8087:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 8088:                         if ($prefix eq 'spares') {
 8089:                             if (ref($changes{$prefix}) eq 'HASH') {
 8090:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 8091:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 8092:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 8093:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 8094:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 8095:                                         foreach my $type (@{$types{$prefix}}) {
 8096:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 8097:                                                 my $offloadto = &mt('None');
 8098:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 8099:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 8100:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 8101:                                                     }
 8102:                                                 }
 8103:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 8104:                                             }
 8105:                                         }
 8106:                                     }
 8107:                                     $resulttext .= '</li>';
 8108:                                 }
 8109:                             }
 8110:                         } else {
 8111:                             foreach my $type (@{$types{$prefix}}) {
 8112:                                 if (defined($changes{$prefix}{$type})) {
 8113:                                     my $newvalue;
 8114:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8115:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 8116:                                             if ($type eq 'version') {
 8117:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 8118:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8119:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 8120:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 8121:                                                 }
 8122:                                             }
 8123:                                         }
 8124:                                     }
 8125:                                     if ($newvalue eq '') {
 8126:                                         if ($type eq 'version') {
 8127:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8128:                                         } else {
 8129:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8130:                                         }
 8131:                                     } else {
 8132:                                         if ($type eq 'version') {
 8133:                                             $newvalue .= ' '.&mt('(or later)'); 
 8134:                                         }
 8135:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8136:                                     }
 8137:                                 }
 8138:                             }
 8139:                         }
 8140:                         $resulttext .= '</ul>';
 8141:                     }
 8142:                 }
 8143:                 $resulttext .= '</ul>';
 8144:             } else {
 8145:                 $resulttext = $nochgmsg;
 8146:             }
 8147:         } else {
 8148:             $resulttext = '<span class="LC_error">'.
 8149:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8150:         }
 8151:     } else {
 8152:         $resulttext = $nochgmsg;
 8153:     }
 8154:     return $resulttext;
 8155: }
 8156: 
 8157: sub modify_loadbalancing {
 8158:     my ($dom,%domconfig) = @_;
 8159:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8160:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8161:     my ($othertitle,$usertypes,$types) =
 8162:         &Apache::loncommon::sorted_inst_types($dom);
 8163:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8164:     my @sparestypes = ('primary','default');
 8165:     my %typetitles = &sparestype_titles();
 8166:     my $resulttext;
 8167:     my (%currbalancer,%currtargets,%currrules,%existing);
 8168:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8169:         %existing = %{$domconfig{'loadbalancing'}};
 8170:     }
 8171:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8172:                               \%currtargets,\%currrules);
 8173:     my ($saveloadbalancing,%defaultshash,%changes);
 8174:     my ($alltypes,$othertypes,$titles) =
 8175:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8176:     my %ruletitles = &offloadtype_text();
 8177:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 8178:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 8179:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 8180:         if ($balancer eq '') {
 8181:             next;
 8182:         }
 8183:         if (!exists($servers{$balancer})) { 
 8184:             if (exists($currbalancer{$balancer})) {
 8185:                 push(@{$changes{'delete'}},$balancer);
 8186:             }
 8187:             next;
 8188:         }
 8189:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 8190:             push(@{$changes{'delete'}},$balancer);
 8191:             next;
 8192:         }
 8193:         if (!exists($currbalancer{$balancer})) {
 8194:             push(@{$changes{'add'}},$balancer);
 8195:         }
 8196:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 8197:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 8198:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 8199:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8200:             $saveloadbalancing = 1;
 8201:         }
 8202:         foreach my $sparetype (@sparestypes) {
 8203:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 8204:             my @offloadto;
 8205:             foreach my $target (@targets) {
 8206:                 if (($servers{$target}) && ($target ne $balancer)) {
 8207:                     if ($sparetype eq 'default') {
 8208:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 8209:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 8210:                         }
 8211:                     }
 8212:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 8213:                         push(@offloadto,$target);
 8214:                     }
 8215:                 }
 8216:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 8217:             }
 8218:         }
 8219:         if (ref($currtargets{$balancer}) eq 'HASH') {
 8220:             foreach my $sparetype (@sparestypes) {
 8221:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 8222:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 8223:                     if (@targetdiffs > 0) {
 8224:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8225:                     }
 8226:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8227:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8228:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8229:                     }
 8230:                 }
 8231:             }
 8232:         } else {
 8233:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 8234:                 foreach my $sparetype (@sparestypes) { 
 8235:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8236:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8237:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 8238:                         }
 8239:                     }
 8240:                 }
 8241:             }  
 8242:         }
 8243:         my $ishomedom;
 8244:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 8245:             $ishomedom = 1;
 8246:         }
 8247:         if (ref($alltypes) eq 'ARRAY') {
 8248:             foreach my $type (@{$alltypes}) {
 8249:                 my $rule;
 8250:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) && 
 8251:                          (!$ishomedom)) {
 8252:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 8253:                 }
 8254:                 if ($rule eq 'specific') {
 8255:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 8256:                 }
 8257:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 8258:                 if (ref($currrules{$balancer}) eq 'HASH') {
 8259:                     if ($rule ne $currrules{$balancer}{$type}) {
 8260:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8261:                     }
 8262:                 } elsif ($rule ne '') {
 8263:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8264:                 }
 8265:             }
 8266:         }
 8267:     }
 8268:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8269:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8270:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 8271:             $defaultshash{'loadbalancing'} = {};
 8272:         }
 8273:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8274:                                                  \%defaultshash,$dom);
 8275:  
 8276:         if ($putresult eq 'ok') {
 8277:             if (keys(%changes) > 0) {
 8278:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 8279:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 8280:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 8281:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8282:                     }
 8283:                 }
 8284:                 if (ref($changes{'add'}) eq 'ARRAY') {
 8285:                     foreach my $balancer (sort(@{$changes{'add'}})) {   
 8286:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 8287:                     }
 8288:                 }
 8289:                 if (ref($changes{'curr'}) eq 'HASH') {
 8290:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 8291:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 8292:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 8293:                                 my %offloadstr;
 8294:                                 foreach my $sparetype (@sparestypes) {
 8295:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8296:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8297:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 8298:                                         }
 8299:                                     }
 8300:                                 }
 8301:                                 if (keys(%offloadstr) == 0) {
 8302:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8303:                                 } else {
 8304:                                     my $showoffload;
 8305:                                     foreach my $sparetype (@sparestypes) {
 8306:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8307:                                         if (defined($offloadstr{$sparetype})) {
 8308:                                             $showoffload .= $offloadstr{$sparetype};
 8309:                                         } else {
 8310:                                             $showoffload .= &mt('None');
 8311:                                         }
 8312:                                         $showoffload .= ('&nbsp;'x3);
 8313:                                     }
 8314:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 8315:                                 }
 8316:                             }
 8317:                         }
 8318:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 8319:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8320:                                 foreach my $type (@{$alltypes}) {
 8321:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 8322:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 8323:                                         my $balancetext;
 8324:                                         if ($rule eq '') {
 8325:                                             $balancetext =  $ruletitles{'default'};
 8326:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 8327:                                             $balancetext =  $ruletitles{$rule};
 8328:                                         } else {
 8329:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 8330:                                         }
 8331:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';     
 8332:                                     }
 8333:                                 }
 8334:                             }
 8335:                         }
 8336:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8337:                     }
 8338:                 }
 8339:                 if ($resulttext ne '') {
 8340:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8341:                 } else {
 8342:                     $resulttext = $nochgmsg;
 8343:                 }
 8344:             } else {
 8345:                 $resulttext = $nochgmsg;
 8346:             }
 8347:         } else {
 8348:             $resulttext = '<span class="LC_error">'.
 8349:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8350:         }
 8351:     } else {
 8352:         $resulttext = $nochgmsg;
 8353:     }
 8354:     return $resulttext;
 8355: }
 8356: 
 8357: sub recurse_check {
 8358:     my ($chkcats,$categories,$depth,$name) = @_;
 8359:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8360:         my $chg = 0;
 8361:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8362:             my $category = $chkcats->[$depth]{$name}[$j];
 8363:             my $item;
 8364:             if ($category eq '') {
 8365:                 $chg ++;
 8366:             } else {
 8367:                 my $deeper = $depth + 1;
 8368:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8369:                 if ($chg) {
 8370:                     $categories->{$item} -= $chg;
 8371:                 }
 8372:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8373:                 $deeper --;
 8374:             }
 8375:         }
 8376:     }
 8377:     return;
 8378: }
 8379: 
 8380: sub recurse_cat_deletes {
 8381:     my ($item,$coursecategories,$deletions) = @_;
 8382:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8383:     my $subdepth = $depth + 1;
 8384:     if (ref($coursecategories) eq 'HASH') {
 8385:         foreach my $subitem (keys(%{$coursecategories})) {
 8386:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8387:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8388:                 delete($coursecategories->{$subitem});
 8389:                 $deletions->{$subitem} = 1;
 8390:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8391:             }
 8392:         }
 8393:     }
 8394:     return;
 8395: }
 8396: 
 8397: sub get_active_dcs {
 8398:     my ($dom) = @_;
 8399:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc']);
 8400:     my %domcoords;
 8401:     my $numdcs = 0;
 8402:     my $now = time;
 8403:     foreach my $server (keys(%dompersonnel)) {
 8404:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8405:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8406:             my ($end,$start) = split(':',$dompersonnel{$server}{$user});
 8407:             if (($end eq '') || ($end == 0) || ($end > $now)) {
 8408:                 if ($start <= $now) {
 8409:                     $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8410:                 }
 8411:             }
 8412:         }
 8413:     }
 8414:     return %domcoords;
 8415: }
 8416: 
 8417: sub active_dc_picker {
 8418:     my ($dom,$curr_dc) = @_;
 8419:     my %domcoords = &get_active_dcs($dom); 
 8420:     my @dcs = sort(keys(%domcoords));
 8421:     my $numdcs = scalar(@dcs); 
 8422:     my $datatable;
 8423:     my $numinrow = 2;
 8424:     if ($numdcs > 1) {
 8425:         $datatable = '<table>';
 8426:         for (my $i=0; $i<@dcs; $i++) {
 8427:             my $rem = $i%($numinrow);
 8428:             if ($rem == 0) {
 8429:                 if ($i > 0) {
 8430:                     $datatable .= '</tr>';
 8431:                 }
 8432:                 $datatable .= '<tr>';
 8433:             }
 8434:             my $check = ' ';
 8435:             if ($curr_dc eq '') {
 8436:                 if (!$i) { 
 8437:                     $check = ' checked="checked" ';
 8438:                 }
 8439:             } elsif ($dcs[$i] eq $curr_dc) {
 8440:                 $check = ' checked="checked" ';
 8441:             }
 8442:             if ($i == @dcs - 1) {
 8443:                 my $colsleft = $numinrow - $rem;
 8444:                 if ($colsleft > 1) {
 8445:                     $datatable .= '<td colspan="'.$colsleft.'">';
 8446:                 } else {
 8447:                     $datatable .= '<td>';
 8448:                 }
 8449:             } else {
 8450:                 $datatable .= '<td>';
 8451:             }
 8452:             my ($dcname,$dcdom) = split(':',$dcs[$i]);
 8453:             $datatable .= '<span class="LC_nobreak"><label>'.
 8454:                           '<input type="radio" name="autocreate_xmldc"'.
 8455:                           ' value="'.$dcs[$i].'"'.$check.'/>'.
 8456:                           &Apache::loncommon::plainname($dcname,$dcdom).
 8457:                           '</label></span></td>';
 8458:         }
 8459:         $datatable .= '</tr></table>';
 8460:     } elsif (@dcs) {
 8461:         $datatable .= '<input type="hidden" name="autocreate_dc" value="'.
 8462:                       $dcs[0].'" />';
 8463:     }
 8464:     return ($numdcs,$datatable);
 8465: }
 8466: 
 8467: sub usersession_titles {
 8468:     return &Apache::lonlocal::texthash(
 8469:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8470:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8471:                spares => 'Servers offloaded to, when busy',
 8472:                version => 'LON-CAPA version requirement',
 8473:                excludedomain => 'Allow all, but exclude specific domains',
 8474:                includedomain => 'Deny all, but include specific domains',
 8475:                primary => 'Primary (checked first)',
 8476:                default => 'Default',
 8477:            );
 8478: }
 8479: 
 8480: sub id_for_thisdom {
 8481:     my (%servers) = @_;
 8482:     my %altids;
 8483:     foreach my $server (keys(%servers)) {
 8484:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8485:         if ($serverhome ne $server) {
 8486:             $altids{$serverhome} = $server;
 8487:         }
 8488:     }
 8489:     return %altids;
 8490: }
 8491: 
 8492: sub count_servers {
 8493:     my ($currbalancer,%servers) = @_;
 8494:     my (@spares,$numspares);
 8495:     foreach my $lonhost (sort(keys(%servers))) {
 8496:         next if ($currbalancer eq $lonhost);
 8497:         push(@spares,$lonhost);
 8498:     }
 8499:     if ($currbalancer) {
 8500:         $numspares = scalar(@spares);
 8501:     } else {
 8502:         $numspares = scalar(@spares) - 1;
 8503:     }
 8504:     return ($numspares,@spares);
 8505: }
 8506: 
 8507: sub lonbalance_targets_js {
 8508:     my ($dom,$types,$servers,$settings) = @_;
 8509:     my $select = &mt('Select');
 8510:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8511:     if (ref($servers) eq 'HASH') {
 8512:         $alltargets = join("','",sort(keys(%{$servers})));
 8513:         my @homedoms;
 8514:         foreach my $server (sort(keys(%{$servers}))) {
 8515:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8516:                 push(@homedoms,'1');
 8517:             } else {
 8518:                 push(@homedoms,'0');
 8519:             }
 8520:         }
 8521:         $allishome = join("','",@homedoms);
 8522:     }
 8523:     if (ref($types) eq 'ARRAY') {
 8524:         if (@{$types} > 0) {
 8525:             @alltypes = @{$types};
 8526:         }
 8527:     }
 8528:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8529:     $allinsttypes = join("','",@alltypes);
 8530:     my (%currbalancer,%currtargets,%currrules,%existing);
 8531:     if (ref($settings) eq 'HASH') {
 8532:         %existing = %{$settings};
 8533:     }
 8534:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 8535:                               \%currtargets,\%currrules);
 8536:     my $balancers = join("','",sort(keys(%currbalancer))); 
 8537:     return <<"END";
 8538: 
 8539: <script type="text/javascript">
 8540: // <![CDATA[
 8541: 
 8542: currBalancers = new Array('$balancers');
 8543: 
 8544: function toggleTargets(balnum) {
 8545:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8546:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 8547:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 8548:     var prevbalancer = prevhostitem.value;
 8549:     var baltotal = document.getElementById('loadbalancing_total').value;
 8550:     prevhostitem.value = balancer;
 8551:     if (prevbalancer != '') {
 8552:         var prevIdx = currBalancers.indexOf(prevbalancer);
 8553:         if (prevIdx != -1) {
 8554:             currBalancers.splice(prevIdx,1);
 8555:         }
 8556:     }
 8557:     if (balancer == '') {
 8558:         hideSpares(balnum);
 8559:     } else {
 8560:         var currIdx = currBalancers.indexOf(balancer);
 8561:         if (currIdx == -1) {
 8562:             currBalancers.push(balancer);
 8563:         }
 8564:         var homedoms = new Array('$allishome');
 8565:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 8566:         showSpares(balancer,ishomedom,balnum);
 8567:     }
 8568:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 8569:     return;
 8570: }
 8571: 
 8572: function showSpares(balancer,ishomedom,balnum) {
 8573:     var alltargets = new Array('$alltargets');
 8574:     var insttypes = new Array('$allinsttypes');
 8575:     var offloadtypes = new Array('primary','default');
 8576: 
 8577:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 8578:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 8579:  
 8580:     for (var i=0; i<offloadtypes.length; i++) {
 8581:         var count = 0;
 8582:         for (var j=0; j<alltargets.length; j++) {
 8583:             if (alltargets[j] != balancer) {
 8584:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 8585:                 item.value = alltargets[j];
 8586:                 item.style.textAlign='left';
 8587:                 item.style.textFace='normal';
 8588:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8589:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 8590:                     item.disabled = '';
 8591:                 } else {
 8592:                     item.disabled = 'disabled';
 8593:                     item.checked = false;
 8594:                 }
 8595:                 count ++;
 8596:             }
 8597:         }
 8598:     }
 8599:     for (var k=0; k<insttypes.length; k++) {
 8600:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8601:             if (ishomedom == 1) {
 8602:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8603:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8604:             } else {
 8605:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8606:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8607: 
 8608:             }
 8609:         } else {
 8610:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8611:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8612:         }
 8613:         if ((insttypes[k] != '_LC_external') && 
 8614:             ((insttypes[k] != '_LC_internetdom') ||
 8615:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8616:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 8617:             item.options.length = 0;
 8618:             item.options[0] = new Option("","",true,true);
 8619:             var idx = 0;  
 8620:             for (var m=0; m<alltargets.length; m++) {
 8621:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 8622:                     idx ++;
 8623:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8624:                     
 8625:                 }
 8626:             }
 8627:         }
 8628:     }
 8629:     return;
 8630: }
 8631: 
 8632: function hideSpares(balnum) {
 8633:     var alltargets = new Array('$alltargets');
 8634:     var insttypes = new Array('$allinsttypes');
 8635:     var offloadtypes = new Array('primary','default');
 8636: 
 8637:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 8638:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 8639: 
 8640:     var total = alltargets.length - 1;
 8641:     for (var i=0; i<offloadtypes; i++) {
 8642:         for (var j=0; j<total; j++) {
 8643:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 8644:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 8645:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8646:         }
 8647:     }
 8648:     for (var k=0; k<insttypes.length; k++) {
 8649:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8650:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8651:         if (insttypes[k] != '_LC_external') {
 8652:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 8653:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 8654:         }
 8655:     }
 8656:     return;
 8657: }
 8658: 
 8659: function checkOffloads(item,balnum,type) {
 8660:     var alltargets = new Array('$alltargets');
 8661:     var offloadtypes = new Array('primary','default');
 8662:     if (item.checked) {
 8663:         var total = alltargets.length - 1;
 8664:         var other;
 8665:         if (type == offloadtypes[0]) {
 8666:             other = offloadtypes[1];
 8667:         } else {
 8668:             other = offloadtypes[0];
 8669:         }
 8670:         for (var i=0; i<total; i++) {
 8671:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 8672:             if (server == item.value) {
 8673:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 8674:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 8675:                 }
 8676:             }
 8677:         }
 8678:     }
 8679:     return;
 8680: }
 8681: 
 8682: function singleServerToggle(balnum,type) {
 8683:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 8684:     if (offloadtoSelIdx == 0) {
 8685:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 8686:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8687: 
 8688:     } else {
 8689:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 8690:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8691:     }
 8692:     return;
 8693: }
 8694: 
 8695: function balanceruleChange(formname,balnum,type) {
 8696:     if (type == '_LC_external') {
 8697:         return;
 8698:     }
 8699:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 8700:     for (var i=0; i<typesRules.length; i++) {
 8701:         if (formname.elements[typesRules[i]].checked) {
 8702:             if (formname.elements[typesRules[i]].value != 'specific') {
 8703:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 8704:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8705:             } else {
 8706:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8707:             }
 8708:         }
 8709:     }
 8710:     return;
 8711: }
 8712: 
 8713: function balancerDeleteChange(balnum) {
 8714:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8715:     var baltotal = document.getElementById('loadbalancing_total').value;
 8716:     var addtarget;
 8717:     var removetarget;
 8718:     var action = 'delete';
 8719:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 8720:         var lonhost = hostitem.value;
 8721:         var currIdx = currBalancers.indexOf(lonhost);
 8722:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 8723:             if (currIdx != -1) {
 8724:                 currBalancers.splice(currIdx,1);
 8725:             }
 8726:             addtarget = lonhost;
 8727:         } else {
 8728:             if (currIdx == -1) {
 8729:                 currBalancers.push(lonhost);
 8730:             }
 8731:             removetarget = lonhost;
 8732:             action = 'undelete';
 8733:         }
 8734:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 8735:     }
 8736:     return;
 8737: }
 8738: 
 8739: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 8740:     if (baltotal > 1) {
 8741:         var offloadtypes = new Array('primary','default');
 8742:         var alltargets = new Array('$alltargets');
 8743:         var insttypes = new Array('$allinsttypes');
 8744:         for (var i=0; i<baltotal; i++) {
 8745:             if (i != balnum) {
 8746:                 for (var j=0; j<offloadtypes.length; j++) {
 8747:                     var total = alltargets.length - 1;
 8748:                     for (var k=0; k<total; k++) {
 8749:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 8750:                         var server = serveritem.value;
 8751:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 8752:                             if (server == addtarget) {
 8753:                                 serveritem.disabled = '';
 8754:                             }
 8755:                         }
 8756:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8757:                             if (server == removetarget) {
 8758:                                 serveritem.disabled = 'disabled';
 8759:                                 serveritem.checked = false;
 8760:                             }
 8761:                         }
 8762:                     }
 8763:                 }
 8764:                 for (var j=0; j<insttypes.length; j++) {
 8765:                     if (insttypes[j] != '_LC_external') {
 8766:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 8767:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 8768:                             var currSel = singleserver.selectedIndex;
 8769:                             var currVal = singleserver.options[currSel].value;
 8770:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 8771:                                 var numoptions = singleserver.options.length;
 8772:                                 var needsnew = 1;
 8773:                                 for (var k=0; k<numoptions; k++) {
 8774:                                     if (singleserver.options[k] == addtarget) {
 8775:                                         needsnew = 0;
 8776:                                         break;
 8777:                                     }
 8778:                                 }
 8779:                                 if (needsnew == 1) {
 8780:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 8781:                                 }
 8782:                             }
 8783:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8784:                                 singleserver.options.length = 0;
 8785:                                 if ((currVal) && (currVal != removetarget)) {
 8786:                                     singleserver.options[0] = new Option("","",false,false);
 8787:                                 } else {
 8788:                                     singleserver.options[0] = new Option("","",true,true);
 8789:                                 }
 8790:                                 var idx = 0;
 8791:                                 for (var m=0; m<alltargets.length; m++) {
 8792:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
 8793:                                         idx ++;
 8794:                                         if (currVal == alltargets[m]) {
 8795:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
 8796:                                         } else {
 8797:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8798:                                         }
 8799:                                     }
 8800:                                 }
 8801:                             }
 8802:                         }
 8803:                     }
 8804:                 }
 8805:             }
 8806:         }
 8807:     }
 8808:     return;
 8809: }
 8810: 
 8811: // ]]>
 8812: </script>
 8813: 
 8814: END
 8815: }
 8816: 
 8817: sub new_spares_js {
 8818:     my @sparestypes = ('primary','default');
 8819:     my $types = join("','",@sparestypes);
 8820:     my $select = &mt('Select');
 8821:     return <<"END";
 8822: 
 8823: <script type="text/javascript">
 8824: // <![CDATA[
 8825: 
 8826: function updateNewSpares(formname,lonhost) {
 8827:     var types = new Array('$types');
 8828:     var include = new Array();
 8829:     var exclude = new Array();
 8830:     for (var i=0; i<types.length; i++) {
 8831:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8832:         for (var j=0; j<spareboxes.length; j++) {
 8833:             if (formname.elements[spareboxes[j]].checked) {
 8834:                 exclude.push(formname.elements[spareboxes[j]].value);
 8835:             } else {
 8836:                 include.push(formname.elements[spareboxes[j]].value);
 8837:             }
 8838:         }
 8839:     }
 8840:     for (var i=0; i<types.length; i++) {
 8841:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8842:         var selIdx = newSpare.selectedIndex;
 8843:         var currnew = newSpare.options[selIdx].value;
 8844:         var okSpares = new Array();
 8845:         for (var j=0; j<newSpare.options.length; j++) {
 8846:             var possible = newSpare.options[j].value;
 8847:             if (possible != '') {
 8848:                 if (exclude.indexOf(possible) == -1) {
 8849:                     okSpares.push(possible);
 8850:                 } else {
 8851:                     if (currnew == possible) {
 8852:                         selIdx = 0;
 8853:                     }
 8854:                 }
 8855:             }
 8856:         }
 8857:         for (var k=0; k<include.length; k++) {
 8858:             if (okSpares.indexOf(include[k]) == -1) {
 8859:                 okSpares.push(include[k]);
 8860:             }
 8861:         }
 8862:         okSpares.sort();
 8863:         newSpare.options.length = 0;
 8864:         if (selIdx == 0) {
 8865:             newSpare.options[0] = new Option("$select","",true,true);
 8866:         } else {
 8867:             newSpare.options[0] = new Option("$select","",false,false);
 8868:         }
 8869:         for (var m=0; m<okSpares.length; m++) {
 8870:             var idx = m+1;
 8871:             var selThis = 0;
 8872:             if (selIdx != 0) {
 8873:                 if (okSpares[m] == currnew) {
 8874:                     selThis = 1;
 8875:                 }
 8876:             }
 8877:             if (selThis == 1) {
 8878:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8879:             } else {
 8880:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8881:             }
 8882:         }
 8883:     }
 8884:     return;
 8885: }
 8886: 
 8887: function checkNewSpares(lonhost,type) {
 8888:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8889:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8890:     if (chosen != '') { 
 8891:         var othertype;
 8892:         var othernewSpare;
 8893:         if (type == 'primary') {
 8894:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 8895:         }
 8896:         if (type == 'default') {
 8897:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 8898:         }
 8899:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 8900:             othernewSpare.selectedIndex = 0;
 8901:         }
 8902:     }
 8903:     return;
 8904: }
 8905: 
 8906: // ]]>
 8907: </script>
 8908: 
 8909: END
 8910: 
 8911: }
 8912: 
 8913: sub common_domprefs_js {
 8914:     return <<"END";
 8915: 
 8916: <script type="text/javascript">
 8917: // <![CDATA[
 8918: 
 8919: function getIndicesByName(formname,item) {
 8920:     var group = new Array();
 8921:     for (var i=0;i<formname.elements.length;i++) {
 8922:         if (formname.elements[i].name == item) {
 8923:             group.push(formname.elements[i].id);
 8924:         }
 8925:     }
 8926:     return group;
 8927: }
 8928: 
 8929: // ]]>
 8930: </script>
 8931: 
 8932: END
 8933: 
 8934: }
 8935: 
 8936: sub recaptcha_js {
 8937:     my %lt = &captcha_phrases();
 8938:     return <<"END";
 8939: 
 8940: <script type="text/javascript">
 8941: // <![CDATA[
 8942: 
 8943: function updateCaptcha(caller,context) {
 8944:     var privitem;
 8945:     var pubitem;
 8946:     var privtext;
 8947:     var pubtext;
 8948:     if (document.getElementById(context+'_recaptchapub')) {
 8949:         pubitem = document.getElementById(context+'_recaptchapub');
 8950:     } else {
 8951:         return;
 8952:     }
 8953:     if (document.getElementById(context+'_recaptchapriv')) {
 8954:         privitem = document.getElementById(context+'_recaptchapriv');
 8955:     } else {
 8956:         return;
 8957:     }
 8958:     if (document.getElementById(context+'_recaptchapubtxt')) {
 8959:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 8960:     } else {
 8961:         return;
 8962:     }
 8963:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 8964:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 8965:     } else {
 8966:         return;
 8967:     }
 8968:     if (caller.checked) {
 8969:         if (caller.value == 'recaptcha') {
 8970:             pubitem.type = 'text';
 8971:             privitem.type = 'text';
 8972:             pubitem.size = '40';
 8973:             privitem.size = '40';
 8974:             pubtext.innerHTML = "$lt{'pub'}";
 8975:             privtext.innerHTML = "$lt{'priv'}";
 8976:         } else {
 8977:             pubitem.type = 'hidden';
 8978:             privitem.type = 'hidden';
 8979:             pubtext.innerHTML = '';
 8980:             privtext.innerHTML = '';
 8981:         }
 8982:     }
 8983:     return;
 8984: }
 8985: 
 8986: // ]]>
 8987: </script>
 8988: 
 8989: END
 8990: 
 8991: }
 8992: 
 8993: sub captcha_phrases {
 8994:     return &Apache::lonlocal::texthash (
 8995:                  priv => 'Private key',
 8996:                  pub  => 'Public key',
 8997:                  original  => 'original (CAPTCHA)',
 8998:                  recaptcha => 'successor (ReCAPTCHA)',
 8999:                  notused   => 'unused',
 9000:     );
 9001: }
 9002: 
 9003: 1;

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