File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.171: download - view: text, annotated - select for diffs
Tue Sep 25 19:54:21 2012 UTC (11 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Load balancing interface extended to support management and use of multiple
  dedicated load balancing servers in a particular domain.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.171 2012/09/25 19:54:21 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:                 if (ref($settings->{'requestauthor'}) eq 'HASH') {
 1718:                     $curroption = $settings->{'requestauthor'};
 1719:                 }
 1720:             }
 1721:             if (!$curroption) {
 1722:                 $curroption = 'norequest';
 1723:             }
 1724:             foreach my $option (@options) {
 1725:                 my $val = $option;
 1726:                 if ($option eq 'norequest') {
 1727:                     $val = 0;
 1728:                 }
 1729:                 my $checked = '';
 1730:                 if ($option eq $curroption) {
 1731:                     $checked = ' checked="checked"';
 1732:                 }
 1733:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1734:                               '<input type="radio" name="authorreq_default"'.
 1735:                               ' value="'.$val.'"'.$checked.' />'.
 1736:                               $titles{$option}.'</label></span>&nbsp; ';
 1737:             }
 1738:         } else {
 1739:             my $checked = 'checked="checked" ';
 1740:             if (ref($settings) eq 'HASH') {
 1741:                 if (ref($settings->{$item}) eq 'HASH') {
 1742:                     if ($settings->{$item}->{'default'} == 0) {
 1743:                         $checked = '';
 1744:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1745:                         $checked = 'checked="checked" ';
 1746:                     }
 1747:                 }
 1748:             }
 1749:             $datatable .= '<span class="LC_nobreak"><label>'.
 1750:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1751:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1752:                           '</label></span>&nbsp; ';
 1753:         }
 1754:     }
 1755:     if ($context eq 'requestcourses') {
 1756:         $datatable .= '</tr><tr>';
 1757:         foreach my $item (@usertools) {
 1758:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1759:         }
 1760:         $datatable .= '</tr></table>';
 1761:     }
 1762:     $datatable .= '</td>';
 1763:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1764:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1765:                       '<input type="text" name="defaultquota" value="'.
 1766:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1767:     }
 1768:     $datatable .= '</tr>';
 1769:     $typecount ++;
 1770:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1771:     $datatable .= '<tr'.$css_class.'>'.
 1772:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1773:     if ($context eq 'requestcourses') {
 1774:         $datatable .= &mt('(overrides affiliation, if set)').
 1775:                       '</td>'.
 1776:                       '<td class="LC_left_item">'.
 1777:                       '<table><tr>';
 1778:     } else {
 1779:         $datatable .= &mt('(overrides affiliation, if checked)').
 1780:                       '</td>'.
 1781:                       '<td class="LC_left_item" colspan="2">'.
 1782:                       '<br />';
 1783:     }
 1784:     my %advcell;
 1785:     foreach my $item (@usertools) {
 1786:         if ($context eq 'requestcourses') {
 1787:             my ($curroption,$currlimit);
 1788:             if (ref($settings) eq 'HASH') {
 1789:                 if (ref($settings->{$item}) eq 'HASH') {
 1790:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1791:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1792:                         $currlimit = $1;
 1793:                     }
 1794:                 }
 1795:             }
 1796:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1797:             my $checked = '';
 1798:             if ($curroption eq '') {
 1799:                 $checked = ' checked="checked"';
 1800:             }
 1801:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1802:                                '<input type="radio" name="crsreq_'.$item.
 1803:                                '__LC_adv" value=""'.$checked.' />'.
 1804:                                &mt('No override set').'</label></span>&nbsp; ';
 1805:             foreach my $option (@options) {
 1806:                 my $val = $option;
 1807:                 if ($option eq 'norequest') {
 1808:                     $val = 0;
 1809:                 }
 1810:                 if ($option eq 'validate') {
 1811:                     my $canvalidate = 0;
 1812:                     if (ref($validations{$item}) eq 'HASH') {
 1813:                         if ($validations{$item}{'_LC_adv'}) {
 1814:                             $canvalidate = 1;
 1815:                         }
 1816:                     }
 1817:                     next if (!$canvalidate);
 1818:                 }
 1819:                 my $checked = '';
 1820:                 if ($val eq $curroption) {
 1821:                     $checked = ' checked="checked"';
 1822:                 } elsif ($option eq 'autolimit') {
 1823:                     if ($curroption =~ /^autolimit/) {
 1824:                         $checked = ' checked="checked"';
 1825:                     }
 1826:                 }
 1827:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1828:                                   '<input type="radio" name="crsreq_'.$item.
 1829:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1830:                                   $titles{$option}.'</label>';
 1831:                 if ($option eq 'autolimit') {
 1832:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1833:                                        $item.'_limit__LC_adv" size="1" '.
 1834:                                        'value="'.$currlimit.'" />';
 1835:                 }
 1836:                 $advcell{$item} .= '</span> ';
 1837:                 if ($option eq 'autolimit') {
 1838:                     $advcell{$item} .= $titles{'unlimited'};
 1839:                 }
 1840:             }
 1841:         } elsif ($context eq 'requestauthor') {
 1842:             my $curroption;
 1843:             if (ref($settings) eq 'HASH') {
 1844:                 $curroption = $settings->{'_LC_adv'};
 1845:             }
 1846:             my $checked = '';
 1847:             if ($curroption eq '') {
 1848:                 $checked = ' checked="checked"';
 1849:             }
 1850:             $datatable .= '<span class="LC_nobreak"><label>'.
 1851:                           '<input type="radio" name="authorreq__LC_adv"'.
 1852:                           ' value=""'.$checked.' />'.
 1853:                           &mt('No override set').'</label></span>&nbsp; ';
 1854:             foreach my $option (@options) {
 1855:                 my $val = $option;
 1856:                 if ($option eq 'norequest') {
 1857:                     $val = 0;
 1858:                 }
 1859:                 my $checked = '';
 1860:                 if ($val eq $curroption) {
 1861:                     $checked = ' checked="checked"';
 1862:                 }
 1863:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1864:                               '<input type="radio" name="crsreq_'.$item.
 1865:                               '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1866:                               $titles{$option}.'</label></span>&nbsp; ';
 1867:             }
 1868:         } else {
 1869:             my $checked = 'checked="checked" ';
 1870:             if (ref($settings) eq 'HASH') {
 1871:                 if (ref($settings->{$item}) eq 'HASH') {
 1872:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1873:                         $checked = '';
 1874:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1875:                         $checked = 'checked="checked" ';
 1876:                     }
 1877:                 }
 1878:             }
 1879:             $datatable .= '<span class="LC_nobreak"><label>'.
 1880:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1881:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1882:                           '</label></span>&nbsp; ';
 1883:         }
 1884:     }
 1885:     if ($context eq 'requestcourses') {
 1886:         $datatable .= '</tr><tr>';
 1887:         foreach my $item (@usertools) {
 1888:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1889:         }
 1890:         $datatable .= '</tr></table>';
 1891:     }
 1892:     $datatable .= '</td></tr>';
 1893:     $$rowtotal += $typecount;
 1894:     return $datatable;
 1895: }
 1896: 
 1897: sub print_requestmail {
 1898:     my ($dom,$action,$settings,$rowtotal) = @_;
 1899:     my ($now,$datatable,%dompersonnel,@domcoord,@currapproval,$rows);
 1900:     $now = time;
 1901:     $rows = 0;
 1902:     %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 1903:     foreach my $server (keys(%dompersonnel)) {
 1904:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 1905:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 1906:             if (!grep(/^$uname:$udom$/,@domcoord)) {
 1907:                 push(@domcoord,$uname.':'.$udom);
 1908:             }
 1909:         }
 1910:     }
 1911:     if (ref($settings) eq 'HASH') {
 1912:         if (ref($settings->{'notify'}) eq 'HASH') {
 1913:             if ($settings->{'notify'}{'approval'} ne '') {
 1914:                @currapproval = split(',',$settings->{'notify'}{'approval'});
 1915:             }
 1916:         }
 1917:     }
 1918:     if (@currapproval) {
 1919:         foreach my $dc (@currapproval) {
 1920:             unless (grep(/^\Q$dc\E$/,@domcoord)) {
 1921:                 push(@domcoord,$dc);
 1922:             }
 1923:         }
 1924:     }
 1925:     @domcoord = sort(@domcoord);
 1926:     my $numinrow = 4;
 1927:     my $numdc = @domcoord;
 1928:     my $css_class = 'class="LC_odd_row"';
 1929:     my $text;
 1930:     if ($action eq 'requestcourses') {
 1931:         $text = &mt('Receive notification of course requests requiring approval');
 1932:     } else {
 1933:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1934:     }
 1935:     $datatable = '<tr '.$css_class.'>'.
 1936:                  ' <td>'.$text.'</td>'.
 1937:                  ' <td class="LC_left_item">';
 1938:     if (@domcoord > 0) {
 1939:         $datatable .= '<table>';
 1940:         for (my $i=0; $i<$numdc; $i++) {
 1941:             my $rem = $i%($numinrow);
 1942:             if ($rem == 0) {
 1943:                 if ($i > 0) {
 1944:                     $datatable .= '</tr>';
 1945:                 }
 1946:                 $datatable .= '<tr>';
 1947:                 $rows ++;
 1948:             }
 1949:             my $check = ' ';
 1950:             if (grep(/^\Q$domcoord[$i]\E$/,@currapproval)) {
 1951:                 $check = ' checked="checked" ';
 1952:             }
 1953:             my ($uname,$udom) = split(':',$domcoord[$i]);
 1954:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
 1955:             if ($i == $numdc-1) {
 1956:                 my $colsleft = $numinrow-$rem;
 1957:                 if ($colsleft > 1) {
 1958:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 1959:                 } else {
 1960:                     $datatable .= '<td class="LC_left_item">';
 1961:                 }
 1962:             } else {
 1963:                 $datatable .= '<td class="LC_left_item">';
 1964:             }
 1965:             $datatable .= '<span class="LC_nobreak"><label>'.
 1966:                           '<input type="checkbox" name="reqapprovalnotify" '.
 1967:                           'value="'.$domcoord[$i].'"'.$check.'/>'.
 1968:                           $fullname.'</label></span></td>';
 1969:         }
 1970:         $datatable .= '</tr></table>';
 1971:     } else {
 1972:         $datatable .= &mt('There are no active Domain Coordinators');
 1973:         $rows ++;
 1974:     }
 1975:     $datatable .='</td></tr>';
 1976:     $$rowtotal += $rows;
 1977:     return $datatable;
 1978: }
 1979: 
 1980: sub print_autoenroll {
 1981:     my ($dom,$settings,$rowtotal) = @_;
 1982:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1983:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1984:     if (ref($settings) eq 'HASH') {
 1985:         if (exists($settings->{'run'})) {
 1986:             if ($settings->{'run'} eq '0') {
 1987:                 $runoff = ' checked="checked" ';
 1988:                 $runon = ' ';
 1989:             } else {
 1990:                 $runon = ' checked="checked" ';
 1991:                 $runoff = ' ';
 1992:             }
 1993:         } else {
 1994:             if ($autorun) {
 1995:                 $runon = ' checked="checked" ';
 1996:                 $runoff = ' ';
 1997:             } else {
 1998:                 $runoff = ' checked="checked" ';
 1999:                 $runon = ' ';
 2000:             }
 2001:         }
 2002:         if (exists($settings->{'co-owners'})) {
 2003:             if ($settings->{'co-owners'} eq '0') {
 2004:                 $coownersoff = ' checked="checked" ';
 2005:                 $coownerson = ' ';
 2006:             } else {
 2007:                 $coownerson = ' checked="checked" ';
 2008:                 $coownersoff = ' ';
 2009:             }
 2010:         } else {
 2011:             $coownersoff = ' checked="checked" ';
 2012:             $coownerson = ' ';
 2013:         }
 2014:         if (exists($settings->{'sender_domain'})) {
 2015:             $defdom = $settings->{'sender_domain'};
 2016:         }
 2017:     } else {
 2018:         if ($autorun) {
 2019:             $runon = ' checked="checked" ';
 2020:             $runoff = ' ';
 2021:         } else {
 2022:             $runoff = ' checked="checked" ';
 2023:             $runon = ' ';
 2024:         }
 2025:     }
 2026:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2027:     my $notif_sender;
 2028:     if (ref($settings) eq 'HASH') {
 2029:         $notif_sender = $settings->{'sender_uname'};
 2030:     }
 2031:     my $datatable='<tr class="LC_odd_row">'.
 2032:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2033:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2034:                   '<input type="radio" name="autoenroll_run"'.
 2035:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2036:                   '<label><input type="radio" name="autoenroll_run"'.
 2037:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2038:                   '</tr><tr>'.
 2039:                   '<td>'.&mt('Notification messages - sender').
 2040:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2041:                   &mt('username').':&nbsp;'.
 2042:                   '<input type="text" name="sender_uname" value="'.
 2043:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2044:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2045:                   '<tr class="LC_odd_row">'.
 2046:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2047:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2048:                   '<input type="radio" name="autoassign_coowners"'.
 2049:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2050:                   '<label><input type="radio" name="autoassign_coowners"'.
 2051:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2052:                   '</tr>';
 2053:     $$rowtotal += 3;
 2054:     return $datatable;
 2055: }
 2056: 
 2057: sub print_autoupdate {
 2058:     my ($position,$dom,$settings,$rowtotal) = @_;
 2059:     my $datatable;
 2060:     if ($position eq 'top') {
 2061:         my $updateon = ' ';
 2062:         my $updateoff = ' checked="checked" ';
 2063:         my $classlistson = ' ';
 2064:         my $classlistsoff = ' checked="checked" ';
 2065:         if (ref($settings) eq 'HASH') {
 2066:             if ($settings->{'run'} eq '1') {
 2067:                 $updateon = $updateoff;
 2068:                 $updateoff = ' ';
 2069:             }
 2070:             if ($settings->{'classlists'} eq '1') {
 2071:                 $classlistson = $classlistsoff;
 2072:                 $classlistsoff = ' ';
 2073:             }
 2074:         }
 2075:         my %title = (
 2076:                    run => 'Auto-update active?',
 2077:                    classlists => 'Update information in classlists?',
 2078:                     );
 2079:         $datatable = '<tr class="LC_odd_row">'. 
 2080:                   '<td>'.&mt($title{'run'}).'</td>'.
 2081:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2082:                   '<input type="radio" name="autoupdate_run"'.
 2083:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2084:                   '<label><input type="radio" name="autoupdate_run"'.
 2085:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2086:                   '</tr><tr>'.
 2087:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2088:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2089:                   '<label><input type="radio" name="classlists"'.
 2090:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2091:                   '<label><input type="radio" name="classlists"'.
 2092:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2093:                   '</tr>';
 2094:         $$rowtotal += 2;
 2095:     } elsif ($position eq 'middle') {
 2096:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2097:         my $numinrow = 3;
 2098:         my $locknamesettings;
 2099:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2100:                                      $dom,$numinrow,$othertitle,
 2101:                                     'lockablenames');
 2102:         $$rowtotal ++;
 2103:     } else {
 2104:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2105:         my @fields = ('lastname','firstname','middlename','generation',
 2106:                       'permanentemail','id');
 2107:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2108:         my $numrows = 0;
 2109:         if (ref($types) eq 'ARRAY') {
 2110:             if (@{$types} > 0) {
 2111:                 $datatable = 
 2112:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2113:                                          \@fields,$types,\$numrows);
 2114:                     $$rowtotal += @{$types}; 
 2115:             }
 2116:         }
 2117:         $datatable .= 
 2118:             &usertype_update_row($settings,{'default' => $othertitle},
 2119:                                  \%fieldtitles,\@fields,['default'],
 2120:                                  \$numrows);
 2121:         $$rowtotal ++;     
 2122:     }
 2123:     return $datatable;
 2124: }
 2125: 
 2126: sub print_autocreate {
 2127:     my ($dom,$settings,$rowtotal) = @_;
 2128:     my (%createon,%createoff);
 2129:     my $curr_dc;
 2130:     my @types = ('xml','req');
 2131:     if (ref($settings) eq 'HASH') {
 2132:         foreach my $item (@types) {
 2133:             $createoff{$item} = ' checked="checked" ';
 2134:             $createon{$item} = ' ';
 2135:             if (exists($settings->{$item})) {
 2136:                 if ($settings->{$item}) {
 2137:                     $createon{$item} = ' checked="checked" ';
 2138:                     $createoff{$item} = ' ';
 2139:                 }
 2140:             }
 2141:         }
 2142:         $curr_dc = $settings->{'xmldc'};
 2143:     } else {
 2144:         foreach my $item (@types) {
 2145:             $createoff{$item} = ' checked="checked" ';
 2146:             $createon{$item} = ' ';
 2147:         }
 2148:     }
 2149:     $$rowtotal += 2;
 2150:     my $datatable='<tr class="LC_odd_row">'.
 2151:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2152:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2153:                   '<input type="radio" name="autocreate_xml"'.
 2154:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2155:                   '<label><input type="radio" name="autocreate_xml"'.
 2156:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2157:                   '</td></tr><tr>'.
 2158:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2159:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2160:                   '<input type="radio" name="autocreate_req"'.
 2161:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2162:                   '<label><input type="radio" name="autocreate_req"'.
 2163:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2164:     my ($numdc,$dctable) = &active_dc_picker($dom,$curr_dc);
 2165:     if ($numdc > 1) {
 2166:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2167:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2168:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2169:         $$rowtotal ++ ;
 2170:     } else {
 2171:         $datatable .= $dctable.'</td></tr>';
 2172:     }
 2173:     return $datatable;
 2174: }
 2175: 
 2176: sub print_directorysrch {
 2177:     my ($dom,$settings,$rowtotal) = @_;
 2178:     my $srchon = ' ';
 2179:     my $srchoff = ' checked="checked" ';
 2180:     my ($exacton,$containson,$beginson);
 2181:     my $localon = ' ';
 2182:     my $localoff = ' checked="checked" ';
 2183:     if (ref($settings) eq 'HASH') {
 2184:         if ($settings->{'available'} eq '1') {
 2185:             $srchon = $srchoff;
 2186:             $srchoff = ' ';
 2187:         }
 2188:         if ($settings->{'localonly'} eq '1') {
 2189:             $localon = $localoff;
 2190:             $localoff = ' ';
 2191:         }
 2192:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2193:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2194:                 if ($type eq 'exact') {
 2195:                     $exacton = ' checked="checked" ';
 2196:                 } elsif ($type eq 'contains') {
 2197:                     $containson = ' checked="checked" ';
 2198:                 } elsif ($type eq 'begins') {
 2199:                     $beginson = ' checked="checked" ';
 2200:                 }
 2201:             }
 2202:         } else {
 2203:             if ($settings->{'searchtypes'} eq 'exact') {
 2204:                 $exacton = ' checked="checked" ';
 2205:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2206:                 $containson = ' checked="checked" ';
 2207:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2208:                 $exacton = ' checked="checked" ';
 2209:                 $containson = ' checked="checked" ';
 2210:             }
 2211:         }
 2212:     }
 2213:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2214:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2215: 
 2216:     my $numinrow = 4;
 2217:     my $cansrchrow = 0;
 2218:     my $datatable='<tr class="LC_odd_row">'.
 2219:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2220:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2221:                   '<input type="radio" name="dirsrch_available"'.
 2222:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2223:                   '<label><input type="radio" name="dirsrch_available"'.
 2224:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2225:                   '</tr><tr>'.
 2226:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2227:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2228:                   '<input type="radio" name="dirsrch_localonly"'.
 2229:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2230:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2231:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2232:                   '</tr>';
 2233:     $$rowtotal += 2;
 2234:     if (ref($usertypes) eq 'HASH') {
 2235:         if (keys(%{$usertypes}) > 0) {
 2236:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2237:                                          $numinrow,$othertitle,'cansearch');
 2238:             $cansrchrow = 1;
 2239:         }
 2240:     }
 2241:     if ($cansrchrow) {
 2242:         $$rowtotal ++;
 2243:         $datatable .= '<tr>';
 2244:     } else {
 2245:         $datatable .= '<tr class="LC_odd_row">';
 2246:     }
 2247:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2248:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2249:     foreach my $title (@{$titleorder}) {
 2250:         if (defined($searchtitles->{$title})) {
 2251:             my $check = ' ';
 2252:             if (ref($settings) eq 'HASH') {
 2253:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2254:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2255:                         $check = ' checked="checked" ';
 2256:                     }
 2257:                 }
 2258:             }
 2259:             $datatable .= '<td class="LC_left_item">'.
 2260:                           '<span class="LC_nobreak"><label>'.
 2261:                           '<input type="checkbox" name="searchby" '.
 2262:                           'value="'.$title.'"'.$check.'/>'.
 2263:                           $searchtitles->{$title}.'</label></span></td>';
 2264:         }
 2265:     }
 2266:     $datatable .= '</tr></table></td></tr>';
 2267:     $$rowtotal ++;
 2268:     if ($cansrchrow) {
 2269:         $datatable .= '<tr class="LC_odd_row">';
 2270:     } else {
 2271:         $datatable .= '<tr>';
 2272:     }
 2273:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2274:                   '<td class="LC_left_item" colspan="2">'.
 2275:                   '<span class="LC_nobreak"><label>'.
 2276:                   '<input type="checkbox" name="searchtypes" '.
 2277:                   $exacton.' value="exact" />'.&mt('Exact match').
 2278:                   '</label>&nbsp;'.
 2279:                   '<label><input type="checkbox" name="searchtypes" '.
 2280:                   $beginson.' value="begins" />'.&mt('Begins with').
 2281:                   '</label>&nbsp;'.
 2282:                   '<label><input type="checkbox" name="searchtypes" '.
 2283:                   $containson.' value="contains" />'.&mt('Contains').
 2284:                   '</label></span></td></tr>';
 2285:     $$rowtotal ++;
 2286:     return $datatable;
 2287: }
 2288: 
 2289: sub print_contacts {
 2290:     my ($dom,$settings,$rowtotal) = @_;
 2291:     my $datatable;
 2292:     my @contacts = ('adminemail','supportemail');
 2293:     my (%checked,%to,%otheremails,%bccemails);
 2294:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2295:                     'requestsmail');
 2296:     foreach my $type (@mailings) {
 2297:         $otheremails{$type} = '';
 2298:     }
 2299:     $bccemails{'helpdeskmail'} = '';
 2300:     if (ref($settings) eq 'HASH') {
 2301:         foreach my $item (@contacts) {
 2302:             if (exists($settings->{$item})) {
 2303:                 $to{$item} = $settings->{$item};
 2304:             }
 2305:         }
 2306:         foreach my $type (@mailings) {
 2307:             if (exists($settings->{$type})) {
 2308:                 if (ref($settings->{$type}) eq 'HASH') {
 2309:                     foreach my $item (@contacts) {
 2310:                         if ($settings->{$type}{$item}) {
 2311:                             $checked{$type}{$item} = ' checked="checked" ';
 2312:                         }
 2313:                     }
 2314:                     $otheremails{$type} = $settings->{$type}{'others'};
 2315:                     if ($type eq 'helpdeskmail') {
 2316:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2317:                     }
 2318:                 }
 2319:             } elsif ($type eq 'lonstatusmail') {
 2320:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2321:             }
 2322:         }
 2323:     } else {
 2324:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2325:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2326:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2327:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2328:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2329:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2330:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2331:     }
 2332:     my ($titles,$short_titles) = &contact_titles();
 2333:     my $rownum = 0;
 2334:     my $css_class;
 2335:     foreach my $item (@contacts) {
 2336:         $rownum ++;
 2337:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2338:         $datatable .= '<tr'.$css_class.'>'. 
 2339:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2340:                   '</span></td><td class="LC_right_item">'.
 2341:                   '<input type="text" name="'.$item.'" value="'.
 2342:                   $to{$item}.'" /></td></tr>';
 2343:     }
 2344:     foreach my $type (@mailings) {
 2345:         $rownum ++;
 2346:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2347:         $datatable .= '<tr'.$css_class.'>'.
 2348:                       '<td><span class="LC_nobreak">'.
 2349:                       $titles->{$type}.': </span></td>'.
 2350:                       '<td class="LC_left_item">'.
 2351:                       '<span class="LC_nobreak">';
 2352:         foreach my $item (@contacts) {
 2353:             $datatable .= '<label>'.
 2354:                           '<input type="checkbox" name="'.$type.'"'.
 2355:                           $checked{$type}{$item}.
 2356:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2357:                           '</label>&nbsp;';
 2358:         }
 2359:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2360:                       '<input type="text" name="'.$type.'_others" '.
 2361:                       'value="'.$otheremails{$type}.'"  />';
 2362:         if ($type eq 'helpdeskmail') {
 2363:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2364:                           '<input type="text" name="'.$type.'_bcc" '.
 2365:                           'value="'.$bccemails{$type}.'"  />';
 2366:         }
 2367:         $datatable .= '</td></tr>'."\n";
 2368:     }
 2369:     $$rowtotal += $rownum;
 2370:     return $datatable;
 2371: }
 2372: 
 2373: sub print_helpsettings {
 2374:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2375:     my ($datatable,$itemcount);
 2376:     $itemcount = 1;
 2377:     my (%choices,%defaultchecked,@toggles);
 2378:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2379:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2380:                                  &mt('LON-CAPA bug tracker'),600,500));
 2381:     %defaultchecked = ('submitbugs' => 'on');
 2382:     @toggles = ('submitbugs',);
 2383: 
 2384:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2385:                                                  \%choices,$itemcount);
 2386:     return $datatable;
 2387: }
 2388: 
 2389: sub radiobutton_prefs {
 2390:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount) = @_;
 2391:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2392:                    (ref($choices) eq 'HASH'));
 2393: 
 2394:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2395: 
 2396:     foreach my $item (@{$toggles}) {
 2397:         if ($defaultchecked->{$item} eq 'on') {
 2398:             $checkedon{$item} = ' checked="checked" ';
 2399:             $checkedoff{$item} = ' ';
 2400:         } elsif ($defaultchecked->{$item} eq 'off') {
 2401:             $checkedoff{$item} = ' checked="checked" ';
 2402:             $checkedon{$item} = ' ';
 2403:         }
 2404:     }
 2405:     if (ref($settings) eq 'HASH') {
 2406:         foreach my $item (@{$toggles}) {
 2407:             if ($settings->{$item} eq '1') {
 2408:                 $checkedon{$item} =  ' checked="checked" ';
 2409:                 $checkedoff{$item} = ' ';
 2410:             } elsif ($settings->{$item} eq '0') {
 2411:                 $checkedoff{$item} =  ' checked="checked" ';
 2412:                 $checkedon{$item} = ' ';
 2413:             }
 2414:         }
 2415:     }
 2416:     foreach my $item (@{$toggles}) {
 2417:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2418:         $datatable .=
 2419:             '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices->{$item}.
 2420:             '</span></td>'.
 2421:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2422:             '<label><input type="radio" name="'.
 2423:             $item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').
 2424:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2425:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2426:             '</span></td>'.
 2427:             '</tr>';
 2428:         $itemcount ++;
 2429:     }
 2430:     return ($datatable,$itemcount);
 2431: }
 2432: 
 2433: sub print_coursedefaults {
 2434:     my ($position,$dom,$settings,$rowtotal) = @_;
 2435:     my ($css_class,$datatable);
 2436:     my $itemcount = 1;
 2437:     if ($position eq 'top') {
 2438:         my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2439:         %choices =
 2440:             &Apache::lonlocal::texthash (
 2441:                 canuse_pdfforms => 'Course/Community users can create/upload PDF forms',
 2442:         );
 2443:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2444:         @toggles = ('canuse_pdfforms',);
 2445:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2446:                                                  \%choices,$itemcount);
 2447:         $$rowtotal += $itemcount;
 2448:     } else {
 2449:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2450:         my %choices =
 2451:             &Apache::lonlocal::texthash (
 2452:                 anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2453:         );
 2454:         my $currdefresponder;
 2455:         if (ref($settings) eq 'HASH') {
 2456:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2457:         }
 2458:         if (!$currdefresponder) {
 2459:             $currdefresponder = 10;
 2460:         } elsif ($currdefresponder < 1) {
 2461:             $currdefresponder = 1;
 2462:         }
 2463:         $datatable .=
 2464:                '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices{'anonsurvey_threshold'}.
 2465:                 '</span></td>'.
 2466:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2467:                 '<input type="text" name="anonsurvey_threshold"'.
 2468:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2469:                 '</td></tr>';
 2470:     }
 2471:     return $datatable;
 2472: }
 2473: 
 2474: sub print_usersessions {
 2475:     my ($position,$dom,$settings,$rowtotal) = @_;
 2476:     my ($css_class,$datatable,%checked,%choices);
 2477:     my (%by_ip,%by_location,@intdoms);
 2478:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2479: 
 2480:     my @alldoms = &Apache::lonnet::all_domains();
 2481:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2482:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2483:     my %altids = &id_for_thisdom(%servers);
 2484:     my $itemcount = 1;
 2485:     if ($position eq 'top') {
 2486:         if (keys(%serverhomes) > 1) {
 2487:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2488:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2489:         } else {
 2490:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2491:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2492:         }
 2493:     } else {
 2494:         if (keys(%by_location) == 0) {
 2495:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2496:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2497:         } else {
 2498:             my %lt = &usersession_titles();
 2499:             my $numinrow = 5;
 2500:             my $prefix;
 2501:             my @types;
 2502:             if ($position eq 'bottom') {
 2503:                 $prefix = 'remote';
 2504:                 @types = ('version','excludedomain','includedomain');
 2505:             } else {
 2506:                 $prefix = 'hosted';
 2507:                 @types = ('excludedomain','includedomain');
 2508:             }
 2509:             my (%current,%checkedon,%checkedoff);
 2510:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2511:             my @locations = sort(keys(%by_location));
 2512:             foreach my $type (@types) {
 2513:                 $checkedon{$type} = '';
 2514:                 $checkedoff{$type} = ' checked="checked"';
 2515:             }
 2516:             if (ref($settings) eq 'HASH') {
 2517:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2518:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2519:                         $current{$key} = $settings->{$prefix}{$key};
 2520:                         if ($key eq 'version') {
 2521:                             if ($current{$key} ne '') {
 2522:                                 $checkedon{$key} = ' checked="checked"';
 2523:                                 $checkedoff{$key} = '';
 2524:                             }
 2525:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2526:                             $checkedon{$key} = ' checked="checked"';
 2527:                             $checkedoff{$key} = '';
 2528:                         }
 2529:                     }
 2530:                 }
 2531:             }
 2532:             foreach my $type (@types) {
 2533:                 next if ($type ne 'version' && !@locations);
 2534:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2535:                 $datatable .= '<tr'.$css_class.'>
 2536:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2537:                                <span class="LC_nobreak">&nbsp;
 2538:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2539:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2540:                 if ($type eq 'version') {
 2541:                     my $selector = '<select name="'.$prefix.'_version">';
 2542:                     foreach my $version (@lcversions) {
 2543:                         my $selected = '';
 2544:                         if ($current{'version'} eq $version) {
 2545:                             $selected = ' selected="selected"';
 2546:                         }
 2547:                         $selector .= ' <option value="'.$version.'"'.
 2548:                                      $selected.'>'.$version.'</option>';
 2549:                     }
 2550:                     $selector .= '</select> ';
 2551:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2552:                 } else {
 2553:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2554:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2555:                                  ' />'.('&nbsp;'x2).
 2556:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2557:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2558:                                  "\n".
 2559:                                  '</div><div><table>';
 2560:                     my $rem;
 2561:                     for (my $i=0; $i<@locations; $i++) {
 2562:                         my ($showloc,$value,$checkedtype);
 2563:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2564:                             my $ip = $by_location{$locations[$i]}->[0];
 2565:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2566:                                  $value = join(':',@{$by_ip{$ip}});
 2567:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2568:                                 if (ref($current{$type}) eq 'ARRAY') {
 2569:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2570:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2571:                                             $checkedtype = ' checked="checked"';
 2572:                                             last;
 2573:                                         }
 2574:                                     }
 2575:                                 }
 2576:                             }
 2577:                         }
 2578:                         $rem = $i%($numinrow);
 2579:                         if ($rem == 0) {
 2580:                             if ($i > 0) {
 2581:                                 $datatable .= '</tr>';
 2582:                             }
 2583:                             $datatable .= '<tr>';
 2584:                         }
 2585:                         $datatable .= '<td class="LC_left_item">'.
 2586:                                       '<span class="LC_nobreak"><label>'.
 2587:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2588:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2589:                                       '</label></span></td>';
 2590:                     }
 2591:                     $rem = @locations%($numinrow);
 2592:                     my $colsleft = $numinrow - $rem;
 2593:                     if ($colsleft > 1 ) {
 2594:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2595:                                       '&nbsp;</td>';
 2596:                     } elsif ($colsleft == 1) {
 2597:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2598:                     }
 2599:                     $datatable .= '</tr></table>';
 2600:                 }
 2601:                 $datatable .= '</td></tr>';
 2602:                 $itemcount ++;
 2603:             }
 2604:         }
 2605:     }
 2606:     $$rowtotal += $itemcount;
 2607:     return $datatable;
 2608: }
 2609: 
 2610: sub build_location_hashes {
 2611:     my ($intdoms,$by_ip,$by_location) = @_;
 2612:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2613:                   (ref($by_location) eq 'HASH')); 
 2614:     my %iphost = &Apache::lonnet::get_iphost();
 2615:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2616:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2617:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2618:         foreach my $id (@{$iphost{$primary_ip}}) {
 2619:             my $intdom = &Apache::lonnet::internet_dom($id);
 2620:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2621:                 push(@{$intdoms},$intdom);
 2622:             }
 2623:         }
 2624:     }
 2625:     foreach my $ip (keys(%iphost)) {
 2626:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2627:             foreach my $id (@{$iphost{$ip}}) {
 2628:                 my $location = &Apache::lonnet::internet_dom($id);
 2629:                 if ($location) {
 2630:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2631:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2632:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2633:                             push(@{$by_ip->{$ip}},$location);
 2634:                         }
 2635:                     } else {
 2636:                         $by_ip->{$ip} = [$location];
 2637:                     }
 2638:                 }
 2639:             }
 2640:         }
 2641:     }
 2642:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2643:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2644:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2645:             my $first = $by_ip->{$ip}->[0];
 2646:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2647:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2648:                     push(@{$by_location->{$first}},$ip);
 2649:                 }
 2650:             } else {
 2651:                 $by_location->{$first} = [$ip];
 2652:             }
 2653:         }
 2654:     }
 2655:     return;
 2656: }
 2657: 
 2658: sub current_offloads_to {
 2659:     my ($dom,$settings,$servers) = @_;
 2660:     my (%spareid,%otherdomconfigs);
 2661:     if (ref($servers) eq 'HASH') {
 2662:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2663:             my $gotspares;
 2664:             if (ref($settings) eq 'HASH') {
 2665:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2666:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2667:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2668:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2669:                         $gotspares = 1;
 2670:                     }
 2671:                 }
 2672:             }
 2673:             unless ($gotspares) {
 2674:                 my $gotspares;
 2675:                 my $serverhomeID =
 2676:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2677:                 my $serverhomedom =
 2678:                     &Apache::lonnet::host_domain($serverhomeID);
 2679:                 if ($serverhomedom ne $dom) {
 2680:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2681:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2682:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2683:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2684:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2685:                                 $gotspares = 1;
 2686:                             }
 2687:                         }
 2688:                     } else {
 2689:                         $otherdomconfigs{$serverhomedom} =
 2690:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2691:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2692:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2693:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2694:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2695:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2696:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2697:                                         $gotspares = 1;
 2698:                                     }
 2699:                                 }
 2700:                             }
 2701:                         }
 2702:                     }
 2703:                 }
 2704:             }
 2705:             unless ($gotspares) {
 2706:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2707:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2708:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2709:                } else {
 2710:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2711:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2712:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2713:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2714:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2715:                     } else {
 2716:                         my %what = (
 2717:                              spareid => 1,
 2718:                         );
 2719:                         my ($result,$returnhash) = 
 2720:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2721:                         if ($result eq 'ok') { 
 2722:                             if (ref($returnhash) eq 'HASH') {
 2723:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2724:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2725:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2726:                                 }
 2727:                             }
 2728:                         }
 2729:                     }
 2730:                 }
 2731:             }
 2732:         }
 2733:     }
 2734:     return %spareid;
 2735: }
 2736: 
 2737: sub spares_row {
 2738:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2739:     my $css_class;
 2740:     my $numinrow = 4;
 2741:     my $itemcount = 1;
 2742:     my $datatable;
 2743:     my %typetitles = &sparestype_titles();
 2744:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2745:         foreach my $server (sort(keys(%{$servers}))) {
 2746:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2747:             my ($othercontrol,$serverdom);
 2748:             if ($serverhome ne $server) {
 2749:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2750:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2751:             } else {
 2752:                 $serverdom = &Apache::lonnet::host_domain($server);
 2753:                 if ($serverdom ne $dom) {
 2754:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2755:                 }
 2756:             }
 2757:             next unless (ref($spareid->{$server}) eq 'HASH');
 2758:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2759:             $datatable .= '<tr'.$css_class.'>
 2760:                            <td rowspan="2">
 2761:                             <span class="LC_nobreak"><b>'.$server.'</b> when busy, offloads to:</span></td>'."\n";
 2762:             my (%current,%canselect);
 2763:             my @choices = 
 2764:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2765:             foreach my $type ('primary','default') {
 2766:                 if (ref($spareid->{$server}) eq 'HASH') {
 2767:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2768:                         my @spares = @{$spareid->{$server}{$type}};
 2769:                         if (@spares > 0) {
 2770:                             if ($othercontrol) {
 2771:                                 $current{$type} = join(', ',@spares);
 2772:                             } else {
 2773:                                 $current{$type} .= '<table>';
 2774:                                 my $numspares = scalar(@spares);
 2775:                                 for (my $i=0;  $i<@spares; $i++) {
 2776:                                     my $rem = $i%($numinrow);
 2777:                                     if ($rem == 0) {
 2778:                                         if ($i > 0) {
 2779:                                             $current{$type} .= '</tr>';
 2780:                                         }
 2781:                                         $current{$type} .= '<tr>';
 2782:                                     }
 2783:                                     $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;'.
 2784:                                                        $spareid->{$server}{$type}[$i].
 2785:                                                        '</label></td>'."\n";
 2786:                                 }
 2787:                                 my $rem = @spares%($numinrow);
 2788:                                 my $colsleft = $numinrow - $rem;
 2789:                                 if ($colsleft > 1 ) {
 2790:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2791:                                                        '" class="LC_left_item">'.
 2792:                                                        '&nbsp;</td>';
 2793:                                 } elsif ($colsleft == 1) {
 2794:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2795:                                 }
 2796:                                 $current{$type} .= '</tr></table>';
 2797:                             }
 2798:                         }
 2799:                     }
 2800:                     if ($current{$type} eq '') {
 2801:                         $current{$type} = &mt('None specified');
 2802:                     }
 2803:                     if ($othercontrol) {
 2804:                         if ($type eq 'primary') {
 2805:                             $canselect{$type} = $othercontrol;
 2806:                         }
 2807:                     } else {
 2808:                         $canselect{$type} = 
 2809:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2810:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2811:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2812:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2813:                         if (@choices > 0) {
 2814:                             foreach my $lonhost (@choices) {
 2815:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2816:                             }
 2817:                         }
 2818:                         $canselect{$type} .= '</select>'."\n";
 2819:                     }
 2820:                 } else {
 2821:                     $current{$type} = &mt('Could not be determined');
 2822:                     if ($type eq 'primary') {
 2823:                         $canselect{$type} =  $othercontrol;
 2824:                     }
 2825:                 }
 2826:                 if ($type eq 'default') {
 2827:                     $datatable .= '<tr'.$css_class.'>';
 2828:                 }
 2829:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2830:                               '<td>'.$current{$type}.'</td>'."\n".
 2831:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2832:             }
 2833:             $itemcount ++;
 2834:         }
 2835:     }
 2836:     $$rowtotal += $itemcount;
 2837:     return $datatable;
 2838: }
 2839: 
 2840: sub possible_newspares {
 2841:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2842:     my $serverhostname = &Apache::lonnet::hostname($server);
 2843:     my %excluded;
 2844:     if ($serverhostname ne '') {
 2845:         %excluded = (
 2846:                        $serverhostname => 1,
 2847:                     );
 2848:     }
 2849:     if (ref($currspares) eq 'HASH') {
 2850:         foreach my $type (keys(%{$currspares})) {
 2851:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2852:                 if (@{$currspares->{$type}} > 0) {
 2853:                     foreach my $curr (@{$currspares->{$type}}) {
 2854:                         my $hostname = &Apache::lonnet::hostname($curr);
 2855:                         $excluded{$hostname} = 1;
 2856:                     }
 2857:                 }
 2858:             }
 2859:         }
 2860:     }
 2861:     my @choices;
 2862:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2863:         if (keys(%{$serverhomes}) > 1) {
 2864:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2865:                 unless ($excluded{$name}) {
 2866:                     if (exists($altids->{$serverhomes->{$name}})) {
 2867:                         push(@choices,$altids->{$serverhomes->{$name}});
 2868:                     } else {
 2869:                         push(@choices,$serverhomes->{$name});
 2870:                     }
 2871:                 }
 2872:             }
 2873:         }
 2874:     }
 2875:     return sort(@choices);
 2876: }
 2877: 
 2878: sub print_loadbalancing {
 2879:     my ($dom,$settings,$rowtotal) = @_;
 2880:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2881:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2882:     my $numinrow = 1;
 2883:     my $datatable;
 2884:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2885:     my (%currbalancer,%currtargets,%currrules,%existing);
 2886:     if (ref($settings) eq 'HASH') {
 2887:         %existing = %{$settings};
 2888:     }
 2889:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 2890:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 2891:                                   \%currtargets,\%currrules);
 2892:     } else {
 2893:         return;
 2894:     }
 2895:     my ($othertitle,$usertypes,$types) =
 2896:         &Apache::loncommon::sorted_inst_types($dom);
 2897:     my $rownum = 6;
 2898:     if (ref($types) eq 'ARRAY') {
 2899:         $rownum += scalar(@{$types});
 2900:     }
 2901:     my @css_class = ('LC_odd_row','LC_even_row');
 2902:     my $balnum = 0;
 2903:     my $islast;
 2904:     my (@toshow,$disabledtext);
 2905:     if (keys(%currbalancer) > 0) {
 2906:         @toshow = sort(keys(%currbalancer));
 2907:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 2908:             push(@toshow,'');
 2909:         }
 2910:     } else {
 2911:         @toshow = ('');
 2912:         $disabledtext = &mt('No existing load balancer');
 2913:     }
 2914:     foreach my $lonhost (@toshow) {
 2915:         if ($balnum == scalar(@toshow)-1) {
 2916:             $islast = 1;
 2917:         } else {
 2918:             $islast = 0;
 2919:         }
 2920:         my $cssidx = $balnum%2;
 2921:         my $targets_div_style = 'display: none';
 2922:         my $disabled_div_style = 'display: block';
 2923:         my $homedom_div_style = 'display: none';
 2924:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 2925:                       '<td rowspan="'.$rownum.'" valign="top">'.
 2926:                       '<p>';
 2927:         if ($lonhost eq '') {
 2928:             $datatable .= '<span class="LC_nobreak">'; 
 2929:             if (keys(%currbalancer) > 0) {
 2930:                 $datatable .= &mt('Add balancer:');
 2931:             } else {
 2932:                 $datatable .= &mt('Enable balancer:');
 2933:             }
 2934:             $datatable .= '&nbsp;'.
 2935:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 2936:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 2937:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 2938:                           '<option value="" selected="selected">'.&mt('None').
 2939:                           '</option>'."\n";
 2940:             foreach my $server (sort(keys(%servers))) {
 2941:                 next if ($currbalancer{$server});
 2942:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 2943:             }
 2944:             $datatable .= 
 2945:                 '</select>'."\n".
 2946:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 2947:         } else {
 2948:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 2949:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 2950:                            &mt('Stop balancing').'</label>'.
 2951:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 2952:             $targets_div_style = 'display: block';
 2953:             $disabled_div_style = 'display: none';
 2954:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 2955:                 $homedom_div_style = 'display: block';
 2956:             }
 2957:         }
 2958:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 2959:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 2960:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 2961:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2962:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 2963:         my @sparestypes = ('primary','default');
 2964:         my %typetitles = &sparestype_titles();
 2965:         foreach my $sparetype (@sparestypes) {
 2966:             my $targettable;
 2967:             for (my $i=0; $i<$numspares; $i++) {
 2968:                 my $checked;
 2969:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 2970:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 2971:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 2972:                             $checked = ' checked="checked"';
 2973:                         }
 2974:                     }
 2975:                 }
 2976:                 my ($chkboxval,$disabled);
 2977:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 2978:                     $chkboxval = $spares[$i];
 2979:                 }
 2980:                 if (exists($currbalancer{$spares[$i]})) {
 2981:                     $disabled = ' disabled="disabled"';
 2982:                 }
 2983:                 $targettable .= 
 2984:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 2985:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 2986:                     '</span></label></td>';
 2987:                 my $rem = $i%($numinrow);
 2988:                 if ($rem == 0) {
 2989:                     if (($i > 0) && ($i < $numspares-1)) {
 2990:                         $targettable .= '</tr>';
 2991:                     }
 2992:                     if ($i < $numspares-1) {
 2993:                         $targettable .= '<tr>';
 2994:                     }
 2995:                 }
 2996:             }
 2997:             if ($targettable ne '') {
 2998:                 my $rem = $numspares%($numinrow);
 2999:                 my $colsleft = $numinrow - $rem;
 3000:                 if ($colsleft > 1 ) {
 3001:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3002:                                     '&nbsp;</td>';
 3003:                 } elsif ($colsleft == 1) {
 3004:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3005:                 }
 3006:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3007:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3008:             }
 3009:         }
 3010:         my $cssidx = $balnum%2; 
 3011:         $datatable .= '</div></td></tr>'.
 3012:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3013:                                            $othertitle,$usertypes,$types,\%servers,
 3014:                                            \%currbalancer,$lonhost,
 3015:                                            $targets_div_style,$homedom_div_style,
 3016:                                            $css_class[$cssidx],$balnum,$islast);
 3017:         $$rowtotal += $rownum;
 3018:         $balnum ++;
 3019:     }
 3020:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3021:     return $datatable;
 3022: }
 3023: 
 3024: sub get_loadbalancers_config {
 3025:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3026:     return unless ((ref($servers) eq 'HASH') &&
 3027:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3028:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3029:     if (keys(%{$existing}) > 0) {
 3030:         my $oldlonhost;
 3031:         foreach my $key (sort(keys(%{$existing}))) {
 3032:             if ($key eq 'lonhost') {
 3033:                 $oldlonhost = $existing->{'lonhost'};
 3034:                 $currbalancer->{$oldlonhost} = 1;
 3035:             } elsif ($key eq 'targets') {
 3036:                 if ($oldlonhost) {
 3037:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3038:                 }
 3039:             } elsif ($key eq 'rules') {
 3040:                 if ($oldlonhost) {
 3041:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3042:                 }
 3043:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3044:                 $currbalancer->{$key} = 1;
 3045:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3046:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3047:             }
 3048:         }
 3049:     } else {
 3050:         my ($balancerref,$targetsref) =
 3051:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3052:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3053:             foreach my $server (sort(keys(%{$balancerref}))) {
 3054:                 $currbalancer->{$server} = 1;
 3055:                 $currtargets->{$server} = $targetsref->{$server};
 3056:             }
 3057:         }
 3058:     }
 3059:     return;
 3060: }
 3061: 
 3062: sub loadbalancing_rules {
 3063:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3064:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3065:         $css_class,$balnum,$islast) = @_;
 3066:     my $output;
 3067:     my $num = 0;
 3068:     my ($alltypes,$othertypes,$titles) = 
 3069:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3070:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3071:         foreach my $type (@{$alltypes}) {
 3072:             $num ++;
 3073:             my $current;
 3074:             if (ref($currrules) eq 'HASH') {
 3075:                 $current = $currrules->{$type};
 3076:             }
 3077:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3078:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3079:                     $current = '';
 3080:                 }
 3081:             }
 3082:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3083:                                              $servers,$currbalancer,$lonhost,$dom,
 3084:                                              $targets_div_style,$homedom_div_style,
 3085:                                              $css_class,$balnum,$num,$islast);
 3086:         }
 3087:     }
 3088:     return $output;
 3089: }
 3090: 
 3091: sub loadbalancing_titles {
 3092:     my ($dom,$intdom,$usertypes,$types) = @_;
 3093:     my %othertypes = (
 3094:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3095:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3096:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3097:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3098:                      );
 3099:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 3100:     if (ref($types) eq 'ARRAY') {
 3101:         unshift(@alltypes,@{$types},'default');
 3102:     }
 3103:     my %titles;
 3104:     foreach my $type (@alltypes) {
 3105:         if ($type =~ /^_LC_/) {
 3106:             $titles{$type} = $othertypes{$type};
 3107:         } elsif ($type eq 'default') {
 3108:             $titles{$type} = &mt('All users from [_1]',$dom);
 3109:             if (ref($types) eq 'ARRAY') {
 3110:                 if (@{$types} > 0) {
 3111:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3112:                 }
 3113:             }
 3114:         } elsif (ref($usertypes) eq 'HASH') {
 3115:             $titles{$type} = $usertypes->{$type};
 3116:         }
 3117:     }
 3118:     return (\@alltypes,\%othertypes,\%titles);
 3119: }
 3120: 
 3121: sub loadbalance_rule_row {
 3122:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3123:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3124:     my @rulenames = ('default','homeserver');
 3125:     my %ruletitles = &offloadtype_text();
 3126:     if ($type eq '_LC_external') {
 3127:         push(@rulenames,'externalbalancer');
 3128:     } else {
 3129:         push(@rulenames,'specific');
 3130:     }
 3131:     push(@rulenames,'none');
 3132:     my $style = $targets_div_style;
 3133:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3134:         $style = $homedom_div_style;
 3135:     }
 3136:     my $space;
 3137:     if ($islast && $num == 1) {
 3138:         $space = '<div display="inline-block">&nbsp;</div>';
 3139:     }
 3140:     my $output = 
 3141:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3142:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3143:         '<td valaign="top">'.$space.
 3144:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3145:     for (my $i=0; $i<@rulenames; $i++) {
 3146:         my $rule = $rulenames[$i];
 3147:         my ($checked,$extra);
 3148:         if ($rulenames[$i] eq 'default') {
 3149:             $rule = '';
 3150:         }
 3151:         if ($rulenames[$i] eq 'specific') {
 3152:             if (ref($servers) eq 'HASH') {
 3153:                 my $default;
 3154:                 if (($current ne '') && (exists($servers->{$current}))) {
 3155:                     $checked = ' checked="checked"';
 3156:                 }
 3157:                 unless ($checked) {
 3158:                     $default = ' selected="selected"';
 3159:                 }
 3160:                 $extra = 
 3161:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3162:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3163:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3164:                     '<option value=""'.$default.'></option>'."\n";
 3165:                 foreach my $server (sort(keys(%{$servers}))) {
 3166:                     if (ref($currbalancer) eq 'HASH') {
 3167:                         next if (exists($currbalancer->{$server}));
 3168:                     }
 3169:                     my $selected;
 3170:                     if ($server eq $current) {
 3171:                         $selected = ' selected="selected"';
 3172:                     }
 3173:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3174:                 }
 3175:                 $extra .= '</select>';
 3176:             }
 3177:         } elsif ($rule eq $current) {
 3178:             $checked = ' checked="checked"';
 3179:         }
 3180:         $output .= '<span class="LC_nobreak"><label>'.
 3181:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3182:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3183:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3184:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3185:                    '</label>'.$extra.'</span><br />'."\n";
 3186:     }
 3187:     $output .= '</div></td></tr>'."\n";
 3188:     return $output;
 3189: }
 3190: 
 3191: sub offloadtype_text {
 3192:     my %ruletitles = &Apache::lonlocal::texthash (
 3193:            'default'          => 'Offloads to default destinations',
 3194:            'homeserver'       => "Offloads to user's home server",
 3195:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3196:            'specific'         => 'Offloads to specific server',
 3197:            'none'             => 'No offload',
 3198:     );
 3199:     return %ruletitles;
 3200: }
 3201: 
 3202: sub sparestype_titles {
 3203:     my %typestitles = &Apache::lonlocal::texthash (
 3204:                           'primary' => 'primary',
 3205:                           'default' => 'default',
 3206:                       );
 3207:     return %typestitles;
 3208: }
 3209: 
 3210: sub contact_titles {
 3211:     my %titles = &Apache::lonlocal::texthash (
 3212:                    'supportemail' => 'Support E-mail address',
 3213:                    'adminemail'   => 'Default Server Admin E-mail address',
 3214:                    'errormail'    => 'Error reports to be e-mailed to',
 3215:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3216:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3217:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3218:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3219:                  );
 3220:     my %short_titles = &Apache::lonlocal::texthash (
 3221:                            adminemail   => 'Admin E-mail address',
 3222:                            supportemail => 'Support E-mail',
 3223:                        );   
 3224:     return (\%titles,\%short_titles);
 3225: }
 3226: 
 3227: sub tool_titles {
 3228:     my %titles = &Apache::lonlocal::texthash (
 3229:                      aboutme    => 'Personal web page',
 3230:                      blog       => 'Blog',
 3231:                      webdav     => 'WebDAV',
 3232:                      portfolio  => 'Portfolio',
 3233:                      official   => 'Official courses (with institutional codes)',
 3234:                      unofficial => 'Unofficial courses',
 3235:                      community  => 'Communities',
 3236:                  );
 3237:     return %titles;
 3238: }
 3239: 
 3240: sub courserequest_titles {
 3241:     my %titles = &Apache::lonlocal::texthash (
 3242:                                    official   => 'Official',
 3243:                                    unofficial => 'Unofficial',
 3244:                                    community  => 'Communities',
 3245:                                    norequest  => 'Not allowed',
 3246:                                    approval   => 'Approval by Dom. Coord.',
 3247:                                    validate   => 'With validation',
 3248:                                    autolimit  => 'Numerical limit',
 3249:                                    unlimited  => '(blank for unlimited)',
 3250:                  );
 3251:     return %titles;
 3252: }
 3253: 
 3254: sub authorrequest_titles {
 3255:     my %titles = &Apache::lonlocal::texthash (
 3256:                                    norequest  => 'Not allowed',
 3257:                                    approval   => 'Approval by Dom. Coord.',
 3258:                                    automatic  => 'Automatic approval',
 3259:                  );
 3260:     return %titles;
 3261: } 
 3262: 
 3263: sub courserequest_conditions {
 3264:     my %conditions = &Apache::lonlocal::texthash (
 3265:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3266:        validate   => '(Processing of request subject to instittutional validation).',
 3267:                  );
 3268:     return %conditions;
 3269: }
 3270: 
 3271: 
 3272: sub print_usercreation {
 3273:     my ($position,$dom,$settings,$rowtotal) = @_;
 3274:     my $numinrow = 4;
 3275:     my $datatable;
 3276:     if ($position eq 'top') {
 3277:         $$rowtotal ++;
 3278:         my $rowcount = 0;
 3279:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3280:         if (ref($rules) eq 'HASH') {
 3281:             if (keys(%{$rules}) > 0) {
 3282:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3283:                                                 $ruleorder,$numinrow,$rowcount);
 3284:                 $$rowtotal ++;
 3285:                 $rowcount ++;
 3286:             }
 3287:         }
 3288:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3289:         if (ref($idrules) eq 'HASH') {
 3290:             if (keys(%{$idrules}) > 0) {
 3291:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3292:                                                 $idruleorder,$numinrow,$rowcount);
 3293:                 $$rowtotal ++;
 3294:                 $rowcount ++;
 3295:             }
 3296:         }
 3297:         my ($emailrules,$emailruleorder) = 
 3298:             &Apache::lonnet::inst_userrules($dom,'email');
 3299:         if (ref($emailrules) eq 'HASH') {
 3300:             if (keys(%{$emailrules}) > 0) {
 3301:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3302:                                                 $emailruleorder,$numinrow,$rowcount);
 3303:                 $$rowtotal ++;
 3304:                 $rowcount ++;
 3305:             }
 3306:         }
 3307:         if ($rowcount == 0) {
 3308:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3309:             $$rowtotal ++;
 3310:             $rowcount ++;
 3311:         }
 3312:     } elsif ($position eq 'middle') {
 3313:         my @creators = ('author','course','requestcrs','selfcreate');
 3314:         my ($rules,$ruleorder) =
 3315:             &Apache::lonnet::inst_userrules($dom,'username');
 3316:         my %lt = &usercreation_types();
 3317:         my %checked;
 3318:         my @selfcreate; 
 3319:         if (ref($settings) eq 'HASH') {
 3320:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3321:                 foreach my $item (@creators) {
 3322:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3323:                 }
 3324:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3325:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3326:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3327:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3328:                         @selfcreate = ('email','login','sso');
 3329:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3330:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3331:                     }
 3332:                 }
 3333:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3334:                 foreach my $item (@creators) {
 3335:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3336:                         $checked{$item} = 'none';
 3337:                     }
 3338:                 }
 3339:             }
 3340:         }
 3341:         my $rownum = 0;
 3342:         foreach my $item (@creators) {
 3343:             $rownum ++;
 3344:             if ($item ne 'selfcreate') {  
 3345:                 if ($checked{$item} eq '') {
 3346:                     $checked{$item} = 'any';
 3347:                 }
 3348:             }
 3349:             my $css_class;
 3350:             if ($rownum%2) {
 3351:                 $css_class = '';
 3352:             } else {
 3353:                 $css_class = ' class="LC_odd_row" ';
 3354:             }
 3355:             $datatable .= '<tr'.$css_class.'>'.
 3356:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3357:                          '</span></td><td align="right">';
 3358:             my @options;
 3359:             if ($item eq 'selfcreate') {
 3360:                 push(@options,('email','login','sso'));
 3361:             } else {
 3362:                 @options = ('any');
 3363:                 if (ref($rules) eq 'HASH') {
 3364:                     if (keys(%{$rules}) > 0) {
 3365:                         push(@options,('official','unofficial'));
 3366:                     }
 3367:                 }
 3368:                 push(@options,'none');
 3369:             }
 3370:             foreach my $option (@options) {
 3371:                 my $type = 'radio';
 3372:                 my $check = ' ';
 3373:                 if ($item eq 'selfcreate') {
 3374:                     $type = 'checkbox';
 3375:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3376:                         $check = ' checked="checked" ';
 3377:                     }
 3378:                 } else {
 3379:                     if ($checked{$item} eq $option) {
 3380:                         $check = ' checked="checked" ';
 3381:                     }
 3382:                 } 
 3383:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3384:                               '<input type="'.$type.'" name="can_createuser_'.
 3385:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3386:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3387:             }
 3388:             $datatable .= '</td></tr>';
 3389:         }
 3390:         my ($othertitle,$usertypes,$types) =
 3391:             &Apache::loncommon::sorted_inst_types($dom);
 3392:         my $createsettings;
 3393:         if (ref($settings) eq 'HASH') {
 3394:             $createsettings = $settings->{cancreate};
 3395:         }
 3396:         if (ref($usertypes) eq 'HASH') {
 3397:             if (keys(%{$usertypes}) > 0) {
 3398:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3399:                                              $dom,$numinrow,$othertitle,
 3400:                                              'statustocreate');
 3401:                 $$rowtotal ++;
 3402:                 $rownum ++;
 3403:             }
 3404:         }
 3405:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3406:     } else {
 3407:         my @contexts = ('author','course','domain');
 3408:         my @authtypes = ('int','krb4','krb5','loc');
 3409:         my %checked;
 3410:         if (ref($settings) eq 'HASH') {
 3411:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3412:                 foreach my $item (@contexts) {
 3413:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3414:                         foreach my $auth (@authtypes) {
 3415:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3416:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3417:                             }
 3418:                         }
 3419:                     }
 3420:                 }
 3421:             }
 3422:         } else {
 3423:             foreach my $item (@contexts) {
 3424:                 foreach my $auth (@authtypes) {
 3425:                     $checked{$item}{$auth} = ' checked="checked" ';
 3426:                 }
 3427:             }
 3428:         }
 3429:         my %title = &context_names();
 3430:         my %authname = &authtype_names();
 3431:         my $rownum = 0;
 3432:         my $css_class; 
 3433:         foreach my $item (@contexts) {
 3434:             if ($rownum%2) {
 3435:                 $css_class = '';
 3436:             } else {
 3437:                 $css_class = ' class="LC_odd_row" ';
 3438:             }
 3439:             $datatable .=   '<tr'.$css_class.'>'.
 3440:                             '<td>'.$title{$item}.
 3441:                             '</td><td class="LC_left_item">'.
 3442:                             '<span class="LC_nobreak">';
 3443:             foreach my $auth (@authtypes) {
 3444:                 $datatable .= '<label>'. 
 3445:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3446:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3447:                               $authname{$auth}.'</label>&nbsp;';
 3448:             }
 3449:             $datatable .= '</span></td></tr>';
 3450:             $rownum ++;
 3451:         }
 3452:         $$rowtotal += $rownum;
 3453:     }
 3454:     return $datatable;
 3455: }
 3456: 
 3457: sub captcha_choice {
 3458:     my ($context,$settings,$itemcount) = @_;
 3459:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3460:     my %lt = &captcha_phrases();
 3461:     $keyentry = 'hidden';
 3462:     if ($context eq 'cancreate') {
 3463:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3464:     } elsif ($context eq 'login') {
 3465:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3466:     }
 3467:     if (ref($settings) eq 'HASH') {
 3468:         if ($settings->{'captcha'}) {
 3469:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3470:         } else {
 3471:             $checked{'original'} = ' checked="checked"';
 3472:         }
 3473:         if ($settings->{'captcha'} eq 'recaptcha') {
 3474:             $pubtext = $lt{'pub'};
 3475:             $privtext = $lt{'priv'};
 3476:             $keyentry = 'text';
 3477:         }
 3478:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3479:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3480:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3481:         }
 3482:     } else {
 3483:         $checked{'original'} = ' checked="checked"';
 3484:     }
 3485:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3486:     my $output = '<tr'.$css_class.'>'.
 3487:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3488:                  '<table><tr><td>'."\n";
 3489:     foreach my $option ('original','recaptcha','notused') {
 3490:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3491:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3492:                    $lt{$option}.'</label></span>';
 3493:         unless ($option eq 'notused') {
 3494:             $output .= ('&nbsp;'x2)."\n";
 3495:         }
 3496:     }
 3497: #
 3498: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3499: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3500: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu) 
 3501: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3502: #  
 3503:     $output .= '</td></tr>'."\n".
 3504:                '<tr><td>'."\n".
 3505:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3506:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3507:                $currpub.'" size="40" /></span><br />'."\n".
 3508:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3509:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3510:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3511:                '</td></tr>';
 3512:     return $output;
 3513: }
 3514: 
 3515: sub user_formats_row {
 3516:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3517:     my $output;
 3518:     my %text = (
 3519:                    'username' => 'new usernames',
 3520:                    'id'       => 'IDs',
 3521:                    'email'    => 'self-created accounts (e-mail)',
 3522:                );
 3523:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3524:     $output = '<tr '.$css_class.'>'.
 3525:               '<td><span class="LC_nobreak">';
 3526:     if ($type eq 'email') {
 3527:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3528:     } else {
 3529:         $output .= &mt("Format rules to check for $text{$type}: ");
 3530:     }
 3531:     $output .= '</span></td>'.
 3532:                '<td class="LC_left_item" colspan="2"><table>';
 3533:     my $rem;
 3534:     if (ref($ruleorder) eq 'ARRAY') {
 3535:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3536:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3537:                 my $rem = $i%($numinrow);
 3538:                 if ($rem == 0) {
 3539:                     if ($i > 0) {
 3540:                         $output .= '</tr>';
 3541:                     }
 3542:                     $output .= '<tr>';
 3543:                 }
 3544:                 my $check = ' ';
 3545:                 if (ref($settings) eq 'HASH') {
 3546:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3547:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3548:                             $check = ' checked="checked" ';
 3549:                         }
 3550:                     }
 3551:                 }
 3552:                 $output .= '<td class="LC_left_item">'.
 3553:                            '<span class="LC_nobreak"><label>'.
 3554:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3555:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3556:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3557:             }
 3558:         }
 3559:         $rem = @{$ruleorder}%($numinrow);
 3560:     }
 3561:     my $colsleft = $numinrow - $rem;
 3562:     if ($colsleft > 1 ) {
 3563:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3564:                    '&nbsp;</td>';
 3565:     } elsif ($colsleft == 1) {
 3566:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3567:     }
 3568:     $output .= '</tr></table></td></tr>';
 3569:     return $output;
 3570: }
 3571: 
 3572: sub usercreation_types {
 3573:     my %lt = &Apache::lonlocal::texthash (
 3574:                     author     => 'When adding a co-author',
 3575:                     course     => 'When adding a user to a course',
 3576:                     requestcrs => 'When requesting a course',
 3577:                     selfcreate => 'User creates own account', 
 3578:                     any        => 'Any',
 3579:                     official   => 'Institutional only ',
 3580:                     unofficial => 'Non-institutional only',
 3581:                     email      => 'E-mail address',
 3582:                     login      => 'Institutional Login',
 3583:                     sso        => 'SSO', 
 3584:                     none       => 'None',
 3585:     );
 3586:     return %lt;
 3587: }
 3588: 
 3589: sub authtype_names {
 3590:     my %lt = &Apache::lonlocal::texthash(
 3591:                       int    => 'Internal',
 3592:                       krb4   => 'Kerberos 4',
 3593:                       krb5   => 'Kerberos 5',
 3594:                       loc    => 'Local',
 3595:                   );
 3596:     return %lt;
 3597: }
 3598: 
 3599: sub context_names {
 3600:     my %context_title = &Apache::lonlocal::texthash(
 3601:        author => 'Creating users when an Author',
 3602:        course => 'Creating users when in a course',
 3603:        domain => 'Creating users when a Domain Coordinator',
 3604:     );
 3605:     return %context_title;
 3606: }
 3607: 
 3608: sub print_usermodification {
 3609:     my ($position,$dom,$settings,$rowtotal) = @_;
 3610:     my $numinrow = 4;
 3611:     my ($context,$datatable,$rowcount);
 3612:     if ($position eq 'top') {
 3613:         $rowcount = 0;
 3614:         $context = 'author'; 
 3615:         foreach my $role ('ca','aa') {
 3616:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3617:                                                    $numinrow,$rowcount);
 3618:             $$rowtotal ++;
 3619:             $rowcount ++;
 3620:         }
 3621:     } elsif ($position eq 'middle') {
 3622:         $context = 'course';
 3623:         $rowcount = 0;
 3624:         foreach my $role ('st','ep','ta','in','cr') {
 3625:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3626:                                                    $numinrow,$rowcount);
 3627:             $$rowtotal ++;
 3628:             $rowcount ++;
 3629:         }
 3630:     } elsif ($position eq 'bottom') {
 3631:         $context = 'selfcreate';
 3632:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3633:         $usertypes->{'default'} = $othertitle;
 3634:         if (ref($types) eq 'ARRAY') {
 3635:             push(@{$types},'default');
 3636:             $usertypes->{'default'} = $othertitle;
 3637:             foreach my $status (@{$types}) {
 3638:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3639:                                                        $numinrow,$rowcount,$usertypes);
 3640:                 $$rowtotal ++;
 3641:                 $rowcount ++;
 3642:             }
 3643:         }
 3644:     }
 3645:     return $datatable;
 3646: }
 3647: 
 3648: sub print_defaults {
 3649:     my ($dom,$rowtotal) = @_;
 3650:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3651:                  'datelocale_def','portal_def');
 3652:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3653:     my $titles = &defaults_titles($dom);
 3654:     my $rownum = 0;
 3655:     my ($datatable,$css_class);
 3656:     foreach my $item (@items) {
 3657:         if ($rownum%2) {
 3658:             $css_class = '';
 3659:         } else {
 3660:             $css_class = ' class="LC_odd_row" ';
 3661:         }
 3662:         $datatable .= '<tr'.$css_class.'>'.
 3663:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3664:                   '</span></td><td class="LC_right_item">';
 3665:         if ($item eq 'auth_def') {
 3666:             my @authtypes = ('internal','krb4','krb5','localauth');
 3667:             my %shortauth = (
 3668:                              internal => 'int',
 3669:                              krb4 => 'krb4',
 3670:                              krb5 => 'krb5',
 3671:                              localauth  => 'loc'
 3672:                            );
 3673:             my %authnames = &authtype_names();
 3674:             foreach my $auth (@authtypes) {
 3675:                 my $checked = ' ';
 3676:                 if ($domdefaults{$item} eq $auth) {
 3677:                     $checked = ' checked="checked" ';
 3678:                 }
 3679:                 $datatable .= '<label><input type="radio" name="'.$item.
 3680:                               '" value="'.$auth.'"'.$checked.'/>'.
 3681:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3682:             }
 3683:         } elsif ($item eq 'timezone_def') {
 3684:             my $includeempty = 1;
 3685:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3686:         } elsif ($item eq 'datelocale_def') {
 3687:             my $includeempty = 1;
 3688:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3689:         } elsif ($item eq 'lang_def') {
 3690:             my %langchoices = &get_languages_hash();
 3691:             $langchoices{''} = 'No language preference';
 3692:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 3693:             $datatable .= &Apache::loncommon::select_form($domdefaults{$item},$item,
 3694:                                                           \%langchoices);
 3695:         } else {
 3696:             my $size;
 3697:             if ($item eq 'portal_def') {
 3698:                 $size = ' size="25"';
 3699:             }
 3700:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3701:                           $domdefaults{$item}.'"'.$size.' />';
 3702:         }
 3703:         $datatable .= '</td></tr>';
 3704:         $rownum ++;
 3705:     }
 3706:     $$rowtotal += $rownum;
 3707:     return $datatable;
 3708: }
 3709: 
 3710: sub get_languages_hash {
 3711:     my %langchoices;
 3712:     foreach my $id (&Apache::loncommon::languageids()) {
 3713:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 3714:         if ($code ne '') {
 3715:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 3716:         }
 3717:     }
 3718:     return %langchoices;
 3719: }
 3720: 
 3721: sub defaults_titles {
 3722:     my ($dom) = @_;
 3723:     my %titles = &Apache::lonlocal::texthash (
 3724:                    'auth_def'      => 'Default authentication type',
 3725:                    'auth_arg_def'  => 'Default authentication argument',
 3726:                    'lang_def'      => 'Default language',
 3727:                    'timezone_def'  => 'Default timezone',
 3728:                    'datelocale_def' => 'Default locale for dates',
 3729:                    'portal_def'     => 'Portal/Default URL',
 3730:                  );
 3731:     if ($dom) {
 3732:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3733:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3734:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3735:         $protocol = 'http' if ($protocol ne 'https');
 3736:         if ($uint_dom) {
 3737:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3738:                                          $uint_dom);
 3739:         }
 3740:     }
 3741:     return (\%titles);
 3742: }
 3743: 
 3744: sub print_scantronformat {
 3745:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3746:     my $itemcount = 1;
 3747:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3748:         %confhash);
 3749:     my $switchserver = &check_switchserver($dom,$confname);
 3750:     my %lt = &Apache::lonlocal::texthash (
 3751:                 default => 'Default bubblesheet format file error',
 3752:                 custom  => 'Custom bubblesheet format file error',
 3753:              );
 3754:     my %scantronfiles = (
 3755:         default => 'default.tab',
 3756:         custom => 'custom.tab',
 3757:     );
 3758:     foreach my $key (keys(%scantronfiles)) {
 3759:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3760:                               .$scantronfiles{$key};
 3761:     }
 3762:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3763:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3764:         if (!$switchserver) {
 3765:             my $servadm = $r->dir_config('lonAdmEMail');
 3766:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3767:             if ($configuserok eq 'ok') {
 3768:                 if ($author_ok eq 'ok') {
 3769:                     my %legacyfile = (
 3770:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3771:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3772:                     );
 3773:                     my %md5chk;
 3774:                     foreach my $type (keys(%legacyfile)) {
 3775:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3776:                         chomp($md5chk{$type});
 3777:                     }
 3778:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3779:                         foreach my $type (keys(%legacyfile)) {
 3780:                             ($scantronurls{$type},my $error) = 
 3781:                                 &legacy_scantronformat($r,$dom,$confname,
 3782:                                                  $type,$legacyfile{$type},
 3783:                                                  $scantronurls{$type},
 3784:                                                  $scantronfiles{$type});
 3785:                             if ($error ne '') {
 3786:                                 $error{$type} = $error;
 3787:                             }
 3788:                         }
 3789:                         if (keys(%error) == 0) {
 3790:                             $is_custom = 1;
 3791:                             $confhash{'scantron'}{'scantronformat'} = 
 3792:                                 $scantronurls{'custom'};
 3793:                             my $putresult = 
 3794:                                 &Apache::lonnet::put_dom('configuration',
 3795:                                                          \%confhash,$dom);
 3796:                             if ($putresult ne 'ok') {
 3797:                                 $error{'custom'} = 
 3798:                                     '<span class="LC_error">'.
 3799:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3800:                             }
 3801:                         }
 3802:                     } else {
 3803:                         ($scantronurls{'default'},my $error) =
 3804:                             &legacy_scantronformat($r,$dom,$confname,
 3805:                                           'default',$legacyfile{'default'},
 3806:                                           $scantronurls{'default'},
 3807:                                           $scantronfiles{'default'});
 3808:                         if ($error eq '') {
 3809:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3810:                             my $putresult =
 3811:                                 &Apache::lonnet::put_dom('configuration',
 3812:                                                          \%confhash,$dom);
 3813:                             if ($putresult ne 'ok') {
 3814:                                 $error{'default'} =
 3815:                                     '<span class="LC_error">'.
 3816:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3817:                             }
 3818:                         } else {
 3819:                             $error{'default'} = $error;
 3820:                         }
 3821:                     }
 3822:                 }
 3823:             }
 3824:         } else {
 3825:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3826:         }
 3827:     }
 3828:     if (ref($settings) eq 'HASH') {
 3829:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3830:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3831:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3832:                 $scantronurl = '';
 3833:             } else {
 3834:                 $scantronurl = $settings->{'scantronformat'};
 3835:             }
 3836:             $is_custom = 1;
 3837:         } else {
 3838:             $scantronurl = $scantronurls{'default'};
 3839:         }
 3840:     } else {
 3841:         if ($is_custom) {
 3842:             $scantronurl = $scantronurls{'custom'};
 3843:         } else {
 3844:             $scantronurl = $scantronurls{'default'};
 3845:         }
 3846:     }
 3847:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3848:     $datatable .= '<tr'.$css_class.'>';
 3849:     if (!$is_custom) {
 3850:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3851:                       '<span class="LC_nobreak">';
 3852:         if ($scantronurl) {
 3853:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3854:                           &mt('Default bubblesheet format file').'</a>';
 3855:         } else {
 3856:             $datatable = &mt('File unavailable for display');
 3857:         }
 3858:         $datatable .= '</span></td>';
 3859:         if (keys(%error) == 0) { 
 3860:             $datatable .= '<td valign="bottom">';
 3861:             if (!$switchserver) {
 3862:                 $datatable .= &mt('Upload:').'<br />';
 3863:             }
 3864:         } else {
 3865:             my $errorstr;
 3866:             foreach my $key (sort(keys(%error))) {
 3867:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3868:             }
 3869:             $datatable .= '<td>'.$errorstr;
 3870:         }
 3871:     } else {
 3872:         if (keys(%error) > 0) {
 3873:             my $errorstr;
 3874:             foreach my $key (sort(keys(%error))) {
 3875:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3876:             } 
 3877:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3878:         } elsif ($scantronurl) {
 3879:             $datatable .= '<td><span class="LC_nobreak">'.
 3880:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3881:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3882:                           '<input type="checkbox" name="scantronformat_del"'.
 3883:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3884:                           '<td><span class="LC_nobreak">&nbsp;'.
 3885:                           &mt('Replace:').'</span><br />';
 3886:         }
 3887:     }
 3888:     if (keys(%error) == 0) {
 3889:         if ($switchserver) {
 3890:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3891:         } else {
 3892:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3893:                          '<input type="file" name="scantronformat" /></span>';
 3894:         }
 3895:     }
 3896:     $datatable .= '</td></tr>';
 3897:     $$rowtotal ++;
 3898:     return $datatable;
 3899: }
 3900: 
 3901: sub legacy_scantronformat {
 3902:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3903:     my ($url,$error);
 3904:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3905:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3906:         (my $result,$url) =
 3907:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3908:                          '','',$newfile);
 3909:         if ($result ne 'ok') {
 3910:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3911:         }
 3912:     }
 3913:     return ($url,$error);
 3914: }
 3915: 
 3916: sub print_coursecategories {
 3917:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3918:     my $datatable;
 3919:     if ($position eq 'top') {
 3920:         my $toggle_cats_crs = ' ';
 3921:         my $toggle_cats_dom = ' checked="checked" ';
 3922:         my $can_cat_crs = ' ';
 3923:         my $can_cat_dom = ' checked="checked" ';
 3924:         my $toggle_catscomm_comm = ' ';
 3925:         my $toggle_catscomm_dom = ' checked="checked" ';
 3926:         my $can_catcomm_comm = ' ';
 3927:         my $can_catcomm_dom = ' checked="checked" ';
 3928: 
 3929:         if (ref($settings) eq 'HASH') {
 3930:             if ($settings->{'togglecats'} eq 'crs') {
 3931:                 $toggle_cats_crs = $toggle_cats_dom;
 3932:                 $toggle_cats_dom = ' ';
 3933:             }
 3934:             if ($settings->{'categorize'} eq 'crs') {
 3935:                 $can_cat_crs = $can_cat_dom;
 3936:                 $can_cat_dom = ' ';
 3937:             }
 3938:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3939:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3940:                 $toggle_catscomm_dom = ' ';
 3941:             }
 3942:             if ($settings->{'categorizecomm'} eq 'comm') {
 3943:                 $can_catcomm_comm = $can_catcomm_dom;
 3944:                 $can_catcomm_dom = ' ';
 3945:             }
 3946:         }
 3947:         my %title = &Apache::lonlocal::texthash (
 3948:                      togglecats     => 'Show/Hide a course in catalog',
 3949:                      togglecatscomm => 'Show/Hide a community in catalog',
 3950:                      categorize     => 'Assign a category to a course',
 3951:                      categorizecomm => 'Assign a category to a community',
 3952:                     );
 3953:         my %level = &Apache::lonlocal::texthash (
 3954:                      dom  => 'Set in Domain',
 3955:                      crs  => 'Set in Course',
 3956:                      comm => 'Set in Community',
 3957:                     );
 3958:         $datatable = '<tr class="LC_odd_row">'.
 3959:                   '<td>'.$title{'togglecats'}.'</td>'.
 3960:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3961:                   '<input type="radio" name="togglecats"'.
 3962:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3963:                   '<label><input type="radio" name="togglecats"'.
 3964:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3965:                   '</tr><tr>'.
 3966:                   '<td>'.$title{'categorize'}.'</td>'.
 3967:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3968:                   '<label><input type="radio" name="categorize"'.
 3969:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3970:                   '<label><input type="radio" name="categorize"'.
 3971:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3972:                   '</tr><tr class="LC_odd_row">'.
 3973:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 3974:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3975:                   '<input type="radio" name="togglecatscomm"'.
 3976:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3977:                   '<label><input type="radio" name="togglecatscomm"'.
 3978:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3979:                   '</tr><tr>'.
 3980:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 3981:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3982:                   '<label><input type="radio" name="categorizecomm"'.
 3983:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3984:                   '<label><input type="radio" name="categorizecomm"'.
 3985:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3986:                   '</tr>';
 3987:         $$rowtotal += 4;
 3988:     } else {
 3989:         my $css_class;
 3990:         my $itemcount = 1;
 3991:         my $cathash; 
 3992:         if (ref($settings) eq 'HASH') {
 3993:             $cathash = $settings->{'cats'};
 3994:         }
 3995:         if (ref($cathash) eq 'HASH') {
 3996:             my (@cats,@trails,%allitems,%idx,@jsarray);
 3997:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 3998:                                                    \%allitems,\%idx,\@jsarray);
 3999:             my $maxdepth = scalar(@cats);
 4000:             my $colattrib = '';
 4001:             if ($maxdepth > 2) {
 4002:                 $colattrib = ' colspan="2" ';
 4003:             }
 4004:             my @path;
 4005:             if (@cats > 0) {
 4006:                 if (ref($cats[0]) eq 'ARRAY') {
 4007:                     my $numtop = @{$cats[0]};
 4008:                     my $maxnum = $numtop;
 4009:                     my %default_names = (
 4010:                           instcode    => &mt('Official courses'),
 4011:                           communities => &mt('Communities'),
 4012:                     );
 4013: 
 4014:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4015:                         ($cathash->{'instcode::0'} eq '') ||
 4016:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4017:                         ($cathash->{'communities::0'} eq '')) {
 4018:                         $maxnum ++;
 4019:                     }
 4020:                     my $lastidx;
 4021:                     for (my $i=0; $i<$numtop; $i++) {
 4022:                         my $parent = $cats[0][$i];
 4023:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4024:                         my $item = &escape($parent).'::0';
 4025:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4026:                         $lastidx = $idx{$item};
 4027:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4028:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4029:                         for (my $k=0; $k<=$maxnum; $k++) {
 4030:                             my $vpos = $k+1;
 4031:                             my $selstr;
 4032:                             if ($k == $i) {
 4033:                                 $selstr = ' selected="selected" ';
 4034:                             }
 4035:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4036:                         }
 4037:                         $datatable .= '</select></td><td>';
 4038:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4039:                             $datatable .=  '<span class="LC_nobreak">'
 4040:                                            .$default_names{$parent}.'</span>';
 4041:                             if ($parent eq 'instcode') {
 4042:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4043:                                               .&mt('with institutional codes')
 4044:                                               .')</span></td><td'.$colattrib.'>';
 4045:                             } else {
 4046:                                 $datatable .= '<table><tr><td>';
 4047:                             }
 4048:                             $datatable .= '<span class="LC_nobreak">'
 4049:                                           .'<label><input type="radio" name="'
 4050:                                           .$parent.'" value="1" checked="checked" />'
 4051:                                           .&mt('Display').'</label>';
 4052:                             if ($parent eq 'instcode') {
 4053:                                 $datatable .= '&nbsp;';
 4054:                             } else {
 4055:                                 $datatable .= '</span></td></tr><tr><td>'
 4056:                                               .'<span class="LC_nobreak">';
 4057:                             }
 4058:                             $datatable .= '<label><input type="radio" name="'
 4059:                                           .$parent.'" value="0" />'
 4060:                                           .&mt('Do not display').'</label></span>';
 4061:                             if ($parent eq 'communities') {
 4062:                                 $datatable .= '</td></tr></table>';
 4063:                             }
 4064:                             $datatable .= '</td>';
 4065:                         } else {
 4066:                             $datatable .= $parent
 4067:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 4068:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4069:                         }
 4070:                         my $depth = 1;
 4071:                         push(@path,$parent);
 4072:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4073:                         pop(@path);
 4074:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4075:                         $itemcount ++;
 4076:                     }
 4077:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4078:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4079:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4080:                     for (my $k=0; $k<=$maxnum; $k++) {
 4081:                         my $vpos = $k+1;
 4082:                         my $selstr;
 4083:                         if ($k == $numtop) {
 4084:                             $selstr = ' selected="selected" ';
 4085:                         }
 4086:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4087:                     }
 4088:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4089:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4090:                                   .'</tr>'."\n";
 4091:                     $itemcount ++;
 4092:                     foreach my $default ('instcode','communities') {
 4093:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4094:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4095:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4096:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4097:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4098:                             for (my $k=0; $k<=$maxnum; $k++) {
 4099:                                 my $vpos = $k+1;
 4100:                                 my $selstr;
 4101:                                 if ($k == $maxnum) {
 4102:                                     $selstr = ' selected="selected" ';
 4103:                                 }
 4104:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4105:                             }
 4106:                             $datatable .= '</select></span></td>'.
 4107:                                           '<td><span class="LC_nobreak">'.
 4108:                                           $default_names{$default}.'</span>';
 4109:                             if ($default eq 'instcode') {
 4110:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4111:                                               .&mt('with institutional codes').')</span>';
 4112:                             }
 4113:                             $datatable .= '</td>'
 4114:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4115:                                           .&mt('Display').'</label>&nbsp;'
 4116:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4117:                                           .&mt('Do not display').'</label></span></td></tr>';
 4118:                         }
 4119:                     }
 4120:                 }
 4121:             } else {
 4122:                 $datatable .= &initialize_categories($itemcount);
 4123:             }
 4124:         } else {
 4125:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4126:                           .&initialize_categories($itemcount);
 4127:         }
 4128:         $$rowtotal += $itemcount;
 4129:     }
 4130:     return $datatable;
 4131: }
 4132: 
 4133: sub print_serverstatuses {
 4134:     my ($dom,$settings,$rowtotal) = @_;
 4135:     my $datatable;
 4136:     my @pages = &serverstatus_pages();
 4137:     my (%namedaccess,%machineaccess);
 4138:     foreach my $type (@pages) {
 4139:         $namedaccess{$type} = '';
 4140:         $machineaccess{$type}= '';
 4141:     }
 4142:     if (ref($settings) eq 'HASH') {
 4143:         foreach my $type (@pages) {
 4144:             if (exists($settings->{$type})) {
 4145:                 if (ref($settings->{$type}) eq 'HASH') {
 4146:                     foreach my $key (keys(%{$settings->{$type}})) {
 4147:                         if ($key eq 'namedusers') {
 4148:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4149:                         } elsif ($key eq 'machines') {
 4150:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4151:                         }
 4152:                     }
 4153:                 }
 4154:             }
 4155:         }
 4156:     }
 4157:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4158:     my $rownum = 0;
 4159:     my $css_class;
 4160:     foreach my $type (@pages) {
 4161:         $rownum ++;
 4162:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4163:         $datatable .= '<tr'.$css_class.'>'.
 4164:                       '<td><span class="LC_nobreak">'.
 4165:                       $titles->{$type}.'</span></td>'.
 4166:                       '<td class="LC_left_item">'.
 4167:                       '<input type="text" name="'.$type.'_namedusers" '.
 4168:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4169:                       '<td class="LC_right_item">'.
 4170:                       '<span class="LC_nobreak">'.
 4171:                       '<input type="text" name="'.$type.'_machines" '.
 4172:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4173:                       '</td></tr>'."\n";
 4174:     }
 4175:     $$rowtotal += $rownum;
 4176:     return $datatable;
 4177: }
 4178: 
 4179: sub serverstatus_pages {
 4180:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4181:             'clusterstatus','metadata_keywords','metadata_harvest',
 4182:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4183: }
 4184: 
 4185: sub coursecategories_javascript {
 4186:     my ($settings) = @_;
 4187:     my ($output,$jstext,$cathash);
 4188:     if (ref($settings) eq 'HASH') {
 4189:         $cathash = $settings->{'cats'};
 4190:     }
 4191:     if (ref($cathash) eq 'HASH') {
 4192:         my (@cats,@jsarray,%idx);
 4193:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4194:         if (@jsarray > 0) {
 4195:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4196:             for (my $i=0; $i<@jsarray; $i++) {
 4197:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4198:                     my $catstr = join('","',@{$jsarray[$i]});
 4199:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4200:                 }
 4201:             }
 4202:         }
 4203:     } else {
 4204:         $jstext  = '    var categories = Array(1);'."\n".
 4205:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4206:     }
 4207:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4208:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4209:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4210:     $output = <<"ENDSCRIPT";
 4211: <script type="text/javascript">
 4212: // <![CDATA[
 4213: function reorderCats(form,parent,item,idx) {
 4214:     var changedVal;
 4215: $jstext
 4216:     var newpos = 'addcategory_pos';
 4217:     var current = new Array;
 4218:     if (parent == '') {
 4219:         var has_instcode = 0;
 4220:         var maxtop = categories[idx].length;
 4221:         for (var j=0; j<maxtop; j++) {
 4222:             if (categories[idx][j] == 'instcode::0') {
 4223:                 has_instcode == 1;
 4224:             }
 4225:         }
 4226:         if (has_instcode == 0) {
 4227:             categories[idx][maxtop] = 'instcode_pos';
 4228:         }
 4229:     } else {
 4230:         newpos += '_'+parent;
 4231:     }
 4232:     var maxh = 1 + categories[idx].length;
 4233:     var current = new Array;
 4234:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4235:     if (item == newpos) {
 4236:         changedVal = newitemVal;
 4237:     } else {
 4238:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4239:         current[newitemVal] = newpos;
 4240:     }
 4241:     for (var i=0; i<categories[idx].length; i++) {
 4242:         var elementName = categories[idx][i];
 4243:         if (elementName != item) {
 4244:             if (form.elements[elementName]) {
 4245:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4246:                 current[currVal] = elementName;
 4247:             }
 4248:         }
 4249:     }
 4250:     var oldVal;
 4251:     for (var j=0; j<maxh; j++) {
 4252:         if (current[j] == undefined) {
 4253:             oldVal = j;
 4254:         }
 4255:     }
 4256:     if (oldVal < changedVal) {
 4257:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4258:            var elementName = current[k];
 4259:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4260:         }
 4261:     } else {
 4262:         for (var k=changedVal; k<oldVal; k++) {
 4263:             var elementName = current[k];
 4264:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4265:         }
 4266:     }
 4267:     return;
 4268: }
 4269: 
 4270: function categoryCheck(form) {
 4271:     if (form.elements['addcategory_name'].value == 'instcode') {
 4272:         alert('$instcode_reserved\\n$choose_again');
 4273:         return false;
 4274:     }
 4275:     if (form.elements['addcategory_name'].value == 'communities') {
 4276:         alert('$communities_reserved\\n$choose_again');
 4277:         return false;
 4278:     }
 4279:     return true;
 4280: }
 4281: 
 4282: // ]]>
 4283: </script>
 4284: 
 4285: ENDSCRIPT
 4286:     return $output;
 4287: }
 4288: 
 4289: sub initialize_categories {
 4290:     my ($itemcount) = @_;
 4291:     my ($datatable,$css_class,$chgstr);
 4292:     my %default_names = (
 4293:                       instcode    => 'Official courses (with institutional codes)',
 4294:                       communities => 'Communities',
 4295:                         );
 4296:     my $select0 = ' selected="selected"';
 4297:     my $select1 = '';
 4298:     foreach my $default ('instcode','communities') {
 4299:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4300:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4301:         if ($default eq 'communities') {
 4302:             $select1 = $select0;
 4303:             $select0 = '';
 4304:         }
 4305:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4306:                      .'<select name="'.$default.'_pos">'
 4307:                      .'<option value="0"'.$select0.'>1</option>'
 4308:                      .'<option value="1"'.$select1.'>2</option>'
 4309:                      .'<option value="2">3</option></select>&nbsp;'
 4310:                      .$default_names{$default}
 4311:                      .'</span></td><td><span class="LC_nobreak">'
 4312:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4313:                      .&mt('Display').'</label>&nbsp;<label>'
 4314:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4315:                  .'</label></span></td></tr>';
 4316:         $itemcount ++;
 4317:     }
 4318:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4319:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4320:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4321:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4322:                   .'<option value="0">1</option>'
 4323:                   .'<option value="1">2</option>'
 4324:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4325:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4326:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4327:     return $datatable;
 4328: }
 4329: 
 4330: sub build_category_rows {
 4331:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4332:     my ($text,$name,$item,$chgstr);
 4333:     if (ref($cats) eq 'ARRAY') {
 4334:         my $maxdepth = scalar(@{$cats});
 4335:         if (ref($cats->[$depth]) eq 'HASH') {
 4336:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4337:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4338:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4339:                 $text .= '<td><table class="LC_datatable">';
 4340:                 my ($idxnum,$parent_name,$parent_item);
 4341:                 my $higher = $depth - 1;
 4342:                 if ($higher == 0) {
 4343:                     $parent_name = &escape($parent).'::'.$higher;
 4344:                 } else {
 4345:                     if (ref($path) eq 'ARRAY') {
 4346:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4347:                     }
 4348:                 }
 4349:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4350:                 for (my $j=0; $j<=$numchildren; $j++) {
 4351:                     if ($j < $numchildren) {
 4352:                         $name = $cats->[$depth]{$parent}[$j];
 4353:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4354:                         $idxnum = $idx->{$item};
 4355:                     } else {
 4356:                         $name = $parent_name;
 4357:                         $item = $parent_item;
 4358:                     }
 4359:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4360:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4361:                     for (my $i=0; $i<=$numchildren; $i++) {
 4362:                         my $vpos = $i+1;
 4363:                         my $selstr;
 4364:                         if ($j == $i) {
 4365:                             $selstr = ' selected="selected" ';
 4366:                         }
 4367:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4368:                     }
 4369:                     $text .= '</select>&nbsp;';
 4370:                     if ($j < $numchildren) {
 4371:                         my $deeper = $depth+1;
 4372:                         $text .= $name.'&nbsp;'
 4373:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4374:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4375:                         if(ref($path) eq 'ARRAY') {
 4376:                             push(@{$path},$name);
 4377:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4378:                             pop(@{$path});
 4379:                         }
 4380:                     } else {
 4381:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4382:                         if ($j == $numchildren) {
 4383:                             $text .= $name;
 4384:                         } else {
 4385:                             $text .= $item;
 4386:                         }
 4387:                         $text .= '" value="" />';
 4388:                     }
 4389:                     $text .= '</td></tr>';
 4390:                 }
 4391:                 $text .= '</table></td>';
 4392:             } else {
 4393:                 my $higher = $depth-1;
 4394:                 if ($higher == 0) {
 4395:                     $name = &escape($parent).'::'.$higher;
 4396:                 } else {
 4397:                     if (ref($path) eq 'ARRAY') {
 4398:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4399:                     }
 4400:                 }
 4401:                 my $colspan;
 4402:                 if ($parent ne 'instcode') {
 4403:                     $colspan = $maxdepth - $depth - 1;
 4404:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4405:                 }
 4406:             }
 4407:         }
 4408:     }
 4409:     return $text;
 4410: }
 4411: 
 4412: sub modifiable_userdata_row {
 4413:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4414:     my $rolename;
 4415:     if ($context eq 'selfcreate') {
 4416:         if (ref($usertypes) eq 'HASH') {
 4417:             $rolename = $usertypes->{$role};
 4418:         } else {
 4419:             $rolename = $role;
 4420:         }
 4421:     } else {
 4422:         if ($role eq 'cr') {
 4423:             $rolename = &mt('Custom role');
 4424:         } else {
 4425:             $rolename = &Apache::lonnet::plaintext($role);
 4426:         }
 4427:     }
 4428:     my @fields = ('lastname','firstname','middlename','generation',
 4429:                   'permanentemail','id');
 4430:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4431:     my $output;
 4432:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4433:     $output = '<tr '.$css_class.'>'.
 4434:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4435:               '<td class="LC_left_item" colspan="2"><table>';
 4436:     my $rem;
 4437:     my %checks;
 4438:     if (ref($settings) eq 'HASH') {
 4439:         if (ref($settings->{$context}) eq 'HASH') {
 4440:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4441:                 foreach my $field (@fields) {
 4442:                     if ($settings->{$context}->{$role}->{$field}) {
 4443:                         $checks{$field} = ' checked="checked" ';
 4444:                     }
 4445:                 }
 4446:             }
 4447:         }
 4448:     }
 4449:     for (my $i=0; $i<@fields; $i++) {
 4450:         my $rem = $i%($numinrow);
 4451:         if ($rem == 0) {
 4452:             if ($i > 0) {
 4453:                 $output .= '</tr>';
 4454:             }
 4455:             $output .= '<tr>';
 4456:         }
 4457:         my $check = ' ';
 4458:         if (exists($checks{$fields[$i]})) {
 4459:             $check = $checks{$fields[$i]}
 4460:         } else {
 4461:             if ($role eq 'st') {
 4462:                 if (ref($settings) ne 'HASH') {
 4463:                     $check = ' checked="checked" '; 
 4464:                 }
 4465:             }
 4466:         }
 4467:         $output .= '<td class="LC_left_item">'.
 4468:                    '<span class="LC_nobreak"><label>'.
 4469:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4470:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4471:                    '</label></span></td>';
 4472:         $rem = @fields%($numinrow);
 4473:     }
 4474:     my $colsleft = $numinrow - $rem;
 4475:     if ($colsleft > 1 ) {
 4476:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4477:                    '&nbsp;</td>';
 4478:     } elsif ($colsleft == 1) {
 4479:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4480:     }
 4481:     $output .= '</tr></table></td></tr>';
 4482:     return $output;
 4483: }
 4484: 
 4485: sub insttypes_row {
 4486:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4487:     my %lt = &Apache::lonlocal::texthash (
 4488:                       cansearch => 'Users allowed to search',
 4489:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4490:                       lockablenames => 'User preference to lock name',
 4491:              );
 4492:     my $showdom;
 4493:     if ($context eq 'cansearch') {
 4494:         $showdom = ' ('.$dom.')';
 4495:     }
 4496:     my $class = 'LC_left_item';
 4497:     if ($context eq 'statustocreate') {
 4498:         $class = 'LC_right_item';
 4499:     }
 4500:     my $output =  '<tr class="LC_odd_row">'.
 4501:                   '<td>'.$lt{$context}.$showdom.
 4502:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4503:     my $rem;
 4504:     if (ref($types) eq 'ARRAY') {
 4505:         for (my $i=0; $i<@{$types}; $i++) {
 4506:             if (defined($usertypes->{$types->[$i]})) {
 4507:                 my $rem = $i%($numinrow);
 4508:                 if ($rem == 0) {
 4509:                     if ($i > 0) {
 4510:                         $output .= '</tr>';
 4511:                     }
 4512:                     $output .= '<tr>';
 4513:                 }
 4514:                 my $check = ' ';
 4515:                 if (ref($settings) eq 'HASH') {
 4516:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4517:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4518:                             $check = ' checked="checked" ';
 4519:                         }
 4520:                     } elsif ($context eq 'statustocreate') {
 4521:                         $check = ' checked="checked" ';
 4522:                     }
 4523:                 }
 4524:                 $output .= '<td class="LC_left_item">'.
 4525:                            '<span class="LC_nobreak"><label>'.
 4526:                            '<input type="checkbox" name="'.$context.'" '.
 4527:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4528:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4529:             }
 4530:         }
 4531:         $rem = @{$types}%($numinrow);
 4532:     }
 4533:     my $colsleft = $numinrow - $rem;
 4534:     if (($rem == 0) && (@{$types} > 0)) {
 4535:         $output .= '<tr>';
 4536:     }
 4537:     if ($colsleft > 1) {
 4538:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4539:     } else {
 4540:         $output .= '<td class="LC_left_item">';
 4541:     }
 4542:     my $defcheck = ' ';
 4543:     if (ref($settings) eq 'HASH') {  
 4544:         if (ref($settings->{$context}) eq 'ARRAY') {
 4545:             if (grep(/^default$/,@{$settings->{$context}})) {
 4546:                 $defcheck = ' checked="checked" ';
 4547:             }
 4548:         } elsif ($context eq 'statustocreate') {
 4549:             $defcheck = ' checked="checked" ';
 4550:         }
 4551:     }
 4552:     $output .= '<span class="LC_nobreak"><label>'.
 4553:                '<input type="checkbox" name="'.$context.'" '.
 4554:                'value="default"'.$defcheck.'/>'.
 4555:                $othertitle.'</label></span></td>'.
 4556:                '</tr></table></td></tr>';
 4557:     return $output;
 4558: }
 4559: 
 4560: sub sorted_searchtitles {
 4561:     my %searchtitles = &Apache::lonlocal::texthash(
 4562:                          'uname' => 'username',
 4563:                          'lastname' => 'last name',
 4564:                          'lastfirst' => 'last name, first name',
 4565:                      );
 4566:     my @titleorder = ('uname','lastname','lastfirst');
 4567:     return (\%searchtitles,\@titleorder);
 4568: }
 4569: 
 4570: sub sorted_searchtypes {
 4571:     my %srchtypes_desc = (
 4572:                            exact    => 'is exact match',
 4573:                            contains => 'contains ..',
 4574:                            begins   => 'begins with ..',
 4575:                          );
 4576:     my @srchtypeorder = ('exact','begins','contains');
 4577:     return (\%srchtypes_desc,\@srchtypeorder);
 4578: }
 4579: 
 4580: sub usertype_update_row {
 4581:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4582:     my $datatable;
 4583:     my $numinrow = 4;
 4584:     foreach my $type (@{$types}) {
 4585:         if (defined($usertypes->{$type})) {
 4586:             $$rownums ++;
 4587:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4588:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4589:                           '</td><td class="LC_left_item"><table>';
 4590:             for (my $i=0; $i<@{$fields}; $i++) {
 4591:                 my $rem = $i%($numinrow);
 4592:                 if ($rem == 0) {
 4593:                     if ($i > 0) {
 4594:                         $datatable .= '</tr>';
 4595:                     }
 4596:                     $datatable .= '<tr>';
 4597:                 }
 4598:                 my $check = ' ';
 4599:                 if (ref($settings) eq 'HASH') {
 4600:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4601:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4602:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4603:                                 $check = ' checked="checked" ';
 4604:                             }
 4605:                         }
 4606:                     }
 4607:                 }
 4608: 
 4609:                 if ($i == @{$fields}-1) {
 4610:                     my $colsleft = $numinrow - $rem;
 4611:                     if ($colsleft > 1) {
 4612:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4613:                     } else {
 4614:                         $datatable .= '<td>';
 4615:                     }
 4616:                 } else {
 4617:                     $datatable .= '<td>';
 4618:                 }
 4619:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4620:                               '<input type="checkbox" name="updateable_'.$type.
 4621:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4622:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4623:             }
 4624:             $datatable .= '</tr></table></td></tr>';
 4625:         }
 4626:     }
 4627:     return $datatable;
 4628: }
 4629: 
 4630: sub modify_login {
 4631:     my ($r,$dom,$confname,%domconfig) = @_;
 4632:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4633:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4634:     %title = ( coursecatalog => 'Display course catalog',
 4635:                adminmail => 'Display administrator E-mail address',
 4636:                newuser => 'Link for visitors to create a user account',
 4637:                loginheader => 'Log-in box header');
 4638:     @offon = ('off','on');
 4639:     if (ref($domconfig{login}) eq 'HASH') {
 4640:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4641:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4642:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4643:             }
 4644:         }
 4645:     }
 4646:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4647:                                            \%domconfig,\%loginhash);
 4648:     my @toggles = ('coursecatalog','adminmail','newuser');
 4649:     foreach my $item (@toggles) {
 4650:         $loginhash{login}{$item} = $env{'form.'.$item};
 4651:     }
 4652:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4653:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4654:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4655:                                          \%loginhash);
 4656:     }
 4657: 
 4658:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4659:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4660:     if (keys(%servers) > 1) {
 4661:         foreach my $lonhost (keys(%servers)) {
 4662:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4663:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4664:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4665:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4666:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4667:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4668:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4669:                         $changes{'loginvia'}{$lonhost} = 1;
 4670:                     } else {
 4671:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4672:                         $changes{'loginvia'}{$lonhost} = 1;
 4673:                     }
 4674:                 } else {
 4675:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4676:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4677:                         $changes{'loginvia'}{$lonhost} = 1;
 4678:                     }
 4679:                 }
 4680:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4681:                     foreach my $item (@loginvia_attribs) {
 4682:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4683:                     }
 4684:                 } else {
 4685:                     foreach my $item (@loginvia_attribs) {
 4686:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4687:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4688:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4689:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4690:                                 $new = '/';
 4691:                             }
 4692:                         }
 4693:                         if (($item eq 'custompath') && 
 4694:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4695:                             $new = '';
 4696:                         }
 4697:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4698:                             $changes{'loginvia'}{$lonhost} = 1;
 4699:                         }
 4700:                         if ($item eq 'exempt') {
 4701:                             $new =~ s/^\s+//;
 4702:                             $new =~ s/\s+$//;
 4703:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4704:                             my @okips;
 4705:                             foreach my $ip (@poss_ips) {
 4706:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4707:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4708:                                         push(@okips,$ip); 
 4709:                                     }
 4710:                                 }
 4711:                             }
 4712:                             if (@okips > 0) {
 4713:                                 $new = join(',',@okips); 
 4714:                             } else {
 4715:                                 $new = ''; 
 4716:                             }
 4717:                         }
 4718:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4719:                     }
 4720:                 }
 4721:             } else {
 4722:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4723:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4724:                     $changes{'loginvia'}{$lonhost} = 1;
 4725:                     foreach my $item (@loginvia_attribs) {
 4726:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4727:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4728:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4729:                                 $new = '/';
 4730:                             }
 4731:                         }
 4732:                         if (($item eq 'custompath') && 
 4733:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4734:                             $new = '';
 4735:                         }
 4736:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4737:                     }
 4738:                 }
 4739:             }
 4740:         }
 4741:     }
 4742: 
 4743:     my $servadm = $r->dir_config('lonAdmEMail');
 4744:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 4745:     if (ref($domconfig{'login'}) eq 'HASH') {
 4746:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 4747:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 4748:                 if ($lang eq 'nolang') {
 4749:                     push(@currlangs,$lang);
 4750:                 } elsif (defined($langchoices{$lang})) {
 4751:                     push(@currlangs,$lang);
 4752:                 } else {
 4753:                     next;
 4754:                 }
 4755:             }
 4756:         }
 4757:     }
 4758:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 4759:     if (@currlangs > 0) {
 4760:         foreach my $lang (@currlangs) {
 4761:             if (grep(/^\Q$lang\E$/,@delurls)) {
 4762:                 $changes{'helpurl'}{$lang} = 1;
 4763:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 4764:                 $changes{'helpurl'}{$lang} = 1;
 4765:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 4766:                 push(@newlangs,$lang);
 4767:             } else {
 4768:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4769:             }
 4770:         }
 4771:     }
 4772:     unless (grep(/^nolang$/,@currlangs)) {
 4773:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 4774:             $changes{'helpurl'}{'nolang'} = 1;
 4775:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 4776:             push(@newlangs,'nolang');
 4777:         }
 4778:     }
 4779:     if ($env{'form.loginhelpurl_add_lang'}) {
 4780:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 4781:             ($env{'form.loginhelpurl_add_file.filename'})) {
 4782:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 4783:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 4784:         }
 4785:     }
 4786:     if ((@newlangs > 0) || ($addedfile)) {
 4787:         my $error;
 4788:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 4789:         if ($configuserok eq 'ok') {
 4790:             if ($switchserver) {
 4791:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 4792:             } elsif ($author_ok eq 'ok') {
 4793:                 my @allnew = @newlangs;
 4794:                 if ($addedfile ne '') {
 4795:                     push(@allnew,$addedfile);
 4796:                 }
 4797:                 foreach my $lang (@allnew) {
 4798:                     my $formelem = 'loginhelpurl_'.$lang;
 4799:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 4800:                         $formelem = 'loginhelpurl_add_file';
 4801:                     }
 4802:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 4803:                                                                "help/$lang",'','',$newfile{$lang});
 4804:                     if ($result eq 'ok') {
 4805:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 4806:                         $changes{'helpurl'}{$lang} = 1;
 4807:                     } else {
 4808:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 4809:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 4810:                         if ((grep(/^\Q$lang\E$/,@currlangs)) && 
 4811:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 4812: 
 4813:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4814:                         }
 4815:                     }
 4816:                 }
 4817:             } else {
 4818:                 $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);
 4819:             }
 4820:         } else {
 4821:             $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);
 4822:         }
 4823:         if ($error) {
 4824:             &Apache::lonnet::logthis($error);
 4825:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4826:         }
 4827:     }
 4828:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 4829: 
 4830:     my $defaulthelpfile = '/adm/loginproblems.html';
 4831:     my $defaulttext = &mt('Default in use');
 4832: 
 4833:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4834:                                              $dom);
 4835:     if ($putresult eq 'ok') {
 4836:         my @toggles = ('coursecatalog','adminmail','newuser');
 4837:         my %defaultchecked = (
 4838:                     'coursecatalog' => 'on',
 4839:                     'adminmail'     => 'off',
 4840:                     'newuser'       => 'off',
 4841:         );
 4842:         if (ref($domconfig{'login'}) eq 'HASH') {
 4843:             foreach my $item (@toggles) {
 4844:                 if ($defaultchecked{$item} eq 'on') { 
 4845:                     if (($domconfig{'login'}{$item} eq '0') &&
 4846:                         ($env{'form.'.$item} eq '1')) {
 4847:                         $changes{$item} = 1;
 4848:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4849:                               $domconfig{'login'}{$item} eq '1') &&
 4850:                              ($env{'form.'.$item} eq '0')) {
 4851:                         $changes{$item} = 1;
 4852:                     }
 4853:                 } elsif ($defaultchecked{$item} eq 'off') {
 4854:                     if (($domconfig{'login'}{$item} eq '1') &&
 4855:                         ($env{'form.'.$item} eq '0')) {
 4856:                         $changes{$item} = 1;
 4857:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4858:                               $domconfig{'login'}{$item} eq '0') &&
 4859:                              ($env{'form.'.$item} eq '1')) {
 4860:                         $changes{$item} = 1;
 4861:                     }
 4862:                 }
 4863:             }
 4864:         }
 4865:         if (keys(%changes) > 0 || $colchgtext) {
 4866:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4867:             $resulttext = &mt('Changes made:').'<ul>';
 4868:             foreach my $item (sort(keys(%changes))) {
 4869:                 if ($item eq 'loginvia') {
 4870:                     if (ref($changes{$item}) eq 'HASH') {
 4871:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4872:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4873:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4874:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4875:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4876:                                     $protocol = 'http' if ($protocol ne 'https');
 4877:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4878: 
 4879:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4880:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4881:                                     } else {
 4882:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4883:                                     }
 4884:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4885:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4886:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4887:                                     }
 4888:                                     $resulttext .= '</li>';
 4889:                                 } else {
 4890:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4891:                                 }
 4892:                             } else {
 4893:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4894:                             }
 4895:                         }
 4896:                         $resulttext .= '</ul></li>';
 4897:                     }
 4898:                 } elsif ($item eq 'helpurl') {
 4899:                     if (ref($changes{$item}) eq 'HASH') {
 4900:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 4901:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 4902:                                 my ($chg,$link);
 4903:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 4904:                                 if ($lang eq 'nolang') {
 4905:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 4906:                                 } else {
 4907:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 4908:                                 }
 4909:                                 $resulttext .= '<li>'.$chg.'</li>';
 4910:                             } else {
 4911:                                 my $chg;
 4912:                                 if ($lang eq 'nolang') {
 4913:                                     $chg = &mt('custom log-in help file for no preferred language');
 4914:                                 } else {
 4915:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 4916:                                 }
 4917:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 4918:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 4919:                                                       '?inhibitmenu=yes',$chg,600,500).
 4920:                                                '</li>';
 4921:                             }
 4922:                         }
 4923:                     }
 4924:                 } elsif ($item eq 'captcha') {
 4925:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4926:                         my $chgtxt; 
 4927:                         if ($loginhash{'login'}{$item} eq 'notused') {
 4928:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 4929:                         } else {
 4930:                             my %captchas = &captcha_phrases();
 4931:                             if ($captchas{$loginhash{'login'}{$item}}) {
 4932:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 4933:                             } else {
 4934:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 4935:                             }
 4936:                         }
 4937:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4938:                     }
 4939:                 } elsif ($item eq 'recaptchakeys') {
 4940:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4941:                         my ($privkey,$pubkey);
 4942:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 4943:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 4944:                             $privkey = $loginhash{'login'}{$item}{'private'};
 4945:                         }
 4946:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 4947:                         if (!$pubkey) {
 4948:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 4949:                         } else {
 4950:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 4951:                         }
 4952:                         if (!$privkey) {
 4953:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 4954:                         } else {
 4955:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 4956:                         }
 4957:                         $chgtxt .= '</ul>';
 4958:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4959:                     }
 4960:                 } else {
 4961:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4962:                 }
 4963:             }
 4964:             $resulttext .= $colchgtext.'</ul>';
 4965:         } else {
 4966:             $resulttext = &mt('No changes made to log-in page settings');
 4967:         }
 4968:     } else {
 4969:         $resulttext = '<span class="LC_error">'.
 4970: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4971:     }
 4972:     if ($errors) {
 4973:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 4974:                        $errors.'</ul>';
 4975:     }
 4976:     return $resulttext;
 4977: }
 4978: 
 4979: sub color_font_choices {
 4980:     my %choices =
 4981:         &Apache::lonlocal::texthash (
 4982:             img => "Header",
 4983:             bgs => "Background colors",
 4984:             links => "Link colors",
 4985:             images => "Images",
 4986:             font => "Font color",
 4987:             fontmenu => "Font Menu",
 4988:             pgbg => "Page",
 4989:             tabbg => "Header",
 4990:             sidebg => "Border",
 4991:             link => "Link",
 4992:             alink => "Active link",
 4993:             vlink => "Visited link",
 4994:         );
 4995:     return %choices;
 4996: }
 4997: 
 4998: sub modify_rolecolors {
 4999:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 5000:     my ($resulttext,%rolehash);
 5001:     $rolehash{'rolecolors'} = {};
 5002:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5003:         if ($domconfig{'rolecolors'} eq '') {
 5004:             $domconfig{'rolecolors'} = {};
 5005:         }
 5006:     }
 5007:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5008:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5009:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5010:                                              $dom);
 5011:     if ($putresult eq 'ok') {
 5012:         if (keys(%changes) > 0) {
 5013:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5014:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5015:                                              $rolehash{'rolecolors'});
 5016:         } else {
 5017:             $resulttext = &mt('No changes made to default color schemes');
 5018:         }
 5019:     } else {
 5020:         $resulttext = '<span class="LC_error">'.
 5021: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5022:     }
 5023:     if ($errors) {
 5024:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5025:                        $errors.'</ul>';
 5026:     }
 5027:     return $resulttext;
 5028: }
 5029: 
 5030: sub modify_colors {
 5031:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5032:     my (%changes,%choices);
 5033:     my @bgs;
 5034:     my @links = ('link','alink','vlink');
 5035:     my @logintext;
 5036:     my @images;
 5037:     my $servadm = $r->dir_config('lonAdmEMail');
 5038:     my $errors;
 5039:     foreach my $role (@{$roles}) {
 5040:         if ($role eq 'login') {
 5041:             %choices = &login_choices();
 5042:             @logintext = ('textcol','bgcol');
 5043:         } else {
 5044:             %choices = &color_font_choices();
 5045:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5046:         }
 5047:         if ($role eq 'login') {
 5048:             @images = ('img','logo','domlogo','login');
 5049:             @bgs = ('pgbg','mainbg','sidebg');
 5050:         } else {
 5051:             @images = ('img');
 5052:             @bgs = ('pgbg','tabbg','sidebg'); 
 5053:         }
 5054:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5055:         foreach my $item (@bgs,@links,@logintext) {
 5056:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5057:         }
 5058:         my ($configuserok,$author_ok,$switchserver) = 
 5059:             &config_check($dom,$confname,$servadm);
 5060:         my ($width,$height) = &thumb_dimensions();
 5061:         if (ref($domconfig->{$role}) ne 'HASH') {
 5062:             $domconfig->{$role} = {};
 5063:         }
 5064:         foreach my $img (@images) {
 5065:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5066:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5067:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5068:                 } else { 
 5069:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5070:                 }
 5071:             } 
 5072: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5073: 		 && !defined($domconfig->{$role}{$img})
 5074: 		 && !$env{'form.'.$role.'_del_'.$img}
 5075: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5076: 		# import the old configured image from the .tab setting
 5077: 		# if they haven't provided a new one 
 5078: 		$domconfig->{$role}{$img} = 
 5079: 		    $env{'form.'.$role.'_import_'.$img};
 5080: 	    }
 5081:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5082:                 my $error;
 5083:                 if ($configuserok eq 'ok') {
 5084:                     if ($switchserver) {
 5085:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5086:                     } else {
 5087:                         if ($author_ok eq 'ok') {
 5088:                             my ($result,$logourl) = 
 5089:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5090:                                            $dom,$confname,$img,$width,$height);
 5091:                             if ($result eq 'ok') {
 5092:                                 $confhash->{$role}{$img} = $logourl;
 5093:                                 $changes{$role}{'images'}{$img} = 1;
 5094:                             } else {
 5095:                                 $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);
 5096:                             }
 5097:                         } else {
 5098:                             $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);
 5099:                         }
 5100:                     }
 5101:                 } else {
 5102:                     $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);
 5103:                 }
 5104:                 if ($error) {
 5105:                     &Apache::lonnet::logthis($error);
 5106:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5107:                 }
 5108:             } elsif ($domconfig->{$role}{$img} ne '') {
 5109:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5110:                     my $error;
 5111:                     if ($configuserok eq 'ok') {
 5112: # is confname an author?
 5113:                         if ($switchserver eq '') {
 5114:                             if ($author_ok eq 'ok') {
 5115:                                 my ($result,$logourl) = 
 5116:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5117:                                             $dom,$confname,$img,$width,$height);
 5118:                                 if ($result eq 'ok') {
 5119:                                     $confhash->{$role}{$img} = $logourl;
 5120: 				    $changes{$role}{'images'}{$img} = 1;
 5121:                                 }
 5122:                             }
 5123:                         }
 5124:                     }
 5125:                 }
 5126:             }
 5127:         }
 5128:         if (ref($domconfig) eq 'HASH') {
 5129:             if (ref($domconfig->{$role}) eq 'HASH') {
 5130:                 foreach my $img (@images) {
 5131:                     if ($domconfig->{$role}{$img} ne '') {
 5132:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5133:                             $confhash->{$role}{$img} = '';
 5134:                             $changes{$role}{'images'}{$img} = 1;
 5135:                         } else {
 5136:                             if ($confhash->{$role}{$img} eq '') {
 5137:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5138:                             }
 5139:                         }
 5140:                     } else {
 5141:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5142:                             $confhash->{$role}{$img} = '';
 5143:                             $changes{$role}{'images'}{$img} = 1;
 5144:                         } 
 5145:                     }
 5146:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5147:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5148:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5149:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5150:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5151:                             }
 5152:                         } else {
 5153:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5154:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5155:                             }
 5156:                         }
 5157:                     }
 5158:                 }
 5159:                 if ($domconfig->{$role}{'font'} ne '') {
 5160:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5161:                         $changes{$role}{'font'} = 1;
 5162:                     }
 5163:                 } else {
 5164:                     if ($confhash->{$role}{'font'}) {
 5165:                         $changes{$role}{'font'} = 1;
 5166:                     }
 5167:                 }
 5168:                 if ($role ne 'login') {
 5169:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5170:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5171:                             $changes{$role}{'fontmenu'} = 1;
 5172:                         }
 5173:                     } else {
 5174:                         if ($confhash->{$role}{'fontmenu'}) {
 5175:                             $changes{$role}{'fontmenu'} = 1;
 5176:                         }
 5177:                     }
 5178:                 }
 5179:                 foreach my $item (@bgs) {
 5180:                     if ($domconfig->{$role}{$item} ne '') {
 5181:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5182:                             $changes{$role}{'bgs'}{$item} = 1;
 5183:                         } 
 5184:                     } else {
 5185:                         if ($confhash->{$role}{$item}) {
 5186:                             $changes{$role}{'bgs'}{$item} = 1;
 5187:                         }
 5188:                     }
 5189:                 }
 5190:                 foreach my $item (@links) {
 5191:                     if ($domconfig->{$role}{$item} ne '') {
 5192:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5193:                             $changes{$role}{'links'}{$item} = 1;
 5194:                         }
 5195:                     } else {
 5196:                         if ($confhash->{$role}{$item}) {
 5197:                             $changes{$role}{'links'}{$item} = 1;
 5198:                         }
 5199:                     }
 5200:                 }
 5201:                 foreach my $item (@logintext) {
 5202:                     if ($domconfig->{$role}{$item} ne '') {
 5203:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5204:                             $changes{$role}{'logintext'}{$item} = 1;
 5205:                         }
 5206:                     } else {
 5207:                         if ($confhash->{$role}{$item}) {
 5208:                             $changes{$role}{'logintext'}{$item} = 1;
 5209:                         }
 5210:                     }
 5211:                 }
 5212:             } else {
 5213:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5214:                                         \@logintext,$confhash,\%changes); 
 5215:             }
 5216:         } else {
 5217:             &default_change_checker($role,\@images,\@links,\@bgs,
 5218:                                     \@logintext,$confhash,\%changes); 
 5219:         }
 5220:     }
 5221:     return ($errors,%changes);
 5222: }
 5223: 
 5224: sub config_check {
 5225:     my ($dom,$confname,$servadm) = @_;
 5226:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5227:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5228:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5229:                                                    $confname,$servadm);
 5230:     if ($configuserok eq 'ok') {
 5231:         $switchserver = &check_switchserver($dom,$confname);
 5232:         if ($switchserver eq '') {
 5233:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5234:         }
 5235:     }
 5236:     return ($configuserok,$author_ok,$switchserver);
 5237: }
 5238: 
 5239: sub default_change_checker {
 5240:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5241:     foreach my $item (@{$links}) {
 5242:         if ($confhash->{$role}{$item}) {
 5243:             $changes->{$role}{'links'}{$item} = 1;
 5244:         }
 5245:     }
 5246:     foreach my $item (@{$bgs}) {
 5247:         if ($confhash->{$role}{$item}) {
 5248:             $changes->{$role}{'bgs'}{$item} = 1;
 5249:         }
 5250:     }
 5251:     foreach my $item (@{$logintext}) {
 5252:         if ($confhash->{$role}{$item}) {
 5253:             $changes->{$role}{'logintext'}{$item} = 1;
 5254:         }
 5255:     }
 5256:     foreach my $img (@{$images}) {
 5257:         if ($env{'form.'.$role.'_del_'.$img}) {
 5258:             $confhash->{$role}{$img} = '';
 5259:             $changes->{$role}{'images'}{$img} = 1;
 5260:         }
 5261:         if ($role eq 'login') {
 5262:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5263:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5264:             }
 5265:         }
 5266:     }
 5267:     if ($confhash->{$role}{'font'}) {
 5268:         $changes->{$role}{'font'} = 1;
 5269:     }
 5270: }
 5271: 
 5272: sub display_colorchgs {
 5273:     my ($dom,$changes,$roles,$confhash) = @_;
 5274:     my (%choices,$resulttext);
 5275:     if (!grep(/^login$/,@{$roles})) {
 5276:         $resulttext = &mt('Changes made:').'<br />';
 5277:     }
 5278:     foreach my $role (@{$roles}) {
 5279:         if ($role eq 'login') {
 5280:             %choices = &login_choices();
 5281:         } else {
 5282:             %choices = &color_font_choices();
 5283:         }
 5284:         if (ref($changes->{$role}) eq 'HASH') {
 5285:             if ($role ne 'login') {
 5286:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5287:             }
 5288:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5289:                 if ($role ne 'login') {
 5290:                     $resulttext .= '<ul>';
 5291:                 }
 5292:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5293:                     if ($role ne 'login') {
 5294:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5295:                     }
 5296:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5297:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5298:                             if ($confhash->{$role}{$key}{$item}) {
 5299:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5300:                             } else {
 5301:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5302:                             }
 5303:                         } elsif ($confhash->{$role}{$item} eq '') {
 5304:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5305:                         } else {
 5306:                             my $newitem = $confhash->{$role}{$item};
 5307:                             if ($key eq 'images') {
 5308:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5309:                             }
 5310:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5311:                         }
 5312:                     }
 5313:                     if ($role ne 'login') {
 5314:                         $resulttext .= '</ul></li>';
 5315:                     }
 5316:                 } else {
 5317:                     if ($confhash->{$role}{$key} eq '') {
 5318:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5319:                     } else {
 5320:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5321:                     }
 5322:                 }
 5323:                 if ($role ne 'login') {
 5324:                     $resulttext .= '</ul>';
 5325:                 }
 5326:             }
 5327:         }
 5328:     }
 5329:     return $resulttext;
 5330: }
 5331: 
 5332: sub thumb_dimensions {
 5333:     return ('200','50');
 5334: }
 5335: 
 5336: sub check_dimensions {
 5337:     my ($inputfile) = @_;
 5338:     my ($fullwidth,$fullheight);
 5339:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5340:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5341:             my $imageinfo = <PIPE>;
 5342:             if (!close(PIPE)) {
 5343:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5344:             }
 5345:             chomp($imageinfo);
 5346:             my ($fullsize) = 
 5347:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5348:             if ($fullsize) {
 5349:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5350:             }
 5351:         }
 5352:     }
 5353:     return ($fullwidth,$fullheight);
 5354: }
 5355: 
 5356: sub check_configuser {
 5357:     my ($uhome,$dom,$confname,$servadm) = @_;
 5358:     my ($configuserok,%currroles);
 5359:     if ($uhome eq 'no_host') {
 5360:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5361:         my $configpass = &LONCAPA::Enrollment::create_password();
 5362:         $configuserok = 
 5363:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5364:                              $configpass,'','','','','',undef,$servadm);
 5365:     } else {
 5366:         $configuserok = 'ok';
 5367:         %currroles = 
 5368:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5369:     }
 5370:     return ($configuserok,%currroles);
 5371: }
 5372: 
 5373: sub check_authorstatus {
 5374:     my ($dom,$confname,%currroles) = @_;
 5375:     my $author_ok;
 5376:     if (!$currroles{':'.$dom.':au'}) {
 5377:         my $start = time;
 5378:         my $end = 0;
 5379:         $author_ok = 
 5380:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5381:                                         'au',$end,$start,'','','domconfig');
 5382:     } else {
 5383:         $author_ok = 'ok';
 5384:     }
 5385:     return $author_ok;
 5386: }
 5387: 
 5388: sub publishlogo {
 5389:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5390:     my ($output,$fname,$logourl);
 5391:     if ($action eq 'upload') {
 5392:         $fname=$env{'form.'.$formname.'.filename'};
 5393:         chop($env{'form.'.$formname});
 5394:     } else {
 5395:         ($fname) = ($formname =~ /([^\/]+)$/);
 5396:     }
 5397:     if ($savefileas ne '') {
 5398:         $fname = $savefileas;
 5399:     }
 5400:     $fname=&Apache::lonnet::clean_filename($fname);
 5401: # See if there is anything left
 5402:     unless ($fname) { return ('error: no uploaded file'); }
 5403:     $fname="$subdir/$fname";
 5404:     my $docroot=$r->dir_config('lonDocRoot'); 
 5405:     my $filepath="$docroot/priv";
 5406:     my $relpath = "$dom/$confname";
 5407:     my ($fnamepath,$file,$fetchthumb);
 5408:     $file=$fname;
 5409:     if ($fname=~m|/|) {
 5410:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5411:     }
 5412:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5413:     my $count;
 5414:     for ($count=5;$count<=$#parts;$count++) {
 5415:         $filepath.="/$parts[$count]";
 5416:         if ((-e $filepath)!=1) {
 5417:             mkdir($filepath,02770);
 5418:         }
 5419:     }
 5420:     # Check for bad extension and disallow upload
 5421:     if ($file=~/\.(\w+)$/ &&
 5422:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5423:         $output = 
 5424:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 5425:     } elsif ($file=~/\.(\w+)$/ &&
 5426:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5427:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5428:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5429:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5430:     } elsif (-d "$filepath/$file") {
 5431:         $output = &mt('File name is a directory name - rename the file and re-upload');
 5432:     } else {
 5433:         my $source = $filepath.'/'.$file;
 5434:         my $logfile;
 5435:         if (!open($logfile,">>$source".'.log')) {
 5436:             return (&mt('No write permission to Construction Space'));
 5437:         }
 5438:         print $logfile
 5439: "\n================= Publish ".localtime()." ================\n".
 5440: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5441: # Save the file
 5442:         if (!open(FH,'>'.$source)) {
 5443:             &Apache::lonnet::logthis('Failed to create '.$source);
 5444:             return (&mt('Failed to create file'));
 5445:         }
 5446:         if ($action eq 'upload') {
 5447:             if (!print FH ($env{'form.'.$formname})) {
 5448:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5449:                 return (&mt('Failed to write file'));
 5450:             }
 5451:         } else {
 5452:             my $original = &Apache::lonnet::filelocation('',$formname);
 5453:             if(!copy($original,$source)) {
 5454:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5455:                 return (&mt('Failed to write file'));
 5456:             }
 5457:         }
 5458:         close(FH);
 5459:         chmod(0660, $source); # Permissions to rw-rw---.
 5460: 
 5461:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5462:         my $copyfile=$targetdir.'/'.$file;
 5463: 
 5464:         my @parts=split(/\//,$targetdir);
 5465:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5466:         for (my $count=5;$count<=$#parts;$count++) {
 5467:             $path.="/$parts[$count]";
 5468:             if (!-e $path) {
 5469:                 print $logfile "\nCreating directory ".$path;
 5470:                 mkdir($path,02770);
 5471:             }
 5472:         }
 5473:         my $versionresult;
 5474:         if (-e $copyfile) {
 5475:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5476:         } else {
 5477:             $versionresult = 'ok';
 5478:         }
 5479:         if ($versionresult eq 'ok') {
 5480:             if (copy($source,$copyfile)) {
 5481:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5482:                 $output = 'ok';
 5483:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5484:                 push(@{$modified_urls},[$copyfile,$source]);
 5485:                 my $metaoutput = 
 5486:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5487:                 unless ($registered_cleanup) {
 5488:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5489:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5490:                     $registered_cleanup=1;
 5491:                 }
 5492:             } else {
 5493:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5494:                 $output = &mt('Failed to copy file to RES space').", $!";
 5495:             }
 5496:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5497:                 my $inputfile = $filepath.'/'.$file;
 5498:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5499:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5500:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5501:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5502:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5503:                         system("convert -sample $thumbsize $inputfile $outfile");
 5504:                         chmod(0660, $filepath.'/tn-'.$file);
 5505:                         if (-e $outfile) {
 5506:                             my $copyfile=$targetdir.'/tn-'.$file;
 5507:                             if (copy($outfile,$copyfile)) {
 5508:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5509:                                 my $thumb_metaoutput = 
 5510:                                     &write_metadata($dom,$confname,$formname,
 5511:                                                     $targetdir,'tn-'.$file,$logfile);
 5512:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5513:                                 unless ($registered_cleanup) {
 5514:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5515:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5516:                                     $registered_cleanup=1;
 5517:                                 }
 5518:                             } else {
 5519:                                 print $logfile "\nUnable to write ".$copyfile.
 5520:                                                ':'.$!."\n";
 5521:                             }
 5522:                         }
 5523:                     }
 5524:                 }
 5525:             }
 5526:         } else {
 5527:             $output = $versionresult;
 5528:         }
 5529:     }
 5530:     return ($output,$logourl);
 5531: }
 5532: 
 5533: sub logo_versioning {
 5534:     my ($targetdir,$file,$logfile) = @_;
 5535:     my $target = $targetdir.'/'.$file;
 5536:     my ($maxversion,$fn,$extn,$output);
 5537:     $maxversion = 0;
 5538:     if ($file =~ /^(.+)\.(\w+)$/) {
 5539:         $fn=$1;
 5540:         $extn=$2;
 5541:     }
 5542:     opendir(DIR,$targetdir);
 5543:     while (my $filename=readdir(DIR)) {
 5544:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5545:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5546:         }
 5547:     }
 5548:     $maxversion++;
 5549:     print $logfile "\nCreating old version ".$maxversion."\n";
 5550:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5551:     if (copy($target,$copyfile)) {
 5552:         print $logfile "Copied old target to ".$copyfile."\n";
 5553:         $copyfile=$copyfile.'.meta';
 5554:         if (copy($target.'.meta',$copyfile)) {
 5555:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5556:             $output = 'ok';
 5557:         } else {
 5558:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5559:             $output = &mt('Failed to copy old meta').", $!, ";
 5560:         }
 5561:     } else {
 5562:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5563:         $output = &mt('Failed to copy old target').", $!, ";
 5564:     }
 5565:     return $output;
 5566: }
 5567: 
 5568: sub write_metadata {
 5569:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5570:     my (%metadatafields,%metadatakeys,$output);
 5571:     $metadatafields{'title'}=$formname;
 5572:     $metadatafields{'creationdate'}=time;
 5573:     $metadatafields{'lastrevisiondate'}=time;
 5574:     $metadatafields{'copyright'}='public';
 5575:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5576:                                          $env{'user.domain'};
 5577:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5578:     $metadatafields{'domain'}=$dom;
 5579:     {
 5580:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5581:         my $mfh;
 5582:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5583:             foreach (sort keys %metadatafields) {
 5584:                 unless ($_=~/\./) {
 5585:                     my $unikey=$_;
 5586:                     $unikey=~/^([A-Za-z]+)/;
 5587:                     my $tag=$1;
 5588:                     $tag=~tr/A-Z/a-z/;
 5589:                     print $mfh "\n\<$tag";
 5590:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5591:                         my $value=$metadatafields{$unikey.'.'.$_};
 5592:                         $value=~s/\"/\'\'/g;
 5593:                         print $mfh ' '.$_.'="'.$value.'"';
 5594:                     }
 5595:                     print $mfh '>'.
 5596:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5597:                             .'</'.$tag.'>';
 5598:                 }
 5599:             }
 5600:             $output = 'ok';
 5601:             print $logfile "\nWrote metadata";
 5602:             close($mfh);
 5603:         } else {
 5604:             print $logfile "\nFailed to open metadata file";
 5605:             $output = &mt('Could not write metadata');
 5606:         }
 5607:     }
 5608:     return $output;
 5609: }
 5610: 
 5611: sub notifysubscribed {
 5612:     foreach my $targetsource (@{$modified_urls}){
 5613:         next unless (ref($targetsource) eq 'ARRAY');
 5614:         my ($target,$source)=@{$targetsource};
 5615:         if ($source ne '') {
 5616:             if (open(my $logfh,'>>'.$source.'.log')) {
 5617:                 print $logfh "\nCleanup phase: Notifications\n";
 5618:                 my @subscribed=&subscribed_hosts($target);
 5619:                 foreach my $subhost (@subscribed) {
 5620:                     print $logfh "\nNotifying host ".$subhost.':';
 5621:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5622:                     print $logfh $reply;
 5623:                 }
 5624:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5625:                 foreach my $subhost (@subscribedmeta) {
 5626:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5627:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5628:                                                         $subhost);
 5629:                     print $logfh $reply;
 5630:                 }
 5631:                 print $logfh "\n============ Done ============\n";
 5632:                 close($logfh);
 5633:             }
 5634:         }
 5635:     }
 5636:     return OK;
 5637: }
 5638: 
 5639: sub subscribed_hosts {
 5640:     my ($target) = @_;
 5641:     my @subscribed;
 5642:     if (open(my $fh,"<$target.subscription")) {
 5643:         while (my $subline=<$fh>) {
 5644:             if ($subline =~ /^($match_lonid):/) {
 5645:                 my $host = $1;
 5646:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5647:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5648:                         push(@subscribed,$host);
 5649:                     }
 5650:                 }
 5651:             }
 5652:         }
 5653:     }
 5654:     return @subscribed;
 5655: }
 5656: 
 5657: sub check_switchserver {
 5658:     my ($dom,$confname) = @_;
 5659:     my ($allowed,$switchserver);
 5660:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5661:     if ($home eq 'no_host') {
 5662:         $home = &Apache::lonnet::domain($dom,'primary');
 5663:     }
 5664:     my @ids=&Apache::lonnet::current_machine_ids();
 5665:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5666:     if (!$allowed) {
 5667: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&role=dc./'.$dom.'/">'.&mt('Switch Server').'</a>';
 5668:     }
 5669:     return $switchserver;
 5670: }
 5671: 
 5672: sub modify_quotas {
 5673:     my ($dom,$action,%domconfig) = @_;
 5674:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5675:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5676:     if ($action eq 'quotas') {
 5677:         $context = 'tools'; 
 5678:     } else {
 5679:         $context = $action;
 5680:     }
 5681:     if ($context eq 'requestcourses') {
 5682:         @usertools = ('official','unofficial','community');
 5683:         @options =('norequest','approval','validate','autolimit');
 5684:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5685:         %titles = &courserequest_titles();
 5686:         $toolregexp = join('|',@usertools);
 5687:         %conditions = &courserequest_conditions();
 5688:     } elsif ($context eq 'requestauthor') {
 5689:         @usertools = ('author');
 5690:         %titles = &authorrequest_titles();
 5691:     } else {
 5692:         @usertools = ('aboutme','blog','webdav','portfolio');
 5693:         %titles = &tool_titles();
 5694:     }
 5695:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5696:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5697:     foreach my $key (keys(%env)) {
 5698:         if ($context eq 'requestcourses') {
 5699:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5700:                 my $item = $1;
 5701:                 my $type = $2;
 5702:                 if ($type =~ /^limit_(.+)/) {
 5703:                     $limithash{$item}{$1} = $env{$key};
 5704:                 } else {
 5705:                     $confhash{$item}{$type} = $env{$key};
 5706:                 }
 5707:             }
 5708:         } elsif ($context eq 'requestauthor') {
 5709:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5710:                 $confhash{$1} = $env{$key};
 5711:             }
 5712:         } else {
 5713:             if ($key =~ /^form\.quota_(.+)$/) {
 5714:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5715:             }
 5716:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5717:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5718:             }
 5719:         }
 5720:     }
 5721:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5722:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5723:         @approvalnotify = sort(@approvalnotify);
 5724:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5725:         if (ref($domconfig{$action}) eq 'HASH') {
 5726:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5727:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5728:                     $changes{'notify'}{'approval'} = 1;
 5729:                 }
 5730:             } else {
 5731:                 if ($confhash{'notify'}{'approval'}) {
 5732:                     $changes{'notify'}{'approval'} = 1;
 5733:                 }
 5734:             }
 5735:         } else {
 5736:             if ($confhash{'notify'}{'approval'}) {
 5737:                 $changes{'notify'}{'approval'} = 1;
 5738:             }
 5739:         }
 5740:     } else {
 5741:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5742:     }
 5743:     foreach my $item (@usertools) {
 5744:         foreach my $type (@{$types},'default','_LC_adv') {
 5745:             my $unset; 
 5746:             if ($context eq 'requestcourses') {
 5747:                 $unset = '0';
 5748:                 if ($type eq '_LC_adv') {
 5749:                     $unset = '';
 5750:                 }
 5751:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5752:                     $confhash{$item}{$type} .= '=';
 5753:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5754:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5755:                     }
 5756:                 }
 5757:             } elsif ($context eq 'requestauthor') {
 5758:                 $unset = '0';
 5759:                 if ($type eq '_LC_adv') {
 5760:                     $unset = '';
 5761:                 }
 5762:             } else {
 5763:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5764:                     $confhash{$item}{$type} = 1;
 5765:                 } else {
 5766:                     $confhash{$item}{$type} = 0;
 5767:                 }
 5768:             }
 5769:             if (ref($domconfig{$action}) eq 'HASH') {
 5770:                 if ($action eq 'requestauthor') {
 5771:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5772:                         $changes{$type} = 1;
 5773:                     }
 5774:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5775:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5776:                         $changes{$item}{$type} = 1;
 5777:                     }
 5778:                 } else {
 5779:                     if ($context eq 'requestcourses') {
 5780:                         if ($confhash{$item}{$type} ne $unset) {
 5781:                             $changes{$item}{$type} = 1;
 5782:                         }
 5783:                     } else {
 5784:                         if (!$confhash{$item}{$type}) {
 5785:                             $changes{$item}{$type} = 1;
 5786:                         }
 5787:                     }
 5788:                 }
 5789:             } else {
 5790:                 if ($context eq 'requestcourses') {
 5791:                     if ($confhash{$item}{$type} ne $unset) {
 5792:                         $changes{$item}{$type} = 1;
 5793:                     }
 5794:                 } elsif ($context eq 'requestauthor') {
 5795:                     if ($confhash{$type} ne $unset) {
 5796:                         $changes{$type} = 1;
 5797:                     }
 5798:                 } else {
 5799:                     if (!$confhash{$item}{$type}) {
 5800:                         $changes{$item}{$type} = 1;
 5801:                     }
 5802:                 }
 5803:             }
 5804:         }
 5805:     }
 5806:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5807:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5808:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5809:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5810:                     if (exists($confhash{'defaultquota'}{$key})) {
 5811:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5812:                             $changes{'defaultquota'}{$key} = 1;
 5813:                         }
 5814:                     } else {
 5815:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5816:                     }
 5817:                 }
 5818:             } else {
 5819:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5820:                     if (exists($confhash{'defaultquota'}{$key})) {
 5821:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5822:                             $changes{'defaultquota'}{$key} = 1;
 5823:                         }
 5824:                     } else {
 5825:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5826:                     }
 5827:                 }
 5828:             }
 5829:         }
 5830:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5831:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5832:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5833:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5834:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5835:                             $changes{'defaultquota'}{$key} = 1;
 5836:                         }
 5837:                     } else {
 5838:                         if (!exists($domconfig{'quotas'}{$key})) {
 5839:                             $changes{'defaultquota'}{$key} = 1;
 5840:                         }
 5841:                     }
 5842:                 } else {
 5843:                     $changes{'defaultquota'}{$key} = 1;
 5844:                 }
 5845:             }
 5846:         }
 5847:     }
 5848: 
 5849:     if ($context eq 'requestauthor') {
 5850:         $domdefaults{'requestauthor'} = \%confhash;
 5851:     } else {
 5852:         foreach my $key (keys(%confhash)) {
 5853:             $domdefaults{$key} = $confhash{$key};
 5854:         }
 5855:     }
 5856: 
 5857:     my %quotahash = (
 5858:                       $action => { %confhash }
 5859:                     );
 5860:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5861:                                              $dom);
 5862:     if ($putresult eq 'ok') {
 5863:         if (keys(%changes) > 0) {
 5864:             my $cachetime = 24*60*60;
 5865:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5866: 
 5867:             $resulttext = &mt('Changes made:').'<ul>';
 5868:             unless (($context eq 'requestcourses') || 
 5869:                     ($context eq 'requestauthor')) {
 5870:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5871:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5872:                     foreach my $type (@{$types},'default') {
 5873:                         if (defined($changes{'defaultquota'}{$type})) {
 5874:                             my $typetitle = $usertypes->{$type};
 5875:                             if ($type eq 'default') {
 5876:                                 $typetitle = $othertitle;
 5877:                             }
 5878:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5879:                         }
 5880:                     }
 5881:                     $resulttext .= '</ul></li>';
 5882:                 }
 5883:             }
 5884:             my %newenv;
 5885:             foreach my $item (@usertools) {
 5886:                 my (%haschgs,%inconf);
 5887:                 if ($context eq 'requestauthor') {
 5888:                     %haschgs = %changes;
 5889:                     %inconf = %confhash; 
 5890:                 } else {
 5891:                     if (ref($changes{$item}) eq 'HASH') {
 5892:                         %haschgs = %{$changes{$item}};
 5893:                     }
 5894:                     if (ref($confhash{$item}) eq 'HASH') {
 5895:                         %inconf = %{$confhash{$item}};
 5896:                     }
 5897:                 }
 5898:                 if (keys(%haschgs) > 0) {
 5899:                     my $newacc = 
 5900:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5901:                                                           $env{'user.domain'},
 5902:                                                           $item,'reload',$context);
 5903:                     if (($context eq 'requestcourses') || 
 5904:                         ($context eq 'requestauthor')) {
 5905:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5906:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5907:                         }
 5908:                     } else {
 5909:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5910:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5911:                         }
 5912:                     }
 5913:                     unless ($context eq 'requestauthor') {
 5914:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5915:                     }
 5916:                     foreach my $type (@{$types},'default','_LC_adv') {
 5917:                         if ($haschgs{$type}) {
 5918:                             my $typetitle = $usertypes->{$type};
 5919:                             if ($type eq 'default') {
 5920:                                 $typetitle = $othertitle;
 5921:                             } elsif ($type eq '_LC_adv') {
 5922:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5923:                             }
 5924:                             if ($inconf{$type}) {
 5925:                                 if ($context eq 'requestcourses') {
 5926:                                     my $cond;
 5927:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 5928:                                         if ($1 eq '') {
 5929:                                             $cond = &mt('(Automatic processing of any request).');
 5930:                                         } else {
 5931:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5932:                                         }
 5933:                                     } else { 
 5934:                                         $cond = $conditions{$inconf{$type}};
 5935:                                     }
 5936:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5937:                                 } else {
 5938:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5939:                                 }
 5940:                             } else {
 5941:                                 if ($type eq '_LC_adv') {
 5942:                                     if ($inconf{$type} eq '0') {
 5943:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5944:                                     } else { 
 5945:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5946:                                     }
 5947:                                 } else {
 5948:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5949:                                 }
 5950:                             }
 5951:                         }
 5952:                     }
 5953:                     unless ($context eq 'requestauthor') {
 5954:                         $resulttext .= '</ul></li>';
 5955:                     }
 5956:                 }
 5957:             }
 5958:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 5959:                 if (ref($changes{'notify'}) eq 'HASH') {
 5960:                     if ($changes{'notify'}{'approval'}) {
 5961:                         if (ref($confhash{'notify'}) eq 'HASH') {
 5962:                             if ($confhash{'notify'}{'approval'}) {
 5963:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 5964:                             } else {
 5965:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 5966:                             }
 5967:                         }
 5968:                     }
 5969:                 }
 5970:             }
 5971:             $resulttext .= '</ul>';
 5972:             if (keys(%newenv)) {
 5973:                 &Apache::lonnet::appenv(\%newenv);
 5974:             }
 5975:         } else {
 5976:             if ($context eq 'requestcourses') {
 5977:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 5978:             } elsif ($context eq 'requestauthor') {
 5979:                 $resulttext = &mt('No changes made to rights to request author space.');
 5980:             } else {
 5981:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 5982:             }
 5983:         }
 5984:     } else {
 5985:         $resulttext = '<span class="LC_error">'.
 5986: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5987:     }
 5988:     return $resulttext;
 5989: }
 5990: 
 5991: sub modify_autoenroll {
 5992:     my ($dom,%domconfig) = @_;
 5993:     my ($resulttext,%changes);
 5994:     my %currautoenroll;
 5995:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 5996:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 5997:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 5998:         }
 5999:     }
 6000:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6001:     my %title = ( run => 'Auto-enrollment active',
 6002:                   sender => 'Sender for notification messages',
 6003:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6004:     my @offon = ('off','on');
 6005:     my $sender_uname = $env{'form.sender_uname'};
 6006:     my $sender_domain = $env{'form.sender_domain'};
 6007:     if ($sender_domain eq '') {
 6008:         $sender_uname = '';
 6009:     } elsif ($sender_uname eq '') {
 6010:         $sender_domain = '';
 6011:     }
 6012:     my $coowners = $env{'form.autoassign_coowners'};
 6013:     my %autoenrollhash =  (
 6014:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6015:                                        'sender_uname' => $sender_uname,
 6016:                                        'sender_domain' => $sender_domain,
 6017:                                        'co-owners' => $coowners,
 6018:                                 }
 6019:                      );
 6020:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6021:                                              $dom);
 6022:     if ($putresult eq 'ok') {
 6023:         if (exists($currautoenroll{'run'})) {
 6024:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6025:                  $changes{'run'} = 1;
 6026:              }
 6027:         } elsif ($autorun) {
 6028:             if ($env{'form.autoenroll_run'} ne '1') {
 6029:                  $changes{'run'} = 1;
 6030:             }
 6031:         }
 6032:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6033:             $changes{'sender'} = 1;
 6034:         }
 6035:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6036:             $changes{'sender'} = 1;
 6037:         }
 6038:         if ($currautoenroll{'co-owners'} ne '') {
 6039:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6040:                 $changes{'coowners'} = 1;
 6041:             }
 6042:         } elsif ($coowners) {
 6043:             $changes{'coowners'} = 1;
 6044:         }      
 6045:         if (keys(%changes) > 0) {
 6046:             $resulttext = &mt('Changes made:').'<ul>';
 6047:             if ($changes{'run'}) {
 6048:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6049:             }
 6050:             if ($changes{'sender'}) {
 6051:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6052:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6053:                 } else {
 6054:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6055:                 }
 6056:             }
 6057:             if ($changes{'coowners'}) {
 6058:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6059:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6060:             }
 6061:             $resulttext .= '</ul>';
 6062:         } else {
 6063:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6064:         }
 6065:     } else {
 6066:         $resulttext = '<span class="LC_error">'.
 6067: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6068:     }
 6069:     return $resulttext;
 6070: }
 6071: 
 6072: sub modify_autoupdate {
 6073:     my ($dom,%domconfig) = @_;
 6074:     my ($resulttext,%currautoupdate,%fields,%changes);
 6075:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6076:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6077:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6078:         }
 6079:     }
 6080:     my @offon = ('off','on');
 6081:     my %title = &Apache::lonlocal::texthash (
 6082:                    run => 'Auto-update:',
 6083:                    classlists => 'Updates to user information in classlists?'
 6084:                 );
 6085:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6086:     my %fieldtitles = &Apache::lonlocal::texthash (
 6087:                         id => 'Student/Employee ID',
 6088:                         permanentemail => 'E-mail address',
 6089:                         lastname => 'Last Name',
 6090:                         firstname => 'First Name',
 6091:                         middlename => 'Middle Name',
 6092:                         generation => 'Generation',
 6093:                       );
 6094:     $othertitle = &mt('All users');
 6095:     if (keys(%{$usertypes}) >  0) {
 6096:         $othertitle = &mt('Other users');
 6097:     }
 6098:     foreach my $key (keys(%env)) {
 6099:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6100:             my ($usertype,$item) = ($1,$2);
 6101:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6102:                 if ($usertype eq 'default') {   
 6103:                     push(@{$fields{$1}},$2);
 6104:                 } elsif (ref($types) eq 'ARRAY') {
 6105:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6106:                         push(@{$fields{$1}},$2);
 6107:                     }
 6108:                 }
 6109:             }
 6110:         }
 6111:     }
 6112:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6113:     @lockablenames = sort(@lockablenames);
 6114:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6115:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6116:         if (@changed) {
 6117:             $changes{'lockablenames'} = 1;
 6118:         }
 6119:     } else {
 6120:         if (@lockablenames) {
 6121:             $changes{'lockablenames'} = 1;
 6122:         }
 6123:     }
 6124:     my %updatehash = (
 6125:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6126:                                       classlists => $env{'form.classlists'},
 6127:                                       fields => {%fields},
 6128:                                       lockablenames => \@lockablenames,
 6129:                                     }
 6130:                      );
 6131:     foreach my $key (keys(%currautoupdate)) {
 6132:         if (($key eq 'run') || ($key eq 'classlists')) {
 6133:             if (exists($updatehash{autoupdate}{$key})) {
 6134:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6135:                     $changes{$key} = 1;
 6136:                 }
 6137:             }
 6138:         } elsif ($key eq 'fields') {
 6139:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6140:                 foreach my $item (@{$types},'default') {
 6141:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6142:                         my $change = 0;
 6143:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6144:                             if (!exists($fields{$item})) {
 6145:                                 $change = 1;
 6146:                                 last;
 6147:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6148:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6149:                                     $change = 1;
 6150:                                     last;
 6151:                                 }
 6152:                             }
 6153:                         }
 6154:                         if ($change) {
 6155:                             push(@{$changes{$key}},$item);
 6156:                         }
 6157:                     } 
 6158:                 }
 6159:             }
 6160:         } elsif ($key eq 'lockablenames') {
 6161:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6162:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6163:                 if (@changed) {
 6164:                     $changes{'lockablenames'} = 1;
 6165:                 }
 6166:             } else {
 6167:                 if (@lockablenames) {
 6168:                     $changes{'lockablenames'} = 1;
 6169:                 }
 6170:             }
 6171:         }
 6172:     }
 6173:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6174:         if (@lockablenames) {
 6175:             $changes{'lockablenames'} = 1;
 6176:         }
 6177:     }
 6178:     foreach my $item (@{$types},'default') {
 6179:         if (defined($fields{$item})) {
 6180:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6181:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6182:                     my $change = 0;
 6183:                     if (ref($fields{$item}) eq 'ARRAY') {
 6184:                         foreach my $type (@{$fields{$item}}) {
 6185:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6186:                                 $change = 1;
 6187:                                 last;
 6188:                             }
 6189:                         }
 6190:                     }
 6191:                     if ($change) {
 6192:                         push(@{$changes{'fields'}},$item);
 6193:                     }
 6194:                 } else {
 6195:                     push(@{$changes{'fields'}},$item);
 6196:                 }
 6197:             } else {
 6198:                 push(@{$changes{'fields'}},$item);
 6199:             }
 6200:         }
 6201:     }
 6202:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6203:                                              $dom);
 6204:     if ($putresult eq 'ok') {
 6205:         if (keys(%changes) > 0) {
 6206:             $resulttext = &mt('Changes made:').'<ul>';
 6207:             foreach my $key (sort(keys(%changes))) {
 6208:                 if ($key eq 'lockablenames') {
 6209:                     $resulttext .= '<li>';
 6210:                     if (@lockablenames) {
 6211:                         $usertypes->{'default'} = $othertitle;
 6212:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6213:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6214:                     } else {
 6215:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6216:                     }
 6217:                     $resulttext .= '</li>';
 6218:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6219:                     foreach my $item (@{$changes{$key}}) {
 6220:                         my @newvalues;
 6221:                         foreach my $type (@{$fields{$item}}) {
 6222:                             push(@newvalues,$fieldtitles{$type});
 6223:                         }
 6224:                         my $newvaluestr;
 6225:                         if (@newvalues > 0) {
 6226:                             $newvaluestr = join(', ',@newvalues);
 6227:                         } else {
 6228:                             $newvaluestr = &mt('none');
 6229:                         }
 6230:                         if ($item eq 'default') {
 6231:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6232:                         } else {
 6233:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6234:                         }
 6235:                     }
 6236:                 } else {
 6237:                     my $newvalue;
 6238:                     if ($key eq 'run') {
 6239:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6240:                     } else {
 6241:                         $newvalue = $offon[$env{'form.'.$key}];
 6242:                     }
 6243:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6244:                 }
 6245:             }
 6246:             $resulttext .= '</ul>';
 6247:         } else {
 6248:             $resulttext = &mt('No changes made to autoupdates');
 6249:         }
 6250:     } else {
 6251:         $resulttext = '<span class="LC_error">'.
 6252: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6253:     }
 6254:     return $resulttext;
 6255: }
 6256: 
 6257: sub modify_autocreate {
 6258:     my ($dom,%domconfig) = @_;
 6259:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6260:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6261:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6262:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6263:         }
 6264:     }
 6265:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6266:                  req => 'Auto-creation of validated requests for official courses',
 6267:                  xmldc => 'Identity of course creator of courses from XML files',
 6268:                );
 6269:     my @types = ('xml','req');
 6270:     foreach my $item (@types) {
 6271:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6272:         $newvals{$item} =~ s/\D//g;
 6273:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6274:     }
 6275:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6276:     my %domcoords = &get_active_dcs($dom);
 6277:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6278:         $newvals{'xmldc'} = '';
 6279:     } 
 6280:     %autocreatehash =  (
 6281:                         autocreate => { xml => $newvals{'xml'},
 6282:                                         req => $newvals{'req'},
 6283:                                       }
 6284:                        );
 6285:     if ($newvals{'xmldc'} ne '') {
 6286:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6287:     }
 6288:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6289:                                              $dom);
 6290:     if ($putresult eq 'ok') {
 6291:         my @items = @types;
 6292:         if ($newvals{'xml'}) {
 6293:             push(@items,'xmldc');
 6294:         }
 6295:         foreach my $item (@items) {
 6296:             if (exists($currautocreate{$item})) {
 6297:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6298:                     $changes{$item} = 1;
 6299:                 }
 6300:             } elsif ($newvals{$item}) {
 6301:                 $changes{$item} = 1;
 6302:             }
 6303:         }
 6304:         if (keys(%changes) > 0) {
 6305:             my @offon = ('off','on'); 
 6306:             $resulttext = &mt('Changes made:').'<ul>';
 6307:             foreach my $item (@types) {
 6308:                 if ($changes{$item}) {
 6309:                     my $newtxt = $offon[$newvals{$item}];
 6310:                     $resulttext .= '<li>'.&mt("$title{$item} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>';
 6311:                 }
 6312:             }
 6313:             if ($changes{'xmldc'}) {
 6314:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6315:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6316:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>'; 
 6317:             }
 6318:             $resulttext .= '</ul>';
 6319:         } else {
 6320:             $resulttext = &mt('No changes made to auto-creation settings');
 6321:         }
 6322:     } else {
 6323:         $resulttext = '<span class="LC_error">'.
 6324:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6325:     }
 6326:     return $resulttext;
 6327: }
 6328: 
 6329: sub modify_directorysrch {
 6330:     my ($dom,%domconfig) = @_;
 6331:     my ($resulttext,%changes);
 6332:     my %currdirsrch;
 6333:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6334:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6335:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6336:         }
 6337:     }
 6338:     my %title = ( available => 'Directory search available',
 6339:                   localonly => 'Other domains can search',
 6340:                   searchby => 'Search types',
 6341:                   searchtypes => 'Search latitude');
 6342:     my @offon = ('off','on');
 6343:     my @otherdoms = ('Yes','No');
 6344: 
 6345:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6346:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6347:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6348: 
 6349:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6350:     if (keys(%{$usertypes}) == 0) {
 6351:         @cansearch = ('default');
 6352:     } else {
 6353:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6354:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6355:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6356:                     push(@{$changes{'cansearch'}},$type);
 6357:                 }
 6358:             }
 6359:             foreach my $type (@cansearch) {
 6360:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6361:                     push(@{$changes{'cansearch'}},$type);
 6362:                 }
 6363:             }
 6364:         } else {
 6365:             push(@{$changes{'cansearch'}},@cansearch);
 6366:         }
 6367:     }
 6368: 
 6369:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6370:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6371:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6372:                 push(@{$changes{'searchby'}},$by);
 6373:             }
 6374:         }
 6375:         foreach my $by (@searchby) {
 6376:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6377:                 push(@{$changes{'searchby'}},$by);
 6378:             }
 6379:         }
 6380:     } else {
 6381:         push(@{$changes{'searchby'}},@searchby);
 6382:     }
 6383: 
 6384:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6385:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6386:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6387:                 push(@{$changes{'searchtypes'}},$type);
 6388:             }
 6389:         }
 6390:         foreach my $type (@searchtypes) {
 6391:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6392:                 push(@{$changes{'searchtypes'}},$type);
 6393:             }
 6394:         }
 6395:     } else {
 6396:         if (exists($currdirsrch{'searchtypes'})) {
 6397:             foreach my $type (@searchtypes) {  
 6398:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6399:                     push(@{$changes{'searchtypes'}},$type);
 6400:                 }
 6401:             }
 6402:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6403:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6404:             }   
 6405:         } else {
 6406:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6407:         }
 6408:     }
 6409: 
 6410:     my %dirsrch_hash =  (
 6411:             directorysrch => { available => $env{'form.dirsrch_available'},
 6412:                                cansearch => \@cansearch,
 6413:                                localonly => $env{'form.dirsrch_localonly'},
 6414:                                searchby => \@searchby,
 6415:                                searchtypes => \@searchtypes,
 6416:                              }
 6417:             );
 6418:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6419:                                              $dom);
 6420:     if ($putresult eq 'ok') {
 6421:         if (exists($currdirsrch{'available'})) {
 6422:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6423:                  $changes{'available'} = 1;
 6424:              }
 6425:         } else {
 6426:             if ($env{'form.dirsrch_available'} eq '1') {
 6427:                 $changes{'available'} = 1;
 6428:             }
 6429:         }
 6430:         if (exists($currdirsrch{'localonly'})) {
 6431:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6432:                  $changes{'localonly'} = 1;
 6433:              }
 6434:         } else {
 6435:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6436:                 $changes{'localonly'} = 1;
 6437:             }
 6438:         }
 6439:         if (keys(%changes) > 0) {
 6440:             $resulttext = &mt('Changes made:').'<ul>';
 6441:             if ($changes{'available'}) {
 6442:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6443:             }
 6444:             if ($changes{'localonly'}) {
 6445:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6446:             }
 6447: 
 6448:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6449:                 my $chgtext;
 6450:                 if (ref($usertypes) eq 'HASH') {
 6451:                     if (keys(%{$usertypes}) > 0) {
 6452:                         foreach my $type (@{$types}) {
 6453:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6454:                                 $chgtext .= $usertypes->{$type}.'; ';
 6455:                             }
 6456:                         }
 6457:                         if (grep(/^default$/,@cansearch)) {
 6458:                             $chgtext .= $othertitle;
 6459:                         } else {
 6460:                             $chgtext =~ s/\; $//;
 6461:                         }
 6462:                         $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>';
 6463:                     }
 6464:                 }
 6465:             }
 6466:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6467:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6468:                 my $chgtext;
 6469:                 foreach my $type (@{$titleorder}) {
 6470:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6471:                         if (defined($searchtitles->{$type})) {
 6472:                             $chgtext .= $searchtitles->{$type}.'; ';
 6473:                         }
 6474:                     }
 6475:                 }
 6476:                 $chgtext =~ s/\; $//;
 6477:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6478:             }
 6479:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6480:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6481:                 my $chgtext;
 6482:                 foreach my $type (@{$srchtypeorder}) {
 6483:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6484:                         if (defined($srchtypes_desc->{$type})) {
 6485:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6486:                         }
 6487:                     }
 6488:                 }
 6489:                 $chgtext =~ s/\; $//;
 6490:                 $resulttext .= '<li>'.&mt("$title{'searchtypes'} set to: \"[_1]\"",$chgtext).'</li>';
 6491:             }
 6492:             $resulttext .= '</ul>';
 6493:         } else {
 6494:             $resulttext = &mt('No changes made to institution directory search settings');
 6495:         }
 6496:     } else {
 6497:         $resulttext = '<span class="LC_error">'.
 6498:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6499:     }
 6500:     return $resulttext;
 6501: }
 6502: 
 6503: sub modify_contacts {
 6504:     my ($dom,%domconfig) = @_;
 6505:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6506:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6507:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6508:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6509:         }
 6510:     }
 6511:     my (%others,%to,%bcc);
 6512:     my @contacts = ('supportemail','adminemail');
 6513:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6514:                     'requestsmail');
 6515:     foreach my $type (@mailings) {
 6516:         @{$newsetting{$type}} = 
 6517:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6518:         foreach my $item (@contacts) {
 6519:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6520:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6521:             } else {
 6522:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6523:             }
 6524:         }  
 6525:         $others{$type} = $env{'form.'.$type.'_others'};
 6526:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6527:         if ($type eq 'helpdeskmail') {
 6528:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6529:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6530:         }
 6531:     }
 6532:     foreach my $item (@contacts) {
 6533:         $to{$item} = $env{'form.'.$item};
 6534:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6535:     }
 6536:     if (keys(%currsetting) > 0) {
 6537:         foreach my $item (@contacts) {
 6538:             if ($to{$item} ne $currsetting{$item}) {
 6539:                 $changes{$item} = 1;
 6540:             }
 6541:         }
 6542:         foreach my $type (@mailings) {
 6543:             foreach my $item (@contacts) {
 6544:                 if (ref($currsetting{$type}) eq 'HASH') {
 6545:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6546:                         push(@{$changes{$type}},$item);
 6547:                     }
 6548:                 } else {
 6549:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6550:                 }
 6551:             }
 6552:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6553:                 push(@{$changes{$type}},'others');
 6554:             }
 6555:             if ($type eq 'helpdeskmail') {   
 6556:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6557:                     push(@{$changes{$type}},'bcc'); 
 6558:                 }
 6559:             }
 6560:         }
 6561:     } else {
 6562:         my %default;
 6563:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6564:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6565:         $default{'errormail'} = 'adminemail';
 6566:         $default{'packagesmail'} = 'adminemail';
 6567:         $default{'helpdeskmail'} = 'supportemail';
 6568:         $default{'lonstatusmail'} = 'adminemail';
 6569:         $default{'requestsmail'} = 'adminemail';
 6570:         foreach my $item (@contacts) {
 6571:            if ($to{$item} ne $default{$item}) {
 6572:               $changes{$item} = 1;
 6573:            } 
 6574:         }
 6575:         foreach my $type (@mailings) {
 6576:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6577:                
 6578:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6579:             }
 6580:             if ($others{$type} ne '') {
 6581:                 push(@{$changes{$type}},'others');
 6582:             }
 6583:             if ($type eq 'helpdeskmail') {
 6584:                 if ($bcc{$type} ne '') {
 6585:                     push(@{$changes{$type}},'bcc');
 6586:                 }
 6587:             }
 6588:         }
 6589:     }
 6590:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6591:                                              $dom);
 6592:     if ($putresult eq 'ok') {
 6593:         if (keys(%changes) > 0) {
 6594:             my ($titles,$short_titles)  = &contact_titles();
 6595:             $resulttext = &mt('Changes made:').'<ul>';
 6596:             foreach my $item (@contacts) {
 6597:                 if ($changes{$item}) {
 6598:                     $resulttext .= '<li>'.$titles->{$item}.
 6599:                                     &mt(' set to: ').
 6600:                                     '<span class="LC_cusr_emph">'.
 6601:                                     $to{$item}.'</span></li>';
 6602:                 }
 6603:             }
 6604:             foreach my $type (@mailings) {
 6605:                 if (ref($changes{$type}) eq 'ARRAY') {
 6606:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6607:                     my @text;
 6608:                     foreach my $item (@{$newsetting{$type}}) {
 6609:                         push(@text,$short_titles->{$item});
 6610:                     }
 6611:                     if ($others{$type} ne '') {
 6612:                         push(@text,$others{$type});
 6613:                     }
 6614:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6615:                                    join(', ',@text).'</span>';
 6616:                     if ($type eq 'helpdeskmail') {
 6617:                         if ($bcc{$type} ne '') {
 6618:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6619:                         }
 6620:                     }
 6621:                     $resulttext .= '</li>';
 6622:                 }
 6623:             }
 6624:             $resulttext .= '</ul>';
 6625:         } else {
 6626:             $resulttext = &mt('No changes made to contact information');
 6627:         }
 6628:     } else {
 6629:         $resulttext = '<span class="LC_error">'.
 6630:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6631:     }
 6632:     return $resulttext;
 6633: }
 6634: 
 6635: sub modify_usercreation {
 6636:     my ($dom,%domconfig) = @_;
 6637:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6638:     my $warningmsg;
 6639:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6640:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6641:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6642:         }
 6643:     }
 6644:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6645:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6646:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6647:     my @contexts = ('author','course','requestcrs','selfcreate');
 6648:     foreach my $item(@contexts) {
 6649:         if ($item eq 'selfcreate') {
 6650:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6651:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6652:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6653:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6654:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6655:                         $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.');   
 6656:                     }
 6657:                 }
 6658:             }
 6659:         } else {
 6660:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6661:         }
 6662:     }
 6663:     my ($othertitle,$usertypes,$types) = 
 6664:         &Apache::loncommon::sorted_inst_types($dom);
 6665:     if (ref($types) eq 'ARRAY') {
 6666:         if (@{$types} > 0) {
 6667:             @{$cancreate{'statustocreate'}} = 
 6668:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6669:         } else {
 6670:             @{$cancreate{'statustocreate'}} = ();
 6671:         }
 6672:         push(@contexts,'statustocreate');
 6673:     }
 6674:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6675:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6676:         foreach my $item (@contexts) {
 6677:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6678:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6679:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6680:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6681:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6682:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6683:                                     push(@{$changes{'cancreate'}},$item);
 6684:                                 }
 6685:                             }
 6686:                         }
 6687:                     }
 6688:                 } else {
 6689:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6690:                         if (@{$cancreate{$item}} > 0) {
 6691:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6692:                                 push(@{$changes{'cancreate'}},$item);
 6693:                             }
 6694:                         }
 6695:                     } else {
 6696:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6697:                             if (@{$cancreate{$item}} < 3) {
 6698:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6699:                                     push(@{$changes{'cancreate'}},$item);
 6700:                                 }
 6701:                             }
 6702:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6703:                             if (@{$cancreate{$item}} > 0) {
 6704:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6705:                                     push(@{$changes{'cancreate'}},$item);
 6706:                                 }
 6707:                             }
 6708:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6709:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6710:                                 push(@{$changes{'cancreate'}},$item);
 6711:                             }
 6712:                         }
 6713:                     }
 6714:                 }
 6715:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6716:                     foreach my $type (@{$cancreate{$item}}) {
 6717:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6718:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6719:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6720:                                     push(@{$changes{'cancreate'}},$item);
 6721:                                 }
 6722:                             }
 6723:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6724:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6725:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6726:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6727:                                     push(@{$changes{'cancreate'}},$item);
 6728:                                 }
 6729:                             }
 6730:                         }
 6731:                     }
 6732:                 }
 6733:             } else {
 6734:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6735:                     push(@{$changes{'cancreate'}},$item);
 6736:                 }
 6737:             }
 6738:         }
 6739:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6740:         foreach my $item (@contexts) {
 6741:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6742:                 if ($cancreate{$item} ne 'any') {
 6743:                     push(@{$changes{'cancreate'}},$item);
 6744:                 }
 6745:             } else {
 6746:                 if ($cancreate{$item} ne 'none') {
 6747:                     push(@{$changes{'cancreate'}},$item);
 6748:                 }
 6749:             }
 6750:         }
 6751:     } else {
 6752:         foreach my $item (@contexts)  {
 6753:             push(@{$changes{'cancreate'}},$item);
 6754:         }
 6755:     }
 6756: 
 6757:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6758:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6759:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6760:                 push(@{$changes{'username_rule'}},$type);
 6761:             }
 6762:         }
 6763:         foreach my $type (@username_rule) {
 6764:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6765:                 push(@{$changes{'username_rule'}},$type);
 6766:             }
 6767:         }
 6768:     } else {
 6769:         push(@{$changes{'username_rule'}},@username_rule);
 6770:     }
 6771: 
 6772:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6773:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 6774:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 6775:                 push(@{$changes{'id_rule'}},$type);
 6776:             }
 6777:         }
 6778:         foreach my $type (@id_rule) {
 6779:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 6780:                 push(@{$changes{'id_rule'}},$type);
 6781:             }
 6782:         }
 6783:     } else {
 6784:         push(@{$changes{'id_rule'}},@id_rule);
 6785:     }
 6786: 
 6787:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 6788:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 6789:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 6790:                 push(@{$changes{'email_rule'}},$type);
 6791:             }
 6792:         }
 6793:         foreach my $type (@email_rule) {
 6794:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 6795:                 push(@{$changes{'email_rule'}},$type);
 6796:             }
 6797:         }
 6798:     } else {
 6799:         push(@{$changes{'email_rule'}},@email_rule);
 6800:     }
 6801: 
 6802:     my @authen_contexts = ('author','course','domain');
 6803:     my @authtypes = ('int','krb4','krb5','loc');
 6804:     my %authhash;
 6805:     foreach my $item (@authen_contexts) {
 6806:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 6807:         foreach my $auth (@authtypes) {
 6808:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 6809:                 $authhash{$item}{$auth} = 1;
 6810:             } else {
 6811:                 $authhash{$item}{$auth} = 0;
 6812:             }
 6813:         }
 6814:     }
 6815:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 6816:         foreach my $item (@authen_contexts) {
 6817:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 6818:                 foreach my $auth (@authtypes) {
 6819:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 6820:                         push(@{$changes{'authtypes'}},$item);
 6821:                         last;
 6822:                     }
 6823:                 }
 6824:             }
 6825:         }
 6826:     } else {
 6827:         foreach my $item (@authen_contexts) {
 6828:             push(@{$changes{'authtypes'}},$item);
 6829:         }
 6830:     }
 6831: 
 6832:     my %usercreation_hash =  (
 6833:             usercreation => {
 6834:                               cancreate     => \%cancreate,
 6835:                               username_rule => \@username_rule,
 6836:                               id_rule       => \@id_rule,
 6837:                               email_rule    => \@email_rule,
 6838:                               authtypes     => \%authhash,
 6839:                             }
 6840:             );
 6841: 
 6842:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 6843:                                              $dom);
 6844: 
 6845:     my %selfcreatetypes = (
 6846:                              sso   => 'users authenticated by institutional single sign on',
 6847:                              login => 'users authenticated by institutional log-in',
 6848:                              email => 'users who provide a valid e-mail address for use as the username',
 6849:                           );
 6850:     if ($putresult eq 'ok') {
 6851:         if (keys(%changes) > 0) {
 6852:             $resulttext = &mt('Changes made:').'<ul>';
 6853:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 6854:                 my %lt = &usercreation_types();
 6855:                 foreach my $type (@{$changes{'cancreate'}}) {
 6856:                     my $chgtext;
 6857:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 6858:                         $chgtext = $lt{$type}.', ';
 6859:                     }
 6860:                     if ($type eq 'selfcreate') {
 6861:                         if (@{$cancreate{$type}} == 0) {
 6862:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 6863:                         } else {
 6864:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 6865:                             foreach my $case (@{$cancreate{$type}}) {
 6866:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 6867:                             }
 6868:                             $chgtext .= '</ul>';
 6869:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 6870:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 6871:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 6872:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 6873:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6874:                                         }
 6875:                                     }
 6876:                                 }
 6877:                             }
 6878:                         }
 6879:                     } elsif ($type eq 'statustocreate') {
 6880:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 6881:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 6882:                             if (@{$cancreate{'selfcreate'}} > 0) {
 6883:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 6884: 
 6885:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 6886:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 6887:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6888:                                     } 
 6889:                                 } elsif (ref($usertypes) eq 'HASH') {
 6890:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6891:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 6892:                                     } else {
 6893:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 6894:                                     }
 6895:                                     $chgtext .= '<ul>';
 6896:                                     foreach my $case (@{$cancreate{$type}}) {
 6897:                                         if ($case eq 'default') {
 6898:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 6899:                                         } else {
 6900:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 6901:                                         }
 6902:                                     }
 6903:                                     $chgtext .= '</ul>';
 6904:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6905:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 6906:                                     }
 6907:                                 }
 6908:                             } else {
 6909:                                 if (@{$cancreate{$type}} == 0) {
 6910:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 6911:                                 } else {
 6912:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 6913:                                 }
 6914:                             }
 6915:                         }
 6916:                     } elsif ($type eq 'captcha') {
 6917:                         if ($cancreate{$type} eq 'notused') {
 6918:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 6919:                         } else {
 6920:                             my %captchas = &captcha_phrases();
 6921:                             if ($captchas{$cancreate{$type}}) {
 6922:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 6923:                             } else {
 6924:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.'); 
 6925:                             }
 6926:                         }
 6927:                     } elsif ($type eq 'recaptchakeys') {
 6928:                         my ($privkey,$pubkey);
 6929:                         if (ref($cancreate{$type}) eq 'HASH') {
 6930:                             $pubkey = $cancreate{$type}{'public'};
 6931:                             $privkey = $cancreate{$type}{'private'};
 6932:                         }
 6933:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 6934:                         if (!$pubkey) {
 6935:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 6936:                         } else {
 6937:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6938:                         }
 6939:                         if (!$privkey) {
 6940:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 6941:                         } else {
 6942:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6943:                         }
 6944:                         $chgtext .= '</ul>';
 6945:                     } else {
 6946:                         if ($cancreate{$type} eq 'none') {
 6947:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 6948:                         } elsif ($cancreate{$type} eq 'any') {
 6949:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 6950:                         } elsif ($cancreate{$type} eq 'official') {
 6951:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 6952:                         } elsif ($cancreate{$type} eq 'unofficial') {
 6953:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 6954:                         }
 6955:                     }
 6956:                     $resulttext .= '<li>'.$chgtext.'</li>';
 6957:                 }
 6958:             }
 6959:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 6960:                 my ($rules,$ruleorder) = 
 6961:                     &Apache::lonnet::inst_userrules($dom,'username');
 6962:                 my $chgtext = '<ul>';
 6963:                 foreach my $type (@username_rule) {
 6964:                     if (ref($rules->{$type}) eq 'HASH') {
 6965:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 6966:                     }
 6967:                 }
 6968:                 $chgtext .= '</ul>';
 6969:                 if (@username_rule > 0) {
 6970:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 6971:                 } else {
 6972:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 6973:                 }
 6974:             }
 6975:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 6976:                 my ($idrules,$idruleorder) = 
 6977:                     &Apache::lonnet::inst_userrules($dom,'id');
 6978:                 my $chgtext = '<ul>';
 6979:                 foreach my $type (@id_rule) {
 6980:                     if (ref($idrules->{$type}) eq 'HASH') {
 6981:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 6982:                     }
 6983:                 }
 6984:                 $chgtext .= '</ul>';
 6985:                 if (@id_rule > 0) {
 6986:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 6987:                 } else {
 6988:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 6989:                 }
 6990:             }
 6991:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 6992:                 my ($emailrules,$emailruleorder) =
 6993:                     &Apache::lonnet::inst_userrules($dom,'email');
 6994:                 my $chgtext = '<ul>';
 6995:                 foreach my $type (@email_rule) {
 6996:                     if (ref($emailrules->{$type}) eq 'HASH') {
 6997:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 6998:                     }
 6999:                 }
 7000:                 $chgtext .= '</ul>';
 7001:                 if (@email_rule > 0) {
 7002:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 7003:                 } else {
 7004:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 7005:                 }
 7006:             }
 7007: 
 7008:             my %authname = &authtype_names();
 7009:             my %context_title = &context_names();
 7010:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7011:                 my $chgtext = '<ul>';
 7012:                 foreach my $type (@{$changes{'authtypes'}}) {
 7013:                     my @allowed;
 7014:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7015:                     foreach my $auth (@authtypes) {
 7016:                         if ($authhash{$type}{$auth}) {
 7017:                             push(@allowed,$authname{$auth});
 7018:                         }
 7019:                     }
 7020:                     if (@allowed > 0) {
 7021:                         $chgtext .= join(', ',@allowed).'</li>';
 7022:                     } else {
 7023:                         $chgtext .= &mt('none').'</li>';
 7024:                     }
 7025:                 }
 7026:                 $chgtext .= '</ul>';
 7027:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7028:                 $resulttext .= '</li>';
 7029:             }
 7030:             $resulttext .= '</ul>';
 7031:         } else {
 7032:             $resulttext = &mt('No changes made to user creation settings');
 7033:         }
 7034:     } else {
 7035:         $resulttext = '<span class="LC_error">'.
 7036:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7037:     }
 7038:     if ($warningmsg ne '') {
 7039:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7040:     }
 7041:     return $resulttext;
 7042: }
 7043: 
 7044: sub process_captcha {
 7045:     my ($container,$changes,$newsettings,$current) = @_;
 7046:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 7047:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 7048:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 7049:         $newsettings->{'captcha'} = 'original';
 7050:     }
 7051:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 7052:         if ($container eq 'cancreate') { 
 7053:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7054:                 push(@{$changes->{'cancreate'}},'captcha');
 7055:             } elsif (!defined($changes->{'cancreate'})) {
 7056:                 $changes->{'cancreate'} = ['captcha'];
 7057:             }
 7058:         } else {
 7059:             $changes->{'captcha'} = 1;
 7060:         }
 7061:     }
 7062:     my ($newpub,$newpriv,$currpub,$currpriv);
 7063:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 7064:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 7065:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 7066:         $newpub =~ s/\W//g;
 7067:         $newpriv =~ s/\W//g;
 7068:         $newsettings->{'recaptchakeys'} = {
 7069:                                              public  => $newpub,
 7070:                                              private => $newpriv,
 7071:                                           };
 7072:     }
 7073:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 7074:         $currpub = $current->{'recaptchakeys'}{'public'};
 7075:         $currpriv = $current->{'recaptchakeys'}{'private'};
 7076:         $newsettings->{'recaptchakeys'} = {
 7077:                                              public  => '',
 7078:                                              private => '',
 7079:                                           }
 7080:     }
 7081:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 7082:         if ($container eq 'cancreate') {
 7083:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7084:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 7085:             } elsif (!defined($changes->{'cancreate'})) {
 7086:                 $changes->{'cancreate'} = ['recaptchakeys'];
 7087:             }
 7088:         } else {
 7089:             $changes->{'recaptchakeys'} = 1; 
 7090:         }
 7091:     }
 7092:     return;
 7093: }
 7094: 
 7095: sub modify_usermodification {
 7096:     my ($dom,%domconfig) = @_;
 7097:     my ($resulttext,%curr_usermodification,%changes);
 7098:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7099:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7100:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 7101:         }
 7102:     }
 7103:     my @contexts = ('author','course','selfcreate');
 7104:     my %context_title = (
 7105:                            author => 'In author context',
 7106:                            course => 'In course context',
 7107:                            selfcreate => 'When self creating account', 
 7108:                         );
 7109:     my @fields = ('lastname','firstname','middlename','generation',
 7110:                   'permanentemail','id');
 7111:     my %roles = (
 7112:                   author => ['ca','aa'],
 7113:                   course => ['st','ep','ta','in','cr'],
 7114:                 );
 7115:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7116:     if (ref($types) eq 'ARRAY') {
 7117:         push(@{$types},'default');
 7118:         $usertypes->{'default'} = $othertitle;
 7119:     }
 7120:     $roles{'selfcreate'} = $types;  
 7121:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7122:     my %modifyhash;
 7123:     foreach my $context (@contexts) {
 7124:         foreach my $role (@{$roles{$context}}) {
 7125:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7126:             foreach my $item (@fields) {
 7127:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7128:                     $modifyhash{$context}{$role}{$item} = 1;
 7129:                 } else {
 7130:                     $modifyhash{$context}{$role}{$item} = 0;
 7131:                 }
 7132:             }
 7133:         }
 7134:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7135:             foreach my $role (@{$roles{$context}}) {
 7136:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7137:                     foreach my $field (@fields) {
 7138:                         if ($modifyhash{$context}{$role}{$field} ne 
 7139:                                 $curr_usermodification{$context}{$role}{$field}) {
 7140:                             push(@{$changes{$context}},$role);
 7141:                             last;
 7142:                         }
 7143:                     }
 7144:                 }
 7145:             }
 7146:         } else {
 7147:             foreach my $context (@contexts) {
 7148:                 foreach my $role (@{$roles{$context}}) {
 7149:                     push(@{$changes{$context}},$role);
 7150:                 }
 7151:             }
 7152:         }
 7153:     }
 7154:     my %usermodification_hash =  (
 7155:                                    usermodification => \%modifyhash,
 7156:                                  );
 7157:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7158:                                              \%usermodification_hash,$dom);
 7159:     if ($putresult eq 'ok') {
 7160:         if (keys(%changes) > 0) {
 7161:             $resulttext = &mt('Changes made: ').'<ul>';
 7162:             foreach my $context (@contexts) {
 7163:                 if (ref($changes{$context}) eq 'ARRAY') {
 7164:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7165:                     if (ref($changes{$context}) eq 'ARRAY') {
 7166:                         foreach my $role (@{$changes{$context}}) {
 7167:                             my $rolename;
 7168:                             if ($context eq 'selfcreate') {
 7169:                                 $rolename = $role;
 7170:                                 if (ref($usertypes) eq 'HASH') {
 7171:                                     if ($usertypes->{$role} ne '') {
 7172:                                         $rolename = $usertypes->{$role};
 7173:                                     }
 7174:                                 }
 7175:                             } else {
 7176:                                 if ($role eq 'cr') {
 7177:                                     $rolename = &mt('Custom');
 7178:                                 } else {
 7179:                                     $rolename = &Apache::lonnet::plaintext($role);
 7180:                                 }
 7181:                             }
 7182:                             my @modifiable;
 7183:                             if ($context eq 'selfcreate') {
 7184:                                 $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): ');
 7185:                             } else {
 7186:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7187:                             }
 7188:                             foreach my $field (@fields) {
 7189:                                 if ($modifyhash{$context}{$role}{$field}) {
 7190:                                     push(@modifiable,$fieldtitles{$field});
 7191:                                 }
 7192:                             }
 7193:                             if (@modifiable > 0) {
 7194:                                 $resulttext .= join(', ',@modifiable);
 7195:                             } else {
 7196:                                 $resulttext .= &mt('none'); 
 7197:                             }
 7198:                             $resulttext .= '</li>';
 7199:                         }
 7200:                         $resulttext .= '</ul></li>';
 7201:                     }
 7202:                 }
 7203:             }
 7204:             $resulttext .= '</ul>';
 7205:         } else {
 7206:             $resulttext = &mt('No changes made to user modification settings');
 7207:         }
 7208:     } else {
 7209:         $resulttext = '<span class="LC_error">'.
 7210:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7211:     }
 7212:     return $resulttext;
 7213: }
 7214: 
 7215: sub modify_defaults {
 7216:     my ($dom,$r) = @_;
 7217:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7218:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7219:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7220:     my @authtypes = ('internal','krb4','krb5','localauth');
 7221:     foreach my $item (@items) {
 7222:         $newvalues{$item} = $env{'form.'.$item};
 7223:         if ($item eq 'auth_def') {
 7224:             if ($newvalues{$item} ne '') {
 7225:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7226:                     push(@errors,$item);
 7227:                 }
 7228:             }
 7229:         } elsif ($item eq 'lang_def') {
 7230:             if ($newvalues{$item} ne '') {
 7231:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7232:                     my $langcode = $1;
 7233:                     if ($langcode ne 'x_chef') {
 7234:                         if (code2language($langcode) eq '') {
 7235:                             push(@errors,$item);
 7236:                         }
 7237:                     }
 7238:                 } else {
 7239:                     push(@errors,$item);
 7240:                 }
 7241:             }
 7242:         } elsif ($item eq 'timezone_def') {
 7243:             if ($newvalues{$item} ne '') {
 7244:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7245:                     push(@errors,$item);   
 7246:                 }
 7247:             }
 7248:         } elsif ($item eq 'datelocale_def') {
 7249:             if ($newvalues{$item} ne '') {
 7250:                 my @datelocale_ids = DateTime::Locale->ids();
 7251:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7252:                     push(@errors,$item);
 7253:                 }
 7254:             }
 7255:         } elsif ($item eq 'portal_def') {
 7256:             if ($newvalues{$item} ne '') {
 7257:                 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])\/?$/) {
 7258:                     push(@errors,$item);
 7259:                 }
 7260:             }
 7261:         }
 7262:         if (grep(/^\Q$item\E$/,@errors)) {
 7263:             $newvalues{$item} = $domdefaults{$item};
 7264:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7265:             $changes{$item} = 1;
 7266:         }
 7267:         $domdefaults{$item} = $newvalues{$item};
 7268:     }
 7269:     my %defaults_hash = (
 7270:                          defaults => \%newvalues,
 7271:                         );
 7272:     my $title = &defaults_titles();
 7273:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7274:                                              $dom);
 7275:     if ($putresult eq 'ok') {
 7276:         if (keys(%changes) > 0) {
 7277:             $resulttext = &mt('Changes made:').'<ul>';
 7278:             my $version = $r->dir_config('lonVersion');
 7279:             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";
 7280:             foreach my $item (sort(keys(%changes))) {
 7281:                 my $value = $env{'form.'.$item};
 7282:                 if ($value eq '') {
 7283:                     $value = &mt('none');
 7284:                 } elsif ($item eq 'auth_def') {
 7285:                     my %authnames = &authtype_names();
 7286:                     my %shortauth = (
 7287:                              internal => 'int',
 7288:                              krb4 => 'krb4',
 7289:                              krb5 => 'krb5',
 7290:                              localauth  => 'loc',
 7291:                     );
 7292:                     $value = $authnames{$shortauth{$value}};
 7293:                 }
 7294:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7295:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7296:             }
 7297:             $resulttext .= '</ul>';
 7298:             $mailmsgtext .= "\n";
 7299:             my $cachetime = 24*60*60;
 7300:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7301:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7302:                 my $sysmail = $r->dir_config('lonSysEMail');
 7303:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 7304:             }
 7305:         } else {
 7306:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 7307:         }
 7308:     } else {
 7309:         $resulttext = '<span class="LC_error">'.
 7310:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7311:     }
 7312:     if (@errors > 0) {
 7313:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7314:         foreach my $item (@errors) {
 7315:             $resulttext .= ' "'.$title->{$item}.'",';
 7316:         }
 7317:         $resulttext =~ s/,$//;
 7318:     }
 7319:     return $resulttext;
 7320: }
 7321: 
 7322: sub modify_scantron {
 7323:     my ($r,$dom,$confname,%domconfig) = @_;
 7324:     my ($resulttext,%confhash,%changes,$errors);
 7325:     my $custom = 'custom.tab';
 7326:     my $default = 'default.tab';
 7327:     my $servadm = $r->dir_config('lonAdmEMail');
 7328:     my ($configuserok,$author_ok,$switchserver) = 
 7329:         &config_check($dom,$confname,$servadm);
 7330:     if ($env{'form.scantronformat.filename'} ne '') {
 7331:         my $error;
 7332:         if ($configuserok eq 'ok') {
 7333:             if ($switchserver) {
 7334:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7335:             } else {
 7336:                 if ($author_ok eq 'ok') {
 7337:                     my ($result,$scantronurl) =
 7338:                         &publishlogo($r,'upload','scantronformat',$dom,
 7339:                                      $confname,'scantron','','',$custom);
 7340:                     if ($result eq 'ok') {
 7341:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7342:                         $changes{'scantronformat'} = 1;
 7343:                     } else {
 7344:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7345:                     }
 7346:                 } else {
 7347:                     $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);
 7348:                 }
 7349:             }
 7350:         } else {
 7351:             $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);
 7352:         }
 7353:         if ($error) {
 7354:             &Apache::lonnet::logthis($error);
 7355:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7356:         }
 7357:     }
 7358:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7359:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7360:             if ($env{'form.scantronformat_del'}) {
 7361:                 $confhash{'scantron'}{'scantronformat'} = '';
 7362:                 $changes{'scantronformat'} = 1;
 7363:             }
 7364:         }
 7365:     }
 7366:     if (keys(%confhash) > 0) {
 7367:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7368:                                                  $dom);
 7369:         if ($putresult eq 'ok') {
 7370:             if (keys(%changes) > 0) {
 7371:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7372:                     $resulttext = &mt('Changes made:').'<ul>';
 7373:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7374:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7375:                     } else {
 7376:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7377:                     }
 7378:                     $resulttext .= '</ul>';
 7379:                 } else {
 7380:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7381:                 }
 7382:                 $resulttext .= '</ul>';
 7383:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7384:             } else {
 7385:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7386:             }
 7387:         } else {
 7388:             $resulttext = '<span class="LC_error">'.
 7389:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7390:         }
 7391:     } else {
 7392:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7393:     }
 7394:     if ($errors) {
 7395:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7396:                        $errors.'</ul>';
 7397:     }
 7398:     return $resulttext;
 7399: }
 7400: 
 7401: sub modify_coursecategories {
 7402:     my ($dom,%domconfig) = @_;
 7403:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7404:         $cathash);
 7405:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7406:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7407:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7408:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7409:             $changes{'togglecats'} = 1;
 7410:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7411:         }
 7412:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7413:             $changes{'categorize'} = 1;
 7414:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7415:         }
 7416:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7417:             $changes{'togglecatscomm'} = 1;
 7418:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7419:         }
 7420:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7421:             $changes{'categorizecomm'} = 1;
 7422:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7423:         }
 7424:     } else {
 7425:         $changes{'togglecats'} = 1;
 7426:         $changes{'categorize'} = 1;
 7427:         $changes{'togglecatscomm'} = 1;
 7428:         $changes{'categorizecomm'} = 1;
 7429:         $domconfig{'coursecategories'} = {
 7430:                                              togglecats => $env{'form.togglecats'},
 7431:                                              categorize => $env{'form.categorize'},
 7432:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7433:                                              categorizecomm => $env{'form.categorizecomm'},
 7434:                                          };
 7435:     }
 7436:     if (ref($cathash) eq 'HASH') {
 7437:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7438:             push (@deletecategory,'instcode::0');
 7439:         }
 7440:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7441:             push(@deletecategory,'communities::0');
 7442:         }
 7443:     }
 7444:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7445:     if (ref($cathash) eq 'HASH') {
 7446:         if (@deletecategory > 0) {
 7447:             #FIXME Need to remove category from all courses using a deleted category 
 7448:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7449:             foreach my $item (@deletecategory) {
 7450:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7451:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7452:                     $deletions{$item} = 1;
 7453:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7454:                 }
 7455:             }
 7456:         }
 7457:         foreach my $item (keys(%{$cathash})) {
 7458:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7459:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7460:                 $reorderings{$item} = 1;
 7461:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7462:             }
 7463:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7464:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7465:                 my $newdepth = $depth+1;
 7466:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7467:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7468:                 $adds{$newitem} = 1; 
 7469:             }
 7470:             if ($env{'form.subcat_'.$item} ne '') {
 7471:                 my $newcat = $env{'form.subcat_'.$item};
 7472:                 my $newdepth = $depth+1;
 7473:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7474:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7475:                 $adds{$newitem} = 1;
 7476:             }
 7477:         }
 7478:     }
 7479:     if ($env{'form.instcode'} eq '1') {
 7480:         if (ref($cathash) eq 'HASH') {
 7481:             my $newitem = 'instcode::0';
 7482:             if ($cathash->{$newitem} eq '') {  
 7483:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7484:                 $adds{$newitem} = 1;
 7485:             }
 7486:         } else {
 7487:             my $newitem = 'instcode::0';
 7488:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7489:             $adds{$newitem} = 1;
 7490:         }
 7491:     }
 7492:     if ($env{'form.communities'} eq '1') {
 7493:         if (ref($cathash) eq 'HASH') {
 7494:             my $newitem = 'communities::0';
 7495:             if ($cathash->{$newitem} eq '') {
 7496:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7497:                 $adds{$newitem} = 1;
 7498:             }
 7499:         } else {
 7500:             my $newitem = 'communities::0';
 7501:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7502:             $adds{$newitem} = 1;
 7503:         }
 7504:     }
 7505:     if ($env{'form.addcategory_name'} ne '') {
 7506:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7507:             ($env{'form.addcategory_name'} ne 'communities')) {
 7508:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7509:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7510:             $adds{$newitem} = 1;
 7511:         }
 7512:     }
 7513:     my $putresult;
 7514:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7515:         if (keys(%deletions) > 0) {
 7516:             foreach my $key (keys(%deletions)) {
 7517:                 if ($predelallitems{$key} ne '') {
 7518:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7519:                 }
 7520:             }
 7521:         }
 7522:         my (@chkcats,@chktrails,%chkallitems);
 7523:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7524:         if (ref($chkcats[0]) eq 'ARRAY') {
 7525:             my $depth = 0;
 7526:             my $chg = 0;
 7527:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7528:                 my $name = $chkcats[0][$i];
 7529:                 my $item;
 7530:                 if ($name eq '') {
 7531:                     $chg ++;
 7532:                 } else {
 7533:                     $item = &escape($name).'::0';
 7534:                     if ($chg) {
 7535:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7536:                     }
 7537:                     $depth ++; 
 7538:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7539:                     $depth --;
 7540:                 }
 7541:             }
 7542:         }
 7543:     }
 7544:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7545:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7546:         if ($putresult eq 'ok') {
 7547:             my %title = (
 7548:                          togglecats     => 'Show/Hide a course in catalog',
 7549:                          categorize     => 'Assign a category to a course',
 7550:                          togglecatscomm => 'Show/Hide a community in catalog',
 7551:                          categorizecomm => 'Assign a category to a community',
 7552:                         );
 7553:             my %level = (
 7554:                          dom  => 'set in Domain ("Modify Course/Community")',
 7555:                          crs  => 'set in Course ("Course Configuration")',
 7556:                          comm => 'set in Community ("Community Configuration")',
 7557:                         );
 7558:             $resulttext = &mt('Changes made:').'<ul>';
 7559:             if ($changes{'togglecats'}) {
 7560:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7561:             }
 7562:             if ($changes{'categorize'}) {
 7563:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7564:             }
 7565:             if ($changes{'togglecatscomm'}) {
 7566:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7567:             }
 7568:             if ($changes{'categorizecomm'}) {
 7569:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7570:             }
 7571:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7572:                 my $cathash;
 7573:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7574:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7575:                 } else {
 7576:                     $cathash = {};
 7577:                 } 
 7578:                 my (@cats,@trails,%allitems);
 7579:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7580:                 if (keys(%deletions) > 0) {
 7581:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7582:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7583:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7584:                     }
 7585:                     $resulttext .= '</ul></li>';
 7586:                 }
 7587:                 if (keys(%reorderings) > 0) {
 7588:                     my %sort_by_trail;
 7589:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7590:                     foreach my $key (keys(%reorderings)) {
 7591:                         if ($allitems{$key} ne '') {
 7592:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7593:                         }
 7594:                     }
 7595:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7596:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7597:                     }
 7598:                     $resulttext .= '</ul></li>';
 7599:                 }
 7600:                 if (keys(%adds) > 0) {
 7601:                     my %sort_by_trail;
 7602:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7603:                     foreach my $key (keys(%adds)) {
 7604:                         if ($allitems{$key} ne '') {
 7605:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7606:                         }
 7607:                     }
 7608:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7609:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7610:                     }
 7611:                     $resulttext .= '</ul></li>';
 7612:                 }
 7613:             }
 7614:             $resulttext .= '</ul>';
 7615:         } else {
 7616:             $resulttext = '<span class="LC_error">'.
 7617:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7618:         }
 7619:     } else {
 7620:         $resulttext = &mt('No changes made to course and community categories');
 7621:     }
 7622:     return $resulttext;
 7623: }
 7624: 
 7625: sub modify_serverstatuses {
 7626:     my ($dom,%domconfig) = @_;
 7627:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7628:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7629:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7630:     }
 7631:     my @pages = &serverstatus_pages();
 7632:     foreach my $type (@pages) {
 7633:         $newserverstatus{$type}{'namedusers'} = '';
 7634:         $newserverstatus{$type}{'machines'} = '';
 7635:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7636:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7637:             my @okusers;
 7638:             foreach my $user (@users) {
 7639:                 my ($uname,$udom) = split(/:/,$user);
 7640:                 if (($udom =~ /^$match_domain$/) &&   
 7641:                     (&Apache::lonnet::domain($udom)) &&
 7642:                     ($uname =~ /^$match_username$/)) {
 7643:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7644:                         push(@okusers,$user);
 7645:                     }
 7646:                 }
 7647:             }
 7648:             if (@okusers > 0) {
 7649:                  @okusers = sort(@okusers);
 7650:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7651:             }
 7652:         }
 7653:         if (defined($env{'form.'.$type.'_machines'})) {
 7654:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7655:             my @okmachines;
 7656:             foreach my $ip (@machines) {
 7657:                 my @parts = split(/\./,$ip);
 7658:                 next if (@parts < 4);
 7659:                 my $badip = 0;
 7660:                 for (my $i=0; $i<4; $i++) {
 7661:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7662:                         $badip = 1;
 7663:                         last;
 7664:                     }
 7665:                 }
 7666:                 if (!$badip) {
 7667:                     push(@okmachines,$ip);     
 7668:                 }
 7669:             }
 7670:             @okmachines = sort(@okmachines);
 7671:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7672:         }
 7673:     }
 7674:     my %serverstatushash =  (
 7675:                                 serverstatuses => \%newserverstatus,
 7676:                             );
 7677:     foreach my $type (@pages) {
 7678:         foreach my $setting ('namedusers','machines') {
 7679:             my (@current,@new);
 7680:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7681:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7682:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7683:                 }
 7684:             }
 7685:             if ($newserverstatus{$type}{$setting} ne '') {
 7686:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7687:             }
 7688:             if (@current > 0) {
 7689:                 if (@new > 0) {
 7690:                     foreach my $item (@current) {
 7691:                         if (!grep(/^\Q$item\E$/,@new)) {
 7692:                             $changes{$type}{$setting} = 1;
 7693:                             last;
 7694:                         }
 7695:                     }
 7696:                     foreach my $item (@new) {
 7697:                         if (!grep(/^\Q$item\E$/,@current)) {
 7698:                             $changes{$type}{$setting} = 1;
 7699:                             last;
 7700:                         }
 7701:                     }
 7702:                 } else {
 7703:                     $changes{$type}{$setting} = 1;
 7704:                 }
 7705:             } elsif (@new > 0) {
 7706:                 $changes{$type}{$setting} = 1;
 7707:             }
 7708:         }
 7709:     }
 7710:     if (keys(%changes) > 0) {
 7711:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7712:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7713:                                                  \%serverstatushash,$dom);
 7714:         if ($putresult eq 'ok') {
 7715:             $resulttext .= &mt('Changes made:').'<ul>';
 7716:             foreach my $type (@pages) {
 7717:                 if (ref($changes{$type}) eq 'HASH') {
 7718:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7719:                     if ($changes{$type}{'namedusers'}) {
 7720:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7721:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7722:                         } else {
 7723:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7724:                         }
 7725:                     }
 7726:                     if ($changes{$type}{'machines'}) {
 7727:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7728:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7729:                         } else {
 7730:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7731:                         }
 7732: 
 7733:                     }
 7734:                     $resulttext .= '</ul></li>';
 7735:                 }
 7736:             }
 7737:             $resulttext .= '</ul>';
 7738:         } else {
 7739:             $resulttext = '<span class="LC_error">'.
 7740:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7741: 
 7742:         }
 7743:     } else {
 7744:         $resulttext = &mt('No changes made to access to server status pages');
 7745:     }
 7746:     return $resulttext;
 7747: }
 7748: 
 7749: sub modify_helpsettings {
 7750:     my ($r,$dom,$confname,%domconfig) = @_;
 7751:     my ($resulttext,$errors,%changes,%helphash);
 7752:     my %defaultchecked = ('submitbugs' => 'on');
 7753:     my @offon = ('off','on');
 7754:     my @toggles = ('submitbugs');
 7755:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7756:         foreach my $item (@toggles) {
 7757:             if ($defaultchecked{$item} eq 'on') { 
 7758:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7759:                     if ($env{'form.'.$item} eq '0') {
 7760:                         $changes{$item} = 1;
 7761:                     }
 7762:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7763:                     $changes{$item} = 1;
 7764:                 }
 7765:             } elsif ($defaultchecked{$item} eq 'off') {
 7766:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7767:                     if ($env{'form.'.$item} eq '1') {
 7768:                         $changes{$item} = 1;
 7769:                     }
 7770:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7771:                     $changes{$item} = 1;
 7772:                 }
 7773:             }
 7774:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) { 
 7775:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7776:             }
 7777:         }
 7778:     }
 7779:     my $putresult;
 7780:     if (keys(%changes) > 0) {
 7781:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7782:         if ($putresult eq 'ok') {
 7783:             $resulttext = &mt('Changes made:').'<ul>';
 7784:             foreach my $item (sort(keys(%changes))) {
 7785:                 if ($item eq 'submitbugs') {
 7786:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 7787:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 7788:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 7789:                 }
 7790:             }
 7791:             $resulttext .= '</ul>';
 7792:         } else {
 7793:             $resulttext = &mt('No changes made to help settings');
 7794:             $errors .= '<li><span class="LC_error">'.
 7795:                        &mt('An error occurred storing the settings: [_1]',
 7796:                            $putresult).'</span></li>';
 7797:         }
 7798:     }
 7799:     if ($errors) {
 7800:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7801:                        $errors.'</ul>';
 7802:     }
 7803:     return $resulttext;
 7804: }
 7805: 
 7806: sub modify_coursedefaults {
 7807:     my ($dom,%domconfig) = @_;
 7808:     my ($resulttext,$errors,%changes,%defaultshash);
 7809:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7810:     my @offon = ('off','on');
 7811:     my @toggles = ('canuse_pdfforms');
 7812: 
 7813:     $defaultshash{'coursedefaults'} = {};
 7814: 
 7815:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7816:         if ($domconfig{'coursedefaults'} eq '') {
 7817:             $domconfig{'coursedefaults'} = {};
 7818:         }
 7819:     }
 7820: 
 7821:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7822:         foreach my $item (@toggles) {
 7823:             if ($defaultchecked{$item} eq 'on') {
 7824:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7825:                     ($env{'form.'.$item} eq '0')) {
 7826:                     $changes{$item} = 1;
 7827:                 } elsif ($domconfig{'coursdefaults'}{$item} ne $env{'form.'.$item}) {
 7828:                     $changes{$item} = 1;
 7829:                 }
 7830:             } elsif ($defaultchecked{$item} eq 'off') {
 7831:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7832:                     ($env{'form.'.$item} eq '1')) {
 7833:                     $changes{$item} = 1;
 7834:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7835:                     $changes{$item} = 1;
 7836:                 }
 7837:             }
 7838:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7839:         }
 7840:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7841:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7842:         $newdefresponder =~ s/\D//g;
 7843:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7844:             $newdefresponder = 1;
 7845:         }
 7846:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7847:         if ($currdefresponder ne $newdefresponder) {
 7848:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7849:                 $changes{'anonsurvey_threshold'} = 1;
 7850:             }
 7851:         }
 7852:     }
 7853:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7854:                                              $dom);
 7855:     if ($putresult eq 'ok') {
 7856:         if (keys(%changes) > 0) {
 7857:             if ($changes{'canuse_pdfforms'}) {
 7858:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7859:                 $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7860:                 my $cachetime = 24*60*60;
 7861:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7862:             }
 7863:             $resulttext = &mt('Changes made:').'<ul>';
 7864:             foreach my $item (sort(keys(%changes))) {
 7865:                 if ($item eq 'canuse_pdfforms') {
 7866:                     if ($env{'form.'.$item} eq '1') {
 7867:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7868:                     } else {
 7869:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7870:                     }
 7871:                 } elsif ($item eq 'anonsurvey_threshold') {
 7872:                         $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7873:                 }
 7874:             }
 7875:             $resulttext .= '</ul>';
 7876:         } else {
 7877:             $resulttext = &mt('No changes made to course defaults');
 7878:         }
 7879:     } else {
 7880:         $resulttext = '<span class="LC_error">'.
 7881:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7882:     }
 7883:     return $resulttext;
 7884: }
 7885: 
 7886: sub modify_usersessions {
 7887:     my ($dom,%domconfig) = @_;
 7888:     my @hostingtypes = ('version','excludedomain','includedomain');
 7889:     my @offloadtypes = ('primary','default');
 7890:     my %types = (
 7891:                   remote => \@hostingtypes,
 7892:                   hosted => \@hostingtypes,
 7893:                   spares => \@offloadtypes,
 7894:                 );
 7895:     my @prefixes = ('remote','hosted','spares');
 7896:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7897:     my (%by_ip,%by_location,@intdoms);
 7898:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7899:     my @locations = sort(keys(%by_location));
 7900:     my (%defaultshash,%changes);
 7901:     foreach my $prefix (@prefixes) {
 7902:         $defaultshash{'usersessions'}{$prefix} = {};
 7903:     }
 7904:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7905:     my $resulttext;
 7906:     my %iphost = &Apache::lonnet::get_iphost();
 7907:     foreach my $prefix (@prefixes) {
 7908:         next if ($prefix eq 'spares');
 7909:         foreach my $type (@{$types{$prefix}}) {
 7910:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7911:             if ($type eq 'version') {
 7912:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7913:                 my $okvalue;
 7914:                 if ($value ne '') {
 7915:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 7916:                         $okvalue = $value;
 7917:                     }
 7918:                 }
 7919:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7920:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7921:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 7922:                             if ($inuse == 0) {
 7923:                                 $changes{$prefix}{$type} = 1;
 7924:                             } else {
 7925:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 7926:                                     $changes{$prefix}{$type} = 1;
 7927:                                 }
 7928:                                 if ($okvalue ne '') {
 7929:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7930:                                 } 
 7931:                             }
 7932:                         } else {
 7933:                             if (($inuse == 1) && ($okvalue ne '')) {
 7934:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7935:                                 $changes{$prefix}{$type} = 1;
 7936:                             }
 7937:                         }
 7938:                     } else {
 7939:                         if (($inuse == 1) && ($okvalue ne '')) {
 7940:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7941:                             $changes{$prefix}{$type} = 1;
 7942:                         }
 7943:                     }
 7944:                 } else {
 7945:                     if (($inuse == 1) && ($okvalue ne '')) {
 7946:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7947:                         $changes{$prefix}{$type} = 1;
 7948:                     }
 7949:                 }
 7950:             } else {
 7951:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 7952:                 my @okvals;
 7953:                 foreach my $val (@vals) {
 7954:                     if ($val =~ /:/) {
 7955:                         my @items = split(/:/,$val);
 7956:                         foreach my $item (@items) {
 7957:                             if (ref($by_location{$item}) eq 'ARRAY') {
 7958:                                 push(@okvals,$item);
 7959:                             }
 7960:                         }
 7961:                     } else {
 7962:                         if (ref($by_location{$val}) eq 'ARRAY') {
 7963:                             push(@okvals,$val);
 7964:                         }
 7965:                     }
 7966:                 }
 7967:                 @okvals = sort(@okvals);
 7968:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7969:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7970:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7971:                             if ($inuse == 0) {
 7972:                                 $changes{$prefix}{$type} = 1; 
 7973:                             } else {
 7974:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7975:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 7976:                                 if (@changed > 0) {
 7977:                                     $changes{$prefix}{$type} = 1;
 7978:                                 }
 7979:                             }
 7980:                         } else {
 7981:                             if ($inuse == 1) {
 7982:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7983:                                 $changes{$prefix}{$type} = 1;
 7984:                             }
 7985:                         } 
 7986:                     } else {
 7987:                         if ($inuse == 1) {
 7988:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7989:                             $changes{$prefix}{$type} = 1;
 7990:                         }
 7991:                     }
 7992:                 } else {
 7993:                     if ($inuse == 1) {
 7994:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7995:                         $changes{$prefix}{$type} = 1;
 7996:                     }
 7997:                 }
 7998:             }
 7999:         }
 8000:     }
 8001: 
 8002:     my @alldoms = &Apache::lonnet::all_domains();
 8003:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8004:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 8005:     my $savespares;
 8006: 
 8007:     foreach my $lonhost (sort(keys(%servers))) {
 8008:         my $serverhomeID =
 8009:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 8010:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 8011:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 8012:         my %spareschg;
 8013:         foreach my $type (@{$types{'spares'}}) {
 8014:             my @okspares;
 8015:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 8016:             foreach my $server (@checked) {
 8017:                 if (&Apache::lonnet::hostname($server) ne '') {
 8018:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 8019:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 8020:                             push(@okspares,$server);
 8021:                         }
 8022:                     }
 8023:                 }
 8024:             }
 8025:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 8026:             my $newspare;
 8027:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 8028:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 8029:                     $newspare = $new;
 8030:                 }
 8031:             }
 8032:             my @spares;
 8033:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 8034:                 @spares = sort(@okspares,$newspare);
 8035:             } else {
 8036:                 @spares = sort(@okspares);
 8037:             }
 8038:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 8039:             if (ref($spareid{$lonhost}) eq 'HASH') {
 8040:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 8041:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 8042:                     if (@diffs > 0) {
 8043:                         $spareschg{$type} = 1;
 8044:                     }
 8045:                 }
 8046:             }
 8047:         }
 8048:         if (keys(%spareschg) > 0) {
 8049:             $changes{'spares'}{$lonhost} = \%spareschg;
 8050:         }
 8051:     }
 8052: 
 8053:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8054:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 8055:             if (ref($changes{'spares'}) eq 'HASH') {
 8056:                 if (keys(%{$changes{'spares'}}) > 0) {
 8057:                     $savespares = 1;
 8058:                 }
 8059:             }
 8060:         } else {
 8061:             $savespares = 1;
 8062:         }
 8063:     }
 8064: 
 8065:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 8066:     if ((keys(%changes) > 0) || ($savespares)) {
 8067:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8068:                                                  $dom);
 8069:         if ($putresult eq 'ok') {
 8070:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8071:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 8072:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 8073:                 }
 8074:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 8075:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 8076:                 }
 8077:             }
 8078:             my $cachetime = 24*60*60;
 8079:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8080:             if (keys(%changes) > 0) {
 8081:                 my %lt = &usersession_titles();
 8082:                 $resulttext = &mt('Changes made:').'<ul>';
 8083:                 foreach my $prefix (@prefixes) {
 8084:                     if (ref($changes{$prefix}) eq 'HASH') {
 8085:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 8086:                         if ($prefix eq 'spares') {
 8087:                             if (ref($changes{$prefix}) eq 'HASH') {
 8088:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 8089:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 8090:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 8091:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 8092:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 8093:                                         foreach my $type (@{$types{$prefix}}) {
 8094:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 8095:                                                 my $offloadto = &mt('None');
 8096:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 8097:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 8098:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 8099:                                                     }
 8100:                                                 }
 8101:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 8102:                                             }
 8103:                                         }
 8104:                                     }
 8105:                                     $resulttext .= '</li>';
 8106:                                 }
 8107:                             }
 8108:                         } else {
 8109:                             foreach my $type (@{$types{$prefix}}) {
 8110:                                 if (defined($changes{$prefix}{$type})) {
 8111:                                     my $newvalue;
 8112:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8113:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 8114:                                             if ($type eq 'version') {
 8115:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 8116:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8117:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 8118:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 8119:                                                 }
 8120:                                             }
 8121:                                         }
 8122:                                     }
 8123:                                     if ($newvalue eq '') {
 8124:                                         if ($type eq 'version') {
 8125:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8126:                                         } else {
 8127:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8128:                                         }
 8129:                                     } else {
 8130:                                         if ($type eq 'version') {
 8131:                                             $newvalue .= ' '.&mt('(or later)'); 
 8132:                                         }
 8133:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8134:                                     }
 8135:                                 }
 8136:                             }
 8137:                         }
 8138:                         $resulttext .= '</ul>';
 8139:                     }
 8140:                 }
 8141:                 $resulttext .= '</ul>';
 8142:             } else {
 8143:                 $resulttext = $nochgmsg;
 8144:             }
 8145:         } else {
 8146:             $resulttext = '<span class="LC_error">'.
 8147:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8148:         }
 8149:     } else {
 8150:         $resulttext = $nochgmsg;
 8151:     }
 8152:     return $resulttext;
 8153: }
 8154: 
 8155: sub modify_loadbalancing {
 8156:     my ($dom,%domconfig) = @_;
 8157:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8158:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8159:     my ($othertitle,$usertypes,$types) =
 8160:         &Apache::loncommon::sorted_inst_types($dom);
 8161:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8162:     my @sparestypes = ('primary','default');
 8163:     my %typetitles = &sparestype_titles();
 8164:     my $resulttext;
 8165:     my (%currbalancer,%currtargets,%currrules,%existing);
 8166:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8167:         %existing = %{$domconfig{'loadbalancing'}};
 8168:     }
 8169:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8170:                               \%currtargets,\%currrules);
 8171:     my ($saveloadbalancing,%defaultshash,%changes);
 8172:     my ($alltypes,$othertypes,$titles) =
 8173:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8174:     my %ruletitles = &offloadtype_text();
 8175:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 8176:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 8177:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 8178:         if ($balancer eq '') {
 8179:             next;
 8180:         }
 8181:         if (!exists($servers{$balancer})) { 
 8182:             if (exists($currbalancer{$balancer})) {
 8183:                 push(@{$changes{'delete'}},$balancer);
 8184:             }
 8185:             next;
 8186:         }
 8187:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 8188:             push(@{$changes{'delete'}},$balancer);
 8189:             next;
 8190:         }
 8191:         if (!exists($currbalancer{$balancer})) {
 8192:             push(@{$changes{'add'}},$balancer);
 8193:         }
 8194:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 8195:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 8196:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 8197:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8198:             $saveloadbalancing = 1;
 8199:         }
 8200:         foreach my $sparetype (@sparestypes) {
 8201:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 8202:             my @offloadto;
 8203:             foreach my $target (@targets) {
 8204:                 if (($servers{$target}) && ($target ne $balancer)) {
 8205:                     if ($sparetype eq 'default') {
 8206:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 8207:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 8208:                         }
 8209:                     }
 8210:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 8211:                         push(@offloadto,$target);
 8212:                     }
 8213:                 }
 8214:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 8215:             }
 8216:         }
 8217:         if (ref($currtargets{$balancer}) eq 'HASH') {
 8218:             foreach my $sparetype (@sparestypes) {
 8219:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 8220:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 8221:                     if (@targetdiffs > 0) {
 8222:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8223:                     }
 8224:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8225:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8226:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8227:                     }
 8228:                 }
 8229:             }
 8230:         } else {
 8231:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 8232:                 foreach my $sparetype (@sparestypes) { 
 8233:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8234:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8235:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 8236:                         }
 8237:                     }
 8238:                 }
 8239:             }  
 8240:         }
 8241:         my $ishomedom;
 8242:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 8243:             $ishomedom = 1;
 8244:         }
 8245:         if (ref($alltypes) eq 'ARRAY') {
 8246:             foreach my $type (@{$alltypes}) {
 8247:                 my $rule;
 8248:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) && 
 8249:                          (!$ishomedom)) {
 8250:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 8251:                 }
 8252:                 if ($rule eq 'specific') {
 8253:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 8254:                 }
 8255:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 8256:                 if (ref($currrules{$balancer}) eq 'HASH') {
 8257:                     if ($rule ne $currrules{$balancer}{$type}) {
 8258:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8259:                     }
 8260:                 } elsif ($rule ne '') {
 8261:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8262:                 }
 8263:             }
 8264:         }
 8265:     }
 8266:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8267:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8268:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 8269:             $defaultshash{'loadbalancing'} = {};
 8270:         }
 8271:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8272:                                                  \%defaultshash,$dom);
 8273:  
 8274:         if ($putresult eq 'ok') {
 8275:             if (keys(%changes) > 0) {
 8276:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 8277:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 8278:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 8279:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8280:                     }
 8281:                 }
 8282:                 if (ref($changes{'add'}) eq 'ARRAY') {
 8283:                     foreach my $balancer (sort(@{$changes{'add'}})) {   
 8284:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 8285:                     }
 8286:                 }
 8287:                 if (ref($changes{'curr'}) eq 'HASH') {
 8288:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 8289:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 8290:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 8291:                                 my %offloadstr;
 8292:                                 foreach my $sparetype (@sparestypes) {
 8293:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8294:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8295:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 8296:                                         }
 8297:                                     }
 8298:                                 }
 8299:                                 if (keys(%offloadstr) == 0) {
 8300:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8301:                                 } else {
 8302:                                     my $showoffload;
 8303:                                     foreach my $sparetype (@sparestypes) {
 8304:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8305:                                         if (defined($offloadstr{$sparetype})) {
 8306:                                             $showoffload .= $offloadstr{$sparetype};
 8307:                                         } else {
 8308:                                             $showoffload .= &mt('None');
 8309:                                         }
 8310:                                         $showoffload .= ('&nbsp;'x3);
 8311:                                     }
 8312:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 8313:                                 }
 8314:                             }
 8315:                         }
 8316:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 8317:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8318:                                 foreach my $type (@{$alltypes}) {
 8319:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 8320:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 8321:                                         my $balancetext;
 8322:                                         if ($rule eq '') {
 8323:                                             $balancetext =  $ruletitles{'default'};
 8324:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 8325:                                             $balancetext =  $ruletitles{$rule};
 8326:                                         } else {
 8327:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 8328:                                         }
 8329:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';     
 8330:                                     }
 8331:                                 }
 8332:                             }
 8333:                         }
 8334:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8335:                     }
 8336:                 }
 8337:                 if ($resulttext ne '') {
 8338:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8339:                 } else {
 8340:                     $resulttext = $nochgmsg;
 8341:                 }
 8342:             } else {
 8343:                 $resulttext = $nochgmsg;
 8344:             }
 8345:         } else {
 8346:             $resulttext = '<span class="LC_error">'.
 8347:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8348:         }
 8349:     } else {
 8350:         $resulttext = $nochgmsg;
 8351:     }
 8352:     return $resulttext;
 8353: }
 8354: 
 8355: sub recurse_check {
 8356:     my ($chkcats,$categories,$depth,$name) = @_;
 8357:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8358:         my $chg = 0;
 8359:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8360:             my $category = $chkcats->[$depth]{$name}[$j];
 8361:             my $item;
 8362:             if ($category eq '') {
 8363:                 $chg ++;
 8364:             } else {
 8365:                 my $deeper = $depth + 1;
 8366:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8367:                 if ($chg) {
 8368:                     $categories->{$item} -= $chg;
 8369:                 }
 8370:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8371:                 $deeper --;
 8372:             }
 8373:         }
 8374:     }
 8375:     return;
 8376: }
 8377: 
 8378: sub recurse_cat_deletes {
 8379:     my ($item,$coursecategories,$deletions) = @_;
 8380:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8381:     my $subdepth = $depth + 1;
 8382:     if (ref($coursecategories) eq 'HASH') {
 8383:         foreach my $subitem (keys(%{$coursecategories})) {
 8384:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8385:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8386:                 delete($coursecategories->{$subitem});
 8387:                 $deletions->{$subitem} = 1;
 8388:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8389:             }
 8390:         }
 8391:     }
 8392:     return;
 8393: }
 8394: 
 8395: sub get_active_dcs {
 8396:     my ($dom) = @_;
 8397:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc']);
 8398:     my %domcoords;
 8399:     my $numdcs = 0;
 8400:     my $now = time;
 8401:     foreach my $server (keys(%dompersonnel)) {
 8402:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8403:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8404:             my ($end,$start) = split(':',$dompersonnel{$server}{$user});
 8405:             if (($end eq '') || ($end == 0) || ($end > $now)) {
 8406:                 if ($start <= $now) {
 8407:                     $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8408:                 }
 8409:             }
 8410:         }
 8411:     }
 8412:     return %domcoords;
 8413: }
 8414: 
 8415: sub active_dc_picker {
 8416:     my ($dom,$curr_dc) = @_;
 8417:     my %domcoords = &get_active_dcs($dom); 
 8418:     my @dcs = sort(keys(%domcoords));
 8419:     my $numdcs = scalar(@dcs); 
 8420:     my $datatable;
 8421:     my $numinrow = 2;
 8422:     if ($numdcs > 1) {
 8423:         $datatable = '<table>';
 8424:         for (my $i=0; $i<@dcs; $i++) {
 8425:             my $rem = $i%($numinrow);
 8426:             if ($rem == 0) {
 8427:                 if ($i > 0) {
 8428:                     $datatable .= '</tr>';
 8429:                 }
 8430:                 $datatable .= '<tr>';
 8431:             }
 8432:             my $check = ' ';
 8433:             if ($curr_dc eq '') {
 8434:                 if (!$i) { 
 8435:                     $check = ' checked="checked" ';
 8436:                 }
 8437:             } elsif ($dcs[$i] eq $curr_dc) {
 8438:                 $check = ' checked="checked" ';
 8439:             }
 8440:             if ($i == @dcs - 1) {
 8441:                 my $colsleft = $numinrow - $rem;
 8442:                 if ($colsleft > 1) {
 8443:                     $datatable .= '<td colspan="'.$colsleft.'">';
 8444:                 } else {
 8445:                     $datatable .= '<td>';
 8446:                 }
 8447:             } else {
 8448:                 $datatable .= '<td>';
 8449:             }
 8450:             my ($dcname,$dcdom) = split(':',$dcs[$i]);
 8451:             $datatable .= '<span class="LC_nobreak"><label>'.
 8452:                           '<input type="radio" name="autocreate_xmldc"'.
 8453:                           ' value="'.$dcs[$i].'"'.$check.'/>'.
 8454:                           &Apache::loncommon::plainname($dcname,$dcdom).
 8455:                           '</label></span></td>';
 8456:         }
 8457:         $datatable .= '</tr></table>';
 8458:     } elsif (@dcs) {
 8459:         $datatable .= '<input type="hidden" name="autocreate_dc" value="'.
 8460:                       $dcs[0].'" />';
 8461:     }
 8462:     return ($numdcs,$datatable);
 8463: }
 8464: 
 8465: sub usersession_titles {
 8466:     return &Apache::lonlocal::texthash(
 8467:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8468:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8469:                spares => 'Servers offloaded to, when busy',
 8470:                version => 'LON-CAPA version requirement',
 8471:                excludedomain => 'Allow all, but exclude specific domains',
 8472:                includedomain => 'Deny all, but include specific domains',
 8473:                primary => 'Primary (checked first)',
 8474:                default => 'Default',
 8475:            );
 8476: }
 8477: 
 8478: sub id_for_thisdom {
 8479:     my (%servers) = @_;
 8480:     my %altids;
 8481:     foreach my $server (keys(%servers)) {
 8482:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8483:         if ($serverhome ne $server) {
 8484:             $altids{$serverhome} = $server;
 8485:         }
 8486:     }
 8487:     return %altids;
 8488: }
 8489: 
 8490: sub count_servers {
 8491:     my ($currbalancer,%servers) = @_;
 8492:     my (@spares,$numspares);
 8493:     foreach my $lonhost (sort(keys(%servers))) {
 8494:         next if ($currbalancer eq $lonhost);
 8495:         push(@spares,$lonhost);
 8496:     }
 8497:     if ($currbalancer) {
 8498:         $numspares = scalar(@spares);
 8499:     } else {
 8500:         $numspares = scalar(@spares) - 1;
 8501:     }
 8502:     return ($numspares,@spares);
 8503: }
 8504: 
 8505: sub lonbalance_targets_js {
 8506:     my ($dom,$types,$servers,$settings) = @_;
 8507:     my $select = &mt('Select');
 8508:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8509:     if (ref($servers) eq 'HASH') {
 8510:         $alltargets = join("','",sort(keys(%{$servers})));
 8511:         my @homedoms;
 8512:         foreach my $server (sort(keys(%{$servers}))) {
 8513:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8514:                 push(@homedoms,'1');
 8515:             } else {
 8516:                 push(@homedoms,'0');
 8517:             }
 8518:         }
 8519:         $allishome = join("','",@homedoms);
 8520:     }
 8521:     if (ref($types) eq 'ARRAY') {
 8522:         if (@{$types} > 0) {
 8523:             @alltypes = @{$types};
 8524:         }
 8525:     }
 8526:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8527:     $allinsttypes = join("','",@alltypes);
 8528:     my (%currbalancer,%currtargets,%currrules,%existing);
 8529:     if (ref($settings) eq 'HASH') {
 8530:         %existing = %{$settings};
 8531:     }
 8532:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 8533:                               \%currtargets,\%currrules);
 8534:     my $balancers = join("','",sort(keys(%currbalancer))); 
 8535:     return <<"END";
 8536: 
 8537: <script type="text/javascript">
 8538: // <![CDATA[
 8539: 
 8540: currBalancers = new Array('$balancers');
 8541: 
 8542: function toggleTargets(balnum) {
 8543:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8544:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 8545:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 8546:     var prevbalancer = prevhostitem.value;
 8547:     var baltotal = document.getElementById('loadbalancing_total').value;
 8548:     prevhostitem.value = balancer;
 8549:     if (prevbalancer != '') {
 8550:         var prevIdx = currBalancers.indexOf(prevbalancer);
 8551:         if (prevIdx != -1) {
 8552:             currBalancers.splice(prevIdx,1);
 8553:         }
 8554:     }
 8555:     if (balancer == '') {
 8556:         hideSpares(balnum);
 8557:     } else {
 8558:         var currIdx = currBalancers.indexOf(balancer);
 8559:         if (currIdx == -1) {
 8560:             currBalancers.push(balancer);
 8561:         }
 8562:         var homedoms = new Array('$allishome');
 8563:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 8564:         showSpares(balancer,ishomedom,balnum);
 8565:     }
 8566:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 8567:     return;
 8568: }
 8569: 
 8570: function showSpares(balancer,ishomedom,balnum) {
 8571:     var alltargets = new Array('$alltargets');
 8572:     var insttypes = new Array('$allinsttypes');
 8573:     var offloadtypes = new Array('primary','default');
 8574: 
 8575:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 8576:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 8577:  
 8578:     for (var i=0; i<offloadtypes.length; i++) {
 8579:         var count = 0;
 8580:         for (var j=0; j<alltargets.length; j++) {
 8581:             if (alltargets[j] != balancer) {
 8582:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 8583:                 item.value = alltargets[j];
 8584:                 item.style.textAlign='left';
 8585:                 item.style.textFace='normal';
 8586:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8587:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 8588:                     item.disabled = '';
 8589:                 } else {
 8590:                     item.disabled = 'disabled';
 8591:                     item.checked = false;
 8592:                 }
 8593:                 count ++;
 8594:             }
 8595:         }
 8596:     }
 8597:     for (var k=0; k<insttypes.length; k++) {
 8598:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8599:             if (ishomedom == 1) {
 8600:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8601:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8602:             } else {
 8603:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8604:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8605: 
 8606:             }
 8607:         } else {
 8608:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8609:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8610:         }
 8611:         if ((insttypes[k] != '_LC_external') && 
 8612:             ((insttypes[k] != '_LC_internetdom') ||
 8613:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8614:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 8615:             item.options.length = 0;
 8616:             item.options[0] = new Option("","",true,true);
 8617:             var idx = 0;  
 8618:             for (var m=0; m<alltargets.length; m++) {
 8619:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 8620:                     idx ++;
 8621:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8622:                     
 8623:                 }
 8624:             }
 8625:         }
 8626:     }
 8627:     return;
 8628: }
 8629: 
 8630: function hideSpares(balnum) {
 8631:     var alltargets = new Array('$alltargets');
 8632:     var insttypes = new Array('$allinsttypes');
 8633:     var offloadtypes = new Array('primary','default');
 8634: 
 8635:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 8636:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 8637: 
 8638:     var total = alltargets.length - 1;
 8639:     for (var i=0; i<offloadtypes; i++) {
 8640:         for (var j=0; j<total; j++) {
 8641:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 8642:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 8643:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8644:         }
 8645:     }
 8646:     for (var k=0; k<insttypes.length; k++) {
 8647:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8648:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8649:         if (insttypes[k] != '_LC_external') {
 8650:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 8651:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 8652:         }
 8653:     }
 8654:     return;
 8655: }
 8656: 
 8657: function checkOffloads(item,balnum,type) {
 8658:     var alltargets = new Array('$alltargets');
 8659:     var offloadtypes = new Array('primary','default');
 8660:     if (item.checked) {
 8661:         var total = alltargets.length - 1;
 8662:         var other;
 8663:         if (type == offloadtypes[0]) {
 8664:             other = offloadtypes[1];
 8665:         } else {
 8666:             other = offloadtypes[0];
 8667:         }
 8668:         for (var i=0; i<total; i++) {
 8669:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 8670:             if (server == item.value) {
 8671:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 8672:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 8673:                 }
 8674:             }
 8675:         }
 8676:     }
 8677:     return;
 8678: }
 8679: 
 8680: function singleServerToggle(balnum,type) {
 8681:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 8682:     if (offloadtoSelIdx == 0) {
 8683:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 8684:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8685: 
 8686:     } else {
 8687:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 8688:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8689:     }
 8690:     return;
 8691: }
 8692: 
 8693: function balanceruleChange(formname,balnum,type) {
 8694:     if (type == '_LC_external') {
 8695:         return;
 8696:     }
 8697:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 8698:     for (var i=0; i<typesRules.length; i++) {
 8699:         if (formname.elements[typesRules[i]].checked) {
 8700:             if (formname.elements[typesRules[i]].value != 'specific') {
 8701:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 8702:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8703:             } else {
 8704:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8705:             }
 8706:         }
 8707:     }
 8708:     return;
 8709: }
 8710: 
 8711: function balancerDeleteChange(balnum) {
 8712:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8713:     var baltotal = document.getElementById('loadbalancing_total').value;
 8714:     var addtarget;
 8715:     var removetarget;
 8716:     var action = 'delete';
 8717:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 8718:         var lonhost = hostitem.value;
 8719:         var currIdx = currBalancers.indexOf(lonhost);
 8720:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 8721:             if (currIdx != -1) {
 8722:                 currBalancers.splice(currIdx,1);
 8723:             }
 8724:             addtarget = lonhost;
 8725:         } else {
 8726:             if (currIdx == -1) {
 8727:                 currBalancers.push(lonhost);
 8728:             }
 8729:             removetarget = lonhost;
 8730:             action = 'undelete';
 8731:         }
 8732:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 8733:     }
 8734:     return;
 8735: }
 8736: 
 8737: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 8738:     if (baltotal > 1) {
 8739:         var offloadtypes = new Array('primary','default');
 8740:         var alltargets = new Array('$alltargets');
 8741:         var insttypes = new Array('$allinsttypes');
 8742:         for (var i=0; i<baltotal; i++) {
 8743:             if (i != balnum) {
 8744:                 for (var j=0; j<offloadtypes.length; j++) {
 8745:                     var total = alltargets.length - 1;
 8746:                     for (var k=0; k<total; k++) {
 8747:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 8748:                         var server = serveritem.value;
 8749:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 8750:                             if (server == addtarget) {
 8751:                                 serveritem.disabled = '';
 8752:                             }
 8753:                         }
 8754:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8755:                             if (server == removetarget) {
 8756:                                 serveritem.disabled = 'disabled';
 8757:                                 serveritem.checked = false;
 8758:                             }
 8759:                         }
 8760:                     }
 8761:                 }
 8762:                 for (var j=0; j<insttypes.length; j++) {
 8763:                     if (insttypes[j] != '_LC_external') {
 8764:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 8765:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 8766:                             var currSel = singleserver.selectedIndex;
 8767:                             var currVal = singleserver.options[currSel].value;
 8768:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 8769:                                 var numoptions = singleserver.options.length;
 8770:                                 var needsnew = 1;
 8771:                                 for (var k=0; k<numoptions; k++) {
 8772:                                     if (singleserver.options[k] == addtarget) {
 8773:                                         needsnew = 0;
 8774:                                         break;
 8775:                                     }
 8776:                                 }
 8777:                                 if (needsnew == 1) {
 8778:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 8779:                                 }
 8780:                             }
 8781:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8782:                                 singleserver.options.length = 0;
 8783:                                 if ((currVal) && (currVal != removetarget)) {
 8784:                                     singleserver.options[0] = new Option("","",false,false);
 8785:                                 } else {
 8786:                                     singleserver.options[0] = new Option("","",true,true);
 8787:                                 }
 8788:                                 var idx = 0;
 8789:                                 for (var m=0; m<alltargets.length; m++) {
 8790:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
 8791:                                         idx ++;
 8792:                                         if (currVal == alltargets[m]) {
 8793:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
 8794:                                         } else {
 8795:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8796:                                         }
 8797:                                     }
 8798:                                 }
 8799:                             }
 8800:                         }
 8801:                     }
 8802:                 }
 8803:             }
 8804:         }
 8805:     }
 8806:     return;
 8807: }
 8808: 
 8809: // ]]>
 8810: </script>
 8811: 
 8812: END
 8813: }
 8814: 
 8815: sub new_spares_js {
 8816:     my @sparestypes = ('primary','default');
 8817:     my $types = join("','",@sparestypes);
 8818:     my $select = &mt('Select');
 8819:     return <<"END";
 8820: 
 8821: <script type="text/javascript">
 8822: // <![CDATA[
 8823: 
 8824: function updateNewSpares(formname,lonhost) {
 8825:     var types = new Array('$types');
 8826:     var include = new Array();
 8827:     var exclude = new Array();
 8828:     for (var i=0; i<types.length; i++) {
 8829:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8830:         for (var j=0; j<spareboxes.length; j++) {
 8831:             if (formname.elements[spareboxes[j]].checked) {
 8832:                 exclude.push(formname.elements[spareboxes[j]].value);
 8833:             } else {
 8834:                 include.push(formname.elements[spareboxes[j]].value);
 8835:             }
 8836:         }
 8837:     }
 8838:     for (var i=0; i<types.length; i++) {
 8839:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8840:         var selIdx = newSpare.selectedIndex;
 8841:         var currnew = newSpare.options[selIdx].value;
 8842:         var okSpares = new Array();
 8843:         for (var j=0; j<newSpare.options.length; j++) {
 8844:             var possible = newSpare.options[j].value;
 8845:             if (possible != '') {
 8846:                 if (exclude.indexOf(possible) == -1) {
 8847:                     okSpares.push(possible);
 8848:                 } else {
 8849:                     if (currnew == possible) {
 8850:                         selIdx = 0;
 8851:                     }
 8852:                 }
 8853:             }
 8854:         }
 8855:         for (var k=0; k<include.length; k++) {
 8856:             if (okSpares.indexOf(include[k]) == -1) {
 8857:                 okSpares.push(include[k]);
 8858:             }
 8859:         }
 8860:         okSpares.sort();
 8861:         newSpare.options.length = 0;
 8862:         if (selIdx == 0) {
 8863:             newSpare.options[0] = new Option("$select","",true,true);
 8864:         } else {
 8865:             newSpare.options[0] = new Option("$select","",false,false);
 8866:         }
 8867:         for (var m=0; m<okSpares.length; m++) {
 8868:             var idx = m+1;
 8869:             var selThis = 0;
 8870:             if (selIdx != 0) {
 8871:                 if (okSpares[m] == currnew) {
 8872:                     selThis = 1;
 8873:                 }
 8874:             }
 8875:             if (selThis == 1) {
 8876:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8877:             } else {
 8878:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8879:             }
 8880:         }
 8881:     }
 8882:     return;
 8883: }
 8884: 
 8885: function checkNewSpares(lonhost,type) {
 8886:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8887:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8888:     if (chosen != '') { 
 8889:         var othertype;
 8890:         var othernewSpare;
 8891:         if (type == 'primary') {
 8892:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 8893:         }
 8894:         if (type == 'default') {
 8895:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 8896:         }
 8897:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 8898:             othernewSpare.selectedIndex = 0;
 8899:         }
 8900:     }
 8901:     return;
 8902: }
 8903: 
 8904: // ]]>
 8905: </script>
 8906: 
 8907: END
 8908: 
 8909: }
 8910: 
 8911: sub common_domprefs_js {
 8912:     return <<"END";
 8913: 
 8914: <script type="text/javascript">
 8915: // <![CDATA[
 8916: 
 8917: function getIndicesByName(formname,item) {
 8918:     var group = new Array();
 8919:     for (var i=0;i<formname.elements.length;i++) {
 8920:         if (formname.elements[i].name == item) {
 8921:             group.push(formname.elements[i].id);
 8922:         }
 8923:     }
 8924:     return group;
 8925: }
 8926: 
 8927: // ]]>
 8928: </script>
 8929: 
 8930: END
 8931: 
 8932: }
 8933: 
 8934: sub recaptcha_js {
 8935:     my %lt = &captcha_phrases();
 8936:     return <<"END";
 8937: 
 8938: <script type="text/javascript">
 8939: // <![CDATA[
 8940: 
 8941: function updateCaptcha(caller,context) {
 8942:     var privitem;
 8943:     var pubitem;
 8944:     var privtext;
 8945:     var pubtext;
 8946:     if (document.getElementById(context+'_recaptchapub')) {
 8947:         pubitem = document.getElementById(context+'_recaptchapub');
 8948:     } else {
 8949:         return;
 8950:     }
 8951:     if (document.getElementById(context+'_recaptchapriv')) {
 8952:         privitem = document.getElementById(context+'_recaptchapriv');
 8953:     } else {
 8954:         return;
 8955:     }
 8956:     if (document.getElementById(context+'_recaptchapubtxt')) {
 8957:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 8958:     } else {
 8959:         return;
 8960:     }
 8961:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 8962:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 8963:     } else {
 8964:         return;
 8965:     }
 8966:     if (caller.checked) {
 8967:         if (caller.value == 'recaptcha') {
 8968:             pubitem.type = 'text';
 8969:             privitem.type = 'text';
 8970:             pubitem.size = '40';
 8971:             privitem.size = '40';
 8972:             pubtext.innerHTML = "$lt{'pub'}";
 8973:             privtext.innerHTML = "$lt{'priv'}";
 8974:         } else {
 8975:             pubitem.type = 'hidden';
 8976:             privitem.type = 'hidden';
 8977:             pubtext.innerHTML = '';
 8978:             privtext.innerHTML = '';
 8979:         }
 8980:     }
 8981:     return;
 8982: }
 8983: 
 8984: // ]]>
 8985: </script>
 8986: 
 8987: END
 8988: 
 8989: }
 8990: 
 8991: sub captcha_phrases {
 8992:     return &Apache::lonlocal::texthash (
 8993:                  priv => 'Private key',
 8994:                  pub  => 'Public key',
 8995:                  original  => 'original (CAPTCHA)',
 8996:                  recaptcha => 'successor (ReCAPTCHA)',
 8997:                  notused   => 'unused',
 8998:     );
 8999: }
 9000: 
 9001: 1;

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