File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.16: download - view: text, annotated - select for diffs
Fri Mar 1 21:17:24 2013 UTC (11 years, 2 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11.
  - Backport 1.191, 1.192

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.16 2013/03/01 21:17:24 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affiliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas,
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course.
   98: 
   99: Outputs: 1
  100: 
  101: $datatable  - HTML containing form elements which allow settings to be changed. 
  102: 
  103: In the case of course requests, radio buttons are displayed for each institutional
  104: affiliate type (and also default, and _LC_adv) for each of the course types 
  105: (official, unofficial and community).  In each case the radio buttons allow the 
  106: selection of one of four values:
  107: 
  108: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  109: which have the following effects:
  110: 
  111: 0
  112: 
  113: =over
  114: 
  115: - course requests are not allowed for this course types/affiliation
  116: 
  117: =back
  118: 
  119: approval 
  120: 
  121: =over 
  122: 
  123: - course requests must be approved by a Doman Coordinator in the 
  124: course's domain
  125: 
  126: =back
  127: 
  128: validate 
  129: 
  130: =over
  131: 
  132: - an institutional validation (e.g., check requestor is instructor
  133: of record) needs to be passed before the course will be created.  The required
  134: validation is in localenroll.pm on the primary library server for the course 
  135: domain.
  136: 
  137: =back
  138: 
  139: autolimit 
  140: 
  141: =over
  142:  
  143: - course requests will be processed automatically up to a limit of
  144: N requests for the course type for the particular requestor.
  145: If N is undefined, there is no limit to the number of course requests
  146: which a course owner may submit and have processed automatically. 
  147: 
  148: =back
  149: 
  150: =item modify_quotas() 
  151: 
  152: =back
  153: 
  154: =cut
  155: 
  156: package Apache::domainprefs;
  157: 
  158: use strict;
  159: use Apache::Constants qw(:common :http);
  160: use Apache::lonnet;
  161: use Apache::loncommon();
  162: use Apache::lonhtmlcommon();
  163: use Apache::lonlocal;
  164: use Apache::lonmsg();
  165: use Apache::lonconfigsettings;
  166: use LONCAPA qw(:DEFAULT :match);
  167: use LONCAPA::Enrollment;
  168: use LONCAPA::lonauthcgi();
  169: use File::Copy;
  170: use Locale::Language;
  171: use DateTime::TimeZone;
  172: use DateTime::Locale;
  173: 
  174: my $registered_cleanup;
  175: my $modified_urls;
  176: 
  177: sub handler {
  178:     my $r=shift;
  179:     if ($r->header_only) {
  180:         &Apache::loncommon::content_type($r,'text/html');
  181:         $r->send_http_header;
  182:         return OK;
  183:     }
  184: 
  185:     my $context = 'domain';
  186:     my $dom = $env{'request.role.domain'};
  187:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  188:     if (&Apache::lonnet::allowed('mau',$dom)) {
  189:         &Apache::loncommon::content_type($r,'text/html');
  190:         $r->send_http_header;
  191:     } else {
  192:         $env{'user.error.msg'}=
  193:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  194:         return HTTP_NOT_ACCEPTABLE;
  195:     }
  196: 
  197:     $registered_cleanup=0;
  198:     @{$modified_urls}=();
  199: 
  200:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  201:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  202:                                             ['phase','actions']);
  203:     my $phase = 'pickactions';
  204:     if ( exists($env{'form.phase'}) ) {
  205:         $phase = $env{'form.phase'};
  206:     }
  207:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  208:     my %domconfig =
  209:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  210:                 'quotas','autoenroll','autoupdate','autocreate',
  211:                 'directorysrch','usercreation','usermodification',
  212:                 'contacts','defaults','scantron','coursecategories',
  213:                 'serverstatuses','requestcourses','coursedefaults',
  214:                 'usersessions','loadbalancing','requestauthor'],$dom);
  215:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  216:                        'autoupdate','autocreate','directorysrch','contacts',
  217:                        'usercreation','usermodification','scantron',
  218:                        'requestcourses','requestauthor','coursecategories',
  219:                        'serverstatuses','coursedefaults','usersessions');
  220:     my %existing;
  221:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  222:         %existing = %{$domconfig{'loadbalancing'}};
  223:     }
  224:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  225:         push(@prefs_order,'loadbalancing');
  226:     }
  227:     my %prefs = (
  228:         'rolecolors' =>
  229:                    { text => 'Default color schemes',
  230:                      help => 'Domain_Configuration_Color_Schemes',
  231:                      header => [{col1 => 'Student Settings',
  232:                                  col2 => '',},
  233:                                 {col1 => 'Coordinator Settings',
  234:                                  col2 => '',},
  235:                                 {col1 => 'Author Settings',
  236:                                  col2 => '',},
  237:                                 {col1 => 'Administrator Settings',
  238:                                  col2 => '',}],
  239:                     },
  240:         'login' =>
  241:                     { text => 'Log-in page options',
  242:                       help => 'Domain_Configuration_Login_Page',
  243:                       header => [{col1 => 'Log-in Page Items',
  244:                                   col2 => '',},
  245:                                  {col1 => 'Log-in Help',
  246:                                   col2 => 'Value'}],
  247:                     },
  248:         'defaults' => 
  249:                     { text => 'Default authentication/language/timezone/portal',
  250:                       help => 'Domain_Configuration_LangTZAuth',
  251:                       header => [{col1 => 'Setting',
  252:                                   col2 => 'Value'}],
  253:                     },
  254:         'quotas' => 
  255:                     { text => 'Blogs, personal web pages, webDAV, portfolios',
  256:                       help => 'Domain_Configuration_Quotas',
  257:                       header => [{col1 => 'User affiliation',
  258:                                   col2 => 'Available tools',
  259:                                   col3 => 'Portfolio quota',}],
  260:                     },
  261:         'autoenroll' =>
  262:                    { text => 'Auto-enrollment settings',
  263:                      help => 'Domain_Configuration_Auto_Enrollment',
  264:                      header => [{col1 => 'Configuration setting',
  265:                                  col2 => 'Value(s)'}],
  266:                    },
  267:         'autoupdate' => 
  268:                    { text => 'Auto-update settings',
  269:                      help => 'Domain_Configuration_Auto_Updates',
  270:                      header => [{col1 => 'Setting',
  271:                                  col2 => 'Value',},
  272:                                 {col1 => 'Setting',
  273:                                  col2 => 'Affiliation'},
  274:                                 {col1 => 'User population',
  275:                                  col2 => 'Updateable user data'}],
  276:                   },
  277:         'autocreate' => 
  278:                   { text => 'Auto-course creation settings',
  279:                      help => 'Domain_Configuration_Auto_Creation',
  280:                      header => [{col1 => 'Configuration Setting',
  281:                                  col2 => 'Value',}],
  282:                   },
  283:         'directorysrch' => 
  284:                   { text => 'Institutional directory searches',
  285:                     help => 'Domain_Configuration_InstDirectory_Search',
  286:                     header => [{col1 => 'Setting',
  287:                                 col2 => 'Value',}],
  288:                   },
  289:         'contacts' =>
  290:                   { text => 'Contact Information',
  291:                     help => 'Domain_Configuration_Contact_Info',
  292:                     header => [{col1 => 'Setting',
  293:                                 col2 => 'Value',}],
  294:                   },
  295: 
  296:         'usercreation' => 
  297:                   { text => 'User creation',
  298:                     help => 'Domain_Configuration_User_Creation',
  299:                     header => [{col1 => 'Format rule type',
  300:                                 col2 => 'Format rules in force'},
  301:                                {col1 => 'User account creation',
  302:                                 col2 => 'Usernames which may be created',},
  303:                                {col1 => 'Context',
  304:                                 col2 => 'Assignable authentication types'}],
  305:                   },
  306:         'usermodification' =>
  307:                   { text => 'User modification',
  308:                     help => 'Domain_Configuration_User_Modification',
  309:                     header => [{col1 => 'Target user has role',
  310:                                 col2 => 'User information updateable in author context'},
  311:                                {col1 => 'Target user has role',
  312:                                 col2 => 'User information updateable in course context'},
  313:                                {col1 => "Status of user",
  314:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  315:                   },
  316:         'scantron' =>
  317:                   { text => 'Bubblesheet format file',
  318:                     help => 'Domain_Configuration_Scantron_Format',
  319:                     header => [ {col1 => 'Item',
  320:                                  col2 => '',
  321:                               }],
  322:                   },
  323:         'requestcourses' => 
  324:                  {text => 'Request creation of courses',
  325:                   help => 'Domain_Configuration_Request_Courses',
  326:                   header => [{col1 => 'User affiliation',
  327:                               col2 => 'Availability/Processing of requests',},
  328:                              {col1 => 'Setting',
  329:                               col2 => 'Value'}],
  330:                  },
  331:         'requestauthor' =>
  332:                  {text => 'Request authoring space',
  333:                   help => 'Domain_Configuration_Request_Author',
  334:                   header => [{col1 => 'User affiliation',
  335:                               col2 => 'Availability/Processing of requests',},
  336:                              {col1 => 'Setting',
  337:                               col2 => 'Value'}],
  338:                  },
  339:         'coursecategories' =>
  340:                   { text => 'Cataloging of courses/communities',
  341:                     help => 'Domain_Configuration_Cataloging_Courses',
  342:                     header => [{col1 => 'Category settings',
  343:                                 col2 => '',},
  344:                                {col1 => 'Categories',
  345:                                 col2 => '',
  346:                                }],
  347:                   },
  348:         'serverstatuses' =>
  349:                  {text   => 'Access to server status pages',
  350:                   help   => 'Domain_Configuration_Server_Status',
  351:                   header => [{col1 => 'Status Page',
  352:                               col2 => 'Other named users',
  353:                               col3 => 'Specific IPs',
  354:                             }],
  355:                  },
  356:         'coursedefaults' =>
  357:                  {text => 'Course/Community defaults',
  358:                   help => 'Domain_Configuration_Course_Defaults',
  359:                   header => [{col1 => 'Defaults which can be overridden for each course by a DC',
  360:                               col2 => 'Value',},],
  361:                  },
  362:         'usersessions' =>
  363:                  {text  => 'User session hosting/offloading',
  364:                   help  => 'Domain_Configuration_User_Sessions',
  365:                   header => [{col1 => 'Domain server',
  366:                               col2 => 'Servers to offload sessions to when busy'},
  367:                              {col1 => 'Hosting of users from other domains',
  368:                               col2 => 'Rules'},
  369:                              {col1 => "Hosting domain's own users elsewhere",
  370:                               col2 => 'Rules'}],
  371:                  },
  372:          'loadbalancing' =>
  373:                  {text  => 'Dedicated Load Balancer(s)',
  374:                   help  => 'Domain_Configuration_Load_Balancing',
  375:                   header => [{col1 => 'Balancers',
  376:                               col2 => 'Default destinations',
  377:                               col3 => 'User affiliation',
  378:                               col4 => 'Overrides'},
  379:                             ],
  380:                  },
  381:     );
  382:     if (keys(%servers) > 1) {
  383:         $prefs{'login'}  = { text   => 'Log-in page options',
  384:                              help   => 'Domain_Configuration_Login_Page',
  385:                             header => [{col1 => 'Log-in Service',
  386:                                         col2 => 'Server Setting',},
  387:                                        {col1 => 'Log-in Page Items',
  388:                                         col2 => ''},
  389:                                        {col1 => 'Log-in Help',
  390:                                         col2 => 'Value'}],
  391:                            };
  392:     }
  393: 
  394:     my @roles = ('student','coordinator','author','admin');
  395:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  396:     &Apache::lonhtmlcommon::add_breadcrumb
  397:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  398:       text=>"Settings to display/modify"});
  399:     my $confname = $dom.'-domainconfig';
  400: 
  401:     if ($phase eq 'process') {
  402:         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
  403:     } elsif ($phase eq 'display') {
  404:         my $js = &recaptcha_js().
  405:                  &credits_js(); 
  406:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  407:             my ($othertitle,$usertypes,$types) =
  408:                 &Apache::loncommon::sorted_inst_types($dom);
  409:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  410:                                           $domconfig{'loadbalancing'}).
  411:                    &new_spares_js().
  412:                    &common_domprefs_js().
  413:                    &Apache::loncommon::javascript_array_indexof();
  414:         }
  415:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  416:     } else {
  417: # check if domconfig user exists for the domain.
  418:         my $servadm = $r->dir_config('lonAdmEMail');
  419:         my ($configuserok,$author_ok,$switchserver) = 
  420:             &config_check($dom,$confname,$servadm);
  421:         unless ($configuserok eq 'ok') {
  422:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  423:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  424:                           $confname).
  425:                       '<br />'
  426:             );
  427:             if ($switchserver) {
  428:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  429:                           '<br />'.
  430:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  431:                           '<br />'.
  432:                           &mt('The "[_1]" user can be created automatically when a Domain Coordinator visits the web-based "Set domain configuration" screen, in a session hosted on the primary library server.',$confname).
  433:                           '<br />'.
  434:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  435:                 );
  436:             } else {
  437:                 $r->print(&mt('To create that user from the command line run the ./UPDATE script found in the top level directory of the extracted LON-CAPA tarball.').
  438:                           '<br />'.
  439:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  440:                 );
  441:             }
  442:             $r->print(&Apache::loncommon::end_page());
  443:             return OK;
  444:         }
  445:         if (keys(%domconfig) == 0) {
  446:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  447:             my @ids=&Apache::lonnet::current_machine_ids();
  448:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  449:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  450:                 my @loginimages = ('img','logo','domlogo','login');
  451:                 my $custom_img_count = 0;
  452:                 foreach my $img (@loginimages) {
  453:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  454:                         $custom_img_count ++;
  455:                     }
  456:                 }
  457:                 foreach my $role (@roles) {
  458:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  459:                         $custom_img_count ++;
  460:                     }
  461:                 }
  462:                 if ($custom_img_count > 0) {
  463:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  464:                     my $switch_server = &check_switchserver($dom,$confname);
  465:                     $r->print(
  466:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  467:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  468:     &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 />'.
  469:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  470:                     if ($switch_server) {
  471:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  472:                     }
  473:                     $r->print(&Apache::loncommon::end_page());
  474:                     return OK;
  475:                 }
  476:             }
  477:         }
  478:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  479:     }
  480:     return OK;
  481: }
  482: 
  483: sub process_changes {
  484:     my ($r,$dom,$confname,$action,$roles,$values) = @_;
  485:     my %domconfig;
  486:     if (ref($values) eq 'HASH') {
  487:         %domconfig = %{$values};
  488:     }
  489:     my $output;
  490:     if ($action eq 'login') {
  491:         $output = &modify_login($r,$dom,$confname,%domconfig);
  492:     } elsif ($action eq 'rolecolors') {
  493:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  494:                                      %domconfig);
  495:     } elsif ($action eq 'quotas') {
  496:         $output = &modify_quotas($dom,$action,%domconfig);
  497:     } elsif ($action eq 'autoenroll') {
  498:         $output = &modify_autoenroll($dom,%domconfig);
  499:     } elsif ($action eq 'autoupdate') {
  500:         $output = &modify_autoupdate($dom,%domconfig);
  501:     } elsif ($action eq 'autocreate') {
  502:         $output = &modify_autocreate($dom,%domconfig);
  503:     } elsif ($action eq 'directorysrch') {
  504:         $output = &modify_directorysrch($dom,%domconfig);
  505:     } elsif ($action eq 'usercreation') {
  506:         $output = &modify_usercreation($dom,%domconfig);
  507:     } elsif ($action eq 'usermodification') {
  508:         $output = &modify_usermodification($dom,%domconfig);
  509:     } elsif ($action eq 'contacts') {
  510:         $output = &modify_contacts($dom,%domconfig);
  511:     } elsif ($action eq 'defaults') {
  512:         $output = &modify_defaults($dom,$r);
  513:     } elsif ($action eq 'scantron') {
  514:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  515:     } elsif ($action eq 'coursecategories') {
  516:         $output = &modify_coursecategories($dom,%domconfig);
  517:     } elsif ($action eq 'serverstatuses') {
  518:         $output = &modify_serverstatuses($dom,%domconfig);
  519:     } elsif ($action eq 'requestcourses') {
  520:         $output = &modify_quotas($dom,$action,%domconfig);
  521:     } elsif ($action eq 'requestauthor') {
  522:         $output = &modify_quotas($dom,$action,%domconfig);
  523:     } elsif ($action eq 'coursedefaults') {
  524:         $output = &modify_coursedefaults($dom,%domconfig);
  525:     } elsif ($action eq 'usersessions') {
  526:         $output = &modify_usersessions($dom,%domconfig);
  527:     } elsif ($action eq 'loadbalancing') {
  528:         $output = &modify_loadbalancing($dom,%domconfig);
  529:     }
  530:     return $output;
  531: }
  532: 
  533: sub print_config_box {
  534:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  535:     my $rowtotal = 0;
  536:     my $output;
  537:     if ($action eq 'coursecategories') {
  538:         $output = &coursecategories_javascript($settings);
  539:     }
  540:     $output .= 
  541:          '<table class="LC_nested_outer">
  542:           <tr>
  543:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  544:            &mt($item->{text}).'&nbsp;'.
  545:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  546:           '</tr>';
  547:     $rowtotal ++;
  548:     my $numheaders = 1;
  549:     if (ref($item->{'header'}) eq 'ARRAY') {
  550:         $numheaders = scalar(@{$item->{'header'}});
  551:     }
  552:     if ($numheaders > 1) {
  553:         my $colspan = '';
  554:         my $rightcolspan = '';
  555:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  556:             (($action eq 'login') && ($numheaders < 3))) {
  557:             $colspan = ' colspan="2"';
  558:         }
  559:         if ($action eq 'usersessions') {
  560:             $rightcolspan = ' colspan="3"'; 
  561:         }
  562:         $output .= '
  563:           <tr>
  564:            <td>
  565:             <table class="LC_nested">
  566:              <tr class="LC_info_row">
  567:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  568:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  569:              </tr>';
  570:         $rowtotal ++;
  571:         if ($action eq 'autoupdate') {
  572:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  573:         } elsif ($action eq 'usercreation') {
  574:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  575:         } elsif ($action eq 'usermodification') {
  576:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  577:         } elsif ($action eq 'coursecategories') {
  578:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  579:         } elsif ($action eq 'login') {
  580:             if ($numheaders == 3) {
  581:                 $colspan = ' colspan="2"';
  582:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  583:             } else {
  584:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  585:             }
  586:         } elsif ($action eq 'requestcourses') {
  587:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  588:         } elsif ($action eq 'requestauthor') {
  589:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  590:         } elsif ($action eq 'usersessions') {
  591:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  592:         } elsif ($action eq 'rolecolors') {
  593:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  594:         }
  595:         $output .= '
  596:            </table>
  597:           </td>
  598:          </tr>
  599:          <tr>
  600:            <td>
  601:             <table class="LC_nested">
  602:              <tr class="LC_info_row">
  603:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  604:         $output .= '
  605:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  606:              </tr>';
  607:             $rowtotal ++;
  608:         if ($action eq 'autoupdate') {
  609:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  610:            </table>
  611:           </td>
  612:          </tr>
  613:          <tr>
  614:            <td>
  615:             <table class="LC_nested">
  616:              <tr class="LC_info_row">
  617:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  618:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  619:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  620:             $rowtotal ++;
  621:         } elsif ($action eq 'usercreation') {
  622:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  623:            </table>
  624:           </td>
  625:          </tr>
  626:          <tr>
  627:            <td>
  628:             <table class="LC_nested">
  629:              <tr class="LC_info_row">
  630:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  631:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  632:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  633:             $rowtotal ++;
  634:         } elsif ($action eq 'usermodification') {
  635:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  636:            </table>
  637:           </td>
  638:          </tr>
  639:          <tr>
  640:            <td>
  641:             <table class="LC_nested">
  642:              <tr class="LC_info_row">
  643:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  644:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  645:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  646:             $rowtotal ++;
  647:         } elsif ($action eq 'coursecategories') {
  648:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  649:         } elsif ($action eq 'login') {
  650:             if ($numheaders == 3) {
  651:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  652:            </table>
  653:           </td>
  654:          </tr>
  655:          <tr>
  656:            <td>
  657:             <table class="LC_nested">
  658:              <tr class="LC_info_row">
  659:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  660:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  661:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  662:                 $rowtotal ++;
  663:             } else {
  664:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  665:             }
  666:         } elsif ($action eq 'requestcourses') {
  667:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  668:         } elsif ($action eq 'requestauthor') {
  669:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  670:         } elsif ($action eq 'usersessions') {
  671:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  672:            </table>
  673:           </td>
  674:          </tr>
  675:          <tr>
  676:            <td>
  677:             <table class="LC_nested">
  678:              <tr class="LC_info_row">
  679:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  680:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  681:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  682:             $rowtotal ++;
  683:         } elsif ($action eq 'rolecolors') {
  684:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  685:            </table>
  686:           </td>
  687:          </tr>
  688:          <tr>
  689:            <td>
  690:             <table class="LC_nested">
  691:              <tr class="LC_info_row">
  692:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  693:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  694:               <td class="LC_right_item" valign="top">'.
  695:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  696:              </tr>'.
  697:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  698:            </table>
  699:           </td>
  700:          </tr>
  701:          <tr>
  702:            <td>
  703:             <table class="LC_nested">
  704:              <tr class="LC_info_row">
  705:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  706:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  707:              </tr>'.
  708:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  709:             $rowtotal += 2;
  710:         }
  711:     } else {
  712:         $output .= '
  713:           <tr>
  714:            <td>
  715:             <table class="LC_nested">
  716:              <tr class="LC_info_row">';
  717:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  718:             $output .= '  
  719:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  720:         } elsif ($action eq 'serverstatuses') {
  721:             $output .= '
  722:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  723:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  724: 
  725:         } else {
  726:             $output .= '
  727:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  728:         }
  729:         if (defined($item->{'header'}->[0]->{'col3'})) {
  730:             $output .= '<td class="LC_left_item" valign="top">'.
  731:                        &mt($item->{'header'}->[0]->{'col2'});
  732:             if ($action eq 'serverstatuses') {
  733:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  734:             } 
  735:         } else {
  736:             $output .= '<td class="LC_right_item" valign="top">'.
  737:                        &mt($item->{'header'}->[0]->{'col2'});
  738:         }
  739:         $output .= '</td>';
  740:         if ($item->{'header'}->[0]->{'col3'}) {
  741:             if (defined($item->{'header'}->[0]->{'col4'})) {
  742:                 $output .= '<td class="LC_left_item" valign="top">'.
  743:                             &mt($item->{'header'}->[0]->{'col3'});
  744:             } else {
  745:                 $output .= '<td class="LC_right_item" valign="top">'.
  746:                            &mt($item->{'header'}->[0]->{'col3'});
  747:             }
  748:             if ($action eq 'serverstatuses') {
  749:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  750:             }
  751:             $output .= '</td>';
  752:         }
  753:         if ($item->{'header'}->[0]->{'col4'}) {
  754:             $output .= '<td class="LC_right_item" valign="top">'.
  755:                        &mt($item->{'header'}->[0]->{'col4'});
  756:         }
  757:         $output .= '</tr>';
  758:         $rowtotal ++;
  759:         if ($action eq 'quotas') {
  760:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  761:         } elsif ($action eq 'autoenroll') {
  762:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  763:         } elsif ($action eq 'autocreate') {
  764:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  765:         } elsif ($action eq 'directorysrch') {
  766:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  767:         } elsif ($action eq 'contacts') {
  768:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  769:         } elsif ($action eq 'defaults') {
  770:             $output .= &print_defaults($dom,\$rowtotal);
  771:         } elsif ($action eq 'scantron') {
  772:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  773:         } elsif ($action eq 'serverstatuses') {
  774:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  775:         } elsif ($action eq 'helpsettings') {
  776:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  777:         } elsif ($action eq 'loadbalancing') {
  778:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  779:         } elsif ($action eq 'coursedefaults') {
  780:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  781:         }
  782:     }
  783:     $output .= '
  784:    </table>
  785:   </td>
  786:  </tr>
  787: </table><br />';
  788:     return ($output,$rowtotal);
  789: }
  790: 
  791: sub print_login {
  792:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  793:     my ($css_class,$datatable);
  794:     my %choices = &login_choices();
  795: 
  796:     if ($caller eq 'service') {
  797:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  798:         my $choice = $choices{'disallowlogin'};
  799:         $css_class = ' class="LC_odd_row"';
  800:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  801:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  802:                       '<th>'.$choices{'server'}.'</th>'.
  803:                       '<th>'.$choices{'serverpath'}.'</th>'.
  804:                       '<th>'.$choices{'custompath'}.'</th>'.
  805:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  806:         my %disallowed;
  807:         if (ref($settings) eq 'HASH') {
  808:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  809:                %disallowed = %{$settings->{'loginvia'}};
  810:             }
  811:         }
  812:         foreach my $lonhost (sort(keys(%servers))) {
  813:             my $direct = 'selected="selected"';
  814:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  815:                 if ($disallowed{$lonhost}{'server'} ne '') {
  816:                     $direct = '';
  817:                 }
  818:             }
  819:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  820:                           '<td><select name="'.$lonhost.'_server">'.
  821:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  822:                           '</option>';
  823:             foreach my $hostid (sort(keys(%servers))) {
  824:                 next if ($servers{$hostid} eq $servers{$lonhost});
  825:                 my $selected = '';
  826:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  827:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  828:                         $selected = 'selected="selected"';
  829:                     }
  830:                 }
  831:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  832:                               $servers{$hostid}.'</option>';
  833:             }
  834:             $datatable .= '</select></td>'.
  835:                           '<td><select name="'.$lonhost.'_serverpath">';
  836:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  837:                 my $pathname = $path;
  838:                 if ($path eq 'custom') {
  839:                     $pathname = &mt('Custom Path').' ->';
  840:                 }
  841:                 my $selected = '';
  842:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  843:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  844:                         $selected = 'selected="selected"';
  845:                     }
  846:                 } elsif ($path eq '') {
  847:                     $selected = 'selected="selected"';
  848:                 }
  849:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  850:             }
  851:             $datatable .= '</select></td>';
  852:             my ($custom,$exempt);
  853:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  854:                 $custom = $disallowed{$lonhost}{'custompath'};
  855:                 $exempt = $disallowed{$lonhost}{'exempt'};
  856:             }
  857:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  858:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  859:                           '</tr>';
  860:         }
  861:         $datatable .= '</table></td></tr>';
  862:         return $datatable;
  863:     } elsif ($caller eq 'page') {
  864:         my %defaultchecked = ( 
  865:                                'coursecatalog' => 'on',
  866:                                'helpdesk'      => 'on',
  867:                                'adminmail'     => 'off',
  868:                                'newuser'       => 'off',
  869:                              );
  870:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  871:         my (%checkedon,%checkedoff);
  872:         foreach my $item (@toggles) {
  873:             if ($defaultchecked{$item} eq 'on') { 
  874:                 $checkedon{$item} = ' checked="checked" ';
  875:                 $checkedoff{$item} = ' ';
  876:             } elsif ($defaultchecked{$item} eq 'off') {
  877:                 $checkedoff{$item} = ' checked="checked" ';
  878:                 $checkedon{$item} = ' ';
  879:             }
  880:         }
  881:         my @images = ('img','logo','domlogo','login');
  882:         my @logintext = ('textcol','bgcol');
  883:         my @bgs = ('pgbg','mainbg','sidebg');
  884:         my @links = ('link','alink','vlink');
  885:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  886:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  887:         my (%is_custom,%designs);
  888:         my %defaults = (
  889:                        font => $defaultdesign{'login.font'},
  890:                        );
  891:         foreach my $item (@images) {
  892:             $defaults{$item} = $defaultdesign{'login.'.$item};
  893:             $defaults{'showlogo'}{$item} = 1;
  894:         }
  895:         foreach my $item (@bgs) {
  896:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  897:         }
  898:         foreach my $item (@logintext) {
  899:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  900:         }
  901:         foreach my $item (@links) {
  902:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  903:         }
  904:         if (ref($settings) eq 'HASH') {
  905:             foreach my $item (@toggles) {
  906:                 if ($settings->{$item} eq '1') {
  907:                     $checkedon{$item} =  ' checked="checked" ';
  908:                     $checkedoff{$item} = ' ';
  909:                 } elsif ($settings->{$item} eq '0') {
  910:                     $checkedoff{$item} =  ' checked="checked" ';
  911:                     $checkedon{$item} = ' ';
  912:                 }
  913:             }
  914:             foreach my $item (@images) {
  915:                 if (defined($settings->{$item})) {
  916:                     $designs{$item} = $settings->{$item};
  917:                     $is_custom{$item} = 1;
  918:                 }
  919:                 if (defined($settings->{'showlogo'}{$item})) {
  920:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  921:                 }
  922:             }
  923:             foreach my $item (@logintext) {
  924:                 if ($settings->{$item} ne '') {
  925:                     $designs{'logintext'}{$item} = $settings->{$item};
  926:                     $is_custom{$item} = 1;
  927:                 }
  928:             }
  929:             if ($settings->{'font'} ne '') {
  930:                 $designs{'font'} = $settings->{'font'};
  931:                 $is_custom{'font'} = 1;
  932:             }
  933:             foreach my $item (@bgs) {
  934:                 if ($settings->{$item} ne '') {
  935:                     $designs{'bgs'}{$item} = $settings->{$item};
  936:                     $is_custom{$item} = 1;
  937:                 }
  938:             }
  939:             foreach my $item (@links) {
  940:                 if ($settings->{$item} ne '') {
  941:                     $designs{'links'}{$item} = $settings->{$item};
  942:                     $is_custom{$item} = 1;
  943:                 }
  944:             }
  945:         } else {
  946:             if ($designhash{$dom.'.login.font'} ne '') {
  947:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  948:                 $is_custom{'font'} = 1;
  949:             }
  950:             foreach my $item (@images) {
  951:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  952:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  953:                     $is_custom{$item} = 1;
  954:                 }
  955:             }
  956:             foreach my $item (@bgs) {
  957:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  958:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  959:                     $is_custom{$item} = 1;
  960:                 }
  961:             }
  962:             foreach my $item (@links) {
  963:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  964:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  965:                     $is_custom{$item} = 1;
  966:                 }
  967:             }
  968:         }
  969:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  970:                                                       logo => 'Institution Logo',
  971:                                                       domlogo => 'Domain Logo',
  972:                                                       login => 'Login box');
  973:         my $itemcount = 1;
  974:         foreach my $item (@toggles) {
  975:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
  976:             $datatable .=  
  977:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  978:                 '</td><td>'.
  979:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
  980:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
  981:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
  982:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
  983:                 '</tr>';
  984:             $itemcount ++;
  985:         }
  986:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
  987:         $datatable .= '</tr></table></td></tr>';
  988:     } elsif ($caller eq 'help') {
  989:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
  990:         my $switchserver = &check_switchserver($dom,$confname);
  991:         my $itemcount = 1;
  992:         $defaulturl = '/adm/loginproblems.html';
  993:         $defaulttype = 'default';
  994:         %lt = &Apache::lonlocal::texthash (
  995:                      del     => 'Delete?',
  996:                      rep     => 'Replace:',
  997:                      upl     => 'Upload:',
  998:                      default => 'Default',
  999:                      custom  => 'Custom',
 1000:                                              );
 1001:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1002:         my @currlangs;
 1003:         if (ref($settings) eq 'HASH') {
 1004:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1005:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1006:                     next if ($settings->{'helpurl'}{$key} eq '');
 1007:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1008:                     $type{$key} = 'custom';
 1009:                     unless ($key eq 'nolang') {
 1010:                         push(@currlangs,$key);
 1011:                     }
 1012:                 }
 1013:             } elsif ($settings->{'helpurl'} ne '') {
 1014:                 $type{'nolang'} = 'custom';
 1015:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1016:             }
 1017:         }
 1018:         foreach my $lang ('nolang',sort(@currlangs)) {
 1019:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1020:             $datatable .= '<tr'.$css_class.'>';
 1021:             if ($url{$lang} eq '') {
 1022:                 $url{$lang} = $defaulturl;
 1023:             }
 1024:             if ($type{$lang} eq '') {
 1025:                 $type{$lang} = $defaulttype;
 1026:             }
 1027:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1028:             if ($lang eq 'nolang') {
 1029:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1030:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1031:             } else {
 1032:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1033:                                   $langchoices{$lang},
 1034:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1035:             }
 1036:             $datatable .= '</span></td>'."\n".
 1037:                           '<td class="LC_left_item">';
 1038:             if ($type{$lang} eq 'custom') {
 1039:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1040:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1041:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1042:             } else {
 1043:                 $datatable .= $lt{'upl'};
 1044:             }
 1045:             $datatable .='<br />';
 1046:             if ($switchserver) {
 1047:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1048:             } else {
 1049:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1050:             }
 1051:             $datatable .= '</td></tr>';
 1052:             $itemcount ++;
 1053:         }
 1054:         my @addlangs;
 1055:         foreach my $lang (sort(keys(%langchoices))) {
 1056:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1057:             push(@addlangs,$lang);
 1058:         }
 1059:         if (@addlangs > 0) {
 1060:             my %toadd;
 1061:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1062:             $toadd{''} = &mt('Select');
 1063:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1064:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1065:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1066:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1067:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1068:             if ($switchserver) {
 1069:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1070:             } else {
 1071:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1072:             }
 1073:             $datatable .= '</td></tr>';
 1074:             $itemcount ++;
 1075:         }
 1076:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1077:     }
 1078:     return $datatable;
 1079: }
 1080: 
 1081: sub login_choices {
 1082:     my %choices =
 1083:         &Apache::lonlocal::texthash (
 1084:             coursecatalog => 'Display Course/Community Catalog link?',
 1085:             adminmail     => "Display Administrator's E-mail Address?",
 1086:             helpdesk      => 'Display "Contact Helpdesk" link',
 1087:             disallowlogin => "Login page requests redirected",
 1088:             hostid        => "Server",
 1089:             server        => "Redirect to:",
 1090:             serverpath    => "Path",
 1091:             custompath    => "Custom", 
 1092:             exempt        => "Exempt IP(s)",
 1093:             directlogin   => "No redirect",
 1094:             newuser       => "Link to create a user account",
 1095:             img           => "Header",
 1096:             logo          => "Main Logo",
 1097:             domlogo       => "Domain Logo",
 1098:             login         => "Log-in Header", 
 1099:             textcol       => "Text color",
 1100:             bgcol         => "Box color",
 1101:             bgs           => "Background colors",
 1102:             links         => "Link colors",
 1103:             font          => "Font color",
 1104:             pgbg          => "Header",
 1105:             mainbg        => "Page",
 1106:             sidebg        => "Login box",
 1107:             link          => "Link",
 1108:             alink         => "Active link",
 1109:             vlink         => "Visited link",
 1110:         );
 1111:     return %choices;
 1112: }
 1113: 
 1114: sub print_rolecolors {
 1115:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1116:     my %choices = &color_font_choices();
 1117:     my @bgs = ('pgbg','tabbg','sidebg');
 1118:     my @links = ('link','alink','vlink');
 1119:     my @images = ('img');
 1120:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1121:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1122:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1123:     my (%is_custom,%designs);
 1124:     my %defaults = (
 1125:                    img => $defaultdesign{$role.'.img'},
 1126:                    font => $defaultdesign{$role.'.font'},
 1127: 		   fontmenu => $defaultdesign{$role.'.fontmenu'},
 1128:                    );
 1129:     foreach my $item (@bgs) {
 1130:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1131:     }
 1132:     foreach my $item (@links) {
 1133:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1134:     }
 1135:     if (ref($settings) eq 'HASH') {
 1136:         if (ref($settings->{$role}) eq 'HASH') {
 1137:             if ($settings->{$role}->{'img'} ne '') {
 1138:                 $designs{'img'} = $settings->{$role}->{'img'};
 1139:                 $is_custom{'img'} = 1;
 1140:             }
 1141:             if ($settings->{$role}->{'font'} ne '') {
 1142:                 $designs{'font'} = $settings->{$role}->{'font'};
 1143:                 $is_custom{'font'} = 1;
 1144:             }
 1145:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1146:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1147:                 $is_custom{'fontmenu'} = 1;
 1148:             }
 1149:             foreach my $item (@bgs) {
 1150:                 if ($settings->{$role}->{$item} ne '') {
 1151:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1152:                     $is_custom{$item} = 1;
 1153:                 }
 1154:             }
 1155:             foreach my $item (@links) {
 1156:                 if ($settings->{$role}->{$item} ne '') {
 1157:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1158:                     $is_custom{$item} = 1;
 1159:                 }
 1160:             }
 1161:         }
 1162:     } else {
 1163:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1164:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1165:             $is_custom{'img'} = 1;
 1166:         }
 1167:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1168:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1169:             $is_custom{'fontmenu'} = 1; 
 1170:         }
 1171:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1172:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1173:             $is_custom{'font'} = 1;
 1174:         }
 1175:         foreach my $item (@bgs) {
 1176:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1177:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1178:                 $is_custom{$item} = 1;
 1179:             
 1180:             }
 1181:         }
 1182:         foreach my $item (@links) {
 1183:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1184:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1185:                 $is_custom{$item} = 1;
 1186:             }
 1187:         }
 1188:     }
 1189:     my $itemcount = 1;
 1190:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1191:     $datatable .= '</tr></table></td></tr>';
 1192:     return $datatable;
 1193: }
 1194: 
 1195: sub display_color_options {
 1196:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1197:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1198:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1199:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1200:     my $datatable = '<tr'.$css_class.'>'.
 1201:         '<td>'.$choices->{'font'}.'</td>';
 1202:     if (!$is_custom->{'font'}) {
 1203:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1204:     } else {
 1205:         $datatable .= '<td>&nbsp;</td>';
 1206:     }
 1207:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1208: 
 1209:     $datatable .= '<td><span class="LC_nobreak">'.
 1210:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1211:                   ' value="'.$current_color.'" />&nbsp;'.
 1212:                   '&nbsp;</td></tr>';
 1213:     unless ($role eq 'login') { 
 1214:         $datatable .= '<tr'.$css_class.'>'.
 1215:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1216:         if (!$is_custom->{'fontmenu'}) {
 1217:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1218:         } else {
 1219:             $datatable .= '<td>&nbsp;</td>';
 1220:         }
 1221:         $current_color = $designs->{'fontmenu'} ?
 1222:             $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1223:         $datatable .= '<td><span class="LC_nobreak">'.
 1224:                       '<input class="colorchooser" type="text" size="10" name="'
 1225:                       .$role.'_fontmenu"'.
 1226:                       ' value="'.$current_color.'" />&nbsp;'.
 1227:                       '&nbsp;</td></tr>';
 1228:     }
 1229:     my $switchserver = &check_switchserver($dom,$confname);
 1230:     foreach my $img (@{$images}) {
 1231: 	$itemcount ++;
 1232:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1233:         $datatable .= '<tr'.$css_class.'>'.
 1234:                       '<td>'.$choices->{$img};
 1235:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1236:         if ($role eq 'login') {
 1237:             if ($img eq 'login') {
 1238:                 $login_hdr_pick =
 1239:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1240:                 $logincolors =
 1241:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1242:                                             $designs);
 1243:             } elsif ($img ne 'domlogo') {
 1244:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1245:             }
 1246:         }
 1247:         $datatable .= '</td>';
 1248:         if ($designs->{$img} ne '') {
 1249:             $imgfile = $designs->{$img};
 1250: 	    $img_import = ($imgfile =~ m{^/adm/});
 1251:         } else {
 1252:             $imgfile = $defaults->{$img};
 1253:         }
 1254:         if ($imgfile) {
 1255:             my ($showfile,$fullsize);
 1256:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1257:                 my $urldir = $1;
 1258:                 my $filename = $2;
 1259:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1260:                 if (@info) {
 1261:                     my $thumbfile = 'tn-'.$filename;
 1262:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1263:                     if (@thumb) {
 1264:                         $showfile = $urldir.'/'.$thumbfile;
 1265:                     } else {
 1266:                         $showfile = $imgfile;
 1267:                     }
 1268:                 } else {
 1269:                     $showfile = '';
 1270:                 }
 1271:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1272:                 $showfile = $imgfile;
 1273:                 my $imgdir = $1;
 1274:                 my $filename = $2;
 1275:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1276:                     $showfile = "/$imgdir/tn-".$filename;
 1277:                 } else {
 1278:                     my $input = $londocroot.$imgfile;
 1279:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1280:                     if (!-e $output) {
 1281:                         my ($width,$height) = &thumb_dimensions();
 1282:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1283:                         if ($fullwidth ne '' && $fullheight ne '') {
 1284:                             if ($fullwidth > $width && $fullheight > $height) { 
 1285:                                 my $size = $width.'x'.$height;
 1286:                                 system("convert -sample $size $input $output");
 1287:                                 $showfile = "/$imgdir/tn-".$filename;
 1288:                             }
 1289:                         }
 1290:                     }
 1291:                 }
 1292:             }
 1293:             if ($showfile) {
 1294:                 if ($showfile =~ m{^/(adm|res)/}) {
 1295:                     if ($showfile =~ m{^/res/}) {
 1296:                         my $local_showfile =
 1297:                             &Apache::lonnet::filelocation('',$showfile);
 1298:                         &Apache::lonnet::repcopy($local_showfile);
 1299:                     }
 1300:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1301:                 }
 1302:                 if ($imgfile) {
 1303:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1304:                         if ($imgfile =~ m{^/res/}) {
 1305:                             my $local_imgfile =
 1306:                                 &Apache::lonnet::filelocation('',$imgfile);
 1307:                             &Apache::lonnet::repcopy($local_imgfile);
 1308:                         }
 1309:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1310:                     } else {
 1311:                         $fullsize = $imgfile;
 1312:                     }
 1313:                 }
 1314:                 $datatable .= '<td>';
 1315:                 if ($img eq 'login') {
 1316:                     $datatable .= $login_hdr_pick;
 1317:                 } 
 1318:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1319:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1320:             } else {
 1321:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1322:                               &mt('Upload:');
 1323:             }
 1324:         } else {
 1325:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1326:                           &mt('Upload:');
 1327:         }
 1328:         if ($switchserver) {
 1329:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1330:         } else {
 1331:             if ($img ne 'login') { # suppress file selection for Log-in header
 1332:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1333:             }
 1334:         }
 1335:         $datatable .= '</td></tr>';
 1336:     }
 1337:     $itemcount ++;
 1338:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1339:     $datatable .= '<tr'.$css_class.'>'.
 1340:                   '<td>'.$choices->{'bgs'}.'</td>';
 1341:     my $bgs_def;
 1342:     foreach my $item (@{$bgs}) {
 1343:         if (!$is_custom->{$item}) {
 1344:             $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>';
 1345:         }
 1346:     }
 1347:     if ($bgs_def) {
 1348:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1349:     } else {
 1350:         $datatable .= '<td>&nbsp;</td>';
 1351:     }
 1352:     $datatable .= '<td class="LC_right_item">'.
 1353:                   '<table border="0"><tr>';
 1354: 
 1355:     foreach my $item (@{$bgs}) {
 1356:         $datatable .= '<td align="center">';
 1357:         my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1358:         if ($designs->{'bgs'}{$item}) {
 1359:             $datatable .= '&nbsp;';
 1360:         }
 1361:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1362:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1363:     }
 1364:     $datatable .= '</tr></table></td></tr>';
 1365:     $itemcount ++;
 1366:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1367:     $datatable .= '<tr'.$css_class.'>'.
 1368:                   '<td>'.$choices->{'links'}.'</td>';
 1369:     my $links_def;
 1370:     foreach my $item (@{$links}) {
 1371:         if (!$is_custom->{$item}) {
 1372:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1373:         }
 1374:     }
 1375:     if ($links_def) {
 1376:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1377:     } else {
 1378:         $datatable .= '<td>&nbsp;</td>';
 1379:     }
 1380:     $datatable .= '<td class="LC_right_item">'.
 1381:                   '<table border="0"><tr>';
 1382:     foreach my $item (@{$links}) {
 1383:         my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1384:         $datatable .= '<td align="center">'."\n";
 1385: 
 1386:         if ($designs->{'links'}{$item}) {
 1387:             $datatable.='&nbsp;';
 1388:         }
 1389:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1390:                       '" /></td>';
 1391:     }
 1392:     $$rowtotal += $itemcount;
 1393:     return $datatable;
 1394: }
 1395: 
 1396: sub logo_display_options {
 1397:     my ($img,$defaults,$designs) = @_;
 1398:     my $checkedon;
 1399:     if (ref($defaults) eq 'HASH') {
 1400:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1401:             if ($defaults->{'showlogo'}{$img}) {
 1402:                 $checkedon = 'checked="checked" ';     
 1403:             }
 1404:         } 
 1405:     }
 1406:     if (ref($designs) eq 'HASH') {
 1407:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1408:             if (defined($designs->{'showlogo'}{$img})) {
 1409:                 if ($designs->{'showlogo'}{$img} == 0) {
 1410:                     $checkedon = '';
 1411:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1412:                     $checkedon = 'checked="checked" ';
 1413:                 }
 1414:             }
 1415:         }
 1416:     }
 1417:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1418:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1419:            &mt('show').'</label>'."\n";
 1420: }
 1421: 
 1422: sub login_header_options  {
 1423:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1424:     my $output = '';
 1425:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1426:         $output .= &mt('Text default(s):').'<br />';
 1427:         if (!$is_custom->{'textcol'}) {
 1428:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1429:                        '&nbsp;&nbsp;&nbsp;';
 1430:         }
 1431:         if (!$is_custom->{'bgcol'}) {
 1432:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1433:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1434:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1435:         }
 1436:         $output .= '<br />';
 1437:     }
 1438:     $output .='<br />';
 1439:     return $output;
 1440: }
 1441: 
 1442: sub login_text_colors {
 1443:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1444:     my $color_menu = '<table border="0"><tr>';
 1445:     foreach my $item (@{$logintext}) {
 1446:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1447:         $color_menu .= '<td align="center">'.$link;
 1448:         if ($designs->{'logintext'}{$item}) {
 1449:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1450:         }
 1451:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1452:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1453:                        '<td>&nbsp;</td>';
 1454:     }
 1455:     $color_menu .= '</tr></table><br />';
 1456:     return $color_menu;
 1457: }
 1458: 
 1459: sub image_changes {
 1460:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1461:     my $output;
 1462:     if ($img eq 'login') {
 1463:             # suppress image for Log-in header
 1464:     } elsif (!$is_custom) {
 1465:         if ($img ne 'domlogo') {
 1466:             $output .= &mt('Default image:').'<br />';
 1467:         } else {
 1468:             $output .= &mt('Default in use:').'<br />';
 1469:         }
 1470:     }
 1471:     if ($img eq 'login') { # suppress image for Log-in header
 1472:         $output .= '<td>'.$logincolors;
 1473:     } else {
 1474:         if ($img_import) {
 1475:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1476:         }
 1477:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1478:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1479:         if ($is_custom) {
 1480:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1481:                        '<input type="checkbox" name="'.
 1482:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1483:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1484:         } else {
 1485:             $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1486:         }
 1487:     }
 1488:     return $output;
 1489: }
 1490: 
 1491: sub color_pick {
 1492:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1493:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1494:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1495:                ');">'.$desc.'</a>';
 1496:     return $link;
 1497: }
 1498: 
 1499: sub print_quotas {
 1500:     my ($dom,$settings,$rowtotal,$action) = @_;
 1501:     my $context;
 1502:     if ($action eq 'quotas') {
 1503:         $context = 'tools';
 1504:     } else {
 1505:         $context = $action;
 1506:     }
 1507:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 1508:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1509:     my $typecount = 0;
 1510:     my ($css_class,%titles);
 1511:     if ($context eq 'requestcourses') {
 1512:         @usertools = ('official','unofficial','community');
 1513:         @options =('norequest','approval','validate','autolimit');
 1514:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1515:         %titles = &courserequest_titles();
 1516:     } elsif ($context eq 'requestauthor') {
 1517:         @usertools = ('author');
 1518:         @options = ('norequest','approval','automatic');
 1519:         %titles = &authorrequest_titles();
 1520:     } else {
 1521:         @usertools = ('aboutme','blog','webdav','portfolio');
 1522:         %titles = &tool_titles();
 1523:     }
 1524:     if (ref($types) eq 'ARRAY') {
 1525:         foreach my $type (@{$types}) {
 1526:             my $currdefquota;
 1527:             unless (($context eq 'requestcourses') ||
 1528:                     ($context eq 'requestauthor')) {
 1529:                 if (ref($settings) eq 'HASH') {
 1530:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1531:                         $currdefquota = $settings->{defaultquota}->{$type}; 
 1532:                     } else {
 1533:                         $currdefquota = $settings->{$type};
 1534:                     }
 1535:                 }
 1536:             }
 1537:             if (defined($usertypes->{$type})) {
 1538:                 $typecount ++;
 1539:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1540:                 $datatable .= '<tr'.$css_class.'>'.
 1541:                               '<td>'.$usertypes->{$type}.'</td>'.
 1542:                               '<td class="LC_left_item">';
 1543:                 if ($context eq 'requestcourses') {
 1544:                     $datatable .= '<table><tr>';
 1545:                 }
 1546:                 my %cell;  
 1547:                 foreach my $item (@usertools) {
 1548:                     if ($context eq 'requestcourses') {
 1549:                         my ($curroption,$currlimit);
 1550:                         if (ref($settings) eq 'HASH') {
 1551:                             if (ref($settings->{$item}) eq 'HASH') {
 1552:                                 $curroption = $settings->{$item}->{$type};
 1553:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1554:                                     $currlimit = $1; 
 1555:                                 }
 1556:                             }
 1557:                         }
 1558:                         if (!$curroption) {
 1559:                             $curroption = 'norequest';
 1560:                         }
 1561:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1562:                         foreach my $option (@options) {
 1563:                             my $val = $option;
 1564:                             if ($option eq 'norequest') {
 1565:                                 $val = 0;  
 1566:                             }
 1567:                             if ($option eq 'validate') {
 1568:                                 my $canvalidate = 0;
 1569:                                 if (ref($validations{$item}) eq 'HASH') { 
 1570:                                     if ($validations{$item}{$type}) {
 1571:                                         $canvalidate = 1;
 1572:                                     }
 1573:                                 }
 1574:                                 next if (!$canvalidate);
 1575:                             }
 1576:                             my $checked = '';
 1577:                             if ($option eq $curroption) {
 1578:                                 $checked = ' checked="checked"';
 1579:                             } elsif ($option eq 'autolimit') {
 1580:                                 if ($curroption =~ /^autolimit/) {
 1581:                                     $checked = ' checked="checked"';
 1582:                                 }                       
 1583:                             } 
 1584:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1585:                                   '<input type="radio" name="crsreq_'.$item.
 1586:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1587:                                   $titles{$option}.'</label>';
 1588:                             if ($option eq 'autolimit') {
 1589:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1590:                                                 $item.'_limit_'.$type.'" size="1" '.
 1591:                                                 'value="'.$currlimit.'" />';
 1592:                             }
 1593:                             $cell{$item} .= '</span> ';
 1594:                             if ($option eq 'autolimit') {
 1595:                                 $cell{$item} .= $titles{'unlimited'};
 1596:                             }
 1597:                         }
 1598:                     } elsif ($context eq 'requestauthor') {
 1599:                         my $curroption;
 1600:                         if (ref($settings) eq 'HASH') {
 1601:                             $curroption = $settings->{$type};
 1602:                         }
 1603:                         if (!$curroption) {
 1604:                             $curroption = 'norequest';
 1605:                         }
 1606:                         foreach my $option (@options) {
 1607:                             my $val = $option;
 1608:                             if ($option eq 'norequest') {
 1609:                                 $val = 0;
 1610:                             }
 1611:                             my $checked = '';
 1612:                             if ($option eq $curroption) {
 1613:                                 $checked = ' checked="checked"';
 1614:                             }
 1615:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1616:                                   '<input type="radio" name="authorreq_'.$type.
 1617:                                   '" value="'.$val.'"'.$checked.' />'.
 1618:                                   $titles{$option}.'</label></span>&nbsp; ';
 1619:                         }
 1620:                     } else {
 1621:                         my $checked = 'checked="checked" ';
 1622:                         if (ref($settings) eq 'HASH') {
 1623:                             if (ref($settings->{$item}) eq 'HASH') {
 1624:                                 if ($settings->{$item}->{$type} == 0) {
 1625:                                     $checked = '';
 1626:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1627:                                     $checked =  'checked="checked" ';
 1628:                                 }
 1629:                             }
 1630:                         }
 1631:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1632:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1633:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1634:                                       '</label></span>&nbsp; ';
 1635:                     }
 1636:                 }
 1637:                 if ($context eq 'requestcourses') {
 1638:                     $datatable .= '</tr><tr>';
 1639:                     foreach my $item (@usertools) {
 1640:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1641:                     }
 1642:                     $datatable .= '</tr></table>';
 1643:                 }
 1644:                 $datatable .= '</td>';
 1645:                 unless (($context eq 'requestcourses') ||
 1646:                         ($context eq 'requestauthor')) {
 1647:                     $datatable .= 
 1648:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1649:                               '<input type="text" name="quota_'.$type.
 1650:                               '" value="'.$currdefquota.
 1651:                               '" size="5" /> Mb</span></td>';
 1652:                 }
 1653:                 $datatable .= '</tr>';
 1654:             }
 1655:         }
 1656:     }
 1657:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1658:         $defaultquota = '20';
 1659:         if (ref($settings) eq 'HASH') {
 1660:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1661:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1662:             } elsif (defined($settings->{'default'})) {
 1663:                 $defaultquota = $settings->{'default'};
 1664:             }
 1665:         }
 1666:     }
 1667:     $typecount ++;
 1668:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1669:     $datatable .= '<tr'.$css_class.'>'.
 1670:                   '<td>'.$othertitle.'</td>'.
 1671:                   '<td class="LC_left_item">';
 1672:     if ($context eq 'requestcourses') {
 1673:         $datatable .= '<table><tr>';
 1674:     }
 1675:     my %defcell;
 1676:     foreach my $item (@usertools) {
 1677:         if ($context eq 'requestcourses') {
 1678:             my ($curroption,$currlimit);
 1679:             if (ref($settings) eq 'HASH') {
 1680:                 if (ref($settings->{$item}) eq 'HASH') {
 1681:                     $curroption = $settings->{$item}->{'default'};
 1682:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1683:                         $currlimit = $1;
 1684:                     }
 1685:                 }
 1686:             }
 1687:             if (!$curroption) {
 1688:                 $curroption = 'norequest';
 1689:             }
 1690:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1691:             foreach my $option (@options) {
 1692:                 my $val = $option;
 1693:                 if ($option eq 'norequest') {
 1694:                     $val = 0;
 1695:                 }
 1696:                 if ($option eq 'validate') {
 1697:                     my $canvalidate = 0;
 1698:                     if (ref($validations{$item}) eq 'HASH') {
 1699:                         if ($validations{$item}{'default'}) {
 1700:                             $canvalidate = 1;
 1701:                         }
 1702:                     }
 1703:                     next if (!$canvalidate);
 1704:                 }
 1705:                 my $checked = '';
 1706:                 if ($option eq $curroption) {
 1707:                     $checked = ' checked="checked"';
 1708:                 } elsif ($option eq 'autolimit') {
 1709:                     if ($curroption =~ /^autolimit/) {
 1710:                         $checked = ' checked="checked"';
 1711:                     }
 1712:                 }
 1713:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1714:                                   '<input type="radio" name="crsreq_'.$item.
 1715:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1716:                                   $titles{$option}.'</label>';
 1717:                 if ($option eq 'autolimit') {
 1718:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1719:                                        $item.'_limit_default" size="1" '.
 1720:                                        'value="'.$currlimit.'" />';
 1721:                 }
 1722:                 $defcell{$item} .= '</span> ';
 1723:                 if ($option eq 'autolimit') {
 1724:                     $defcell{$item} .= $titles{'unlimited'};
 1725:                 }
 1726:             }
 1727:         } elsif ($context eq 'requestauthor') {
 1728:             my $curroption;
 1729:             if (ref($settings) eq 'HASH') {
 1730:                 $curroption = $settings->{'default'};
 1731:             }
 1732:             if (!$curroption) {
 1733:                 $curroption = 'norequest';
 1734:             }
 1735:             foreach my $option (@options) {
 1736:                 my $val = $option;
 1737:                 if ($option eq 'norequest') {
 1738:                     $val = 0;
 1739:                 }
 1740:                 my $checked = '';
 1741:                 if ($option eq $curroption) {
 1742:                     $checked = ' checked="checked"';
 1743:                 }
 1744:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1745:                               '<input type="radio" name="authorreq_default"'.
 1746:                               ' value="'.$val.'"'.$checked.' />'.
 1747:                               $titles{$option}.'</label></span>&nbsp; ';
 1748:             }
 1749:         } else {
 1750:             my $checked = 'checked="checked" ';
 1751:             if (ref($settings) eq 'HASH') {
 1752:                 if (ref($settings->{$item}) eq 'HASH') {
 1753:                     if ($settings->{$item}->{'default'} == 0) {
 1754:                         $checked = '';
 1755:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1756:                         $checked = 'checked="checked" ';
 1757:                     }
 1758:                 }
 1759:             }
 1760:             $datatable .= '<span class="LC_nobreak"><label>'.
 1761:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1762:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1763:                           '</label></span>&nbsp; ';
 1764:         }
 1765:     }
 1766:     if ($context eq 'requestcourses') {
 1767:         $datatable .= '</tr><tr>';
 1768:         foreach my $item (@usertools) {
 1769:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1770:         }
 1771:         $datatable .= '</tr></table>';
 1772:     }
 1773:     $datatable .= '</td>';
 1774:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1775:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1776:                       '<input type="text" name="defaultquota" value="'.
 1777:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1778:     }
 1779:     $datatable .= '</tr>';
 1780:     $typecount ++;
 1781:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1782:     $datatable .= '<tr'.$css_class.'>'.
 1783:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1784:     if ($context eq 'requestcourses') {
 1785:         $datatable .= &mt('(overrides affiliation, if set)').
 1786:                       '</td>'.
 1787:                       '<td class="LC_left_item">'.
 1788:                       '<table><tr>';
 1789:     } else {
 1790:         $datatable .= &mt('(overrides affiliation, if checked)').
 1791:                       '</td>'.
 1792:                       '<td class="LC_left_item" colspan="2">'.
 1793:                       '<br />';
 1794:     }
 1795:     my %advcell;
 1796:     foreach my $item (@usertools) {
 1797:         if ($context eq 'requestcourses') {
 1798:             my ($curroption,$currlimit);
 1799:             if (ref($settings) eq 'HASH') {
 1800:                 if (ref($settings->{$item}) eq 'HASH') {
 1801:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1802:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1803:                         $currlimit = $1;
 1804:                     }
 1805:                 }
 1806:             }
 1807:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1808:             my $checked = '';
 1809:             if ($curroption eq '') {
 1810:                 $checked = ' checked="checked"';
 1811:             }
 1812:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1813:                                '<input type="radio" name="crsreq_'.$item.
 1814:                                '__LC_adv" value=""'.$checked.' />'.
 1815:                                &mt('No override set').'</label></span>&nbsp; ';
 1816:             foreach my $option (@options) {
 1817:                 my $val = $option;
 1818:                 if ($option eq 'norequest') {
 1819:                     $val = 0;
 1820:                 }
 1821:                 if ($option eq 'validate') {
 1822:                     my $canvalidate = 0;
 1823:                     if (ref($validations{$item}) eq 'HASH') {
 1824:                         if ($validations{$item}{'_LC_adv'}) {
 1825:                             $canvalidate = 1;
 1826:                         }
 1827:                     }
 1828:                     next if (!$canvalidate);
 1829:                 }
 1830:                 my $checked = '';
 1831:                 if ($val eq $curroption) {
 1832:                     $checked = ' checked="checked"';
 1833:                 } elsif ($option eq 'autolimit') {
 1834:                     if ($curroption =~ /^autolimit/) {
 1835:                         $checked = ' checked="checked"';
 1836:                     }
 1837:                 }
 1838:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1839:                                   '<input type="radio" name="crsreq_'.$item.
 1840:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1841:                                   $titles{$option}.'</label>';
 1842:                 if ($option eq 'autolimit') {
 1843:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1844:                                        $item.'_limit__LC_adv" size="1" '.
 1845:                                        'value="'.$currlimit.'" />';
 1846:                 }
 1847:                 $advcell{$item} .= '</span> ';
 1848:                 if ($option eq 'autolimit') {
 1849:                     $advcell{$item} .= $titles{'unlimited'};
 1850:                 }
 1851:             }
 1852:         } elsif ($context eq 'requestauthor') {
 1853:             my $curroption;
 1854:             if (ref($settings) eq 'HASH') {
 1855:                 $curroption = $settings->{'_LC_adv'};
 1856:             }
 1857:             my $checked = '';
 1858:             if ($curroption eq '') {
 1859:                 $checked = ' checked="checked"';
 1860:             }
 1861:             $datatable .= '<span class="LC_nobreak"><label>'.
 1862:                           '<input type="radio" name="authorreq__LC_adv"'.
 1863:                           ' value=""'.$checked.' />'.
 1864:                           &mt('No override set').'</label></span>&nbsp; ';
 1865:             foreach my $option (@options) {
 1866:                 my $val = $option;
 1867:                 if ($option eq 'norequest') {
 1868:                     $val = 0;
 1869:                 }
 1870:                 my $checked = '';
 1871:                 if ($val eq $curroption) {
 1872:                     $checked = ' checked="checked"';
 1873:                 }
 1874:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1875:                               '<input type="radio" name="authorreq__LC_adv"'.
 1876:                               ' value="'.$val.'"'.$checked.' />'.
 1877:                               $titles{$option}.'</label></span>&nbsp; ';
 1878:             }
 1879:         } else {
 1880:             my $checked = 'checked="checked" ';
 1881:             if (ref($settings) eq 'HASH') {
 1882:                 if (ref($settings->{$item}) eq 'HASH') {
 1883:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1884:                         $checked = '';
 1885:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1886:                         $checked = 'checked="checked" ';
 1887:                     }
 1888:                 }
 1889:             }
 1890:             $datatable .= '<span class="LC_nobreak"><label>'.
 1891:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1892:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1893:                           '</label></span>&nbsp; ';
 1894:         }
 1895:     }
 1896:     if ($context eq 'requestcourses') {
 1897:         $datatable .= '</tr><tr>';
 1898:         foreach my $item (@usertools) {
 1899:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1900:         }
 1901:         $datatable .= '</tr></table>';
 1902:     }
 1903:     $datatable .= '</td></tr>';
 1904:     $$rowtotal += $typecount;
 1905:     return $datatable;
 1906: }
 1907: 
 1908: sub print_requestmail {
 1909:     my ($dom,$action,$settings,$rowtotal) = @_;
 1910:     my ($now,$datatable,%currapp,$rows);
 1911:     $now = time;
 1912:     if (ref($settings) eq 'HASH') {
 1913:         if (ref($settings->{'notify'}) eq 'HASH') {
 1914:             if ($settings->{'notify'}{'approval'} ne '') {
 1915:                map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 1916:             }
 1917:         }
 1918:     }
 1919:     my $numinrow = 2;
 1920:     my $css_class = 'class="LC_odd_row"';
 1921:     my $text;
 1922:     if ($action eq 'requestcourses') {
 1923:         $text = &mt('Receive notification of course requests requiring approval');
 1924:     } else {
 1925:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1926:     }
 1927:     $datatable = '<tr '.$css_class.'>'.
 1928:                  ' <td>'.$text.'</td>'.
 1929:                  ' <td class="LC_left_item">';
 1930:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 1931:                                                  'reqapprovalnotify',%currapp);
 1932:     if ($numdc > 0) {
 1933:         $datatable .= $table;
 1934:     } else {
 1935:         $datatable .= &mt('There are no active Domain Coordinators');
 1936:     }
 1937:     $datatable .='</td></tr>';
 1938:     $$rowtotal += $rows;
 1939:     return $datatable;
 1940: }
 1941: 
 1942: sub print_autoenroll {
 1943:     my ($dom,$settings,$rowtotal) = @_;
 1944:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1945:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1946:     if (ref($settings) eq 'HASH') {
 1947:         if (exists($settings->{'run'})) {
 1948:             if ($settings->{'run'} eq '0') {
 1949:                 $runoff = ' checked="checked" ';
 1950:                 $runon = ' ';
 1951:             } else {
 1952:                 $runon = ' checked="checked" ';
 1953:                 $runoff = ' ';
 1954:             }
 1955:         } else {
 1956:             if ($autorun) {
 1957:                 $runon = ' checked="checked" ';
 1958:                 $runoff = ' ';
 1959:             } else {
 1960:                 $runoff = ' checked="checked" ';
 1961:                 $runon = ' ';
 1962:             }
 1963:         }
 1964:         if (exists($settings->{'co-owners'})) {
 1965:             if ($settings->{'co-owners'} eq '0') {
 1966:                 $coownersoff = ' checked="checked" ';
 1967:                 $coownerson = ' ';
 1968:             } else {
 1969:                 $coownerson = ' checked="checked" ';
 1970:                 $coownersoff = ' ';
 1971:             }
 1972:         } else {
 1973:             $coownersoff = ' checked="checked" ';
 1974:             $coownerson = ' ';
 1975:         }
 1976:         if (exists($settings->{'sender_domain'})) {
 1977:             $defdom = $settings->{'sender_domain'};
 1978:         }
 1979:     } else {
 1980:         if ($autorun) {
 1981:             $runon = ' checked="checked" ';
 1982:             $runoff = ' ';
 1983:         } else {
 1984:             $runoff = ' checked="checked" ';
 1985:             $runon = ' ';
 1986:         }
 1987:     }
 1988:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 1989:     my $notif_sender;
 1990:     if (ref($settings) eq 'HASH') {
 1991:         $notif_sender = $settings->{'sender_uname'};
 1992:     }
 1993:     my $datatable='<tr class="LC_odd_row">'.
 1994:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 1995:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1996:                   '<input type="radio" name="autoenroll_run"'.
 1997:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1998:                   '<label><input type="radio" name="autoenroll_run"'.
 1999:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2000:                   '</tr><tr>'.
 2001:                   '<td>'.&mt('Notification messages - sender').
 2002:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2003:                   &mt('username').':&nbsp;'.
 2004:                   '<input type="text" name="sender_uname" value="'.
 2005:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2006:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2007:                   '<tr class="LC_odd_row">'.
 2008:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2009:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2010:                   '<input type="radio" name="autoassign_coowners"'.
 2011:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2012:                   '<label><input type="radio" name="autoassign_coowners"'.
 2013:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2014:                   '</tr>';
 2015:     $$rowtotal += 3;
 2016:     return $datatable;
 2017: }
 2018: 
 2019: sub print_autoupdate {
 2020:     my ($position,$dom,$settings,$rowtotal) = @_;
 2021:     my $datatable;
 2022:     if ($position eq 'top') {
 2023:         my $updateon = ' ';
 2024:         my $updateoff = ' checked="checked" ';
 2025:         my $classlistson = ' ';
 2026:         my $classlistsoff = ' checked="checked" ';
 2027:         if (ref($settings) eq 'HASH') {
 2028:             if ($settings->{'run'} eq '1') {
 2029:                 $updateon = $updateoff;
 2030:                 $updateoff = ' ';
 2031:             }
 2032:             if ($settings->{'classlists'} eq '1') {
 2033:                 $classlistson = $classlistsoff;
 2034:                 $classlistsoff = ' ';
 2035:             }
 2036:         }
 2037:         my %title = (
 2038:                    run => 'Auto-update active?',
 2039:                    classlists => 'Update information in classlists?',
 2040:                     );
 2041:         $datatable = '<tr class="LC_odd_row">'. 
 2042:                   '<td>'.&mt($title{'run'}).'</td>'.
 2043:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2044:                   '<input type="radio" name="autoupdate_run"'.
 2045:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2046:                   '<label><input type="radio" name="autoupdate_run"'.
 2047:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2048:                   '</tr><tr>'.
 2049:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2050:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2051:                   '<label><input type="radio" name="classlists"'.
 2052:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2053:                   '<label><input type="radio" name="classlists"'.
 2054:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2055:                   '</tr>';
 2056:         $$rowtotal += 2;
 2057:     } elsif ($position eq 'middle') {
 2058:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2059:         my $numinrow = 3;
 2060:         my $locknamesettings;
 2061:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2062:                                      $dom,$numinrow,$othertitle,
 2063:                                     'lockablenames');
 2064:         $$rowtotal ++;
 2065:     } else {
 2066:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2067:         my @fields = ('lastname','firstname','middlename','generation',
 2068:                       'permanentemail','id');
 2069:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2070:         my $numrows = 0;
 2071:         if (ref($types) eq 'ARRAY') {
 2072:             if (@{$types} > 0) {
 2073:                 $datatable = 
 2074:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2075:                                          \@fields,$types,\$numrows);
 2076:                     $$rowtotal += @{$types}; 
 2077:             }
 2078:         }
 2079:         $datatable .= 
 2080:             &usertype_update_row($settings,{'default' => $othertitle},
 2081:                                  \%fieldtitles,\@fields,['default'],
 2082:                                  \$numrows);
 2083:         $$rowtotal ++;     
 2084:     }
 2085:     return $datatable;
 2086: }
 2087: 
 2088: sub print_autocreate {
 2089:     my ($dom,$settings,$rowtotal) = @_;
 2090:     my (%createon,%createoff,%currhash);
 2091:     my @types = ('xml','req');
 2092:     if (ref($settings) eq 'HASH') {
 2093:         foreach my $item (@types) {
 2094:             $createoff{$item} = ' checked="checked" ';
 2095:             $createon{$item} = ' ';
 2096:             if (exists($settings->{$item})) {
 2097:                 if ($settings->{$item}) {
 2098:                     $createon{$item} = ' checked="checked" ';
 2099:                     $createoff{$item} = ' ';
 2100:                 }
 2101:             }
 2102:         }
 2103:         if ($settings->{'xmldc'} ne '') {
 2104:             $currhash{$settings->{'xmldc'}} = 1;
 2105:         }
 2106:     } else {
 2107:         foreach my $item (@types) {
 2108:             $createoff{$item} = ' checked="checked" ';
 2109:             $createon{$item} = ' ';
 2110:         }
 2111:     }
 2112:     $$rowtotal += 2;
 2113:     my $numinrow = 2;
 2114:     my $datatable='<tr class="LC_odd_row">'.
 2115:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2116:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2117:                   '<input type="radio" name="autocreate_xml"'.
 2118:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2119:                   '<label><input type="radio" name="autocreate_xml"'.
 2120:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2121:                   '</td></tr><tr>'.
 2122:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2123:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2124:                   '<input type="radio" name="autocreate_req"'.
 2125:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2126:                   '<label><input type="radio" name="autocreate_req"'.
 2127:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2128:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2129:                                                    'autocreate_xmldc',%currhash);
 2130:     if ($numdc > 1) {
 2131:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2132:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2133:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2134:     } else {
 2135:         $datatable .= $dctable.'</td></tr>';
 2136:     }
 2137:     $$rowtotal += $rows;
 2138:     return $datatable;
 2139: }
 2140: 
 2141: sub print_directorysrch {
 2142:     my ($dom,$settings,$rowtotal) = @_;
 2143:     my $srchon = ' ';
 2144:     my $srchoff = ' checked="checked" ';
 2145:     my ($exacton,$containson,$beginson);
 2146:     my $localon = ' ';
 2147:     my $localoff = ' checked="checked" ';
 2148:     if (ref($settings) eq 'HASH') {
 2149:         if ($settings->{'available'} eq '1') {
 2150:             $srchon = $srchoff;
 2151:             $srchoff = ' ';
 2152:         }
 2153:         if ($settings->{'localonly'} eq '1') {
 2154:             $localon = $localoff;
 2155:             $localoff = ' ';
 2156:         }
 2157:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2158:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2159:                 if ($type eq 'exact') {
 2160:                     $exacton = ' checked="checked" ';
 2161:                 } elsif ($type eq 'contains') {
 2162:                     $containson = ' checked="checked" ';
 2163:                 } elsif ($type eq 'begins') {
 2164:                     $beginson = ' checked="checked" ';
 2165:                 }
 2166:             }
 2167:         } else {
 2168:             if ($settings->{'searchtypes'} eq 'exact') {
 2169:                 $exacton = ' checked="checked" ';
 2170:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2171:                 $containson = ' checked="checked" ';
 2172:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2173:                 $exacton = ' checked="checked" ';
 2174:                 $containson = ' checked="checked" ';
 2175:             }
 2176:         }
 2177:     }
 2178:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2179:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2180: 
 2181:     my $numinrow = 4;
 2182:     my $cansrchrow = 0;
 2183:     my $datatable='<tr class="LC_odd_row">'.
 2184:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2185:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2186:                   '<input type="radio" name="dirsrch_available"'.
 2187:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2188:                   '<label><input type="radio" name="dirsrch_available"'.
 2189:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2190:                   '</tr><tr>'.
 2191:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2192:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2193:                   '<input type="radio" name="dirsrch_localonly"'.
 2194:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2195:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2196:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2197:                   '</tr>';
 2198:     $$rowtotal += 2;
 2199:     if (ref($usertypes) eq 'HASH') {
 2200:         if (keys(%{$usertypes}) > 0) {
 2201:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2202:                                          $numinrow,$othertitle,'cansearch');
 2203:             $cansrchrow = 1;
 2204:         }
 2205:     }
 2206:     if ($cansrchrow) {
 2207:         $$rowtotal ++;
 2208:         $datatable .= '<tr>';
 2209:     } else {
 2210:         $datatable .= '<tr class="LC_odd_row">';
 2211:     }
 2212:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2213:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2214:     foreach my $title (@{$titleorder}) {
 2215:         if (defined($searchtitles->{$title})) {
 2216:             my $check = ' ';
 2217:             if (ref($settings) eq 'HASH') {
 2218:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2219:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2220:                         $check = ' checked="checked" ';
 2221:                     }
 2222:                 }
 2223:             }
 2224:             $datatable .= '<td class="LC_left_item">'.
 2225:                           '<span class="LC_nobreak"><label>'.
 2226:                           '<input type="checkbox" name="searchby" '.
 2227:                           'value="'.$title.'"'.$check.'/>'.
 2228:                           $searchtitles->{$title}.'</label></span></td>';
 2229:         }
 2230:     }
 2231:     $datatable .= '</tr></table></td></tr>';
 2232:     $$rowtotal ++;
 2233:     if ($cansrchrow) {
 2234:         $datatable .= '<tr class="LC_odd_row">';
 2235:     } else {
 2236:         $datatable .= '<tr>';
 2237:     }
 2238:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2239:                   '<td class="LC_left_item" colspan="2">'.
 2240:                   '<span class="LC_nobreak"><label>'.
 2241:                   '<input type="checkbox" name="searchtypes" '.
 2242:                   $exacton.' value="exact" />'.&mt('Exact match').
 2243:                   '</label>&nbsp;'.
 2244:                   '<label><input type="checkbox" name="searchtypes" '.
 2245:                   $beginson.' value="begins" />'.&mt('Begins with').
 2246:                   '</label>&nbsp;'.
 2247:                   '<label><input type="checkbox" name="searchtypes" '.
 2248:                   $containson.' value="contains" />'.&mt('Contains').
 2249:                   '</label></span></td></tr>';
 2250:     $$rowtotal ++;
 2251:     return $datatable;
 2252: }
 2253: 
 2254: sub print_contacts {
 2255:     my ($dom,$settings,$rowtotal) = @_;
 2256:     my $datatable;
 2257:     my @contacts = ('adminemail','supportemail');
 2258:     my (%checked,%to,%otheremails,%bccemails);
 2259:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2260:                     'requestsmail','updatesmail');
 2261:     foreach my $type (@mailings) {
 2262:         $otheremails{$type} = '';
 2263:     }
 2264:     $bccemails{'helpdeskmail'} = '';
 2265:     if (ref($settings) eq 'HASH') {
 2266:         foreach my $item (@contacts) {
 2267:             if (exists($settings->{$item})) {
 2268:                 $to{$item} = $settings->{$item};
 2269:             }
 2270:         }
 2271:         foreach my $type (@mailings) {
 2272:             if (exists($settings->{$type})) {
 2273:                 if (ref($settings->{$type}) eq 'HASH') {
 2274:                     foreach my $item (@contacts) {
 2275:                         if ($settings->{$type}{$item}) {
 2276:                             $checked{$type}{$item} = ' checked="checked" ';
 2277:                         }
 2278:                     }
 2279:                     $otheremails{$type} = $settings->{$type}{'others'};
 2280:                     if ($type eq 'helpdeskmail') {
 2281:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2282:                     }
 2283:                 }
 2284:             } elsif ($type eq 'lonstatusmail') {
 2285:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2286:             }
 2287:         }
 2288:     } else {
 2289:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2290:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2291:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2292:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2293:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2294:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2295:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2296:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" '; 
 2297:     }
 2298:     my ($titles,$short_titles) = &contact_titles();
 2299:     my $rownum = 0;
 2300:     my $css_class;
 2301:     foreach my $item (@contacts) {
 2302:         $rownum ++;
 2303:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2304:         $datatable .= '<tr'.$css_class.'>'. 
 2305:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2306:                   '</span></td><td class="LC_right_item">'.
 2307:                   '<input type="text" name="'.$item.'" value="'.
 2308:                   $to{$item}.'" /></td></tr>';
 2309:     }
 2310:     foreach my $type (@mailings) {
 2311:         $rownum ++;
 2312:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2313:         $datatable .= '<tr'.$css_class.'>'.
 2314:                       '<td><span class="LC_nobreak">'.
 2315:                       $titles->{$type}.': </span></td>'.
 2316:                       '<td class="LC_left_item">'.
 2317:                       '<span class="LC_nobreak">';
 2318:         foreach my $item (@contacts) {
 2319:             $datatable .= '<label>'.
 2320:                           '<input type="checkbox" name="'.$type.'"'.
 2321:                           $checked{$type}{$item}.
 2322:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2323:                           '</label>&nbsp;';
 2324:         }
 2325:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2326:                       '<input type="text" name="'.$type.'_others" '.
 2327:                       'value="'.$otheremails{$type}.'"  />';
 2328:         if ($type eq 'helpdeskmail') {
 2329:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2330:                           '<input type="text" name="'.$type.'_bcc" '.
 2331:                           'value="'.$bccemails{$type}.'"  />';
 2332:         }
 2333:         $datatable .= '</td></tr>'."\n";
 2334:     }
 2335:     $$rowtotal += $rownum;
 2336:     return $datatable;
 2337: }
 2338: 
 2339: sub print_helpsettings {
 2340:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2341:     my ($datatable,$itemcount);
 2342:     $itemcount = 1;
 2343:     my (%choices,%defaultchecked,@toggles);
 2344:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2345:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2346:                                  &mt('LON-CAPA bug tracker'),600,500));
 2347:     %defaultchecked = ('submitbugs' => 'on');
 2348:     @toggles = ('submitbugs',);
 2349: 
 2350:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2351:                                                  \%choices,$itemcount);
 2352:     return $datatable;
 2353: }
 2354: 
 2355: sub radiobutton_prefs {
 2356:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2357:         $additional) = @_;
 2358:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2359:                    (ref($choices) eq 'HASH'));
 2360: 
 2361:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2362: 
 2363:     foreach my $item (@{$toggles}) {
 2364:         if ($defaultchecked->{$item} eq 'on') {
 2365:             $checkedon{$item} = ' checked="checked" ';
 2366:             $checkedoff{$item} = ' ';
 2367:         } elsif ($defaultchecked->{$item} eq 'off') {
 2368:             $checkedoff{$item} = ' checked="checked" ';
 2369:             $checkedon{$item} = ' ';
 2370:         }
 2371:     }
 2372:     if (ref($settings) eq 'HASH') {
 2373:         foreach my $item (@{$toggles}) {
 2374:             if ($settings->{$item} eq '1') {
 2375:                 $checkedon{$item} =  ' checked="checked" ';
 2376:                 $checkedoff{$item} = ' ';
 2377:             } elsif ($settings->{$item} eq '0') {
 2378:                 $checkedoff{$item} =  ' checked="checked" ';
 2379:                 $checkedon{$item} = ' ';
 2380:             }
 2381:         }
 2382:     }
 2383:     if ($onclick) {
 2384:         $onclick = ' onclick="'.$onclick.'"';
 2385:     }
 2386:     foreach my $item (@{$toggles}) {
 2387:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2388:         $datatable .=
 2389:             '<tr'.$css_class.'><td valign="top">'.
 2390:             '<span class="LC_nobreak">'.$choices->{$item}.
 2391:             '</span></td>'.
 2392:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2393:             '<label><input type="radio" name="'.
 2394:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2395:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2396:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2397:             '</span>'.$additional.
 2398:             '</td>'.
 2399:             '</tr>';
 2400:         $itemcount ++;
 2401:     }
 2402:     return ($datatable,$itemcount);
 2403: }
 2404: 
 2405: sub print_coursedefaults {
 2406:     my ($position,$dom,$settings,$rowtotal) = @_;
 2407:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2408:     my $itemcount = 1;
 2409:     my %choices =  &Apache::lonlocal::texthash (
 2410:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2411:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2412:         coursecredits        => 'Credits can be specified for courses',
 2413:     );
 2414:     if ($position eq 'top') {
 2415:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2416:         @toggles = ('canuse_pdfforms');
 2417:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2418:                                                  \%choices,$itemcount);
 2419:     } else {
 2420:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2421:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits);
 2422:         my $currusecredits = 0;
 2423:         if (ref($settings) eq 'HASH') {
 2424:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2425:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2426:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2427:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2428:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '')) {
 2429:                     $currusecredits = 1;
 2430:                 }
 2431:             }
 2432:         }
 2433:         if (!$currdefresponder) {
 2434:             $currdefresponder = 10;
 2435:         } elsif ($currdefresponder < 1) {
 2436:             $currdefresponder = 1;
 2437:         }
 2438:         $datatable .=
 2439:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2440:                 $choices{'anonsurvey_threshold'}.
 2441:                 '</span></td>'.
 2442:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2443:                 '<input type="text" name="anonsurvey_threshold"'.
 2444:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2445:                 '</td></tr>'."\n";
 2446:         $itemcount ++;
 2447:         my $onclick = 'toggleCredits(this.form);';
 2448:         my $display = 'none';
 2449:         if ($currusecredits) {
 2450:             $display = 'block';
 2451:         }
 2452:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2453:                          '<span class="LC_nobreak">'.
 2454:                          &mt('Default credits for official courses [_1]',
 2455:                          '<input type="text" name="official_credits" value="'.
 2456:                          $def_official_credits.'" size="3" />').
 2457:                          '</span><br />'.
 2458:                          '<span class="LC_nobreak">'.
 2459:                          &mt('Default credits for unofficial courses [_1]',
 2460:                          '<input type="text" name="unofficial_credits" value="'.
 2461:                          $def_unofficial_credits.'" size="3" />').
 2462:                          '</span></div>'."\n";
 2463:         %defaultchecked = ('coursecredits' => 'off');
 2464:         @toggles = ('coursecredits');
 2465:         my $current = {
 2466:                         'coursecredits' => $currusecredits,
 2467:                       };
 2468:         (my $table,$itemcount) =
 2469:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2470:                                \%choices,$itemcount,$onclick,$additional);
 2471:         $datatable .= $table;
 2472:     }
 2473:     $$rowtotal += $itemcount;
 2474:     return $datatable;
 2475: }
 2476: 
 2477: sub print_usersessions {
 2478:     my ($position,$dom,$settings,$rowtotal) = @_;
 2479:     my ($css_class,$datatable,%checked,%choices);
 2480:     my (%by_ip,%by_location,@intdoms);
 2481:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2482: 
 2483:     my @alldoms = &Apache::lonnet::all_domains();
 2484:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2485:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2486:     my %altids = &id_for_thisdom(%servers);
 2487:     my $itemcount = 1;
 2488:     if ($position eq 'top') {
 2489:         if (keys(%serverhomes) > 1) {
 2490:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2491:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2492:         } else {
 2493:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2494:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2495:         }
 2496:     } else {
 2497:         if (keys(%by_location) == 0) {
 2498:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2499:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2500:         } else {
 2501:             my %lt = &usersession_titles();
 2502:             my $numinrow = 5;
 2503:             my $prefix;
 2504:             my @types;
 2505:             if ($position eq 'bottom') {
 2506:                 $prefix = 'remote';
 2507:                 @types = ('version','excludedomain','includedomain');
 2508:             } else {
 2509:                 $prefix = 'hosted';
 2510:                 @types = ('excludedomain','includedomain');
 2511:             }
 2512:             my (%current,%checkedon,%checkedoff);
 2513:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2514:             my @locations = sort(keys(%by_location));
 2515:             foreach my $type (@types) {
 2516:                 $checkedon{$type} = '';
 2517:                 $checkedoff{$type} = ' checked="checked"';
 2518:             }
 2519:             if (ref($settings) eq 'HASH') {
 2520:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2521:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2522:                         $current{$key} = $settings->{$prefix}{$key};
 2523:                         if ($key eq 'version') {
 2524:                             if ($current{$key} ne '') {
 2525:                                 $checkedon{$key} = ' checked="checked"';
 2526:                                 $checkedoff{$key} = '';
 2527:                             }
 2528:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2529:                             $checkedon{$key} = ' checked="checked"';
 2530:                             $checkedoff{$key} = '';
 2531:                         }
 2532:                     }
 2533:                 }
 2534:             }
 2535:             foreach my $type (@types) {
 2536:                 next if ($type ne 'version' && !@locations);
 2537:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2538:                 $datatable .= '<tr'.$css_class.'>
 2539:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2540:                                <span class="LC_nobreak">&nbsp;
 2541:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2542:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2543:                 if ($type eq 'version') {
 2544:                     my $selector = '<select name="'.$prefix.'_version">';
 2545:                     foreach my $version (@lcversions) {
 2546:                         my $selected = '';
 2547:                         if ($current{'version'} eq $version) {
 2548:                             $selected = ' selected="selected"';
 2549:                         }
 2550:                         $selector .= ' <option value="'.$version.'"'.
 2551:                                      $selected.'>'.$version.'</option>';
 2552:                     }
 2553:                     $selector .= '</select> ';
 2554:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2555:                 } else {
 2556:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2557:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2558:                                  ' />'.('&nbsp;'x2).
 2559:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2560:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2561:                                  "\n".
 2562:                                  '</div><div><table>';
 2563:                     my $rem;
 2564:                     for (my $i=0; $i<@locations; $i++) {
 2565:                         my ($showloc,$value,$checkedtype);
 2566:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2567:                             my $ip = $by_location{$locations[$i]}->[0];
 2568:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2569:                                  $value = join(':',@{$by_ip{$ip}});
 2570:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2571:                                 if (ref($current{$type}) eq 'ARRAY') {
 2572:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2573:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2574:                                             $checkedtype = ' checked="checked"';
 2575:                                             last;
 2576:                                         }
 2577:                                     }
 2578:                                 }
 2579:                             }
 2580:                         }
 2581:                         $rem = $i%($numinrow);
 2582:                         if ($rem == 0) {
 2583:                             if ($i > 0) {
 2584:                                 $datatable .= '</tr>';
 2585:                             }
 2586:                             $datatable .= '<tr>';
 2587:                         }
 2588:                         $datatable .= '<td class="LC_left_item">'.
 2589:                                       '<span class="LC_nobreak"><label>'.
 2590:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2591:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2592:                                       '</label></span></td>';
 2593:                     }
 2594:                     $rem = @locations%($numinrow);
 2595:                     my $colsleft = $numinrow - $rem;
 2596:                     if ($colsleft > 1 ) {
 2597:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2598:                                       '&nbsp;</td>';
 2599:                     } elsif ($colsleft == 1) {
 2600:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2601:                     }
 2602:                     $datatable .= '</tr></table>';
 2603:                 }
 2604:                 $datatable .= '</td></tr>';
 2605:                 $itemcount ++;
 2606:             }
 2607:         }
 2608:     }
 2609:     $$rowtotal += $itemcount;
 2610:     return $datatable;
 2611: }
 2612: 
 2613: sub build_location_hashes {
 2614:     my ($intdoms,$by_ip,$by_location) = @_;
 2615:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2616:                   (ref($by_location) eq 'HASH')); 
 2617:     my %iphost = &Apache::lonnet::get_iphost();
 2618:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2619:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2620:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2621:         foreach my $id (@{$iphost{$primary_ip}}) {
 2622:             my $intdom = &Apache::lonnet::internet_dom($id);
 2623:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2624:                 push(@{$intdoms},$intdom);
 2625:             }
 2626:         }
 2627:     }
 2628:     foreach my $ip (keys(%iphost)) {
 2629:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2630:             foreach my $id (@{$iphost{$ip}}) {
 2631:                 my $location = &Apache::lonnet::internet_dom($id);
 2632:                 if ($location) {
 2633:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2634:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2635:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2636:                             push(@{$by_ip->{$ip}},$location);
 2637:                         }
 2638:                     } else {
 2639:                         $by_ip->{$ip} = [$location];
 2640:                     }
 2641:                 }
 2642:             }
 2643:         }
 2644:     }
 2645:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2646:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2647:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2648:             my $first = $by_ip->{$ip}->[0];
 2649:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2650:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2651:                     push(@{$by_location->{$first}},$ip);
 2652:                 }
 2653:             } else {
 2654:                 $by_location->{$first} = [$ip];
 2655:             }
 2656:         }
 2657:     }
 2658:     return;
 2659: }
 2660: 
 2661: sub current_offloads_to {
 2662:     my ($dom,$settings,$servers) = @_;
 2663:     my (%spareid,%otherdomconfigs);
 2664:     if (ref($servers) eq 'HASH') {
 2665:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2666:             my $gotspares;
 2667:             if (ref($settings) eq 'HASH') {
 2668:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2669:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2670:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2671:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2672:                         $gotspares = 1;
 2673:                     }
 2674:                 }
 2675:             }
 2676:             unless ($gotspares) {
 2677:                 my $gotspares;
 2678:                 my $serverhomeID =
 2679:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2680:                 my $serverhomedom =
 2681:                     &Apache::lonnet::host_domain($serverhomeID);
 2682:                 if ($serverhomedom ne $dom) {
 2683:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2684:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2685:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2686:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2687:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2688:                                 $gotspares = 1;
 2689:                             }
 2690:                         }
 2691:                     } else {
 2692:                         $otherdomconfigs{$serverhomedom} =
 2693:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2694:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2695:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2696:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2697:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2698:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2699:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2700:                                         $gotspares = 1;
 2701:                                     }
 2702:                                 }
 2703:                             }
 2704:                         }
 2705:                     }
 2706:                 }
 2707:             }
 2708:             unless ($gotspares) {
 2709:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2710:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2711:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2712:                } else {
 2713:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2714:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2715:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2716:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2717:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2718:                     } else {
 2719:                         my %what = (
 2720:                              spareid => 1,
 2721:                         );
 2722:                         my ($result,$returnhash) = 
 2723:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2724:                         if ($result eq 'ok') { 
 2725:                             if (ref($returnhash) eq 'HASH') {
 2726:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2727:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2728:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2729:                                 }
 2730:                             }
 2731:                         }
 2732:                     }
 2733:                 }
 2734:             }
 2735:         }
 2736:     }
 2737:     return %spareid;
 2738: }
 2739: 
 2740: sub spares_row {
 2741:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2742:     my $css_class;
 2743:     my $numinrow = 4;
 2744:     my $itemcount = 1;
 2745:     my $datatable;
 2746:     my %typetitles = &sparestype_titles();
 2747:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2748:         foreach my $server (sort(keys(%{$servers}))) {
 2749:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2750:             my ($othercontrol,$serverdom);
 2751:             if ($serverhome ne $server) {
 2752:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2753:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2754:             } else {
 2755:                 $serverdom = &Apache::lonnet::host_domain($server);
 2756:                 if ($serverdom ne $dom) {
 2757:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2758:                 }
 2759:             }
 2760:             next unless (ref($spareid->{$server}) eq 'HASH');
 2761:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2762:             $datatable .= '<tr'.$css_class.'>
 2763:                            <td rowspan="2">
 2764:                             <span class="LC_nobreak">'.
 2765:                           &mt('[_1] when busy, offloads to:'
 2766:                               ,'<b>'.$server.'</b>').
 2767:                           "\n";
 2768:             my (%current,%canselect);
 2769:             my @choices = 
 2770:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2771:             foreach my $type ('primary','default') {
 2772:                 if (ref($spareid->{$server}) eq 'HASH') {
 2773:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2774:                         my @spares = @{$spareid->{$server}{$type}};
 2775:                         if (@spares > 0) {
 2776:                             if ($othercontrol) {
 2777:                                 $current{$type} = join(', ',@spares);
 2778:                             } else {
 2779:                                 $current{$type} .= '<table>';
 2780:                                 my $numspares = scalar(@spares);
 2781:                                 for (my $i=0;  $i<@spares; $i++) {
 2782:                                     my $rem = $i%($numinrow);
 2783:                                     if ($rem == 0) {
 2784:                                         if ($i > 0) {
 2785:                                             $current{$type} .= '</tr>';
 2786:                                         }
 2787:                                         $current{$type} .= '<tr>';
 2788:                                     }
 2789:                                     $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;'.
 2790:                                                        $spareid->{$server}{$type}[$i].
 2791:                                                        '</label></td>'."\n";
 2792:                                 }
 2793:                                 my $rem = @spares%($numinrow);
 2794:                                 my $colsleft = $numinrow - $rem;
 2795:                                 if ($colsleft > 1 ) {
 2796:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2797:                                                        '" class="LC_left_item">'.
 2798:                                                        '&nbsp;</td>';
 2799:                                 } elsif ($colsleft == 1) {
 2800:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2801:                                 }
 2802:                                 $current{$type} .= '</tr></table>';
 2803:                             }
 2804:                         }
 2805:                     }
 2806:                     if ($current{$type} eq '') {
 2807:                         $current{$type} = &mt('None specified');
 2808:                     }
 2809:                     if ($othercontrol) {
 2810:                         if ($type eq 'primary') {
 2811:                             $canselect{$type} = $othercontrol;
 2812:                         }
 2813:                     } else {
 2814:                         $canselect{$type} = 
 2815:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2816:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2817:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2818:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2819:                         if (@choices > 0) {
 2820:                             foreach my $lonhost (@choices) {
 2821:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2822:                             }
 2823:                         }
 2824:                         $canselect{$type} .= '</select>'."\n";
 2825:                     }
 2826:                 } else {
 2827:                     $current{$type} = &mt('Could not be determined');
 2828:                     if ($type eq 'primary') {
 2829:                         $canselect{$type} =  $othercontrol;
 2830:                     }
 2831:                 }
 2832:                 if ($type eq 'default') {
 2833:                     $datatable .= '<tr'.$css_class.'>';
 2834:                 }
 2835:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2836:                               '<td>'.$current{$type}.'</td>'."\n".
 2837:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2838:             }
 2839:             $itemcount ++;
 2840:         }
 2841:     }
 2842:     $$rowtotal += $itemcount;
 2843:     return $datatable;
 2844: }
 2845: 
 2846: sub possible_newspares {
 2847:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2848:     my $serverhostname = &Apache::lonnet::hostname($server);
 2849:     my %excluded;
 2850:     if ($serverhostname ne '') {
 2851:         %excluded = (
 2852:                        $serverhostname => 1,
 2853:                     );
 2854:     }
 2855:     if (ref($currspares) eq 'HASH') {
 2856:         foreach my $type (keys(%{$currspares})) {
 2857:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2858:                 if (@{$currspares->{$type}} > 0) {
 2859:                     foreach my $curr (@{$currspares->{$type}}) {
 2860:                         my $hostname = &Apache::lonnet::hostname($curr);
 2861:                         $excluded{$hostname} = 1;
 2862:                     }
 2863:                 }
 2864:             }
 2865:         }
 2866:     }
 2867:     my @choices;
 2868:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2869:         if (keys(%{$serverhomes}) > 1) {
 2870:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2871:                 unless ($excluded{$name}) {
 2872:                     if (exists($altids->{$serverhomes->{$name}})) {
 2873:                         push(@choices,$altids->{$serverhomes->{$name}});
 2874:                     } else {
 2875:                         push(@choices,$serverhomes->{$name});
 2876:                     }
 2877:                 }
 2878:             }
 2879:         }
 2880:     }
 2881:     return sort(@choices);
 2882: }
 2883: 
 2884: sub print_loadbalancing {
 2885:     my ($dom,$settings,$rowtotal) = @_;
 2886:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2887:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2888:     my $numinrow = 1;
 2889:     my $datatable;
 2890:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2891:     my (%currbalancer,%currtargets,%currrules,%existing);
 2892:     if (ref($settings) eq 'HASH') {
 2893:         %existing = %{$settings};
 2894:     }
 2895:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 2896:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 2897:                                   \%currtargets,\%currrules);
 2898:     } else {
 2899:         return;
 2900:     }
 2901:     my ($othertitle,$usertypes,$types) =
 2902:         &Apache::loncommon::sorted_inst_types($dom);
 2903:     my $rownum = 6;
 2904:     if (ref($types) eq 'ARRAY') {
 2905:         $rownum += scalar(@{$types});
 2906:     }
 2907:     my @css_class = ('LC_odd_row','LC_even_row');
 2908:     my $balnum = 0;
 2909:     my $islast;
 2910:     my (@toshow,$disabledtext);
 2911:     if (keys(%currbalancer) > 0) {
 2912:         @toshow = sort(keys(%currbalancer));
 2913:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 2914:             push(@toshow,'');
 2915:         }
 2916:     } else {
 2917:         @toshow = ('');
 2918:         $disabledtext = &mt('No existing load balancer');
 2919:     }
 2920:     foreach my $lonhost (@toshow) {
 2921:         if ($balnum == scalar(@toshow)-1) {
 2922:             $islast = 1;
 2923:         } else {
 2924:             $islast = 0;
 2925:         }
 2926:         my $cssidx = $balnum%2;
 2927:         my $targets_div_style = 'display: none';
 2928:         my $disabled_div_style = 'display: block';
 2929:         my $homedom_div_style = 'display: none';
 2930:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 2931:                       '<td rowspan="'.$rownum.'" valign="top">'.
 2932:                       '<p>';
 2933:         if ($lonhost eq '') {
 2934:             $datatable .= '<span class="LC_nobreak">';
 2935:             if (keys(%currbalancer) > 0) {
 2936:                 $datatable .= &mt('Add balancer:');
 2937:             } else {
 2938:                 $datatable .= &mt('Enable balancer:');
 2939:             }
 2940:             $datatable .= '&nbsp;'.
 2941:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 2942:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 2943:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 2944:                           '<option value="" selected="selected">'.&mt('None').
 2945:                           '</option>'."\n";
 2946:             foreach my $server (sort(keys(%servers))) {
 2947:                 next if ($currbalancer{$server});
 2948:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 2949:             }
 2950:             $datatable .=
 2951:                 '</select>'."\n".
 2952:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 2953:         } else {
 2954:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 2955:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 2956:                            &mt('Stop balancing').'</label>'.
 2957:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 2958:             $targets_div_style = 'display: block';
 2959:             $disabled_div_style = 'display: none';
 2960:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 2961:                 $homedom_div_style = 'display: block';
 2962:             }
 2963:         }
 2964:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 2965:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 2966:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 2967:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2968:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 2969:         my @sparestypes = ('primary','default');
 2970:         my %typetitles = &sparestype_titles();
 2971:         foreach my $sparetype (@sparestypes) {
 2972:             my $targettable;
 2973:             for (my $i=0; $i<$numspares; $i++) {
 2974:                 my $checked;
 2975:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 2976:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 2977:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 2978:                             $checked = ' checked="checked"';
 2979:                         }
 2980:                     }
 2981:                 }
 2982:                 my ($chkboxval,$disabled);
 2983:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 2984:                     $chkboxval = $spares[$i];
 2985:                 }
 2986:                 if (exists($currbalancer{$spares[$i]})) {
 2987:                     $disabled = ' disabled="disabled"';
 2988:                 }
 2989:                 $targettable .=
 2990:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 2991:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 2992:                     '</span></label></td>';
 2993:                 my $rem = $i%($numinrow);
 2994:                 if ($rem == 0) {
 2995:                     if (($i > 0) && ($i < $numspares-1)) {
 2996:                         $targettable .= '</tr>';
 2997:                     }
 2998:                     if ($i < $numspares-1) {
 2999:                         $targettable .= '<tr>';
 3000:                     }
 3001:                 }
 3002:             }
 3003:             if ($targettable ne '') {
 3004:                 my $rem = $numspares%($numinrow);
 3005:                 my $colsleft = $numinrow - $rem;
 3006:                 if ($colsleft > 1 ) {
 3007:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3008:                                     '&nbsp;</td>';
 3009:                 } elsif ($colsleft == 1) {
 3010:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3011:                 }
 3012:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3013:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3014:             }
 3015:         }
 3016:         $datatable .= '</div></td></tr>'.
 3017:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3018:                                            $othertitle,$usertypes,$types,\%servers,
 3019:                                            \%currbalancer,$lonhost,
 3020:                                            $targets_div_style,$homedom_div_style,
 3021:                                            $css_class[$cssidx],$balnum,$islast);
 3022:         $$rowtotal += $rownum;
 3023:         $balnum ++;
 3024:     }
 3025:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3026:     return $datatable;
 3027: }
 3028: 
 3029: sub get_loadbalancers_config {
 3030:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3031:     return unless ((ref($servers) eq 'HASH') &&
 3032:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3033:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3034:     if (keys(%{$existing}) > 0) {
 3035:         my $oldlonhost;
 3036:         foreach my $key (sort(keys(%{$existing}))) {
 3037:             if ($key eq 'lonhost') {
 3038:                 $oldlonhost = $existing->{'lonhost'};
 3039:                 $currbalancer->{$oldlonhost} = 1;
 3040:             } elsif ($key eq 'targets') {
 3041:                 if ($oldlonhost) {
 3042:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3043:                 }
 3044:             } elsif ($key eq 'rules') {
 3045:                 if ($oldlonhost) {
 3046:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3047:                 }
 3048:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3049:                 $currbalancer->{$key} = 1;
 3050:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3051:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3052:             }
 3053:         }
 3054:     } else {
 3055:         my ($balancerref,$targetsref) =
 3056:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3057:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3058:             foreach my $server (sort(keys(%{$balancerref}))) {
 3059:                 $currbalancer->{$server} = 1;
 3060:                 $currtargets->{$server} = $targetsref->{$server};
 3061:             }
 3062:         }
 3063:     }
 3064:     return;
 3065: }
 3066: 
 3067: sub loadbalancing_rules {
 3068:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3069:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3070:         $css_class,$balnum,$islast) = @_;
 3071:     my $output;
 3072:     my $num = 0;
 3073:     my ($alltypes,$othertypes,$titles) =
 3074:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3075:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3076:         foreach my $type (@{$alltypes}) {
 3077:             $num ++;
 3078:             my $current;
 3079:             if (ref($currrules) eq 'HASH') {
 3080:                 $current = $currrules->{$type};
 3081:             }
 3082:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3083:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3084:                     $current = '';
 3085:                 }
 3086:             }
 3087:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3088:                                              $servers,$currbalancer,$lonhost,$dom,
 3089:                                              $targets_div_style,$homedom_div_style,
 3090:                                              $css_class,$balnum,$num,$islast);
 3091:         }
 3092:     }
 3093:     return $output;
 3094: }
 3095: 
 3096: sub loadbalancing_titles {
 3097:     my ($dom,$intdom,$usertypes,$types) = @_;
 3098:     my %othertypes = (
 3099:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3100:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3101:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3102:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3103:                      );
 3104:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 3105:     if (ref($types) eq 'ARRAY') {
 3106:         unshift(@alltypes,@{$types},'default');
 3107:     }
 3108:     my %titles;
 3109:     foreach my $type (@alltypes) {
 3110:         if ($type =~ /^_LC_/) {
 3111:             $titles{$type} = $othertypes{$type};
 3112:         } elsif ($type eq 'default') {
 3113:             $titles{$type} = &mt('All users from [_1]',$dom);
 3114:             if (ref($types) eq 'ARRAY') {
 3115:                 if (@{$types} > 0) {
 3116:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3117:                 }
 3118:             }
 3119:         } elsif (ref($usertypes) eq 'HASH') {
 3120:             $titles{$type} = $usertypes->{$type};
 3121:         }
 3122:     }
 3123:     return (\@alltypes,\%othertypes,\%titles);
 3124: }
 3125: 
 3126: sub loadbalance_rule_row {
 3127:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3128:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3129:     my @rulenames = ('default','homeserver');
 3130:     my %ruletitles = &offloadtype_text();
 3131:     if ($type eq '_LC_external') {
 3132:         push(@rulenames,'externalbalancer');
 3133:     } else {
 3134:         push(@rulenames,'specific');
 3135:     }
 3136:     push(@rulenames,'none');
 3137:     my $style = $targets_div_style;
 3138:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3139:         $style = $homedom_div_style;
 3140:     }
 3141:     my $space;
 3142:     if ($islast && $num == 1) {
 3143:         $space = '<div display="inline-block">&nbsp;</div>';
 3144:     }
 3145:     my $output =
 3146:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3147:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3148:         '<td valaign="top">'.$space.
 3149:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3150:     for (my $i=0; $i<@rulenames; $i++) {
 3151:         my $rule = $rulenames[$i];
 3152:         my ($checked,$extra);
 3153:         if ($rulenames[$i] eq 'default') {
 3154:             $rule = '';
 3155:         }
 3156:         if ($rulenames[$i] eq 'specific') {
 3157:             if (ref($servers) eq 'HASH') {
 3158:                 my $default;
 3159:                 if (($current ne '') && (exists($servers->{$current}))) {
 3160:                     $checked = ' checked="checked"';
 3161:                 }
 3162:                 unless ($checked) {
 3163:                     $default = ' selected="selected"';
 3164:                 }
 3165:                 $extra =
 3166:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3167:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3168:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3169:                     '<option value=""'.$default.'></option>'."\n";
 3170:                 foreach my $server (sort(keys(%{$servers}))) {
 3171:                     if (ref($currbalancer) eq 'HASH') {
 3172:                         next if (exists($currbalancer->{$server}));
 3173:                     }
 3174:                     my $selected;
 3175:                     if ($server eq $current) {
 3176:                         $selected = ' selected="selected"';
 3177:                     }
 3178:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3179:                 }
 3180:                 $extra .= '</select>';
 3181:             }
 3182:         } elsif ($rule eq $current) {
 3183:             $checked = ' checked="checked"';
 3184:         }
 3185:         $output .= '<span class="LC_nobreak"><label>'.
 3186:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3187:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3188:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3189:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3190:                    '</label>'.$extra.'</span><br />'."\n";
 3191:     }
 3192:     $output .= '</div></td></tr>'."\n";
 3193:     return $output;
 3194: }
 3195: 
 3196: sub offloadtype_text {
 3197:     my %ruletitles = &Apache::lonlocal::texthash (
 3198:            'default'          => 'Offloads to default destinations',
 3199:            'homeserver'       => "Offloads to user's home server",
 3200:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3201:            'specific'         => 'Offloads to specific server',
 3202:            'none'             => 'No offload',
 3203:     );
 3204:     return %ruletitles;
 3205: }
 3206: 
 3207: sub sparestype_titles {
 3208:     my %typestitles = &Apache::lonlocal::texthash (
 3209:                           'primary' => 'primary',
 3210:                           'default' => 'default',
 3211:                       );
 3212:     return %typestitles;
 3213: }
 3214: 
 3215: sub contact_titles {
 3216:     my %titles = &Apache::lonlocal::texthash (
 3217:                    'supportemail' => 'Support E-mail address',
 3218:                    'adminemail'   => 'Default Server Admin E-mail address',
 3219:                    'errormail'    => 'Error reports to be e-mailed to',
 3220:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3221:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3222:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3223:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3224:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3225:                  );
 3226:     my %short_titles = &Apache::lonlocal::texthash (
 3227:                            adminemail   => 'Admin E-mail address',
 3228:                            supportemail => 'Support E-mail',
 3229:                        );   
 3230:     return (\%titles,\%short_titles);
 3231: }
 3232: 
 3233: sub tool_titles {
 3234:     my %titles = &Apache::lonlocal::texthash (
 3235:                      aboutme    => 'Personal web page',
 3236:                      blog       => 'Blog',
 3237:                      webdav     => 'WebDAV',
 3238:                      portfolio  => 'Portfolio',
 3239:                      official   => 'Official courses (with institutional codes)',
 3240:                      unofficial => 'Unofficial courses',
 3241:                      community  => 'Communities',
 3242:                  );
 3243:     return %titles;
 3244: }
 3245: 
 3246: sub courserequest_titles {
 3247:     my %titles = &Apache::lonlocal::texthash (
 3248:                                    official   => 'Official',
 3249:                                    unofficial => 'Unofficial',
 3250:                                    community  => 'Communities',
 3251:                                    norequest  => 'Not allowed',
 3252:                                    approval   => 'Approval by Dom. Coord.',
 3253:                                    validate   => 'With validation',
 3254:                                    autolimit  => 'Numerical limit',
 3255:                                    unlimited  => '(blank for unlimited)',
 3256:                  );
 3257:     return %titles;
 3258: }
 3259: 
 3260: sub authorrequest_titles {
 3261:     my %titles = &Apache::lonlocal::texthash (
 3262:                                    norequest  => 'Not allowed',
 3263:                                    approval   => 'Approval by Dom. Coord.',
 3264:                                    automatic  => 'Automatic approval',
 3265:                  );
 3266:     return %titles;
 3267: }
 3268: 
 3269: sub courserequest_conditions {
 3270:     my %conditions = &Apache::lonlocal::texthash (
 3271:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3272:        validate   => '(Processing of request subject to instittutional validation).',
 3273:                  );
 3274:     return %conditions;
 3275: }
 3276: 
 3277: 
 3278: sub print_usercreation {
 3279:     my ($position,$dom,$settings,$rowtotal) = @_;
 3280:     my $numinrow = 4;
 3281:     my $datatable;
 3282:     if ($position eq 'top') {
 3283:         $$rowtotal ++;
 3284:         my $rowcount = 0;
 3285:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3286:         if (ref($rules) eq 'HASH') {
 3287:             if (keys(%{$rules}) > 0) {
 3288:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3289:                                                 $ruleorder,$numinrow,$rowcount);
 3290:                 $$rowtotal ++;
 3291:                 $rowcount ++;
 3292:             }
 3293:         }
 3294:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3295:         if (ref($idrules) eq 'HASH') {
 3296:             if (keys(%{$idrules}) > 0) {
 3297:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3298:                                                 $idruleorder,$numinrow,$rowcount);
 3299:                 $$rowtotal ++;
 3300:                 $rowcount ++;
 3301:             }
 3302:         }
 3303:         my ($emailrules,$emailruleorder) = 
 3304:             &Apache::lonnet::inst_userrules($dom,'email');
 3305:         if (ref($emailrules) eq 'HASH') {
 3306:             if (keys(%{$emailrules}) > 0) {
 3307:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3308:                                                 $emailruleorder,$numinrow,$rowcount);
 3309:                 $$rowtotal ++;
 3310:                 $rowcount ++;
 3311:             }
 3312:         }
 3313:         if ($rowcount == 0) {
 3314:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3315:             $$rowtotal ++;
 3316:             $rowcount ++;
 3317:         }
 3318:     } elsif ($position eq 'middle') {
 3319:         my @creators = ('author','course','requestcrs','selfcreate');
 3320:         my ($rules,$ruleorder) =
 3321:             &Apache::lonnet::inst_userrules($dom,'username');
 3322:         my %lt = &usercreation_types();
 3323:         my %checked;
 3324:         my @selfcreate; 
 3325:         if (ref($settings) eq 'HASH') {
 3326:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3327:                 foreach my $item (@creators) {
 3328:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3329:                 }
 3330:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3331:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3332:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3333:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3334:                         @selfcreate = ('email','login','sso');
 3335:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3336:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3337:                     }
 3338:                 }
 3339:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3340:                 foreach my $item (@creators) {
 3341:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3342:                         $checked{$item} = 'none';
 3343:                     }
 3344:                 }
 3345:             }
 3346:         }
 3347:         my $rownum = 0;
 3348:         foreach my $item (@creators) {
 3349:             $rownum ++;
 3350:             if ($item ne 'selfcreate') {  
 3351:                 if ($checked{$item} eq '') {
 3352:                     $checked{$item} = 'any';
 3353:                 }
 3354:             }
 3355:             my $css_class;
 3356:             if ($rownum%2) {
 3357:                 $css_class = '';
 3358:             } else {
 3359:                 $css_class = ' class="LC_odd_row" ';
 3360:             }
 3361:             $datatable .= '<tr'.$css_class.'>'.
 3362:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3363:                          '</span></td><td align="right">';
 3364:             my @options;
 3365:             if ($item eq 'selfcreate') {
 3366:                 push(@options,('email','login','sso'));
 3367:             } else {
 3368:                 @options = ('any');
 3369:                 if (ref($rules) eq 'HASH') {
 3370:                     if (keys(%{$rules}) > 0) {
 3371:                         push(@options,('official','unofficial'));
 3372:                     }
 3373:                 }
 3374:                 push(@options,'none');
 3375:             }
 3376:             foreach my $option (@options) {
 3377:                 my $type = 'radio';
 3378:                 my $check = ' ';
 3379:                 if ($item eq 'selfcreate') {
 3380:                     $type = 'checkbox';
 3381:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3382:                         $check = ' checked="checked" ';
 3383:                     }
 3384:                 } else {
 3385:                     if ($checked{$item} eq $option) {
 3386:                         $check = ' checked="checked" ';
 3387:                     }
 3388:                 } 
 3389:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3390:                               '<input type="'.$type.'" name="can_createuser_'.
 3391:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3392:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3393:             }
 3394:             $datatable .= '</td></tr>';
 3395:         }
 3396:         my ($othertitle,$usertypes,$types) =
 3397:             &Apache::loncommon::sorted_inst_types($dom);
 3398:         my $createsettings;
 3399:         if (ref($settings) eq 'HASH') {
 3400:             $createsettings = $settings->{cancreate};
 3401:         }
 3402:         if (ref($usertypes) eq 'HASH') {
 3403:             if (keys(%{$usertypes}) > 0) {
 3404:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3405:                                              $dom,$numinrow,$othertitle,
 3406:                                              'statustocreate');
 3407:                 $$rowtotal ++;
 3408:                 $rownum ++;
 3409:             }
 3410:         }
 3411:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3412:     } else {
 3413:         my @contexts = ('author','course','domain');
 3414:         my @authtypes = ('int','krb4','krb5','loc');
 3415:         my %checked;
 3416:         if (ref($settings) eq 'HASH') {
 3417:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3418:                 foreach my $item (@contexts) {
 3419:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3420:                         foreach my $auth (@authtypes) {
 3421:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3422:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3423:                             }
 3424:                         }
 3425:                     }
 3426:                 }
 3427:             }
 3428:         } else {
 3429:             foreach my $item (@contexts) {
 3430:                 foreach my $auth (@authtypes) {
 3431:                     $checked{$item}{$auth} = ' checked="checked" ';
 3432:                 }
 3433:             }
 3434:         }
 3435:         my %title = &context_names();
 3436:         my %authname = &authtype_names();
 3437:         my $rownum = 0;
 3438:         my $css_class; 
 3439:         foreach my $item (@contexts) {
 3440:             if ($rownum%2) {
 3441:                 $css_class = '';
 3442:             } else {
 3443:                 $css_class = ' class="LC_odd_row" ';
 3444:             }
 3445:             $datatable .=   '<tr'.$css_class.'>'.
 3446:                             '<td>'.$title{$item}.
 3447:                             '</td><td class="LC_left_item">'.
 3448:                             '<span class="LC_nobreak">';
 3449:             foreach my $auth (@authtypes) {
 3450:                 $datatable .= '<label>'. 
 3451:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3452:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3453:                               $authname{$auth}.'</label>&nbsp;';
 3454:             }
 3455:             $datatable .= '</span></td></tr>';
 3456:             $rownum ++;
 3457:         }
 3458:         $$rowtotal += $rownum;
 3459:     }
 3460:     return $datatable;
 3461: }
 3462: 
 3463: sub captcha_choice {
 3464:     my ($context,$settings,$itemcount) = @_;
 3465:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3466:     my %lt = &captcha_phrases();
 3467:     $keyentry = 'hidden';
 3468:     if ($context eq 'cancreate') {
 3469:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3470:     } elsif ($context eq 'login') {
 3471:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3472:     }
 3473:     if (ref($settings) eq 'HASH') {
 3474:         if ($settings->{'captcha'}) {
 3475:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3476:         } else {
 3477:             $checked{'original'} = ' checked="checked"';
 3478:         }
 3479:         if ($settings->{'captcha'} eq 'recaptcha') {
 3480:             $pubtext = $lt{'pub'};
 3481:             $privtext = $lt{'priv'};
 3482:             $keyentry = 'text';
 3483:         }
 3484:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3485:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3486:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3487:         }
 3488:     } else {
 3489:         $checked{'original'} = ' checked="checked"';
 3490:     }
 3491:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3492:     my $output = '<tr'.$css_class.'>'.
 3493:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3494:                  '<table><tr><td>'."\n";
 3495:     foreach my $option ('original','recaptcha','notused') {
 3496:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3497:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3498:                    $lt{$option}.'</label></span>';
 3499:         unless ($option eq 'notused') {
 3500:             $output .= ('&nbsp;'x2)."\n";
 3501:         }
 3502:     }
 3503: #
 3504: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3505: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3506: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 3507: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3508: #
 3509:     $output .= '</td></tr>'."\n".
 3510:                '<tr><td>'."\n".
 3511:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3512:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3513:                $currpub.'" size="40" /></span><br />'."\n".
 3514:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3515:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3516:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3517:                '</td></tr>';
 3518:     return $output;
 3519: }
 3520: 
 3521: sub user_formats_row {
 3522:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3523:     my $output;
 3524:     my %text = (
 3525:                    'username' => 'new usernames',
 3526:                    'id'       => 'IDs',
 3527:                    'email'    => 'self-created accounts (e-mail)',
 3528:                );
 3529:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3530:     $output = '<tr '.$css_class.'>'.
 3531:               '<td><span class="LC_nobreak">';
 3532:     if ($type eq 'email') {
 3533:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3534:     } else {
 3535:         $output .= &mt("Format rules to check for $text{$type}: ");
 3536:     }
 3537:     $output .= '</span></td>'.
 3538:                '<td class="LC_left_item" colspan="2"><table>';
 3539:     my $rem;
 3540:     if (ref($ruleorder) eq 'ARRAY') {
 3541:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3542:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3543:                 my $rem = $i%($numinrow);
 3544:                 if ($rem == 0) {
 3545:                     if ($i > 0) {
 3546:                         $output .= '</tr>';
 3547:                     }
 3548:                     $output .= '<tr>';
 3549:                 }
 3550:                 my $check = ' ';
 3551:                 if (ref($settings) eq 'HASH') {
 3552:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3553:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3554:                             $check = ' checked="checked" ';
 3555:                         }
 3556:                     }
 3557:                 }
 3558:                 $output .= '<td class="LC_left_item">'.
 3559:                            '<span class="LC_nobreak"><label>'.
 3560:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3561:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3562:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3563:             }
 3564:         }
 3565:         $rem = @{$ruleorder}%($numinrow);
 3566:     }
 3567:     my $colsleft = $numinrow - $rem;
 3568:     if ($colsleft > 1 ) {
 3569:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3570:                    '&nbsp;</td>';
 3571:     } elsif ($colsleft == 1) {
 3572:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3573:     }
 3574:     $output .= '</tr></table></td></tr>';
 3575:     return $output;
 3576: }
 3577: 
 3578: sub usercreation_types {
 3579:     my %lt = &Apache::lonlocal::texthash (
 3580:                     author     => 'When adding a co-author',
 3581:                     course     => 'When adding a user to a course',
 3582:                     requestcrs => 'When requesting a course',
 3583:                     selfcreate => 'User creates own account', 
 3584:                     any        => 'Any',
 3585:                     official   => 'Institutional only ',
 3586:                     unofficial => 'Non-institutional only',
 3587:                     email      => 'E-mail address',
 3588:                     login      => 'Institutional Login',
 3589:                     sso        => 'SSO', 
 3590:                     none       => 'None',
 3591:     );
 3592:     return %lt;
 3593: }
 3594: 
 3595: sub authtype_names {
 3596:     my %lt = &Apache::lonlocal::texthash(
 3597:                       int    => 'Internal',
 3598:                       krb4   => 'Kerberos 4',
 3599:                       krb5   => 'Kerberos 5',
 3600:                       loc    => 'Local',
 3601:                   );
 3602:     return %lt;
 3603: }
 3604: 
 3605: sub context_names {
 3606:     my %context_title = &Apache::lonlocal::texthash(
 3607:        author => 'Creating users when an Author',
 3608:        course => 'Creating users when in a course',
 3609:        domain => 'Creating users when a Domain Coordinator',
 3610:     );
 3611:     return %context_title;
 3612: }
 3613: 
 3614: sub print_usermodification {
 3615:     my ($position,$dom,$settings,$rowtotal) = @_;
 3616:     my $numinrow = 4;
 3617:     my ($context,$datatable,$rowcount);
 3618:     if ($position eq 'top') {
 3619:         $rowcount = 0;
 3620:         $context = 'author'; 
 3621:         foreach my $role ('ca','aa') {
 3622:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3623:                                                    $numinrow,$rowcount);
 3624:             $$rowtotal ++;
 3625:             $rowcount ++;
 3626:         }
 3627:     } elsif ($position eq 'middle') {
 3628:         $context = 'course';
 3629:         $rowcount = 0;
 3630:         foreach my $role ('st','ep','ta','in','cr') {
 3631:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3632:                                                    $numinrow,$rowcount);
 3633:             $$rowtotal ++;
 3634:             $rowcount ++;
 3635:         }
 3636:     } elsif ($position eq 'bottom') {
 3637:         $context = 'selfcreate';
 3638:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3639:         $usertypes->{'default'} = $othertitle;
 3640:         if (ref($types) eq 'ARRAY') {
 3641:             push(@{$types},'default');
 3642:             $usertypes->{'default'} = $othertitle;
 3643:             foreach my $status (@{$types}) {
 3644:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3645:                                                        $numinrow,$rowcount,$usertypes);
 3646:                 $$rowtotal ++;
 3647:                 $rowcount ++;
 3648:             }
 3649:         }
 3650:     }
 3651:     return $datatable;
 3652: }
 3653: 
 3654: sub print_defaults {
 3655:     my ($dom,$rowtotal) = @_;
 3656:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3657:                  'datelocale_def','portal_def');
 3658:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3659:     my $titles = &defaults_titles($dom);
 3660:     my $rownum = 0;
 3661:     my ($datatable,$css_class);
 3662:     foreach my $item (@items) {
 3663:         if ($rownum%2) {
 3664:             $css_class = '';
 3665:         } else {
 3666:             $css_class = ' class="LC_odd_row" ';
 3667:         }
 3668:         $datatable .= '<tr'.$css_class.'>'.
 3669:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3670:                   '</span></td><td class="LC_right_item">';
 3671:         if ($item eq 'auth_def') {
 3672:             my @authtypes = ('internal','krb4','krb5','localauth');
 3673:             my %shortauth = (
 3674:                              internal => 'int',
 3675:                              krb4 => 'krb4',
 3676:                              krb5 => 'krb5',
 3677:                              localauth  => 'loc'
 3678:                            );
 3679:             my %authnames = &authtype_names();
 3680:             foreach my $auth (@authtypes) {
 3681:                 my $checked = ' ';
 3682:                 if ($domdefaults{$item} eq $auth) {
 3683:                     $checked = ' checked="checked" ';
 3684:                 }
 3685:                 $datatable .= '<label><input type="radio" name="'.$item.
 3686:                               '" value="'.$auth.'"'.$checked.'/>'.
 3687:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3688:             }
 3689:         } elsif ($item eq 'timezone_def') {
 3690:             my $includeempty = 1;
 3691:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3692:         } elsif ($item eq 'datelocale_def') {
 3693:             my $includeempty = 1;
 3694:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3695:         } elsif ($item eq 'lang_def') {
 3696:             my %langchoices = &get_languages_hash();
 3697:             $langchoices{''} = 'No language preference';
 3698:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 3699:             $datatable .= &Apache::loncommon::select_form($domdefaults{$item},$item,
 3700:                                                           \%langchoices);
 3701:         } else {
 3702:             my $size;
 3703:             if ($item eq 'portal_def') {
 3704:                 $size = ' size="25"';
 3705:             }
 3706:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3707:                           $domdefaults{$item}.'"'.$size.' />';
 3708:         }
 3709:         $datatable .= '</td></tr>';
 3710:         $rownum ++;
 3711:     }
 3712:     $$rowtotal += $rownum;
 3713:     return $datatable;
 3714: }
 3715: 
 3716: sub get_languages_hash {
 3717:     my %langchoices;
 3718:     foreach my $id (&Apache::loncommon::languageids()) {
 3719:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 3720:         if ($code ne '') {
 3721:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 3722:         }
 3723:     }
 3724:     return %langchoices;
 3725: }
 3726: 
 3727: sub defaults_titles {
 3728:     my ($dom) = @_;
 3729:     my %titles = &Apache::lonlocal::texthash (
 3730:                    'auth_def'      => 'Default authentication type',
 3731:                    'auth_arg_def'  => 'Default authentication argument',
 3732:                    'lang_def'      => 'Default language',
 3733:                    'timezone_def'  => 'Default timezone',
 3734:                    'datelocale_def' => 'Default locale for dates',
 3735:                    'portal_def'     => 'Portal/Default URL',
 3736:                  );
 3737:     if ($dom) {
 3738:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3739:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3740:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3741:         $protocol = 'http' if ($protocol ne 'https');
 3742:         if ($uint_dom) {
 3743:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3744:                                          $uint_dom);
 3745:         }
 3746:     }
 3747:     return (\%titles);
 3748: }
 3749: 
 3750: sub print_scantronformat {
 3751:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3752:     my $itemcount = 1;
 3753:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3754:         %confhash);
 3755:     my $switchserver = &check_switchserver($dom,$confname);
 3756:     my %lt = &Apache::lonlocal::texthash (
 3757:                 default => 'Default bubblesheet format file error',
 3758:                 custom  => 'Custom bubblesheet format file error',
 3759:              );
 3760:     my %scantronfiles = (
 3761:         default => 'default.tab',
 3762:         custom => 'custom.tab',
 3763:     );
 3764:     foreach my $key (keys(%scantronfiles)) {
 3765:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3766:                               .$scantronfiles{$key};
 3767:     }
 3768:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3769:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3770:         if (!$switchserver) {
 3771:             my $servadm = $r->dir_config('lonAdmEMail');
 3772:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3773:             if ($configuserok eq 'ok') {
 3774:                 if ($author_ok eq 'ok') {
 3775:                     my %legacyfile = (
 3776:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3777:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3778:                     );
 3779:                     my %md5chk;
 3780:                     foreach my $type (keys(%legacyfile)) {
 3781:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3782:                         chomp($md5chk{$type});
 3783:                     }
 3784:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3785:                         foreach my $type (keys(%legacyfile)) {
 3786:                             ($scantronurls{$type},my $error) = 
 3787:                                 &legacy_scantronformat($r,$dom,$confname,
 3788:                                                  $type,$legacyfile{$type},
 3789:                                                  $scantronurls{$type},
 3790:                                                  $scantronfiles{$type});
 3791:                             if ($error ne '') {
 3792:                                 $error{$type} = $error;
 3793:                             }
 3794:                         }
 3795:                         if (keys(%error) == 0) {
 3796:                             $is_custom = 1;
 3797:                             $confhash{'scantron'}{'scantronformat'} = 
 3798:                                 $scantronurls{'custom'};
 3799:                             my $putresult = 
 3800:                                 &Apache::lonnet::put_dom('configuration',
 3801:                                                          \%confhash,$dom);
 3802:                             if ($putresult ne 'ok') {
 3803:                                 $error{'custom'} = 
 3804:                                     '<span class="LC_error">'.
 3805:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3806:                             }
 3807:                         }
 3808:                     } else {
 3809:                         ($scantronurls{'default'},my $error) =
 3810:                             &legacy_scantronformat($r,$dom,$confname,
 3811:                                           'default',$legacyfile{'default'},
 3812:                                           $scantronurls{'default'},
 3813:                                           $scantronfiles{'default'});
 3814:                         if ($error eq '') {
 3815:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3816:                             my $putresult =
 3817:                                 &Apache::lonnet::put_dom('configuration',
 3818:                                                          \%confhash,$dom);
 3819:                             if ($putresult ne 'ok') {
 3820:                                 $error{'default'} =
 3821:                                     '<span class="LC_error">'.
 3822:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3823:                             }
 3824:                         } else {
 3825:                             $error{'default'} = $error;
 3826:                         }
 3827:                     }
 3828:                 }
 3829:             }
 3830:         } else {
 3831:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3832:         }
 3833:     }
 3834:     if (ref($settings) eq 'HASH') {
 3835:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3836:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3837:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3838:                 $scantronurl = '';
 3839:             } else {
 3840:                 $scantronurl = $settings->{'scantronformat'};
 3841:             }
 3842:             $is_custom = 1;
 3843:         } else {
 3844:             $scantronurl = $scantronurls{'default'};
 3845:         }
 3846:     } else {
 3847:         if ($is_custom) {
 3848:             $scantronurl = $scantronurls{'custom'};
 3849:         } else {
 3850:             $scantronurl = $scantronurls{'default'};
 3851:         }
 3852:     }
 3853:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3854:     $datatable .= '<tr'.$css_class.'>';
 3855:     if (!$is_custom) {
 3856:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3857:                       '<span class="LC_nobreak">';
 3858:         if ($scantronurl) {
 3859:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3860:                           &mt('Default bubblesheet format file').'</a>';
 3861:         } else {
 3862:             $datatable = &mt('File unavailable for display');
 3863:         }
 3864:         $datatable .= '</span></td>';
 3865:         if (keys(%error) == 0) { 
 3866:             $datatable .= '<td valign="bottom">';
 3867:             if (!$switchserver) {
 3868:                 $datatable .= &mt('Upload:').'<br />';
 3869:             }
 3870:         } else {
 3871:             my $errorstr;
 3872:             foreach my $key (sort(keys(%error))) {
 3873:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3874:             }
 3875:             $datatable .= '<td>'.$errorstr;
 3876:         }
 3877:     } else {
 3878:         if (keys(%error) > 0) {
 3879:             my $errorstr;
 3880:             foreach my $key (sort(keys(%error))) {
 3881:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3882:             } 
 3883:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3884:         } elsif ($scantronurl) {
 3885:             $datatable .= '<td><span class="LC_nobreak">'.
 3886:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3887:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3888:                           '<input type="checkbox" name="scantronformat_del"'.
 3889:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3890:                           '<td><span class="LC_nobreak">&nbsp;'.
 3891:                           &mt('Replace:').'</span><br />';
 3892:         }
 3893:     }
 3894:     if (keys(%error) == 0) {
 3895:         if ($switchserver) {
 3896:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3897:         } else {
 3898:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3899:                          '<input type="file" name="scantronformat" /></span>';
 3900:         }
 3901:     }
 3902:     $datatable .= '</td></tr>';
 3903:     $$rowtotal ++;
 3904:     return $datatable;
 3905: }
 3906: 
 3907: sub legacy_scantronformat {
 3908:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3909:     my ($url,$error);
 3910:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3911:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3912:         (my $result,$url) =
 3913:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3914:                          '','',$newfile);
 3915:         if ($result ne 'ok') {
 3916:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3917:         }
 3918:     }
 3919:     return ($url,$error);
 3920: }
 3921: 
 3922: sub print_coursecategories {
 3923:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3924:     my $datatable;
 3925:     if ($position eq 'top') {
 3926:         my $toggle_cats_crs = ' ';
 3927:         my $toggle_cats_dom = ' checked="checked" ';
 3928:         my $can_cat_crs = ' ';
 3929:         my $can_cat_dom = ' checked="checked" ';
 3930:         my $toggle_catscomm_comm = ' ';
 3931:         my $toggle_catscomm_dom = ' checked="checked" ';
 3932:         my $can_catcomm_comm = ' ';
 3933:         my $can_catcomm_dom = ' checked="checked" ';
 3934: 
 3935:         if (ref($settings) eq 'HASH') {
 3936:             if ($settings->{'togglecats'} eq 'crs') {
 3937:                 $toggle_cats_crs = $toggle_cats_dom;
 3938:                 $toggle_cats_dom = ' ';
 3939:             }
 3940:             if ($settings->{'categorize'} eq 'crs') {
 3941:                 $can_cat_crs = $can_cat_dom;
 3942:                 $can_cat_dom = ' ';
 3943:             }
 3944:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3945:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3946:                 $toggle_catscomm_dom = ' ';
 3947:             }
 3948:             if ($settings->{'categorizecomm'} eq 'comm') {
 3949:                 $can_catcomm_comm = $can_catcomm_dom;
 3950:                 $can_catcomm_dom = ' ';
 3951:             }
 3952:         }
 3953:         my %title = &Apache::lonlocal::texthash (
 3954:                      togglecats     => 'Show/Hide a course in catalog',
 3955:                      togglecatscomm => 'Show/Hide a community in catalog',
 3956:                      categorize     => 'Assign a category to a course',
 3957:                      categorizecomm => 'Assign a category to a community',
 3958:                     );
 3959:         my %level = &Apache::lonlocal::texthash (
 3960:                      dom  => 'Set in Domain',
 3961:                      crs  => 'Set in Course',
 3962:                      comm => 'Set in Community',
 3963:                     );
 3964:         $datatable = '<tr class="LC_odd_row">'.
 3965:                   '<td>'.$title{'togglecats'}.'</td>'.
 3966:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3967:                   '<input type="radio" name="togglecats"'.
 3968:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3969:                   '<label><input type="radio" name="togglecats"'.
 3970:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3971:                   '</tr><tr>'.
 3972:                   '<td>'.$title{'categorize'}.'</td>'.
 3973:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3974:                   '<label><input type="radio" name="categorize"'.
 3975:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3976:                   '<label><input type="radio" name="categorize"'.
 3977:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3978:                   '</tr><tr class="LC_odd_row">'.
 3979:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 3980:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3981:                   '<input type="radio" name="togglecatscomm"'.
 3982:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3983:                   '<label><input type="radio" name="togglecatscomm"'.
 3984:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3985:                   '</tr><tr>'.
 3986:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 3987:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3988:                   '<label><input type="radio" name="categorizecomm"'.
 3989:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3990:                   '<label><input type="radio" name="categorizecomm"'.
 3991:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3992:                   '</tr>';
 3993:         $$rowtotal += 4;
 3994:     } else {
 3995:         my $css_class;
 3996:         my $itemcount = 1;
 3997:         my $cathash; 
 3998:         if (ref($settings) eq 'HASH') {
 3999:             $cathash = $settings->{'cats'};
 4000:         }
 4001:         if (ref($cathash) eq 'HASH') {
 4002:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4003:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4004:                                                    \%allitems,\%idx,\@jsarray);
 4005:             my $maxdepth = scalar(@cats);
 4006:             my $colattrib = '';
 4007:             if ($maxdepth > 2) {
 4008:                 $colattrib = ' colspan="2" ';
 4009:             }
 4010:             my @path;
 4011:             if (@cats > 0) {
 4012:                 if (ref($cats[0]) eq 'ARRAY') {
 4013:                     my $numtop = @{$cats[0]};
 4014:                     my $maxnum = $numtop;
 4015:                     my %default_names = (
 4016:                           instcode    => &mt('Official courses'),
 4017:                           communities => &mt('Communities'),
 4018:                     );
 4019: 
 4020:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4021:                         ($cathash->{'instcode::0'} eq '') ||
 4022:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4023:                         ($cathash->{'communities::0'} eq '')) {
 4024:                         $maxnum ++;
 4025:                     }
 4026:                     my $lastidx;
 4027:                     for (my $i=0; $i<$numtop; $i++) {
 4028:                         my $parent = $cats[0][$i];
 4029:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4030:                         my $item = &escape($parent).'::0';
 4031:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4032:                         $lastidx = $idx{$item};
 4033:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4034:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4035:                         for (my $k=0; $k<=$maxnum; $k++) {
 4036:                             my $vpos = $k+1;
 4037:                             my $selstr;
 4038:                             if ($k == $i) {
 4039:                                 $selstr = ' selected="selected" ';
 4040:                             }
 4041:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4042:                         }
 4043:                         $datatable .= '</select></td><td>';
 4044:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4045:                             $datatable .=  '<span class="LC_nobreak">'
 4046:                                            .$default_names{$parent}.'</span>';
 4047:                             if ($parent eq 'instcode') {
 4048:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4049:                                               .&mt('with institutional codes')
 4050:                                               .')</span></td><td'.$colattrib.'>';
 4051:                             } else {
 4052:                                 $datatable .= '<table><tr><td>';
 4053:                             }
 4054:                             $datatable .= '<span class="LC_nobreak">'
 4055:                                           .'<label><input type="radio" name="'
 4056:                                           .$parent.'" value="1" checked="checked" />'
 4057:                                           .&mt('Display').'</label>';
 4058:                             if ($parent eq 'instcode') {
 4059:                                 $datatable .= '&nbsp;';
 4060:                             } else {
 4061:                                 $datatable .= '</span></td></tr><tr><td>'
 4062:                                               .'<span class="LC_nobreak">';
 4063:                             }
 4064:                             $datatable .= '<label><input type="radio" name="'
 4065:                                           .$parent.'" value="0" />'
 4066:                                           .&mt('Do not display').'</label></span>';
 4067:                             if ($parent eq 'communities') {
 4068:                                 $datatable .= '</td></tr></table>';
 4069:                             }
 4070:                             $datatable .= '</td>';
 4071:                         } else {
 4072:                             $datatable .= $parent
 4073:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 4074:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4075:                         }
 4076:                         my $depth = 1;
 4077:                         push(@path,$parent);
 4078:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4079:                         pop(@path);
 4080:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4081:                         $itemcount ++;
 4082:                     }
 4083:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4084:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4085:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4086:                     for (my $k=0; $k<=$maxnum; $k++) {
 4087:                         my $vpos = $k+1;
 4088:                         my $selstr;
 4089:                         if ($k == $numtop) {
 4090:                             $selstr = ' selected="selected" ';
 4091:                         }
 4092:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4093:                     }
 4094:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4095:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4096:                                   .'</tr>'."\n";
 4097:                     $itemcount ++;
 4098:                     foreach my $default ('instcode','communities') {
 4099:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4100:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4101:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4102:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4103:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4104:                             for (my $k=0; $k<=$maxnum; $k++) {
 4105:                                 my $vpos = $k+1;
 4106:                                 my $selstr;
 4107:                                 if ($k == $maxnum) {
 4108:                                     $selstr = ' selected="selected" ';
 4109:                                 }
 4110:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4111:                             }
 4112:                             $datatable .= '</select></span></td>'.
 4113:                                           '<td><span class="LC_nobreak">'.
 4114:                                           $default_names{$default}.'</span>';
 4115:                             if ($default eq 'instcode') {
 4116:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4117:                                               .&mt('with institutional codes').')</span>';
 4118:                             }
 4119:                             $datatable .= '</td>'
 4120:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4121:                                           .&mt('Display').'</label>&nbsp;'
 4122:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4123:                                           .&mt('Do not display').'</label></span></td></tr>';
 4124:                         }
 4125:                     }
 4126:                 }
 4127:             } else {
 4128:                 $datatable .= &initialize_categories($itemcount);
 4129:             }
 4130:         } else {
 4131:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4132:                           .&initialize_categories($itemcount);
 4133:         }
 4134:         $$rowtotal += $itemcount;
 4135:     }
 4136:     return $datatable;
 4137: }
 4138: 
 4139: sub print_serverstatuses {
 4140:     my ($dom,$settings,$rowtotal) = @_;
 4141:     my $datatable;
 4142:     my @pages = &serverstatus_pages();
 4143:     my (%namedaccess,%machineaccess);
 4144:     foreach my $type (@pages) {
 4145:         $namedaccess{$type} = '';
 4146:         $machineaccess{$type}= '';
 4147:     }
 4148:     if (ref($settings) eq 'HASH') {
 4149:         foreach my $type (@pages) {
 4150:             if (exists($settings->{$type})) {
 4151:                 if (ref($settings->{$type}) eq 'HASH') {
 4152:                     foreach my $key (keys(%{$settings->{$type}})) {
 4153:                         if ($key eq 'namedusers') {
 4154:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4155:                         } elsif ($key eq 'machines') {
 4156:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4157:                         }
 4158:                     }
 4159:                 }
 4160:             }
 4161:         }
 4162:     }
 4163:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4164:     my $rownum = 0;
 4165:     my $css_class;
 4166:     foreach my $type (@pages) {
 4167:         $rownum ++;
 4168:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4169:         $datatable .= '<tr'.$css_class.'>'.
 4170:                       '<td><span class="LC_nobreak">'.
 4171:                       $titles->{$type}.'</span></td>'.
 4172:                       '<td class="LC_left_item">'.
 4173:                       '<input type="text" name="'.$type.'_namedusers" '.
 4174:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4175:                       '<td class="LC_right_item">'.
 4176:                       '<span class="LC_nobreak">'.
 4177:                       '<input type="text" name="'.$type.'_machines" '.
 4178:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4179:                       '</td></tr>'."\n";
 4180:     }
 4181:     $$rowtotal += $rownum;
 4182:     return $datatable;
 4183: }
 4184: 
 4185: sub serverstatus_pages {
 4186:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4187:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4188:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4189: }
 4190: 
 4191: sub coursecategories_javascript {
 4192:     my ($settings) = @_;
 4193:     my ($output,$jstext,$cathash);
 4194:     if (ref($settings) eq 'HASH') {
 4195:         $cathash = $settings->{'cats'};
 4196:     }
 4197:     if (ref($cathash) eq 'HASH') {
 4198:         my (@cats,@jsarray,%idx);
 4199:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4200:         if (@jsarray > 0) {
 4201:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4202:             for (my $i=0; $i<@jsarray; $i++) {
 4203:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4204:                     my $catstr = join('","',@{$jsarray[$i]});
 4205:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4206:                 }
 4207:             }
 4208:         }
 4209:     } else {
 4210:         $jstext  = '    var categories = Array(1);'."\n".
 4211:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4212:     }
 4213:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4214:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4215:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4216:     $output = <<"ENDSCRIPT";
 4217: <script type="text/javascript">
 4218: // <![CDATA[
 4219: function reorderCats(form,parent,item,idx) {
 4220:     var changedVal;
 4221: $jstext
 4222:     var newpos = 'addcategory_pos';
 4223:     var current = new Array;
 4224:     if (parent == '') {
 4225:         var has_instcode = 0;
 4226:         var maxtop = categories[idx].length;
 4227:         for (var j=0; j<maxtop; j++) {
 4228:             if (categories[idx][j] == 'instcode::0') {
 4229:                 has_instcode == 1;
 4230:             }
 4231:         }
 4232:         if (has_instcode == 0) {
 4233:             categories[idx][maxtop] = 'instcode_pos';
 4234:         }
 4235:     } else {
 4236:         newpos += '_'+parent;
 4237:     }
 4238:     var maxh = 1 + categories[idx].length;
 4239:     var current = new Array;
 4240:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4241:     if (item == newpos) {
 4242:         changedVal = newitemVal;
 4243:     } else {
 4244:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4245:         current[newitemVal] = newpos;
 4246:     }
 4247:     for (var i=0; i<categories[idx].length; i++) {
 4248:         var elementName = categories[idx][i];
 4249:         if (elementName != item) {
 4250:             if (form.elements[elementName]) {
 4251:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4252:                 current[currVal] = elementName;
 4253:             }
 4254:         }
 4255:     }
 4256:     var oldVal;
 4257:     for (var j=0; j<maxh; j++) {
 4258:         if (current[j] == undefined) {
 4259:             oldVal = j;
 4260:         }
 4261:     }
 4262:     if (oldVal < changedVal) {
 4263:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4264:            var elementName = current[k];
 4265:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4266:         }
 4267:     } else {
 4268:         for (var k=changedVal; k<oldVal; k++) {
 4269:             var elementName = current[k];
 4270:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4271:         }
 4272:     }
 4273:     return;
 4274: }
 4275: 
 4276: function categoryCheck(form) {
 4277:     if (form.elements['addcategory_name'].value == 'instcode') {
 4278:         alert('$instcode_reserved\\n$choose_again');
 4279:         return false;
 4280:     }
 4281:     if (form.elements['addcategory_name'].value == 'communities') {
 4282:         alert('$communities_reserved\\n$choose_again');
 4283:         return false;
 4284:     }
 4285:     return true;
 4286: }
 4287: 
 4288: // ]]>
 4289: </script>
 4290: 
 4291: ENDSCRIPT
 4292:     return $output;
 4293: }
 4294: 
 4295: sub initialize_categories {
 4296:     my ($itemcount) = @_;
 4297:     my ($datatable,$css_class,$chgstr);
 4298:     my %default_names = (
 4299:                       instcode    => 'Official courses (with institutional codes)',
 4300:                       communities => 'Communities',
 4301:                         );
 4302:     my $select0 = ' selected="selected"';
 4303:     my $select1 = '';
 4304:     foreach my $default ('instcode','communities') {
 4305:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4306:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4307:         if ($default eq 'communities') {
 4308:             $select1 = $select0;
 4309:             $select0 = '';
 4310:         }
 4311:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4312:                      .'<select name="'.$default.'_pos">'
 4313:                      .'<option value="0"'.$select0.'>1</option>'
 4314:                      .'<option value="1"'.$select1.'>2</option>'
 4315:                      .'<option value="2">3</option></select>&nbsp;'
 4316:                      .$default_names{$default}
 4317:                      .'</span></td><td><span class="LC_nobreak">'
 4318:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4319:                      .&mt('Display').'</label>&nbsp;<label>'
 4320:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4321:                  .'</label></span></td></tr>';
 4322:         $itemcount ++;
 4323:     }
 4324:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4325:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4326:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4327:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4328:                   .'<option value="0">1</option>'
 4329:                   .'<option value="1">2</option>'
 4330:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4331:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4332:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4333:     return $datatable;
 4334: }
 4335: 
 4336: sub build_category_rows {
 4337:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4338:     my ($text,$name,$item,$chgstr);
 4339:     if (ref($cats) eq 'ARRAY') {
 4340:         my $maxdepth = scalar(@{$cats});
 4341:         if (ref($cats->[$depth]) eq 'HASH') {
 4342:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4343:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4344:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4345:                 $text .= '<td><table class="LC_datatable">';
 4346:                 my ($idxnum,$parent_name,$parent_item);
 4347:                 my $higher = $depth - 1;
 4348:                 if ($higher == 0) {
 4349:                     $parent_name = &escape($parent).'::'.$higher;
 4350:                 } else {
 4351:                     if (ref($path) eq 'ARRAY') {
 4352:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4353:                     }
 4354:                 }
 4355:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4356:                 for (my $j=0; $j<=$numchildren; $j++) {
 4357:                     if ($j < $numchildren) {
 4358:                         $name = $cats->[$depth]{$parent}[$j];
 4359:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4360:                         $idxnum = $idx->{$item};
 4361:                     } else {
 4362:                         $name = $parent_name;
 4363:                         $item = $parent_item;
 4364:                     }
 4365:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4366:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4367:                     for (my $i=0; $i<=$numchildren; $i++) {
 4368:                         my $vpos = $i+1;
 4369:                         my $selstr;
 4370:                         if ($j == $i) {
 4371:                             $selstr = ' selected="selected" ';
 4372:                         }
 4373:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4374:                     }
 4375:                     $text .= '</select>&nbsp;';
 4376:                     if ($j < $numchildren) {
 4377:                         my $deeper = $depth+1;
 4378:                         $text .= $name.'&nbsp;'
 4379:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4380:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4381:                         if(ref($path) eq 'ARRAY') {
 4382:                             push(@{$path},$name);
 4383:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4384:                             pop(@{$path});
 4385:                         }
 4386:                     } else {
 4387:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4388:                         if ($j == $numchildren) {
 4389:                             $text .= $name;
 4390:                         } else {
 4391:                             $text .= $item;
 4392:                         }
 4393:                         $text .= '" value="" />';
 4394:                     }
 4395:                     $text .= '</td></tr>';
 4396:                 }
 4397:                 $text .= '</table></td>';
 4398:             } else {
 4399:                 my $higher = $depth-1;
 4400:                 if ($higher == 0) {
 4401:                     $name = &escape($parent).'::'.$higher;
 4402:                 } else {
 4403:                     if (ref($path) eq 'ARRAY') {
 4404:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4405:                     }
 4406:                 }
 4407:                 my $colspan;
 4408:                 if ($parent ne 'instcode') {
 4409:                     $colspan = $maxdepth - $depth - 1;
 4410:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4411:                 }
 4412:             }
 4413:         }
 4414:     }
 4415:     return $text;
 4416: }
 4417: 
 4418: sub modifiable_userdata_row {
 4419:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4420:     my $rolename;
 4421:     if ($context eq 'selfcreate') {
 4422:         if (ref($usertypes) eq 'HASH') {
 4423:             $rolename = $usertypes->{$role};
 4424:         } else {
 4425:             $rolename = $role;
 4426:         }
 4427:     } else {
 4428:         if ($role eq 'cr') {
 4429:             $rolename = &mt('Custom role');
 4430:         } else {
 4431:             $rolename = &Apache::lonnet::plaintext($role);
 4432:         }
 4433:     }
 4434:     my @fields = ('lastname','firstname','middlename','generation',
 4435:                   'permanentemail','id');
 4436:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4437:     my $output;
 4438:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4439:     $output = '<tr '.$css_class.'>'.
 4440:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4441:               '<td class="LC_left_item" colspan="2"><table>';
 4442:     my $rem;
 4443:     my %checks;
 4444:     if (ref($settings) eq 'HASH') {
 4445:         if (ref($settings->{$context}) eq 'HASH') {
 4446:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4447:                 foreach my $field (@fields) {
 4448:                     if ($settings->{$context}->{$role}->{$field}) {
 4449:                         $checks{$field} = ' checked="checked" ';
 4450:                     }
 4451:                 }
 4452:             }
 4453:         }
 4454:     }
 4455:     for (my $i=0; $i<@fields; $i++) {
 4456:         my $rem = $i%($numinrow);
 4457:         if ($rem == 0) {
 4458:             if ($i > 0) {
 4459:                 $output .= '</tr>';
 4460:             }
 4461:             $output .= '<tr>';
 4462:         }
 4463:         my $check = ' ';
 4464:         if (exists($checks{$fields[$i]})) {
 4465:             $check = $checks{$fields[$i]}
 4466:         } else {
 4467:             if ($role eq 'st') {
 4468:                 if (ref($settings) ne 'HASH') {
 4469:                     $check = ' checked="checked" '; 
 4470:                 }
 4471:             }
 4472:         }
 4473:         $output .= '<td class="LC_left_item">'.
 4474:                    '<span class="LC_nobreak"><label>'.
 4475:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4476:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4477:                    '</label></span></td>';
 4478:         $rem = @fields%($numinrow);
 4479:     }
 4480:     my $colsleft = $numinrow - $rem;
 4481:     if ($colsleft > 1 ) {
 4482:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4483:                    '&nbsp;</td>';
 4484:     } elsif ($colsleft == 1) {
 4485:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4486:     }
 4487:     $output .= '</tr></table></td></tr>';
 4488:     return $output;
 4489: }
 4490: 
 4491: sub insttypes_row {
 4492:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4493:     my %lt = &Apache::lonlocal::texthash (
 4494:                       cansearch => 'Users allowed to search',
 4495:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4496:                       lockablenames => 'User preference to lock name',
 4497:              );
 4498:     my $showdom;
 4499:     if ($context eq 'cansearch') {
 4500:         $showdom = ' ('.$dom.')';
 4501:     }
 4502:     my $class = 'LC_left_item';
 4503:     if ($context eq 'statustocreate') {
 4504:         $class = 'LC_right_item';
 4505:     }
 4506:     my $output =  '<tr class="LC_odd_row">'.
 4507:                   '<td>'.$lt{$context}.$showdom.
 4508:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4509:     my $rem;
 4510:     if (ref($types) eq 'ARRAY') {
 4511:         for (my $i=0; $i<@{$types}; $i++) {
 4512:             if (defined($usertypes->{$types->[$i]})) {
 4513:                 my $rem = $i%($numinrow);
 4514:                 if ($rem == 0) {
 4515:                     if ($i > 0) {
 4516:                         $output .= '</tr>';
 4517:                     }
 4518:                     $output .= '<tr>';
 4519:                 }
 4520:                 my $check = ' ';
 4521:                 if (ref($settings) eq 'HASH') {
 4522:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4523:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4524:                             $check = ' checked="checked" ';
 4525:                         }
 4526:                     } elsif ($context eq 'statustocreate') {
 4527:                         $check = ' checked="checked" ';
 4528:                     }
 4529:                 }
 4530:                 $output .= '<td class="LC_left_item">'.
 4531:                            '<span class="LC_nobreak"><label>'.
 4532:                            '<input type="checkbox" name="'.$context.'" '.
 4533:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4534:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4535:             }
 4536:         }
 4537:         $rem = @{$types}%($numinrow);
 4538:     }
 4539:     my $colsleft = $numinrow - $rem;
 4540:     if (($rem == 0) && (@{$types} > 0)) {
 4541:         $output .= '<tr>';
 4542:     }
 4543:     if ($colsleft > 1) {
 4544:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4545:     } else {
 4546:         $output .= '<td class="LC_left_item">';
 4547:     }
 4548:     my $defcheck = ' ';
 4549:     if (ref($settings) eq 'HASH') {  
 4550:         if (ref($settings->{$context}) eq 'ARRAY') {
 4551:             if (grep(/^default$/,@{$settings->{$context}})) {
 4552:                 $defcheck = ' checked="checked" ';
 4553:             }
 4554:         } elsif ($context eq 'statustocreate') {
 4555:             $defcheck = ' checked="checked" ';
 4556:         }
 4557:     }
 4558:     $output .= '<span class="LC_nobreak"><label>'.
 4559:                '<input type="checkbox" name="'.$context.'" '.
 4560:                'value="default"'.$defcheck.'/>'.
 4561:                $othertitle.'</label></span></td>'.
 4562:                '</tr></table></td></tr>';
 4563:     return $output;
 4564: }
 4565: 
 4566: sub sorted_searchtitles {
 4567:     my %searchtitles = &Apache::lonlocal::texthash(
 4568:                          'uname' => 'username',
 4569:                          'lastname' => 'last name',
 4570:                          'lastfirst' => 'last name, first name',
 4571:                      );
 4572:     my @titleorder = ('uname','lastname','lastfirst');
 4573:     return (\%searchtitles,\@titleorder);
 4574: }
 4575: 
 4576: sub sorted_searchtypes {
 4577:     my %srchtypes_desc = (
 4578:                            exact    => 'is exact match',
 4579:                            contains => 'contains ..',
 4580:                            begins   => 'begins with ..',
 4581:                          );
 4582:     my @srchtypeorder = ('exact','begins','contains');
 4583:     return (\%srchtypes_desc,\@srchtypeorder);
 4584: }
 4585: 
 4586: sub usertype_update_row {
 4587:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4588:     my $datatable;
 4589:     my $numinrow = 4;
 4590:     foreach my $type (@{$types}) {
 4591:         if (defined($usertypes->{$type})) {
 4592:             $$rownums ++;
 4593:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4594:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4595:                           '</td><td class="LC_left_item"><table>';
 4596:             for (my $i=0; $i<@{$fields}; $i++) {
 4597:                 my $rem = $i%($numinrow);
 4598:                 if ($rem == 0) {
 4599:                     if ($i > 0) {
 4600:                         $datatable .= '</tr>';
 4601:                     }
 4602:                     $datatable .= '<tr>';
 4603:                 }
 4604:                 my $check = ' ';
 4605:                 if (ref($settings) eq 'HASH') {
 4606:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4607:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4608:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4609:                                 $check = ' checked="checked" ';
 4610:                             }
 4611:                         }
 4612:                     }
 4613:                 }
 4614: 
 4615:                 if ($i == @{$fields}-1) {
 4616:                     my $colsleft = $numinrow - $rem;
 4617:                     if ($colsleft > 1) {
 4618:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4619:                     } else {
 4620:                         $datatable .= '<td>';
 4621:                     }
 4622:                 } else {
 4623:                     $datatable .= '<td>';
 4624:                 }
 4625:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4626:                               '<input type="checkbox" name="updateable_'.$type.
 4627:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4628:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4629:             }
 4630:             $datatable .= '</tr></table></td></tr>';
 4631:         }
 4632:     }
 4633:     return $datatable;
 4634: }
 4635: 
 4636: sub modify_login {
 4637:     my ($r,$dom,$confname,%domconfig) = @_;
 4638:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4639:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4640:     %title = ( coursecatalog => 'Display course catalog',
 4641:                adminmail => 'Display administrator E-mail address',
 4642:                helpdesk  => 'Display "Contact Helpdesk" link',
 4643:                newuser => 'Link for visitors to create a user account',
 4644:                loginheader => 'Log-in box header');
 4645:     @offon = ('off','on');
 4646:     if (ref($domconfig{login}) eq 'HASH') {
 4647:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4648:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4649:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4650:             }
 4651:         }
 4652:     }
 4653:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4654:                                            \%domconfig,\%loginhash);
 4655:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 4656:     foreach my $item (@toggles) {
 4657:         $loginhash{login}{$item} = $env{'form.'.$item};
 4658:     }
 4659:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4660:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4661:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4662:                                          \%loginhash);
 4663:     }
 4664: 
 4665:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4666:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4667:     if (keys(%servers) > 1) {
 4668:         foreach my $lonhost (keys(%servers)) {
 4669:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4670:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4671:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4672:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4673:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4674:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4675:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4676:                         $changes{'loginvia'}{$lonhost} = 1;
 4677:                     } else {
 4678:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4679:                         $changes{'loginvia'}{$lonhost} = 1;
 4680:                     }
 4681:                 } else {
 4682:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4683:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4684:                         $changes{'loginvia'}{$lonhost} = 1;
 4685:                     }
 4686:                 }
 4687:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4688:                     foreach my $item (@loginvia_attribs) {
 4689:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4690:                     }
 4691:                 } else {
 4692:                     foreach my $item (@loginvia_attribs) {
 4693:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4694:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4695:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4696:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4697:                                 $new = '/';
 4698:                             }
 4699:                         }
 4700:                         if (($item eq 'custompath') && 
 4701:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4702:                             $new = '';
 4703:                         }
 4704:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4705:                             $changes{'loginvia'}{$lonhost} = 1;
 4706:                         }
 4707:                         if ($item eq 'exempt') {
 4708:                             $new =~ s/^\s+//;
 4709:                             $new =~ s/\s+$//;
 4710:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4711:                             my @okips;
 4712:                             foreach my $ip (@poss_ips) {
 4713:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4714:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4715:                                         push(@okips,$ip); 
 4716:                                     }
 4717:                                 }
 4718:                             }
 4719:                             if (@okips > 0) {
 4720:                                 $new = join(',',@okips); 
 4721:                             } else {
 4722:                                 $new = ''; 
 4723:                             }
 4724:                         }
 4725:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4726:                     }
 4727:                 }
 4728:             } else {
 4729:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4730:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4731:                     $changes{'loginvia'}{$lonhost} = 1;
 4732:                     foreach my $item (@loginvia_attribs) {
 4733:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4734:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4735:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4736:                                 $new = '/';
 4737:                             }
 4738:                         }
 4739:                         if (($item eq 'custompath') && 
 4740:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4741:                             $new = '';
 4742:                         }
 4743:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4744:                     }
 4745:                 }
 4746:             }
 4747:         }
 4748:     }
 4749: 
 4750:     my $servadm = $r->dir_config('lonAdmEMail');
 4751:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 4752:     if (ref($domconfig{'login'}) eq 'HASH') {
 4753:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 4754:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 4755:                 if ($lang eq 'nolang') {
 4756:                     push(@currlangs,$lang);
 4757:                 } elsif (defined($langchoices{$lang})) {
 4758:                     push(@currlangs,$lang);
 4759:                 } else {
 4760:                     next;
 4761:                 }
 4762:             }
 4763:         }
 4764:     }
 4765:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 4766:     if (@currlangs > 0) {
 4767:         foreach my $lang (@currlangs) {
 4768:             if (grep(/^\Q$lang\E$/,@delurls)) {
 4769:                 $changes{'helpurl'}{$lang} = 1;
 4770:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 4771:                 $changes{'helpurl'}{$lang} = 1;
 4772:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 4773:                 push(@newlangs,$lang);
 4774:             } else {
 4775:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4776:             }
 4777:         }
 4778:     }
 4779:     unless (grep(/^nolang$/,@currlangs)) {
 4780:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 4781:             $changes{'helpurl'}{'nolang'} = 1;
 4782:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 4783:             push(@newlangs,'nolang');
 4784:         }
 4785:     }
 4786:     if ($env{'form.loginhelpurl_add_lang'}) {
 4787:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 4788:             ($env{'form.loginhelpurl_add_file.filename'})) {
 4789:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 4790:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 4791:         }
 4792:     }
 4793:     if ((@newlangs > 0) || ($addedfile)) {
 4794:         my $error;
 4795:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 4796:         if ($configuserok eq 'ok') {
 4797:             if ($switchserver) {
 4798:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 4799:             } elsif ($author_ok eq 'ok') {
 4800:                 my @allnew = @newlangs;
 4801:                 if ($addedfile ne '') {
 4802:                     push(@allnew,$addedfile);
 4803:                 }
 4804:                 foreach my $lang (@allnew) {
 4805:                     my $formelem = 'loginhelpurl_'.$lang;
 4806:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 4807:                         $formelem = 'loginhelpurl_add_file';
 4808:                     }
 4809:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 4810:                                                                "help/$lang",'','',$newfile{$lang});
 4811:                     if ($result eq 'ok') {
 4812:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 4813:                         $changes{'helpurl'}{$lang} = 1;
 4814:                     } else {
 4815:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 4816:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 4817:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 4818:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 4819: 
 4820:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4821:                         }
 4822:                     }
 4823:                 }
 4824:             } else {
 4825:                 $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);
 4826:             }
 4827:         } else {
 4828:             $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);
 4829:         }
 4830:         if ($error) {
 4831:             &Apache::lonnet::logthis($error);
 4832:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4833:         }
 4834:     }
 4835:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 4836: 
 4837:     my $defaulthelpfile = '/adm/loginproblems.html';
 4838:     my $defaulttext = &mt('Default in use');
 4839: 
 4840:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4841:                                              $dom);
 4842:     if ($putresult eq 'ok') {
 4843:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 4844:         my %defaultchecked = (
 4845:                     'coursecatalog' => 'on',
 4846:                     'helpdesk'      => 'on',
 4847:                     'adminmail'     => 'off',
 4848:                     'newuser'       => 'off',
 4849:         );
 4850:         if (ref($domconfig{'login'}) eq 'HASH') {
 4851:             foreach my $item (@toggles) {
 4852:                 if ($defaultchecked{$item} eq 'on') { 
 4853:                     if (($domconfig{'login'}{$item} eq '0') &&
 4854:                         ($env{'form.'.$item} eq '1')) {
 4855:                         $changes{$item} = 1;
 4856:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4857:                               $domconfig{'login'}{$item} eq '1') &&
 4858:                              ($env{'form.'.$item} eq '0')) {
 4859:                         $changes{$item} = 1;
 4860:                     }
 4861:                 } elsif ($defaultchecked{$item} eq 'off') {
 4862:                     if (($domconfig{'login'}{$item} eq '1') &&
 4863:                         ($env{'form.'.$item} eq '0')) {
 4864:                         $changes{$item} = 1;
 4865:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4866:                               $domconfig{'login'}{$item} eq '0') &&
 4867:                              ($env{'form.'.$item} eq '1')) {
 4868:                         $changes{$item} = 1;
 4869:                     }
 4870:                 }
 4871:             }
 4872:         }
 4873:         if (keys(%changes) > 0 || $colchgtext) {
 4874:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4875:             $resulttext = &mt('Changes made:').'<ul>';
 4876:             foreach my $item (sort(keys(%changes))) {
 4877:                 if ($item eq 'loginvia') {
 4878:                     if (ref($changes{$item}) eq 'HASH') {
 4879:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4880:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4881:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4882:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4883:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4884:                                     $protocol = 'http' if ($protocol ne 'https');
 4885:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4886: 
 4887:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4888:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4889:                                     } else {
 4890:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4891:                                     }
 4892:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4893:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4894:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4895:                                     }
 4896:                                     $resulttext .= '</li>';
 4897:                                 } else {
 4898:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4899:                                 }
 4900:                             } else {
 4901:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4902:                             }
 4903:                         }
 4904:                         $resulttext .= '</ul></li>';
 4905:                     }
 4906:                 } elsif ($item eq 'helpurl') {
 4907:                     if (ref($changes{$item}) eq 'HASH') {
 4908:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 4909:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 4910:                                 my ($chg,$link);
 4911:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 4912:                                 if ($lang eq 'nolang') {
 4913:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 4914:                                 } else {
 4915:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 4916:                                 }
 4917:                                 $resulttext .= '<li>'.$chg.'</li>';
 4918:                             } else {
 4919:                                 my $chg;
 4920:                                 if ($lang eq 'nolang') {
 4921:                                     $chg = &mt('custom log-in help file for no preferred language');
 4922:                                 } else {
 4923:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 4924:                                 }
 4925:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 4926:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 4927:                                                       '?inhibitmenu=yes',$chg,600,500).
 4928:                                                '</li>';
 4929:                             }
 4930:                         }
 4931:                     }
 4932:                 } elsif ($item eq 'captcha') {
 4933:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4934:                         my $chgtxt;
 4935:                         if ($loginhash{'login'}{$item} eq 'notused') {
 4936:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 4937:                         } else {
 4938:                             my %captchas = &captcha_phrases();
 4939:                             if ($captchas{$loginhash{'login'}{$item}}) {
 4940:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 4941:                             } else {
 4942:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 4943:                             }
 4944:                         }
 4945:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4946:                     }
 4947:                 } elsif ($item eq 'recaptchakeys') {
 4948:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4949:                         my ($privkey,$pubkey);
 4950:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 4951:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 4952:                             $privkey = $loginhash{'login'}{$item}{'private'};
 4953:                         }
 4954:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 4955:                         if (!$pubkey) {
 4956:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 4957:                         } else {
 4958:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 4959:                         }
 4960:                         if (!$privkey) {
 4961:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 4962:                         } else {
 4963:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 4964:                         }
 4965:                         $chgtxt .= '</ul>';
 4966:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4967:                     }
 4968:                 } else {
 4969:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4970:                 }
 4971:             }
 4972:             $resulttext .= $colchgtext.'</ul>';
 4973:         } else {
 4974:             $resulttext = &mt('No changes made to log-in page settings');
 4975:         }
 4976:     } else {
 4977:         $resulttext = '<span class="LC_error">'.
 4978: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4979:     }
 4980:     if ($errors) {
 4981:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 4982:                        $errors.'</ul>';
 4983:     }
 4984:     return $resulttext;
 4985: }
 4986: 
 4987: sub color_font_choices {
 4988:     my %choices =
 4989:         &Apache::lonlocal::texthash (
 4990:             img => "Header",
 4991:             bgs => "Background colors",
 4992:             links => "Link colors",
 4993:             images => "Images",
 4994:             font => "Font color",
 4995:             fontmenu => "Font Menu",
 4996:             pgbg => "Page",
 4997:             tabbg => "Header",
 4998:             sidebg => "Border",
 4999:             link => "Link",
 5000:             alink => "Active link",
 5001:             vlink => "Visited link",
 5002:         );
 5003:     return %choices;
 5004: }
 5005: 
 5006: sub modify_rolecolors {
 5007:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 5008:     my ($resulttext,%rolehash);
 5009:     $rolehash{'rolecolors'} = {};
 5010:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5011:         if ($domconfig{'rolecolors'} eq '') {
 5012:             $domconfig{'rolecolors'} = {};
 5013:         }
 5014:     }
 5015:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5016:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5017:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5018:                                              $dom);
 5019:     if ($putresult eq 'ok') {
 5020:         if (keys(%changes) > 0) {
 5021:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5022:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5023:                                              $rolehash{'rolecolors'});
 5024:         } else {
 5025:             $resulttext = &mt('No changes made to default color schemes');
 5026:         }
 5027:     } else {
 5028:         $resulttext = '<span class="LC_error">'.
 5029: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5030:     }
 5031:     if ($errors) {
 5032:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5033:                        $errors.'</ul>';
 5034:     }
 5035:     return $resulttext;
 5036: }
 5037: 
 5038: sub modify_colors {
 5039:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5040:     my (%changes,%choices);
 5041:     my @bgs;
 5042:     my @links = ('link','alink','vlink');
 5043:     my @logintext;
 5044:     my @images;
 5045:     my $servadm = $r->dir_config('lonAdmEMail');
 5046:     my $errors;
 5047:     foreach my $role (@{$roles}) {
 5048:         if ($role eq 'login') {
 5049:             %choices = &login_choices();
 5050:             @logintext = ('textcol','bgcol');
 5051:         } else {
 5052:             %choices = &color_font_choices();
 5053:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5054:         }
 5055:         if ($role eq 'login') {
 5056:             @images = ('img','logo','domlogo','login');
 5057:             @bgs = ('pgbg','mainbg','sidebg');
 5058:         } else {
 5059:             @images = ('img');
 5060:             @bgs = ('pgbg','tabbg','sidebg'); 
 5061:         }
 5062:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5063:         foreach my $item (@bgs,@links,@logintext) {
 5064:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5065:         }
 5066:         my ($configuserok,$author_ok,$switchserver) = 
 5067:             &config_check($dom,$confname,$servadm);
 5068:         my ($width,$height) = &thumb_dimensions();
 5069:         if (ref($domconfig->{$role}) ne 'HASH') {
 5070:             $domconfig->{$role} = {};
 5071:         }
 5072:         foreach my $img (@images) {
 5073:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5074:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5075:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5076:                 } else { 
 5077:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5078:                 }
 5079:             } 
 5080: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5081: 		 && !defined($domconfig->{$role}{$img})
 5082: 		 && !$env{'form.'.$role.'_del_'.$img}
 5083: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5084: 		# import the old configured image from the .tab setting
 5085: 		# if they haven't provided a new one 
 5086: 		$domconfig->{$role}{$img} = 
 5087: 		    $env{'form.'.$role.'_import_'.$img};
 5088: 	    }
 5089:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5090:                 my $error;
 5091:                 if ($configuserok eq 'ok') {
 5092:                     if ($switchserver) {
 5093:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5094:                     } else {
 5095:                         if ($author_ok eq 'ok') {
 5096:                             my ($result,$logourl) = 
 5097:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5098:                                            $dom,$confname,$img,$width,$height);
 5099:                             if ($result eq 'ok') {
 5100:                                 $confhash->{$role}{$img} = $logourl;
 5101:                                 $changes{$role}{'images'}{$img} = 1;
 5102:                             } else {
 5103:                                 $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);
 5104:                             }
 5105:                         } else {
 5106:                             $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);
 5107:                         }
 5108:                     }
 5109:                 } else {
 5110:                     $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);
 5111:                 }
 5112:                 if ($error) {
 5113:                     &Apache::lonnet::logthis($error);
 5114:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5115:                 }
 5116:             } elsif ($domconfig->{$role}{$img} ne '') {
 5117:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5118:                     my $error;
 5119:                     if ($configuserok eq 'ok') {
 5120: # is confname an author?
 5121:                         if ($switchserver eq '') {
 5122:                             if ($author_ok eq 'ok') {
 5123:                                 my ($result,$logourl) = 
 5124:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5125:                                             $dom,$confname,$img,$width,$height);
 5126:                                 if ($result eq 'ok') {
 5127:                                     $confhash->{$role}{$img} = $logourl;
 5128: 				    $changes{$role}{'images'}{$img} = 1;
 5129:                                 }
 5130:                             }
 5131:                         }
 5132:                     }
 5133:                 }
 5134:             }
 5135:         }
 5136:         if (ref($domconfig) eq 'HASH') {
 5137:             if (ref($domconfig->{$role}) eq 'HASH') {
 5138:                 foreach my $img (@images) {
 5139:                     if ($domconfig->{$role}{$img} ne '') {
 5140:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5141:                             $confhash->{$role}{$img} = '';
 5142:                             $changes{$role}{'images'}{$img} = 1;
 5143:                         } else {
 5144:                             if ($confhash->{$role}{$img} eq '') {
 5145:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5146:                             }
 5147:                         }
 5148:                     } else {
 5149:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5150:                             $confhash->{$role}{$img} = '';
 5151:                             $changes{$role}{'images'}{$img} = 1;
 5152:                         } 
 5153:                     }
 5154:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5155:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5156:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5157:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5158:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5159:                             }
 5160:                         } else {
 5161:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5162:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5163:                             }
 5164:                         }
 5165:                     }
 5166:                 }
 5167:                 if ($domconfig->{$role}{'font'} ne '') {
 5168:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5169:                         $changes{$role}{'font'} = 1;
 5170:                     }
 5171:                 } else {
 5172:                     if ($confhash->{$role}{'font'}) {
 5173:                         $changes{$role}{'font'} = 1;
 5174:                     }
 5175:                 }
 5176:                 if ($role ne 'login') {
 5177:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5178:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5179:                             $changes{$role}{'fontmenu'} = 1;
 5180:                         }
 5181:                     } else {
 5182:                         if ($confhash->{$role}{'fontmenu'}) {
 5183:                             $changes{$role}{'fontmenu'} = 1;
 5184:                         }
 5185:                     }
 5186:                 }
 5187:                 foreach my $item (@bgs) {
 5188:                     if ($domconfig->{$role}{$item} ne '') {
 5189:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5190:                             $changes{$role}{'bgs'}{$item} = 1;
 5191:                         } 
 5192:                     } else {
 5193:                         if ($confhash->{$role}{$item}) {
 5194:                             $changes{$role}{'bgs'}{$item} = 1;
 5195:                         }
 5196:                     }
 5197:                 }
 5198:                 foreach my $item (@links) {
 5199:                     if ($domconfig->{$role}{$item} ne '') {
 5200:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5201:                             $changes{$role}{'links'}{$item} = 1;
 5202:                         }
 5203:                     } else {
 5204:                         if ($confhash->{$role}{$item}) {
 5205:                             $changes{$role}{'links'}{$item} = 1;
 5206:                         }
 5207:                     }
 5208:                 }
 5209:                 foreach my $item (@logintext) {
 5210:                     if ($domconfig->{$role}{$item} ne '') {
 5211:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5212:                             $changes{$role}{'logintext'}{$item} = 1;
 5213:                         }
 5214:                     } else {
 5215:                         if ($confhash->{$role}{$item}) {
 5216:                             $changes{$role}{'logintext'}{$item} = 1;
 5217:                         }
 5218:                     }
 5219:                 }
 5220:             } else {
 5221:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5222:                                         \@logintext,$confhash,\%changes); 
 5223:             }
 5224:         } else {
 5225:             &default_change_checker($role,\@images,\@links,\@bgs,
 5226:                                     \@logintext,$confhash,\%changes); 
 5227:         }
 5228:     }
 5229:     return ($errors,%changes);
 5230: }
 5231: 
 5232: sub config_check {
 5233:     my ($dom,$confname,$servadm) = @_;
 5234:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5235:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5236:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5237:                                                    $confname,$servadm);
 5238:     if ($configuserok eq 'ok') {
 5239:         $switchserver = &check_switchserver($dom,$confname);
 5240:         if ($switchserver eq '') {
 5241:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5242:         }
 5243:     }
 5244:     return ($configuserok,$author_ok,$switchserver);
 5245: }
 5246: 
 5247: sub default_change_checker {
 5248:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5249:     foreach my $item (@{$links}) {
 5250:         if ($confhash->{$role}{$item}) {
 5251:             $changes->{$role}{'links'}{$item} = 1;
 5252:         }
 5253:     }
 5254:     foreach my $item (@{$bgs}) {
 5255:         if ($confhash->{$role}{$item}) {
 5256:             $changes->{$role}{'bgs'}{$item} = 1;
 5257:         }
 5258:     }
 5259:     foreach my $item (@{$logintext}) {
 5260:         if ($confhash->{$role}{$item}) {
 5261:             $changes->{$role}{'logintext'}{$item} = 1;
 5262:         }
 5263:     }
 5264:     foreach my $img (@{$images}) {
 5265:         if ($env{'form.'.$role.'_del_'.$img}) {
 5266:             $confhash->{$role}{$img} = '';
 5267:             $changes->{$role}{'images'}{$img} = 1;
 5268:         }
 5269:         if ($role eq 'login') {
 5270:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5271:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5272:             }
 5273:         }
 5274:     }
 5275:     if ($confhash->{$role}{'font'}) {
 5276:         $changes->{$role}{'font'} = 1;
 5277:     }
 5278: }
 5279: 
 5280: sub display_colorchgs {
 5281:     my ($dom,$changes,$roles,$confhash) = @_;
 5282:     my (%choices,$resulttext);
 5283:     if (!grep(/^login$/,@{$roles})) {
 5284:         $resulttext = &mt('Changes made:').'<br />';
 5285:     }
 5286:     foreach my $role (@{$roles}) {
 5287:         if ($role eq 'login') {
 5288:             %choices = &login_choices();
 5289:         } else {
 5290:             %choices = &color_font_choices();
 5291:         }
 5292:         if (ref($changes->{$role}) eq 'HASH') {
 5293:             if ($role ne 'login') {
 5294:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5295:             }
 5296:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5297:                 if ($role ne 'login') {
 5298:                     $resulttext .= '<ul>';
 5299:                 }
 5300:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5301:                     if ($role ne 'login') {
 5302:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5303:                     }
 5304:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5305:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5306:                             if ($confhash->{$role}{$key}{$item}) {
 5307:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5308:                             } else {
 5309:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5310:                             }
 5311:                         } elsif ($confhash->{$role}{$item} eq '') {
 5312:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5313:                         } else {
 5314:                             my $newitem = $confhash->{$role}{$item};
 5315:                             if ($key eq 'images') {
 5316:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5317:                             }
 5318:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5319:                         }
 5320:                     }
 5321:                     if ($role ne 'login') {
 5322:                         $resulttext .= '</ul></li>';
 5323:                     }
 5324:                 } else {
 5325:                     if ($confhash->{$role}{$key} eq '') {
 5326:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5327:                     } else {
 5328:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5329:                     }
 5330:                 }
 5331:                 if ($role ne 'login') {
 5332:                     $resulttext .= '</ul>';
 5333:                 }
 5334:             }
 5335:         }
 5336:     }
 5337:     return $resulttext;
 5338: }
 5339: 
 5340: sub thumb_dimensions {
 5341:     return ('200','50');
 5342: }
 5343: 
 5344: sub check_dimensions {
 5345:     my ($inputfile) = @_;
 5346:     my ($fullwidth,$fullheight);
 5347:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5348:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5349:             my $imageinfo = <PIPE>;
 5350:             if (!close(PIPE)) {
 5351:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5352:             }
 5353:             chomp($imageinfo);
 5354:             my ($fullsize) = 
 5355:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5356:             if ($fullsize) {
 5357:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5358:             }
 5359:         }
 5360:     }
 5361:     return ($fullwidth,$fullheight);
 5362: }
 5363: 
 5364: sub check_configuser {
 5365:     my ($uhome,$dom,$confname,$servadm) = @_;
 5366:     my ($configuserok,%currroles);
 5367:     if ($uhome eq 'no_host') {
 5368:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5369:         my $configpass = &LONCAPA::Enrollment::create_password();
 5370:         $configuserok = 
 5371:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5372:                              $configpass,'','','','','',undef,$servadm);
 5373:     } else {
 5374:         $configuserok = 'ok';
 5375:         %currroles = 
 5376:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5377:     }
 5378:     return ($configuserok,%currroles);
 5379: }
 5380: 
 5381: sub check_authorstatus {
 5382:     my ($dom,$confname,%currroles) = @_;
 5383:     my $author_ok;
 5384:     if (!$currroles{':'.$dom.':au'}) {
 5385:         my $start = time;
 5386:         my $end = 0;
 5387:         $author_ok = 
 5388:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5389:                                         'au',$end,$start,'','','domconfig');
 5390:     } else {
 5391:         $author_ok = 'ok';
 5392:     }
 5393:     return $author_ok;
 5394: }
 5395: 
 5396: sub publishlogo {
 5397:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5398:     my ($output,$fname,$logourl);
 5399:     if ($action eq 'upload') {
 5400:         $fname=$env{'form.'.$formname.'.filename'};
 5401:         chop($env{'form.'.$formname});
 5402:     } else {
 5403:         ($fname) = ($formname =~ /([^\/]+)$/);
 5404:     }
 5405:     if ($savefileas ne '') {
 5406:         $fname = $savefileas;
 5407:     }
 5408:     $fname=&Apache::lonnet::clean_filename($fname);
 5409: # See if there is anything left
 5410:     unless ($fname) { return ('error: no uploaded file'); }
 5411:     $fname="$subdir/$fname";
 5412:     my $docroot=$r->dir_config('lonDocRoot');
 5413:     my $filepath="$docroot/priv";
 5414:     my $relpath = "$dom/$confname";
 5415:     my ($fnamepath,$file,$fetchthumb);
 5416:     $file=$fname;
 5417:     if ($fname=~m|/|) {
 5418:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5419:     }
 5420:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");  
 5421:     my $count;
 5422:     for ($count=5;$count<=$#parts;$count++) {
 5423:         $filepath.="/$parts[$count]";
 5424:         if ((-e $filepath)!=1) {
 5425:             mkdir($filepath,02770);
 5426:         }
 5427:     }
 5428:     # Check for bad extension and disallow upload
 5429:     if ($file=~/\.(\w+)$/ &&
 5430:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5431:         $output = 
 5432:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 5433:     } elsif ($file=~/\.(\w+)$/ &&
 5434:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5435:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5436:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5437:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5438:     } elsif (-d "$filepath/$file") {
 5439:         $output = &mt('File name is a directory name - rename the file and re-upload');
 5440:     } else {
 5441:         my $source = $filepath.'/'.$file;
 5442:         my $logfile;
 5443:         if (!open($logfile,">>$source".'.log')) {
 5444:             return (&mt('No write permission to Construction Space'));
 5445:         }
 5446:         print $logfile
 5447: "\n================= Publish ".localtime()." ================\n".
 5448: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5449: # Save the file
 5450:         if (!open(FH,'>'.$source)) {
 5451:             &Apache::lonnet::logthis('Failed to create '.$source);
 5452:             return (&mt('Failed to create file'));
 5453:         }
 5454:         if ($action eq 'upload') {
 5455:             if (!print FH ($env{'form.'.$formname})) {
 5456:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5457:                 return (&mt('Failed to write file'));
 5458:             }
 5459:         } else {
 5460:             my $original = &Apache::lonnet::filelocation('',$formname);
 5461:             if(!copy($original,$source)) {
 5462:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5463:                 return (&mt('Failed to write file'));
 5464:             }
 5465:         }
 5466:         close(FH);
 5467:         chmod(0660, $source); # Permissions to rw-rw---.
 5468: 
 5469:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5470:         my $copyfile=$targetdir.'/'.$file;
 5471: 
 5472:         my @parts=split(/\//,$targetdir);
 5473:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5474:         for (my $count=5;$count<=$#parts;$count++) {
 5475:             $path.="/$parts[$count]";
 5476:             if (!-e $path) {
 5477:                 print $logfile "\nCreating directory ".$path;
 5478:                 mkdir($path,02770);
 5479:             }
 5480:         }
 5481:         my $versionresult;
 5482:         if (-e $copyfile) {
 5483:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5484:         } else {
 5485:             $versionresult = 'ok';
 5486:         }
 5487:         if ($versionresult eq 'ok') {
 5488:             if (copy($source,$copyfile)) {
 5489:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5490:                 $output = 'ok';
 5491:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5492:                 push(@{$modified_urls},[$copyfile,$source]);
 5493:                 my $metaoutput = 
 5494:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5495:                 unless ($registered_cleanup) {
 5496:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5497:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5498:                     $registered_cleanup=1;
 5499:                 }
 5500:             } else {
 5501:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5502:                 $output = &mt('Failed to copy file to RES space').", $!";
 5503:             }
 5504:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5505:                 my $inputfile = $filepath.'/'.$file;
 5506:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5507:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5508:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5509:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5510:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5511:                         system("convert -sample $thumbsize $inputfile $outfile");
 5512:                         chmod(0660, $filepath.'/tn-'.$file);
 5513:                         if (-e $outfile) {
 5514:                             my $copyfile=$targetdir.'/tn-'.$file;
 5515:                             if (copy($outfile,$copyfile)) {
 5516:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5517:                                 my $thumb_metaoutput = 
 5518:                                     &write_metadata($dom,$confname,$formname,
 5519:                                                     $targetdir,'tn-'.$file,$logfile);
 5520:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5521:                                 unless ($registered_cleanup) {
 5522:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5523:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5524:                                     $registered_cleanup=1;
 5525:                                 }
 5526:                             } else {
 5527:                                 print $logfile "\nUnable to write ".$copyfile.
 5528:                                                ':'.$!."\n";
 5529:                             }
 5530:                         }
 5531:                     }
 5532:                 }
 5533:             }
 5534:         } else {
 5535:             $output = $versionresult;
 5536:         }
 5537:     }
 5538:     return ($output,$logourl);
 5539: }
 5540: 
 5541: sub logo_versioning {
 5542:     my ($targetdir,$file,$logfile) = @_;
 5543:     my $target = $targetdir.'/'.$file;
 5544:     my ($maxversion,$fn,$extn,$output);
 5545:     $maxversion = 0;
 5546:     if ($file =~ /^(.+)\.(\w+)$/) {
 5547:         $fn=$1;
 5548:         $extn=$2;
 5549:     }
 5550:     opendir(DIR,$targetdir);
 5551:     while (my $filename=readdir(DIR)) {
 5552:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5553:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5554:         }
 5555:     }
 5556:     $maxversion++;
 5557:     print $logfile "\nCreating old version ".$maxversion."\n";
 5558:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5559:     if (copy($target,$copyfile)) {
 5560:         print $logfile "Copied old target to ".$copyfile."\n";
 5561:         $copyfile=$copyfile.'.meta';
 5562:         if (copy($target.'.meta',$copyfile)) {
 5563:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5564:             $output = 'ok';
 5565:         } else {
 5566:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5567:             $output = &mt('Failed to copy old meta').", $!, ";
 5568:         }
 5569:     } else {
 5570:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5571:         $output = &mt('Failed to copy old target').", $!, ";
 5572:     }
 5573:     return $output;
 5574: }
 5575: 
 5576: sub write_metadata {
 5577:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5578:     my (%metadatafields,%metadatakeys,$output);
 5579:     $metadatafields{'title'}=$formname;
 5580:     $metadatafields{'creationdate'}=time;
 5581:     $metadatafields{'lastrevisiondate'}=time;
 5582:     $metadatafields{'copyright'}='public';
 5583:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5584:                                          $env{'user.domain'};
 5585:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5586:     $metadatafields{'domain'}=$dom;
 5587:     {
 5588:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5589:         my $mfh;
 5590:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5591:             foreach (sort(keys(%metadatafields))) {
 5592:                 unless ($_=~/\./) {
 5593:                     my $unikey=$_;
 5594:                     $unikey=~/^([A-Za-z]+)/;
 5595:                     my $tag=$1;
 5596:                     $tag=~tr/A-Z/a-z/;
 5597:                     print $mfh "\n\<$tag";
 5598:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5599:                         my $value=$metadatafields{$unikey.'.'.$_};
 5600:                         $value=~s/\"/\'\'/g;
 5601:                         print $mfh ' '.$_.'="'.$value.'"';
 5602:                     }
 5603:                     print $mfh '>'.
 5604:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5605:                             .'</'.$tag.'>';
 5606:                 }
 5607:             }
 5608:             $output = 'ok';
 5609:             print $logfile "\nWrote metadata";
 5610:             close($mfh);
 5611:         } else {
 5612:             print $logfile "\nFailed to open metadata file";
 5613:             $output = &mt('Could not write metadata');
 5614:         }
 5615:     }
 5616:     return $output;
 5617: }
 5618: 
 5619: sub notifysubscribed {
 5620:     foreach my $targetsource (@{$modified_urls}){
 5621:         next unless (ref($targetsource) eq 'ARRAY');
 5622:         my ($target,$source)=@{$targetsource};
 5623:         if ($source ne '') {
 5624:             if (open(my $logfh,'>>'.$source.'.log')) {
 5625:                 print $logfh "\nCleanup phase: Notifications\n";
 5626:                 my @subscribed=&subscribed_hosts($target);
 5627:                 foreach my $subhost (@subscribed) {
 5628:                     print $logfh "\nNotifying host ".$subhost.':';
 5629:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5630:                     print $logfh $reply;
 5631:                 }
 5632:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5633:                 foreach my $subhost (@subscribedmeta) {
 5634:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5635:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5636:                                                         $subhost);
 5637:                     print $logfh $reply;
 5638:                 }
 5639:                 print $logfh "\n============ Done ============\n";
 5640:                 close($logfh);
 5641:             }
 5642:         }
 5643:     }
 5644:     return OK;
 5645: }
 5646: 
 5647: sub subscribed_hosts {
 5648:     my ($target) = @_;
 5649:     my @subscribed;
 5650:     if (open(my $fh,"<$target.subscription")) {
 5651:         while (my $subline=<$fh>) {
 5652:             if ($subline =~ /^($match_lonid):/) {
 5653:                 my $host = $1;
 5654:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5655:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5656:                         push(@subscribed,$host);
 5657:                     }
 5658:                 }
 5659:             }
 5660:         }
 5661:     }
 5662:     return @subscribed;
 5663: }
 5664: 
 5665: sub check_switchserver {
 5666:     my ($dom,$confname) = @_;
 5667:     my ($allowed,$switchserver);
 5668:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5669:     if ($home eq 'no_host') {
 5670:         $home = &Apache::lonnet::domain($dom,'primary');
 5671:     }
 5672:     my @ids=&Apache::lonnet::current_machine_ids();
 5673:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5674:     if (!$allowed) {
 5675: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5676:     }
 5677:     return $switchserver;
 5678: }
 5679: 
 5680: sub modify_quotas {
 5681:     my ($dom,$action,%domconfig) = @_;
 5682:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5683:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5684:     if ($action eq 'quotas') {
 5685:         $context = 'tools'; 
 5686:     } else { 
 5687:         $context = $action;
 5688:     }
 5689:     if ($context eq 'requestcourses') {
 5690:         @usertools = ('official','unofficial','community');
 5691:         @options =('norequest','approval','validate','autolimit');
 5692:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5693:         %titles = &courserequest_titles();
 5694:         $toolregexp = join('|',@usertools);
 5695:         %conditions = &courserequest_conditions();
 5696:     } elsif ($context eq 'requestauthor') {
 5697:         @usertools = ('author');
 5698:         %titles = &authorrequest_titles();
 5699:     } else {
 5700:         @usertools = ('aboutme','blog','webdav','portfolio');
 5701:         %titles = &tool_titles();
 5702:     }
 5703:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5704:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5705:     foreach my $key (keys(%env)) {
 5706:         if ($context eq 'requestcourses') {
 5707:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5708:                 my $item = $1;
 5709:                 my $type = $2;
 5710:                 if ($type =~ /^limit_(.+)/) {
 5711:                     $limithash{$item}{$1} = $env{$key};
 5712:                 } else {
 5713:                     $confhash{$item}{$type} = $env{$key};
 5714:                 }
 5715:             }
 5716:         } elsif ($context eq 'requestauthor') {
 5717:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5718:                 $confhash{$1} = $env{$key};
 5719:             }
 5720:         } else {
 5721:             if ($key =~ /^form\.quota_(.+)$/) {
 5722:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5723:             }
 5724:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5725:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5726:             }
 5727:         }
 5728:     }
 5729:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5730:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5731:         @approvalnotify = sort(@approvalnotify);
 5732:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5733:         if (ref($domconfig{$action}) eq 'HASH') {
 5734:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5735:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5736:                     $changes{'notify'}{'approval'} = 1;
 5737:                 }
 5738:             } else {
 5739:                 if ($confhash{'notify'}{'approval'}) {
 5740:                     $changes{'notify'}{'approval'} = 1;
 5741:                 }
 5742:             }
 5743:         } else {
 5744:             if ($confhash{'notify'}{'approval'}) {
 5745:                 $changes{'notify'}{'approval'} = 1;
 5746:             }
 5747:         }
 5748:     } else {
 5749:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5750:     }
 5751:     foreach my $item (@usertools) {
 5752:         foreach my $type (@{$types},'default','_LC_adv') {
 5753:             my $unset; 
 5754:             if ($context eq 'requestcourses') {
 5755:                 $unset = '0';
 5756:                 if ($type eq '_LC_adv') {
 5757:                     $unset = '';
 5758:                 }
 5759:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5760:                     $confhash{$item}{$type} .= '=';
 5761:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5762:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5763:                     }
 5764:                 }
 5765:             } elsif ($context eq 'requestauthor') {
 5766:                 $unset = '0';
 5767:                 if ($type eq '_LC_adv') {
 5768:                     $unset = '';
 5769:                 }
 5770:             } else {
 5771:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5772:                     $confhash{$item}{$type} = 1;
 5773:                 } else {
 5774:                     $confhash{$item}{$type} = 0;
 5775:                 }
 5776:             }
 5777:             if (ref($domconfig{$action}) eq 'HASH') {
 5778:                 if ($action eq 'requestauthor') {
 5779:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5780:                         $changes{$type} = 1;
 5781:                     }
 5782:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5783:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5784:                         $changes{$item}{$type} = 1;
 5785:                     }
 5786:                 } else {
 5787:                     if ($context eq 'requestcourses') {
 5788:                         if ($confhash{$item}{$type} ne $unset) {
 5789:                             $changes{$item}{$type} = 1;
 5790:                         }
 5791:                     } else {
 5792:                         if (!$confhash{$item}{$type}) {
 5793:                             $changes{$item}{$type} = 1;
 5794:                         }
 5795:                     }
 5796:                 }
 5797:             } else {
 5798:                 if ($context eq 'requestcourses') {
 5799:                     if ($confhash{$item}{$type} ne $unset) {
 5800:                         $changes{$item}{$type} = 1;
 5801:                     }
 5802:                 } elsif ($context eq 'requestauthor') {
 5803:                     if ($confhash{$type} ne $unset) {
 5804:                         $changes{$type} = 1;
 5805:                     }
 5806:                 } else {
 5807:                     if (!$confhash{$item}{$type}) {
 5808:                         $changes{$item}{$type} = 1;
 5809:                     }
 5810:                 }
 5811:             }
 5812:         }
 5813:     }
 5814:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5815:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5816:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5817:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5818:                     if (exists($confhash{'defaultquota'}{$key})) {
 5819:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5820:                             $changes{'defaultquota'}{$key} = 1;
 5821:                         }
 5822:                     } else {
 5823:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5824:                     }
 5825:                 }
 5826:             } else {
 5827:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5828:                     if (exists($confhash{'defaultquota'}{$key})) {
 5829:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5830:                             $changes{'defaultquota'}{$key} = 1;
 5831:                         }
 5832:                     } else {
 5833:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5834:                     }
 5835:                 }
 5836:             }
 5837:         }
 5838:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5839:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5840:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5841:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5842:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5843:                             $changes{'defaultquota'}{$key} = 1;
 5844:                         }
 5845:                     } else {
 5846:                         if (!exists($domconfig{'quotas'}{$key})) {
 5847:                             $changes{'defaultquota'}{$key} = 1;
 5848:                         }
 5849:                     }
 5850:                 } else {
 5851:                     $changes{'defaultquota'}{$key} = 1;
 5852:                 }
 5853:             }
 5854:         }
 5855:     }
 5856: 
 5857:     if ($context eq 'requestauthor') {
 5858:         $domdefaults{'requestauthor'} = \%confhash;
 5859:     } else {
 5860:         foreach my $key (keys(%confhash)) {
 5861:             $domdefaults{$key} = $confhash{$key};
 5862:         }
 5863:     }
 5864: 
 5865:     my %quotahash = (
 5866:                       $action => { %confhash }
 5867:                     );
 5868:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5869:                                              $dom);
 5870:     if ($putresult eq 'ok') {
 5871:         if (keys(%changes) > 0) {
 5872:             my $cachetime = 24*60*60;
 5873:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5874: 
 5875:             $resulttext = &mt('Changes made:').'<ul>';
 5876:             unless (($context eq 'requestcourses') ||
 5877:                     ($context eq 'requestauthor')) {
 5878:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5879:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5880:                     foreach my $type (@{$types},'default') {
 5881:                         if (defined($changes{'defaultquota'}{$type})) {
 5882:                             my $typetitle = $usertypes->{$type};
 5883:                             if ($type eq 'default') {
 5884:                                 $typetitle = $othertitle;
 5885:                             }
 5886:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5887:                         }
 5888:                     }
 5889:                     $resulttext .= '</ul></li>';
 5890:                 }
 5891:             }
 5892:             my %newenv;
 5893:             foreach my $item (@usertools) {
 5894:                 my (%haschgs,%inconf);
 5895:                 if ($context eq 'requestauthor') {
 5896:                     %haschgs = %changes;
 5897:                     %inconf = %confhash;
 5898:                 } else {
 5899:                     if (ref($changes{$item}) eq 'HASH') {
 5900:                         %haschgs = %{$changes{$item}};
 5901:                     }
 5902:                     if (ref($confhash{$item}) eq 'HASH') {
 5903:                         %inconf = %{$confhash{$item}};
 5904:                     }
 5905:                 }
 5906:                 if (keys(%haschgs) > 0) {
 5907:                     my $newacc = 
 5908:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5909:                                                           $env{'user.domain'},
 5910:                                                           $item,'reload',$context);
 5911:                     if (($context eq 'requestcourses') ||
 5912:                         ($context eq 'requestauthor')) {
 5913:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5914:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5915:                         }
 5916:                     } else {
 5917:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5918:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5919:                         }
 5920:                     }
 5921:                     unless ($context eq 'requestauthor') {
 5922:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5923:                     }
 5924:                     foreach my $type (@{$types},'default','_LC_adv') {
 5925:                         if ($haschgs{$type}) {
 5926:                             my $typetitle = $usertypes->{$type};
 5927:                             if ($type eq 'default') {
 5928:                                 $typetitle = $othertitle;
 5929:                             } elsif ($type eq '_LC_adv') {
 5930:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5931:                             }
 5932:                             if ($inconf{$type}) {
 5933:                                 if ($context eq 'requestcourses') {
 5934:                                     my $cond;
 5935:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 5936:                                         if ($1 eq '') {
 5937:                                             $cond = &mt('(Automatic processing of any request).');
 5938:                                         } else {
 5939:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5940:                                         }
 5941:                                     } else { 
 5942:                                         $cond = $conditions{$inconf{$type}};
 5943:                                     }
 5944:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5945:                                 } elsif ($context eq 'requestauthor') {
 5946:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 5947:                                                              $titles{$inconf{$type}},$typetitle);
 5948: 
 5949:                                 } else {
 5950:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5951:                                 }
 5952:                             } else {
 5953:                                 if ($type eq '_LC_adv') {
 5954:                                     if ($inconf{$type} eq '0') {
 5955:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5956:                                     } else { 
 5957:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5958:                                     }
 5959:                                 } else {
 5960:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5961:                                 }
 5962:                             }
 5963:                         }
 5964:                     }
 5965:                     unless ($context eq 'requestauthor') {
 5966:                         $resulttext .= '</ul></li>';
 5967:                     }
 5968:                 }
 5969:             }
 5970:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 5971:                 if (ref($changes{'notify'}) eq 'HASH') {
 5972:                     if ($changes{'notify'}{'approval'}) {
 5973:                         if (ref($confhash{'notify'}) eq 'HASH') {
 5974:                             if ($confhash{'notify'}{'approval'}) {
 5975:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 5976:                             } else {
 5977:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 5978:                             }
 5979:                         }
 5980:                     }
 5981:                 }
 5982:             }
 5983:             $resulttext .= '</ul>';
 5984:             if (keys(%newenv)) {
 5985:                 &Apache::lonnet::appenv(\%newenv);
 5986:             }
 5987:         } else {
 5988:             if ($context eq 'requestcourses') {
 5989:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 5990:             } elsif ($context eq 'requestauthor') {
 5991:                 $resulttext = &mt('No changes made to rights to request author space.');
 5992:             } else {
 5993:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 5994:             }
 5995:         }
 5996:     } else {
 5997:         $resulttext = '<span class="LC_error">'.
 5998: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5999:     }
 6000:     return $resulttext;
 6001: }
 6002: 
 6003: sub modify_autoenroll {
 6004:     my ($dom,%domconfig) = @_;
 6005:     my ($resulttext,%changes);
 6006:     my %currautoenroll;
 6007:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 6008:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 6009:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 6010:         }
 6011:     }
 6012:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6013:     my %title = ( run => 'Auto-enrollment active',
 6014:                   sender => 'Sender for notification messages',
 6015:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6016:     my @offon = ('off','on');
 6017:     my $sender_uname = $env{'form.sender_uname'};
 6018:     my $sender_domain = $env{'form.sender_domain'};
 6019:     if ($sender_domain eq '') {
 6020:         $sender_uname = '';
 6021:     } elsif ($sender_uname eq '') {
 6022:         $sender_domain = '';
 6023:     }
 6024:     my $coowners = $env{'form.autoassign_coowners'};
 6025:     my %autoenrollhash =  (
 6026:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6027:                                        'sender_uname' => $sender_uname,
 6028:                                        'sender_domain' => $sender_domain,
 6029:                                        'co-owners' => $coowners,
 6030:                                 }
 6031:                      );
 6032:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6033:                                              $dom);
 6034:     if ($putresult eq 'ok') {
 6035:         if (exists($currautoenroll{'run'})) {
 6036:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6037:                  $changes{'run'} = 1;
 6038:              }
 6039:         } elsif ($autorun) {
 6040:             if ($env{'form.autoenroll_run'} ne '1') {
 6041:                  $changes{'run'} = 1;
 6042:             }
 6043:         }
 6044:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6045:             $changes{'sender'} = 1;
 6046:         }
 6047:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6048:             $changes{'sender'} = 1;
 6049:         }
 6050:         if ($currautoenroll{'co-owners'} ne '') {
 6051:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6052:                 $changes{'coowners'} = 1;
 6053:             }
 6054:         } elsif ($coowners) {
 6055:             $changes{'coowners'} = 1;
 6056:         }      
 6057:         if (keys(%changes) > 0) {
 6058:             $resulttext = &mt('Changes made:').'<ul>';
 6059:             if ($changes{'run'}) {
 6060:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6061:             }
 6062:             if ($changes{'sender'}) {
 6063:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6064:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6065:                 } else {
 6066:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6067:                 }
 6068:             }
 6069:             if ($changes{'coowners'}) {
 6070:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6071:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6072:             }
 6073:             $resulttext .= '</ul>';
 6074:         } else {
 6075:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6076:         }
 6077:     } else {
 6078:         $resulttext = '<span class="LC_error">'.
 6079: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6080:     }
 6081:     return $resulttext;
 6082: }
 6083: 
 6084: sub modify_autoupdate {
 6085:     my ($dom,%domconfig) = @_;
 6086:     my ($resulttext,%currautoupdate,%fields,%changes);
 6087:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6088:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6089:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6090:         }
 6091:     }
 6092:     my @offon = ('off','on');
 6093:     my %title = &Apache::lonlocal::texthash (
 6094:                    run => 'Auto-update:',
 6095:                    classlists => 'Updates to user information in classlists?'
 6096:                 );
 6097:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6098:     my %fieldtitles = &Apache::lonlocal::texthash (
 6099:                         id => 'Student/Employee ID',
 6100:                         permanentemail => 'E-mail address',
 6101:                         lastname => 'Last Name',
 6102:                         firstname => 'First Name',
 6103:                         middlename => 'Middle Name',
 6104:                         generation => 'Generation',
 6105:                       );
 6106:     $othertitle = &mt('All users');
 6107:     if (keys(%{$usertypes}) >  0) {
 6108:         $othertitle = &mt('Other users');
 6109:     }
 6110:     foreach my $key (keys(%env)) {
 6111:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6112:             my ($usertype,$item) = ($1,$2);
 6113:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6114:                 if ($usertype eq 'default') {   
 6115:                     push(@{$fields{$1}},$2);
 6116:                 } elsif (ref($types) eq 'ARRAY') {
 6117:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6118:                         push(@{$fields{$1}},$2);
 6119:                     }
 6120:                 }
 6121:             }
 6122:         }
 6123:     }
 6124:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6125:     @lockablenames = sort(@lockablenames);
 6126:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6127:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6128:         if (@changed) {
 6129:             $changes{'lockablenames'} = 1;
 6130:         }
 6131:     } else {
 6132:         if (@lockablenames) {
 6133:             $changes{'lockablenames'} = 1;
 6134:         }
 6135:     }
 6136:     my %updatehash = (
 6137:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6138:                                       classlists => $env{'form.classlists'},
 6139:                                       fields => {%fields},
 6140:                                       lockablenames => \@lockablenames,
 6141:                                     }
 6142:                      );
 6143:     foreach my $key (keys(%currautoupdate)) {
 6144:         if (($key eq 'run') || ($key eq 'classlists')) {
 6145:             if (exists($updatehash{autoupdate}{$key})) {
 6146:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6147:                     $changes{$key} = 1;
 6148:                 }
 6149:             }
 6150:         } elsif ($key eq 'fields') {
 6151:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6152:                 foreach my $item (@{$types},'default') {
 6153:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6154:                         my $change = 0;
 6155:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6156:                             if (!exists($fields{$item})) {
 6157:                                 $change = 1;
 6158:                                 last;
 6159:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6160:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6161:                                     $change = 1;
 6162:                                     last;
 6163:                                 }
 6164:                             }
 6165:                         }
 6166:                         if ($change) {
 6167:                             push(@{$changes{$key}},$item);
 6168:                         }
 6169:                     } 
 6170:                 }
 6171:             }
 6172:         } elsif ($key eq 'lockablenames') {
 6173:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6174:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6175:                 if (@changed) {
 6176:                     $changes{'lockablenames'} = 1;
 6177:                 }
 6178:             } else {
 6179:                 if (@lockablenames) {
 6180:                     $changes{'lockablenames'} = 1;
 6181:                 }
 6182:             }
 6183:         }
 6184:     }
 6185:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6186:         if (@lockablenames) {
 6187:             $changes{'lockablenames'} = 1;
 6188:         }
 6189:     }
 6190:     foreach my $item (@{$types},'default') {
 6191:         if (defined($fields{$item})) {
 6192:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6193:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6194:                     my $change = 0;
 6195:                     if (ref($fields{$item}) eq 'ARRAY') {
 6196:                         foreach my $type (@{$fields{$item}}) {
 6197:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6198:                                 $change = 1;
 6199:                                 last;
 6200:                             }
 6201:                         }
 6202:                     }
 6203:                     if ($change) {
 6204:                         push(@{$changes{'fields'}},$item);
 6205:                     }
 6206:                 } else {
 6207:                     push(@{$changes{'fields'}},$item);
 6208:                 }
 6209:             } else {
 6210:                 push(@{$changes{'fields'}},$item);
 6211:             }
 6212:         }
 6213:     }
 6214:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6215:                                              $dom);
 6216:     if ($putresult eq 'ok') {
 6217:         if (keys(%changes) > 0) {
 6218:             $resulttext = &mt('Changes made:').'<ul>';
 6219:             foreach my $key (sort(keys(%changes))) {
 6220:                 if ($key eq 'lockablenames') {
 6221:                     $resulttext .= '<li>';
 6222:                     if (@lockablenames) {
 6223:                         $usertypes->{'default'} = $othertitle;
 6224:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6225:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6226:                     } else {
 6227:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6228:                     }
 6229:                     $resulttext .= '</li>';
 6230:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6231:                     foreach my $item (@{$changes{$key}}) {
 6232:                         my @newvalues;
 6233:                         foreach my $type (@{$fields{$item}}) {
 6234:                             push(@newvalues,$fieldtitles{$type});
 6235:                         }
 6236:                         my $newvaluestr;
 6237:                         if (@newvalues > 0) {
 6238:                             $newvaluestr = join(', ',@newvalues);
 6239:                         } else {
 6240:                             $newvaluestr = &mt('none');
 6241:                         }
 6242:                         if ($item eq 'default') {
 6243:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6244:                         } else {
 6245:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6246:                         }
 6247:                     }
 6248:                 } else {
 6249:                     my $newvalue;
 6250:                     if ($key eq 'run') {
 6251:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6252:                     } else {
 6253:                         $newvalue = $offon[$env{'form.'.$key}];
 6254:                     }
 6255:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6256:                 }
 6257:             }
 6258:             $resulttext .= '</ul>';
 6259:         } else {
 6260:             $resulttext = &mt('No changes made to autoupdates');
 6261:         }
 6262:     } else {
 6263:         $resulttext = '<span class="LC_error">'.
 6264: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6265:     }
 6266:     return $resulttext;
 6267: }
 6268: 
 6269: sub modify_autocreate {
 6270:     my ($dom,%domconfig) = @_;
 6271:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6272:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6273:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6274:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6275:         }
 6276:     }
 6277:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6278:                  req => 'Auto-creation of validated requests for official courses',
 6279:                  xmldc => 'Identity of course creator of courses from XML files',
 6280:                );
 6281:     my @types = ('xml','req');
 6282:     foreach my $item (@types) {
 6283:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6284:         $newvals{$item} =~ s/\D//g;
 6285:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6286:     }
 6287:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6288:     my %domcoords = &get_active_dcs($dom);
 6289:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6290:         $newvals{'xmldc'} = '';
 6291:     } 
 6292:     %autocreatehash =  (
 6293:                         autocreate => { xml => $newvals{'xml'},
 6294:                                         req => $newvals{'req'},
 6295:                                       }
 6296:                        );
 6297:     if ($newvals{'xmldc'} ne '') {
 6298:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6299:     }
 6300:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6301:                                              $dom);
 6302:     if ($putresult eq 'ok') {
 6303:         my @items = @types;
 6304:         if ($newvals{'xml'}) {
 6305:             push(@items,'xmldc');
 6306:         }
 6307:         foreach my $item (@items) {
 6308:             if (exists($currautocreate{$item})) {
 6309:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6310:                     $changes{$item} = 1;
 6311:                 }
 6312:             } elsif ($newvals{$item}) {
 6313:                 $changes{$item} = 1;
 6314:             }
 6315:         }
 6316:         if (keys(%changes) > 0) {
 6317:             my @offon = ('off','on'); 
 6318:             $resulttext = &mt('Changes made:').'<ul>';
 6319:             foreach my $item (@types) {
 6320:                 if ($changes{$item}) {
 6321:                     my $newtxt = $offon[$newvals{$item}];
 6322:                     $resulttext .= '<li>'.
 6323:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 6324:                                        '<b>','</b>').
 6325:                                    '</li>';
 6326:                 }
 6327:             }
 6328:             if ($changes{'xmldc'}) {
 6329:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6330:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6331:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 6332:             }
 6333:             $resulttext .= '</ul>';
 6334:         } else {
 6335:             $resulttext = &mt('No changes made to auto-creation settings');
 6336:         }
 6337:     } else {
 6338:         $resulttext = '<span class="LC_error">'.
 6339:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6340:     }
 6341:     return $resulttext;
 6342: }
 6343: 
 6344: sub modify_directorysrch {
 6345:     my ($dom,%domconfig) = @_;
 6346:     my ($resulttext,%changes);
 6347:     my %currdirsrch;
 6348:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6349:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6350:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6351:         }
 6352:     }
 6353:     my %title = ( available => 'Directory search available',
 6354:                   localonly => 'Other domains can search',
 6355:                   searchby => 'Search types',
 6356:                   searchtypes => 'Search latitude');
 6357:     my @offon = ('off','on');
 6358:     my @otherdoms = ('Yes','No');
 6359: 
 6360:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6361:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6362:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6363: 
 6364:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6365:     if (keys(%{$usertypes}) == 0) {
 6366:         @cansearch = ('default');
 6367:     } else {
 6368:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6369:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6370:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6371:                     push(@{$changes{'cansearch'}},$type);
 6372:                 }
 6373:             }
 6374:             foreach my $type (@cansearch) {
 6375:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6376:                     push(@{$changes{'cansearch'}},$type);
 6377:                 }
 6378:             }
 6379:         } else {
 6380:             push(@{$changes{'cansearch'}},@cansearch);
 6381:         }
 6382:     }
 6383: 
 6384:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6385:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6386:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6387:                 push(@{$changes{'searchby'}},$by);
 6388:             }
 6389:         }
 6390:         foreach my $by (@searchby) {
 6391:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6392:                 push(@{$changes{'searchby'}},$by);
 6393:             }
 6394:         }
 6395:     } else {
 6396:         push(@{$changes{'searchby'}},@searchby);
 6397:     }
 6398: 
 6399:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6400:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6401:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6402:                 push(@{$changes{'searchtypes'}},$type);
 6403:             }
 6404:         }
 6405:         foreach my $type (@searchtypes) {
 6406:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6407:                 push(@{$changes{'searchtypes'}},$type);
 6408:             }
 6409:         }
 6410:     } else {
 6411:         if (exists($currdirsrch{'searchtypes'})) {
 6412:             foreach my $type (@searchtypes) {  
 6413:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6414:                     push(@{$changes{'searchtypes'}},$type);
 6415:                 }
 6416:             }
 6417:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6418:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6419:             }   
 6420:         } else {
 6421:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6422:         }
 6423:     }
 6424: 
 6425:     my %dirsrch_hash =  (
 6426:             directorysrch => { available => $env{'form.dirsrch_available'},
 6427:                                cansearch => \@cansearch,
 6428:                                localonly => $env{'form.dirsrch_localonly'},
 6429:                                searchby => \@searchby,
 6430:                                searchtypes => \@searchtypes,
 6431:                              }
 6432:             );
 6433:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6434:                                              $dom);
 6435:     if ($putresult eq 'ok') {
 6436:         if (exists($currdirsrch{'available'})) {
 6437:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6438:                  $changes{'available'} = 1;
 6439:              }
 6440:         } else {
 6441:             if ($env{'form.dirsrch_available'} eq '1') {
 6442:                 $changes{'available'} = 1;
 6443:             }
 6444:         }
 6445:         if (exists($currdirsrch{'localonly'})) {
 6446:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6447:                  $changes{'localonly'} = 1;
 6448:              }
 6449:         } else {
 6450:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6451:                 $changes{'localonly'} = 1;
 6452:             }
 6453:         }
 6454:         if (keys(%changes) > 0) {
 6455:             $resulttext = &mt('Changes made:').'<ul>';
 6456:             if ($changes{'available'}) {
 6457:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6458:             }
 6459:             if ($changes{'localonly'}) {
 6460:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6461:             }
 6462: 
 6463:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6464:                 my $chgtext;
 6465:                 if (ref($usertypes) eq 'HASH') {
 6466:                     if (keys(%{$usertypes}) > 0) {
 6467:                         foreach my $type (@{$types}) {
 6468:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6469:                                 $chgtext .= $usertypes->{$type}.'; ';
 6470:                             }
 6471:                         }
 6472:                         if (grep(/^default$/,@cansearch)) {
 6473:                             $chgtext .= $othertitle;
 6474:                         } else {
 6475:                             $chgtext =~ s/\; $//;
 6476:                         }
 6477:                         $resulttext .=
 6478:                             '<li>'.
 6479:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 6480:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 6481:                             '</li>';
 6482:                     }
 6483:                 }
 6484:             }
 6485:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6486:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6487:                 my $chgtext;
 6488:                 foreach my $type (@{$titleorder}) {
 6489:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6490:                         if (defined($searchtitles->{$type})) {
 6491:                             $chgtext .= $searchtitles->{$type}.'; ';
 6492:                         }
 6493:                     }
 6494:                 }
 6495:                 $chgtext =~ s/\; $//;
 6496:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6497:             }
 6498:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6499:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6500:                 my $chgtext;
 6501:                 foreach my $type (@{$srchtypeorder}) {
 6502:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6503:                         if (defined($srchtypes_desc->{$type})) {
 6504:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6505:                         }
 6506:                     }
 6507:                 }
 6508:                 $chgtext =~ s/\; $//;
 6509:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 6510:             }
 6511:             $resulttext .= '</ul>';
 6512:         } else {
 6513:             $resulttext = &mt('No changes made to institution directory search settings');
 6514:         }
 6515:     } else {
 6516:         $resulttext = '<span class="LC_error">'.
 6517:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6518:     }
 6519:     return $resulttext;
 6520: }
 6521: 
 6522: sub modify_contacts {
 6523:     my ($dom,%domconfig) = @_;
 6524:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6525:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6526:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6527:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6528:         }
 6529:     }
 6530:     my (%others,%to,%bcc);
 6531:     my @contacts = ('supportemail','adminemail');
 6532:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6533:                     'requestsmail','updatesmail');
 6534:     foreach my $type (@mailings) {
 6535:         @{$newsetting{$type}} = 
 6536:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6537:         foreach my $item (@contacts) {
 6538:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6539:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6540:             } else {
 6541:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6542:             }
 6543:         }  
 6544:         $others{$type} = $env{'form.'.$type.'_others'};
 6545:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6546:         if ($type eq 'helpdeskmail') {
 6547:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6548:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6549:         }
 6550:     }
 6551:     foreach my $item (@contacts) {
 6552:         $to{$item} = $env{'form.'.$item};
 6553:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6554:     }
 6555:     if (keys(%currsetting) > 0) {
 6556:         foreach my $item (@contacts) {
 6557:             if ($to{$item} ne $currsetting{$item}) {
 6558:                 $changes{$item} = 1;
 6559:             }
 6560:         }
 6561:         foreach my $type (@mailings) {
 6562:             foreach my $item (@contacts) {
 6563:                 if (ref($currsetting{$type}) eq 'HASH') {
 6564:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6565:                         push(@{$changes{$type}},$item);
 6566:                     }
 6567:                 } else {
 6568:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6569:                 }
 6570:             }
 6571:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6572:                 push(@{$changes{$type}},'others');
 6573:             }
 6574:             if ($type eq 'helpdeskmail') {   
 6575:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6576:                     push(@{$changes{$type}},'bcc'); 
 6577:                 }
 6578:             }
 6579:         }
 6580:     } else {
 6581:         my %default;
 6582:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6583:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6584:         $default{'errormail'} = 'adminemail';
 6585:         $default{'packagesmail'} = 'adminemail';
 6586:         $default{'helpdeskmail'} = 'supportemail';
 6587:         $default{'lonstatusmail'} = 'adminemail';
 6588:         $default{'requestsmail'} = 'adminemail';
 6589:         $default{'updatesmail'} = 'adminemail';
 6590:         foreach my $item (@contacts) {
 6591:            if ($to{$item} ne $default{$item}) {
 6592:               $changes{$item} = 1;
 6593:            } 
 6594:         }
 6595:         foreach my $type (@mailings) {
 6596:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6597:                
 6598:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6599:             }
 6600:             if ($others{$type} ne '') {
 6601:                 push(@{$changes{$type}},'others');
 6602:             }
 6603:             if ($type eq 'helpdeskmail') {
 6604:                 if ($bcc{$type} ne '') {
 6605:                     push(@{$changes{$type}},'bcc');
 6606:                 }
 6607:             }
 6608:         }
 6609:     }
 6610:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6611:                                              $dom);
 6612:     if ($putresult eq 'ok') {
 6613:         if (keys(%changes) > 0) {
 6614:             my ($titles,$short_titles)  = &contact_titles();
 6615:             $resulttext = &mt('Changes made:').'<ul>';
 6616:             foreach my $item (@contacts) {
 6617:                 if ($changes{$item}) {
 6618:                     $resulttext .= '<li>'.$titles->{$item}.
 6619:                                     &mt(' set to: ').
 6620:                                     '<span class="LC_cusr_emph">'.
 6621:                                     $to{$item}.'</span></li>';
 6622:                 }
 6623:             }
 6624:             foreach my $type (@mailings) {
 6625:                 if (ref($changes{$type}) eq 'ARRAY') {
 6626:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6627:                     my @text;
 6628:                     foreach my $item (@{$newsetting{$type}}) {
 6629:                         push(@text,$short_titles->{$item});
 6630:                     }
 6631:                     if ($others{$type} ne '') {
 6632:                         push(@text,$others{$type});
 6633:                     }
 6634:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6635:                                    join(', ',@text).'</span>';
 6636:                     if ($type eq 'helpdeskmail') {
 6637:                         if ($bcc{$type} ne '') {
 6638:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6639:                         }
 6640:                     }
 6641:                     $resulttext .= '</li>';
 6642:                 }
 6643:             }
 6644:             $resulttext .= '</ul>';
 6645:         } else {
 6646:             $resulttext = &mt('No changes made to contact information');
 6647:         }
 6648:     } else {
 6649:         $resulttext = '<span class="LC_error">'.
 6650:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6651:     }
 6652:     return $resulttext;
 6653: }
 6654: 
 6655: sub modify_usercreation {
 6656:     my ($dom,%domconfig) = @_;
 6657:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6658:     my $warningmsg;
 6659:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6660:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6661:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6662:         }
 6663:     }
 6664:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6665:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6666:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6667:     my @contexts = ('author','course','requestcrs','selfcreate');
 6668:     foreach my $item(@contexts) {
 6669:         if ($item eq 'selfcreate') {
 6670:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6671:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6672:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6673:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6674:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6675:                         $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.');   
 6676:                     }
 6677:                 }
 6678:             }
 6679:         } else {
 6680:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6681:         }
 6682:     }
 6683:     my ($othertitle,$usertypes,$types) = 
 6684:         &Apache::loncommon::sorted_inst_types($dom);
 6685:     if (ref($types) eq 'ARRAY') {
 6686:         if (@{$types} > 0) {
 6687:             @{$cancreate{'statustocreate'}} = 
 6688:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6689:         } else {
 6690:             @{$cancreate{'statustocreate'}} = ();
 6691:         }
 6692:         push(@contexts,'statustocreate');
 6693:     }
 6694:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6695:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6696:         foreach my $item (@contexts) {
 6697:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6698:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6699:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6700:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6701:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6702:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6703:                                     push(@{$changes{'cancreate'}},$item);
 6704:                                 }
 6705:                             }
 6706:                         }
 6707:                     }
 6708:                 } else {
 6709:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6710:                         if (@{$cancreate{$item}} > 0) {
 6711:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6712:                                 push(@{$changes{'cancreate'}},$item);
 6713:                             }
 6714:                         }
 6715:                     } else {
 6716:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6717:                             if (@{$cancreate{$item}} < 3) {
 6718:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6719:                                     push(@{$changes{'cancreate'}},$item);
 6720:                                 }
 6721:                             }
 6722:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6723:                             if (@{$cancreate{$item}} > 0) {
 6724:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6725:                                     push(@{$changes{'cancreate'}},$item);
 6726:                                 }
 6727:                             }
 6728:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6729:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6730:                                 push(@{$changes{'cancreate'}},$item);
 6731:                             }
 6732:                         }
 6733:                     }
 6734:                 }
 6735:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6736:                     foreach my $type (@{$cancreate{$item}}) {
 6737:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6738:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6739:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6740:                                     push(@{$changes{'cancreate'}},$item);
 6741:                                 }
 6742:                             }
 6743:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6744:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6745:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6746:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6747:                                     push(@{$changes{'cancreate'}},$item);
 6748:                                 }
 6749:                             }
 6750:                         }
 6751:                     }
 6752:                 }
 6753:             } else {
 6754:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6755:                     push(@{$changes{'cancreate'}},$item);
 6756:                 }
 6757:             }
 6758:         }
 6759:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6760:         foreach my $item (@contexts) {
 6761:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6762:                 if ($cancreate{$item} ne 'any') {
 6763:                     push(@{$changes{'cancreate'}},$item);
 6764:                 }
 6765:             } else {
 6766:                 if ($cancreate{$item} ne 'none') {
 6767:                     push(@{$changes{'cancreate'}},$item);
 6768:                 }
 6769:             }
 6770:         }
 6771:     } else {
 6772:         foreach my $item (@contexts)  {
 6773:             push(@{$changes{'cancreate'}},$item);
 6774:         }
 6775:     }
 6776: 
 6777:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6778:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6779:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6780:                 push(@{$changes{'username_rule'}},$type);
 6781:             }
 6782:         }
 6783:         foreach my $type (@username_rule) {
 6784:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6785:                 push(@{$changes{'username_rule'}},$type);
 6786:             }
 6787:         }
 6788:     } else {
 6789:         push(@{$changes{'username_rule'}},@username_rule);
 6790:     }
 6791: 
 6792:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6793:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 6794:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 6795:                 push(@{$changes{'id_rule'}},$type);
 6796:             }
 6797:         }
 6798:         foreach my $type (@id_rule) {
 6799:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 6800:                 push(@{$changes{'id_rule'}},$type);
 6801:             }
 6802:         }
 6803:     } else {
 6804:         push(@{$changes{'id_rule'}},@id_rule);
 6805:     }
 6806: 
 6807:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 6808:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 6809:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 6810:                 push(@{$changes{'email_rule'}},$type);
 6811:             }
 6812:         }
 6813:         foreach my $type (@email_rule) {
 6814:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 6815:                 push(@{$changes{'email_rule'}},$type);
 6816:             }
 6817:         }
 6818:     } else {
 6819:         push(@{$changes{'email_rule'}},@email_rule);
 6820:     }
 6821: 
 6822:     my @authen_contexts = ('author','course','domain');
 6823:     my @authtypes = ('int','krb4','krb5','loc');
 6824:     my %authhash;
 6825:     foreach my $item (@authen_contexts) {
 6826:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 6827:         foreach my $auth (@authtypes) {
 6828:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 6829:                 $authhash{$item}{$auth} = 1;
 6830:             } else {
 6831:                 $authhash{$item}{$auth} = 0;
 6832:             }
 6833:         }
 6834:     }
 6835:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 6836:         foreach my $item (@authen_contexts) {
 6837:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 6838:                 foreach my $auth (@authtypes) {
 6839:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 6840:                         push(@{$changes{'authtypes'}},$item);
 6841:                         last;
 6842:                     }
 6843:                 }
 6844:             }
 6845:         }
 6846:     } else {
 6847:         foreach my $item (@authen_contexts) {
 6848:             push(@{$changes{'authtypes'}},$item);
 6849:         }
 6850:     }
 6851: 
 6852:     my %usercreation_hash =  (
 6853:             usercreation => {
 6854:                               cancreate     => \%cancreate,
 6855:                               username_rule => \@username_rule,
 6856:                               id_rule       => \@id_rule,
 6857:                               email_rule    => \@email_rule,
 6858:                               authtypes     => \%authhash,
 6859:                             }
 6860:             );
 6861: 
 6862:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 6863:                                              $dom);
 6864: 
 6865:     my %selfcreatetypes = (
 6866:                              sso   => 'users authenticated by institutional single sign on',
 6867:                              login => 'users authenticated by institutional log-in',
 6868:                              email => 'users who provide a valid e-mail address for use as the username',
 6869:                           );
 6870:     if ($putresult eq 'ok') {
 6871:         if (keys(%changes) > 0) {
 6872:             $resulttext = &mt('Changes made:').'<ul>';
 6873:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 6874:                 my %lt = &usercreation_types();
 6875:                 foreach my $type (@{$changes{'cancreate'}}) {
 6876:                     my $chgtext;
 6877:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 6878:                         $chgtext = $lt{$type}.', ';
 6879:                     }
 6880:                     if ($type eq 'selfcreate') {
 6881:                         if (@{$cancreate{$type}} == 0) {
 6882:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 6883:                         } else {
 6884:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 6885:                             foreach my $case (@{$cancreate{$type}}) {
 6886:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 6887:                             }
 6888:                             $chgtext .= '</ul>';
 6889:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 6890:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 6891:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 6892:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 6893:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6894:                                         }
 6895:                                     }
 6896:                                 }
 6897:                             }
 6898:                         }
 6899:                     } elsif ($type eq 'statustocreate') {
 6900:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 6901:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 6902:                             if (@{$cancreate{'selfcreate'}} > 0) {
 6903:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 6904: 
 6905:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 6906:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 6907:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6908:                                     } 
 6909:                                 } elsif (ref($usertypes) eq 'HASH') {
 6910:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6911:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 6912:                                     } else {
 6913:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 6914:                                     }
 6915:                                     $chgtext .= '<ul>';
 6916:                                     foreach my $case (@{$cancreate{$type}}) {
 6917:                                         if ($case eq 'default') {
 6918:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 6919:                                         } else {
 6920:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 6921:                                         }
 6922:                                     }
 6923:                                     $chgtext .= '</ul>';
 6924:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6925:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 6926:                                     }
 6927:                                 }
 6928:                             } else {
 6929:                                 if (@{$cancreate{$type}} == 0) {
 6930:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 6931:                                 } else {
 6932:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 6933:                                 }
 6934:                             }
 6935:                         }
 6936:                     } elsif ($type eq 'captcha') {
 6937:                         if ($cancreate{$type} eq 'notused') {
 6938:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 6939:                         } else {
 6940:                             my %captchas = &captcha_phrases();
 6941:                             if ($captchas{$cancreate{$type}}) {
 6942:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 6943:                             } else {
 6944:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 6945:                             }
 6946:                         }
 6947:                     } elsif ($type eq 'recaptchakeys') {
 6948:                         my ($privkey,$pubkey);
 6949:                         if (ref($cancreate{$type}) eq 'HASH') {
 6950:                             $pubkey = $cancreate{$type}{'public'};
 6951:                             $privkey = $cancreate{$type}{'private'};
 6952:                         }
 6953:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 6954:                         if (!$pubkey) {
 6955:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 6956:                         } else {
 6957:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6958:                         }
 6959:                         if (!$privkey) {
 6960:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 6961:                         } else {
 6962:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6963:                         }
 6964:                         $chgtext .= '</ul>';
 6965:                     } else {
 6966:                         if ($cancreate{$type} eq 'none') {
 6967:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 6968:                         } elsif ($cancreate{$type} eq 'any') {
 6969:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 6970:                         } elsif ($cancreate{$type} eq 'official') {
 6971:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 6972:                         } elsif ($cancreate{$type} eq 'unofficial') {
 6973:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 6974:                         }
 6975:                     }
 6976:                     $resulttext .= '<li>'.$chgtext.'</li>';
 6977:                 }
 6978:             }
 6979:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 6980:                 my ($rules,$ruleorder) = 
 6981:                     &Apache::lonnet::inst_userrules($dom,'username');
 6982:                 my $chgtext = '<ul>';
 6983:                 foreach my $type (@username_rule) {
 6984:                     if (ref($rules->{$type}) eq 'HASH') {
 6985:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 6986:                     }
 6987:                 }
 6988:                 $chgtext .= '</ul>';
 6989:                 if (@username_rule > 0) {
 6990:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 6991:                 } else {
 6992:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 6993:                 }
 6994:             }
 6995:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 6996:                 my ($idrules,$idruleorder) = 
 6997:                     &Apache::lonnet::inst_userrules($dom,'id');
 6998:                 my $chgtext = '<ul>';
 6999:                 foreach my $type (@id_rule) {
 7000:                     if (ref($idrules->{$type}) eq 'HASH') {
 7001:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 7002:                     }
 7003:                 }
 7004:                 $chgtext .= '</ul>';
 7005:                 if (@id_rule > 0) {
 7006:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 7007:                 } else {
 7008:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 7009:                 }
 7010:             }
 7011:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 7012:                 my ($emailrules,$emailruleorder) =
 7013:                     &Apache::lonnet::inst_userrules($dom,'email');
 7014:                 my $chgtext = '<ul>';
 7015:                 foreach my $type (@email_rule) {
 7016:                     if (ref($emailrules->{$type}) eq 'HASH') {
 7017:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 7018:                     }
 7019:                 }
 7020:                 $chgtext .= '</ul>';
 7021:                 if (@email_rule > 0) {
 7022:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 7023:                 } else {
 7024:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 7025:                 }
 7026:             }
 7027: 
 7028:             my %authname = &authtype_names();
 7029:             my %context_title = &context_names();
 7030:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7031:                 my $chgtext = '<ul>';
 7032:                 foreach my $type (@{$changes{'authtypes'}}) {
 7033:                     my @allowed;
 7034:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7035:                     foreach my $auth (@authtypes) {
 7036:                         if ($authhash{$type}{$auth}) {
 7037:                             push(@allowed,$authname{$auth});
 7038:                         }
 7039:                     }
 7040:                     if (@allowed > 0) {
 7041:                         $chgtext .= join(', ',@allowed).'</li>';
 7042:                     } else {
 7043:                         $chgtext .= &mt('none').'</li>';
 7044:                     }
 7045:                 }
 7046:                 $chgtext .= '</ul>';
 7047:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7048:                 $resulttext .= '</li>';
 7049:             }
 7050:             $resulttext .= '</ul>';
 7051:         } else {
 7052:             $resulttext = &mt('No changes made to user creation settings');
 7053:         }
 7054:     } else {
 7055:         $resulttext = '<span class="LC_error">'.
 7056:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7057:     }
 7058:     if ($warningmsg ne '') {
 7059:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7060:     }
 7061:     return $resulttext;
 7062: }
 7063: 
 7064: sub process_captcha {
 7065:     my ($container,$changes,$newsettings,$current) = @_;
 7066:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 7067:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 7068:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 7069:         $newsettings->{'captcha'} = 'original';
 7070:     }
 7071:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 7072:         if ($container eq 'cancreate') {
 7073:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7074:                 push(@{$changes->{'cancreate'}},'captcha');
 7075:             } elsif (!defined($changes->{'cancreate'})) {
 7076:                 $changes->{'cancreate'} = ['captcha'];
 7077:             }
 7078:         } else {
 7079:             $changes->{'captcha'} = 1;
 7080:         }
 7081:     }
 7082:     my ($newpub,$newpriv,$currpub,$currpriv);
 7083:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 7084:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 7085:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 7086:         $newpub =~ s/\W//g;
 7087:         $newpriv =~ s/\W//g;
 7088:         $newsettings->{'recaptchakeys'} = {
 7089:                                              public  => $newpub,
 7090:                                              private => $newpriv,
 7091:                                           };
 7092:     }
 7093:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 7094:         $currpub = $current->{'recaptchakeys'}{'public'};
 7095:         $currpriv = $current->{'recaptchakeys'}{'private'};
 7096:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 7097:             $newsettings->{'recaptchakeys'} = {
 7098:                                                  public  => '',
 7099:                                                  private => '',
 7100:                                               }
 7101:         }
 7102:     }
 7103:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 7104:         if ($container eq 'cancreate') {
 7105:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7106:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 7107:             } elsif (!defined($changes->{'cancreate'})) {
 7108:                 $changes->{'cancreate'} = ['recaptchakeys'];
 7109:             }
 7110:         } else {
 7111:             $changes->{'recaptchakeys'} = 1;
 7112:         }
 7113:     }
 7114:     return;
 7115: }
 7116: 
 7117: sub modify_usermodification {
 7118:     my ($dom,%domconfig) = @_;
 7119:     my ($resulttext,%curr_usermodification,%changes);
 7120:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7121:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7122:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 7123:         }
 7124:     }
 7125:     my @contexts = ('author','course','selfcreate');
 7126:     my %context_title = (
 7127:                            author => 'In author context',
 7128:                            course => 'In course context',
 7129:                            selfcreate => 'When self creating account', 
 7130:                         );
 7131:     my @fields = ('lastname','firstname','middlename','generation',
 7132:                   'permanentemail','id');
 7133:     my %roles = (
 7134:                   author => ['ca','aa'],
 7135:                   course => ['st','ep','ta','in','cr'],
 7136:                 );
 7137:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7138:     if (ref($types) eq 'ARRAY') {
 7139:         push(@{$types},'default');
 7140:         $usertypes->{'default'} = $othertitle;
 7141:     }
 7142:     $roles{'selfcreate'} = $types;  
 7143:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7144:     my %modifyhash;
 7145:     foreach my $context (@contexts) {
 7146:         foreach my $role (@{$roles{$context}}) {
 7147:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7148:             foreach my $item (@fields) {
 7149:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7150:                     $modifyhash{$context}{$role}{$item} = 1;
 7151:                 } else {
 7152:                     $modifyhash{$context}{$role}{$item} = 0;
 7153:                 }
 7154:             }
 7155:         }
 7156:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7157:             foreach my $role (@{$roles{$context}}) {
 7158:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7159:                     foreach my $field (@fields) {
 7160:                         if ($modifyhash{$context}{$role}{$field} ne 
 7161:                                 $curr_usermodification{$context}{$role}{$field}) {
 7162:                             push(@{$changes{$context}},$role);
 7163:                             last;
 7164:                         }
 7165:                     }
 7166:                 }
 7167:             }
 7168:         } else {
 7169:             foreach my $context (@contexts) {
 7170:                 foreach my $role (@{$roles{$context}}) {
 7171:                     push(@{$changes{$context}},$role);
 7172:                 }
 7173:             }
 7174:         }
 7175:     }
 7176:     my %usermodification_hash =  (
 7177:                                    usermodification => \%modifyhash,
 7178:                                  );
 7179:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7180:                                              \%usermodification_hash,$dom);
 7181:     if ($putresult eq 'ok') {
 7182:         if (keys(%changes) > 0) {
 7183:             $resulttext = &mt('Changes made: ').'<ul>';
 7184:             foreach my $context (@contexts) {
 7185:                 if (ref($changes{$context}) eq 'ARRAY') {
 7186:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7187:                     if (ref($changes{$context}) eq 'ARRAY') {
 7188:                         foreach my $role (@{$changes{$context}}) {
 7189:                             my $rolename;
 7190:                             if ($context eq 'selfcreate') {
 7191:                                 $rolename = $role;
 7192:                                 if (ref($usertypes) eq 'HASH') {
 7193:                                     if ($usertypes->{$role} ne '') {
 7194:                                         $rolename = $usertypes->{$role};
 7195:                                     }
 7196:                                 }
 7197:                             } else {
 7198:                                 if ($role eq 'cr') {
 7199:                                     $rolename = &mt('Custom');
 7200:                                 } else {
 7201:                                     $rolename = &Apache::lonnet::plaintext($role);
 7202:                                 }
 7203:                             }
 7204:                             my @modifiable;
 7205:                             if ($context eq 'selfcreate') {
 7206:                                 $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): ');
 7207:                             } else {
 7208:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7209:                             }
 7210:                             foreach my $field (@fields) {
 7211:                                 if ($modifyhash{$context}{$role}{$field}) {
 7212:                                     push(@modifiable,$fieldtitles{$field});
 7213:                                 }
 7214:                             }
 7215:                             if (@modifiable > 0) {
 7216:                                 $resulttext .= join(', ',@modifiable);
 7217:                             } else {
 7218:                                 $resulttext .= &mt('none'); 
 7219:                             }
 7220:                             $resulttext .= '</li>';
 7221:                         }
 7222:                         $resulttext .= '</ul></li>';
 7223:                     }
 7224:                 }
 7225:             }
 7226:             $resulttext .= '</ul>';
 7227:         } else {
 7228:             $resulttext = &mt('No changes made to user modification settings');
 7229:         }
 7230:     } else {
 7231:         $resulttext = '<span class="LC_error">'.
 7232:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7233:     }
 7234:     return $resulttext;
 7235: }
 7236: 
 7237: sub modify_defaults {
 7238:     my ($dom,$r) = @_;
 7239:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7240:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7241:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7242:     my @authtypes = ('internal','krb4','krb5','localauth');
 7243:     foreach my $item (@items) {
 7244:         $newvalues{$item} = $env{'form.'.$item};
 7245:         if ($item eq 'auth_def') {
 7246:             if ($newvalues{$item} ne '') {
 7247:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7248:                     push(@errors,$item);
 7249:                 }
 7250:             }
 7251:         } elsif ($item eq 'lang_def') {
 7252:             if ($newvalues{$item} ne '') {
 7253:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7254:                     my $langcode = $1;
 7255:                     if ($langcode ne 'x_chef') {
 7256:                         if (code2language($langcode) eq '') {
 7257:                             push(@errors,$item);
 7258:                         }
 7259:                     }
 7260:                 } else {
 7261:                     push(@errors,$item);
 7262:                 }
 7263:             }
 7264:         } elsif ($item eq 'timezone_def') {
 7265:             if ($newvalues{$item} ne '') {
 7266:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7267:                     push(@errors,$item);   
 7268:                 }
 7269:             }
 7270:         } elsif ($item eq 'datelocale_def') {
 7271:             if ($newvalues{$item} ne '') {
 7272:                 my @datelocale_ids = DateTime::Locale->ids();
 7273:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7274:                     push(@errors,$item);
 7275:                 }
 7276:             }
 7277:         } elsif ($item eq 'portal_def') {
 7278:             if ($newvalues{$item} ne '') {
 7279:                 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])\/?$/) {
 7280:                     push(@errors,$item);
 7281:                 }
 7282:             }
 7283:         }
 7284:         if (grep(/^\Q$item\E$/,@errors)) {
 7285:             $newvalues{$item} = $domdefaults{$item};
 7286:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7287:             $changes{$item} = 1;
 7288:         }
 7289:         $domdefaults{$item} = $newvalues{$item};
 7290:     }
 7291:     my %defaults_hash = (
 7292:                          defaults => \%newvalues,
 7293:                         );
 7294:     my $title = &defaults_titles();
 7295:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7296:                                              $dom);
 7297:     if ($putresult eq 'ok') {
 7298:         if (keys(%changes) > 0) {
 7299:             $resulttext = &mt('Changes made:').'<ul>';
 7300:             my $version = $r->dir_config('lonVersion');
 7301:             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";
 7302:             foreach my $item (sort(keys(%changes))) {
 7303:                 my $value = $env{'form.'.$item};
 7304:                 if ($value eq '') {
 7305:                     $value = &mt('none');
 7306:                 } elsif ($item eq 'auth_def') {
 7307:                     my %authnames = &authtype_names();
 7308:                     my %shortauth = (
 7309:                              internal => 'int',
 7310:                              krb4 => 'krb4',
 7311:                              krb5 => 'krb5',
 7312:                              localauth  => 'loc',
 7313:                     );
 7314:                     $value = $authnames{$shortauth{$value}};
 7315:                 }
 7316:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7317:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7318:             }
 7319:             $resulttext .= '</ul>';
 7320:             $mailmsgtext .= "\n";
 7321:             my $cachetime = 24*60*60;
 7322:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7323:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7324:                 my $sysmail = $r->dir_config('lonSysEMail');
 7325:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 7326:             }
 7327:         } else {
 7328:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 7329:         }
 7330:     } else {
 7331:         $resulttext = '<span class="LC_error">'.
 7332:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7333:     }
 7334:     if (@errors > 0) {
 7335:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7336:         foreach my $item (@errors) {
 7337:             $resulttext .= ' "'.$title->{$item}.'",';
 7338:         }
 7339:         $resulttext =~ s/,$//;
 7340:     }
 7341:     return $resulttext;
 7342: }
 7343: 
 7344: sub modify_scantron {
 7345:     my ($r,$dom,$confname,%domconfig) = @_;
 7346:     my ($resulttext,%confhash,%changes,$errors);
 7347:     my $custom = 'custom.tab';
 7348:     my $default = 'default.tab';
 7349:     my $servadm = $r->dir_config('lonAdmEMail');
 7350:     my ($configuserok,$author_ok,$switchserver) = 
 7351:         &config_check($dom,$confname,$servadm);
 7352:     if ($env{'form.scantronformat.filename'} ne '') {
 7353:         my $error;
 7354:         if ($configuserok eq 'ok') {
 7355:             if ($switchserver) {
 7356:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7357:             } else {
 7358:                 if ($author_ok eq 'ok') {
 7359:                     my ($result,$scantronurl) =
 7360:                         &publishlogo($r,'upload','scantronformat',$dom,
 7361:                                      $confname,'scantron','','',$custom);
 7362:                     if ($result eq 'ok') {
 7363:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7364:                         $changes{'scantronformat'} = 1;
 7365:                     } else {
 7366:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7367:                     }
 7368:                 } else {
 7369:                     $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);
 7370:                 }
 7371:             }
 7372:         } else {
 7373:             $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);
 7374:         }
 7375:         if ($error) {
 7376:             &Apache::lonnet::logthis($error);
 7377:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7378:         }
 7379:     }
 7380:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7381:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7382:             if ($env{'form.scantronformat_del'}) {
 7383:                 $confhash{'scantron'}{'scantronformat'} = '';
 7384:                 $changes{'scantronformat'} = 1;
 7385:             }
 7386:         }
 7387:     }
 7388:     if (keys(%confhash) > 0) {
 7389:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7390:                                                  $dom);
 7391:         if ($putresult eq 'ok') {
 7392:             if (keys(%changes) > 0) {
 7393:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7394:                     $resulttext = &mt('Changes made:').'<ul>';
 7395:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7396:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7397:                     } else {
 7398:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7399:                     }
 7400:                     $resulttext .= '</ul>';
 7401:                 } else {
 7402:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7403:                 }
 7404:                 $resulttext .= '</ul>';
 7405:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7406:             } else {
 7407:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7408:             }
 7409:         } else {
 7410:             $resulttext = '<span class="LC_error">'.
 7411:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7412:         }
 7413:     } else {
 7414:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7415:     }
 7416:     if ($errors) {
 7417:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7418:                        $errors.'</ul>';
 7419:     }
 7420:     return $resulttext;
 7421: }
 7422: 
 7423: sub modify_coursecategories {
 7424:     my ($dom,%domconfig) = @_;
 7425:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7426:         $cathash);
 7427:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7428:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7429:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7430:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7431:             $changes{'togglecats'} = 1;
 7432:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7433:         }
 7434:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7435:             $changes{'categorize'} = 1;
 7436:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7437:         }
 7438:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7439:             $changes{'togglecatscomm'} = 1;
 7440:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7441:         }
 7442:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7443:             $changes{'categorizecomm'} = 1;
 7444:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7445:         }
 7446:     } else {
 7447:         $changes{'togglecats'} = 1;
 7448:         $changes{'categorize'} = 1;
 7449:         $changes{'togglecatscomm'} = 1;
 7450:         $changes{'categorizecomm'} = 1;
 7451:         $domconfig{'coursecategories'} = {
 7452:                                              togglecats => $env{'form.togglecats'},
 7453:                                              categorize => $env{'form.categorize'},
 7454:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7455:                                              categorizecomm => $env{'form.categorizecomm'},
 7456:                                          };
 7457:     }
 7458:     if (ref($cathash) eq 'HASH') {
 7459:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7460:             push (@deletecategory,'instcode::0');
 7461:         }
 7462:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7463:             push(@deletecategory,'communities::0');
 7464:         }
 7465:     }
 7466:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7467:     if (ref($cathash) eq 'HASH') {
 7468:         if (@deletecategory > 0) {
 7469:             #FIXME Need to remove category from all courses using a deleted category 
 7470:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7471:             foreach my $item (@deletecategory) {
 7472:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7473:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7474:                     $deletions{$item} = 1;
 7475:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7476:                 }
 7477:             }
 7478:         }
 7479:         foreach my $item (keys(%{$cathash})) {
 7480:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7481:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7482:                 $reorderings{$item} = 1;
 7483:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7484:             }
 7485:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7486:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7487:                 my $newdepth = $depth+1;
 7488:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7489:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7490:                 $adds{$newitem} = 1; 
 7491:             }
 7492:             if ($env{'form.subcat_'.$item} ne '') {
 7493:                 my $newcat = $env{'form.subcat_'.$item};
 7494:                 my $newdepth = $depth+1;
 7495:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7496:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7497:                 $adds{$newitem} = 1;
 7498:             }
 7499:         }
 7500:     }
 7501:     if ($env{'form.instcode'} eq '1') {
 7502:         if (ref($cathash) eq 'HASH') {
 7503:             my $newitem = 'instcode::0';
 7504:             if ($cathash->{$newitem} eq '') {  
 7505:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7506:                 $adds{$newitem} = 1;
 7507:             }
 7508:         } else {
 7509:             my $newitem = 'instcode::0';
 7510:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7511:             $adds{$newitem} = 1;
 7512:         }
 7513:     }
 7514:     if ($env{'form.communities'} eq '1') {
 7515:         if (ref($cathash) eq 'HASH') {
 7516:             my $newitem = 'communities::0';
 7517:             if ($cathash->{$newitem} eq '') {
 7518:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7519:                 $adds{$newitem} = 1;
 7520:             }
 7521:         } else {
 7522:             my $newitem = 'communities::0';
 7523:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7524:             $adds{$newitem} = 1;
 7525:         }
 7526:     }
 7527:     if ($env{'form.addcategory_name'} ne '') {
 7528:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7529:             ($env{'form.addcategory_name'} ne 'communities')) {
 7530:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7531:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7532:             $adds{$newitem} = 1;
 7533:         }
 7534:     }
 7535:     my $putresult;
 7536:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7537:         if (keys(%deletions) > 0) {
 7538:             foreach my $key (keys(%deletions)) {
 7539:                 if ($predelallitems{$key} ne '') {
 7540:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7541:                 }
 7542:             }
 7543:         }
 7544:         my (@chkcats,@chktrails,%chkallitems);
 7545:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7546:         if (ref($chkcats[0]) eq 'ARRAY') {
 7547:             my $depth = 0;
 7548:             my $chg = 0;
 7549:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7550:                 my $name = $chkcats[0][$i];
 7551:                 my $item;
 7552:                 if ($name eq '') {
 7553:                     $chg ++;
 7554:                 } else {
 7555:                     $item = &escape($name).'::0';
 7556:                     if ($chg) {
 7557:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7558:                     }
 7559:                     $depth ++; 
 7560:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7561:                     $depth --;
 7562:                 }
 7563:             }
 7564:         }
 7565:     }
 7566:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7567:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7568:         if ($putresult eq 'ok') {
 7569:             my %title = (
 7570:                          togglecats     => 'Show/Hide a course in catalog',
 7571:                          categorize     => 'Assign a category to a course',
 7572:                          togglecatscomm => 'Show/Hide a community in catalog',
 7573:                          categorizecomm => 'Assign a category to a community',
 7574:                         );
 7575:             my %level = (
 7576:                          dom  => 'set in Domain ("Modify Course/Community")',
 7577:                          crs  => 'set in Course ("Course Configuration")',
 7578:                          comm => 'set in Community ("Community Configuration")',
 7579:                         );
 7580:             $resulttext = &mt('Changes made:').'<ul>';
 7581:             if ($changes{'togglecats'}) {
 7582:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7583:             }
 7584:             if ($changes{'categorize'}) {
 7585:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7586:             }
 7587:             if ($changes{'togglecatscomm'}) {
 7588:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7589:             }
 7590:             if ($changes{'categorizecomm'}) {
 7591:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7592:             }
 7593:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7594:                 my $cathash;
 7595:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7596:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7597:                 } else {
 7598:                     $cathash = {};
 7599:                 } 
 7600:                 my (@cats,@trails,%allitems);
 7601:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7602:                 if (keys(%deletions) > 0) {
 7603:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7604:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7605:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7606:                     }
 7607:                     $resulttext .= '</ul></li>';
 7608:                 }
 7609:                 if (keys(%reorderings) > 0) {
 7610:                     my %sort_by_trail;
 7611:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7612:                     foreach my $key (keys(%reorderings)) {
 7613:                         if ($allitems{$key} ne '') {
 7614:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7615:                         }
 7616:                     }
 7617:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7618:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7619:                     }
 7620:                     $resulttext .= '</ul></li>';
 7621:                 }
 7622:                 if (keys(%adds) > 0) {
 7623:                     my %sort_by_trail;
 7624:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7625:                     foreach my $key (keys(%adds)) {
 7626:                         if ($allitems{$key} ne '') {
 7627:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7628:                         }
 7629:                     }
 7630:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7631:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7632:                     }
 7633:                     $resulttext .= '</ul></li>';
 7634:                 }
 7635:             }
 7636:             $resulttext .= '</ul>';
 7637:         } else {
 7638:             $resulttext = '<span class="LC_error">'.
 7639:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7640:         }
 7641:     } else {
 7642:         $resulttext = &mt('No changes made to course and community categories');
 7643:     }
 7644:     return $resulttext;
 7645: }
 7646: 
 7647: sub modify_serverstatuses {
 7648:     my ($dom,%domconfig) = @_;
 7649:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7650:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7651:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7652:     }
 7653:     my @pages = &serverstatus_pages();
 7654:     foreach my $type (@pages) {
 7655:         $newserverstatus{$type}{'namedusers'} = '';
 7656:         $newserverstatus{$type}{'machines'} = '';
 7657:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7658:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7659:             my @okusers;
 7660:             foreach my $user (@users) {
 7661:                 my ($uname,$udom) = split(/:/,$user);
 7662:                 if (($udom =~ /^$match_domain$/) &&   
 7663:                     (&Apache::lonnet::domain($udom)) &&
 7664:                     ($uname =~ /^$match_username$/)) {
 7665:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7666:                         push(@okusers,$user);
 7667:                     }
 7668:                 }
 7669:             }
 7670:             if (@okusers > 0) {
 7671:                  @okusers = sort(@okusers);
 7672:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7673:             }
 7674:         }
 7675:         if (defined($env{'form.'.$type.'_machines'})) {
 7676:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7677:             my @okmachines;
 7678:             foreach my $ip (@machines) {
 7679:                 my @parts = split(/\./,$ip);
 7680:                 next if (@parts < 4);
 7681:                 my $badip = 0;
 7682:                 for (my $i=0; $i<4; $i++) {
 7683:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7684:                         $badip = 1;
 7685:                         last;
 7686:                     }
 7687:                 }
 7688:                 if (!$badip) {
 7689:                     push(@okmachines,$ip);     
 7690:                 }
 7691:             }
 7692:             @okmachines = sort(@okmachines);
 7693:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7694:         }
 7695:     }
 7696:     my %serverstatushash =  (
 7697:                                 serverstatuses => \%newserverstatus,
 7698:                             );
 7699:     foreach my $type (@pages) {
 7700:         foreach my $setting ('namedusers','machines') {
 7701:             my (@current,@new);
 7702:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7703:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7704:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7705:                 }
 7706:             }
 7707:             if ($newserverstatus{$type}{$setting} ne '') {
 7708:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7709:             }
 7710:             if (@current > 0) {
 7711:                 if (@new > 0) {
 7712:                     foreach my $item (@current) {
 7713:                         if (!grep(/^\Q$item\E$/,@new)) {
 7714:                             $changes{$type}{$setting} = 1;
 7715:                             last;
 7716:                         }
 7717:                     }
 7718:                     foreach my $item (@new) {
 7719:                         if (!grep(/^\Q$item\E$/,@current)) {
 7720:                             $changes{$type}{$setting} = 1;
 7721:                             last;
 7722:                         }
 7723:                     }
 7724:                 } else {
 7725:                     $changes{$type}{$setting} = 1;
 7726:                 }
 7727:             } elsif (@new > 0) {
 7728:                 $changes{$type}{$setting} = 1;
 7729:             }
 7730:         }
 7731:     }
 7732:     if (keys(%changes) > 0) {
 7733:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7734:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7735:                                                  \%serverstatushash,$dom);
 7736:         if ($putresult eq 'ok') {
 7737:             $resulttext .= &mt('Changes made:').'<ul>';
 7738:             foreach my $type (@pages) {
 7739:                 if (ref($changes{$type}) eq 'HASH') {
 7740:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7741:                     if ($changes{$type}{'namedusers'}) {
 7742:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7743:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7744:                         } else {
 7745:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7746:                         }
 7747:                     }
 7748:                     if ($changes{$type}{'machines'}) {
 7749:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7750:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7751:                         } else {
 7752:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7753:                         }
 7754: 
 7755:                     }
 7756:                     $resulttext .= '</ul></li>';
 7757:                 }
 7758:             }
 7759:             $resulttext .= '</ul>';
 7760:         } else {
 7761:             $resulttext = '<span class="LC_error">'.
 7762:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7763: 
 7764:         }
 7765:     } else {
 7766:         $resulttext = &mt('No changes made to access to server status pages');
 7767:     }
 7768:     return $resulttext;
 7769: }
 7770: 
 7771: sub modify_helpsettings {
 7772:     my ($r,$dom,$confname,%domconfig) = @_;
 7773:     my ($resulttext,$errors,%changes,%helphash);
 7774:     my %defaultchecked = ('submitbugs' => 'on');
 7775:     my @offon = ('off','on');
 7776:     my @toggles = ('submitbugs');
 7777:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7778:         foreach my $item (@toggles) {
 7779:             if ($defaultchecked{$item} eq 'on') { 
 7780:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7781:                     if ($env{'form.'.$item} eq '0') {
 7782:                         $changes{$item} = 1;
 7783:                     }
 7784:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7785:                     $changes{$item} = 1;
 7786:                 }
 7787:             } elsif ($defaultchecked{$item} eq 'off') {
 7788:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7789:                     if ($env{'form.'.$item} eq '1') {
 7790:                         $changes{$item} = 1;
 7791:                     }
 7792:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7793:                     $changes{$item} = 1;
 7794:                 }
 7795: 	    }
 7796:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 7797:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7798:             }
 7799:         }
 7800:     }
 7801:     my $putresult;
 7802:     if (keys(%changes) > 0) {
 7803:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7804:         if ($putresult eq 'ok') {
 7805:             $resulttext = &mt('Changes made:').'<ul>';
 7806:             foreach my $item (sort(keys(%changes))) {
 7807:                 if ($item eq 'submitbugs') {
 7808:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 7809:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 7810:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 7811:                 }
 7812:             }
 7813:             $resulttext .= '</ul>';
 7814:         } else {
 7815:             $resulttext = &mt('No changes made to help settings');
 7816:             $errors .= '<li><span class="LC_error">'.
 7817:                        &mt('An error occurred storing the settings: [_1]',
 7818:                            $putresult).'</span></li>';
 7819:         }
 7820:     }
 7821:     if ($errors) {
 7822:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7823:                        $errors.'</ul>';
 7824:     }
 7825:     return $resulttext;
 7826: }
 7827: 
 7828: sub modify_coursedefaults {
 7829:     my ($dom,%domconfig) = @_;
 7830:     my ($resulttext,$errors,%changes,%defaultshash);
 7831:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7832:     my @toggles = ('canuse_pdfforms');
 7833: 
 7834:     $defaultshash{'coursedefaults'} = {};
 7835: 
 7836:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7837:         if ($domconfig{'coursedefaults'} eq '') {
 7838:             $domconfig{'coursedefaults'} = {};
 7839:         }
 7840:     }
 7841: 
 7842:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7843:         foreach my $item (@toggles) {
 7844:             if ($defaultchecked{$item} eq 'on') {
 7845:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7846:                     ($env{'form.'.$item} eq '0')) {
 7847:                     $changes{$item} = 1;
 7848:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7849:                     $changes{$item} = 1;
 7850:                 }
 7851:             } elsif ($defaultchecked{$item} eq 'off') {
 7852:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7853:                     ($env{'form.'.$item} eq '1')) {
 7854:                     $changes{$item} = 1;
 7855:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7856:                     $changes{$item} = 1;
 7857:                 }
 7858:             }
 7859:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7860:         }
 7861:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7862:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7863:         $newdefresponder =~ s/\D//g;
 7864:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7865:             $newdefresponder = 1;
 7866:         }
 7867:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7868:         if ($currdefresponder ne $newdefresponder) {
 7869:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7870:                 $changes{'anonsurvey_threshold'} = 1;
 7871:             }
 7872:         }
 7873:         my $officialcreds = $env{'form.official_credits'};
 7874:         $officialcreds =~ s/^[^\d\.]//g;
 7875:         my $unofficialcreds = $env{'form.unofficial_credits'};
 7876:         $unofficialcreds =~ s/^[^\d\.]//g;
 7877:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 7878:                 ($env{'form.coursecredits'} eq '1')) {
 7879:                 $changes{'coursecredits'} = 1;
 7880:         } else {
 7881:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 7882:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds)) {
 7883:                 $changes{'coursecredits'} = 1;
 7884:             }
 7885:         }
 7886:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 7887:             official   => $officialcreds,
 7888:             unofficial => $unofficialcreds,
 7889:         }
 7890:     }
 7891:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7892:                                              $dom);
 7893:     if ($putresult eq 'ok') {
 7894:         my %domdefaults;
 7895:         if (keys(%changes) > 0) {
 7896:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'})) {
 7897:                 %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7898:                 if ($changes{'canuse_pdfforms'}) {
 7899:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7900:                 }
 7901:                 if ($changes{'coursecredits'}) {
 7902:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 7903:                         $domdefaults{'officialcredits'} =
 7904:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 7905:                         $domdefaults{'unofficialcredits'} =
 7906:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 7907:                     }
 7908:                 }
 7909:                 my $cachetime = 24*60*60;
 7910:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7911:             }
 7912:             $resulttext = &mt('Changes made:').'<ul>';
 7913:             foreach my $item (sort(keys(%changes))) {
 7914:                 if ($item eq 'canuse_pdfforms') {
 7915:                     if ($env{'form.'.$item} eq '1') {
 7916:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7917:                     } else {
 7918:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7919:                     }
 7920:                 } elsif ($item eq 'anonsurvey_threshold') {
 7921:                         $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7922:                 } elsif ($item eq 'coursecredits') {
 7923:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 7924:                         if (($domdefaults{'officialcredits'} eq '') &&
 7925:                             ($domdefaults{'unofficialcredits'} eq '')) {
 7926:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 7927:                         } else {
 7928:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
 7929:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
 7930:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
 7931:                                            '</ul>'.
 7932:                                            '</li>';
 7933:                         }
 7934:                     } else {
 7935:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 7936:                     }
 7937:                 }
 7938:             }
 7939:             $resulttext .= '</ul>';
 7940:         } else {
 7941:             $resulttext = &mt('No changes made to course defaults');
 7942:         }
 7943:     } else {
 7944:         $resulttext = '<span class="LC_error">'.
 7945:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7946:     }
 7947:     return $resulttext;
 7948: }
 7949: 
 7950: sub modify_usersessions {
 7951:     my ($dom,%domconfig) = @_;
 7952:     my @hostingtypes = ('version','excludedomain','includedomain');
 7953:     my @offloadtypes = ('primary','default');
 7954:     my %types = (
 7955:                   remote => \@hostingtypes,
 7956:                   hosted => \@hostingtypes,
 7957:                   spares => \@offloadtypes,
 7958:                 );
 7959:     my @prefixes = ('remote','hosted','spares');
 7960:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7961:     my (%by_ip,%by_location,@intdoms);
 7962:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7963:     my @locations = sort(keys(%by_location));
 7964:     my (%defaultshash,%changes);
 7965:     foreach my $prefix (@prefixes) {
 7966:         $defaultshash{'usersessions'}{$prefix} = {};
 7967:     }
 7968:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7969:     my $resulttext;
 7970:     my %iphost = &Apache::lonnet::get_iphost();
 7971:     foreach my $prefix (@prefixes) {
 7972:         next if ($prefix eq 'spares');
 7973:         foreach my $type (@{$types{$prefix}}) {
 7974:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7975:             if ($type eq 'version') {
 7976:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7977:                 my $okvalue;
 7978:                 if ($value ne '') {
 7979:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 7980:                         $okvalue = $value;
 7981:                     }
 7982:                 }
 7983:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7984:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7985:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 7986:                             if ($inuse == 0) {
 7987:                                 $changes{$prefix}{$type} = 1;
 7988:                             } else {
 7989:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 7990:                                     $changes{$prefix}{$type} = 1;
 7991:                                 }
 7992:                                 if ($okvalue ne '') {
 7993:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7994:                                 } 
 7995:                             }
 7996:                         } else {
 7997:                             if (($inuse == 1) && ($okvalue ne '')) {
 7998:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7999:                                 $changes{$prefix}{$type} = 1;
 8000:                             }
 8001:                         }
 8002:                     } else {
 8003:                         if (($inuse == 1) && ($okvalue ne '')) {
 8004:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8005:                             $changes{$prefix}{$type} = 1;
 8006:                         }
 8007:                     }
 8008:                 } else {
 8009:                     if (($inuse == 1) && ($okvalue ne '')) {
 8010:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8011:                         $changes{$prefix}{$type} = 1;
 8012:                     }
 8013:                 }
 8014:             } else {
 8015:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 8016:                 my @okvals;
 8017:                 foreach my $val (@vals) {
 8018:                     if ($val =~ /:/) {
 8019:                         my @items = split(/:/,$val);
 8020:                         foreach my $item (@items) {
 8021:                             if (ref($by_location{$item}) eq 'ARRAY') {
 8022:                                 push(@okvals,$item);
 8023:                             }
 8024:                         }
 8025:                     } else {
 8026:                         if (ref($by_location{$val}) eq 'ARRAY') {
 8027:                             push(@okvals,$val);
 8028:                         }
 8029:                     }
 8030:                 }
 8031:                 @okvals = sort(@okvals);
 8032:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8033:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8034:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8035:                             if ($inuse == 0) {
 8036:                                 $changes{$prefix}{$type} = 1; 
 8037:                             } else {
 8038:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8039:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 8040:                                 if (@changed > 0) {
 8041:                                     $changes{$prefix}{$type} = 1;
 8042:                                 }
 8043:                             }
 8044:                         } else {
 8045:                             if ($inuse == 1) {
 8046:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8047:                                 $changes{$prefix}{$type} = 1;
 8048:                             }
 8049:                         } 
 8050:                     } else {
 8051:                         if ($inuse == 1) {
 8052:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8053:                             $changes{$prefix}{$type} = 1;
 8054:                         }
 8055:                     }
 8056:                 } else {
 8057:                     if ($inuse == 1) {
 8058:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8059:                         $changes{$prefix}{$type} = 1;
 8060:                     }
 8061:                 }
 8062:             }
 8063:         }
 8064:     }
 8065: 
 8066:     my @alldoms = &Apache::lonnet::all_domains();
 8067:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8068:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 8069:     my $savespares;
 8070: 
 8071:     foreach my $lonhost (sort(keys(%servers))) {
 8072:         my $serverhomeID =
 8073:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 8074:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 8075:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 8076:         my %spareschg;
 8077:         foreach my $type (@{$types{'spares'}}) {
 8078:             my @okspares;
 8079:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 8080:             foreach my $server (@checked) {
 8081:                 if (&Apache::lonnet::hostname($server) ne '') {
 8082:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 8083:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 8084:                             push(@okspares,$server);
 8085:                         }
 8086:                     }
 8087:                 }
 8088:             }
 8089:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 8090:             my $newspare;
 8091:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 8092:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 8093:                     $newspare = $new;
 8094:                 }
 8095:             }
 8096:             my @spares;
 8097:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 8098:                 @spares = sort(@okspares,$newspare);
 8099:             } else {
 8100:                 @spares = sort(@okspares);
 8101:             }
 8102:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 8103:             if (ref($spareid{$lonhost}) eq 'HASH') {
 8104:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 8105:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 8106:                     if (@diffs > 0) {
 8107:                         $spareschg{$type} = 1;
 8108:                     }
 8109:                 }
 8110:             }
 8111:         }
 8112:         if (keys(%spareschg) > 0) {
 8113:             $changes{'spares'}{$lonhost} = \%spareschg;
 8114:         }
 8115:     }
 8116: 
 8117:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8118:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 8119:             if (ref($changes{'spares'}) eq 'HASH') {
 8120:                 if (keys(%{$changes{'spares'}}) > 0) {
 8121:                     $savespares = 1;
 8122:                 }
 8123:             }
 8124:         } else {
 8125:             $savespares = 1;
 8126:         }
 8127:     }
 8128: 
 8129:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 8130:     if ((keys(%changes) > 0) || ($savespares)) {
 8131:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8132:                                                  $dom);
 8133:         if ($putresult eq 'ok') {
 8134:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8135:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 8136:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 8137:                 }
 8138:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 8139:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 8140:                 }
 8141:             }
 8142:             my $cachetime = 24*60*60;
 8143:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8144:             if (keys(%changes) > 0) {
 8145:                 my %lt = &usersession_titles();
 8146:                 $resulttext = &mt('Changes made:').'<ul>';
 8147:                 foreach my $prefix (@prefixes) {
 8148:                     if (ref($changes{$prefix}) eq 'HASH') {
 8149:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 8150:                         if ($prefix eq 'spares') {
 8151:                             if (ref($changes{$prefix}) eq 'HASH') {
 8152:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 8153:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 8154:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 8155:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 8156:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 8157:                                         foreach my $type (@{$types{$prefix}}) {
 8158:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 8159:                                                 my $offloadto = &mt('None');
 8160:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 8161:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 8162:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 8163:                                                     }
 8164:                                                 }
 8165:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 8166:                                             }
 8167:                                         }
 8168:                                     }
 8169:                                     $resulttext .= '</li>';
 8170:                                 }
 8171:                             }
 8172:                         } else {
 8173:                             foreach my $type (@{$types{$prefix}}) {
 8174:                                 if (defined($changes{$prefix}{$type})) {
 8175:                                     my $newvalue;
 8176:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8177:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 8178:                                             if ($type eq 'version') {
 8179:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 8180:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8181:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 8182:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 8183:                                                 }
 8184:                                             }
 8185:                                         }
 8186:                                     }
 8187:                                     if ($newvalue eq '') {
 8188:                                         if ($type eq 'version') {
 8189:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8190:                                         } else {
 8191:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8192:                                         }
 8193:                                     } else {
 8194:                                         if ($type eq 'version') {
 8195:                                             $newvalue .= ' '.&mt('(or later)'); 
 8196:                                         }
 8197:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8198:                                     }
 8199:                                 }
 8200:                             }
 8201:                         }
 8202:                         $resulttext .= '</ul>';
 8203:                     }
 8204:                 }
 8205:                 $resulttext .= '</ul>';
 8206:             } else {
 8207:                 $resulttext = $nochgmsg;
 8208:             }
 8209:         } else {
 8210:             $resulttext = '<span class="LC_error">'.
 8211:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8212:         }
 8213:     } else {
 8214:         $resulttext = $nochgmsg;
 8215:     }
 8216:     return $resulttext;
 8217: }
 8218: 
 8219: sub modify_loadbalancing {
 8220:     my ($dom,%domconfig) = @_;
 8221:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8222:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8223:     my ($othertitle,$usertypes,$types) =
 8224:         &Apache::loncommon::sorted_inst_types($dom);
 8225:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8226:     my @sparestypes = ('primary','default');
 8227:     my %typetitles = &sparestype_titles();
 8228:     my $resulttext;
 8229:     my (%currbalancer,%currtargets,%currrules,%existing);
 8230:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8231:         %existing = %{$domconfig{'loadbalancing'}};
 8232:     }
 8233:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8234:                               \%currtargets,\%currrules);
 8235:     my ($saveloadbalancing,%defaultshash,%changes);
 8236:     my ($alltypes,$othertypes,$titles) =
 8237:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8238:     my %ruletitles = &offloadtype_text();
 8239:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 8240:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 8241:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 8242:         if ($balancer eq '') {
 8243:             next;
 8244:         }
 8245:         if (!exists($servers{$balancer})) {
 8246:             if (exists($currbalancer{$balancer})) {
 8247:                 push(@{$changes{'delete'}},$balancer);
 8248:             }
 8249:             next;
 8250:         }
 8251:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 8252:             push(@{$changes{'delete'}},$balancer);
 8253:             next;
 8254:         }
 8255:         if (!exists($currbalancer{$balancer})) {
 8256:             push(@{$changes{'add'}},$balancer);
 8257:         }
 8258:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 8259:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 8260:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 8261:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8262:             $saveloadbalancing = 1;
 8263:         }
 8264:         foreach my $sparetype (@sparestypes) {
 8265:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 8266:             my @offloadto;
 8267:             foreach my $target (@targets) {
 8268:                 if (($servers{$target}) && ($target ne $balancer)) {
 8269:                     if ($sparetype eq 'default') {
 8270:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 8271:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 8272:                         }
 8273:                     }
 8274:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 8275:                         push(@offloadto,$target);
 8276:                     }
 8277:                 }
 8278:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 8279:             }
 8280:         }
 8281:         if (ref($currtargets{$balancer}) eq 'HASH') {
 8282:             foreach my $sparetype (@sparestypes) {
 8283:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 8284:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 8285:                     if (@targetdiffs > 0) {
 8286:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8287:                     }
 8288:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8289:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8290:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8291:                     }
 8292:                 }
 8293:             }
 8294:         } else {
 8295:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 8296:                 foreach my $sparetype (@sparestypes) {
 8297:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8298:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8299:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 8300:                         }
 8301:                     }
 8302:                 }
 8303:             }
 8304:         }
 8305:         my $ishomedom;
 8306:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 8307:             $ishomedom = 1;
 8308:         }
 8309:         if (ref($alltypes) eq 'ARRAY') {
 8310:             foreach my $type (@{$alltypes}) {
 8311:                 my $rule;
 8312:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
 8313:                          (!$ishomedom)) {
 8314:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 8315:                 }
 8316:                 if ($rule eq 'specific') {
 8317:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 8318:                 }
 8319:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 8320:                 if (ref($currrules{$balancer}) eq 'HASH') {
 8321:                     if ($rule ne $currrules{$balancer}{$type}) {
 8322:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8323:                     }
 8324:                 } elsif ($rule ne '') {
 8325:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8326:                 }
 8327:             }
 8328:         }
 8329:     }
 8330:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8331:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8332:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 8333:             $defaultshash{'loadbalancing'} = {};
 8334:         }
 8335:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8336:                                                  \%defaultshash,$dom);
 8337: 
 8338:         if ($putresult eq 'ok') {
 8339:             if (keys(%changes) > 0) {
 8340:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 8341:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 8342:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 8343:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8344:                     }
 8345:                 }
 8346:                 if (ref($changes{'add'}) eq 'ARRAY') {
 8347:                     foreach my $balancer (sort(@{$changes{'add'}})) {
 8348:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 8349:                     }
 8350:                 }
 8351:                 if (ref($changes{'curr'}) eq 'HASH') {
 8352:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 8353:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 8354:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 8355:                                 my %offloadstr;
 8356:                                 foreach my $sparetype (@sparestypes) {
 8357:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8358:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8359:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 8360:                                         }
 8361:                                     }
 8362:                                 }
 8363:                                 if (keys(%offloadstr) == 0) {
 8364:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8365:                                 } else {
 8366:                                     my $showoffload;
 8367:                                     foreach my $sparetype (@sparestypes) {
 8368:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8369:                                         if (defined($offloadstr{$sparetype})) {
 8370:                                             $showoffload .= $offloadstr{$sparetype};
 8371:                                         } else {
 8372:                                             $showoffload .= &mt('None');
 8373:                                         }
 8374:                                         $showoffload .= ('&nbsp;'x3);
 8375:                                     }
 8376:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 8377:                                 }
 8378:                             }
 8379:                         }
 8380:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 8381:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8382:                                 foreach my $type (@{$alltypes}) {
 8383:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 8384:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 8385:                                         my $balancetext;
 8386:                                         if ($rule eq '') {
 8387:                                             $balancetext =  $ruletitles{'default'};
 8388:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 8389:                                             $balancetext =  $ruletitles{$rule};
 8390:                                         } else {
 8391:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 8392:                                         }
 8393:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>'; 
 8394:                                     }
 8395:                                 }
 8396:                             }
 8397:                         }
 8398:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8399:                     }
 8400:                 }
 8401:                 if ($resulttext ne '') {
 8402:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8403:                 } else {
 8404:                     $resulttext = $nochgmsg;
 8405:                 }
 8406:             } else {
 8407:                 $resulttext = $nochgmsg;
 8408:             }
 8409:         } else {
 8410:             $resulttext = '<span class="LC_error">'.
 8411:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8412:         }
 8413:     } else {
 8414:         $resulttext = $nochgmsg;
 8415:     }
 8416:     return $resulttext;
 8417: }
 8418: 
 8419: sub recurse_check {
 8420:     my ($chkcats,$categories,$depth,$name) = @_;
 8421:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8422:         my $chg = 0;
 8423:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8424:             my $category = $chkcats->[$depth]{$name}[$j];
 8425:             my $item;
 8426:             if ($category eq '') {
 8427:                 $chg ++;
 8428:             } else {
 8429:                 my $deeper = $depth + 1;
 8430:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8431:                 if ($chg) {
 8432:                     $categories->{$item} -= $chg;
 8433:                 }
 8434:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8435:                 $deeper --;
 8436:             }
 8437:         }
 8438:     }
 8439:     return;
 8440: }
 8441: 
 8442: sub recurse_cat_deletes {
 8443:     my ($item,$coursecategories,$deletions) = @_;
 8444:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8445:     my $subdepth = $depth + 1;
 8446:     if (ref($coursecategories) eq 'HASH') {
 8447:         foreach my $subitem (keys(%{$coursecategories})) {
 8448:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8449:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8450:                 delete($coursecategories->{$subitem});
 8451:                 $deletions->{$subitem} = 1;
 8452:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8453:             }  
 8454:         }
 8455:     }
 8456:     return;
 8457: }
 8458: 
 8459: sub get_active_dcs {
 8460:     my ($dom) = @_;
 8461:     my $now = time;
 8462:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 8463:     my %domcoords;
 8464:     my $numdcs = 0;
 8465:     foreach my $server (keys(%dompersonnel)) {
 8466:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8467:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8468:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8469:         }
 8470:     }
 8471:     return %domcoords;
 8472: }
 8473: 
 8474: sub active_dc_picker {
 8475:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
 8476:     my %domcoords = &get_active_dcs($dom); 
 8477:     my @domcoord = keys(%domcoords);
 8478:     if (keys(%currhash)) {
 8479:         foreach my $dc (keys(%currhash)) {
 8480:             unless (exists($domcoords{$dc})) {
 8481:                 push(@domcoord,$dc);
 8482:             }
 8483:         }
 8484:     }
 8485:     @domcoord = sort(@domcoord);
 8486:     my $numdcs = scalar(@domcoord);
 8487:     my $rows = 0;
 8488:     my $table;
 8489:     if ($numdcs > 1) {
 8490:         $table = '<table>';
 8491:         for (my $i=0; $i<@domcoord; $i++) {
 8492:             my $rem = $i%($numinrow);
 8493:             if ($rem == 0) {
 8494:                 if ($i > 0) {
 8495:                     $table .= '</tr>';
 8496:                 }
 8497:                 $table .= '<tr>';
 8498:                 $rows ++;
 8499:             }
 8500:             my $check = '';
 8501:             if ($inputtype eq 'radio') {
 8502:                 if (keys(%currhash) == 0) {
 8503:                     if (!$i) {
 8504:                         $check = ' checked="checked"';
 8505:                     }
 8506:                 } elsif (exists($currhash{$domcoord[$i]})) {
 8507:                     $check = ' checked="checked"';
 8508:                 }
 8509:             } else {
 8510:                 if (exists($currhash{$domcoord[$i]})) {
 8511:                     $check = ' checked="checked"';
 8512:                 }
 8513:             }
 8514:             if ($i == @domcoord - 1) {
 8515:                 my $colsleft = $numinrow - $rem;
 8516:                 if ($colsleft > 1) {
 8517:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
 8518:                 } else {
 8519:                     $table .= '<td class="LC_left_item">';
 8520:                 }
 8521:             } else {
 8522:                 $table .= '<td class="LC_left_item">';
 8523:             }
 8524:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
 8525:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
 8526:             $table .= '<span class="LC_nobreak"><label>'.
 8527:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
 8528:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
 8529:             if ($user ne $dcname.':'.$dcdom) {
 8530:                 $table .=  ' ('.$dcname.':'.$dcdom.')'.
 8531:                            '</label></span></td>';
 8532:             }
 8533:         }
 8534:         $table .= '</tr></table>';
 8535:     } elsif ($numdcs == 1) {
 8536:         if ($inputtype eq 'radio') {
 8537:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />';
 8538:         } else {
 8539:             my $check;
 8540:             if (exists($currhash{$domcoord[0]})) {
 8541:                 $check = ' checked="checked"';
 8542:             }
 8543:             $table .= '<input type="checkbox" name="'.$name.'" '.
 8544:                       'value="'.$domcoord[0].'"'.$check.' />';
 8545:             $rows ++;
 8546:         }
 8547:     }
 8548:     return ($numdcs,$table,$rows);
 8549: }
 8550: 
 8551: sub usersession_titles {
 8552:     return &Apache::lonlocal::texthash(
 8553:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8554:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8555:                spares => 'Servers offloaded to, when busy',
 8556:                version => 'LON-CAPA version requirement',
 8557:                excludedomain => 'Allow all, but exclude specific domains',
 8558:                includedomain => 'Deny all, but include specific domains',
 8559:                primary => 'Primary (checked first)',
 8560:                default => 'Default',
 8561:            );
 8562: }
 8563: 
 8564: sub id_for_thisdom {
 8565:     my (%servers) = @_;
 8566:     my %altids;
 8567:     foreach my $server (keys(%servers)) {
 8568:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8569:         if ($serverhome ne $server) {
 8570:             $altids{$serverhome} = $server;
 8571:         }
 8572:     }
 8573:     return %altids;
 8574: }
 8575: 
 8576: sub count_servers {
 8577:     my ($currbalancer,%servers) = @_;
 8578:     my (@spares,$numspares);
 8579:     foreach my $lonhost (sort(keys(%servers))) {
 8580:         next if ($currbalancer eq $lonhost);
 8581:         push(@spares,$lonhost);
 8582:     }
 8583:     if ($currbalancer) {
 8584:         $numspares = scalar(@spares);
 8585:     } else {
 8586:         $numspares = scalar(@spares) - 1;
 8587:     }
 8588:     return ($numspares,@spares);
 8589: }
 8590: 
 8591: sub lonbalance_targets_js {
 8592:     my ($dom,$types,$servers,$settings) = @_;
 8593:     my $select = &mt('Select');
 8594:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8595:     if (ref($servers) eq 'HASH') {
 8596:         $alltargets = join("','",sort(keys(%{$servers})));
 8597:         my @homedoms;
 8598:         foreach my $server (sort(keys(%{$servers}))) {
 8599:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8600:                 push(@homedoms,'1');
 8601:             } else {
 8602:                 push(@homedoms,'0');
 8603:             }
 8604:         }
 8605:         $allishome = join("','",@homedoms);
 8606:     }
 8607:     if (ref($types) eq 'ARRAY') {
 8608:         if (@{$types} > 0) {
 8609:             @alltypes = @{$types};
 8610:         }
 8611:     }
 8612:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8613:     $allinsttypes = join("','",@alltypes);
 8614:     my (%currbalancer,%currtargets,%currrules,%existing);
 8615:     if (ref($settings) eq 'HASH') {
 8616:         %existing = %{$settings};
 8617:     }
 8618:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 8619:                               \%currtargets,\%currrules);
 8620:     my $balancers = join("','",sort(keys(%currbalancer)));
 8621:     return <<"END";
 8622: 
 8623: <script type="text/javascript">
 8624: // <![CDATA[
 8625: 
 8626: currBalancers = new Array('$balancers');
 8627: 
 8628: function toggleTargets(balnum) {
 8629:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8630:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 8631:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 8632:     var prevbalancer = prevhostitem.value;
 8633:     var baltotal = document.getElementById('loadbalancing_total').value;
 8634:     prevhostitem.value = balancer;
 8635:     if (prevbalancer != '') {
 8636:         var prevIdx = currBalancers.indexOf(prevbalancer);
 8637:         if (prevIdx != -1) {
 8638:             currBalancers.splice(prevIdx,1);
 8639:         }
 8640:     }
 8641:     if (balancer == '') {
 8642:         hideSpares(balnum);
 8643:     } else {
 8644:         var currIdx = currBalancers.indexOf(balancer);
 8645:         if (currIdx == -1) {
 8646:             currBalancers.push(balancer);
 8647:         }
 8648:         var homedoms = new Array('$allishome');
 8649:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 8650:         showSpares(balancer,ishomedom,balnum);
 8651:     }
 8652:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 8653:     return;
 8654: }
 8655: 
 8656: function showSpares(balancer,ishomedom,balnum) {
 8657:     var alltargets = new Array('$alltargets');
 8658:     var insttypes = new Array('$allinsttypes');
 8659:     var offloadtypes = new Array('primary','default');
 8660: 
 8661:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 8662:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 8663:  
 8664:     for (var i=0; i<offloadtypes.length; i++) {
 8665:         var count = 0;
 8666:         for (var j=0; j<alltargets.length; j++) {
 8667:             if (alltargets[j] != balancer) {
 8668:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 8669:                 item.value = alltargets[j];
 8670:                 item.style.textAlign='left';
 8671:                 item.style.textFace='normal';
 8672:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8673:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 8674:                     item.disabled = '';
 8675:                 } else {
 8676:                     item.disabled = 'disabled';
 8677:                     item.checked = false;
 8678:                 }
 8679:                 count ++;
 8680:             }
 8681:         }
 8682:     }
 8683:     for (var k=0; k<insttypes.length; k++) {
 8684:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8685:             if (ishomedom == 1) {
 8686:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8687:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8688:             } else {
 8689:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8690:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8691: 
 8692:             }
 8693:         } else {
 8694:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8695:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8696:         }
 8697:         if ((insttypes[k] != '_LC_external') && 
 8698:             ((insttypes[k] != '_LC_internetdom') ||
 8699:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8700:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 8701:             item.options.length = 0;
 8702:             item.options[0] = new Option("","",true,true);
 8703:             var idx = 0;
 8704:             for (var m=0; m<alltargets.length; m++) {
 8705:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 8706:                     idx ++;
 8707:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8708: 
 8709:                 }
 8710:             }
 8711:         }
 8712:     }
 8713:     return;
 8714: }
 8715: 
 8716: function hideSpares(balnum) {
 8717:     var alltargets = new Array('$alltargets');
 8718:     var insttypes = new Array('$allinsttypes');
 8719:     var offloadtypes = new Array('primary','default');
 8720: 
 8721:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 8722:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 8723: 
 8724:     var total = alltargets.length - 1;
 8725:     for (var i=0; i<offloadtypes; i++) {
 8726:         for (var j=0; j<total; j++) {
 8727:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 8728:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 8729:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8730:         }
 8731:     }
 8732:     for (var k=0; k<insttypes.length; k++) {
 8733:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8734:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8735:         if (insttypes[k] != '_LC_external') {
 8736:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 8737:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 8738:         }
 8739:     }
 8740:     return;
 8741: }
 8742: 
 8743: function checkOffloads(item,balnum,type) {
 8744:     var alltargets = new Array('$alltargets');
 8745:     var offloadtypes = new Array('primary','default');
 8746:     if (item.checked) {
 8747:         var total = alltargets.length - 1;
 8748:         var other;
 8749:         if (type == offloadtypes[0]) {
 8750:             other = offloadtypes[1];
 8751:         } else {
 8752:             other = offloadtypes[0];
 8753:         }
 8754:         for (var i=0; i<total; i++) {
 8755:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 8756:             if (server == item.value) {
 8757:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 8758:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 8759:                 }
 8760:             }
 8761:         }
 8762:     }
 8763:     return;
 8764: }
 8765: 
 8766: function singleServerToggle(balnum,type) {
 8767:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 8768:     if (offloadtoSelIdx == 0) {
 8769:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 8770:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8771: 
 8772:     } else {
 8773:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 8774:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8775:     }
 8776:     return;
 8777: }
 8778: 
 8779: function balanceruleChange(formname,balnum,type) {
 8780:     if (type == '_LC_external') {
 8781:         return; 
 8782:     }
 8783:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 8784:     for (var i=0; i<typesRules.length; i++) {
 8785:         if (formname.elements[typesRules[i]].checked) {
 8786:             if (formname.elements[typesRules[i]].value != 'specific') {
 8787:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 8788:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8789:             } else {
 8790:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8791:             }
 8792:         }
 8793:     }
 8794:     return;
 8795: }
 8796: 
 8797: function balancerDeleteChange(balnum) {
 8798:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8799:     var baltotal = document.getElementById('loadbalancing_total').value;
 8800:     var addtarget;
 8801:     var removetarget;
 8802:     var action = 'delete';
 8803:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 8804:         var lonhost = hostitem.value;
 8805:         var currIdx = currBalancers.indexOf(lonhost);
 8806:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 8807:             if (currIdx != -1) {
 8808:                 currBalancers.splice(currIdx,1);
 8809:             }
 8810:             addtarget = lonhost;
 8811:         } else {
 8812:             if (currIdx == -1) {
 8813:                 currBalancers.push(lonhost);
 8814:             }
 8815:             removetarget = lonhost;
 8816:             action = 'undelete';
 8817:         }
 8818:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 8819:     }
 8820:     return;
 8821: }
 8822: 
 8823: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 8824:     if (baltotal > 1) {
 8825:         var offloadtypes = new Array('primary','default');
 8826:         var alltargets = new Array('$alltargets');
 8827:         var insttypes = new Array('$allinsttypes');
 8828:         for (var i=0; i<baltotal; i++) {
 8829:             if (i != balnum) {
 8830:                 for (var j=0; j<offloadtypes.length; j++) {
 8831:                     var total = alltargets.length - 1;
 8832:                     for (var k=0; k<total; k++) {
 8833:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 8834:                         var server = serveritem.value;
 8835:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 8836:                             if (server == addtarget) {
 8837:                                 serveritem.disabled = '';
 8838:                             }
 8839:                         }
 8840:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8841:                             if (server == removetarget) {
 8842:                                 serveritem.disabled = 'disabled';
 8843:                                 serveritem.checked = false;
 8844:                             }
 8845:                         }
 8846:                     }
 8847:                 }
 8848:                 for (var j=0; j<insttypes.length; j++) {
 8849:                     if (insttypes[j] != '_LC_external') {
 8850:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 8851:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 8852:                             var currSel = singleserver.selectedIndex;
 8853:                             var currVal = singleserver.options[currSel].value;
 8854:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 8855:                                 var numoptions = singleserver.options.length;
 8856:                                 var needsnew = 1;
 8857:                                 for (var k=0; k<numoptions; k++) {
 8858:                                     if (singleserver.options[k] == addtarget) {
 8859:                                         needsnew = 0;
 8860:                                         break;
 8861:                                     }
 8862:                                 }
 8863:                                 if (needsnew == 1) {
 8864:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 8865:                                 }
 8866:                             }
 8867:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8868:                                 singleserver.options.length = 0;
 8869:                                 if ((currVal) && (currVal != removetarget)) {
 8870:                                     singleserver.options[0] = new Option("","",false,false);
 8871:                                 } else {
 8872:                                     singleserver.options[0] = new Option("","",true,true);
 8873:                                 }
 8874:                                 var idx = 0;
 8875:                                 for (var m=0; m<alltargets.length; m++) {
 8876:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
 8877:                                         idx ++;
 8878:                                         if (currVal == alltargets[m]) {
 8879:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
 8880:                                         } else {
 8881:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8882:                                         }
 8883:                                     }
 8884:                                 }
 8885:                             }
 8886:                         }
 8887:                     }
 8888:                 }
 8889:             }
 8890:         }
 8891:     }
 8892:     return;
 8893: }
 8894: 
 8895: // ]]>
 8896: </script>
 8897: 
 8898: END
 8899: }
 8900: 
 8901: sub new_spares_js {
 8902:     my @sparestypes = ('primary','default');
 8903:     my $types = join("','",@sparestypes);
 8904:     my $select = &mt('Select');
 8905:     return <<"END";
 8906: 
 8907: <script type="text/javascript">
 8908: // <![CDATA[
 8909: 
 8910: function updateNewSpares(formname,lonhost) {
 8911:     var types = new Array('$types');
 8912:     var include = new Array();
 8913:     var exclude = new Array();
 8914:     for (var i=0; i<types.length; i++) {
 8915:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8916:         for (var j=0; j<spareboxes.length; j++) {
 8917:             if (formname.elements[spareboxes[j]].checked) {
 8918:                 exclude.push(formname.elements[spareboxes[j]].value);
 8919:             } else {
 8920:                 include.push(formname.elements[spareboxes[j]].value);
 8921:             }
 8922:         }
 8923:     }
 8924:     for (var i=0; i<types.length; i++) {
 8925:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8926:         var selIdx = newSpare.selectedIndex;
 8927:         var currnew = newSpare.options[selIdx].value;
 8928:         var okSpares = new Array();
 8929:         for (var j=0; j<newSpare.options.length; j++) {
 8930:             var possible = newSpare.options[j].value;
 8931:             if (possible != '') {
 8932:                 if (exclude.indexOf(possible) == -1) {
 8933:                     okSpares.push(possible);
 8934:                 } else {
 8935:                     if (currnew == possible) {
 8936:                         selIdx = 0;
 8937:                     }
 8938:                 }
 8939:             }
 8940:         }
 8941:         for (var k=0; k<include.length; k++) {
 8942:             if (okSpares.indexOf(include[k]) == -1) {
 8943:                 okSpares.push(include[k]);
 8944:             }
 8945:         }
 8946:         okSpares.sort();
 8947:         newSpare.options.length = 0;
 8948:         if (selIdx == 0) {
 8949:             newSpare.options[0] = new Option("$select","",true,true);
 8950:         } else {
 8951:             newSpare.options[0] = new Option("$select","",false,false);
 8952:         }
 8953:         for (var m=0; m<okSpares.length; m++) {
 8954:             var idx = m+1;
 8955:             var selThis = 0;
 8956:             if (selIdx != 0) {
 8957:                 if (okSpares[m] == currnew) {
 8958:                     selThis = 1;
 8959:                 }
 8960:             }
 8961:             if (selThis == 1) {
 8962:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8963:             } else {
 8964:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8965:             }
 8966:         }
 8967:     }
 8968:     return;
 8969: }
 8970: 
 8971: function checkNewSpares(lonhost,type) {
 8972:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8973:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8974:     if (chosen != '') { 
 8975:         var othertype;
 8976:         var othernewSpare;
 8977:         if (type == 'primary') {
 8978:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 8979:         }
 8980:         if (type == 'default') {
 8981:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 8982:         }
 8983:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 8984:             othernewSpare.selectedIndex = 0;
 8985:         }
 8986:     }
 8987:     return;
 8988: }
 8989: 
 8990: // ]]>
 8991: </script>
 8992: 
 8993: END
 8994: 
 8995: }
 8996: 
 8997: sub common_domprefs_js {
 8998:     return <<"END";
 8999: 
 9000: <script type="text/javascript">
 9001: // <![CDATA[
 9002: 
 9003: function getIndicesByName(formname,item) {
 9004:     var group = new Array();
 9005:     for (var i=0;i<formname.elements.length;i++) {
 9006:         if (formname.elements[i].name == item) {
 9007:             group.push(formname.elements[i].id);
 9008:         }
 9009:     }
 9010:     return group;
 9011: }
 9012: 
 9013: // ]]>
 9014: </script>
 9015: 
 9016: END
 9017: 
 9018: }
 9019: 
 9020: sub recaptcha_js {
 9021:     my %lt = &captcha_phrases();
 9022:     return <<"END";
 9023: 
 9024: <script type="text/javascript">
 9025: // <![CDATA[
 9026: 
 9027: function updateCaptcha(caller,context) {
 9028:     var privitem;
 9029:     var pubitem;
 9030:     var privtext;
 9031:     var pubtext;
 9032:     if (document.getElementById(context+'_recaptchapub')) {
 9033:         pubitem = document.getElementById(context+'_recaptchapub');
 9034:     } else {
 9035:         return;
 9036:     }
 9037:     if (document.getElementById(context+'_recaptchapriv')) {
 9038:         privitem = document.getElementById(context+'_recaptchapriv');
 9039:     } else {
 9040:         return;
 9041:     }
 9042:     if (document.getElementById(context+'_recaptchapubtxt')) {
 9043:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 9044:     } else {
 9045:         return;
 9046:     }
 9047:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 9048:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 9049:     } else {
 9050:         return;
 9051:     }
 9052:     if (caller.checked) {
 9053:         if (caller.value == 'recaptcha') {
 9054:             pubitem.type = 'text';
 9055:             privitem.type = 'text';
 9056:             pubitem.size = '40';
 9057:             privitem.size = '40';
 9058:             pubtext.innerHTML = "$lt{'pub'}";
 9059:             privtext.innerHTML = "$lt{'priv'}";
 9060:         } else {
 9061:             pubitem.type = 'hidden';
 9062:             privitem.type = 'hidden';
 9063:             pubtext.innerHTML = '';
 9064:             privtext.innerHTML = '';
 9065:         }
 9066:     }
 9067:     return;
 9068: }
 9069: 
 9070: // ]]>
 9071: </script>
 9072: 
 9073: END
 9074: 
 9075: }
 9076: 
 9077: sub credits_js {
 9078:     return <<"END";
 9079: 
 9080: <script type="text/javascript">
 9081: // <![CDATA[
 9082: 
 9083: function toggleCredits(domForm) {
 9084:     if (document.getElementById('credits')) {
 9085:         creditsitem = document.getElementById('credits');
 9086:         var creditsLength = domForm.coursecredits.length;
 9087:         if (creditsLength) {
 9088:             var currval;
 9089:             for (var i=0; i<creditsLength; i++) {
 9090:                 if (domForm.coursecredits[i].checked) {
 9091:                    currval = domForm.coursecredits[i].value;
 9092:                 }
 9093:             }
 9094:             if (currval == 1) {
 9095:                 creditsitem.style.display = 'block';
 9096:             } else {
 9097:                 creditsitem.style.display = 'none';
 9098:             }
 9099:         }
 9100:     }
 9101:     return;
 9102: }
 9103: 
 9104: // ]]>
 9105: </script>
 9106: 
 9107: END
 9108: 
 9109: }
 9110: 
 9111: sub captcha_phrases {
 9112:     return &Apache::lonlocal::texthash (
 9113:                  priv => 'Private key',
 9114:                  pub  => 'Public key',
 9115:                  original  => 'original (CAPTCHA)',
 9116:                  recaptcha => 'successor (ReCAPTCHA)',
 9117:                  notused   => 'unused',
 9118:     );
 9119: }
 9120: 
 9121: 1;

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