File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.168: download - view: text, annotated - select for diffs
Mon Aug 27 00:53:22 2012 UTC (11 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Move &loginhelpdisplay() from lonlogin.pm to lonauth.pm to facilitate reuse.
- Log-in help page configuration moved from helpsettings to login section in
  Domain configuration interface.
- Add ability to upload different help files for the different supported languages.
- Link to log-in help page depends on user's browser language preferences.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.168 2012/08/27 00:53:22 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas,  
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course.
   98: 
   99: Outputs: 1
  100: 
  101: $datatable  - HTML containing form elements which allow settings to be changed. 
  102: 
  103: In the case of course requests, radio buttons are displayed for each institutional
  104: affiliate type (and also default, and _LC_adv) for each of the course types 
  105: (official, unofficial and community).  In each case the radio buttons allow the 
  106: selection of one of four values:
  107: 
  108: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  109: which have the following effects:
  110: 
  111: 0
  112: 
  113: =over
  114: 
  115: - course requests are not allowed for this course types/affiliation
  116: 
  117: =back
  118: 
  119: approval 
  120: 
  121: =over 
  122: 
  123: - course requests must be approved by a Doman Coordinator in the 
  124: course's domain
  125: 
  126: =back
  127: 
  128: validate 
  129: 
  130: =over
  131: 
  132: - an institutional validation (e.g., check requestor is instructor
  133: of record) needs to be passed before the course will be created.  The required
  134: validation is in localenroll.pm on the primary library server for the course 
  135: domain.
  136: 
  137: =back
  138: 
  139: autolimit 
  140: 
  141: =over
  142:  
  143: - course requests will be processed automatically up to a limit of
  144: N requests for the course type for the particular requestor.
  145: If N is undefined, there is no limit to the number of course requests
  146: which a course owner may submit and have processed automatically. 
  147: 
  148: =back
  149: 
  150: =item modify_quotas() 
  151: 
  152: =back
  153: 
  154: =cut
  155: 
  156: package Apache::domainprefs;
  157: 
  158: use strict;
  159: use Apache::Constants qw(:common :http);
  160: use Apache::lonnet;
  161: use Apache::loncommon();
  162: use Apache::lonhtmlcommon();
  163: use Apache::lonlocal;
  164: use Apache::lonmsg();
  165: use Apache::lonconfigsettings;
  166: use LONCAPA qw(:DEFAULT :match);
  167: use LONCAPA::Enrollment;
  168: use LONCAPA::lonauthcgi();
  169: use File::Copy;
  170: use Locale::Language;
  171: use DateTime::TimeZone;
  172: use DateTime::Locale;
  173: 
  174: my $registered_cleanup;
  175: my $modified_urls;
  176: 
  177: sub handler {
  178:     my $r=shift;
  179:     if ($r->header_only) {
  180:         &Apache::loncommon::content_type($r,'text/html');
  181:         $r->send_http_header;
  182:         return OK;
  183:     }
  184: 
  185:     my $context = 'domain';
  186:     my $dom = $env{'request.role.domain'};
  187:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  188:     if (&Apache::lonnet::allowed('mau',$dom)) {
  189:         &Apache::loncommon::content_type($r,'text/html');
  190:         $r->send_http_header;
  191:     } else {
  192:         $env{'user.error.msg'}=
  193:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  194:         return HTTP_NOT_ACCEPTABLE;
  195:     }
  196: 
  197:     $registered_cleanup=0;
  198:     @{$modified_urls}=();
  199: 
  200:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  201:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  202:                                             ['phase','actions']);
  203:     my $phase = 'pickactions';
  204:     if ( exists($env{'form.phase'}) ) {
  205:         $phase = $env{'form.phase'};
  206:     }
  207:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  208:     my %domconfig =
  209:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  210:                 'quotas','autoenroll','autoupdate','autocreate',
  211:                 'directorysrch','usercreation','usermodification',
  212:                 'contacts','defaults','scantron','coursecategories',
  213:                 'serverstatuses','requestcourses','helpsettings',
  214:                 'coursedefaults','usersessions','loadbalancing',
  215:                 'requestauthor'],$dom);
  216:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  217:                        'autoupdate','autocreate','directorysrch','contacts',
  218:                        'usercreation','usermodification','scantron',
  219:                        'requestcourses','requestauthor','coursecategories',
  220:                        'serverstatuses','helpsettings',
  221:                        'coursedefaults','usersessions');
  222:     if (keys(%servers) > 1) {
  223:         push(@prefs_order,'loadbalancing');
  224:     }
  225:     my %prefs = (
  226:         'rolecolors' =>
  227:                    { text => 'Default color schemes',
  228:                      help => 'Domain_Configuration_Color_Schemes',
  229:                      header => [{col1 => 'Student Settings',
  230:                                  col2 => '',},
  231:                                 {col1 => 'Coordinator Settings',
  232:                                  col2 => '',},
  233:                                 {col1 => 'Author Settings',
  234:                                  col2 => '',},
  235:                                 {col1 => 'Administrator Settings',
  236:                                  col2 => '',}],
  237:                     },
  238:         'login' =>
  239:                     { text => 'Log-in page options',
  240:                       help => 'Domain_Configuration_Login_Page',
  241:                       header => [{col1 => 'Log-in Page Items',
  242:                                   col2 => '',},
  243:                                  {col1 => 'Log-in Help',
  244:                                   col2 => 'Value'}],
  245:                     },
  246:         'defaults' => 
  247:                     { text => 'Default authentication/language/timezone/portal',
  248:                       help => 'Domain_Configuration_LangTZAuth',
  249:                       header => [{col1 => 'Setting',
  250:                                   col2 => 'Value'}],
  251:                     },
  252:         'quotas' => 
  253:                     { text => 'Blogs, personal web pages, webDAV, portfolios',
  254:                       help => 'Domain_Configuration_Quotas',
  255:                       header => [{col1 => 'User affiliation',
  256:                                   col2 => 'Available tools',
  257:                                   col3 => 'Portfolio quota',}],
  258:                     },
  259:         'autoenroll' =>
  260:                    { text => 'Auto-enrollment settings',
  261:                      help => 'Domain_Configuration_Auto_Enrollment',
  262:                      header => [{col1 => 'Configuration setting',
  263:                                  col2 => 'Value(s)'}],
  264:                    },
  265:         'autoupdate' => 
  266:                    { text => 'Auto-update settings',
  267:                      help => 'Domain_Configuration_Auto_Updates',
  268:                      header => [{col1 => 'Setting',
  269:                                  col2 => 'Value',},
  270:                                 {col1 => 'Setting',
  271:                                  col2 => 'Affiliation'},
  272:                                 {col1 => 'User population',
  273:                                  col2 => 'Updateable user data'}],
  274:                   },
  275:         'autocreate' => 
  276:                   { text => 'Auto-course creation settings',
  277:                      help => 'Domain_Configuration_Auto_Creation',
  278:                      header => [{col1 => 'Configuration Setting',
  279:                                  col2 => 'Value',}],
  280:                   },
  281:         'directorysrch' => 
  282:                   { text => 'Institutional directory searches',
  283:                     help => 'Domain_Configuration_InstDirectory_Search',
  284:                     header => [{col1 => 'Setting',
  285:                                 col2 => 'Value',}],
  286:                   },
  287:         'contacts' =>
  288:                   { text => 'Contact Information',
  289:                     help => 'Domain_Configuration_Contact_Info',
  290:                     header => [{col1 => 'Setting',
  291:                                 col2 => 'Value',}],
  292:                   },
  293: 
  294:         'usercreation' => 
  295:                   { text => 'User creation',
  296:                     help => 'Domain_Configuration_User_Creation',
  297:                     header => [{col1 => 'Format rule type',
  298:                                 col2 => 'Format rules in force'},
  299:                                {col1 => 'User account creation',
  300:                                 col2 => 'Usernames which may be created',},
  301:                                {col1 => 'Context',
  302:                                 col2 => 'Assignable authentication types'}],
  303:                   },
  304:         'usermodification' =>
  305:                   { text => 'User modification',
  306:                     help => 'Domain_Configuration_User_Modification',
  307:                     header => [{col1 => 'Target user has role',
  308:                                 col2 => 'User information updateable in author context'},
  309:                                {col1 => 'Target user has role',
  310:                                 col2 => 'User information updateable in course context'},
  311:                                {col1 => "Status of user",
  312:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  313:                   },
  314:         'scantron' =>
  315:                   { text => 'Bubblesheet format file',
  316:                     help => 'Domain_Configuration_Scantron_Format',
  317:                     header => [ {col1 => 'Item',
  318:                                  col2 => '',
  319:                               }],
  320:                   },
  321:         'requestcourses' => 
  322:                  {text => 'Request creation of courses',
  323:                   help => 'Domain_Configuration_Request_Courses',
  324:                   header => [{col1 => 'User affiliation',
  325:                               col2 => 'Availability/Processing of requests',},
  326:                              {col1 => 'Setting',
  327:                               col2 => 'Value'}],
  328:                  },
  329:         'requestauthor' =>
  330:                  {text => 'Request authoring space',
  331:                   help => 'Domain_Configuration_Request_Author',
  332:                   header => [{col1 => 'User affiliation',
  333:                               col2 => 'Availability/Processing of requests',},
  334:                              {col1 => 'Setting',
  335:                               col2 => 'Value'}],
  336:                  },
  337:         'coursecategories' =>
  338:                   { text => 'Cataloging of courses/communities',
  339:                     help => 'Domain_Configuration_Cataloging_Courses',
  340:                     header => [{col1 => 'Category settings',
  341:                                 col2 => '',},
  342:                                {col1 => 'Categories',
  343:                                 col2 => '',
  344:                                }],
  345:                   },
  346:         'serverstatuses' =>
  347:                  {text   => 'Access to server status pages',
  348:                   help   => 'Domain_Configuration_Server_Status',
  349:                   header => [{col1 => 'Status Page',
  350:                               col2 => 'Other named users',
  351:                               col3 => 'Specific IPs',
  352:                             }],
  353:                  },
  354:         'helpsettings' =>
  355:                  {text   => 'Help page settings',
  356:                   help   => 'Domain_Configuration_Help_Settings',
  357:                   header => [{col1 => 'Help Settings (logged-in users)',
  358:                               col2 => 'Value'}],
  359:                  },
  360:         'coursedefaults' => 
  361:                  {text => 'Course/Community defaults',
  362:                   help => 'Domain_Configuration_Course_Defaults',
  363:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  364:                               col2 => 'Value',},
  365:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  366:                               col2 => 'Value',},],
  367:                  },
  368:         'privacy' => 
  369:                  {text   => 'User Privacy',
  370:                   help   => 'Domain_Configuration_User_Privacy',
  371:                   header => [{col1 => 'Setting',
  372:                               col2 => 'Value',}],
  373:                  },
  374:         'usersessions' =>
  375:                  {text  => 'User session hosting/offloading',
  376:                   help  => 'Domain_Configuration_User_Sessions',
  377:                   header => [{col1 => 'Domain server',
  378:                               col2 => 'Servers to offload sessions to when busy'},
  379:                              {col1 => 'Hosting of users from other domains',
  380:                               col2 => 'Rules'},
  381:                              {col1 => "Hosting domain's own users elsewhere",
  382:                               col2 => 'Rules'}],
  383:                  },
  384:          'loadbalancing' =>
  385:                  {text  => 'Dedicated Load Balancer',
  386:                   help  => 'Domain_Configuration_Load_Balancing',
  387:                   header => [{col1 => 'Server',
  388:                               col2 => 'Default destinations',
  389:                               col3 => 'User affliation',
  390:                               col4 => 'Overrides'},
  391:                             ],
  392:                  },
  393:     );
  394:     if (keys(%servers) > 1) {
  395:         $prefs{'login'}  = { text   => 'Log-in page options',
  396:                              help   => 'Domain_Configuration_Login_Page',
  397:                             header => [{col1 => 'Log-in Service',
  398:                                         col2 => 'Server Setting',},
  399:                                        {col1 => 'Log-in Page Items',
  400:                                         col2 => ''},
  401:                                        {col1 => 'Log-in Help',
  402:                                         col2 => 'Value'}],
  403:                            };
  404:     }
  405:     my @roles = ('student','coordinator','author','admin');
  406:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  407:     &Apache::lonhtmlcommon::add_breadcrumb
  408:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  409:       text=>"Settings to display/modify"});
  410:     my $confname = $dom.'-domainconfig';
  411:     if ($phase eq 'process') {
  412:         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
  413:     } elsif ($phase eq 'display') {
  414:         my $js = &recaptcha_js();
  415:         if (keys(%servers) > 1) {
  416:             my ($othertitle,$usertypes,$types) =
  417:                 &Apache::loncommon::sorted_inst_types($dom);
  418:             $js = &lonbalance_targets_js($dom,$types,\%servers).
  419:                   &new_spares_js().
  420:                   &common_domprefs_js().
  421:                   &Apache::loncommon::javascript_array_indexof();
  422:         }
  423:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  424:     } else {
  425:         if (keys(%domconfig) == 0) {
  426:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  427:             my @ids=&Apache::lonnet::current_machine_ids();
  428:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  429:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  430:                 my @loginimages = ('img','logo','domlogo','login');
  431:                 my $custom_img_count = 0;
  432:                 foreach my $img (@loginimages) {
  433:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  434:                         $custom_img_count ++;
  435:                     }
  436:                 }
  437:                 foreach my $role (@roles) {
  438:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  439:                         $custom_img_count ++;
  440:                     }
  441:                 }
  442:                 if ($custom_img_count > 0) {
  443:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  444:                     my $switch_server = &check_switchserver($dom,$confname);
  445:                     $r->print(
  446:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  447:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  448:     &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 />'.
  449:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  450:                     if ($switch_server) {
  451:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  452:                     }
  453:                     $r->print(&Apache::loncommon::end_page());
  454:                     return OK;
  455:                 }
  456:             }
  457:         }
  458:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  459:     }
  460:     return OK;
  461: }
  462: 
  463: sub process_changes {
  464:     my ($r,$dom,$confname,$action,$roles,$values) = @_;
  465:     my %domconfig;
  466:     if (ref($values) eq 'HASH') {
  467:         %domconfig = %{$values};
  468:     }
  469:     my $output;
  470:     if ($action eq 'login') {
  471:         $output = &modify_login($r,$dom,$confname,%domconfig);
  472:     } elsif ($action eq 'rolecolors') {
  473:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  474:                                      %domconfig);
  475:     } elsif ($action eq 'quotas') {
  476:         $output = &modify_quotas($dom,$action,%domconfig);
  477:     } elsif ($action eq 'autoenroll') {
  478:         $output = &modify_autoenroll($dom,%domconfig);
  479:     } elsif ($action eq 'autoupdate') {
  480:         $output = &modify_autoupdate($dom,%domconfig);
  481:     } elsif ($action eq 'autocreate') {
  482:         $output = &modify_autocreate($dom,%domconfig);
  483:     } elsif ($action eq 'directorysrch') {
  484:         $output = &modify_directorysrch($dom,%domconfig);
  485:     } elsif ($action eq 'usercreation') {
  486:         $output = &modify_usercreation($dom,%domconfig);
  487:     } elsif ($action eq 'usermodification') {
  488:         $output = &modify_usermodification($dom,%domconfig);
  489:     } elsif ($action eq 'contacts') {
  490:         $output = &modify_contacts($dom,%domconfig);
  491:     } elsif ($action eq 'defaults') {
  492:         $output = &modify_defaults($dom,$r);
  493:     } elsif ($action eq 'scantron') {
  494:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  495:     } elsif ($action eq 'coursecategories') {
  496:         $output = &modify_coursecategories($dom,%domconfig);
  497:     } elsif ($action eq 'serverstatuses') {
  498:         $output = &modify_serverstatuses($dom,%domconfig);
  499:     } elsif ($action eq 'requestcourses') {
  500:         $output = &modify_quotas($dom,$action,%domconfig);
  501:     } elsif ($action eq 'requestauthor') {
  502:         $output = &modify_quotas($dom,$action,%domconfig);
  503:     } elsif ($action eq 'helpsettings') {
  504:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  505:     } elsif ($action eq 'coursedefaults') {
  506:         $output = &modify_coursedefaults($dom,%domconfig);
  507:     } elsif ($action eq 'usersessions') {
  508:         $output = &modify_usersessions($dom,%domconfig);
  509:     } elsif ($action eq 'loadbalancing') {
  510:         $output = &modify_loadbalancing($dom,%domconfig);
  511:     }
  512:     return $output;
  513: }
  514: 
  515: sub print_config_box {
  516:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  517:     my $rowtotal = 0;
  518:     my $output;
  519:     if ($action eq 'coursecategories') {
  520:         $output = &coursecategories_javascript($settings);
  521:     }
  522:     $output .= 
  523:          '<table class="LC_nested_outer">
  524:           <tr>
  525:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  526:            &mt($item->{text}).'&nbsp;'.
  527:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  528:           '</tr>';
  529:     $rowtotal ++;
  530:     my $numheaders = 1;
  531:     if (ref($item->{'header'}) eq 'ARRAY') {
  532:         $numheaders = scalar(@{$item->{'header'}});
  533:     }
  534:     if ($numheaders > 1) {
  535:         my $colspan = '';
  536:         my $rightcolspan = '';
  537:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') || 
  538:             (($action eq 'login') && ($numheaders < 3))) {
  539:             $colspan = ' colspan="2"';
  540:         }
  541:         if ($action eq 'usersessions') {
  542:             $rightcolspan = ' colspan="3"'; 
  543:         }
  544:         $output .= '
  545:           <tr>
  546:            <td>
  547:             <table class="LC_nested">
  548:              <tr class="LC_info_row">
  549:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  550:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  551:              </tr>';
  552:         $rowtotal ++;
  553:         if ($action eq 'autoupdate') {
  554:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  555:         } elsif ($action eq 'usercreation') {
  556:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  557:         } elsif ($action eq 'usermodification') {
  558:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  559:         } elsif ($action eq 'coursecategories') {
  560:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  561:         } elsif ($action eq 'login') {
  562:             if ($numheaders == 3) {
  563:                 $colspan = ' colspan="2"';
  564:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  565:             } else {
  566:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  567:             }
  568:         } elsif ($action eq 'requestcourses') {
  569:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  570:         } elsif ($action eq 'requestauthor') {
  571:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  572:         } elsif ($action eq 'usersessions') {
  573:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  574:         } elsif ($action eq 'rolecolors') {
  575:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  576:         } elsif ($action eq 'coursedefaults') {
  577:             $output .= &print_coursedefaults('top',$dom,$settings,\$rowtotal);
  578:         }
  579:         $output .= '
  580:            </table>
  581:           </td>
  582:          </tr>
  583:          <tr>
  584:            <td>
  585:             <table class="LC_nested">
  586:              <tr class="LC_info_row">
  587:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  588:         $output .= '
  589:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  590:              </tr>';
  591:             $rowtotal ++;
  592:         if ($action eq 'autoupdate') {
  593:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  594:            </table>
  595:           </td>
  596:          </tr>
  597:          <tr>
  598:            <td>
  599:             <table class="LC_nested">
  600:              <tr class="LC_info_row">
  601:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  602:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  603:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  604:             $rowtotal ++;
  605:         } elsif ($action eq 'usercreation') {
  606:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  607:            </table>
  608:           </td>
  609:          </tr>
  610:          <tr>
  611:            <td>
  612:             <table class="LC_nested">
  613:              <tr class="LC_info_row">
  614:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  615:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  616:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  617:             $rowtotal ++;
  618:         } elsif ($action eq 'usermodification') {
  619:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  620:            </table>
  621:           </td>
  622:          </tr>
  623:          <tr>
  624:            <td>
  625:             <table class="LC_nested">
  626:              <tr class="LC_info_row">
  627:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  628:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  629:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  630:             $rowtotal ++;
  631:         } elsif ($action eq 'coursecategories') {
  632:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  633:         } elsif ($action eq 'login') {
  634:             if ($numheaders == 3) {
  635:                 $output .= &print_login('page',$dom,$confname,$phase,$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"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  645:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  646:                 $rowtotal ++;
  647:             } else {
  648:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  649:             }
  650:         } elsif ($action eq 'requestcourses') {
  651:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  652:         } elsif ($action eq 'requestauthor') {
  653:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  654:         } elsif ($action eq 'usersessions') {
  655:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  656:            </table>
  657:           </td>
  658:          </tr>
  659:          <tr>
  660:            <td>
  661:             <table class="LC_nested">
  662:              <tr class="LC_info_row">
  663:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  664:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  665:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  666:             $rowtotal ++;
  667:         } elsif ($action eq 'coursedefaults') {
  668:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  669:         } elsif ($action eq 'rolecolors') {
  670:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  671:            </table>
  672:           </td>
  673:          </tr>
  674:          <tr>
  675:            <td>
  676:             <table class="LC_nested">
  677:              <tr class="LC_info_row">
  678:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  679:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  680:               <td class="LC_right_item" valign="top">'.
  681:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  682:              </tr>'.
  683:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  684:            </table>
  685:           </td>
  686:          </tr>
  687:          <tr>
  688:            <td>
  689:             <table class="LC_nested">
  690:              <tr class="LC_info_row">
  691:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  692:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  693:              </tr>'.
  694:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  695:             $rowtotal += 2;
  696:         }
  697:     } else {
  698:         $output .= '
  699:           <tr>
  700:            <td>
  701:             <table class="LC_nested">
  702:              <tr class="LC_info_row">';
  703:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  704:             $output .= '  
  705:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  706:         } elsif ($action eq 'serverstatuses') {
  707:             $output .= '
  708:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  709:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  710: 
  711:         } else {
  712:             $output .= '
  713:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  714:         }
  715:         if (defined($item->{'header'}->[0]->{'col3'})) {
  716:             $output .= '<td class="LC_left_item" valign="top">'.
  717:                        &mt($item->{'header'}->[0]->{'col2'});
  718:             if ($action eq 'serverstatuses') {
  719:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  720:             } 
  721:         } else {
  722:             $output .= '<td class="LC_right_item" valign="top">'.
  723:                        &mt($item->{'header'}->[0]->{'col2'});
  724:         }
  725:         $output .= '</td>';
  726:         if ($item->{'header'}->[0]->{'col3'}) {
  727:             if (defined($item->{'header'}->[0]->{'col4'})) {
  728:                 $output .= '<td class="LC_left_item" valign="top">'.
  729:                             &mt($item->{'header'}->[0]->{'col3'});
  730:             } else {
  731:                 $output .= '<td class="LC_right_item" valign="top">'.
  732:                            &mt($item->{'header'}->[0]->{'col3'});
  733:             }
  734:             if ($action eq 'serverstatuses') {
  735:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  736:             }
  737:             $output .= '</td>';
  738:         }
  739:         if ($item->{'header'}->[0]->{'col4'}) {
  740:             $output .= '<td class="LC_right_item" valign="top">'.
  741:                        &mt($item->{'header'}->[0]->{'col4'});
  742:         }
  743:         $output .= '</tr>';
  744:         $rowtotal ++;
  745:         if ($action eq 'quotas') {
  746:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  747:         } elsif ($action eq 'autoenroll') {
  748:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  749:         } elsif ($action eq 'autocreate') {
  750:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  751:         } elsif ($action eq 'directorysrch') {
  752:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  753:         } elsif ($action eq 'contacts') {
  754:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  755:         } elsif ($action eq 'defaults') {
  756:             $output .= &print_defaults($dom,\$rowtotal);
  757:         } elsif ($action eq 'scantron') {
  758:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  759:         } elsif ($action eq 'serverstatuses') {
  760:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  761:         } elsif ($action eq 'helpsettings') {
  762:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  763:         } elsif ($action eq 'loadbalancing') {
  764:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  765:         }
  766:     }
  767:     $output .= '
  768:    </table>
  769:   </td>
  770:  </tr>
  771: </table><br />';
  772:     return ($output,$rowtotal);
  773: }
  774: 
  775: sub print_login {
  776:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  777:     my ($css_class,$datatable);
  778:     my %choices = &login_choices();
  779: 
  780:     if ($caller eq 'service') {
  781:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  782:         my $choice = $choices{'disallowlogin'};
  783:         $css_class = ' class="LC_odd_row"';
  784:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  785:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  786:                       '<th>'.$choices{'server'}.'</th>'.
  787:                       '<th>'.$choices{'serverpath'}.'</th>'.
  788:                       '<th>'.$choices{'custompath'}.'</th>'.
  789:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  790:         my %disallowed;
  791:         if (ref($settings) eq 'HASH') {
  792:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  793:                %disallowed = %{$settings->{'loginvia'}};
  794:             }
  795:         }
  796:         foreach my $lonhost (sort(keys(%servers))) {
  797:             my $direct = 'selected="selected"';
  798:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  799:                 if ($disallowed{$lonhost}{'server'} ne '') {
  800:                     $direct = '';
  801:                 }
  802:             }
  803:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  804:                           '<td><select name="'.$lonhost.'_server">'.
  805:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  806:                           '</option>';
  807:             foreach my $hostid (keys(%servers)) {
  808:                 next if ($servers{$hostid} eq $servers{$lonhost});
  809:                 my $selected = '';
  810:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  811:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  812:                         $selected = 'selected="selected"';
  813:                     }
  814:                 }
  815:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  816:                               $servers{$hostid}.'</option>';
  817:             }
  818:             $datatable .= '</select></td>'.
  819:                           '<td><select name="'.$lonhost.'_serverpath">';
  820:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  821:                 my $pathname = $path;
  822:                 if ($path eq 'custom') {
  823:                     $pathname = &mt('Custom Path').' ->';
  824:                 }
  825:                 my $selected = '';
  826:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  827:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  828:                         $selected = 'selected="selected"';
  829:                     }
  830:                 } elsif ($path eq '') {
  831:                     $selected = 'selected="selected"';
  832:                 }
  833:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  834:             }
  835:             $datatable .= '</select></td>';
  836:             my ($custom,$exempt);
  837:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  838:                 $custom = $disallowed{$lonhost}{'custompath'};
  839:                 $exempt = $disallowed{$lonhost}{'exempt'};
  840:             }
  841:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  842:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  843:                           '</tr>';
  844:         }
  845:         $datatable .= '</table></td></tr>';
  846:         return $datatable;
  847:     } elsif ($caller eq 'page') {
  848:         my %defaultchecked = ( 
  849:                                'coursecatalog' => 'on',
  850:                                'adminmail'     => 'off',
  851:                                'newuser'       => 'off',
  852:                              );
  853:         my @toggles = ('coursecatalog','adminmail','newuser');
  854:         my (%checkedon,%checkedoff);
  855:         foreach my $item (@toggles) {
  856:             if ($defaultchecked{$item} eq 'on') { 
  857:                 $checkedon{$item} = ' checked="checked" ';
  858:                 $checkedoff{$item} = ' ';
  859:             } elsif ($defaultchecked{$item} eq 'off') {
  860:                 $checkedoff{$item} = ' checked="checked" ';
  861:                 $checkedon{$item} = ' ';
  862:             }
  863:         }
  864:         my @images = ('img','logo','domlogo','login');
  865:         my @logintext = ('textcol','bgcol');
  866:         my @bgs = ('pgbg','mainbg','sidebg');
  867:         my @links = ('link','alink','vlink');
  868:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  869:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  870:         my (%is_custom,%designs);
  871:         my %defaults = (
  872:                        font => $defaultdesign{'login.font'},
  873:                        );
  874:         foreach my $item (@images) {
  875:             $defaults{$item} = $defaultdesign{'login.'.$item};
  876:             $defaults{'showlogo'}{$item} = 1;
  877:         }
  878:         foreach my $item (@bgs) {
  879:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  880:         }
  881:         foreach my $item (@logintext) {
  882:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  883:         }
  884:         foreach my $item (@links) {
  885:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  886:         }
  887:         if (ref($settings) eq 'HASH') {
  888:             foreach my $item (@toggles) {
  889:                 if ($settings->{$item} eq '1') {
  890:                     $checkedon{$item} =  ' checked="checked" ';
  891:                     $checkedoff{$item} = ' ';
  892:                 } elsif ($settings->{$item} eq '0') {
  893:                     $checkedoff{$item} =  ' checked="checked" ';
  894:                     $checkedon{$item} = ' ';
  895:                 }
  896:             }
  897:             foreach my $item (@images) {
  898:                 if (defined($settings->{$item})) {
  899:                     $designs{$item} = $settings->{$item};
  900:                     $is_custom{$item} = 1;
  901:                 }
  902:                 if (defined($settings->{'showlogo'}{$item})) {
  903:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  904:                 }
  905:             }
  906:             foreach my $item (@logintext) {
  907:                 if ($settings->{$item} ne '') {
  908:                     $designs{'logintext'}{$item} = $settings->{$item};
  909:                     $is_custom{$item} = 1;
  910:                 }
  911:             }
  912:             if ($settings->{'font'} ne '') {
  913:                 $designs{'font'} = $settings->{'font'};
  914:                 $is_custom{'font'} = 1;
  915:             }
  916:             foreach my $item (@bgs) {
  917:                 if ($settings->{$item} ne '') {
  918:                     $designs{'bgs'}{$item} = $settings->{$item};
  919:                     $is_custom{$item} = 1;
  920:                 }
  921:             }
  922:             foreach my $item (@links) {
  923:                 if ($settings->{$item} ne '') {
  924:                     $designs{'links'}{$item} = $settings->{$item};
  925:                     $is_custom{$item} = 1;
  926:                 }
  927:             }
  928:         } else {
  929:             if ($designhash{$dom.'.login.font'} ne '') {
  930:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  931:                 $is_custom{'font'} = 1;
  932:             }
  933:             foreach my $item (@images) {
  934:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  935:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  936:                     $is_custom{$item} = 1;
  937:                 }
  938:             }
  939:             foreach my $item (@bgs) {
  940:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  941:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  942:                     $is_custom{$item} = 1;
  943:                 }
  944:             }
  945:             foreach my $item (@links) {
  946:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  947:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  948:                     $is_custom{$item} = 1;
  949:                 }
  950:             }
  951:         }
  952:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  953:                                                       logo => 'Institution Logo',
  954:                                                       domlogo => 'Domain Logo',
  955:                                                       login => 'Login box');
  956:         my $itemcount = 1;
  957:         foreach my $item (@toggles) {
  958:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
  959:             $datatable .=  
  960:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  961:                 '</td><td>'.
  962:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
  963:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
  964:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
  965:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
  966:                 '</tr>';
  967:             $itemcount ++;
  968:         }
  969:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
  970:         $datatable .= '</tr></table></td></tr>';
  971:     } elsif ($caller eq 'help') {
  972:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
  973:         my $switchserver = &check_switchserver($dom,$confname);
  974:         my $itemcount = 1;
  975:         $defaulturl = '/adm/loginproblems.html';
  976:         $defaulttype = 'default';
  977:         %lt = &Apache::lonlocal::texthash (
  978:                      del     => 'Delete?',
  979:                      rep     => 'Replace:',
  980:                      upl     => 'Upload:',
  981:                      default => 'Default',
  982:                      custom  => 'Custom',
  983:                                              );
  984:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
  985:         my @currlangs;
  986:         if (ref($settings) eq 'HASH') {
  987:             if (ref($settings->{'helpurl'}) eq 'HASH') {
  988:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
  989:                     next if ($settings->{'helpurl'}{$key} eq '');
  990:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
  991:                     $type{$key} = 'custom';
  992:                     unless ($key eq 'nolang') {
  993:                         push(@currlangs,$key);
  994:                     }
  995:                 }
  996:             } elsif ($settings->{'helpurl'} ne '') {
  997:                 $type{'nolang'} = 'custom';
  998:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
  999:             }
 1000:         }
 1001:         foreach my $lang ('nolang',sort(@currlangs)) {
 1002:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1003:             $datatable .= '<tr'.$css_class.'>';
 1004:             if ($url{$lang} eq '') {
 1005:                 $url{$lang} = $defaulturl;
 1006:             }
 1007:             if ($type{$lang} eq '') {
 1008:                 $type{$lang} = $defaulttype;
 1009:             }
 1010:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1011:             if ($lang eq 'nolang') {
 1012:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1013:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1014:             } else {
 1015:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1016:                                   $langchoices{$lang},
 1017:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1018:             }
 1019:             $datatable .= '</span></td>'."\n".
 1020:                           '<td class="LC_left_item">';
 1021:             if ($type{$lang} eq 'custom') {
 1022:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1023:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1024:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1025:             } else {
 1026:                 $datatable .= $lt{'upl'};
 1027:             }
 1028:             $datatable .='<br />';
 1029:             if ($switchserver) {
 1030:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1031:             } else {
 1032:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1033:             }
 1034:             $datatable .= '</td></tr>';
 1035:             $itemcount ++;
 1036:         }
 1037:         my @addlangs;
 1038:         foreach my $lang (sort(keys(%langchoices))) {
 1039:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1040:             push(@addlangs,$lang);
 1041:         }
 1042:         if (@addlangs > 0) {
 1043:             my %toadd;
 1044:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1045:             $toadd{''} = &mt('Select');
 1046:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1047:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1048:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1049:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1050:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1051:             if ($switchserver) {
 1052:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1053:             } else {
 1054:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1055:             }
 1056:             $datatable .= '</td></tr>';
 1057:         }
 1058:     }
 1059:     return $datatable;
 1060: }
 1061: 
 1062: sub login_choices {
 1063:     my %choices =
 1064:         &Apache::lonlocal::texthash (
 1065:             coursecatalog => 'Display Course/Community Catalog link?',
 1066:             adminmail     => "Display Administrator's E-mail Address?",
 1067:             disallowlogin => "Login page requests redirected",
 1068:             hostid        => "Server",
 1069:             server        => "Redirect to:",
 1070:             serverpath    => "Path",
 1071:             custompath    => "Custom", 
 1072:             exempt        => "Exempt IP(s)",
 1073:             directlogin   => "No redirect",
 1074:             newuser       => "Link to create a user account",
 1075:             img           => "Header",
 1076:             logo          => "Main Logo",
 1077:             domlogo       => "Domain Logo",
 1078:             login         => "Log-in Header", 
 1079:             textcol       => "Text color",
 1080:             bgcol         => "Box color",
 1081:             bgs           => "Background colors",
 1082:             links         => "Link colors",
 1083:             font          => "Font color",
 1084:             pgbg          => "Header",
 1085:             mainbg        => "Page",
 1086:             sidebg        => "Login box",
 1087:             link          => "Link",
 1088:             alink         => "Active link",
 1089:             vlink         => "Visited link",
 1090:         );
 1091:     return %choices;
 1092: }
 1093: 
 1094: sub print_rolecolors {
 1095:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1096:     my %choices = &color_font_choices();
 1097:     my @bgs = ('pgbg','tabbg','sidebg');
 1098:     my @links = ('link','alink','vlink');
 1099:     my @images = ('img');
 1100:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1101:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1102:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1103:     my (%is_custom,%designs);
 1104:     my %defaults = (
 1105:                    img => $defaultdesign{$role.'.img'},
 1106:                    font => $defaultdesign{$role.'.font'},
 1107: 		   fontmenu => $defaultdesign{$role.'.fontmenu'},
 1108:                    );
 1109:     foreach my $item (@bgs) {
 1110:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1111:     }
 1112:     foreach my $item (@links) {
 1113:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1114:     }
 1115:     if (ref($settings) eq 'HASH') {
 1116:         if (ref($settings->{$role}) eq 'HASH') {
 1117:             if ($settings->{$role}->{'img'} ne '') {
 1118:                 $designs{'img'} = $settings->{$role}->{'img'};
 1119:                 $is_custom{'img'} = 1;
 1120:             }
 1121:             if ($settings->{$role}->{'font'} ne '') {
 1122:                 $designs{'font'} = $settings->{$role}->{'font'};
 1123:                 $is_custom{'font'} = 1;
 1124:             }
 1125:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1126:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1127:                 $is_custom{'fontmenu'} = 1;
 1128:             }
 1129:             foreach my $item (@bgs) {
 1130:                 if ($settings->{$role}->{$item} ne '') {
 1131:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1132:                     $is_custom{$item} = 1;
 1133:                 }
 1134:             }
 1135:             foreach my $item (@links) {
 1136:                 if ($settings->{$role}->{$item} ne '') {
 1137:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1138:                     $is_custom{$item} = 1;
 1139:                 }
 1140:             }
 1141:         }
 1142:     } else {
 1143:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1144:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1145:             $is_custom{'img'} = 1;
 1146:         }
 1147:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1148:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1149:             $is_custom{'fontmenu'} = 1; 
 1150:         }
 1151:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1152:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1153:             $is_custom{'font'} = 1;
 1154:         }
 1155:         foreach my $item (@bgs) {
 1156:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1157:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1158:                 $is_custom{$item} = 1;
 1159:             
 1160:             }
 1161:         }
 1162:         foreach my $item (@links) {
 1163:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1164:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1165:                 $is_custom{$item} = 1;
 1166:             }
 1167:         }
 1168:     }
 1169:     my $itemcount = 1;
 1170:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1171:     $datatable .= '</tr></table></td></tr>';
 1172:     return $datatable;
 1173: }
 1174: 
 1175: sub display_color_options {
 1176:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1177:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1178:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1179:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1180:     my $datatable = '<tr'.$css_class.'>'.
 1181:         '<td>'.$choices->{'font'}.'</td>';
 1182:     if (!$is_custom->{'font'}) {
 1183:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1184:     } else {
 1185:         $datatable .= '<td>&nbsp;</td>';
 1186:     }
 1187:     my $fontlink = &color_pick($phase,$role,'font',$choices->{'font'},$designs->{'font'});
 1188:     $datatable .= '<td><span class="LC_nobreak">'.
 1189:                   '<input type="text" size="10" name="'.$role.'_font"'.
 1190:                   ' value="'.$designs->{'font'}.'" />&nbsp;'.$fontlink.
 1191:                   '&nbsp;<span id="css_'.$role.'_font" style="background-color: '.
 1192:                   $designs->{'font'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1193:                   '</span></td></tr>';
 1194:     unless ($role eq 'login') { 
 1195:         $datatable .= '<tr'.$css_class.'>'.
 1196:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1197:         if (!$is_custom->{'fontmenu'}) {
 1198:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1199:         } else {
 1200:             $datatable .= '<td>&nbsp;</td>';
 1201:         }
 1202:         $fontlink = &color_pick($phase,$role,'fontmenu',$choices->{'fontmenu'},$designs->{'fontmenu'});
 1203:         $datatable .= '<td><span class="LC_nobreak">'.
 1204:                       '<input type="text" size="10" name="'.$role.'_fontmenu"'.
 1205:                       ' value="'.$designs->{'fontmenu'}.'" />&nbsp;'.$fontlink.
 1206:                       '&nbsp;<span id="css_'.$role.'_fontmenu" style="background-color: '.
 1207:                       $designs->{'fontmenu'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1208:                       '</span></td></tr>';
 1209:     }
 1210:     my $switchserver = &check_switchserver($dom,$confname);
 1211:     foreach my $img (@{$images}) {
 1212: 	$itemcount ++;
 1213:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1214:         $datatable .= '<tr'.$css_class.'>'.
 1215:                       '<td>'.$choices->{$img};
 1216:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1217:         if ($role eq 'login') {
 1218:             if ($img eq 'login') {
 1219:                 $login_hdr_pick =
 1220:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1221:                 $logincolors =
 1222:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1223:                                             $designs);
 1224:             } elsif ($img ne 'domlogo') {
 1225:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1226:             }
 1227:         }
 1228:         $datatable .= '</td>';
 1229:         if ($designs->{$img} ne '') {
 1230:             $imgfile = $designs->{$img};
 1231: 	    $img_import = ($imgfile =~ m{^/adm/});
 1232:         } else {
 1233:             $imgfile = $defaults->{$img};
 1234:         }
 1235:         if ($imgfile) {
 1236:             my ($showfile,$fullsize);
 1237:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1238:                 my $urldir = $1;
 1239:                 my $filename = $2;
 1240:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1241:                 if (@info) {
 1242:                     my $thumbfile = 'tn-'.$filename;
 1243:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1244:                     if (@thumb) {
 1245:                         $showfile = $urldir.'/'.$thumbfile;
 1246:                     } else {
 1247:                         $showfile = $imgfile;
 1248:                     }
 1249:                 } else {
 1250:                     $showfile = '';
 1251:                 }
 1252:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1253:                 $showfile = $imgfile;
 1254:                 my $imgdir = $1;
 1255:                 my $filename = $2;
 1256:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1257:                     $showfile = "/$imgdir/tn-".$filename;
 1258:                 } else {
 1259:                     my $input = $londocroot.$imgfile;
 1260:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1261:                     if (!-e $output) {
 1262:                         my ($width,$height) = &thumb_dimensions();
 1263:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1264:                         if ($fullwidth ne '' && $fullheight ne '') {
 1265:                             if ($fullwidth > $width && $fullheight > $height) { 
 1266:                                 my $size = $width.'x'.$height;
 1267:                                 system("convert -sample $size $input $output");
 1268:                                 $showfile = "/$imgdir/tn-".$filename;
 1269:                             }
 1270:                         }
 1271:                     }
 1272:                 }
 1273:             }
 1274:             if ($showfile) {
 1275:                 if ($showfile =~ m{^/(adm|res)/}) {
 1276:                     if ($showfile =~ m{^/res/}) {
 1277:                         my $local_showfile =
 1278:                             &Apache::lonnet::filelocation('',$showfile);
 1279:                         &Apache::lonnet::repcopy($local_showfile);
 1280:                     }
 1281:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1282:                 }
 1283:                 if ($imgfile) {
 1284:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1285:                         if ($imgfile =~ m{^/res/}) {
 1286:                             my $local_imgfile =
 1287:                                 &Apache::lonnet::filelocation('',$imgfile);
 1288:                             &Apache::lonnet::repcopy($local_imgfile);
 1289:                         }
 1290:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1291:                     } else {
 1292:                         $fullsize = $imgfile;
 1293:                     }
 1294:                 }
 1295:                 $datatable .= '<td>';
 1296:                 if ($img eq 'login') {
 1297:                     $datatable .= $login_hdr_pick;
 1298:                 } 
 1299:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1300:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1301:             } else {
 1302:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1303:                               &mt('Upload:');
 1304:             }
 1305:         } else {
 1306:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1307:                           &mt('Upload:');
 1308:         }
 1309:         if ($switchserver) {
 1310:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1311:         } else {
 1312:             if ($img ne 'login') { # suppress file selection for Log-in header
 1313:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1314:             }
 1315:         }
 1316:         $datatable .= '</td></tr>';
 1317:     }
 1318:     $itemcount ++;
 1319:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1320:     $datatable .= '<tr'.$css_class.'>'.
 1321:                   '<td>'.$choices->{'bgs'}.'</td>';
 1322:     my $bgs_def;
 1323:     foreach my $item (@{$bgs}) {
 1324:         if (!$is_custom->{$item}) {
 1325:             $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>';
 1326:         }
 1327:     }
 1328:     if ($bgs_def) {
 1329:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1330:     } else {
 1331:         $datatable .= '<td>&nbsp;</td>';
 1332:     }
 1333:     $datatable .= '<td class="LC_right_item">'.
 1334:                   '<table border="0"><tr>';
 1335:     foreach my $item (@{$bgs}) {
 1336:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'bgs'}{$item});
 1337:         $datatable .= '<td align="center">'.$link;
 1338:         if ($designs->{'bgs'}{$item}) {
 1339:             $datatable .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1340:         }
 1341:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'bgs'}{$item}.
 1342:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1343:     }
 1344:     $datatable .= '</tr></table></td></tr>';
 1345:     $itemcount ++;
 1346:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1347:     $datatable .= '<tr'.$css_class.'>'.
 1348:                   '<td>'.$choices->{'links'}.'</td>';
 1349:     my $links_def;
 1350:     foreach my $item (@{$links}) {
 1351:         if (!$is_custom->{$item}) {
 1352:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1353:         }
 1354:     }
 1355:     if ($links_def) {
 1356:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1357:     } else {
 1358:         $datatable .= '<td>&nbsp;</td>';
 1359:     }
 1360:     $datatable .= '<td class="LC_right_item">'.
 1361:                   '<table border="0"><tr>';
 1362:     foreach my $item (@{$links}) {
 1363:         $datatable .= '<td align="center">'."\n".
 1364:                       &color_pick($phase,$role,$item,$choices->{$item},
 1365:                                   $designs->{'links'}{$item});
 1366:         if ($designs->{'links'}{$item}) {
 1367:             $datatable.='&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'links'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1368:         }
 1369:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'links'}{$item}.
 1370:                       '" /></td>';
 1371:     }
 1372:     $$rowtotal += $itemcount;
 1373:     return $datatable;
 1374: }
 1375: 
 1376: sub logo_display_options {
 1377:     my ($img,$defaults,$designs) = @_;
 1378:     my $checkedon;
 1379:     if (ref($defaults) eq 'HASH') {
 1380:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1381:             if ($defaults->{'showlogo'}{$img}) {
 1382:                 $checkedon = 'checked="checked" ';     
 1383:             }
 1384:         } 
 1385:     }
 1386:     if (ref($designs) eq 'HASH') {
 1387:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1388:             if (defined($designs->{'showlogo'}{$img})) {
 1389:                 if ($designs->{'showlogo'}{$img} == 0) {
 1390:                     $checkedon = '';
 1391:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1392:                     $checkedon = 'checked="checked" ';
 1393:                 }
 1394:             }
 1395:         }
 1396:     }
 1397:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1398:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1399:            &mt('show').'</label>'."\n";
 1400: }
 1401: 
 1402: sub login_header_options  {
 1403:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1404:     my $output = '';
 1405:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1406:         $output .= &mt('Text default(s):').'<br />';
 1407:         if (!$is_custom->{'textcol'}) {
 1408:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1409:                        '&nbsp;&nbsp;&nbsp;';
 1410:         }
 1411:         if (!$is_custom->{'bgcol'}) {
 1412:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1413:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1414:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1415:         }
 1416:         $output .= '<br />';
 1417:     }
 1418:     $output .='<br />';
 1419:     return $output;
 1420: }
 1421: 
 1422: sub login_text_colors {
 1423:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1424:     my $color_menu = '<table border="0"><tr>';
 1425:     foreach my $item (@{$logintext}) {
 1426:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1427:         $color_menu .= '<td align="center">'.$link;
 1428:         if ($designs->{'logintext'}{$item}) {
 1429:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1430:         }
 1431:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1432:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1433:                        '<td>&nbsp;</td>';
 1434:     }
 1435:     $color_menu .= '</tr></table><br />';
 1436:     return $color_menu;
 1437: }
 1438: 
 1439: sub image_changes {
 1440:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1441:     my $output;
 1442:     if ($img eq 'login') {
 1443:             # suppress image for Log-in header
 1444:     } elsif (!$is_custom) {
 1445:         if ($img ne 'domlogo') {
 1446:             $output .= &mt('Default image:').'<br />';
 1447:         } else {
 1448:             $output .= &mt('Default in use:').'<br />';
 1449:         }
 1450:     }
 1451:     if ($img eq 'login') { # suppress image for Log-in header
 1452:         $output .= '<td>'.$logincolors;
 1453:     } else {
 1454:         if ($img_import) {
 1455:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1456:         }
 1457:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1458:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1459:         if ($is_custom) {
 1460:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1461:                        '<input type="checkbox" name="'.
 1462:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1463:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1464:         } else {
 1465:             $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1466:         }
 1467:     }
 1468:     return $output;
 1469: }
 1470: 
 1471: sub color_pick {
 1472:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1473:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1474:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1475:                ');">'.$desc.'</a>';
 1476:     return $link;
 1477: }
 1478: 
 1479: sub print_quotas {
 1480:     my ($dom,$settings,$rowtotal,$action) = @_;
 1481:     my $context;
 1482:     if ($action eq 'quotas') {
 1483:         $context = 'tools';
 1484:     } else {
 1485:         $context = $action;
 1486:     }
 1487:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 1488:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1489:     my $typecount = 0;
 1490:     my ($css_class,%titles);
 1491:     if ($context eq 'requestcourses') {
 1492:         @usertools = ('official','unofficial','community');
 1493:         @options =('norequest','approval','validate','autolimit');
 1494:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1495:         %titles = &courserequest_titles();
 1496:     } elsif ($context eq 'requestauthor') {
 1497:         @usertools = ('author');
 1498:         @options = ('norequest','approval','automatic');
 1499:         %titles = &authorrequest_titles(); 
 1500:     } else {
 1501:         @usertools = ('aboutme','blog','webdav','portfolio');
 1502:         %titles = &tool_titles();
 1503:     }
 1504:     if (ref($types) eq 'ARRAY') {
 1505:         foreach my $type (@{$types}) {
 1506:             my $currdefquota;
 1507:             unless (($context eq 'requestcourses') ||
 1508:                     ($context eq 'requestauthor')) {
 1509:                 if (ref($settings) eq 'HASH') {
 1510:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1511:                         $currdefquota = $settings->{defaultquota}->{$type}; 
 1512:                     } else {
 1513:                         $currdefquota = $settings->{$type};
 1514:                     }
 1515:                 }
 1516:             }
 1517:             if (defined($usertypes->{$type})) {
 1518:                 $typecount ++;
 1519:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1520:                 $datatable .= '<tr'.$css_class.'>'.
 1521:                               '<td>'.$usertypes->{$type}.'</td>'.
 1522:                               '<td class="LC_left_item">';
 1523:                 if ($context eq 'requestcourses') {
 1524:                     $datatable .= '<table><tr>';
 1525:                 }
 1526:                 my %cell;  
 1527:                 foreach my $item (@usertools) {
 1528:                     if ($context eq 'requestcourses') {
 1529:                         my ($curroption,$currlimit);
 1530:                         if (ref($settings) eq 'HASH') {
 1531:                             if (ref($settings->{$item}) eq 'HASH') {
 1532:                                 $curroption = $settings->{$item}->{$type};
 1533:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1534:                                     $currlimit = $1; 
 1535:                                 }
 1536:                             }
 1537:                         }
 1538:                         if (!$curroption) {
 1539:                             $curroption = 'norequest';
 1540:                         }
 1541:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1542:                         foreach my $option (@options) {
 1543:                             my $val = $option;
 1544:                             if ($option eq 'norequest') {
 1545:                                 $val = 0;  
 1546:                             }
 1547:                             if ($option eq 'validate') {
 1548:                                 my $canvalidate = 0;
 1549:                                 if (ref($validations{$item}) eq 'HASH') { 
 1550:                                     if ($validations{$item}{$type}) {
 1551:                                         $canvalidate = 1;
 1552:                                     }
 1553:                                 }
 1554:                                 next if (!$canvalidate);
 1555:                             }
 1556:                             my $checked = '';
 1557:                             if ($option eq $curroption) {
 1558:                                 $checked = ' checked="checked"';
 1559:                             } elsif ($option eq 'autolimit') {
 1560:                                 if ($curroption =~ /^autolimit/) {
 1561:                                     $checked = ' checked="checked"';
 1562:                                 }                       
 1563:                             } 
 1564:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1565:                                   '<input type="radio" name="crsreq_'.$item.
 1566:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1567:                                   $titles{$option}.'</label>';
 1568:                             if ($option eq 'autolimit') {
 1569:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1570:                                                 $item.'_limit_'.$type.'" size="1" '.
 1571:                                                 'value="'.$currlimit.'" />';
 1572:                             }
 1573:                             $cell{$item} .= '</span> ';
 1574:                             if ($option eq 'autolimit') {
 1575:                                 $cell{$item} .= $titles{'unlimited'};
 1576:                             }
 1577:                         }
 1578:                     } elsif ($context eq 'requestauthor') {
 1579:                         my $curroption;
 1580:                         if (ref($settings) eq 'HASH') {
 1581:                             $curroption = $settings->{$type};
 1582:                         }
 1583:                         if (!$curroption) {
 1584:                             $curroption = 'norequest';
 1585:                         }
 1586:                         foreach my $option (@options) {
 1587:                             my $val = $option;
 1588:                             if ($option eq 'norequest') {
 1589:                                 $val = 0;
 1590:                             }
 1591:                             my $checked = '';
 1592:                             if ($option eq $curroption) {
 1593:                                 $checked = ' checked="checked"';
 1594:                             }
 1595:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1596:                                   '<input type="radio" name="authorreq_'.$type.
 1597:                                   '" value="'.$val.'"'.$checked.' />'.
 1598:                                   $titles{$option}.'</label></span>&nbsp; ';
 1599:                         }
 1600:                     } else {
 1601:                         my $checked = 'checked="checked" ';
 1602:                         if (ref($settings) eq 'HASH') {
 1603:                             if (ref($settings->{$item}) eq 'HASH') {
 1604:                                 if ($settings->{$item}->{$type} == 0) {
 1605:                                     $checked = '';
 1606:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1607:                                     $checked =  'checked="checked" ';
 1608:                                 }
 1609:                             }
 1610:                         }
 1611:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1612:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1613:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1614:                                       '</label></span>&nbsp; ';
 1615:                     }
 1616:                 }
 1617:                 if ($context eq 'requestcourses') {
 1618:                     $datatable .= '</tr><tr>';
 1619:                     foreach my $item (@usertools) {
 1620:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1621:                     }
 1622:                     $datatable .= '</tr></table>';
 1623:                 }
 1624:                 $datatable .= '</td>';
 1625:                 unless (($context eq 'requestcourses') ||
 1626:                         ($context eq 'requestauthor')) {
 1627:                     $datatable .= 
 1628:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1629:                               '<input type="text" name="quota_'.$type.
 1630:                               '" value="'.$currdefquota.
 1631:                               '" size="5" /> Mb</span></td>';
 1632:                 }
 1633:                 $datatable .= '</tr>';
 1634:             }
 1635:         }
 1636:     }
 1637:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1638:         $defaultquota = '20';
 1639:         if (ref($settings) eq 'HASH') {
 1640:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1641:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1642:             } elsif (defined($settings->{'default'})) {
 1643:                 $defaultquota = $settings->{'default'};
 1644:             }
 1645:         }
 1646:     }
 1647:     $typecount ++;
 1648:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1649:     $datatable .= '<tr'.$css_class.'>'.
 1650:                   '<td>'.$othertitle.'</td>'.
 1651:                   '<td class="LC_left_item">';
 1652:     if ($context eq 'requestcourses') {
 1653:         $datatable .= '<table><tr>';
 1654:     }
 1655:     my %defcell;
 1656:     foreach my $item (@usertools) {
 1657:         if ($context eq 'requestcourses') {
 1658:             my ($curroption,$currlimit);
 1659:             if (ref($settings) eq 'HASH') {
 1660:                 if (ref($settings->{$item}) eq 'HASH') {
 1661:                     $curroption = $settings->{$item}->{'default'};
 1662:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1663:                         $currlimit = $1;
 1664:                     }
 1665:                 }
 1666:             }
 1667:             if (!$curroption) {
 1668:                 $curroption = 'norequest';
 1669:             }
 1670:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1671:             foreach my $option (@options) {
 1672:                 my $val = $option;
 1673:                 if ($option eq 'norequest') {
 1674:                     $val = 0;
 1675:                 }
 1676:                 if ($option eq 'validate') {
 1677:                     my $canvalidate = 0;
 1678:                     if (ref($validations{$item}) eq 'HASH') {
 1679:                         if ($validations{$item}{'default'}) {
 1680:                             $canvalidate = 1;
 1681:                         }
 1682:                     }
 1683:                     next if (!$canvalidate);
 1684:                 }
 1685:                 my $checked = '';
 1686:                 if ($option eq $curroption) {
 1687:                     $checked = ' checked="checked"';
 1688:                 } elsif ($option eq 'autolimit') {
 1689:                     if ($curroption =~ /^autolimit/) {
 1690:                         $checked = ' checked="checked"';
 1691:                     }
 1692:                 }
 1693:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1694:                                   '<input type="radio" name="crsreq_'.$item.
 1695:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1696:                                   $titles{$option}.'</label>';
 1697:                 if ($option eq 'autolimit') {
 1698:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1699:                                        $item.'_limit_default" size="1" '.
 1700:                                        'value="'.$currlimit.'" />';
 1701:                 }
 1702:                 $defcell{$item} .= '</span> ';
 1703:                 if ($option eq 'autolimit') {
 1704:                     $defcell{$item} .= $titles{'unlimited'};
 1705:                 }
 1706:             }
 1707:         } elsif ($context eq 'requestauthor') {
 1708:             my $curroption;
 1709:             if (ref($settings) eq 'HASH') {
 1710:                 if (ref($settings->{'requestauthor'}) eq 'HASH') {
 1711:                     $curroption = $settings->{'requestauthor'};
 1712:                 }
 1713:             }
 1714:             if (!$curroption) {
 1715:                 $curroption = 'norequest';
 1716:             }
 1717:             foreach my $option (@options) {
 1718:                 my $val = $option;
 1719:                 if ($option eq 'norequest') {
 1720:                     $val = 0;
 1721:                 }
 1722:                 my $checked = '';
 1723:                 if ($option eq $curroption) {
 1724:                     $checked = ' checked="checked"';
 1725:                 }
 1726:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1727:                               '<input type="radio" name="authorreq_default"'.
 1728:                               ' value="'.$val.'"'.$checked.' />'.
 1729:                               $titles{$option}.'</label></span>&nbsp; ';
 1730:             }
 1731:         } else {
 1732:             my $checked = 'checked="checked" ';
 1733:             if (ref($settings) eq 'HASH') {
 1734:                 if (ref($settings->{$item}) eq 'HASH') {
 1735:                     if ($settings->{$item}->{'default'} == 0) {
 1736:                         $checked = '';
 1737:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1738:                         $checked = 'checked="checked" ';
 1739:                     }
 1740:                 }
 1741:             }
 1742:             $datatable .= '<span class="LC_nobreak"><label>'.
 1743:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1744:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1745:                           '</label></span>&nbsp; ';
 1746:         }
 1747:     }
 1748:     if ($context eq 'requestcourses') {
 1749:         $datatable .= '</tr><tr>';
 1750:         foreach my $item (@usertools) {
 1751:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1752:         }
 1753:         $datatable .= '</tr></table>';
 1754:     }
 1755:     $datatable .= '</td>';
 1756:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1757:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1758:                       '<input type="text" name="defaultquota" value="'.
 1759:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1760:     }
 1761:     $datatable .= '</tr>';
 1762:     $typecount ++;
 1763:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1764:     $datatable .= '<tr'.$css_class.'>'.
 1765:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1766:     if ($context eq 'requestcourses') {
 1767:         $datatable .= &mt('(overrides affiliation, if set)').
 1768:                       '</td>'.
 1769:                       '<td class="LC_left_item">'.
 1770:                       '<table><tr>';
 1771:     } else {
 1772:         $datatable .= &mt('(overrides affiliation, if checked)').
 1773:                       '</td>'.
 1774:                       '<td class="LC_left_item" colspan="2">'.
 1775:                       '<br />';
 1776:     }
 1777:     my %advcell;
 1778:     foreach my $item (@usertools) {
 1779:         if ($context eq 'requestcourses') {
 1780:             my ($curroption,$currlimit);
 1781:             if (ref($settings) eq 'HASH') {
 1782:                 if (ref($settings->{$item}) eq 'HASH') {
 1783:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1784:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1785:                         $currlimit = $1;
 1786:                     }
 1787:                 }
 1788:             }
 1789:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1790:             my $checked = '';
 1791:             if ($curroption eq '') {
 1792:                 $checked = ' checked="checked"';
 1793:             }
 1794:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1795:                                '<input type="radio" name="crsreq_'.$item.
 1796:                                '__LC_adv" value=""'.$checked.' />'.
 1797:                                &mt('No override set').'</label></span>&nbsp; ';
 1798:             foreach my $option (@options) {
 1799:                 my $val = $option;
 1800:                 if ($option eq 'norequest') {
 1801:                     $val = 0;
 1802:                 }
 1803:                 if ($option eq 'validate') {
 1804:                     my $canvalidate = 0;
 1805:                     if (ref($validations{$item}) eq 'HASH') {
 1806:                         if ($validations{$item}{'_LC_adv'}) {
 1807:                             $canvalidate = 1;
 1808:                         }
 1809:                     }
 1810:                     next if (!$canvalidate);
 1811:                 }
 1812:                 my $checked = '';
 1813:                 if ($val eq $curroption) {
 1814:                     $checked = ' checked="checked"';
 1815:                 } elsif ($option eq 'autolimit') {
 1816:                     if ($curroption =~ /^autolimit/) {
 1817:                         $checked = ' checked="checked"';
 1818:                     }
 1819:                 }
 1820:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1821:                                   '<input type="radio" name="crsreq_'.$item.
 1822:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1823:                                   $titles{$option}.'</label>';
 1824:                 if ($option eq 'autolimit') {
 1825:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1826:                                        $item.'_limit__LC_adv" size="1" '.
 1827:                                        'value="'.$currlimit.'" />';
 1828:                 }
 1829:                 $advcell{$item} .= '</span> ';
 1830:                 if ($option eq 'autolimit') {
 1831:                     $advcell{$item} .= $titles{'unlimited'};
 1832:                 }
 1833:             }
 1834:         } elsif ($context eq 'requestauthor') {
 1835:             my $curroption;
 1836:             if (ref($settings) eq 'HASH') {
 1837:                 $curroption = $settings->{'_LC_adv'};
 1838:             }
 1839:             my $checked = '';
 1840:             if ($curroption eq '') {
 1841:                 $checked = ' checked="checked"';
 1842:             }
 1843:             $datatable .= '<span class="LC_nobreak"><label>'.
 1844:                           '<input type="radio" name="authorreq__LC_adv"'.
 1845:                           ' value=""'.$checked.' />'.
 1846:                           &mt('No override set').'</label></span>&nbsp; ';
 1847:             foreach my $option (@options) {
 1848:                 my $val = $option;
 1849:                 if ($option eq 'norequest') {
 1850:                     $val = 0;
 1851:                 }
 1852:                 my $checked = '';
 1853:                 if ($val eq $curroption) {
 1854:                     $checked = ' checked="checked"';
 1855:                 }
 1856:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1857:                               '<input type="radio" name="crsreq_'.$item.
 1858:                               '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1859:                               $titles{$option}.'</label></span>&nbsp; ';
 1860:             }
 1861:         } else {
 1862:             my $checked = 'checked="checked" ';
 1863:             if (ref($settings) eq 'HASH') {
 1864:                 if (ref($settings->{$item}) eq 'HASH') {
 1865:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1866:                         $checked = '';
 1867:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1868:                         $checked = 'checked="checked" ';
 1869:                     }
 1870:                 }
 1871:             }
 1872:             $datatable .= '<span class="LC_nobreak"><label>'.
 1873:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1874:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1875:                           '</label></span>&nbsp; ';
 1876:         }
 1877:     }
 1878:     if ($context eq 'requestcourses') {
 1879:         $datatable .= '</tr><tr>';
 1880:         foreach my $item (@usertools) {
 1881:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1882:         }
 1883:         $datatable .= '</tr></table>';
 1884:     }
 1885:     $datatable .= '</td></tr>';
 1886:     $$rowtotal += $typecount;
 1887:     return $datatable;
 1888: }
 1889: 
 1890: sub print_requestmail {
 1891:     my ($dom,$action,$settings,$rowtotal) = @_;
 1892:     my ($now,$datatable,%dompersonnel,@domcoord,@currapproval,$rows);
 1893:     $now = time;
 1894:     $rows = 0;
 1895:     %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 1896:     foreach my $server (keys(%dompersonnel)) {
 1897:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 1898:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 1899:             if (!grep(/^$uname:$udom$/,@domcoord)) {
 1900:                 push(@domcoord,$uname.':'.$udom);
 1901:             }
 1902:         }
 1903:     }
 1904:     if (ref($settings) eq 'HASH') {
 1905:         if (ref($settings->{'notify'}) eq 'HASH') {
 1906:             if ($settings->{'notify'}{'approval'} ne '') {
 1907:                @currapproval = split(',',$settings->{'notify'}{'approval'});
 1908:             }
 1909:         }
 1910:     }
 1911:     if (@currapproval) {
 1912:         foreach my $dc (@currapproval) {
 1913:             unless (grep(/^\Q$dc\E$/,@domcoord)) {
 1914:                 push(@domcoord,$dc);
 1915:             }
 1916:         }
 1917:     }
 1918:     @domcoord = sort(@domcoord);
 1919:     my $numinrow = 4;
 1920:     my $numdc = @domcoord;
 1921:     my $css_class = 'class="LC_odd_row"';
 1922:     my $text;
 1923:     if ($action eq 'requestcourses') {
 1924:         $text = &mt('Receive notification of course requests requiring approval');
 1925:     } else {
 1926:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1927:     }
 1928:     $datatable = '<tr '.$css_class.'>'.
 1929:                  ' <td>'.$text.'</td>'.
 1930:                  ' <td class="LC_left_item">';
 1931:     if (@domcoord > 0) {
 1932:         $datatable .= '<table>';
 1933:         for (my $i=0; $i<$numdc; $i++) {
 1934:             my $rem = $i%($numinrow);
 1935:             if ($rem == 0) {
 1936:                 if ($i > 0) {
 1937:                     $datatable .= '</tr>';
 1938:                 }
 1939:                 $datatable .= '<tr>';
 1940:                 $rows ++;
 1941:             }
 1942:             my $check = ' ';
 1943:             if (grep(/^\Q$domcoord[$i]\E$/,@currapproval)) {
 1944:                 $check = ' checked="checked" ';
 1945:             }
 1946:             my ($uname,$udom) = split(':',$domcoord[$i]);
 1947:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
 1948:             if ($i == $numdc-1) {
 1949:                 my $colsleft = $numinrow-$rem;
 1950:                 if ($colsleft > 1) {
 1951:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 1952:                 } else {
 1953:                     $datatable .= '<td class="LC_left_item">';
 1954:                 }
 1955:             } else {
 1956:                 $datatable .= '<td class="LC_left_item">';
 1957:             }
 1958:             $datatable .= '<span class="LC_nobreak"><label>'.
 1959:                           '<input type="checkbox" name="reqapprovalnotify" '.
 1960:                           'value="'.$domcoord[$i].'"'.$check.'/>'.
 1961:                           $fullname.'</label></span></td>';
 1962:         }
 1963:         $datatable .= '</tr></table>';
 1964:     } else {
 1965:         $datatable .= &mt('There are no active Domain Coordinators');
 1966:         $rows ++;
 1967:     }
 1968:     $datatable .='</td></tr>';
 1969:     $$rowtotal += $rows;
 1970:     return $datatable;
 1971: }
 1972: 
 1973: sub print_autoenroll {
 1974:     my ($dom,$settings,$rowtotal) = @_;
 1975:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1976:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1977:     if (ref($settings) eq 'HASH') {
 1978:         if (exists($settings->{'run'})) {
 1979:             if ($settings->{'run'} eq '0') {
 1980:                 $runoff = ' checked="checked" ';
 1981:                 $runon = ' ';
 1982:             } else {
 1983:                 $runon = ' checked="checked" ';
 1984:                 $runoff = ' ';
 1985:             }
 1986:         } else {
 1987:             if ($autorun) {
 1988:                 $runon = ' checked="checked" ';
 1989:                 $runoff = ' ';
 1990:             } else {
 1991:                 $runoff = ' checked="checked" ';
 1992:                 $runon = ' ';
 1993:             }
 1994:         }
 1995:         if (exists($settings->{'co-owners'})) {
 1996:             if ($settings->{'co-owners'} eq '0') {
 1997:                 $coownersoff = ' checked="checked" ';
 1998:                 $coownerson = ' ';
 1999:             } else {
 2000:                 $coownerson = ' checked="checked" ';
 2001:                 $coownersoff = ' ';
 2002:             }
 2003:         } else {
 2004:             $coownersoff = ' checked="checked" ';
 2005:             $coownerson = ' ';
 2006:         }
 2007:         if (exists($settings->{'sender_domain'})) {
 2008:             $defdom = $settings->{'sender_domain'};
 2009:         }
 2010:     } else {
 2011:         if ($autorun) {
 2012:             $runon = ' checked="checked" ';
 2013:             $runoff = ' ';
 2014:         } else {
 2015:             $runoff = ' checked="checked" ';
 2016:             $runon = ' ';
 2017:         }
 2018:     }
 2019:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2020:     my $notif_sender;
 2021:     if (ref($settings) eq 'HASH') {
 2022:         $notif_sender = $settings->{'sender_uname'};
 2023:     }
 2024:     my $datatable='<tr class="LC_odd_row">'.
 2025:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2026:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2027:                   '<input type="radio" name="autoenroll_run"'.
 2028:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2029:                   '<label><input type="radio" name="autoenroll_run"'.
 2030:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2031:                   '</tr><tr>'.
 2032:                   '<td>'.&mt('Notification messages - sender').
 2033:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2034:                   &mt('username').':&nbsp;'.
 2035:                   '<input type="text" name="sender_uname" value="'.
 2036:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2037:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2038:                   '<tr class="LC_odd_row">'.
 2039:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2040:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2041:                   '<input type="radio" name="autoassign_coowners"'.
 2042:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2043:                   '<label><input type="radio" name="autoassign_coowners"'.
 2044:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2045:                   '</tr>';
 2046:     $$rowtotal += 3;
 2047:     return $datatable;
 2048: }
 2049: 
 2050: sub print_autoupdate {
 2051:     my ($position,$dom,$settings,$rowtotal) = @_;
 2052:     my $datatable;
 2053:     if ($position eq 'top') {
 2054:         my $updateon = ' ';
 2055:         my $updateoff = ' checked="checked" ';
 2056:         my $classlistson = ' ';
 2057:         my $classlistsoff = ' checked="checked" ';
 2058:         if (ref($settings) eq 'HASH') {
 2059:             if ($settings->{'run'} eq '1') {
 2060:                 $updateon = $updateoff;
 2061:                 $updateoff = ' ';
 2062:             }
 2063:             if ($settings->{'classlists'} eq '1') {
 2064:                 $classlistson = $classlistsoff;
 2065:                 $classlistsoff = ' ';
 2066:             }
 2067:         }
 2068:         my %title = (
 2069:                    run => 'Auto-update active?',
 2070:                    classlists => 'Update information in classlists?',
 2071:                     );
 2072:         $datatable = '<tr class="LC_odd_row">'. 
 2073:                   '<td>'.&mt($title{'run'}).'</td>'.
 2074:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2075:                   '<input type="radio" name="autoupdate_run"'.
 2076:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2077:                   '<label><input type="radio" name="autoupdate_run"'.
 2078:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2079:                   '</tr><tr>'.
 2080:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2081:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2082:                   '<label><input type="radio" name="classlists"'.
 2083:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2084:                   '<label><input type="radio" name="classlists"'.
 2085:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2086:                   '</tr>';
 2087:         $$rowtotal += 2;
 2088:     } elsif ($position eq 'middle') {
 2089:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2090:         my $numinrow = 3;
 2091:         my $locknamesettings;
 2092:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2093:                                      $dom,$numinrow,$othertitle,
 2094:                                     'lockablenames');
 2095:         $$rowtotal ++;
 2096:     } else {
 2097:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2098:         my @fields = ('lastname','firstname','middlename','generation',
 2099:                       'permanentemail','id');
 2100:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2101:         my $numrows = 0;
 2102:         if (ref($types) eq 'ARRAY') {
 2103:             if (@{$types} > 0) {
 2104:                 $datatable = 
 2105:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2106:                                          \@fields,$types,\$numrows);
 2107:                     $$rowtotal += @{$types}; 
 2108:             }
 2109:         }
 2110:         $datatable .= 
 2111:             &usertype_update_row($settings,{'default' => $othertitle},
 2112:                                  \%fieldtitles,\@fields,['default'],
 2113:                                  \$numrows);
 2114:         $$rowtotal ++;     
 2115:     }
 2116:     return $datatable;
 2117: }
 2118: 
 2119: sub print_autocreate {
 2120:     my ($dom,$settings,$rowtotal) = @_;
 2121:     my (%createon,%createoff);
 2122:     my $curr_dc;
 2123:     my @types = ('xml','req');
 2124:     if (ref($settings) eq 'HASH') {
 2125:         foreach my $item (@types) {
 2126:             $createoff{$item} = ' checked="checked" ';
 2127:             $createon{$item} = ' ';
 2128:             if (exists($settings->{$item})) {
 2129:                 if ($settings->{$item}) {
 2130:                     $createon{$item} = ' checked="checked" ';
 2131:                     $createoff{$item} = ' ';
 2132:                 }
 2133:             }
 2134:         }
 2135:         $curr_dc = $settings->{'xmldc'};
 2136:     } else {
 2137:         foreach my $item (@types) {
 2138:             $createoff{$item} = ' checked="checked" ';
 2139:             $createon{$item} = ' ';
 2140:         }
 2141:     }
 2142:     $$rowtotal += 2;
 2143:     my $datatable='<tr class="LC_odd_row">'.
 2144:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2145:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2146:                   '<input type="radio" name="autocreate_xml"'.
 2147:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2148:                   '<label><input type="radio" name="autocreate_xml"'.
 2149:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2150:                   '</td></tr><tr>'.
 2151:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2152:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2153:                   '<input type="radio" name="autocreate_req"'.
 2154:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2155:                   '<label><input type="radio" name="autocreate_req"'.
 2156:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2157:     my ($numdc,$dctable) = &active_dc_picker($dom,$curr_dc);
 2158:     if ($numdc > 1) {
 2159:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2160:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2161:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2162:         $$rowtotal ++ ;
 2163:     } else {
 2164:         $datatable .= $dctable.'</td></tr>';
 2165:     }
 2166:     return $datatable;
 2167: }
 2168: 
 2169: sub print_directorysrch {
 2170:     my ($dom,$settings,$rowtotal) = @_;
 2171:     my $srchon = ' ';
 2172:     my $srchoff = ' checked="checked" ';
 2173:     my ($exacton,$containson,$beginson);
 2174:     my $localon = ' ';
 2175:     my $localoff = ' checked="checked" ';
 2176:     if (ref($settings) eq 'HASH') {
 2177:         if ($settings->{'available'} eq '1') {
 2178:             $srchon = $srchoff;
 2179:             $srchoff = ' ';
 2180:         }
 2181:         if ($settings->{'localonly'} eq '1') {
 2182:             $localon = $localoff;
 2183:             $localoff = ' ';
 2184:         }
 2185:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2186:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2187:                 if ($type eq 'exact') {
 2188:                     $exacton = ' checked="checked" ';
 2189:                 } elsif ($type eq 'contains') {
 2190:                     $containson = ' checked="checked" ';
 2191:                 } elsif ($type eq 'begins') {
 2192:                     $beginson = ' checked="checked" ';
 2193:                 }
 2194:             }
 2195:         } else {
 2196:             if ($settings->{'searchtypes'} eq 'exact') {
 2197:                 $exacton = ' checked="checked" ';
 2198:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2199:                 $containson = ' checked="checked" ';
 2200:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2201:                 $exacton = ' checked="checked" ';
 2202:                 $containson = ' checked="checked" ';
 2203:             }
 2204:         }
 2205:     }
 2206:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2207:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2208: 
 2209:     my $numinrow = 4;
 2210:     my $cansrchrow = 0;
 2211:     my $datatable='<tr class="LC_odd_row">'.
 2212:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2213:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2214:                   '<input type="radio" name="dirsrch_available"'.
 2215:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2216:                   '<label><input type="radio" name="dirsrch_available"'.
 2217:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2218:                   '</tr><tr>'.
 2219:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2220:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2221:                   '<input type="radio" name="dirsrch_localonly"'.
 2222:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2223:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2224:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2225:                   '</tr>';
 2226:     $$rowtotal += 2;
 2227:     if (ref($usertypes) eq 'HASH') {
 2228:         if (keys(%{$usertypes}) > 0) {
 2229:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2230:                                          $numinrow,$othertitle,'cansearch');
 2231:             $cansrchrow = 1;
 2232:         }
 2233:     }
 2234:     if ($cansrchrow) {
 2235:         $$rowtotal ++;
 2236:         $datatable .= '<tr>';
 2237:     } else {
 2238:         $datatable .= '<tr class="LC_odd_row">';
 2239:     }
 2240:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2241:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2242:     foreach my $title (@{$titleorder}) {
 2243:         if (defined($searchtitles->{$title})) {
 2244:             my $check = ' ';
 2245:             if (ref($settings) eq 'HASH') {
 2246:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2247:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2248:                         $check = ' checked="checked" ';
 2249:                     }
 2250:                 }
 2251:             }
 2252:             $datatable .= '<td class="LC_left_item">'.
 2253:                           '<span class="LC_nobreak"><label>'.
 2254:                           '<input type="checkbox" name="searchby" '.
 2255:                           'value="'.$title.'"'.$check.'/>'.
 2256:                           $searchtitles->{$title}.'</label></span></td>';
 2257:         }
 2258:     }
 2259:     $datatable .= '</tr></table></td></tr>';
 2260:     $$rowtotal ++;
 2261:     if ($cansrchrow) {
 2262:         $datatable .= '<tr class="LC_odd_row">';
 2263:     } else {
 2264:         $datatable .= '<tr>';
 2265:     }
 2266:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2267:                   '<td class="LC_left_item" colspan="2">'.
 2268:                   '<span class="LC_nobreak"><label>'.
 2269:                   '<input type="checkbox" name="searchtypes" '.
 2270:                   $exacton.' value="exact" />'.&mt('Exact match').
 2271:                   '</label>&nbsp;'.
 2272:                   '<label><input type="checkbox" name="searchtypes" '.
 2273:                   $beginson.' value="begins" />'.&mt('Begins with').
 2274:                   '</label>&nbsp;'.
 2275:                   '<label><input type="checkbox" name="searchtypes" '.
 2276:                   $containson.' value="contains" />'.&mt('Contains').
 2277:                   '</label></span></td></tr>';
 2278:     $$rowtotal ++;
 2279:     return $datatable;
 2280: }
 2281: 
 2282: sub print_contacts {
 2283:     my ($dom,$settings,$rowtotal) = @_;
 2284:     my $datatable;
 2285:     my @contacts = ('adminemail','supportemail');
 2286:     my (%checked,%to,%otheremails,%bccemails);
 2287:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2288:                     'requestsmail');
 2289:     foreach my $type (@mailings) {
 2290:         $otheremails{$type} = '';
 2291:     }
 2292:     $bccemails{'helpdeskmail'} = '';
 2293:     if (ref($settings) eq 'HASH') {
 2294:         foreach my $item (@contacts) {
 2295:             if (exists($settings->{$item})) {
 2296:                 $to{$item} = $settings->{$item};
 2297:             }
 2298:         }
 2299:         foreach my $type (@mailings) {
 2300:             if (exists($settings->{$type})) {
 2301:                 if (ref($settings->{$type}) eq 'HASH') {
 2302:                     foreach my $item (@contacts) {
 2303:                         if ($settings->{$type}{$item}) {
 2304:                             $checked{$type}{$item} = ' checked="checked" ';
 2305:                         }
 2306:                     }
 2307:                     $otheremails{$type} = $settings->{$type}{'others'};
 2308:                     if ($type eq 'helpdeskmail') {
 2309:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2310:                     }
 2311:                 }
 2312:             } elsif ($type eq 'lonstatusmail') {
 2313:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2314:             }
 2315:         }
 2316:     } else {
 2317:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2318:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2319:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2320:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2321:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2322:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2323:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2324:     }
 2325:     my ($titles,$short_titles) = &contact_titles();
 2326:     my $rownum = 0;
 2327:     my $css_class;
 2328:     foreach my $item (@contacts) {
 2329:         $rownum ++;
 2330:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2331:         $datatable .= '<tr'.$css_class.'>'. 
 2332:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2333:                   '</span></td><td class="LC_right_item">'.
 2334:                   '<input type="text" name="'.$item.'" value="'.
 2335:                   $to{$item}.'" /></td></tr>';
 2336:     }
 2337:     foreach my $type (@mailings) {
 2338:         $rownum ++;
 2339:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2340:         $datatable .= '<tr'.$css_class.'>'.
 2341:                       '<td><span class="LC_nobreak">'.
 2342:                       $titles->{$type}.': </span></td>'.
 2343:                       '<td class="LC_left_item">'.
 2344:                       '<span class="LC_nobreak">';
 2345:         foreach my $item (@contacts) {
 2346:             $datatable .= '<label>'.
 2347:                           '<input type="checkbox" name="'.$type.'"'.
 2348:                           $checked{$type}{$item}.
 2349:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2350:                           '</label>&nbsp;';
 2351:         }
 2352:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2353:                       '<input type="text" name="'.$type.'_others" '.
 2354:                       'value="'.$otheremails{$type}.'"  />';
 2355:         if ($type eq 'helpdeskmail') {
 2356:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2357:                           '<input type="text" name="'.$type.'_bcc" '.
 2358:                           'value="'.$bccemails{$type}.'"  />';
 2359:         }
 2360:         $datatable .= '</td></tr>'."\n";
 2361:     }
 2362:     $$rowtotal += $rownum;
 2363:     return $datatable;
 2364: }
 2365: 
 2366: sub print_helpsettings {
 2367:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2368:     my ($datatable,$itemcount);
 2369:     $itemcount = 1;
 2370:     my (%choices,%defaultchecked,@toggles);
 2371:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2372:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2373:                                  &mt('LON-CAPA bug tracker'),600,500));
 2374:     %defaultchecked = ('submitbugs' => 'on');
 2375:     @toggles = ('submitbugs',);
 2376: 
 2377:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2378:                                                  \%choices,$itemcount);
 2379:     return $datatable;
 2380: }
 2381: 
 2382: sub radiobutton_prefs {
 2383:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount) = @_;
 2384:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2385:                    (ref($choices) eq 'HASH'));
 2386: 
 2387:     my (%checkedon,%checkedoff,$datatable,$css_class,$cell_colspan);
 2388: 
 2389:     foreach my $item (@{$toggles}) {
 2390:         if ($defaultchecked->{$item} eq 'on') {
 2391:             $checkedon{$item} = ' checked="checked" ';
 2392:             $checkedoff{$item} = ' ';
 2393:         } elsif ($defaultchecked->{$item} eq 'off') {
 2394:             $checkedoff{$item} = ' checked="checked" ';
 2395:             $checkedon{$item} = ' ';
 2396:         }
 2397:     }
 2398:     if (ref($settings) eq 'HASH') {
 2399:         foreach my $item (@{$toggles}) {
 2400:             if ($settings->{$item} eq '1') {
 2401:                 $checkedon{$item} =  ' checked="checked" ';
 2402:                 $checkedoff{$item} = ' ';
 2403:             } elsif ($settings->{$item} eq '0') {
 2404:                 $checkedoff{$item} =  ' checked="checked" ';
 2405:                 $checkedon{$item} = ' ';
 2406:             }
 2407:         }
 2408:     }
 2409:     foreach my $item (@{$toggles}) {
 2410:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2411:         $datatable .=
 2412:             '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices->{$item}.
 2413:             '</span></td>'.
 2414:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2415:             '<label><input type="radio" name="'.
 2416:             $item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').
 2417:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2418:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2419:             '</span></td>'.
 2420:             '</tr>';
 2421:         $itemcount ++;
 2422:     }
 2423:     return ($datatable,$itemcount);
 2424: }
 2425: 
 2426: sub print_coursedefaults {
 2427:     my ($position,$dom,$settings,$rowtotal) = @_;
 2428:     my ($css_class,$datatable);
 2429:     my $itemcount = 1;
 2430:     if ($position eq 'top') {
 2431:         my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2432:         %choices =
 2433:             &Apache::lonlocal::texthash (
 2434:                 canuse_pdfforms => 'Course/Community users can create/upload PDF forms',
 2435:         );
 2436:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2437:         @toggles = ('canuse_pdfforms',);
 2438:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2439:                                                  \%choices,$itemcount);
 2440:         $$rowtotal += $itemcount;
 2441:     } else {
 2442:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2443:         my %choices =
 2444:             &Apache::lonlocal::texthash (
 2445:                 anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2446:         );
 2447:         my $currdefresponder;
 2448:         if (ref($settings) eq 'HASH') {
 2449:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2450:         }
 2451:         if (!$currdefresponder) {
 2452:             $currdefresponder = 10;
 2453:         } elsif ($currdefresponder < 1) {
 2454:             $currdefresponder = 1;
 2455:         }
 2456:         $datatable .=
 2457:                '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices{'anonsurvey_threshold'}.
 2458:                 '</span></td>'.
 2459:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2460:                 '<input type="text" name="anonsurvey_threshold"'.
 2461:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2462:                 '</td></tr>';
 2463:     }
 2464:     return $datatable;
 2465: }
 2466: 
 2467: sub print_usersessions {
 2468:     my ($position,$dom,$settings,$rowtotal) = @_;
 2469:     my ($css_class,$datatable,%checked,%choices);
 2470:     my (%by_ip,%by_location,@intdoms);
 2471:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2472: 
 2473:     my @alldoms = &Apache::lonnet::all_domains();
 2474:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2475:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2476:     my %altids = &id_for_thisdom(%servers);
 2477:     my $itemcount = 1;
 2478:     if ($position eq 'top') {
 2479:         if (keys(%serverhomes) > 1) {
 2480:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2481:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2482:         } else {
 2483:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2484:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2485:         }
 2486:     } else {
 2487:         if (keys(%by_location) == 0) {
 2488:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2489:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2490:         } else {
 2491:             my %lt = &usersession_titles();
 2492:             my $numinrow = 5;
 2493:             my $prefix;
 2494:             my @types;
 2495:             if ($position eq 'bottom') {
 2496:                 $prefix = 'remote';
 2497:                 @types = ('version','excludedomain','includedomain');
 2498:             } else {
 2499:                 $prefix = 'hosted';
 2500:                 @types = ('excludedomain','includedomain');
 2501:             }
 2502:             my (%current,%checkedon,%checkedoff);
 2503:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2504:             my @locations = sort(keys(%by_location));
 2505:             foreach my $type (@types) {
 2506:                 $checkedon{$type} = '';
 2507:                 $checkedoff{$type} = ' checked="checked"';
 2508:             }
 2509:             if (ref($settings) eq 'HASH') {
 2510:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2511:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2512:                         $current{$key} = $settings->{$prefix}{$key};
 2513:                         if ($key eq 'version') {
 2514:                             if ($current{$key} ne '') {
 2515:                                 $checkedon{$key} = ' checked="checked"';
 2516:                                 $checkedoff{$key} = '';
 2517:                             }
 2518:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2519:                             $checkedon{$key} = ' checked="checked"';
 2520:                             $checkedoff{$key} = '';
 2521:                         }
 2522:                     }
 2523:                 }
 2524:             }
 2525:             foreach my $type (@types) {
 2526:                 next if ($type ne 'version' && !@locations);
 2527:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2528:                 $datatable .= '<tr'.$css_class.'>
 2529:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2530:                                <span class="LC_nobreak">&nbsp;
 2531:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2532:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2533:                 if ($type eq 'version') {
 2534:                     my $selector = '<select name="'.$prefix.'_version">';
 2535:                     foreach my $version (@lcversions) {
 2536:                         my $selected = '';
 2537:                         if ($current{'version'} eq $version) {
 2538:                             $selected = ' selected="selected"';
 2539:                         }
 2540:                         $selector .= ' <option value="'.$version.'"'.
 2541:                                      $selected.'>'.$version.'</option>';
 2542:                     }
 2543:                     $selector .= '</select> ';
 2544:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2545:                 } else {
 2546:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2547:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2548:                                  ' />'.('&nbsp;'x2).
 2549:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2550:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2551:                                  "\n".
 2552:                                  '</div><div><table>';
 2553:                     my $rem;
 2554:                     for (my $i=0; $i<@locations; $i++) {
 2555:                         my ($showloc,$value,$checkedtype);
 2556:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2557:                             my $ip = $by_location{$locations[$i]}->[0];
 2558:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2559:                                  $value = join(':',@{$by_ip{$ip}});
 2560:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2561:                                 if (ref($current{$type}) eq 'ARRAY') {
 2562:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2563:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2564:                                             $checkedtype = ' checked="checked"';
 2565:                                             last;
 2566:                                         }
 2567:                                     }
 2568:                                 }
 2569:                             }
 2570:                         }
 2571:                         $rem = $i%($numinrow);
 2572:                         if ($rem == 0) {
 2573:                             if ($i > 0) {
 2574:                                 $datatable .= '</tr>';
 2575:                             }
 2576:                             $datatable .= '<tr>';
 2577:                         }
 2578:                         $datatable .= '<td class="LC_left_item">'.
 2579:                                       '<span class="LC_nobreak"><label>'.
 2580:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2581:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2582:                                       '</label></span></td>';
 2583:                     }
 2584:                     $rem = @locations%($numinrow);
 2585:                     my $colsleft = $numinrow - $rem;
 2586:                     if ($colsleft > 1 ) {
 2587:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2588:                                       '&nbsp;</td>';
 2589:                     } elsif ($colsleft == 1) {
 2590:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2591:                     }
 2592:                     $datatable .= '</tr></table>';
 2593:                 }
 2594:                 $datatable .= '</td></tr>';
 2595:                 $itemcount ++;
 2596:             }
 2597:         }
 2598:     }
 2599:     $$rowtotal += $itemcount;
 2600:     return $datatable;
 2601: }
 2602: 
 2603: sub build_location_hashes {
 2604:     my ($intdoms,$by_ip,$by_location) = @_;
 2605:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2606:                   (ref($by_location) eq 'HASH')); 
 2607:     my %iphost = &Apache::lonnet::get_iphost();
 2608:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2609:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2610:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2611:         foreach my $id (@{$iphost{$primary_ip}}) {
 2612:             my $intdom = &Apache::lonnet::internet_dom($id);
 2613:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2614:                 push(@{$intdoms},$intdom);
 2615:             }
 2616:         }
 2617:     }
 2618:     foreach my $ip (keys(%iphost)) {
 2619:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2620:             foreach my $id (@{$iphost{$ip}}) {
 2621:                 my $location = &Apache::lonnet::internet_dom($id);
 2622:                 if ($location) {
 2623:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2624:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2625:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2626:                             push(@{$by_ip->{$ip}},$location);
 2627:                         }
 2628:                     } else {
 2629:                         $by_ip->{$ip} = [$location];
 2630:                     }
 2631:                 }
 2632:             }
 2633:         }
 2634:     }
 2635:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2636:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2637:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2638:             my $first = $by_ip->{$ip}->[0];
 2639:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2640:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2641:                     push(@{$by_location->{$first}},$ip);
 2642:                 }
 2643:             } else {
 2644:                 $by_location->{$first} = [$ip];
 2645:             }
 2646:         }
 2647:     }
 2648:     return;
 2649: }
 2650: 
 2651: sub current_offloads_to {
 2652:     my ($dom,$settings,$servers) = @_;
 2653:     my (%spareid,%otherdomconfigs);
 2654:     if (ref($servers) eq 'HASH') {
 2655:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2656:             my $gotspares;
 2657:             if (ref($settings) eq 'HASH') {
 2658:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2659:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2660:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2661:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2662:                         $gotspares = 1;
 2663:                     }
 2664:                 }
 2665:             }
 2666:             unless ($gotspares) {
 2667:                 my $gotspares;
 2668:                 my $serverhomeID =
 2669:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2670:                 my $serverhomedom =
 2671:                     &Apache::lonnet::host_domain($serverhomeID);
 2672:                 if ($serverhomedom ne $dom) {
 2673:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2674:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2675:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2676:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2677:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2678:                                 $gotspares = 1;
 2679:                             }
 2680:                         }
 2681:                     } else {
 2682:                         $otherdomconfigs{$serverhomedom} =
 2683:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2684:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2685:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2686:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2687:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2688:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2689:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2690:                                         $gotspares = 1;
 2691:                                     }
 2692:                                 }
 2693:                             }
 2694:                         }
 2695:                     }
 2696:                 }
 2697:             }
 2698:             unless ($gotspares) {
 2699:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2700:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2701:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2702:                } else {
 2703:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2704:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2705:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2706:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2707:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2708:                     } else {
 2709:                         my %what = (
 2710:                              spareid => 1,
 2711:                         );
 2712:                         my ($result,$returnhash) = 
 2713:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2714:                         if ($result eq 'ok') { 
 2715:                             if (ref($returnhash) eq 'HASH') {
 2716:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2717:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2718:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2719:                                 }
 2720:                             }
 2721:                         }
 2722:                     }
 2723:                 }
 2724:             }
 2725:         }
 2726:     }
 2727:     return %spareid;
 2728: }
 2729: 
 2730: sub spares_row {
 2731:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2732:     my $css_class;
 2733:     my $numinrow = 4;
 2734:     my $itemcount = 1;
 2735:     my $datatable;
 2736:     my %typetitles = &sparestype_titles();
 2737:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2738:         foreach my $server (sort(keys(%{$servers}))) {
 2739:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2740:             my ($othercontrol,$serverdom);
 2741:             if ($serverhome ne $server) {
 2742:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2743:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2744:             } else {
 2745:                 $serverdom = &Apache::lonnet::host_domain($server);
 2746:                 if ($serverdom ne $dom) {
 2747:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2748:                 }
 2749:             }
 2750:             next unless (ref($spareid->{$server}) eq 'HASH');
 2751:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2752:             $datatable .= '<tr'.$css_class.'>
 2753:                            <td rowspan="2">
 2754:                             <span class="LC_nobreak"><b>'.$server.'</b> when busy, offloads to:</span></td>'."\n";
 2755:             my (%current,%canselect);
 2756:             my @choices = 
 2757:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2758:             foreach my $type ('primary','default') {
 2759:                 if (ref($spareid->{$server}) eq 'HASH') {
 2760:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2761:                         my @spares = @{$spareid->{$server}{$type}};
 2762:                         if (@spares > 0) {
 2763:                             if ($othercontrol) {
 2764:                                 $current{$type} = join(', ',@spares);
 2765:                             } else {
 2766:                                 $current{$type} .= '<table>';
 2767:                                 my $numspares = scalar(@spares);
 2768:                                 for (my $i=0;  $i<@spares; $i++) {
 2769:                                     my $rem = $i%($numinrow);
 2770:                                     if ($rem == 0) {
 2771:                                         if ($i > 0) {
 2772:                                             $current{$type} .= '</tr>';
 2773:                                         }
 2774:                                         $current{$type} .= '<tr>';
 2775:                                     }
 2776:                                     $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;'.
 2777:                                                        $spareid->{$server}{$type}[$i].
 2778:                                                        '</label></td>'."\n";
 2779:                                 }
 2780:                                 my $rem = @spares%($numinrow);
 2781:                                 my $colsleft = $numinrow - $rem;
 2782:                                 if ($colsleft > 1 ) {
 2783:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2784:                                                        '" class="LC_left_item">'.
 2785:                                                        '&nbsp;</td>';
 2786:                                 } elsif ($colsleft == 1) {
 2787:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2788:                                 }
 2789:                                 $current{$type} .= '</tr></table>';
 2790:                             }
 2791:                         }
 2792:                     }
 2793:                     if ($current{$type} eq '') {
 2794:                         $current{$type} = &mt('None specified');
 2795:                     }
 2796:                     if ($othercontrol) {
 2797:                         if ($type eq 'primary') {
 2798:                             $canselect{$type} = $othercontrol;
 2799:                         }
 2800:                     } else {
 2801:                         $canselect{$type} = 
 2802:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2803:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2804:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2805:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2806:                         if (@choices > 0) {
 2807:                             foreach my $lonhost (@choices) {
 2808:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2809:                             }
 2810:                         }
 2811:                         $canselect{$type} .= '</select>'."\n";
 2812:                     }
 2813:                 } else {
 2814:                     $current{$type} = &mt('Could not be determined');
 2815:                     if ($type eq 'primary') {
 2816:                         $canselect{$type} =  $othercontrol;
 2817:                     }
 2818:                 }
 2819:                 if ($type eq 'default') {
 2820:                     $datatable .= '<tr'.$css_class.'>';
 2821:                 }
 2822:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2823:                               '<td>'.$current{$type}.'</td>'."\n".
 2824:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2825:             }
 2826:             $itemcount ++;
 2827:         }
 2828:     }
 2829:     $$rowtotal += $itemcount;
 2830:     return $datatable;
 2831: }
 2832: 
 2833: sub possible_newspares {
 2834:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2835:     my $serverhostname = &Apache::lonnet::hostname($server);
 2836:     my %excluded;
 2837:     if ($serverhostname ne '') {
 2838:         %excluded = (
 2839:                        $serverhostname => 1,
 2840:                     );
 2841:     }
 2842:     if (ref($currspares) eq 'HASH') {
 2843:         foreach my $type (keys(%{$currspares})) {
 2844:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2845:                 if (@{$currspares->{$type}} > 0) {
 2846:                     foreach my $curr (@{$currspares->{$type}}) {
 2847:                         my $hostname = &Apache::lonnet::hostname($curr);
 2848:                         $excluded{$hostname} = 1;
 2849:                     }
 2850:                 }
 2851:             }
 2852:         }
 2853:     }
 2854:     my @choices;
 2855:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2856:         if (keys(%{$serverhomes}) > 1) {
 2857:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2858:                 unless ($excluded{$name}) {
 2859:                     if (exists($altids->{$serverhomes->{$name}})) {
 2860:                         push(@choices,$altids->{$serverhomes->{$name}});
 2861:                     } else {
 2862:                         push(@choices,$serverhomes->{$name});
 2863:                     }
 2864:                 }
 2865:             }
 2866:         }
 2867:     }
 2868:     return sort(@choices);
 2869: }
 2870: 
 2871: sub print_loadbalancing {
 2872:     my ($dom,$settings,$rowtotal) = @_;
 2873:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2874:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2875:     my $numinrow = 1;
 2876:     my $datatable;
 2877:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2878:     my ($currbalancer,$currtargets,$currrules);
 2879:     if (keys(%servers) > 1) {
 2880:         if (ref($settings) eq 'HASH') {
 2881:             $currbalancer = $settings->{'lonhost'};
 2882:             $currtargets = $settings->{'targets'};
 2883:             $currrules = $settings->{'rules'};
 2884:         } else {
 2885:             ($currbalancer,$currtargets) = 
 2886:                 &Apache::lonnet::get_lonbalancer_config(\%servers);
 2887:         }
 2888:     } else {
 2889:         return;
 2890:     }
 2891:     my ($othertitle,$usertypes,$types) =
 2892:         &Apache::loncommon::sorted_inst_types($dom);
 2893:     my $rownum = 6;
 2894:     if (ref($types) eq 'ARRAY') {
 2895:         $rownum += scalar(@{$types});
 2896:     }
 2897:     my $css_class = ' class="LC_odd_row"';
 2898:     my $targets_div_style = 'display: none';
 2899:     my $disabled_div_style = 'display: block';
 2900:     my $homedom_div_style = 'display: none';
 2901:     $datatable = '<tr'.$css_class.'>'.
 2902:                  '<td rowspan="'.$rownum.'" valign="top">'.
 2903:                  '<p><select name="loadbalancing_lonhost" onchange="toggleTargets();">'."\n".
 2904:                  '<option value=""';
 2905:     if (($currbalancer eq '') || (!grep(/^\Q$currbalancer\E$/,keys(%servers)))) {
 2906:         $datatable .= ' selected="selected"';
 2907:     } else {
 2908:         $targets_div_style = 'display: block';
 2909:         $disabled_div_style = 'display: none';
 2910:         if ($dom eq &Apache::lonnet::host_domain($currbalancer)) {
 2911:             $homedom_div_style = 'display: block'; 
 2912:         }
 2913:     }
 2914:     $datatable .= '>'.&mt('None').'</option>'."\n";
 2915:     foreach my $lonhost (sort(keys(%servers))) {
 2916:         my $selected;
 2917:         if ($lonhost eq $currbalancer) {
 2918:             $selected .= ' selected="selected"';
 2919:         }
 2920:         $datatable .= '<option value="'.$lonhost.'"'.$selected.'>'.$lonhost.'</option>'."\n";
 2921:     }
 2922:     $datatable .= '</select></p></td><td rowspan="'.$rownum.'" valign="top">'.
 2923:                   '<div id="loadbalancing_disabled" style="'.$disabled_div_style.'">'.&mt('No dedicated Load Balancer').'</div>'."\n".
 2924:                   '<div id="loadbalancing_targets" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2925:     my ($numspares,@spares) = &count_servers($currbalancer,%servers);
 2926:     my @sparestypes = ('primary','default');
 2927:     my %typetitles = &sparestype_titles();
 2928:     foreach my $sparetype (@sparestypes) {
 2929:         my $targettable;
 2930:         for (my $i=0; $i<$numspares; $i++) {
 2931:             my $checked;
 2932:             if (ref($currtargets) eq 'HASH') {
 2933:                 if (ref($currtargets->{$sparetype}) eq 'ARRAY') {
 2934:                     if (grep(/^\Q$spares[$i]\E$/,@{$currtargets->{$sparetype}})) {
 2935:                         $checked = ' checked="checked"';
 2936:                     }
 2937:                 }
 2938:             }
 2939:             my $chkboxval;
 2940:             if (($currbalancer ne '') && (grep((/^\Q$currbalancer\E$/,keys(%servers))))) {
 2941:                 $chkboxval = $spares[$i];
 2942:             }
 2943:             $targettable .= '<td><label><input type="checkbox" name="loadbalancing_target_'.$sparetype.'"'.
 2944:                       $checked.' value="'.$chkboxval.'" id="loadbalancing_target_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$sparetype'".');" /><span id="loadbalancing_targettxt_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 2945:                       '</span></label></td>';
 2946:             my $rem = $i%($numinrow);
 2947:             if ($rem == 0) {
 2948:                 if ($i > 0) {
 2949:                     $targettable .= '</tr>';
 2950:                 }
 2951:                 $targettable .= '<tr>';
 2952:             }
 2953:         }
 2954:         if ($targettable ne '') {
 2955:             my $rem = $numspares%($numinrow);
 2956:             my $colsleft = $numinrow - $rem;
 2957:             if ($colsleft > 1 ) {
 2958:                 $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2959:                                 '&nbsp;</td>';
 2960:             } elsif ($colsleft == 1) {
 2961:                 $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 2962:             }
 2963:             $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 2964:                            '<table><tr>'.$targettable.'</table><br />';
 2965:         }
 2966:     }
 2967:     $datatable .= '</div></td></tr>'.
 2968:                   &loadbalancing_rules($dom,$intdom,$currrules,$othertitle,
 2969:                                        $usertypes,$types,\%servers,$currbalancer,
 2970:                                        $targets_div_style,$homedom_div_style,$css_class);
 2971:     $$rowtotal += $rownum;
 2972:     return $datatable;
 2973: }
 2974: 
 2975: sub loadbalancing_rules {
 2976:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 2977:         $currbalancer,$targets_div_style,$homedom_div_style,$css_class) = @_;
 2978:     my $output;
 2979:     my ($alltypes,$othertypes,$titles) = 
 2980:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 2981:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 2982:         foreach my $type (@{$alltypes}) {
 2983:             my $current;
 2984:             if (ref($currrules) eq 'HASH') {
 2985:                 $current = $currrules->{$type};
 2986:             }
 2987:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 2988:                 if ($dom ne &Apache::lonnet::host_domain($currbalancer)) {
 2989:                     $current = '';
 2990:                 }
 2991:             }
 2992:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 2993:                                              $servers,$currbalancer,$dom,
 2994:                                              $targets_div_style,$homedom_div_style,$css_class);
 2995:         }
 2996:     }
 2997:     return $output;
 2998: }
 2999: 
 3000: sub loadbalancing_titles {
 3001:     my ($dom,$intdom,$usertypes,$types) = @_;
 3002:     my %othertypes = (
 3003:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3004:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3005:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3006:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3007:                      );
 3008:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 3009:     if (ref($types) eq 'ARRAY') {
 3010:         unshift(@alltypes,@{$types},'default');
 3011:     }
 3012:     my %titles;
 3013:     foreach my $type (@alltypes) {
 3014:         if ($type =~ /^_LC_/) {
 3015:             $titles{$type} = $othertypes{$type};
 3016:         } elsif ($type eq 'default') {
 3017:             $titles{$type} = &mt('All users from [_1]',$dom);
 3018:             if (ref($types) eq 'ARRAY') {
 3019:                 if (@{$types} > 0) {
 3020:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3021:                 }
 3022:             }
 3023:         } elsif (ref($usertypes) eq 'HASH') {
 3024:             $titles{$type} = $usertypes->{$type};
 3025:         }
 3026:     }
 3027:     return (\@alltypes,\%othertypes,\%titles);
 3028: }
 3029: 
 3030: sub loadbalance_rule_row {
 3031:     my ($type,$title,$current,$servers,$currbalancer,$dom,$targets_div_style,
 3032:         $homedom_div_style,$css_class) = @_;
 3033:     my @rulenames = ('default','homeserver');
 3034:     my %ruletitles = &offloadtype_text();
 3035:     if ($type eq '_LC_external') {
 3036:         push(@rulenames,'externalbalancer');
 3037:     } else {
 3038:         push(@rulenames,'specific');
 3039:     }
 3040:     push(@rulenames,'none');
 3041:     my $style = $targets_div_style;
 3042:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3043:         $style = $homedom_div_style;
 3044:     }
 3045:     my $output = 
 3046:         '<tr'.$css_class.'><td valign="top"><div id="balanceruletitle_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3047:         '<td><div id="balancerule_'.$type.'" style="'.$style.'">'."\n";
 3048:     for (my $i=0; $i<@rulenames; $i++) {
 3049:         my $rule = $rulenames[$i];
 3050:         my ($checked,$extra);
 3051:         if ($rulenames[$i] eq 'default') {
 3052:             $rule = '';
 3053:         }
 3054:         if ($rulenames[$i] eq 'specific') {
 3055:             if (ref($servers) eq 'HASH') {
 3056:                 my $default;
 3057:                 if (($current ne '') && (exists($servers->{$current}))) {
 3058:                     $checked = ' checked="checked"';
 3059:                 }
 3060:                 unless ($checked) {
 3061:                     $default = ' selected="selected"';
 3062:                 }
 3063:                 $extra = ':&nbsp;<select name="loadbalancing_singleserver_'.$type.
 3064:                          '" id="loadbalancing_singleserver_'.$type.
 3065:                          '" onchange="singleServerToggle('."'$type'".')">'."\n".
 3066:                          '<option value=""'.$default.'></option>'."\n";
 3067:                 foreach my $lonhost (sort(keys(%{$servers}))) {
 3068:                     next if ($lonhost eq $currbalancer);
 3069:                     my $selected;
 3070:                     if ($lonhost eq $current) {
 3071:                         $selected = ' selected="selected"';
 3072:                     }
 3073:                     $extra .= '<option value="'.$lonhost.'"'.$selected.'>'.$lonhost.'</option>';
 3074:                 }
 3075:                 $extra .= '</select>';
 3076:             }
 3077:         } elsif ($rule eq $current) {
 3078:             $checked = ' checked="checked"';
 3079:         }
 3080:         $output .= '<span class="LC_nobreak"><label>'.
 3081:                    '<input type="radio" name="loadbalancing_rules_'.$type.
 3082:                    '" id="loadbalancing_rules_'.$type.'_'.$i.'" value="'.
 3083:                    $rule.'" onclick="balanceruleChange('."this.form,'$type'".
 3084:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3085:                    '</label>'.$extra.'</span><br />'."\n";
 3086:     }
 3087:     $output .= '</div></td></tr>'."\n";
 3088:     return $output;
 3089: }
 3090: 
 3091: sub offloadtype_text {
 3092:     my %ruletitles = &Apache::lonlocal::texthash (
 3093:            'default'          => 'Offloads to default destinations',
 3094:            'homeserver'       => "Offloads to user's home server",
 3095:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3096:            'specific'         => 'Offloads to specific server',
 3097:            'none'             => 'No offload',
 3098:     );
 3099:     return %ruletitles;
 3100: }
 3101: 
 3102: sub sparestype_titles {
 3103:     my %typestitles = &Apache::lonlocal::texthash (
 3104:                           'primary' => 'primary',
 3105:                           'default' => 'default',
 3106:                       );
 3107:     return %typestitles;
 3108: }
 3109: 
 3110: sub contact_titles {
 3111:     my %titles = &Apache::lonlocal::texthash (
 3112:                    'supportemail' => 'Support E-mail address',
 3113:                    'adminemail'   => 'Default Server Admin E-mail address',
 3114:                    'errormail'    => 'Error reports to be e-mailed to',
 3115:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3116:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3117:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3118:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3119:                  );
 3120:     my %short_titles = &Apache::lonlocal::texthash (
 3121:                            adminemail   => 'Admin E-mail address',
 3122:                            supportemail => 'Support E-mail',
 3123:                        );   
 3124:     return (\%titles,\%short_titles);
 3125: }
 3126: 
 3127: sub tool_titles {
 3128:     my %titles = &Apache::lonlocal::texthash (
 3129:                      aboutme    => 'Personal web page',
 3130:                      blog       => 'Blog',
 3131:                      webdav     => 'WebDAV',
 3132:                      portfolio  => 'Portfolio',
 3133:                      official   => 'Official courses (with institutional codes)',
 3134:                      unofficial => 'Unofficial courses',
 3135:                      community  => 'Communities',
 3136:                  );
 3137:     return %titles;
 3138: }
 3139: 
 3140: sub courserequest_titles {
 3141:     my %titles = &Apache::lonlocal::texthash (
 3142:                                    official   => 'Official',
 3143:                                    unofficial => 'Unofficial',
 3144:                                    community  => 'Communities',
 3145:                                    norequest  => 'Not allowed',
 3146:                                    approval   => 'Approval by Dom. Coord.',
 3147:                                    validate   => 'With validation',
 3148:                                    autolimit  => 'Numerical limit',
 3149:                                    unlimited  => '(blank for unlimited)',
 3150:                  );
 3151:     return %titles;
 3152: }
 3153: 
 3154: sub authorrequest_titles {
 3155:     my %titles = &Apache::lonlocal::texthash (
 3156:                                    norequest  => 'Not allowed',
 3157:                                    approval   => 'Approval by Dom. Coord.',
 3158:                                    automatic  => 'Automatic approval',
 3159:                  );
 3160:     return %titles;
 3161: } 
 3162: 
 3163: sub courserequest_conditions {
 3164:     my %conditions = &Apache::lonlocal::texthash (
 3165:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3166:        validate   => '(Processing of request subject to instittutional validation).',
 3167:                  );
 3168:     return %conditions;
 3169: }
 3170: 
 3171: 
 3172: sub print_usercreation {
 3173:     my ($position,$dom,$settings,$rowtotal) = @_;
 3174:     my $numinrow = 4;
 3175:     my $datatable;
 3176:     if ($position eq 'top') {
 3177:         $$rowtotal ++;
 3178:         my $rowcount = 0;
 3179:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3180:         if (ref($rules) eq 'HASH') {
 3181:             if (keys(%{$rules}) > 0) {
 3182:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3183:                                                 $ruleorder,$numinrow,$rowcount);
 3184:                 $$rowtotal ++;
 3185:                 $rowcount ++;
 3186:             }
 3187:         }
 3188:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3189:         if (ref($idrules) eq 'HASH') {
 3190:             if (keys(%{$idrules}) > 0) {
 3191:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3192:                                                 $idruleorder,$numinrow,$rowcount);
 3193:                 $$rowtotal ++;
 3194:                 $rowcount ++;
 3195:             }
 3196:         }
 3197:         my ($emailrules,$emailruleorder) = 
 3198:             &Apache::lonnet::inst_userrules($dom,'email');
 3199:         if (ref($emailrules) eq 'HASH') {
 3200:             if (keys(%{$emailrules}) > 0) {
 3201:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3202:                                                 $emailruleorder,$numinrow,$rowcount);
 3203:                 $$rowtotal ++;
 3204:                 $rowcount ++;
 3205:             }
 3206:         }
 3207:         if ($rowcount == 0) {
 3208:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3209:             $$rowtotal ++;
 3210:             $rowcount ++;
 3211:         }
 3212:     } elsif ($position eq 'middle') {
 3213:         my @creators = ('author','course','requestcrs','selfcreate');
 3214:         my ($rules,$ruleorder) =
 3215:             &Apache::lonnet::inst_userrules($dom,'username');
 3216:         my %lt = &usercreation_types();
 3217:         my %checked;
 3218:         my @selfcreate; 
 3219:         if (ref($settings) eq 'HASH') {
 3220:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3221:                 foreach my $item (@creators) {
 3222:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3223:                 }
 3224:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3225:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3226:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3227:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3228:                         @selfcreate = ('email','login','sso');
 3229:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3230:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3231:                     }
 3232:                 }
 3233:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3234:                 foreach my $item (@creators) {
 3235:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3236:                         $checked{$item} = 'none';
 3237:                     }
 3238:                 }
 3239:             }
 3240:         }
 3241:         my $rownum = 0;
 3242:         foreach my $item (@creators) {
 3243:             $rownum ++;
 3244:             if ($item ne 'selfcreate') {  
 3245:                 if ($checked{$item} eq '') {
 3246:                     $checked{$item} = 'any';
 3247:                 }
 3248:             }
 3249:             my $css_class;
 3250:             if ($rownum%2) {
 3251:                 $css_class = '';
 3252:             } else {
 3253:                 $css_class = ' class="LC_odd_row" ';
 3254:             }
 3255:             $datatable .= '<tr'.$css_class.'>'.
 3256:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3257:                          '</span></td><td align="right">';
 3258:             my @options;
 3259:             if ($item eq 'selfcreate') {
 3260:                 push(@options,('email','login','sso'));
 3261:             } else {
 3262:                 @options = ('any');
 3263:                 if (ref($rules) eq 'HASH') {
 3264:                     if (keys(%{$rules}) > 0) {
 3265:                         push(@options,('official','unofficial'));
 3266:                     }
 3267:                 }
 3268:                 push(@options,'none');
 3269:             }
 3270:             foreach my $option (@options) {
 3271:                 my $type = 'radio';
 3272:                 my $check = ' ';
 3273:                 if ($item eq 'selfcreate') {
 3274:                     $type = 'checkbox';
 3275:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3276:                         $check = ' checked="checked" ';
 3277:                     }
 3278:                 } else {
 3279:                     if ($checked{$item} eq $option) {
 3280:                         $check = ' checked="checked" ';
 3281:                     }
 3282:                 } 
 3283:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3284:                               '<input type="'.$type.'" name="can_createuser_'.
 3285:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3286:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3287:             }
 3288:             $datatable .= '</td></tr>';
 3289:         }
 3290:         my ($othertitle,$usertypes,$types) =
 3291:             &Apache::loncommon::sorted_inst_types($dom);
 3292:         my $createsettings;
 3293:         if (ref($settings) eq 'HASH') {
 3294:             $createsettings = $settings->{cancreate};
 3295:         }
 3296:         if (ref($usertypes) eq 'HASH') {
 3297:             if (keys(%{$usertypes}) > 0) {
 3298:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3299:                                              $dom,$numinrow,$othertitle,
 3300:                                              'statustocreate');
 3301:                 $$rowtotal ++;
 3302:             }
 3303:         }
 3304:         $datatable .= &captcha_choice('cancreate',$createsettings);
 3305:     } else {
 3306:         my @contexts = ('author','course','domain');
 3307:         my @authtypes = ('int','krb4','krb5','loc');
 3308:         my %checked;
 3309:         if (ref($settings) eq 'HASH') {
 3310:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3311:                 foreach my $item (@contexts) {
 3312:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3313:                         foreach my $auth (@authtypes) {
 3314:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3315:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3316:                             }
 3317:                         }
 3318:                     }
 3319:                 }
 3320:             }
 3321:         } else {
 3322:             foreach my $item (@contexts) {
 3323:                 foreach my $auth (@authtypes) {
 3324:                     $checked{$item}{$auth} = ' checked="checked" ';
 3325:                 }
 3326:             }
 3327:         }
 3328:         my %title = &context_names();
 3329:         my %authname = &authtype_names();
 3330:         my $rownum = 0;
 3331:         my $css_class; 
 3332:         foreach my $item (@contexts) {
 3333:             if ($rownum%2) {
 3334:                 $css_class = '';
 3335:             } else {
 3336:                 $css_class = ' class="LC_odd_row" ';
 3337:             }
 3338:             $datatable .=   '<tr'.$css_class.'>'.
 3339:                             '<td>'.$title{$item}.
 3340:                             '</td><td class="LC_left_item">'.
 3341:                             '<span class="LC_nobreak">';
 3342:             foreach my $auth (@authtypes) {
 3343:                 $datatable .= '<label>'. 
 3344:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3345:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3346:                               $authname{$auth}.'</label>&nbsp;';
 3347:             }
 3348:             $datatable .= '</span></td></tr>';
 3349:             $rownum ++;
 3350:         }
 3351:         $$rowtotal += $rownum;
 3352:     }
 3353:     return $datatable;
 3354: }
 3355: 
 3356: sub captcha_choice {
 3357:     my ($context,$settings) = @_;
 3358:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3359:     my %lt = &captcha_phrases();
 3360:     $keyentry = 'hidden';
 3361:     if ($context eq 'cancreate') {
 3362:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3363:     } elsif ($context eq 'help') {
 3364:         $rowname =  &mt('CAPTCHA validation');
 3365:     }
 3366:     if (ref($settings) eq 'HASH') {
 3367:         if ($settings->{'captcha'}) {
 3368:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3369:         } else {
 3370:             $checked{'original'} = ' checked="checked"';
 3371:         }
 3372:         if ($settings->{'captcha'} eq 'recaptcha') {
 3373:             $pubtext = $lt{'pub'};
 3374:             $privtext = $lt{'priv'};
 3375:             $keyentry = 'text';
 3376:         }
 3377:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3378:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3379:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3380:         }
 3381:     } else {
 3382:         $checked{'original'} = ' checked="checked"';
 3383:     }
 3384:     my $output = '<tr class="LC_odd_row">'.
 3385:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_right_item" colspan="2">'."\n".
 3386:                  '<table><tr><td>'."\n";
 3387:     foreach my $option ('original','recaptcha','notused') {
 3388:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3389:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3390:                    $lt{$option}.'</label></span>';
 3391:         unless ($option eq 'notused') {
 3392:             $output .= ('&nbsp;'x2)."\n";
 3393:         }
 3394:     }
 3395: #
 3396: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3397: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3398: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu) 
 3399: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3400: #  
 3401:     $output .= '</td></tr>'."\n".
 3402:                '<tr><td>'."\n".
 3403:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3404:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3405:                $currpub.'" size="40" /></span><br />'."\n".
 3406:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3407:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3408:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3409:                '</td></tr>';
 3410:     return $output;
 3411: }
 3412: 
 3413: sub user_formats_row {
 3414:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3415:     my $output;
 3416:     my %text = (
 3417:                    'username' => 'new usernames',
 3418:                    'id'       => 'IDs',
 3419:                    'email'    => 'self-created accounts (e-mail)',
 3420:                );
 3421:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3422:     $output = '<tr '.$css_class.'>'.
 3423:               '<td><span class="LC_nobreak">';
 3424:     if ($type eq 'email') {
 3425:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3426:     } else {
 3427:         $output .= &mt("Format rules to check for $text{$type}: ");
 3428:     }
 3429:     $output .= '</span></td>'.
 3430:                '<td class="LC_left_item" colspan="2"><table>';
 3431:     my $rem;
 3432:     if (ref($ruleorder) eq 'ARRAY') {
 3433:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3434:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3435:                 my $rem = $i%($numinrow);
 3436:                 if ($rem == 0) {
 3437:                     if ($i > 0) {
 3438:                         $output .= '</tr>';
 3439:                     }
 3440:                     $output .= '<tr>';
 3441:                 }
 3442:                 my $check = ' ';
 3443:                 if (ref($settings) eq 'HASH') {
 3444:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3445:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3446:                             $check = ' checked="checked" ';
 3447:                         }
 3448:                     }
 3449:                 }
 3450:                 $output .= '<td class="LC_left_item">'.
 3451:                            '<span class="LC_nobreak"><label>'.
 3452:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3453:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3454:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3455:             }
 3456:         }
 3457:         $rem = @{$ruleorder}%($numinrow);
 3458:     }
 3459:     my $colsleft = $numinrow - $rem;
 3460:     if ($colsleft > 1 ) {
 3461:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3462:                    '&nbsp;</td>';
 3463:     } elsif ($colsleft == 1) {
 3464:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3465:     }
 3466:     $output .= '</tr></table></td></tr>';
 3467:     return $output;
 3468: }
 3469: 
 3470: sub usercreation_types {
 3471:     my %lt = &Apache::lonlocal::texthash (
 3472:                     author     => 'When adding a co-author',
 3473:                     course     => 'When adding a user to a course',
 3474:                     requestcrs => 'When requesting a course',
 3475:                     selfcreate => 'User creates own account', 
 3476:                     any        => 'Any',
 3477:                     official   => 'Institutional only ',
 3478:                     unofficial => 'Non-institutional only',
 3479:                     email      => 'E-mail address',
 3480:                     login      => 'Institutional Login',
 3481:                     sso        => 'SSO', 
 3482:                     none       => 'None',
 3483:     );
 3484:     return %lt;
 3485: }
 3486: 
 3487: sub authtype_names {
 3488:     my %lt = &Apache::lonlocal::texthash(
 3489:                       int    => 'Internal',
 3490:                       krb4   => 'Kerberos 4',
 3491:                       krb5   => 'Kerberos 5',
 3492:                       loc    => 'Local',
 3493:                   );
 3494:     return %lt;
 3495: }
 3496: 
 3497: sub context_names {
 3498:     my %context_title = &Apache::lonlocal::texthash(
 3499:        author => 'Creating users when an Author',
 3500:        course => 'Creating users when in a course',
 3501:        domain => 'Creating users when a Domain Coordinator',
 3502:     );
 3503:     return %context_title;
 3504: }
 3505: 
 3506: sub print_usermodification {
 3507:     my ($position,$dom,$settings,$rowtotal) = @_;
 3508:     my $numinrow = 4;
 3509:     my ($context,$datatable,$rowcount);
 3510:     if ($position eq 'top') {
 3511:         $rowcount = 0;
 3512:         $context = 'author'; 
 3513:         foreach my $role ('ca','aa') {
 3514:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3515:                                                    $numinrow,$rowcount);
 3516:             $$rowtotal ++;
 3517:             $rowcount ++;
 3518:         }
 3519:     } elsif ($position eq 'middle') {
 3520:         $context = 'course';
 3521:         $rowcount = 0;
 3522:         foreach my $role ('st','ep','ta','in','cr') {
 3523:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3524:                                                    $numinrow,$rowcount);
 3525:             $$rowtotal ++;
 3526:             $rowcount ++;
 3527:         }
 3528:     } elsif ($position eq 'bottom') {
 3529:         $context = 'selfcreate';
 3530:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3531:         $usertypes->{'default'} = $othertitle;
 3532:         if (ref($types) eq 'ARRAY') {
 3533:             push(@{$types},'default');
 3534:             $usertypes->{'default'} = $othertitle;
 3535:             foreach my $status (@{$types}) {
 3536:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3537:                                                        $numinrow,$rowcount,$usertypes);
 3538:                 $$rowtotal ++;
 3539:                 $rowcount ++;
 3540:             }
 3541:         }
 3542:     }
 3543:     return $datatable;
 3544: }
 3545: 
 3546: sub print_defaults {
 3547:     my ($dom,$rowtotal) = @_;
 3548:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3549:                  'datelocale_def','portal_def');
 3550:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3551:     my $titles = &defaults_titles($dom);
 3552:     my $rownum = 0;
 3553:     my ($datatable,$css_class);
 3554:     foreach my $item (@items) {
 3555:         if ($rownum%2) {
 3556:             $css_class = '';
 3557:         } else {
 3558:             $css_class = ' class="LC_odd_row" ';
 3559:         }
 3560:         $datatable .= '<tr'.$css_class.'>'.
 3561:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3562:                   '</span></td><td class="LC_right_item">';
 3563:         if ($item eq 'auth_def') {
 3564:             my @authtypes = ('internal','krb4','krb5','localauth');
 3565:             my %shortauth = (
 3566:                              internal => 'int',
 3567:                              krb4 => 'krb4',
 3568:                              krb5 => 'krb5',
 3569:                              localauth  => 'loc'
 3570:                            );
 3571:             my %authnames = &authtype_names();
 3572:             foreach my $auth (@authtypes) {
 3573:                 my $checked = ' ';
 3574:                 if ($domdefaults{$item} eq $auth) {
 3575:                     $checked = ' checked="checked" ';
 3576:                 }
 3577:                 $datatable .= '<label><input type="radio" name="'.$item.
 3578:                               '" value="'.$auth.'"'.$checked.'/>'.
 3579:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3580:             }
 3581:         } elsif ($item eq 'timezone_def') {
 3582:             my $includeempty = 1;
 3583:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3584:         } elsif ($item eq 'datelocale_def') {
 3585:             my $includeempty = 1;
 3586:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3587:         } elsif ($item eq 'lang_def') {
 3588:             my %langchoices = &get_languages_hash();
 3589:             $langchoices{''} = 'No language preference';
 3590:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 3591:             $datatable .= &Apache::loncommon::select_form($domdefaults{$item},$item,
 3592:                                                           \%langchoices);
 3593:         } else {
 3594:             my $size;
 3595:             if ($item eq 'portal_def') {
 3596:                 $size = ' size="25"';
 3597:             }
 3598:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3599:                           $domdefaults{$item}.'"'.$size.' />';
 3600:         }
 3601:         $datatable .= '</td></tr>';
 3602:         $rownum ++;
 3603:     }
 3604:     $$rowtotal += $rownum;
 3605:     return $datatable;
 3606: }
 3607: 
 3608: sub get_languages_hash {
 3609:     my %langchoices;
 3610:     foreach my $id (&Apache::loncommon::languageids()) {
 3611:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 3612:         if ($code ne '') {
 3613:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 3614:         }
 3615:     }
 3616:     return %langchoices;
 3617: }
 3618: 
 3619: sub defaults_titles {
 3620:     my ($dom) = @_;
 3621:     my %titles = &Apache::lonlocal::texthash (
 3622:                    'auth_def'      => 'Default authentication type',
 3623:                    'auth_arg_def'  => 'Default authentication argument',
 3624:                    'lang_def'      => 'Default language',
 3625:                    'timezone_def'  => 'Default timezone',
 3626:                    'datelocale_def' => 'Default locale for dates',
 3627:                    'portal_def'     => 'Portal/Default URL',
 3628:                  );
 3629:     if ($dom) {
 3630:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3631:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3632:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3633:         $protocol = 'http' if ($protocol ne 'https');
 3634:         if ($uint_dom) {
 3635:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3636:                                          $uint_dom);
 3637:         }
 3638:     }
 3639:     return (\%titles);
 3640: }
 3641: 
 3642: sub print_scantronformat {
 3643:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3644:     my $itemcount = 1;
 3645:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3646:         %confhash);
 3647:     my $switchserver = &check_switchserver($dom,$confname);
 3648:     my %lt = &Apache::lonlocal::texthash (
 3649:                 default => 'Default bubblesheet format file error',
 3650:                 custom  => 'Custom bubblesheet format file error',
 3651:              );
 3652:     my %scantronfiles = (
 3653:         default => 'default.tab',
 3654:         custom => 'custom.tab',
 3655:     );
 3656:     foreach my $key (keys(%scantronfiles)) {
 3657:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3658:                               .$scantronfiles{$key};
 3659:     }
 3660:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3661:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3662:         if (!$switchserver) {
 3663:             my $servadm = $r->dir_config('lonAdmEMail');
 3664:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3665:             if ($configuserok eq 'ok') {
 3666:                 if ($author_ok eq 'ok') {
 3667:                     my %legacyfile = (
 3668:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3669:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3670:                     );
 3671:                     my %md5chk;
 3672:                     foreach my $type (keys(%legacyfile)) {
 3673:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3674:                         chomp($md5chk{$type});
 3675:                     }
 3676:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3677:                         foreach my $type (keys(%legacyfile)) {
 3678:                             ($scantronurls{$type},my $error) = 
 3679:                                 &legacy_scantronformat($r,$dom,$confname,
 3680:                                                  $type,$legacyfile{$type},
 3681:                                                  $scantronurls{$type},
 3682:                                                  $scantronfiles{$type});
 3683:                             if ($error ne '') {
 3684:                                 $error{$type} = $error;
 3685:                             }
 3686:                         }
 3687:                         if (keys(%error) == 0) {
 3688:                             $is_custom = 1;
 3689:                             $confhash{'scantron'}{'scantronformat'} = 
 3690:                                 $scantronurls{'custom'};
 3691:                             my $putresult = 
 3692:                                 &Apache::lonnet::put_dom('configuration',
 3693:                                                          \%confhash,$dom);
 3694:                             if ($putresult ne 'ok') {
 3695:                                 $error{'custom'} = 
 3696:                                     '<span class="LC_error">'.
 3697:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3698:                             }
 3699:                         }
 3700:                     } else {
 3701:                         ($scantronurls{'default'},my $error) =
 3702:                             &legacy_scantronformat($r,$dom,$confname,
 3703:                                           'default',$legacyfile{'default'},
 3704:                                           $scantronurls{'default'},
 3705:                                           $scantronfiles{'default'});
 3706:                         if ($error eq '') {
 3707:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3708:                             my $putresult =
 3709:                                 &Apache::lonnet::put_dom('configuration',
 3710:                                                          \%confhash,$dom);
 3711:                             if ($putresult ne 'ok') {
 3712:                                 $error{'default'} =
 3713:                                     '<span class="LC_error">'.
 3714:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3715:                             }
 3716:                         } else {
 3717:                             $error{'default'} = $error;
 3718:                         }
 3719:                     }
 3720:                 }
 3721:             }
 3722:         } else {
 3723:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3724:         }
 3725:     }
 3726:     if (ref($settings) eq 'HASH') {
 3727:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3728:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3729:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3730:                 $scantronurl = '';
 3731:             } else {
 3732:                 $scantronurl = $settings->{'scantronformat'};
 3733:             }
 3734:             $is_custom = 1;
 3735:         } else {
 3736:             $scantronurl = $scantronurls{'default'};
 3737:         }
 3738:     } else {
 3739:         if ($is_custom) {
 3740:             $scantronurl = $scantronurls{'custom'};
 3741:         } else {
 3742:             $scantronurl = $scantronurls{'default'};
 3743:         }
 3744:     }
 3745:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3746:     $datatable .= '<tr'.$css_class.'>';
 3747:     if (!$is_custom) {
 3748:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3749:                       '<span class="LC_nobreak">';
 3750:         if ($scantronurl) {
 3751:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3752:                           &mt('Default bubblesheet format file').'</a>';
 3753:         } else {
 3754:             $datatable = &mt('File unavailable for display');
 3755:         }
 3756:         $datatable .= '</span></td>';
 3757:         if (keys(%error) == 0) { 
 3758:             $datatable .= '<td valign="bottom">';
 3759:             if (!$switchserver) {
 3760:                 $datatable .= &mt('Upload:').'<br />';
 3761:             }
 3762:         } else {
 3763:             my $errorstr;
 3764:             foreach my $key (sort(keys(%error))) {
 3765:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3766:             }
 3767:             $datatable .= '<td>'.$errorstr;
 3768:         }
 3769:     } else {
 3770:         if (keys(%error) > 0) {
 3771:             my $errorstr;
 3772:             foreach my $key (sort(keys(%error))) {
 3773:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3774:             } 
 3775:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3776:         } elsif ($scantronurl) {
 3777:             $datatable .= '<td><span class="LC_nobreak">'.
 3778:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3779:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3780:                           '<input type="checkbox" name="scantronformat_del"'.
 3781:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3782:                           '<td><span class="LC_nobreak">&nbsp;'.
 3783:                           &mt('Replace:').'</span><br />';
 3784:         }
 3785:     }
 3786:     if (keys(%error) == 0) {
 3787:         if ($switchserver) {
 3788:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3789:         } else {
 3790:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3791:                          '<input type="file" name="scantronformat" /></span>';
 3792:         }
 3793:     }
 3794:     $datatable .= '</td></tr>';
 3795:     $$rowtotal ++;
 3796:     return $datatable;
 3797: }
 3798: 
 3799: sub legacy_scantronformat {
 3800:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3801:     my ($url,$error);
 3802:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3803:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3804:         (my $result,$url) =
 3805:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3806:                          '','',$newfile);
 3807:         if ($result ne 'ok') {
 3808:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3809:         }
 3810:     }
 3811:     return ($url,$error);
 3812: }
 3813: 
 3814: sub print_coursecategories {
 3815:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3816:     my $datatable;
 3817:     if ($position eq 'top') {
 3818:         my $toggle_cats_crs = ' ';
 3819:         my $toggle_cats_dom = ' checked="checked" ';
 3820:         my $can_cat_crs = ' ';
 3821:         my $can_cat_dom = ' checked="checked" ';
 3822:         my $toggle_catscomm_comm = ' ';
 3823:         my $toggle_catscomm_dom = ' checked="checked" ';
 3824:         my $can_catcomm_comm = ' ';
 3825:         my $can_catcomm_dom = ' checked="checked" ';
 3826: 
 3827:         if (ref($settings) eq 'HASH') {
 3828:             if ($settings->{'togglecats'} eq 'crs') {
 3829:                 $toggle_cats_crs = $toggle_cats_dom;
 3830:                 $toggle_cats_dom = ' ';
 3831:             }
 3832:             if ($settings->{'categorize'} eq 'crs') {
 3833:                 $can_cat_crs = $can_cat_dom;
 3834:                 $can_cat_dom = ' ';
 3835:             }
 3836:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3837:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3838:                 $toggle_catscomm_dom = ' ';
 3839:             }
 3840:             if ($settings->{'categorizecomm'} eq 'comm') {
 3841:                 $can_catcomm_comm = $can_catcomm_dom;
 3842:                 $can_catcomm_dom = ' ';
 3843:             }
 3844:         }
 3845:         my %title = &Apache::lonlocal::texthash (
 3846:                      togglecats     => 'Show/Hide a course in catalog',
 3847:                      togglecatscomm => 'Show/Hide a community in catalog',
 3848:                      categorize     => 'Assign a category to a course',
 3849:                      categorizecomm => 'Assign a category to a community',
 3850:                     );
 3851:         my %level = &Apache::lonlocal::texthash (
 3852:                      dom  => 'Set in Domain',
 3853:                      crs  => 'Set in Course',
 3854:                      comm => 'Set in Community',
 3855:                     );
 3856:         $datatable = '<tr class="LC_odd_row">'.
 3857:                   '<td>'.$title{'togglecats'}.'</td>'.
 3858:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3859:                   '<input type="radio" name="togglecats"'.
 3860:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3861:                   '<label><input type="radio" name="togglecats"'.
 3862:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3863:                   '</tr><tr>'.
 3864:                   '<td>'.$title{'categorize'}.'</td>'.
 3865:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3866:                   '<label><input type="radio" name="categorize"'.
 3867:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3868:                   '<label><input type="radio" name="categorize"'.
 3869:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3870:                   '</tr><tr class="LC_odd_row">'.
 3871:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 3872:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3873:                   '<input type="radio" name="togglecatscomm"'.
 3874:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3875:                   '<label><input type="radio" name="togglecatscomm"'.
 3876:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3877:                   '</tr><tr>'.
 3878:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 3879:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3880:                   '<label><input type="radio" name="categorizecomm"'.
 3881:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3882:                   '<label><input type="radio" name="categorizecomm"'.
 3883:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3884:                   '</tr>';
 3885:         $$rowtotal += 4;
 3886:     } else {
 3887:         my $css_class;
 3888:         my $itemcount = 1;
 3889:         my $cathash; 
 3890:         if (ref($settings) eq 'HASH') {
 3891:             $cathash = $settings->{'cats'};
 3892:         }
 3893:         if (ref($cathash) eq 'HASH') {
 3894:             my (@cats,@trails,%allitems,%idx,@jsarray);
 3895:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 3896:                                                    \%allitems,\%idx,\@jsarray);
 3897:             my $maxdepth = scalar(@cats);
 3898:             my $colattrib = '';
 3899:             if ($maxdepth > 2) {
 3900:                 $colattrib = ' colspan="2" ';
 3901:             }
 3902:             my @path;
 3903:             if (@cats > 0) {
 3904:                 if (ref($cats[0]) eq 'ARRAY') {
 3905:                     my $numtop = @{$cats[0]};
 3906:                     my $maxnum = $numtop;
 3907:                     my %default_names = (
 3908:                           instcode    => &mt('Official courses'),
 3909:                           communities => &mt('Communities'),
 3910:                     );
 3911: 
 3912:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 3913:                         ($cathash->{'instcode::0'} eq '') ||
 3914:                         (!grep(/^communities$/,@{$cats[0]})) || 
 3915:                         ($cathash->{'communities::0'} eq '')) {
 3916:                         $maxnum ++;
 3917:                     }
 3918:                     my $lastidx;
 3919:                     for (my $i=0; $i<$numtop; $i++) {
 3920:                         my $parent = $cats[0][$i];
 3921:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3922:                         my $item = &escape($parent).'::0';
 3923:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 3924:                         $lastidx = $idx{$item};
 3925:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3926:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 3927:                         for (my $k=0; $k<=$maxnum; $k++) {
 3928:                             my $vpos = $k+1;
 3929:                             my $selstr;
 3930:                             if ($k == $i) {
 3931:                                 $selstr = ' selected="selected" ';
 3932:                             }
 3933:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3934:                         }
 3935:                         $datatable .= '</select></td><td>';
 3936:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 3937:                             $datatable .=  '<span class="LC_nobreak">'
 3938:                                            .$default_names{$parent}.'</span>';
 3939:                             if ($parent eq 'instcode') {
 3940:                                 $datatable .= '<br /><span class="LC_nobreak">('
 3941:                                               .&mt('with institutional codes')
 3942:                                               .')</span></td><td'.$colattrib.'>';
 3943:                             } else {
 3944:                                 $datatable .= '<table><tr><td>';
 3945:                             }
 3946:                             $datatable .= '<span class="LC_nobreak">'
 3947:                                           .'<label><input type="radio" name="'
 3948:                                           .$parent.'" value="1" checked="checked" />'
 3949:                                           .&mt('Display').'</label>';
 3950:                             if ($parent eq 'instcode') {
 3951:                                 $datatable .= '&nbsp;';
 3952:                             } else {
 3953:                                 $datatable .= '</span></td></tr><tr><td>'
 3954:                                               .'<span class="LC_nobreak">';
 3955:                             }
 3956:                             $datatable .= '<label><input type="radio" name="'
 3957:                                           .$parent.'" value="0" />'
 3958:                                           .&mt('Do not display').'</label></span>';
 3959:                             if ($parent eq 'communities') {
 3960:                                 $datatable .= '</td></tr></table>';
 3961:                             }
 3962:                             $datatable .= '</td>';
 3963:                         } else {
 3964:                             $datatable .= $parent
 3965:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 3966:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 3967:                         }
 3968:                         my $depth = 1;
 3969:                         push(@path,$parent);
 3970:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 3971:                         pop(@path);
 3972:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 3973:                         $itemcount ++;
 3974:                     }
 3975:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3976:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 3977:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 3978:                     for (my $k=0; $k<=$maxnum; $k++) {
 3979:                         my $vpos = $k+1;
 3980:                         my $selstr;
 3981:                         if ($k == $numtop) {
 3982:                             $selstr = ' selected="selected" ';
 3983:                         }
 3984:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3985:                     }
 3986:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 3987:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 3988:                                   .'</tr>'."\n";
 3989:                     $itemcount ++;
 3990:                     foreach my $default ('instcode','communities') {
 3991:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 3992:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3993:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 3994:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 3995:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 3996:                             for (my $k=0; $k<=$maxnum; $k++) {
 3997:                                 my $vpos = $k+1;
 3998:                                 my $selstr;
 3999:                                 if ($k == $maxnum) {
 4000:                                     $selstr = ' selected="selected" ';
 4001:                                 }
 4002:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4003:                             }
 4004:                             $datatable .= '</select></span></td>'.
 4005:                                           '<td><span class="LC_nobreak">'.
 4006:                                           $default_names{$default}.'</span>';
 4007:                             if ($default eq 'instcode') {
 4008:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4009:                                               .&mt('with institutional codes').')</span>';
 4010:                             }
 4011:                             $datatable .= '</td>'
 4012:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4013:                                           .&mt('Display').'</label>&nbsp;'
 4014:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4015:                                           .&mt('Do not display').'</label></span></td></tr>';
 4016:                         }
 4017:                     }
 4018:                 }
 4019:             } else {
 4020:                 $datatable .= &initialize_categories($itemcount);
 4021:             }
 4022:         } else {
 4023:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4024:                           .&initialize_categories($itemcount);
 4025:         }
 4026:         $$rowtotal += $itemcount;
 4027:     }
 4028:     return $datatable;
 4029: }
 4030: 
 4031: sub print_serverstatuses {
 4032:     my ($dom,$settings,$rowtotal) = @_;
 4033:     my $datatable;
 4034:     my @pages = &serverstatus_pages();
 4035:     my (%namedaccess,%machineaccess);
 4036:     foreach my $type (@pages) {
 4037:         $namedaccess{$type} = '';
 4038:         $machineaccess{$type}= '';
 4039:     }
 4040:     if (ref($settings) eq 'HASH') {
 4041:         foreach my $type (@pages) {
 4042:             if (exists($settings->{$type})) {
 4043:                 if (ref($settings->{$type}) eq 'HASH') {
 4044:                     foreach my $key (keys(%{$settings->{$type}})) {
 4045:                         if ($key eq 'namedusers') {
 4046:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4047:                         } elsif ($key eq 'machines') {
 4048:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4049:                         }
 4050:                     }
 4051:                 }
 4052:             }
 4053:         }
 4054:     }
 4055:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4056:     my $rownum = 0;
 4057:     my $css_class;
 4058:     foreach my $type (@pages) {
 4059:         $rownum ++;
 4060:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4061:         $datatable .= '<tr'.$css_class.'>'.
 4062:                       '<td><span class="LC_nobreak">'.
 4063:                       $titles->{$type}.'</span></td>'.
 4064:                       '<td class="LC_left_item">'.
 4065:                       '<input type="text" name="'.$type.'_namedusers" '.
 4066:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4067:                       '<td class="LC_right_item">'.
 4068:                       '<span class="LC_nobreak">'.
 4069:                       '<input type="text" name="'.$type.'_machines" '.
 4070:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4071:                       '</td></tr>'."\n";
 4072:     }
 4073:     $$rowtotal += $rownum;
 4074:     return $datatable;
 4075: }
 4076: 
 4077: sub serverstatus_pages {
 4078:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4079:             'clusterstatus','metadata_keywords','metadata_harvest',
 4080:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4081: }
 4082: 
 4083: sub coursecategories_javascript {
 4084:     my ($settings) = @_;
 4085:     my ($output,$jstext,$cathash);
 4086:     if (ref($settings) eq 'HASH') {
 4087:         $cathash = $settings->{'cats'};
 4088:     }
 4089:     if (ref($cathash) eq 'HASH') {
 4090:         my (@cats,@jsarray,%idx);
 4091:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4092:         if (@jsarray > 0) {
 4093:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4094:             for (my $i=0; $i<@jsarray; $i++) {
 4095:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4096:                     my $catstr = join('","',@{$jsarray[$i]});
 4097:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4098:                 }
 4099:             }
 4100:         }
 4101:     } else {
 4102:         $jstext  = '    var categories = Array(1);'."\n".
 4103:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4104:     }
 4105:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4106:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4107:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4108:     $output = <<"ENDSCRIPT";
 4109: <script type="text/javascript">
 4110: // <![CDATA[
 4111: function reorderCats(form,parent,item,idx) {
 4112:     var changedVal;
 4113: $jstext
 4114:     var newpos = 'addcategory_pos';
 4115:     var current = new Array;
 4116:     if (parent == '') {
 4117:         var has_instcode = 0;
 4118:         var maxtop = categories[idx].length;
 4119:         for (var j=0; j<maxtop; j++) {
 4120:             if (categories[idx][j] == 'instcode::0') {
 4121:                 has_instcode == 1;
 4122:             }
 4123:         }
 4124:         if (has_instcode == 0) {
 4125:             categories[idx][maxtop] = 'instcode_pos';
 4126:         }
 4127:     } else {
 4128:         newpos += '_'+parent;
 4129:     }
 4130:     var maxh = 1 + categories[idx].length;
 4131:     var current = new Array;
 4132:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4133:     if (item == newpos) {
 4134:         changedVal = newitemVal;
 4135:     } else {
 4136:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4137:         current[newitemVal] = newpos;
 4138:     }
 4139:     for (var i=0; i<categories[idx].length; i++) {
 4140:         var elementName = categories[idx][i];
 4141:         if (elementName != item) {
 4142:             if (form.elements[elementName]) {
 4143:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4144:                 current[currVal] = elementName;
 4145:             }
 4146:         }
 4147:     }
 4148:     var oldVal;
 4149:     for (var j=0; j<maxh; j++) {
 4150:         if (current[j] == undefined) {
 4151:             oldVal = j;
 4152:         }
 4153:     }
 4154:     if (oldVal < changedVal) {
 4155:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4156:            var elementName = current[k];
 4157:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4158:         }
 4159:     } else {
 4160:         for (var k=changedVal; k<oldVal; k++) {
 4161:             var elementName = current[k];
 4162:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4163:         }
 4164:     }
 4165:     return;
 4166: }
 4167: 
 4168: function categoryCheck(form) {
 4169:     if (form.elements['addcategory_name'].value == 'instcode') {
 4170:         alert('$instcode_reserved\\n$choose_again');
 4171:         return false;
 4172:     }
 4173:     if (form.elements['addcategory_name'].value == 'communities') {
 4174:         alert('$communities_reserved\\n$choose_again');
 4175:         return false;
 4176:     }
 4177:     return true;
 4178: }
 4179: 
 4180: // ]]>
 4181: </script>
 4182: 
 4183: ENDSCRIPT
 4184:     return $output;
 4185: }
 4186: 
 4187: sub initialize_categories {
 4188:     my ($itemcount) = @_;
 4189:     my ($datatable,$css_class,$chgstr);
 4190:     my %default_names = (
 4191:                       instcode    => 'Official courses (with institutional codes)',
 4192:                       communities => 'Communities',
 4193:                         );
 4194:     my $select0 = ' selected="selected"';
 4195:     my $select1 = '';
 4196:     foreach my $default ('instcode','communities') {
 4197:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4198:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4199:         if ($default eq 'communities') {
 4200:             $select1 = $select0;
 4201:             $select0 = '';
 4202:         }
 4203:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4204:                      .'<select name="'.$default.'_pos">'
 4205:                      .'<option value="0"'.$select0.'>1</option>'
 4206:                      .'<option value="1"'.$select1.'>2</option>'
 4207:                      .'<option value="2">3</option></select>&nbsp;'
 4208:                      .$default_names{$default}
 4209:                      .'</span></td><td><span class="LC_nobreak">'
 4210:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4211:                      .&mt('Display').'</label>&nbsp;<label>'
 4212:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4213:                  .'</label></span></td></tr>';
 4214:         $itemcount ++;
 4215:     }
 4216:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4217:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4218:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4219:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4220:                   .'<option value="0">1</option>'
 4221:                   .'<option value="1">2</option>'
 4222:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4223:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4224:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4225:     return $datatable;
 4226: }
 4227: 
 4228: sub build_category_rows {
 4229:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4230:     my ($text,$name,$item,$chgstr);
 4231:     if (ref($cats) eq 'ARRAY') {
 4232:         my $maxdepth = scalar(@{$cats});
 4233:         if (ref($cats->[$depth]) eq 'HASH') {
 4234:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4235:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4236:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4237:                 $text .= '<td><table class="LC_datatable">';
 4238:                 my ($idxnum,$parent_name,$parent_item);
 4239:                 my $higher = $depth - 1;
 4240:                 if ($higher == 0) {
 4241:                     $parent_name = &escape($parent).'::'.$higher;
 4242:                 } else {
 4243:                     if (ref($path) eq 'ARRAY') {
 4244:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4245:                     }
 4246:                 }
 4247:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4248:                 for (my $j=0; $j<=$numchildren; $j++) {
 4249:                     if ($j < $numchildren) {
 4250:                         $name = $cats->[$depth]{$parent}[$j];
 4251:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4252:                         $idxnum = $idx->{$item};
 4253:                     } else {
 4254:                         $name = $parent_name;
 4255:                         $item = $parent_item;
 4256:                     }
 4257:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4258:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4259:                     for (my $i=0; $i<=$numchildren; $i++) {
 4260:                         my $vpos = $i+1;
 4261:                         my $selstr;
 4262:                         if ($j == $i) {
 4263:                             $selstr = ' selected="selected" ';
 4264:                         }
 4265:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4266:                     }
 4267:                     $text .= '</select>&nbsp;';
 4268:                     if ($j < $numchildren) {
 4269:                         my $deeper = $depth+1;
 4270:                         $text .= $name.'&nbsp;'
 4271:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4272:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4273:                         if(ref($path) eq 'ARRAY') {
 4274:                             push(@{$path},$name);
 4275:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4276:                             pop(@{$path});
 4277:                         }
 4278:                     } else {
 4279:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4280:                         if ($j == $numchildren) {
 4281:                             $text .= $name;
 4282:                         } else {
 4283:                             $text .= $item;
 4284:                         }
 4285:                         $text .= '" value="" />';
 4286:                     }
 4287:                     $text .= '</td></tr>';
 4288:                 }
 4289:                 $text .= '</table></td>';
 4290:             } else {
 4291:                 my $higher = $depth-1;
 4292:                 if ($higher == 0) {
 4293:                     $name = &escape($parent).'::'.$higher;
 4294:                 } else {
 4295:                     if (ref($path) eq 'ARRAY') {
 4296:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4297:                     }
 4298:                 }
 4299:                 my $colspan;
 4300:                 if ($parent ne 'instcode') {
 4301:                     $colspan = $maxdepth - $depth - 1;
 4302:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4303:                 }
 4304:             }
 4305:         }
 4306:     }
 4307:     return $text;
 4308: }
 4309: 
 4310: sub modifiable_userdata_row {
 4311:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4312:     my $rolename;
 4313:     if ($context eq 'selfcreate') {
 4314:         if (ref($usertypes) eq 'HASH') {
 4315:             $rolename = $usertypes->{$role};
 4316:         } else {
 4317:             $rolename = $role;
 4318:         }
 4319:     } else {
 4320:         if ($role eq 'cr') {
 4321:             $rolename = &mt('Custom role');
 4322:         } else {
 4323:             $rolename = &Apache::lonnet::plaintext($role);
 4324:         }
 4325:     }
 4326:     my @fields = ('lastname','firstname','middlename','generation',
 4327:                   'permanentemail','id');
 4328:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4329:     my $output;
 4330:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4331:     $output = '<tr '.$css_class.'>'.
 4332:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4333:               '<td class="LC_left_item" colspan="2"><table>';
 4334:     my $rem;
 4335:     my %checks;
 4336:     if (ref($settings) eq 'HASH') {
 4337:         if (ref($settings->{$context}) eq 'HASH') {
 4338:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4339:                 foreach my $field (@fields) {
 4340:                     if ($settings->{$context}->{$role}->{$field}) {
 4341:                         $checks{$field} = ' checked="checked" ';
 4342:                     }
 4343:                 }
 4344:             }
 4345:         }
 4346:     }
 4347:     for (my $i=0; $i<@fields; $i++) {
 4348:         my $rem = $i%($numinrow);
 4349:         if ($rem == 0) {
 4350:             if ($i > 0) {
 4351:                 $output .= '</tr>';
 4352:             }
 4353:             $output .= '<tr>';
 4354:         }
 4355:         my $check = ' ';
 4356:         if (exists($checks{$fields[$i]})) {
 4357:             $check = $checks{$fields[$i]}
 4358:         } else {
 4359:             if ($role eq 'st') {
 4360:                 if (ref($settings) ne 'HASH') {
 4361:                     $check = ' checked="checked" '; 
 4362:                 }
 4363:             }
 4364:         }
 4365:         $output .= '<td class="LC_left_item">'.
 4366:                    '<span class="LC_nobreak"><label>'.
 4367:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4368:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4369:                    '</label></span></td>';
 4370:         $rem = @fields%($numinrow);
 4371:     }
 4372:     my $colsleft = $numinrow - $rem;
 4373:     if ($colsleft > 1 ) {
 4374:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4375:                    '&nbsp;</td>';
 4376:     } elsif ($colsleft == 1) {
 4377:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4378:     }
 4379:     $output .= '</tr></table></td></tr>';
 4380:     return $output;
 4381: }
 4382: 
 4383: sub insttypes_row {
 4384:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4385:     my %lt = &Apache::lonlocal::texthash (
 4386:                       cansearch => 'Users allowed to search',
 4387:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4388:                       lockablenames => 'User preference to lock name',
 4389:              );
 4390:     my $showdom;
 4391:     if ($context eq 'cansearch') {
 4392:         $showdom = ' ('.$dom.')';
 4393:     }
 4394:     my $class = 'LC_left_item';
 4395:     if ($context eq 'statustocreate') {
 4396:         $class = 'LC_right_item';
 4397:     }
 4398:     my $output =  '<tr class="LC_odd_row">'.
 4399:                   '<td>'.$lt{$context}.$showdom.
 4400:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4401:     my $rem;
 4402:     if (ref($types) eq 'ARRAY') {
 4403:         for (my $i=0; $i<@{$types}; $i++) {
 4404:             if (defined($usertypes->{$types->[$i]})) {
 4405:                 my $rem = $i%($numinrow);
 4406:                 if ($rem == 0) {
 4407:                     if ($i > 0) {
 4408:                         $output .= '</tr>';
 4409:                     }
 4410:                     $output .= '<tr>';
 4411:                 }
 4412:                 my $check = ' ';
 4413:                 if (ref($settings) eq 'HASH') {
 4414:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4415:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4416:                             $check = ' checked="checked" ';
 4417:                         }
 4418:                     } elsif ($context eq 'statustocreate') {
 4419:                         $check = ' checked="checked" ';
 4420:                     }
 4421:                 }
 4422:                 $output .= '<td class="LC_left_item">'.
 4423:                            '<span class="LC_nobreak"><label>'.
 4424:                            '<input type="checkbox" name="'.$context.'" '.
 4425:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4426:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4427:             }
 4428:         }
 4429:         $rem = @{$types}%($numinrow);
 4430:     }
 4431:     my $colsleft = $numinrow - $rem;
 4432:     if (($rem == 0) && (@{$types} > 0)) {
 4433:         $output .= '<tr>';
 4434:     }
 4435:     if ($colsleft > 1) {
 4436:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4437:     } else {
 4438:         $output .= '<td class="LC_left_item">';
 4439:     }
 4440:     my $defcheck = ' ';
 4441:     if (ref($settings) eq 'HASH') {  
 4442:         if (ref($settings->{$context}) eq 'ARRAY') {
 4443:             if (grep(/^default$/,@{$settings->{$context}})) {
 4444:                 $defcheck = ' checked="checked" ';
 4445:             }
 4446:         } elsif ($context eq 'statustocreate') {
 4447:             $defcheck = ' checked="checked" ';
 4448:         }
 4449:     }
 4450:     $output .= '<span class="LC_nobreak"><label>'.
 4451:                '<input type="checkbox" name="'.$context.'" '.
 4452:                'value="default"'.$defcheck.'/>'.
 4453:                $othertitle.'</label></span></td>'.
 4454:                '</tr></table></td></tr>';
 4455:     return $output;
 4456: }
 4457: 
 4458: sub sorted_searchtitles {
 4459:     my %searchtitles = &Apache::lonlocal::texthash(
 4460:                          'uname' => 'username',
 4461:                          'lastname' => 'last name',
 4462:                          'lastfirst' => 'last name, first name',
 4463:                      );
 4464:     my @titleorder = ('uname','lastname','lastfirst');
 4465:     return (\%searchtitles,\@titleorder);
 4466: }
 4467: 
 4468: sub sorted_searchtypes {
 4469:     my %srchtypes_desc = (
 4470:                            exact    => 'is exact match',
 4471:                            contains => 'contains ..',
 4472:                            begins   => 'begins with ..',
 4473:                          );
 4474:     my @srchtypeorder = ('exact','begins','contains');
 4475:     return (\%srchtypes_desc,\@srchtypeorder);
 4476: }
 4477: 
 4478: sub usertype_update_row {
 4479:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4480:     my $datatable;
 4481:     my $numinrow = 4;
 4482:     foreach my $type (@{$types}) {
 4483:         if (defined($usertypes->{$type})) {
 4484:             $$rownums ++;
 4485:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4486:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4487:                           '</td><td class="LC_left_item"><table>';
 4488:             for (my $i=0; $i<@{$fields}; $i++) {
 4489:                 my $rem = $i%($numinrow);
 4490:                 if ($rem == 0) {
 4491:                     if ($i > 0) {
 4492:                         $datatable .= '</tr>';
 4493:                     }
 4494:                     $datatable .= '<tr>';
 4495:                 }
 4496:                 my $check = ' ';
 4497:                 if (ref($settings) eq 'HASH') {
 4498:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4499:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4500:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4501:                                 $check = ' checked="checked" ';
 4502:                             }
 4503:                         }
 4504:                     }
 4505:                 }
 4506: 
 4507:                 if ($i == @{$fields}-1) {
 4508:                     my $colsleft = $numinrow - $rem;
 4509:                     if ($colsleft > 1) {
 4510:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4511:                     } else {
 4512:                         $datatable .= '<td>';
 4513:                     }
 4514:                 } else {
 4515:                     $datatable .= '<td>';
 4516:                 }
 4517:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4518:                               '<input type="checkbox" name="updateable_'.$type.
 4519:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4520:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4521:             }
 4522:             $datatable .= '</tr></table></td></tr>';
 4523:         }
 4524:     }
 4525:     return $datatable;
 4526: }
 4527: 
 4528: sub modify_login {
 4529:     my ($r,$dom,$confname,%domconfig) = @_;
 4530:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4531:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4532:     %title = ( coursecatalog => 'Display course catalog',
 4533:                adminmail => 'Display administrator E-mail address',
 4534:                newuser => 'Link for visitors to create a user account',
 4535:                loginheader => 'Log-in box header');
 4536:     @offon = ('off','on');
 4537:     if (ref($domconfig{login}) eq 'HASH') {
 4538:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4539:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4540:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4541:             }
 4542:         }
 4543:     }
 4544:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4545:                                            \%domconfig,\%loginhash);
 4546:     my @toggles = ('coursecatalog','adminmail','newuser');
 4547:     foreach my $item (@toggles) {
 4548:         $loginhash{login}{$item} = $env{'form.'.$item};
 4549:     }
 4550:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4551:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4552:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4553:                                          \%loginhash);
 4554:     }
 4555: 
 4556:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4557:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4558:     if (keys(%servers) > 1) {
 4559:         foreach my $lonhost (keys(%servers)) {
 4560:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4561:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4562:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4563:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4564:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4565:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4566:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4567:                         $changes{'loginvia'}{$lonhost} = 1;
 4568:                     } else {
 4569:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4570:                         $changes{'loginvia'}{$lonhost} = 1;
 4571:                     }
 4572:                 } else {
 4573:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4574:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4575:                         $changes{'loginvia'}{$lonhost} = 1;
 4576:                     }
 4577:                 }
 4578:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4579:                     foreach my $item (@loginvia_attribs) {
 4580:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4581:                     }
 4582:                 } else {
 4583:                     foreach my $item (@loginvia_attribs) {
 4584:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4585:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4586:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4587:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4588:                                 $new = '/';
 4589:                             }
 4590:                         }
 4591:                         if (($item eq 'custompath') && 
 4592:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4593:                             $new = '';
 4594:                         }
 4595:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4596:                             $changes{'loginvia'}{$lonhost} = 1;
 4597:                         }
 4598:                         if ($item eq 'exempt') {
 4599:                             $new =~ s/^\s+//;
 4600:                             $new =~ s/\s+$//;
 4601:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4602:                             my @okips;
 4603:                             foreach my $ip (@poss_ips) {
 4604:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4605:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4606:                                         push(@okips,$ip); 
 4607:                                     }
 4608:                                 }
 4609:                             }
 4610:                             if (@okips > 0) {
 4611:                                 $new = join(',',@okips); 
 4612:                             } else {
 4613:                                 $new = ''; 
 4614:                             }
 4615:                         }
 4616: 
 4617:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4618:                     }
 4619:                 }
 4620:             } else {
 4621:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4622:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4623:                     $changes{'loginvia'}{$lonhost} = 1;
 4624:                     foreach my $item (@loginvia_attribs) {
 4625:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4626:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4627:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4628:                                 $new = '/';
 4629:                             }
 4630:                         }
 4631:                         if (($item eq 'custompath') && 
 4632:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4633:                             $new = '';
 4634:                         }
 4635:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4636:                     }
 4637:                 }
 4638:             }
 4639:         }
 4640:     }
 4641: 
 4642:     my $servadm = $r->dir_config('lonAdmEMail');
 4643:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 4644:     if (ref($domconfig{'login'}) eq 'HASH') {
 4645:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 4646:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 4647:                 if ($lang eq 'nolang') {
 4648:                     push(@currlangs,$lang);
 4649:                 } elsif (defined($langchoices{$lang})) {
 4650:                     push(@currlangs,$lang);
 4651:                 } else {
 4652:                     next;
 4653:                 }
 4654:             }
 4655:         }
 4656:     }
 4657:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 4658:     if (@currlangs > 0) {
 4659:         foreach my $lang (@currlangs) {
 4660:             if (grep(/^\Q$lang\E$/,@delurls)) {
 4661:                 $changes{'helpurl'}{$lang} = 1;
 4662:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 4663:                 $changes{'helpurl'}{$lang} = 1;
 4664:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 4665:                 push(@newlangs,$lang);
 4666:             } else {
 4667:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4668:             }
 4669:         }
 4670:     }
 4671:     unless (grep(/^nolang$/,@currlangs)) {
 4672:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 4673:             $changes{'helpurl'}{'nolang'} = 1;
 4674:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 4675:             push(@newlangs,'nolang');
 4676:         }
 4677:     }
 4678:     if ($env{'form.loginhelpurl_add_lang'}) {
 4679:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 4680:             ($env{'form.loginhelpurl_add_file.filename'})) {
 4681:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 4682:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 4683:         }
 4684:     }
 4685:     if ((@newlangs > 0) || ($addedfile)) {
 4686:         my $error;
 4687:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 4688:         if ($configuserok eq 'ok') {
 4689:             if ($switchserver) {
 4690:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 4691:             } elsif ($author_ok eq 'ok') {
 4692:                 my @allnew = @newlangs;
 4693:                 if ($addedfile ne '') {
 4694:                     push(@allnew,$addedfile);
 4695:                 }
 4696:                 foreach my $lang (@allnew) {
 4697:                     my $formelem = 'loginhelpurl_'.$lang;
 4698:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 4699:                         $formelem = 'loginhelpurl_add_file';
 4700:                     }
 4701:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 4702:                                                                "help/$lang",'','',$newfile{$lang});
 4703:                     if ($result eq 'ok') {
 4704:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 4705:                         $changes{'helpurl'}{$lang} = 1;
 4706:                     } else {
 4707:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 4708:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 4709:                         if ((grep(/^\Q$lang\E$/,@currlangs)) && 
 4710:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 4711: 
 4712:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4713:                         }
 4714:                     }
 4715:                 }
 4716:             } else {
 4717:                 $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);
 4718:             }
 4719:         } else {
 4720:             $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);
 4721:         }
 4722:         if ($error) {
 4723:             &Apache::lonnet::logthis($error);
 4724:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4725:         }
 4726:     }
 4727: 
 4728:     my $defaulthelpfile = '/adm/loginproblems.html';
 4729:     my $defaulttext = &mt('Default in use');
 4730: 
 4731:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4732:                                              $dom);
 4733:     if ($putresult eq 'ok') {
 4734:         my @toggles = ('coursecatalog','adminmail','newuser');
 4735:         my %defaultchecked = (
 4736:                     'coursecatalog' => 'on',
 4737:                     'adminmail'     => 'off',
 4738:                     'newuser'       => 'off',
 4739:         );
 4740:         if (ref($domconfig{'login'}) eq 'HASH') {
 4741:             foreach my $item (@toggles) {
 4742:                 if ($defaultchecked{$item} eq 'on') { 
 4743:                     if (($domconfig{'login'}{$item} eq '0') &&
 4744:                         ($env{'form.'.$item} eq '1')) {
 4745:                         $changes{$item} = 1;
 4746:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4747:                               $domconfig{'login'}{$item} eq '1') &&
 4748:                              ($env{'form.'.$item} eq '0')) {
 4749:                         $changes{$item} = 1;
 4750:                     }
 4751:                 } elsif ($defaultchecked{$item} eq 'off') {
 4752:                     if (($domconfig{'login'}{$item} eq '1') &&
 4753:                         ($env{'form.'.$item} eq '0')) {
 4754:                         $changes{$item} = 1;
 4755:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4756:                               $domconfig{'login'}{$item} eq '0') &&
 4757:                              ($env{'form.'.$item} eq '1')) {
 4758:                         $changes{$item} = 1;
 4759:                     }
 4760:                 }
 4761:             }
 4762:         }
 4763:         if (keys(%changes) > 0 || $colchgtext) {
 4764:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4765:             $resulttext = &mt('Changes made:').'<ul>';
 4766:             foreach my $item (sort(keys(%changes))) {
 4767:                 if ($item eq 'loginvia') {
 4768:                     if (ref($changes{$item}) eq 'HASH') {
 4769:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4770:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4771:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4772:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4773:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4774:                                     $protocol = 'http' if ($protocol ne 'https');
 4775:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4776: 
 4777:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4778:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4779:                                     } else {
 4780:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4781:                                     }
 4782:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4783:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4784:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4785:                                     }
 4786:                                     $resulttext .= '</li>';
 4787:                                 } else {
 4788:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4789:                                 }
 4790:                             } else {
 4791:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4792:                             }
 4793:                         }
 4794:                         $resulttext .= '</ul></li>';
 4795:                     }
 4796:                 } elsif ($item eq 'helpurl') {
 4797:                     if (ref($changes{$item}) eq 'HASH') {
 4798:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 4799:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 4800:                                 my ($chg,$link);
 4801:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 4802:                                 if ($lang eq 'nolang') {
 4803:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 4804:                                 } else {
 4805:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 4806:                                 }
 4807:                                 $resulttext .= '<li>'.$chg.'</li>';
 4808:                             } else {
 4809:                                 my $chg;
 4810:                                 if ($lang eq 'nolang') {
 4811:                                     $chg = &mt('custom log-in help file for no preferred language');
 4812:                                 } else {
 4813:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 4814:                                 }
 4815:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 4816:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 4817:                                                       '?inhibitmenu=yes',$chg,600,500).
 4818:                                                '</li>';
 4819:                             }
 4820:                         }
 4821:                     }
 4822:                 } else {
 4823:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4824:                 }
 4825:             }
 4826:             $resulttext .= $colchgtext.'</ul>';
 4827:         } else {
 4828:             $resulttext = &mt('No changes made to log-in page settings');
 4829:         }
 4830:     } else {
 4831:         $resulttext = '<span class="LC_error">'.
 4832: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4833:     }
 4834:     if ($errors) {
 4835:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 4836:                        $errors.'</ul>';
 4837:     }
 4838:     return $resulttext;
 4839: }
 4840: 
 4841: sub color_font_choices {
 4842:     my %choices =
 4843:         &Apache::lonlocal::texthash (
 4844:             img => "Header",
 4845:             bgs => "Background colors",
 4846:             links => "Link colors",
 4847:             images => "Images",
 4848:             font => "Font color",
 4849:             fontmenu => "Font Menu",
 4850:             pgbg => "Page",
 4851:             tabbg => "Header",
 4852:             sidebg => "Border",
 4853:             link => "Link",
 4854:             alink => "Active link",
 4855:             vlink => "Visited link",
 4856:         );
 4857:     return %choices;
 4858: }
 4859: 
 4860: sub modify_rolecolors {
 4861:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 4862:     my ($resulttext,%rolehash);
 4863:     $rolehash{'rolecolors'} = {};
 4864:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 4865:         if ($domconfig{'rolecolors'} eq '') {
 4866:             $domconfig{'rolecolors'} = {};
 4867:         }
 4868:     }
 4869:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 4870:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 4871:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 4872:                                              $dom);
 4873:     if ($putresult eq 'ok') {
 4874:         if (keys(%changes) > 0) {
 4875:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4876:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 4877:                                              $rolehash{'rolecolors'});
 4878:         } else {
 4879:             $resulttext = &mt('No changes made to default color schemes');
 4880:         }
 4881:     } else {
 4882:         $resulttext = '<span class="LC_error">'.
 4883: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4884:     }
 4885:     if ($errors) {
 4886:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 4887:                        $errors.'</ul>';
 4888:     }
 4889:     return $resulttext;
 4890: }
 4891: 
 4892: sub modify_colors {
 4893:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 4894:     my (%changes,%choices);
 4895:     my @bgs;
 4896:     my @links = ('link','alink','vlink');
 4897:     my @logintext;
 4898:     my @images;
 4899:     my $servadm = $r->dir_config('lonAdmEMail');
 4900:     my $errors;
 4901:     foreach my $role (@{$roles}) {
 4902:         if ($role eq 'login') {
 4903:             %choices = &login_choices();
 4904:             @logintext = ('textcol','bgcol');
 4905:         } else {
 4906:             %choices = &color_font_choices();
 4907:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 4908:         }
 4909:         if ($role eq 'login') {
 4910:             @images = ('img','logo','domlogo','login');
 4911:             @bgs = ('pgbg','mainbg','sidebg');
 4912:         } else {
 4913:             @images = ('img');
 4914:             @bgs = ('pgbg','tabbg','sidebg'); 
 4915:         }
 4916:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 4917:         foreach my $item (@bgs,@links,@logintext) {
 4918:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 4919:         }
 4920:         my ($configuserok,$author_ok,$switchserver) = 
 4921:             &config_check($dom,$confname,$servadm);
 4922:         my ($width,$height) = &thumb_dimensions();
 4923:         if (ref($domconfig->{$role}) ne 'HASH') {
 4924:             $domconfig->{$role} = {};
 4925:         }
 4926:         foreach my $img (@images) {
 4927:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 4928:                 if (defined($env{'form.login_showlogo_'.$img})) {
 4929:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 4930:                 } else { 
 4931:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 4932:                 }
 4933:             } 
 4934: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 4935: 		 && !defined($domconfig->{$role}{$img})
 4936: 		 && !$env{'form.'.$role.'_del_'.$img}
 4937: 		 && $env{'form.'.$role.'_import_'.$img}) {
 4938: 		# import the old configured image from the .tab setting
 4939: 		# if they haven't provided a new one 
 4940: 		$domconfig->{$role}{$img} = 
 4941: 		    $env{'form.'.$role.'_import_'.$img};
 4942: 	    }
 4943:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 4944:                 my $error;
 4945:                 if ($configuserok eq 'ok') {
 4946:                     if ($switchserver) {
 4947:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 4948:                     } else {
 4949:                         if ($author_ok eq 'ok') {
 4950:                             my ($result,$logourl) = 
 4951:                                 &publishlogo($r,'upload',$role.'_'.$img,
 4952:                                            $dom,$confname,$img,$width,$height);
 4953:                             if ($result eq 'ok') {
 4954:                                 $confhash->{$role}{$img} = $logourl;
 4955:                                 $changes{$role}{'images'}{$img} = 1;
 4956:                             } else {
 4957:                                 $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);
 4958:                             }
 4959:                         } else {
 4960:                             $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);
 4961:                         }
 4962:                     }
 4963:                 } else {
 4964:                     $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);
 4965:                 }
 4966:                 if ($error) {
 4967:                     &Apache::lonnet::logthis($error);
 4968:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4969:                 }
 4970:             } elsif ($domconfig->{$role}{$img} ne '') {
 4971:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 4972:                     my $error;
 4973:                     if ($configuserok eq 'ok') {
 4974: # is confname an author?
 4975:                         if ($switchserver eq '') {
 4976:                             if ($author_ok eq 'ok') {
 4977:                                 my ($result,$logourl) = 
 4978:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 4979:                                             $dom,$confname,$img,$width,$height);
 4980:                                 if ($result eq 'ok') {
 4981:                                     $confhash->{$role}{$img} = $logourl;
 4982: 				    $changes{$role}{'images'}{$img} = 1;
 4983:                                 }
 4984:                             }
 4985:                         }
 4986:                     }
 4987:                 }
 4988:             }
 4989:         }
 4990:         if (ref($domconfig) eq 'HASH') {
 4991:             if (ref($domconfig->{$role}) eq 'HASH') {
 4992:                 foreach my $img (@images) {
 4993:                     if ($domconfig->{$role}{$img} ne '') {
 4994:                         if ($env{'form.'.$role.'_del_'.$img}) {
 4995:                             $confhash->{$role}{$img} = '';
 4996:                             $changes{$role}{'images'}{$img} = 1;
 4997:                         } else {
 4998:                             if ($confhash->{$role}{$img} eq '') {
 4999:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5000:                             }
 5001:                         }
 5002:                     } else {
 5003:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5004:                             $confhash->{$role}{$img} = '';
 5005:                             $changes{$role}{'images'}{$img} = 1;
 5006:                         } 
 5007:                     }
 5008:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5009:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5010:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5011:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5012:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5013:                             }
 5014:                         } else {
 5015:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5016:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5017:                             }
 5018:                         }
 5019:                     }
 5020:                 }
 5021:                 if ($domconfig->{$role}{'font'} ne '') {
 5022:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5023:                         $changes{$role}{'font'} = 1;
 5024:                     }
 5025:                 } else {
 5026:                     if ($confhash->{$role}{'font'}) {
 5027:                         $changes{$role}{'font'} = 1;
 5028:                     }
 5029:                 }
 5030:                 if ($role ne 'login') {
 5031:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5032:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5033:                             $changes{$role}{'fontmenu'} = 1;
 5034:                         }
 5035:                     } else {
 5036:                         if ($confhash->{$role}{'fontmenu'}) {
 5037:                             $changes{$role}{'fontmenu'} = 1;
 5038:                         }
 5039:                     }
 5040:                 }
 5041:                 foreach my $item (@bgs) {
 5042:                     if ($domconfig->{$role}{$item} ne '') {
 5043:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5044:                             $changes{$role}{'bgs'}{$item} = 1;
 5045:                         } 
 5046:                     } else {
 5047:                         if ($confhash->{$role}{$item}) {
 5048:                             $changes{$role}{'bgs'}{$item} = 1;
 5049:                         }
 5050:                     }
 5051:                 }
 5052:                 foreach my $item (@links) {
 5053:                     if ($domconfig->{$role}{$item} ne '') {
 5054:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5055:                             $changes{$role}{'links'}{$item} = 1;
 5056:                         }
 5057:                     } else {
 5058:                         if ($confhash->{$role}{$item}) {
 5059:                             $changes{$role}{'links'}{$item} = 1;
 5060:                         }
 5061:                     }
 5062:                 }
 5063:                 foreach my $item (@logintext) {
 5064:                     if ($domconfig->{$role}{$item} ne '') {
 5065:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5066:                             $changes{$role}{'logintext'}{$item} = 1;
 5067:                         }
 5068:                     } else {
 5069:                         if ($confhash->{$role}{$item}) {
 5070:                             $changes{$role}{'logintext'}{$item} = 1;
 5071:                         }
 5072:                     }
 5073:                 }
 5074:             } else {
 5075:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5076:                                         \@logintext,$confhash,\%changes); 
 5077:             }
 5078:         } else {
 5079:             &default_change_checker($role,\@images,\@links,\@bgs,
 5080:                                     \@logintext,$confhash,\%changes); 
 5081:         }
 5082:     }
 5083:     return ($errors,%changes);
 5084: }
 5085: 
 5086: sub config_check {
 5087:     my ($dom,$confname,$servadm) = @_;
 5088:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5089:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5090:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5091:                                                    $confname,$servadm);
 5092:     if ($configuserok eq 'ok') {
 5093:         $switchserver = &check_switchserver($dom,$confname);
 5094:         if ($switchserver eq '') {
 5095:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5096:         }
 5097:     }
 5098:     return ($configuserok,$author_ok,$switchserver);
 5099: }
 5100: 
 5101: sub default_change_checker {
 5102:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5103:     foreach my $item (@{$links}) {
 5104:         if ($confhash->{$role}{$item}) {
 5105:             $changes->{$role}{'links'}{$item} = 1;
 5106:         }
 5107:     }
 5108:     foreach my $item (@{$bgs}) {
 5109:         if ($confhash->{$role}{$item}) {
 5110:             $changes->{$role}{'bgs'}{$item} = 1;
 5111:         }
 5112:     }
 5113:     foreach my $item (@{$logintext}) {
 5114:         if ($confhash->{$role}{$item}) {
 5115:             $changes->{$role}{'logintext'}{$item} = 1;
 5116:         }
 5117:     }
 5118:     foreach my $img (@{$images}) {
 5119:         if ($env{'form.'.$role.'_del_'.$img}) {
 5120:             $confhash->{$role}{$img} = '';
 5121:             $changes->{$role}{'images'}{$img} = 1;
 5122:         }
 5123:         if ($role eq 'login') {
 5124:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5125:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5126:             }
 5127:         }
 5128:     }
 5129:     if ($confhash->{$role}{'font'}) {
 5130:         $changes->{$role}{'font'} = 1;
 5131:     }
 5132: }
 5133: 
 5134: sub display_colorchgs {
 5135:     my ($dom,$changes,$roles,$confhash) = @_;
 5136:     my (%choices,$resulttext);
 5137:     if (!grep(/^login$/,@{$roles})) {
 5138:         $resulttext = &mt('Changes made:').'<br />';
 5139:     }
 5140:     foreach my $role (@{$roles}) {
 5141:         if ($role eq 'login') {
 5142:             %choices = &login_choices();
 5143:         } else {
 5144:             %choices = &color_font_choices();
 5145:         }
 5146:         if (ref($changes->{$role}) eq 'HASH') {
 5147:             if ($role ne 'login') {
 5148:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5149:             }
 5150:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5151:                 if ($role ne 'login') {
 5152:                     $resulttext .= '<ul>';
 5153:                 }
 5154:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5155:                     if ($role ne 'login') {
 5156:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5157:                     }
 5158:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5159:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5160:                             if ($confhash->{$role}{$key}{$item}) {
 5161:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5162:                             } else {
 5163:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5164:                             }
 5165:                         } elsif ($confhash->{$role}{$item} eq '') {
 5166:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5167:                         } else {
 5168:                             my $newitem = $confhash->{$role}{$item};
 5169:                             if ($key eq 'images') {
 5170:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5171:                             }
 5172:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5173:                         }
 5174:                     }
 5175:                     if ($role ne 'login') {
 5176:                         $resulttext .= '</ul></li>';
 5177:                     }
 5178:                 } else {
 5179:                     if ($confhash->{$role}{$key} eq '') {
 5180:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5181:                     } else {
 5182:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5183:                     }
 5184:                 }
 5185:                 if ($role ne 'login') {
 5186:                     $resulttext .= '</ul>';
 5187:                 }
 5188:             }
 5189:         }
 5190:     }
 5191:     return $resulttext;
 5192: }
 5193: 
 5194: sub thumb_dimensions {
 5195:     return ('200','50');
 5196: }
 5197: 
 5198: sub check_dimensions {
 5199:     my ($inputfile) = @_;
 5200:     my ($fullwidth,$fullheight);
 5201:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5202:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5203:             my $imageinfo = <PIPE>;
 5204:             if (!close(PIPE)) {
 5205:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5206:             }
 5207:             chomp($imageinfo);
 5208:             my ($fullsize) = 
 5209:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5210:             if ($fullsize) {
 5211:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5212:             }
 5213:         }
 5214:     }
 5215:     return ($fullwidth,$fullheight);
 5216: }
 5217: 
 5218: sub check_configuser {
 5219:     my ($uhome,$dom,$confname,$servadm) = @_;
 5220:     my ($configuserok,%currroles);
 5221:     if ($uhome eq 'no_host') {
 5222:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5223:         my $configpass = &LONCAPA::Enrollment::create_password();
 5224:         $configuserok = 
 5225:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5226:                              $configpass,'','','','','',undef,$servadm);
 5227:     } else {
 5228:         $configuserok = 'ok';
 5229:         %currroles = 
 5230:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5231:     }
 5232:     return ($configuserok,%currroles);
 5233: }
 5234: 
 5235: sub check_authorstatus {
 5236:     my ($dom,$confname,%currroles) = @_;
 5237:     my $author_ok;
 5238:     if (!$currroles{':'.$dom.':au'}) {
 5239:         my $start = time;
 5240:         my $end = 0;
 5241:         $author_ok = 
 5242:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5243:                                         'au',$end,$start,'','','domconfig');
 5244:     } else {
 5245:         $author_ok = 'ok';
 5246:     }
 5247:     return $author_ok;
 5248: }
 5249: 
 5250: sub publishlogo {
 5251:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5252:     my ($output,$fname,$logourl);
 5253:     if ($action eq 'upload') {
 5254:         $fname=$env{'form.'.$formname.'.filename'};
 5255:         chop($env{'form.'.$formname});
 5256:     } else {
 5257:         ($fname) = ($formname =~ /([^\/]+)$/);
 5258:     }
 5259:     if ($savefileas ne '') {
 5260:         $fname = $savefileas;
 5261:     }
 5262:     $fname=&Apache::lonnet::clean_filename($fname);
 5263: # See if there is anything left
 5264:     unless ($fname) { return ('error: no uploaded file'); }
 5265:     $fname="$subdir/$fname";
 5266:     my $docroot=$r->dir_config('lonDocRoot'); 
 5267:     my $filepath="$docroot/priv";
 5268:     my $relpath = "$dom/$confname";
 5269:     my ($fnamepath,$file,$fetchthumb);
 5270:     $file=$fname;
 5271:     if ($fname=~m|/|) {
 5272:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5273:     }
 5274:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5275:     my $count;
 5276:     for ($count=5;$count<=$#parts;$count++) {
 5277:         $filepath.="/$parts[$count]";
 5278:         if ((-e $filepath)!=1) {
 5279:             mkdir($filepath,02770);
 5280:         }
 5281:     }
 5282:     # Check for bad extension and disallow upload
 5283:     if ($file=~/\.(\w+)$/ &&
 5284:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5285:         $output = 
 5286:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 5287:     } elsif ($file=~/\.(\w+)$/ &&
 5288:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5289:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5290:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5291:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5292:     } elsif (-d "$filepath/$file") {
 5293:         $output = &mt('File name is a directory name - rename the file and re-upload');
 5294:     } else {
 5295:         my $source = $filepath.'/'.$file;
 5296:         my $logfile;
 5297:         if (!open($logfile,">>$source".'.log')) {
 5298:             return (&mt('No write permission to Construction Space'));
 5299:         }
 5300:         print $logfile
 5301: "\n================= Publish ".localtime()." ================\n".
 5302: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5303: # Save the file
 5304:         if (!open(FH,'>'.$source)) {
 5305:             &Apache::lonnet::logthis('Failed to create '.$source);
 5306:             return (&mt('Failed to create file'));
 5307:         }
 5308:         if ($action eq 'upload') {
 5309:             if (!print FH ($env{'form.'.$formname})) {
 5310:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5311:                 return (&mt('Failed to write file'));
 5312:             }
 5313:         } else {
 5314:             my $original = &Apache::lonnet::filelocation('',$formname);
 5315:             if(!copy($original,$source)) {
 5316:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5317:                 return (&mt('Failed to write file'));
 5318:             }
 5319:         }
 5320:         close(FH);
 5321:         chmod(0660, $source); # Permissions to rw-rw---.
 5322: 
 5323:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5324:         my $copyfile=$targetdir.'/'.$file;
 5325: 
 5326:         my @parts=split(/\//,$targetdir);
 5327:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5328:         for (my $count=5;$count<=$#parts;$count++) {
 5329:             $path.="/$parts[$count]";
 5330:             if (!-e $path) {
 5331:                 print $logfile "\nCreating directory ".$path;
 5332:                 mkdir($path,02770);
 5333:             }
 5334:         }
 5335:         my $versionresult;
 5336:         if (-e $copyfile) {
 5337:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5338:         } else {
 5339:             $versionresult = 'ok';
 5340:         }
 5341:         if ($versionresult eq 'ok') {
 5342:             if (copy($source,$copyfile)) {
 5343:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5344:                 $output = 'ok';
 5345:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5346:                 push(@{$modified_urls},[$copyfile,$source]);
 5347:                 my $metaoutput = 
 5348:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5349:                 unless ($registered_cleanup) {
 5350:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5351:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5352:                     $registered_cleanup=1;
 5353:                 }
 5354:             } else {
 5355:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5356:                 $output = &mt('Failed to copy file to RES space').", $!";
 5357:             }
 5358:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5359:                 my $inputfile = $filepath.'/'.$file;
 5360:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5361:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5362:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5363:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5364:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5365:                         system("convert -sample $thumbsize $inputfile $outfile");
 5366:                         chmod(0660, $filepath.'/tn-'.$file);
 5367:                         if (-e $outfile) {
 5368:                             my $copyfile=$targetdir.'/tn-'.$file;
 5369:                             if (copy($outfile,$copyfile)) {
 5370:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5371:                                 my $thumb_metaoutput = 
 5372:                                     &write_metadata($dom,$confname,$formname,
 5373:                                                     $targetdir,'tn-'.$file,$logfile);
 5374:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5375:                                 unless ($registered_cleanup) {
 5376:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5377:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5378:                                     $registered_cleanup=1;
 5379:                                 }
 5380:                             } else {
 5381:                                 print $logfile "\nUnable to write ".$copyfile.
 5382:                                                ':'.$!."\n";
 5383:                             }
 5384:                         }
 5385:                     }
 5386:                 }
 5387:             }
 5388:         } else {
 5389:             $output = $versionresult;
 5390:         }
 5391:     }
 5392:     return ($output,$logourl);
 5393: }
 5394: 
 5395: sub logo_versioning {
 5396:     my ($targetdir,$file,$logfile) = @_;
 5397:     my $target = $targetdir.'/'.$file;
 5398:     my ($maxversion,$fn,$extn,$output);
 5399:     $maxversion = 0;
 5400:     if ($file =~ /^(.+)\.(\w+)$/) {
 5401:         $fn=$1;
 5402:         $extn=$2;
 5403:     }
 5404:     opendir(DIR,$targetdir);
 5405:     while (my $filename=readdir(DIR)) {
 5406:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5407:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5408:         }
 5409:     }
 5410:     $maxversion++;
 5411:     print $logfile "\nCreating old version ".$maxversion."\n";
 5412:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5413:     if (copy($target,$copyfile)) {
 5414:         print $logfile "Copied old target to ".$copyfile."\n";
 5415:         $copyfile=$copyfile.'.meta';
 5416:         if (copy($target.'.meta',$copyfile)) {
 5417:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5418:             $output = 'ok';
 5419:         } else {
 5420:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5421:             $output = &mt('Failed to copy old meta').", $!, ";
 5422:         }
 5423:     } else {
 5424:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5425:         $output = &mt('Failed to copy old target').", $!, ";
 5426:     }
 5427:     return $output;
 5428: }
 5429: 
 5430: sub write_metadata {
 5431:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5432:     my (%metadatafields,%metadatakeys,$output);
 5433:     $metadatafields{'title'}=$formname;
 5434:     $metadatafields{'creationdate'}=time;
 5435:     $metadatafields{'lastrevisiondate'}=time;
 5436:     $metadatafields{'copyright'}='public';
 5437:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5438:                                          $env{'user.domain'};
 5439:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5440:     $metadatafields{'domain'}=$dom;
 5441:     {
 5442:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5443:         my $mfh;
 5444:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5445:             foreach (sort keys %metadatafields) {
 5446:                 unless ($_=~/\./) {
 5447:                     my $unikey=$_;
 5448:                     $unikey=~/^([A-Za-z]+)/;
 5449:                     my $tag=$1;
 5450:                     $tag=~tr/A-Z/a-z/;
 5451:                     print $mfh "\n\<$tag";
 5452:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5453:                         my $value=$metadatafields{$unikey.'.'.$_};
 5454:                         $value=~s/\"/\'\'/g;
 5455:                         print $mfh ' '.$_.'="'.$value.'"';
 5456:                     }
 5457:                     print $mfh '>'.
 5458:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5459:                             .'</'.$tag.'>';
 5460:                 }
 5461:             }
 5462:             $output = 'ok';
 5463:             print $logfile "\nWrote metadata";
 5464:             close($mfh);
 5465:         } else {
 5466:             print $logfile "\nFailed to open metadata file";
 5467:             $output = &mt('Could not write metadata');
 5468:         }
 5469:     }
 5470:     return $output;
 5471: }
 5472: 
 5473: sub notifysubscribed {
 5474:     foreach my $targetsource (@{$modified_urls}){
 5475:         next unless (ref($targetsource) eq 'ARRAY');
 5476:         my ($target,$source)=@{$targetsource};
 5477:         if ($source ne '') {
 5478:             if (open(my $logfh,'>>'.$source.'.log')) {
 5479:                 print $logfh "\nCleanup phase: Notifications\n";
 5480:                 my @subscribed=&subscribed_hosts($target);
 5481:                 foreach my $subhost (@subscribed) {
 5482:                     print $logfh "\nNotifying host ".$subhost.':';
 5483:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5484:                     print $logfh $reply;
 5485:                 }
 5486:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5487:                 foreach my $subhost (@subscribedmeta) {
 5488:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5489:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5490:                                                         $subhost);
 5491:                     print $logfh $reply;
 5492:                 }
 5493:                 print $logfh "\n============ Done ============\n";
 5494:                 close($logfh);
 5495:             }
 5496:         }
 5497:     }
 5498:     return OK;
 5499: }
 5500: 
 5501: sub subscribed_hosts {
 5502:     my ($target) = @_;
 5503:     my @subscribed;
 5504:     if (open(my $fh,"<$target.subscription")) {
 5505:         while (my $subline=<$fh>) {
 5506:             if ($subline =~ /^($match_lonid):/) {
 5507:                 my $host = $1;
 5508:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5509:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5510:                         push(@subscribed,$host);
 5511:                     }
 5512:                 }
 5513:             }
 5514:         }
 5515:     }
 5516:     return @subscribed;
 5517: }
 5518: 
 5519: sub check_switchserver {
 5520:     my ($dom,$confname) = @_;
 5521:     my ($allowed,$switchserver);
 5522:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5523:     if ($home eq 'no_host') {
 5524:         $home = &Apache::lonnet::domain($dom,'primary');
 5525:     }
 5526:     my @ids=&Apache::lonnet::current_machine_ids();
 5527:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5528:     if (!$allowed) {
 5529: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&role=dc./'.$dom.'/">'.&mt('Switch Server').'</a>';
 5530:     }
 5531:     return $switchserver;
 5532: }
 5533: 
 5534: sub modify_quotas {
 5535:     my ($dom,$action,%domconfig) = @_;
 5536:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5537:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5538:     if ($action eq 'quotas') {
 5539:         $context = 'tools'; 
 5540:     } else {
 5541:         $context = $action;
 5542:     }
 5543:     if ($context eq 'requestcourses') {
 5544:         @usertools = ('official','unofficial','community');
 5545:         @options =('norequest','approval','validate','autolimit');
 5546:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5547:         %titles = &courserequest_titles();
 5548:         $toolregexp = join('|',@usertools);
 5549:         %conditions = &courserequest_conditions();
 5550:     } elsif ($context eq 'requestauthor') {
 5551:         @usertools = ('author');
 5552:         %titles = &authorrequest_titles();
 5553:     } else {
 5554:         @usertools = ('aboutme','blog','webdav','portfolio');
 5555:         %titles = &tool_titles();
 5556:     }
 5557:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5558:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5559:     foreach my $key (keys(%env)) {
 5560:         if ($context eq 'requestcourses') {
 5561:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5562:                 my $item = $1;
 5563:                 my $type = $2;
 5564:                 if ($type =~ /^limit_(.+)/) {
 5565:                     $limithash{$item}{$1} = $env{$key};
 5566:                 } else {
 5567:                     $confhash{$item}{$type} = $env{$key};
 5568:                 }
 5569:             }
 5570:         } elsif ($context eq 'requestauthor') {
 5571:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5572:                 $confhash{$1} = $env{$key};
 5573:             }
 5574:         } else {
 5575:             if ($key =~ /^form\.quota_(.+)$/) {
 5576:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5577:             }
 5578:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5579:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5580:             }
 5581:         }
 5582:     }
 5583:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5584:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5585:         @approvalnotify = sort(@approvalnotify);
 5586:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5587:         if (ref($domconfig{$action}) eq 'HASH') {
 5588:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5589:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5590:                     $changes{'notify'}{'approval'} = 1;
 5591:                 }
 5592:             } else {
 5593:                 if ($confhash{'notify'}{'approval'}) {
 5594:                     $changes{'notify'}{'approval'} = 1;
 5595:                 }
 5596:             }
 5597:         } else {
 5598:             if ($confhash{'notify'}{'approval'}) {
 5599:                 $changes{'notify'}{'approval'} = 1;
 5600:             }
 5601:         }
 5602:     } else {
 5603:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5604:     }
 5605:     foreach my $item (@usertools) {
 5606:         foreach my $type (@{$types},'default','_LC_adv') {
 5607:             my $unset; 
 5608:             if ($context eq 'requestcourses') {
 5609:                 $unset = '0';
 5610:                 if ($type eq '_LC_adv') {
 5611:                     $unset = '';
 5612:                 }
 5613:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5614:                     $confhash{$item}{$type} .= '=';
 5615:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5616:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5617:                     }
 5618:                 }
 5619:             } elsif ($context eq 'requestauthor') {
 5620:                 $unset = '0';
 5621:                 if ($type eq '_LC_adv') {
 5622:                     $unset = '';
 5623:                 }
 5624:             } else {
 5625:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5626:                     $confhash{$item}{$type} = 1;
 5627:                 } else {
 5628:                     $confhash{$item}{$type} = 0;
 5629:                 }
 5630:             }
 5631:             if (ref($domconfig{$action}) eq 'HASH') {
 5632:                 if ($action eq 'requestauthor') {
 5633:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5634:                         $changes{$type} = 1;
 5635:                     }
 5636:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5637:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5638:                         $changes{$item}{$type} = 1;
 5639:                     }
 5640:                 } else {
 5641:                     if ($context eq 'requestcourses') {
 5642:                         if ($confhash{$item}{$type} ne $unset) {
 5643:                             $changes{$item}{$type} = 1;
 5644:                         }
 5645:                     } else {
 5646:                         if (!$confhash{$item}{$type}) {
 5647:                             $changes{$item}{$type} = 1;
 5648:                         }
 5649:                     }
 5650:                 }
 5651:             } else {
 5652:                 if ($context eq 'requestcourses') {
 5653:                     if ($confhash{$item}{$type} ne $unset) {
 5654:                         $changes{$item}{$type} = 1;
 5655:                     }
 5656:                 } elsif ($context eq 'requestauthor') {
 5657:                     if ($confhash{$type} ne $unset) {
 5658:                         $changes{$type} = 1;
 5659:                     }
 5660:                 } else {
 5661:                     if (!$confhash{$item}{$type}) {
 5662:                         $changes{$item}{$type} = 1;
 5663:                     }
 5664:                 }
 5665:             }
 5666:         }
 5667:     }
 5668:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5669:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5670:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5671:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5672:                     if (exists($confhash{'defaultquota'}{$key})) {
 5673:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5674:                             $changes{'defaultquota'}{$key} = 1;
 5675:                         }
 5676:                     } else {
 5677:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5678:                     }
 5679:                 }
 5680:             } else {
 5681:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5682:                     if (exists($confhash{'defaultquota'}{$key})) {
 5683:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5684:                             $changes{'defaultquota'}{$key} = 1;
 5685:                         }
 5686:                     } else {
 5687:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5688:                     }
 5689:                 }
 5690:             }
 5691:         }
 5692:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5693:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5694:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5695:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5696:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5697:                             $changes{'defaultquota'}{$key} = 1;
 5698:                         }
 5699:                     } else {
 5700:                         if (!exists($domconfig{'quotas'}{$key})) {
 5701:                             $changes{'defaultquota'}{$key} = 1;
 5702:                         }
 5703:                     }
 5704:                 } else {
 5705:                     $changes{'defaultquota'}{$key} = 1;
 5706:                 }
 5707:             }
 5708:         }
 5709:     }
 5710: 
 5711:     if ($context eq 'requestauthor') {
 5712:         $domdefaults{'requestauthor'} = \%confhash;
 5713:     } else {
 5714:         foreach my $key (keys(%confhash)) {
 5715:             $domdefaults{$key} = $confhash{$key};
 5716:         }
 5717:     }
 5718: 
 5719:     my %quotahash = (
 5720:                       $action => { %confhash }
 5721:                     );
 5722:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5723:                                              $dom);
 5724:     if ($putresult eq 'ok') {
 5725:         if (keys(%changes) > 0) {
 5726:             my $cachetime = 24*60*60;
 5727:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5728: 
 5729:             $resulttext = &mt('Changes made:').'<ul>';
 5730:             unless (($context eq 'requestcourses') || 
 5731:                     ($context eq 'requestauthor')) {
 5732:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5733:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5734:                     foreach my $type (@{$types},'default') {
 5735:                         if (defined($changes{'defaultquota'}{$type})) {
 5736:                             my $typetitle = $usertypes->{$type};
 5737:                             if ($type eq 'default') {
 5738:                                 $typetitle = $othertitle;
 5739:                             }
 5740:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5741:                         }
 5742:                     }
 5743:                     $resulttext .= '</ul></li>';
 5744:                 }
 5745:             }
 5746:             my %newenv;
 5747:             foreach my $item (@usertools) {
 5748:                 my (%haschgs,%inconf);
 5749:                 if ($context eq 'requestauthor') {
 5750:                     %haschgs = %changes;
 5751:                     %inconf = %confhash; 
 5752:                 } else {
 5753:                     if (ref($changes{$item}) eq 'HASH') {
 5754:                         %haschgs = %{$changes{$item}};
 5755:                     }
 5756:                     if (ref($confhash{$item}) eq 'HASH') {
 5757:                         %inconf = %{$confhash{$item}};
 5758:                     }
 5759:                 }
 5760:                 if (keys(%haschgs) > 0) {
 5761:                     my $newacc = 
 5762:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5763:                                                           $env{'user.domain'},
 5764:                                                           $item,'reload',$context);
 5765:                     if (($context eq 'requestcourses') || 
 5766:                         ($context eq 'requestauthor')) {
 5767:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5768:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5769:                         }
 5770:                     } else {
 5771:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5772:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5773:                         }
 5774:                     }
 5775:                     unless ($context eq 'requestauthor') {
 5776:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5777:                     }
 5778:                     foreach my $type (@{$types},'default','_LC_adv') {
 5779:                         if ($haschgs{$type}) {
 5780:                             my $typetitle = $usertypes->{$type};
 5781:                             if ($type eq 'default') {
 5782:                                 $typetitle = $othertitle;
 5783:                             } elsif ($type eq '_LC_adv') {
 5784:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5785:                             }
 5786:                             if ($inconf{$type}) {
 5787:                                 if ($context eq 'requestcourses') {
 5788:                                     my $cond;
 5789:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 5790:                                         if ($1 eq '') {
 5791:                                             $cond = &mt('(Automatic processing of any request).');
 5792:                                         } else {
 5793:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5794:                                         }
 5795:                                     } else { 
 5796:                                         $cond = $conditions{$inconf{$type}};
 5797:                                     }
 5798:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5799:                                 } else {
 5800:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5801:                                 }
 5802:                             } else {
 5803:                                 if ($type eq '_LC_adv') {
 5804:                                     if ($inconf{$type} eq '0') {
 5805:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5806:                                     } else { 
 5807:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5808:                                     }
 5809:                                 } else {
 5810:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5811:                                 }
 5812:                             }
 5813:                         }
 5814:                     }
 5815:                     unless ($context eq 'requestauthor') {
 5816:                         $resulttext .= '</ul></li>';
 5817:                     }
 5818:                 }
 5819:             }
 5820:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 5821:                 if (ref($changes{'notify'}) eq 'HASH') {
 5822:                     if ($changes{'notify'}{'approval'}) {
 5823:                         if (ref($confhash{'notify'}) eq 'HASH') {
 5824:                             if ($confhash{'notify'}{'approval'}) {
 5825:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 5826:                             } else {
 5827:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 5828:                             }
 5829:                         }
 5830:                     }
 5831:                 }
 5832:             }
 5833:             $resulttext .= '</ul>';
 5834:             if (keys(%newenv)) {
 5835:                 &Apache::lonnet::appenv(\%newenv);
 5836:             }
 5837:         } else {
 5838:             if ($context eq 'requestcourses') {
 5839:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 5840:             } elsif ($context eq 'requestauthor') {
 5841:                 $resulttext = &mt('No changes made to rights to request author space.');
 5842:             } else {
 5843:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 5844:             }
 5845:         }
 5846:     } else {
 5847:         $resulttext = '<span class="LC_error">'.
 5848: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5849:     }
 5850:     return $resulttext;
 5851: }
 5852: 
 5853: sub modify_autoenroll {
 5854:     my ($dom,%domconfig) = @_;
 5855:     my ($resulttext,%changes);
 5856:     my %currautoenroll;
 5857:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 5858:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 5859:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 5860:         }
 5861:     }
 5862:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 5863:     my %title = ( run => 'Auto-enrollment active',
 5864:                   sender => 'Sender for notification messages',
 5865:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 5866:     my @offon = ('off','on');
 5867:     my $sender_uname = $env{'form.sender_uname'};
 5868:     my $sender_domain = $env{'form.sender_domain'};
 5869:     if ($sender_domain eq '') {
 5870:         $sender_uname = '';
 5871:     } elsif ($sender_uname eq '') {
 5872:         $sender_domain = '';
 5873:     }
 5874:     my $coowners = $env{'form.autoassign_coowners'};
 5875:     my %autoenrollhash =  (
 5876:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 5877:                                        'sender_uname' => $sender_uname,
 5878:                                        'sender_domain' => $sender_domain,
 5879:                                        'co-owners' => $coowners,
 5880:                                 }
 5881:                      );
 5882:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 5883:                                              $dom);
 5884:     if ($putresult eq 'ok') {
 5885:         if (exists($currautoenroll{'run'})) {
 5886:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 5887:                  $changes{'run'} = 1;
 5888:              }
 5889:         } elsif ($autorun) {
 5890:             if ($env{'form.autoenroll_run'} ne '1') {
 5891:                  $changes{'run'} = 1;
 5892:             }
 5893:         }
 5894:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 5895:             $changes{'sender'} = 1;
 5896:         }
 5897:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 5898:             $changes{'sender'} = 1;
 5899:         }
 5900:         if ($currautoenroll{'co-owners'} ne '') {
 5901:             if ($currautoenroll{'co-owners'} ne $coowners) {
 5902:                 $changes{'coowners'} = 1;
 5903:             }
 5904:         } elsif ($coowners) {
 5905:             $changes{'coowners'} = 1;
 5906:         }      
 5907:         if (keys(%changes) > 0) {
 5908:             $resulttext = &mt('Changes made:').'<ul>';
 5909:             if ($changes{'run'}) {
 5910:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 5911:             }
 5912:             if ($changes{'sender'}) {
 5913:                 if ($sender_uname eq '' || $sender_domain eq '') {
 5914:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 5915:                 } else {
 5916:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 5917:                 }
 5918:             }
 5919:             if ($changes{'coowners'}) {
 5920:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 5921:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 5922:             }
 5923:             $resulttext .= '</ul>';
 5924:         } else {
 5925:             $resulttext = &mt('No changes made to auto-enrollment settings');
 5926:         }
 5927:     } else {
 5928:         $resulttext = '<span class="LC_error">'.
 5929: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5930:     }
 5931:     return $resulttext;
 5932: }
 5933: 
 5934: sub modify_autoupdate {
 5935:     my ($dom,%domconfig) = @_;
 5936:     my ($resulttext,%currautoupdate,%fields,%changes);
 5937:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 5938:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 5939:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 5940:         }
 5941:     }
 5942:     my @offon = ('off','on');
 5943:     my %title = &Apache::lonlocal::texthash (
 5944:                    run => 'Auto-update:',
 5945:                    classlists => 'Updates to user information in classlists?'
 5946:                 );
 5947:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5948:     my %fieldtitles = &Apache::lonlocal::texthash (
 5949:                         id => 'Student/Employee ID',
 5950:                         permanentemail => 'E-mail address',
 5951:                         lastname => 'Last Name',
 5952:                         firstname => 'First Name',
 5953:                         middlename => 'Middle Name',
 5954:                         generation => 'Generation',
 5955:                       );
 5956:     $othertitle = &mt('All users');
 5957:     if (keys(%{$usertypes}) >  0) {
 5958:         $othertitle = &mt('Other users');
 5959:     }
 5960:     foreach my $key (keys(%env)) {
 5961:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 5962:             my ($usertype,$item) = ($1,$2);
 5963:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 5964:                 if ($usertype eq 'default') {   
 5965:                     push(@{$fields{$1}},$2);
 5966:                 } elsif (ref($types) eq 'ARRAY') {
 5967:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 5968:                         push(@{$fields{$1}},$2);
 5969:                     }
 5970:                 }
 5971:             }
 5972:         }
 5973:     }
 5974:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 5975:     @lockablenames = sort(@lockablenames);
 5976:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 5977:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 5978:         if (@changed) {
 5979:             $changes{'lockablenames'} = 1;
 5980:         }
 5981:     } else {
 5982:         if (@lockablenames) {
 5983:             $changes{'lockablenames'} = 1;
 5984:         }
 5985:     }
 5986:     my %updatehash = (
 5987:                       autoupdate => { run => $env{'form.autoupdate_run'},
 5988:                                       classlists => $env{'form.classlists'},
 5989:                                       fields => {%fields},
 5990:                                       lockablenames => \@lockablenames,
 5991:                                     }
 5992:                      );
 5993:     foreach my $key (keys(%currautoupdate)) {
 5994:         if (($key eq 'run') || ($key eq 'classlists')) {
 5995:             if (exists($updatehash{autoupdate}{$key})) {
 5996:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 5997:                     $changes{$key} = 1;
 5998:                 }
 5999:             }
 6000:         } elsif ($key eq 'fields') {
 6001:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6002:                 foreach my $item (@{$types},'default') {
 6003:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6004:                         my $change = 0;
 6005:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6006:                             if (!exists($fields{$item})) {
 6007:                                 $change = 1;
 6008:                                 last;
 6009:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6010:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6011:                                     $change = 1;
 6012:                                     last;
 6013:                                 }
 6014:                             }
 6015:                         }
 6016:                         if ($change) {
 6017:                             push(@{$changes{$key}},$item);
 6018:                         }
 6019:                     } 
 6020:                 }
 6021:             }
 6022:         } elsif ($key eq 'lockablenames') {
 6023:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6024:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6025:                 if (@changed) {
 6026:                     $changes{'lockablenames'} = 1;
 6027:                 }
 6028:             } else {
 6029:                 if (@lockablenames) {
 6030:                     $changes{'lockablenames'} = 1;
 6031:                 }
 6032:             }
 6033:         }
 6034:     }
 6035:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6036:         if (@lockablenames) {
 6037:             $changes{'lockablenames'} = 1;
 6038:         }
 6039:     }
 6040:     foreach my $item (@{$types},'default') {
 6041:         if (defined($fields{$item})) {
 6042:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6043:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6044:                     my $change = 0;
 6045:                     if (ref($fields{$item}) eq 'ARRAY') {
 6046:                         foreach my $type (@{$fields{$item}}) {
 6047:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6048:                                 $change = 1;
 6049:                                 last;
 6050:                             }
 6051:                         }
 6052:                     }
 6053:                     if ($change) {
 6054:                         push(@{$changes{'fields'}},$item);
 6055:                     }
 6056:                 } else {
 6057:                     push(@{$changes{'fields'}},$item);
 6058:                 }
 6059:             } else {
 6060:                 push(@{$changes{'fields'}},$item);
 6061:             }
 6062:         }
 6063:     }
 6064:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6065:                                              $dom);
 6066:     if ($putresult eq 'ok') {
 6067:         if (keys(%changes) > 0) {
 6068:             $resulttext = &mt('Changes made:').'<ul>';
 6069:             foreach my $key (sort(keys(%changes))) {
 6070:                 if ($key eq 'lockablenames') {
 6071:                     $resulttext .= '<li>';
 6072:                     if (@lockablenames) {
 6073:                         $usertypes->{'default'} = $othertitle;
 6074:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6075:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6076:                     } else {
 6077:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6078:                     }
 6079:                     $resulttext .= '</li>';
 6080:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6081:                     foreach my $item (@{$changes{$key}}) {
 6082:                         my @newvalues;
 6083:                         foreach my $type (@{$fields{$item}}) {
 6084:                             push(@newvalues,$fieldtitles{$type});
 6085:                         }
 6086:                         my $newvaluestr;
 6087:                         if (@newvalues > 0) {
 6088:                             $newvaluestr = join(', ',@newvalues);
 6089:                         } else {
 6090:                             $newvaluestr = &mt('none');
 6091:                         }
 6092:                         if ($item eq 'default') {
 6093:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6094:                         } else {
 6095:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6096:                         }
 6097:                     }
 6098:                 } else {
 6099:                     my $newvalue;
 6100:                     if ($key eq 'run') {
 6101:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6102:                     } else {
 6103:                         $newvalue = $offon[$env{'form.'.$key}];
 6104:                     }
 6105:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6106:                 }
 6107:             }
 6108:             $resulttext .= '</ul>';
 6109:         } else {
 6110:             $resulttext = &mt('No changes made to autoupdates');
 6111:         }
 6112:     } else {
 6113:         $resulttext = '<span class="LC_error">'.
 6114: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6115:     }
 6116:     return $resulttext;
 6117: }
 6118: 
 6119: sub modify_autocreate {
 6120:     my ($dom,%domconfig) = @_;
 6121:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6122:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6123:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6124:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6125:         }
 6126:     }
 6127:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6128:                  req => 'Auto-creation of validated requests for official courses',
 6129:                  xmldc => 'Identity of course creator of courses from XML files',
 6130:                );
 6131:     my @types = ('xml','req');
 6132:     foreach my $item (@types) {
 6133:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6134:         $newvals{$item} =~ s/\D//g;
 6135:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6136:     }
 6137:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6138:     my %domcoords = &get_active_dcs($dom);
 6139:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6140:         $newvals{'xmldc'} = '';
 6141:     } 
 6142:     %autocreatehash =  (
 6143:                         autocreate => { xml => $newvals{'xml'},
 6144:                                         req => $newvals{'req'},
 6145:                                       }
 6146:                        );
 6147:     if ($newvals{'xmldc'} ne '') {
 6148:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6149:     }
 6150:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6151:                                              $dom);
 6152:     if ($putresult eq 'ok') {
 6153:         my @items = @types;
 6154:         if ($newvals{'xml'}) {
 6155:             push(@items,'xmldc');
 6156:         }
 6157:         foreach my $item (@items) {
 6158:             if (exists($currautocreate{$item})) {
 6159:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6160:                     $changes{$item} = 1;
 6161:                 }
 6162:             } elsif ($newvals{$item}) {
 6163:                 $changes{$item} = 1;
 6164:             }
 6165:         }
 6166:         if (keys(%changes) > 0) {
 6167:             my @offon = ('off','on'); 
 6168:             $resulttext = &mt('Changes made:').'<ul>';
 6169:             foreach my $item (@types) {
 6170:                 if ($changes{$item}) {
 6171:                     my $newtxt = $offon[$newvals{$item}];
 6172:                     $resulttext .= '<li>'.&mt("$title{$item} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>';
 6173:                 }
 6174:             }
 6175:             if ($changes{'xmldc'}) {
 6176:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6177:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6178:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>'; 
 6179:             }
 6180:             $resulttext .= '</ul>';
 6181:         } else {
 6182:             $resulttext = &mt('No changes made to auto-creation settings');
 6183:         }
 6184:     } else {
 6185:         $resulttext = '<span class="LC_error">'.
 6186:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6187:     }
 6188:     return $resulttext;
 6189: }
 6190: 
 6191: sub modify_directorysrch {
 6192:     my ($dom,%domconfig) = @_;
 6193:     my ($resulttext,%changes);
 6194:     my %currdirsrch;
 6195:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6196:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6197:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6198:         }
 6199:     }
 6200:     my %title = ( available => 'Directory search available',
 6201:                   localonly => 'Other domains can search',
 6202:                   searchby => 'Search types',
 6203:                   searchtypes => 'Search latitude');
 6204:     my @offon = ('off','on');
 6205:     my @otherdoms = ('Yes','No');
 6206: 
 6207:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6208:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6209:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6210: 
 6211:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6212:     if (keys(%{$usertypes}) == 0) {
 6213:         @cansearch = ('default');
 6214:     } else {
 6215:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6216:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6217:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6218:                     push(@{$changes{'cansearch'}},$type);
 6219:                 }
 6220:             }
 6221:             foreach my $type (@cansearch) {
 6222:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6223:                     push(@{$changes{'cansearch'}},$type);
 6224:                 }
 6225:             }
 6226:         } else {
 6227:             push(@{$changes{'cansearch'}},@cansearch);
 6228:         }
 6229:     }
 6230: 
 6231:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6232:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6233:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6234:                 push(@{$changes{'searchby'}},$by);
 6235:             }
 6236:         }
 6237:         foreach my $by (@searchby) {
 6238:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6239:                 push(@{$changes{'searchby'}},$by);
 6240:             }
 6241:         }
 6242:     } else {
 6243:         push(@{$changes{'searchby'}},@searchby);
 6244:     }
 6245: 
 6246:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6247:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6248:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6249:                 push(@{$changes{'searchtypes'}},$type);
 6250:             }
 6251:         }
 6252:         foreach my $type (@searchtypes) {
 6253:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6254:                 push(@{$changes{'searchtypes'}},$type);
 6255:             }
 6256:         }
 6257:     } else {
 6258:         if (exists($currdirsrch{'searchtypes'})) {
 6259:             foreach my $type (@searchtypes) {  
 6260:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6261:                     push(@{$changes{'searchtypes'}},$type);
 6262:                 }
 6263:             }
 6264:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6265:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6266:             }   
 6267:         } else {
 6268:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6269:         }
 6270:     }
 6271: 
 6272:     my %dirsrch_hash =  (
 6273:             directorysrch => { available => $env{'form.dirsrch_available'},
 6274:                                cansearch => \@cansearch,
 6275:                                localonly => $env{'form.dirsrch_localonly'},
 6276:                                searchby => \@searchby,
 6277:                                searchtypes => \@searchtypes,
 6278:                              }
 6279:             );
 6280:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6281:                                              $dom);
 6282:     if ($putresult eq 'ok') {
 6283:         if (exists($currdirsrch{'available'})) {
 6284:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6285:                  $changes{'available'} = 1;
 6286:              }
 6287:         } else {
 6288:             if ($env{'form.dirsrch_available'} eq '1') {
 6289:                 $changes{'available'} = 1;
 6290:             }
 6291:         }
 6292:         if (exists($currdirsrch{'localonly'})) {
 6293:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6294:                  $changes{'localonly'} = 1;
 6295:              }
 6296:         } else {
 6297:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6298:                 $changes{'localonly'} = 1;
 6299:             }
 6300:         }
 6301:         if (keys(%changes) > 0) {
 6302:             $resulttext = &mt('Changes made:').'<ul>';
 6303:             if ($changes{'available'}) {
 6304:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6305:             }
 6306:             if ($changes{'localonly'}) {
 6307:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6308:             }
 6309: 
 6310:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6311:                 my $chgtext;
 6312:                 if (ref($usertypes) eq 'HASH') {
 6313:                     if (keys(%{$usertypes}) > 0) {
 6314:                         foreach my $type (@{$types}) {
 6315:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6316:                                 $chgtext .= $usertypes->{$type}.'; ';
 6317:                             }
 6318:                         }
 6319:                         if (grep(/^default$/,@cansearch)) {
 6320:                             $chgtext .= $othertitle;
 6321:                         } else {
 6322:                             $chgtext =~ s/\; $//;
 6323:                         }
 6324:                         $resulttext .= '<li>'.&mt("Users from domain '<span class=\"LC_cusr_emph\">[_1]</span>' permitted to search the institutional directory set to: [_2]",$dom,$chgtext).'</li>';
 6325:                     }
 6326:                 }
 6327:             }
 6328:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6329:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6330:                 my $chgtext;
 6331:                 foreach my $type (@{$titleorder}) {
 6332:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6333:                         if (defined($searchtitles->{$type})) {
 6334:                             $chgtext .= $searchtitles->{$type}.'; ';
 6335:                         }
 6336:                     }
 6337:                 }
 6338:                 $chgtext =~ s/\; $//;
 6339:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6340:             }
 6341:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6342:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6343:                 my $chgtext;
 6344:                 foreach my $type (@{$srchtypeorder}) {
 6345:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6346:                         if (defined($srchtypes_desc->{$type})) {
 6347:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6348:                         }
 6349:                     }
 6350:                 }
 6351:                 $chgtext =~ s/\; $//;
 6352:                 $resulttext .= '<li>'.&mt("$title{'searchtypes'} set to: \"[_1]\"",$chgtext).'</li>';
 6353:             }
 6354:             $resulttext .= '</ul>';
 6355:         } else {
 6356:             $resulttext = &mt('No changes made to institution directory search settings');
 6357:         }
 6358:     } else {
 6359:         $resulttext = '<span class="LC_error">'.
 6360:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6361:     }
 6362:     return $resulttext;
 6363: }
 6364: 
 6365: sub modify_contacts {
 6366:     my ($dom,%domconfig) = @_;
 6367:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6368:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6369:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6370:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6371:         }
 6372:     }
 6373:     my (%others,%to,%bcc);
 6374:     my @contacts = ('supportemail','adminemail');
 6375:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6376:                     'requestsmail');
 6377:     foreach my $type (@mailings) {
 6378:         @{$newsetting{$type}} = 
 6379:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6380:         foreach my $item (@contacts) {
 6381:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6382:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6383:             } else {
 6384:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6385:             }
 6386:         }  
 6387:         $others{$type} = $env{'form.'.$type.'_others'};
 6388:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6389:         if ($type eq 'helpdeskmail') {
 6390:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6391:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6392:         }
 6393:     }
 6394:     foreach my $item (@contacts) {
 6395:         $to{$item} = $env{'form.'.$item};
 6396:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6397:     }
 6398:     if (keys(%currsetting) > 0) {
 6399:         foreach my $item (@contacts) {
 6400:             if ($to{$item} ne $currsetting{$item}) {
 6401:                 $changes{$item} = 1;
 6402:             }
 6403:         }
 6404:         foreach my $type (@mailings) {
 6405:             foreach my $item (@contacts) {
 6406:                 if (ref($currsetting{$type}) eq 'HASH') {
 6407:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6408:                         push(@{$changes{$type}},$item);
 6409:                     }
 6410:                 } else {
 6411:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6412:                 }
 6413:             }
 6414:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6415:                 push(@{$changes{$type}},'others');
 6416:             }
 6417:             if ($type eq 'helpdeskmail') {   
 6418:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6419:                     push(@{$changes{$type}},'bcc'); 
 6420:                 }
 6421:             }
 6422:         }
 6423:     } else {
 6424:         my %default;
 6425:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6426:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6427:         $default{'errormail'} = 'adminemail';
 6428:         $default{'packagesmail'} = 'adminemail';
 6429:         $default{'helpdeskmail'} = 'supportemail';
 6430:         $default{'lonstatusmail'} = 'adminemail';
 6431:         $default{'requestsmail'} = 'adminemail';
 6432:         foreach my $item (@contacts) {
 6433:            if ($to{$item} ne $default{$item}) {
 6434:               $changes{$item} = 1;
 6435:            } 
 6436:         }
 6437:         foreach my $type (@mailings) {
 6438:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6439:                
 6440:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6441:             }
 6442:             if ($others{$type} ne '') {
 6443:                 push(@{$changes{$type}},'others');
 6444:             }
 6445:             if ($type eq 'helpdeskmail') {
 6446:                 if ($bcc{$type} ne '') {
 6447:                     push(@{$changes{$type}},'bcc');
 6448:                 }
 6449:             }
 6450:         }
 6451:     }
 6452:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6453:                                              $dom);
 6454:     if ($putresult eq 'ok') {
 6455:         if (keys(%changes) > 0) {
 6456:             my ($titles,$short_titles)  = &contact_titles();
 6457:             $resulttext = &mt('Changes made:').'<ul>';
 6458:             foreach my $item (@contacts) {
 6459:                 if ($changes{$item}) {
 6460:                     $resulttext .= '<li>'.$titles->{$item}.
 6461:                                     &mt(' set to: ').
 6462:                                     '<span class="LC_cusr_emph">'.
 6463:                                     $to{$item}.'</span></li>';
 6464:                 }
 6465:             }
 6466:             foreach my $type (@mailings) {
 6467:                 if (ref($changes{$type}) eq 'ARRAY') {
 6468:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6469:                     my @text;
 6470:                     foreach my $item (@{$newsetting{$type}}) {
 6471:                         push(@text,$short_titles->{$item});
 6472:                     }
 6473:                     if ($others{$type} ne '') {
 6474:                         push(@text,$others{$type});
 6475:                     }
 6476:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6477:                                    join(', ',@text).'</span>';
 6478:                     if ($type eq 'helpdeskmail') {
 6479:                         if ($bcc{$type} ne '') {
 6480:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6481:                         }
 6482:                     }
 6483:                     $resulttext .= '</li>';
 6484:                 }
 6485:             }
 6486:             $resulttext .= '</ul>';
 6487:         } else {
 6488:             $resulttext = &mt('No changes made to contact information');
 6489:         }
 6490:     } else {
 6491:         $resulttext = '<span class="LC_error">'.
 6492:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6493:     }
 6494:     return $resulttext;
 6495: }
 6496: 
 6497: sub modify_usercreation {
 6498:     my ($dom,%domconfig) = @_;
 6499:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6500:     my $warningmsg;
 6501:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6502:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6503:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6504:         }
 6505:     }
 6506:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6507:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6508:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6509:     my @contexts = ('author','course','requestcrs','selfcreate');
 6510:     foreach my $item(@contexts) {
 6511:         if ($item eq 'selfcreate') {
 6512:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6513:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6514:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6515:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6516:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6517:                         $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.');   
 6518:                     }
 6519:                 }
 6520:             }
 6521:         } else {
 6522:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6523:         }
 6524:     }
 6525:     my ($othertitle,$usertypes,$types) = 
 6526:         &Apache::loncommon::sorted_inst_types($dom);
 6527:     if (ref($types) eq 'ARRAY') {
 6528:         if (@{$types} > 0) {
 6529:             @{$cancreate{'statustocreate'}} = 
 6530:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6531:         } else {
 6532:             @{$cancreate{'statustocreate'}} = ();
 6533:         }
 6534:         push(@contexts,'statustocreate');
 6535:     }
 6536:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6537:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6538:         foreach my $item (@contexts) {
 6539:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6540:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6541:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6542:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6543:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6544:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6545:                                     push(@{$changes{'cancreate'}},$item);
 6546:                                 }
 6547:                             }
 6548:                         }
 6549:                     }
 6550:                 } else {
 6551:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6552:                         if (@{$cancreate{$item}} > 0) {
 6553:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6554:                                 push(@{$changes{'cancreate'}},$item);
 6555:                             }
 6556:                         }
 6557:                     } else {
 6558:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6559:                             if (@{$cancreate{$item}} < 3) {
 6560:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6561:                                     push(@{$changes{'cancreate'}},$item);
 6562:                                 }
 6563:                             }
 6564:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6565:                             if (@{$cancreate{$item}} > 0) {
 6566:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6567:                                     push(@{$changes{'cancreate'}},$item);
 6568:                                 }
 6569:                             }
 6570:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6571:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6572:                                 push(@{$changes{'cancreate'}},$item);
 6573:                             }
 6574:                         }
 6575:                     }
 6576:                 }
 6577:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6578:                     foreach my $type (@{$cancreate{$item}}) {
 6579:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6580:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6581:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6582:                                     push(@{$changes{'cancreate'}},$item);
 6583:                                 }
 6584:                             }
 6585:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6586:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6587:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6588:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6589:                                     push(@{$changes{'cancreate'}},$item);
 6590:                                 }
 6591:                             }
 6592:                         }
 6593:                     }
 6594:                 }
 6595:             } else {
 6596:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6597:                     push(@{$changes{'cancreate'}},$item);
 6598:                 }
 6599:             }
 6600:         }
 6601:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6602:         foreach my $item (@contexts) {
 6603:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6604:                 if ($cancreate{$item} ne 'any') {
 6605:                     push(@{$changes{'cancreate'}},$item);
 6606:                 }
 6607:             } else {
 6608:                 if ($cancreate{$item} ne 'none') {
 6609:                     push(@{$changes{'cancreate'}},$item);
 6610:                 }
 6611:             }
 6612:         }
 6613:     } else {
 6614:         foreach my $item (@contexts)  {
 6615:             push(@{$changes{'cancreate'}},$item);
 6616:         }
 6617:     }
 6618: 
 6619:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6620:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6621:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6622:                 push(@{$changes{'username_rule'}},$type);
 6623:             }
 6624:         }
 6625:         foreach my $type (@username_rule) {
 6626:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6627:                 push(@{$changes{'username_rule'}},$type);
 6628:             }
 6629:         }
 6630:     } else {
 6631:         push(@{$changes{'username_rule'}},@username_rule);
 6632:     }
 6633: 
 6634:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6635:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 6636:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 6637:                 push(@{$changes{'id_rule'}},$type);
 6638:             }
 6639:         }
 6640:         foreach my $type (@id_rule) {
 6641:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 6642:                 push(@{$changes{'id_rule'}},$type);
 6643:             }
 6644:         }
 6645:     } else {
 6646:         push(@{$changes{'id_rule'}},@id_rule);
 6647:     }
 6648: 
 6649:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 6650:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 6651:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 6652:                 push(@{$changes{'email_rule'}},$type);
 6653:             }
 6654:         }
 6655:         foreach my $type (@email_rule) {
 6656:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 6657:                 push(@{$changes{'email_rule'}},$type);
 6658:             }
 6659:         }
 6660:     } else {
 6661:         push(@{$changes{'email_rule'}},@email_rule);
 6662:     }
 6663: 
 6664:     my @authen_contexts = ('author','course','domain');
 6665:     my @authtypes = ('int','krb4','krb5','loc');
 6666:     my %authhash;
 6667:     foreach my $item (@authen_contexts) {
 6668:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 6669:         foreach my $auth (@authtypes) {
 6670:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 6671:                 $authhash{$item}{$auth} = 1;
 6672:             } else {
 6673:                 $authhash{$item}{$auth} = 0;
 6674:             }
 6675:         }
 6676:     }
 6677:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 6678:         foreach my $item (@authen_contexts) {
 6679:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 6680:                 foreach my $auth (@authtypes) {
 6681:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 6682:                         push(@{$changes{'authtypes'}},$item);
 6683:                         last;
 6684:                     }
 6685:                 }
 6686:             }
 6687:         }
 6688:     } else {
 6689:         foreach my $item (@authen_contexts) {
 6690:             push(@{$changes{'authtypes'}},$item);
 6691:         }
 6692:     }
 6693: 
 6694:     my %usercreation_hash =  (
 6695:             usercreation => {
 6696:                               cancreate     => \%cancreate,
 6697:                               username_rule => \@username_rule,
 6698:                               id_rule       => \@id_rule,
 6699:                               email_rule    => \@email_rule,
 6700:                               authtypes     => \%authhash,
 6701:                             }
 6702:             );
 6703: 
 6704:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 6705:                                              $dom);
 6706: 
 6707:     my %selfcreatetypes = (
 6708:                              sso   => 'users authenticated by institutional single sign on',
 6709:                              login => 'users authenticated by institutional log-in',
 6710:                              email => 'users who provide a valid e-mail address for use as the username',
 6711:                           );
 6712:     if ($putresult eq 'ok') {
 6713:         if (keys(%changes) > 0) {
 6714:             $resulttext = &mt('Changes made:').'<ul>';
 6715:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 6716:                 my %lt = &usercreation_types();
 6717:                 foreach my $type (@{$changes{'cancreate'}}) {
 6718:                     my $chgtext;
 6719:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 6720:                         $chgtext = $lt{$type}.', ';
 6721:                     }
 6722:                     if ($type eq 'selfcreate') {
 6723:                         if (@{$cancreate{$type}} == 0) {
 6724:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 6725:                         } else {
 6726:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 6727:                             foreach my $case (@{$cancreate{$type}}) {
 6728:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 6729:                             }
 6730:                             $chgtext .= '</ul>';
 6731:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 6732:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 6733:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 6734:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 6735:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6736:                                         }
 6737:                                     }
 6738:                                 }
 6739:                             }
 6740:                         }
 6741:                     } elsif ($type eq 'statustocreate') {
 6742:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 6743:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 6744:                             if (@{$cancreate{'selfcreate'}} > 0) {
 6745:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 6746: 
 6747:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 6748:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 6749:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6750:                                     } 
 6751:                                 } elsif (ref($usertypes) eq 'HASH') {
 6752:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6753:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 6754:                                     } else {
 6755:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 6756:                                     }
 6757:                                     $chgtext .= '<ul>';
 6758:                                     foreach my $case (@{$cancreate{$type}}) {
 6759:                                         if ($case eq 'default') {
 6760:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 6761:                                         } else {
 6762:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 6763:                                         }
 6764:                                     }
 6765:                                     $chgtext .= '</ul>';
 6766:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6767:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 6768:                                     }
 6769:                                 }
 6770:                             } else {
 6771:                                 if (@{$cancreate{$type}} == 0) {
 6772:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 6773:                                 } else {
 6774:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 6775:                                 }
 6776:                             }
 6777:                         }
 6778:                     } elsif ($type eq 'captcha') {
 6779:                         if ($cancreate{$type} eq 'notused') {
 6780:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 6781:                         } else {
 6782:                             my %captchas = &captcha_phrases();
 6783:                             if ($captchas{$cancreate{$type}}) {
 6784:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 6785:                             } else {
 6786:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.'); 
 6787:                             }
 6788:                         }
 6789:                     } elsif ($type eq 'recaptchakeys') {
 6790:                         my ($privkey,$pubkey);
 6791:                         if (ref($cancreate{$type}) eq 'HASH') {
 6792:                             $pubkey = $cancreate{$type}{'public'};
 6793:                             $privkey = $cancreate{$type}{'private'};
 6794:                         }
 6795:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 6796:                         if (!$pubkey) {
 6797:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 6798:                         } else {
 6799:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6800:                         }
 6801:                         if (!$privkey) {
 6802:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 6803:                         } else {
 6804:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6805:                         }
 6806:                         $chgtext .= '</ul>';
 6807:                     } else {
 6808:                         if ($cancreate{$type} eq 'none') {
 6809:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 6810:                         } elsif ($cancreate{$type} eq 'any') {
 6811:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 6812:                         } elsif ($cancreate{$type} eq 'official') {
 6813:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 6814:                         } elsif ($cancreate{$type} eq 'unofficial') {
 6815:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 6816:                         }
 6817:                     }
 6818:                     $resulttext .= '<li>'.$chgtext.'</li>';
 6819:                 }
 6820:             }
 6821:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 6822:                 my ($rules,$ruleorder) = 
 6823:                     &Apache::lonnet::inst_userrules($dom,'username');
 6824:                 my $chgtext = '<ul>';
 6825:                 foreach my $type (@username_rule) {
 6826:                     if (ref($rules->{$type}) eq 'HASH') {
 6827:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 6828:                     }
 6829:                 }
 6830:                 $chgtext .= '</ul>';
 6831:                 if (@username_rule > 0) {
 6832:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 6833:                 } else {
 6834:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 6835:                 }
 6836:             }
 6837:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 6838:                 my ($idrules,$idruleorder) = 
 6839:                     &Apache::lonnet::inst_userrules($dom,'id');
 6840:                 my $chgtext = '<ul>';
 6841:                 foreach my $type (@id_rule) {
 6842:                     if (ref($idrules->{$type}) eq 'HASH') {
 6843:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 6844:                     }
 6845:                 }
 6846:                 $chgtext .= '</ul>';
 6847:                 if (@id_rule > 0) {
 6848:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 6849:                 } else {
 6850:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 6851:                 }
 6852:             }
 6853:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 6854:                 my ($emailrules,$emailruleorder) =
 6855:                     &Apache::lonnet::inst_userrules($dom,'email');
 6856:                 my $chgtext = '<ul>';
 6857:                 foreach my $type (@email_rule) {
 6858:                     if (ref($emailrules->{$type}) eq 'HASH') {
 6859:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 6860:                     }
 6861:                 }
 6862:                 $chgtext .= '</ul>';
 6863:                 if (@email_rule > 0) {
 6864:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 6865:                 } else {
 6866:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 6867:                 }
 6868:             }
 6869: 
 6870:             my %authname = &authtype_names();
 6871:             my %context_title = &context_names();
 6872:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 6873:                 my $chgtext = '<ul>';
 6874:                 foreach my $type (@{$changes{'authtypes'}}) {
 6875:                     my @allowed;
 6876:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 6877:                     foreach my $auth (@authtypes) {
 6878:                         if ($authhash{$type}{$auth}) {
 6879:                             push(@allowed,$authname{$auth});
 6880:                         }
 6881:                     }
 6882:                     if (@allowed > 0) {
 6883:                         $chgtext .= join(', ',@allowed).'</li>';
 6884:                     } else {
 6885:                         $chgtext .= &mt('none').'</li>';
 6886:                     }
 6887:                 }
 6888:                 $chgtext .= '</ul>';
 6889:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 6890:                 $resulttext .= '</li>';
 6891:             }
 6892:             $resulttext .= '</ul>';
 6893:         } else {
 6894:             $resulttext = &mt('No changes made to user creation settings');
 6895:         }
 6896:     } else {
 6897:         $resulttext = '<span class="LC_error">'.
 6898:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6899:     }
 6900:     if ($warningmsg ne '') {
 6901:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 6902:     }
 6903:     return $resulttext;
 6904: }
 6905: 
 6906: sub process_captcha {
 6907:     my ($container,$changes,$newsettings,$current) = @_;
 6908:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 6909:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 6910:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 6911:         $newsettings->{'captcha'} = 'original';
 6912:     }
 6913:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 6914:         if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 6915:             push(@{$changes->{'cancreate'}},'captcha');
 6916:         } elsif (!defined($changes->{'cancreate'})) {
 6917:             $changes->{'cancreate'} = ['captcha'];
 6918:         }
 6919:     }
 6920:     my ($newpub,$newpriv,$currpub,$currpriv);
 6921:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 6922:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 6923:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 6924:     }
 6925:     $newsettings->{'recaptchakeys'} = {
 6926:                                          public  => $newpub,
 6927:                                          private => $newpriv,
 6928:                                       };
 6929:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 6930:         $currpub = $current->{'recaptchakeys'}{'public'};
 6931:         $currpriv = $current->{'recaptchakeys'}{'private'};
 6932:     }
 6933:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 6934:         if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 6935:             push(@{$changes->{'cancreate'}},'recaptchakeys');
 6936:         } elsif (!defined($changes->{'cancreate'})) {
 6937:             $changes->{'cancreate'} = ['recaptchakeys'];
 6938:         }
 6939:     }
 6940:     return;
 6941: }
 6942: 
 6943: sub modify_usermodification {
 6944:     my ($dom,%domconfig) = @_;
 6945:     my ($resulttext,%curr_usermodification,%changes);
 6946:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 6947:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 6948:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 6949:         }
 6950:     }
 6951:     my @contexts = ('author','course','selfcreate');
 6952:     my %context_title = (
 6953:                            author => 'In author context',
 6954:                            course => 'In course context',
 6955:                            selfcreate => 'When self creating account', 
 6956:                         );
 6957:     my @fields = ('lastname','firstname','middlename','generation',
 6958:                   'permanentemail','id');
 6959:     my %roles = (
 6960:                   author => ['ca','aa'],
 6961:                   course => ['st','ep','ta','in','cr'],
 6962:                 );
 6963:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6964:     if (ref($types) eq 'ARRAY') {
 6965:         push(@{$types},'default');
 6966:         $usertypes->{'default'} = $othertitle;
 6967:     }
 6968:     $roles{'selfcreate'} = $types;  
 6969:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6970:     my %modifyhash;
 6971:     foreach my $context (@contexts) {
 6972:         foreach my $role (@{$roles{$context}}) {
 6973:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 6974:             foreach my $item (@fields) {
 6975:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 6976:                     $modifyhash{$context}{$role}{$item} = 1;
 6977:                 } else {
 6978:                     $modifyhash{$context}{$role}{$item} = 0;
 6979:                 }
 6980:             }
 6981:         }
 6982:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 6983:             foreach my $role (@{$roles{$context}}) {
 6984:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 6985:                     foreach my $field (@fields) {
 6986:                         if ($modifyhash{$context}{$role}{$field} ne 
 6987:                                 $curr_usermodification{$context}{$role}{$field}) {
 6988:                             push(@{$changes{$context}},$role);
 6989:                             last;
 6990:                         }
 6991:                     }
 6992:                 }
 6993:             }
 6994:         } else {
 6995:             foreach my $context (@contexts) {
 6996:                 foreach my $role (@{$roles{$context}}) {
 6997:                     push(@{$changes{$context}},$role);
 6998:                 }
 6999:             }
 7000:         }
 7001:     }
 7002:     my %usermodification_hash =  (
 7003:                                    usermodification => \%modifyhash,
 7004:                                  );
 7005:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7006:                                              \%usermodification_hash,$dom);
 7007:     if ($putresult eq 'ok') {
 7008:         if (keys(%changes) > 0) {
 7009:             $resulttext = &mt('Changes made: ').'<ul>';
 7010:             foreach my $context (@contexts) {
 7011:                 if (ref($changes{$context}) eq 'ARRAY') {
 7012:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7013:                     if (ref($changes{$context}) eq 'ARRAY') {
 7014:                         foreach my $role (@{$changes{$context}}) {
 7015:                             my $rolename;
 7016:                             if ($context eq 'selfcreate') {
 7017:                                 $rolename = $role;
 7018:                                 if (ref($usertypes) eq 'HASH') {
 7019:                                     if ($usertypes->{$role} ne '') {
 7020:                                         $rolename = $usertypes->{$role};
 7021:                                     }
 7022:                                 }
 7023:                             } else {
 7024:                                 if ($role eq 'cr') {
 7025:                                     $rolename = &mt('Custom');
 7026:                                 } else {
 7027:                                     $rolename = &Apache::lonnet::plaintext($role);
 7028:                                 }
 7029:                             }
 7030:                             my @modifiable;
 7031:                             if ($context eq 'selfcreate') {
 7032:                                 $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): ');
 7033:                             } else {
 7034:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7035:                             }
 7036:                             foreach my $field (@fields) {
 7037:                                 if ($modifyhash{$context}{$role}{$field}) {
 7038:                                     push(@modifiable,$fieldtitles{$field});
 7039:                                 }
 7040:                             }
 7041:                             if (@modifiable > 0) {
 7042:                                 $resulttext .= join(', ',@modifiable);
 7043:                             } else {
 7044:                                 $resulttext .= &mt('none'); 
 7045:                             }
 7046:                             $resulttext .= '</li>';
 7047:                         }
 7048:                         $resulttext .= '</ul></li>';
 7049:                     }
 7050:                 }
 7051:             }
 7052:             $resulttext .= '</ul>';
 7053:         } else {
 7054:             $resulttext = &mt('No changes made to user modification settings');
 7055:         }
 7056:     } else {
 7057:         $resulttext = '<span class="LC_error">'.
 7058:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7059:     }
 7060:     return $resulttext;
 7061: }
 7062: 
 7063: sub modify_defaults {
 7064:     my ($dom,$r) = @_;
 7065:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7066:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7067:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7068:     my @authtypes = ('internal','krb4','krb5','localauth');
 7069:     foreach my $item (@items) {
 7070:         $newvalues{$item} = $env{'form.'.$item};
 7071:         if ($item eq 'auth_def') {
 7072:             if ($newvalues{$item} ne '') {
 7073:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7074:                     push(@errors,$item);
 7075:                 }
 7076:             }
 7077:         } elsif ($item eq 'lang_def') {
 7078:             if ($newvalues{$item} ne '') {
 7079:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7080:                     my $langcode = $1;
 7081:                     if ($langcode ne 'x_chef') {
 7082:                         if (code2language($langcode) eq '') {
 7083:                             push(@errors,$item);
 7084:                         }
 7085:                     }
 7086:                 } else {
 7087:                     push(@errors,$item);
 7088:                 }
 7089:             }
 7090:         } elsif ($item eq 'timezone_def') {
 7091:             if ($newvalues{$item} ne '') {
 7092:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7093:                     push(@errors,$item);   
 7094:                 }
 7095:             }
 7096:         } elsif ($item eq 'datelocale_def') {
 7097:             if ($newvalues{$item} ne '') {
 7098:                 my @datelocale_ids = DateTime::Locale->ids();
 7099:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7100:                     push(@errors,$item);
 7101:                 }
 7102:             }
 7103:         } elsif ($item eq 'portal_def') {
 7104:             if ($newvalues{$item} ne '') {
 7105:                 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])\/?$/) {
 7106:                     push(@errors,$item);
 7107:                 }
 7108:             }
 7109:         }
 7110:         if (grep(/^\Q$item\E$/,@errors)) {
 7111:             $newvalues{$item} = $domdefaults{$item};
 7112:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7113:             $changes{$item} = 1;
 7114:         }
 7115:         $domdefaults{$item} = $newvalues{$item};
 7116:     }
 7117:     my %defaults_hash = (
 7118:                          defaults => \%newvalues,
 7119:                         );
 7120:     my $title = &defaults_titles();
 7121:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7122:                                              $dom);
 7123:     if ($putresult eq 'ok') {
 7124:         if (keys(%changes) > 0) {
 7125:             $resulttext = &mt('Changes made:').'<ul>';
 7126:             my $version = $r->dir_config('lonVersion');
 7127:             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";
 7128:             foreach my $item (sort(keys(%changes))) {
 7129:                 my $value = $env{'form.'.$item};
 7130:                 if ($value eq '') {
 7131:                     $value = &mt('none');
 7132:                 } elsif ($item eq 'auth_def') {
 7133:                     my %authnames = &authtype_names();
 7134:                     my %shortauth = (
 7135:                              internal => 'int',
 7136:                              krb4 => 'krb4',
 7137:                              krb5 => 'krb5',
 7138:                              localauth  => 'loc',
 7139:                     );
 7140:                     $value = $authnames{$shortauth{$value}};
 7141:                 }
 7142:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7143:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7144:             }
 7145:             $resulttext .= '</ul>';
 7146:             $mailmsgtext .= "\n";
 7147:             my $cachetime = 24*60*60;
 7148:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7149:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7150:                 my $sysmail = $r->dir_config('lonSysEMail');
 7151:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 7152:             }
 7153:         } else {
 7154:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 7155:         }
 7156:     } else {
 7157:         $resulttext = '<span class="LC_error">'.
 7158:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7159:     }
 7160:     if (@errors > 0) {
 7161:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7162:         foreach my $item (@errors) {
 7163:             $resulttext .= ' "'.$title->{$item}.'",';
 7164:         }
 7165:         $resulttext =~ s/,$//;
 7166:     }
 7167:     return $resulttext;
 7168: }
 7169: 
 7170: sub modify_scantron {
 7171:     my ($r,$dom,$confname,%domconfig) = @_;
 7172:     my ($resulttext,%confhash,%changes,$errors);
 7173:     my $custom = 'custom.tab';
 7174:     my $default = 'default.tab';
 7175:     my $servadm = $r->dir_config('lonAdmEMail');
 7176:     my ($configuserok,$author_ok,$switchserver) = 
 7177:         &config_check($dom,$confname,$servadm);
 7178:     if ($env{'form.scantronformat.filename'} ne '') {
 7179:         my $error;
 7180:         if ($configuserok eq 'ok') {
 7181:             if ($switchserver) {
 7182:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7183:             } else {
 7184:                 if ($author_ok eq 'ok') {
 7185:                     my ($result,$scantronurl) =
 7186:                         &publishlogo($r,'upload','scantronformat',$dom,
 7187:                                      $confname,'scantron','','',$custom);
 7188:                     if ($result eq 'ok') {
 7189:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7190:                         $changes{'scantronformat'} = 1;
 7191:                     } else {
 7192:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7193:                     }
 7194:                 } else {
 7195:                     $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);
 7196:                 }
 7197:             }
 7198:         } else {
 7199:             $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);
 7200:         }
 7201:         if ($error) {
 7202:             &Apache::lonnet::logthis($error);
 7203:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7204:         }
 7205:     }
 7206:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7207:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7208:             if ($env{'form.scantronformat_del'}) {
 7209:                 $confhash{'scantron'}{'scantronformat'} = '';
 7210:                 $changes{'scantronformat'} = 1;
 7211:             }
 7212:         }
 7213:     }
 7214:     if (keys(%confhash) > 0) {
 7215:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7216:                                                  $dom);
 7217:         if ($putresult eq 'ok') {
 7218:             if (keys(%changes) > 0) {
 7219:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7220:                     $resulttext = &mt('Changes made:').'<ul>';
 7221:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7222:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7223:                     } else {
 7224:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7225:                     }
 7226:                     $resulttext .= '</ul>';
 7227:                 } else {
 7228:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7229:                 }
 7230:                 $resulttext .= '</ul>';
 7231:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7232:             } else {
 7233:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7234:             }
 7235:         } else {
 7236:             $resulttext = '<span class="LC_error">'.
 7237:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7238:         }
 7239:     } else {
 7240:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7241:     }
 7242:     if ($errors) {
 7243:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7244:                        $errors.'</ul>';
 7245:     }
 7246:     return $resulttext;
 7247: }
 7248: 
 7249: sub modify_coursecategories {
 7250:     my ($dom,%domconfig) = @_;
 7251:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7252:         $cathash);
 7253:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7254:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7255:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7256:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7257:             $changes{'togglecats'} = 1;
 7258:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7259:         }
 7260:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7261:             $changes{'categorize'} = 1;
 7262:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7263:         }
 7264:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7265:             $changes{'togglecatscomm'} = 1;
 7266:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7267:         }
 7268:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7269:             $changes{'categorizecomm'} = 1;
 7270:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7271:         }
 7272:     } else {
 7273:         $changes{'togglecats'} = 1;
 7274:         $changes{'categorize'} = 1;
 7275:         $changes{'togglecatscomm'} = 1;
 7276:         $changes{'categorizecomm'} = 1;
 7277:         $domconfig{'coursecategories'} = {
 7278:                                              togglecats => $env{'form.togglecats'},
 7279:                                              categorize => $env{'form.categorize'},
 7280:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7281:                                              categorizecomm => $env{'form.categorizecomm'},
 7282:                                          };
 7283:     }
 7284:     if (ref($cathash) eq 'HASH') {
 7285:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7286:             push (@deletecategory,'instcode::0');
 7287:         }
 7288:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7289:             push(@deletecategory,'communities::0');
 7290:         }
 7291:     }
 7292:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7293:     if (ref($cathash) eq 'HASH') {
 7294:         if (@deletecategory > 0) {
 7295:             #FIXME Need to remove category from all courses using a deleted category 
 7296:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7297:             foreach my $item (@deletecategory) {
 7298:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7299:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7300:                     $deletions{$item} = 1;
 7301:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7302:                 }
 7303:             }
 7304:         }
 7305:         foreach my $item (keys(%{$cathash})) {
 7306:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7307:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7308:                 $reorderings{$item} = 1;
 7309:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7310:             }
 7311:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7312:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7313:                 my $newdepth = $depth+1;
 7314:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7315:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7316:                 $adds{$newitem} = 1; 
 7317:             }
 7318:             if ($env{'form.subcat_'.$item} ne '') {
 7319:                 my $newcat = $env{'form.subcat_'.$item};
 7320:                 my $newdepth = $depth+1;
 7321:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7322:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7323:                 $adds{$newitem} = 1;
 7324:             }
 7325:         }
 7326:     }
 7327:     if ($env{'form.instcode'} eq '1') {
 7328:         if (ref($cathash) eq 'HASH') {
 7329:             my $newitem = 'instcode::0';
 7330:             if ($cathash->{$newitem} eq '') {  
 7331:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7332:                 $adds{$newitem} = 1;
 7333:             }
 7334:         } else {
 7335:             my $newitem = 'instcode::0';
 7336:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7337:             $adds{$newitem} = 1;
 7338:         }
 7339:     }
 7340:     if ($env{'form.communities'} eq '1') {
 7341:         if (ref($cathash) eq 'HASH') {
 7342:             my $newitem = 'communities::0';
 7343:             if ($cathash->{$newitem} eq '') {
 7344:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7345:                 $adds{$newitem} = 1;
 7346:             }
 7347:         } else {
 7348:             my $newitem = 'communities::0';
 7349:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7350:             $adds{$newitem} = 1;
 7351:         }
 7352:     }
 7353:     if ($env{'form.addcategory_name'} ne '') {
 7354:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7355:             ($env{'form.addcategory_name'} ne 'communities')) {
 7356:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7357:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7358:             $adds{$newitem} = 1;
 7359:         }
 7360:     }
 7361:     my $putresult;
 7362:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7363:         if (keys(%deletions) > 0) {
 7364:             foreach my $key (keys(%deletions)) {
 7365:                 if ($predelallitems{$key} ne '') {
 7366:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7367:                 }
 7368:             }
 7369:         }
 7370:         my (@chkcats,@chktrails,%chkallitems);
 7371:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7372:         if (ref($chkcats[0]) eq 'ARRAY') {
 7373:             my $depth = 0;
 7374:             my $chg = 0;
 7375:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7376:                 my $name = $chkcats[0][$i];
 7377:                 my $item;
 7378:                 if ($name eq '') {
 7379:                     $chg ++;
 7380:                 } else {
 7381:                     $item = &escape($name).'::0';
 7382:                     if ($chg) {
 7383:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7384:                     }
 7385:                     $depth ++; 
 7386:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7387:                     $depth --;
 7388:                 }
 7389:             }
 7390:         }
 7391:     }
 7392:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7393:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7394:         if ($putresult eq 'ok') {
 7395:             my %title = (
 7396:                          togglecats     => 'Show/Hide a course in catalog',
 7397:                          categorize     => 'Assign a category to a course',
 7398:                          togglecatscomm => 'Show/Hide a community in catalog',
 7399:                          categorizecomm => 'Assign a category to a community',
 7400:                         );
 7401:             my %level = (
 7402:                          dom  => 'set in Domain ("Modify Course/Community")',
 7403:                          crs  => 'set in Course ("Course Configuration")',
 7404:                          comm => 'set in Community ("Community Configuration")',
 7405:                         );
 7406:             $resulttext = &mt('Changes made:').'<ul>';
 7407:             if ($changes{'togglecats'}) {
 7408:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7409:             }
 7410:             if ($changes{'categorize'}) {
 7411:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7412:             }
 7413:             if ($changes{'togglecatscomm'}) {
 7414:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7415:             }
 7416:             if ($changes{'categorizecomm'}) {
 7417:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7418:             }
 7419:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7420:                 my $cathash;
 7421:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7422:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7423:                 } else {
 7424:                     $cathash = {};
 7425:                 } 
 7426:                 my (@cats,@trails,%allitems);
 7427:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7428:                 if (keys(%deletions) > 0) {
 7429:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7430:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7431:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7432:                     }
 7433:                     $resulttext .= '</ul></li>';
 7434:                 }
 7435:                 if (keys(%reorderings) > 0) {
 7436:                     my %sort_by_trail;
 7437:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7438:                     foreach my $key (keys(%reorderings)) {
 7439:                         if ($allitems{$key} ne '') {
 7440:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7441:                         }
 7442:                     }
 7443:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7444:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7445:                     }
 7446:                     $resulttext .= '</ul></li>';
 7447:                 }
 7448:                 if (keys(%adds) > 0) {
 7449:                     my %sort_by_trail;
 7450:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7451:                     foreach my $key (keys(%adds)) {
 7452:                         if ($allitems{$key} ne '') {
 7453:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7454:                         }
 7455:                     }
 7456:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7457:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7458:                     }
 7459:                     $resulttext .= '</ul></li>';
 7460:                 }
 7461:             }
 7462:             $resulttext .= '</ul>';
 7463:         } else {
 7464:             $resulttext = '<span class="LC_error">'.
 7465:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7466:         }
 7467:     } else {
 7468:         $resulttext = &mt('No changes made to course and community categories');
 7469:     }
 7470:     return $resulttext;
 7471: }
 7472: 
 7473: sub modify_serverstatuses {
 7474:     my ($dom,%domconfig) = @_;
 7475:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7476:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7477:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7478:     }
 7479:     my @pages = &serverstatus_pages();
 7480:     foreach my $type (@pages) {
 7481:         $newserverstatus{$type}{'namedusers'} = '';
 7482:         $newserverstatus{$type}{'machines'} = '';
 7483:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7484:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7485:             my @okusers;
 7486:             foreach my $user (@users) {
 7487:                 my ($uname,$udom) = split(/:/,$user);
 7488:                 if (($udom =~ /^$match_domain$/) &&   
 7489:                     (&Apache::lonnet::domain($udom)) &&
 7490:                     ($uname =~ /^$match_username$/)) {
 7491:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7492:                         push(@okusers,$user);
 7493:                     }
 7494:                 }
 7495:             }
 7496:             if (@okusers > 0) {
 7497:                  @okusers = sort(@okusers);
 7498:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7499:             }
 7500:         }
 7501:         if (defined($env{'form.'.$type.'_machines'})) {
 7502:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7503:             my @okmachines;
 7504:             foreach my $ip (@machines) {
 7505:                 my @parts = split(/\./,$ip);
 7506:                 next if (@parts < 4);
 7507:                 my $badip = 0;
 7508:                 for (my $i=0; $i<4; $i++) {
 7509:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7510:                         $badip = 1;
 7511:                         last;
 7512:                     }
 7513:                 }
 7514:                 if (!$badip) {
 7515:                     push(@okmachines,$ip);     
 7516:                 }
 7517:             }
 7518:             @okmachines = sort(@okmachines);
 7519:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7520:         }
 7521:     }
 7522:     my %serverstatushash =  (
 7523:                                 serverstatuses => \%newserverstatus,
 7524:                             );
 7525:     foreach my $type (@pages) {
 7526:         foreach my $setting ('namedusers','machines') {
 7527:             my (@current,@new);
 7528:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7529:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7530:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7531:                 }
 7532:             }
 7533:             if ($newserverstatus{$type}{$setting} ne '') {
 7534:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7535:             }
 7536:             if (@current > 0) {
 7537:                 if (@new > 0) {
 7538:                     foreach my $item (@current) {
 7539:                         if (!grep(/^\Q$item\E$/,@new)) {
 7540:                             $changes{$type}{$setting} = 1;
 7541:                             last;
 7542:                         }
 7543:                     }
 7544:                     foreach my $item (@new) {
 7545:                         if (!grep(/^\Q$item\E$/,@current)) {
 7546:                             $changes{$type}{$setting} = 1;
 7547:                             last;
 7548:                         }
 7549:                     }
 7550:                 } else {
 7551:                     $changes{$type}{$setting} = 1;
 7552:                 }
 7553:             } elsif (@new > 0) {
 7554:                 $changes{$type}{$setting} = 1;
 7555:             }
 7556:         }
 7557:     }
 7558:     if (keys(%changes) > 0) {
 7559:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7560:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7561:                                                  \%serverstatushash,$dom);
 7562:         if ($putresult eq 'ok') {
 7563:             $resulttext .= &mt('Changes made:').'<ul>';
 7564:             foreach my $type (@pages) {
 7565:                 if (ref($changes{$type}) eq 'HASH') {
 7566:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7567:                     if ($changes{$type}{'namedusers'}) {
 7568:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7569:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7570:                         } else {
 7571:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7572:                         }
 7573:                     }
 7574:                     if ($changes{$type}{'machines'}) {
 7575:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7576:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7577:                         } else {
 7578:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7579:                         }
 7580: 
 7581:                     }
 7582:                     $resulttext .= '</ul></li>';
 7583:                 }
 7584:             }
 7585:             $resulttext .= '</ul>';
 7586:         } else {
 7587:             $resulttext = '<span class="LC_error">'.
 7588:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7589: 
 7590:         }
 7591:     } else {
 7592:         $resulttext = &mt('No changes made to access to server status pages');
 7593:     }
 7594:     return $resulttext;
 7595: }
 7596: 
 7597: sub modify_helpsettings {
 7598:     my ($r,$dom,$confname,%domconfig) = @_;
 7599:     my ($resulttext,$errors,%changes,%helphash);
 7600:     my %defaultchecked = ('submitbugs' => 'on');
 7601:     my @offon = ('off','on');
 7602:     my @toggles = ('submitbugs');
 7603:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7604:         foreach my $item (@toggles) {
 7605:             if ($defaultchecked{$item} eq 'on') { 
 7606:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7607:                     if ($env{'form.'.$item} eq '0') {
 7608:                         $changes{$item} = 1;
 7609:                     }
 7610:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7611:                     $changes{$item} = 1;
 7612:                 }
 7613:             } elsif ($defaultchecked{$item} eq 'off') {
 7614:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7615:                     if ($env{'form.'.$item} eq '1') {
 7616:                         $changes{$item} = 1;
 7617:                     }
 7618:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7619:                     $changes{$item} = 1;
 7620:                 }
 7621:             }
 7622:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) { 
 7623:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7624:             }
 7625:         }
 7626:     }
 7627:     my $putresult;
 7628:     if (keys(%changes) > 0) {
 7629:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7630:         if ($putresult eq 'ok') {
 7631:             $resulttext = &mt('Changes made:').'<ul>';
 7632:             foreach my $item (sort(keys(%changes))) {
 7633:                 if ($item eq 'submitbugs') {
 7634:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 7635:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 7636:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 7637:                 }
 7638:             }
 7639:             $resulttext .= '</ul>';
 7640:         } else {
 7641:             $resulttext = &mt('No changes made to help settings');
 7642:             $errors .= '<li><span class="LC_error">'.
 7643:                        &mt('An error occurred storing the settings: [_1]',
 7644:                            $putresult).'</span></li>';
 7645:         }
 7646:     }
 7647:     if ($errors) {
 7648:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7649:                        $errors.'</ul>';
 7650:     }
 7651:     return $resulttext;
 7652: }
 7653: 
 7654: sub modify_coursedefaults {
 7655:     my ($dom,%domconfig) = @_;
 7656:     my ($resulttext,$errors,%changes,%defaultshash);
 7657:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7658:     my @offon = ('off','on');
 7659:     my @toggles = ('canuse_pdfforms');
 7660: 
 7661:     $defaultshash{'coursedefaults'} = {};
 7662: 
 7663:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7664:         if ($domconfig{'coursedefaults'} eq '') {
 7665:             $domconfig{'coursedefaults'} = {};
 7666:         }
 7667:     }
 7668: 
 7669:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7670:         foreach my $item (@toggles) {
 7671:             if ($defaultchecked{$item} eq 'on') {
 7672:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7673:                     ($env{'form.'.$item} eq '0')) {
 7674:                     $changes{$item} = 1;
 7675:                 } elsif ($domconfig{'coursdefaults'}{$item} ne $env{'form.'.$item}) {
 7676:                     $changes{$item} = 1;
 7677:                 }
 7678:             } elsif ($defaultchecked{$item} eq 'off') {
 7679:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7680:                     ($env{'form.'.$item} eq '1')) {
 7681:                     $changes{$item} = 1;
 7682:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7683:                     $changes{$item} = 1;
 7684:                 }
 7685:             }
 7686:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7687:         }
 7688:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7689:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7690:         $newdefresponder =~ s/\D//g;
 7691:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7692:             $newdefresponder = 1;
 7693:         }
 7694:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7695:         if ($currdefresponder ne $newdefresponder) {
 7696:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7697:                 $changes{'anonsurvey_threshold'} = 1;
 7698:             }
 7699:         }
 7700:     }
 7701:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7702:                                              $dom);
 7703:     if ($putresult eq 'ok') {
 7704:         if (keys(%changes) > 0) {
 7705:             if ($changes{'canuse_pdfforms'}) {
 7706:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7707:                 $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7708:                 my $cachetime = 24*60*60;
 7709:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7710:             }
 7711:             $resulttext = &mt('Changes made:').'<ul>';
 7712:             foreach my $item (sort(keys(%changes))) {
 7713:                 if ($item eq 'canuse_pdfforms') {
 7714:                     if ($env{'form.'.$item} eq '1') {
 7715:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7716:                     } else {
 7717:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7718:                     }
 7719:                 } elsif ($item eq 'anonsurvey_threshold') {
 7720:                         $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7721:                 }
 7722:             }
 7723:             $resulttext .= '</ul>';
 7724:         } else {
 7725:             $resulttext = &mt('No changes made to course defaults');
 7726:         }
 7727:     } else {
 7728:         $resulttext = '<span class="LC_error">'.
 7729:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7730:     }
 7731:     return $resulttext;
 7732: }
 7733: 
 7734: sub modify_usersessions {
 7735:     my ($dom,%domconfig) = @_;
 7736:     my @hostingtypes = ('version','excludedomain','includedomain');
 7737:     my @offloadtypes = ('primary','default');
 7738:     my %types = (
 7739:                   remote => \@hostingtypes,
 7740:                   hosted => \@hostingtypes,
 7741:                   spares => \@offloadtypes,
 7742:                 );
 7743:     my @prefixes = ('remote','hosted','spares');
 7744:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7745:     my (%by_ip,%by_location,@intdoms);
 7746:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7747:     my @locations = sort(keys(%by_location));
 7748:     my (%defaultshash,%changes);
 7749:     foreach my $prefix (@prefixes) {
 7750:         $defaultshash{'usersessions'}{$prefix} = {};
 7751:     }
 7752:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7753:     my $resulttext;
 7754:     my %iphost = &Apache::lonnet::get_iphost();
 7755:     foreach my $prefix (@prefixes) {
 7756:         next if ($prefix eq 'spares');
 7757:         foreach my $type (@{$types{$prefix}}) {
 7758:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7759:             if ($type eq 'version') {
 7760:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7761:                 my $okvalue;
 7762:                 if ($value ne '') {
 7763:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 7764:                         $okvalue = $value;
 7765:                     }
 7766:                 }
 7767:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7768:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7769:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 7770:                             if ($inuse == 0) {
 7771:                                 $changes{$prefix}{$type} = 1;
 7772:                             } else {
 7773:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 7774:                                     $changes{$prefix}{$type} = 1;
 7775:                                 }
 7776:                                 if ($okvalue ne '') {
 7777:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7778:                                 } 
 7779:                             }
 7780:                         } else {
 7781:                             if (($inuse == 1) && ($okvalue ne '')) {
 7782:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7783:                                 $changes{$prefix}{$type} = 1;
 7784:                             }
 7785:                         }
 7786:                     } else {
 7787:                         if (($inuse == 1) && ($okvalue ne '')) {
 7788:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7789:                             $changes{$prefix}{$type} = 1;
 7790:                         }
 7791:                     }
 7792:                 } else {
 7793:                     if (($inuse == 1) && ($okvalue ne '')) {
 7794:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7795:                         $changes{$prefix}{$type} = 1;
 7796:                     }
 7797:                 }
 7798:             } else {
 7799:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 7800:                 my @okvals;
 7801:                 foreach my $val (@vals) {
 7802:                     if ($val =~ /:/) {
 7803:                         my @items = split(/:/,$val);
 7804:                         foreach my $item (@items) {
 7805:                             if (ref($by_location{$item}) eq 'ARRAY') {
 7806:                                 push(@okvals,$item);
 7807:                             }
 7808:                         }
 7809:                     } else {
 7810:                         if (ref($by_location{$val}) eq 'ARRAY') {
 7811:                             push(@okvals,$val);
 7812:                         }
 7813:                     }
 7814:                 }
 7815:                 @okvals = sort(@okvals);
 7816:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7817:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7818:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7819:                             if ($inuse == 0) {
 7820:                                 $changes{$prefix}{$type} = 1; 
 7821:                             } else {
 7822:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7823:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 7824:                                 if (@changed > 0) {
 7825:                                     $changes{$prefix}{$type} = 1;
 7826:                                 }
 7827:                             }
 7828:                         } else {
 7829:                             if ($inuse == 1) {
 7830:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7831:                                 $changes{$prefix}{$type} = 1;
 7832:                             }
 7833:                         } 
 7834:                     } else {
 7835:                         if ($inuse == 1) {
 7836:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7837:                             $changes{$prefix}{$type} = 1;
 7838:                         }
 7839:                     }
 7840:                 } else {
 7841:                     if ($inuse == 1) {
 7842:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7843:                         $changes{$prefix}{$type} = 1;
 7844:                     }
 7845:                 }
 7846:             }
 7847:         }
 7848:     }
 7849: 
 7850:     my @alldoms = &Apache::lonnet::all_domains();
 7851:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7852:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 7853:     my $savespares;
 7854: 
 7855:     foreach my $lonhost (sort(keys(%servers))) {
 7856:         my $serverhomeID =
 7857:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 7858:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 7859:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 7860:         my %spareschg;
 7861:         foreach my $type (@{$types{'spares'}}) {
 7862:             my @okspares;
 7863:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 7864:             foreach my $server (@checked) {
 7865:                 if (&Apache::lonnet::hostname($server) ne '') {
 7866:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 7867:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 7868:                             push(@okspares,$server);
 7869:                         }
 7870:                     }
 7871:                 }
 7872:             }
 7873:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 7874:             my $newspare;
 7875:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 7876:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 7877:                     $newspare = $new;
 7878:                 }
 7879:             }
 7880:             my @spares;
 7881:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 7882:                 @spares = sort(@okspares,$newspare);
 7883:             } else {
 7884:                 @spares = sort(@okspares);
 7885:             }
 7886:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 7887:             if (ref($spareid{$lonhost}) eq 'HASH') {
 7888:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 7889:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 7890:                     if (@diffs > 0) {
 7891:                         $spareschg{$type} = 1;
 7892:                     }
 7893:                 }
 7894:             }
 7895:         }
 7896:         if (keys(%spareschg) > 0) {
 7897:             $changes{'spares'}{$lonhost} = \%spareschg;
 7898:         }
 7899:     }
 7900: 
 7901:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7902:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 7903:             if (ref($changes{'spares'}) eq 'HASH') {
 7904:                 if (keys(%{$changes{'spares'}}) > 0) {
 7905:                     $savespares = 1;
 7906:                 }
 7907:             }
 7908:         } else {
 7909:             $savespares = 1;
 7910:         }
 7911:     }
 7912: 
 7913:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 7914:     if ((keys(%changes) > 0) || ($savespares)) {
 7915:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7916:                                                  $dom);
 7917:         if ($putresult eq 'ok') {
 7918:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 7919:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 7920:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 7921:                 }
 7922:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 7923:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 7924:                 }
 7925:             }
 7926:             my $cachetime = 24*60*60;
 7927:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7928:             if (keys(%changes) > 0) {
 7929:                 my %lt = &usersession_titles();
 7930:                 $resulttext = &mt('Changes made:').'<ul>';
 7931:                 foreach my $prefix (@prefixes) {
 7932:                     if (ref($changes{$prefix}) eq 'HASH') {
 7933:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 7934:                         if ($prefix eq 'spares') {
 7935:                             if (ref($changes{$prefix}) eq 'HASH') {
 7936:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 7937:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 7938:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 7939:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 7940:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 7941:                                         foreach my $type (@{$types{$prefix}}) {
 7942:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 7943:                                                 my $offloadto = &mt('None');
 7944:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 7945:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 7946:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 7947:                                                     }
 7948:                                                 }
 7949:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 7950:                                             }
 7951:                                         }
 7952:                                     }
 7953:                                     $resulttext .= '</li>';
 7954:                                 }
 7955:                             }
 7956:                         } else {
 7957:                             foreach my $type (@{$types{$prefix}}) {
 7958:                                 if (defined($changes{$prefix}{$type})) {
 7959:                                     my $newvalue;
 7960:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 7961:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 7962:                                             if ($type eq 'version') {
 7963:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 7964:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7965:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 7966:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 7967:                                                 }
 7968:                                             }
 7969:                                         }
 7970:                                     }
 7971:                                     if ($newvalue eq '') {
 7972:                                         if ($type eq 'version') {
 7973:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 7974:                                         } else {
 7975:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 7976:                                         }
 7977:                                     } else {
 7978:                                         if ($type eq 'version') {
 7979:                                             $newvalue .= ' '.&mt('(or later)'); 
 7980:                                         }
 7981:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 7982:                                     }
 7983:                                 }
 7984:                             }
 7985:                         }
 7986:                         $resulttext .= '</ul>';
 7987:                     }
 7988:                 }
 7989:                 $resulttext .= '</ul>';
 7990:             } else {
 7991:                 $resulttext = $nochgmsg;
 7992:             }
 7993:         } else {
 7994:             $resulttext = '<span class="LC_error">'.
 7995:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7996:         }
 7997:     } else {
 7998:         $resulttext = $nochgmsg;
 7999:     }
 8000:     return $resulttext;
 8001: }
 8002: 
 8003: sub modify_loadbalancing {
 8004:     my ($dom,%domconfig) = @_;
 8005:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8006:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8007:     my ($othertitle,$usertypes,$types) =
 8008:         &Apache::loncommon::sorted_inst_types($dom);
 8009:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8010:     my @sparestypes = ('primary','default');
 8011:     my %typetitles = &sparestype_titles();
 8012:     my $resulttext;
 8013:     if (keys(%servers) > 1) {
 8014:         my ($currbalancer,$currtargets,$currrules);
 8015:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8016:             $currbalancer = $domconfig{'loadbalancing'}{'lonhost'};
 8017:             $currtargets = $domconfig{'loadbalancing'}{'targets'};
 8018:             $currrules = $domconfig{'loadbalancing'}{'rules'};
 8019:         } else {
 8020:             ($currbalancer,$currtargets) = 
 8021:                 &Apache::lonnet::get_lonbalancer_config(\%servers);
 8022:         }
 8023:         my ($saveloadbalancing,%defaultshash,%changes);
 8024:         my ($alltypes,$othertypes,$titles) =
 8025:             &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8026:         my %ruletitles = &offloadtype_text();
 8027:         my $balancer = $env{'form.loadbalancing_lonhost'};
 8028:         if (!$servers{$balancer}) {
 8029:             undef($balancer);
 8030:         }
 8031:         if ($currbalancer ne $balancer) {
 8032:             $changes{'lonhost'} = 1;
 8033:         }
 8034:         $defaultshash{'loadbalancing'}{'lonhost'} = $balancer;
 8035:         if ($balancer ne '') {
 8036:             unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8037:                 $saveloadbalancing = 1;
 8038:             }
 8039:             foreach my $sparetype (@sparestypes) {
 8040:                 my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$sparetype);
 8041:                 my @offloadto;
 8042:                 foreach my $target (@targets) {
 8043:                     if (($servers{$target}) && ($target ne $balancer)) {
 8044:                         if ($sparetype eq 'default') {
 8045:                             if (ref($defaultshash{'loadbalancing'}{'targets'}{'primary'}) eq 'ARRAY') {
 8046:                                 next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{'targets'}{'primary'}}));
 8047:                             }
 8048:                         }
 8049:                         unless(grep(/^\Q$target\E$/,@offloadto)) {
 8050:                             push(@offloadto,$target);
 8051:                         }
 8052:                     }
 8053:                     $defaultshash{'loadbalancing'}{'targets'}{$sparetype} = \@offloadto;
 8054:                 }
 8055:             }
 8056:         } else {
 8057:             foreach my $sparetype (@sparestypes) {
 8058:                 $defaultshash{'loadbalancing'}{'targets'}{$sparetype} = [];
 8059:             }
 8060:         }
 8061:         if (ref($currtargets) eq 'HASH') {
 8062:             foreach my $sparetype (@sparestypes) {
 8063:                 if (ref($currtargets->{$sparetype}) eq 'ARRAY') {
 8064:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets->{$sparetype},$defaultshash{'loadbalancing'}{'targets'}{$sparetype});
 8065:                     if (@targetdiffs > 0) {
 8066:                         $changes{'targets'} = 1;
 8067:                     }
 8068:                 } elsif (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 8069:                     if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 8070:                         $changes{'targets'} = 1;
 8071:                     }
 8072:                 }
 8073:             }
 8074:         } else {
 8075:             foreach my $sparetype (@sparestypes) {
 8076:                 if (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 8077:                     if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 8078:                         $changes{'targets'} = 1;  
 8079:                     }
 8080:                 }
 8081:             }  
 8082:         }
 8083:         my $ishomedom;
 8084:         if ($balancer ne '') {
 8085:             if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 8086:                 $ishomedom = 1;
 8087:             }
 8088:         }
 8089:         if (ref($alltypes) eq 'ARRAY') {
 8090:             foreach my $type (@{$alltypes}) {
 8091:                 my $rule;
 8092:                 if ($balancer ne '') {
 8093:                     unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) && 
 8094:                          (!$ishomedom)) {
 8095:                         $rule = $env{'form.loadbalancing_rules_'.$type};
 8096:                     }
 8097:                     if ($rule eq 'specific') {
 8098:                         $rule = $env{'form.loadbalancing_singleserver_'.$type};
 8099:                     }
 8100:                 }
 8101:                 $defaultshash{'loadbalancing'}{'rules'}{$type} = $rule;
 8102:                 if (ref($currrules) eq 'HASH') {
 8103:                     if ($rule ne $currrules->{$type}) {
 8104:                         $changes{'rules'}{$type} = 1;
 8105:                     }
 8106:                 } elsif ($rule ne '') {
 8107:                     $changes{'rules'}{$type} = 1;
 8108:                 }
 8109:             }
 8110:         }
 8111:         my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8112:         if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8113:             my $putresult = &Apache::lonnet::put_dom('configuration',
 8114:                                                      \%defaultshash,$dom);
 8115:             if ($putresult eq 'ok') {
 8116:                 if (keys(%changes) > 0) {
 8117:                     if ($changes{'lonhost'}) {
 8118:                         if ($currbalancer ne '') {
 8119:                             &Apache::lonnet::remote_devalidate_cache($currbalancer,'loadbalancing',$dom);
 8120:                         }
 8121:                         if ($balancer eq '') {
 8122:                             $resulttext .= '<li>'.&mt('Load Balancing with dedicated server discontinued').'</li>'; 
 8123:                         } else {
 8124:                             &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8125:                             $resulttext .= '<li>'.&mt('Dedicated Load Balancer server set to [_1]',$balancer);
 8126:                         }
 8127:                     } else {
 8128:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8129:                     }
 8130:                     if (($changes{'targets'}) && ($balancer ne '')) {
 8131:                         my %offloadstr;
 8132:                         foreach my $sparetype (@sparestypes) {
 8133:                             if (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 8134:                                 if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 8135:                                     $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}});
 8136:                                 }
 8137:                             }
 8138:                         }
 8139:                         if (keys(%offloadstr) == 0) {
 8140:                             $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8141:                         } else {
 8142:                             my $showoffload;
 8143:                             foreach my $sparetype (@sparestypes) {
 8144:                                 $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8145:                                 if (defined($offloadstr{$sparetype})) {
 8146:                                     $showoffload .= $offloadstr{$sparetype};
 8147:                                 } else {
 8148:                                     $showoffload .= &mt('None');
 8149:                                 }
 8150:                                 $showoffload .= ('&nbsp;'x3);
 8151:                             }
 8152:                             $resulttext .= '<li>'.&mt('By default, Load Balancer server set to offload to: [_1]',$showoffload).'</li>';
 8153:                         }
 8154:                     }
 8155:                     if ((ref($changes{'rules'}) eq 'HASH') && ($balancer ne '')) {
 8156:                         if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8157:                             foreach my $type (@{$alltypes}) {
 8158:                                 if ($changes{'rules'}{$type}) {
 8159:                                     my $rule = $defaultshash{'loadbalancing'}{'rules'}{$type};
 8160:                                     my $balancetext;
 8161:                                     if ($rule eq '') {
 8162:                                         $balancetext =  $ruletitles{'default'};
 8163:                                     } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 8164:                                         $balancetext =  $ruletitles{$rule};
 8165:                                     } else {
 8166:                                         $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{'rules'}{$type});
 8167:                                     }
 8168:                                     $resulttext .= '<li>'.&mt('Load Balancing for [_1] set to: [_2]',$titles->{$type},$balancetext).'</li>';     
 8169:                                 }
 8170:                             }
 8171:                         }
 8172:                     }
 8173:                     if ($resulttext ne '') {
 8174:                         $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8175:                     } else {
 8176:                         $resulttext = $nochgmsg;
 8177:                     }
 8178:                 } else {
 8179:                     $resulttext = $nochgmsg;
 8180:                     if ($balancer ne '') {
 8181:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8182:                     }
 8183:                 }
 8184:             } else {
 8185:                 $resulttext = '<span class="LC_error">'.
 8186:                               &mt('An error occurred: [_1]',$putresult).'</span>';
 8187:             }
 8188:         } else {
 8189:             $resulttext = $nochgmsg;
 8190:         }
 8191:     } else {
 8192:         $resulttext =  &mt('Load Balancing unavailable as this domain only has one server.');
 8193:     }
 8194:     return $resulttext;
 8195: }
 8196: 
 8197: sub recurse_check {
 8198:     my ($chkcats,$categories,$depth,$name) = @_;
 8199:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8200:         my $chg = 0;
 8201:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8202:             my $category = $chkcats->[$depth]{$name}[$j];
 8203:             my $item;
 8204:             if ($category eq '') {
 8205:                 $chg ++;
 8206:             } else {
 8207:                 my $deeper = $depth + 1;
 8208:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8209:                 if ($chg) {
 8210:                     $categories->{$item} -= $chg;
 8211:                 }
 8212:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8213:                 $deeper --;
 8214:             }
 8215:         }
 8216:     }
 8217:     return;
 8218: }
 8219: 
 8220: sub recurse_cat_deletes {
 8221:     my ($item,$coursecategories,$deletions) = @_;
 8222:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8223:     my $subdepth = $depth + 1;
 8224:     if (ref($coursecategories) eq 'HASH') {
 8225:         foreach my $subitem (keys(%{$coursecategories})) {
 8226:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8227:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8228:                 delete($coursecategories->{$subitem});
 8229:                 $deletions->{$subitem} = 1;
 8230:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8231:             }
 8232:         }
 8233:     }
 8234:     return;
 8235: }
 8236: 
 8237: sub get_active_dcs {
 8238:     my ($dom) = @_;
 8239:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc']);
 8240:     my %domcoords;
 8241:     my $numdcs = 0;
 8242:     my $now = time;
 8243:     foreach my $server (keys(%dompersonnel)) {
 8244:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8245:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8246:             my ($end,$start) = split(':',$dompersonnel{$server}{$user});
 8247:             if (($end eq '') || ($end == 0) || ($end > $now)) {
 8248:                 if ($start <= $now) {
 8249:                     $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8250:                 }
 8251:             }
 8252:         }
 8253:     }
 8254:     return %domcoords;
 8255: }
 8256: 
 8257: sub active_dc_picker {
 8258:     my ($dom,$curr_dc) = @_;
 8259:     my %domcoords = &get_active_dcs($dom); 
 8260:     my @dcs = sort(keys(%domcoords));
 8261:     my $numdcs = scalar(@dcs); 
 8262:     my $datatable;
 8263:     my $numinrow = 2;
 8264:     if ($numdcs > 1) {
 8265:         $datatable = '<table>';
 8266:         for (my $i=0; $i<@dcs; $i++) {
 8267:             my $rem = $i%($numinrow);
 8268:             if ($rem == 0) {
 8269:                 if ($i > 0) {
 8270:                     $datatable .= '</tr>';
 8271:                 }
 8272:                 $datatable .= '<tr>';
 8273:             }
 8274:             my $check = ' ';
 8275:             if ($curr_dc eq '') {
 8276:                 if (!$i) { 
 8277:                     $check = ' checked="checked" ';
 8278:                 }
 8279:             } elsif ($dcs[$i] eq $curr_dc) {
 8280:                 $check = ' checked="checked" ';
 8281:             }
 8282:             if ($i == @dcs - 1) {
 8283:                 my $colsleft = $numinrow - $rem;
 8284:                 if ($colsleft > 1) {
 8285:                     $datatable .= '<td colspan="'.$colsleft.'">';
 8286:                 } else {
 8287:                     $datatable .= '<td>';
 8288:                 }
 8289:             } else {
 8290:                 $datatable .= '<td>';
 8291:             }
 8292:             my ($dcname,$dcdom) = split(':',$dcs[$i]);
 8293:             $datatable .= '<span class="LC_nobreak"><label>'.
 8294:                           '<input type="radio" name="autocreate_xmldc"'.
 8295:                           ' value="'.$dcs[$i].'"'.$check.'/>'.
 8296:                           &Apache::loncommon::plainname($dcname,$dcdom).
 8297:                           '</label></span></td>';
 8298:         }
 8299:         $datatable .= '</tr></table>';
 8300:     } elsif (@dcs) {
 8301:         $datatable .= '<input type="hidden" name="autocreate_dc" value="'.
 8302:                       $dcs[0].'" />';
 8303:     }
 8304:     return ($numdcs,$datatable);
 8305: }
 8306: 
 8307: sub usersession_titles {
 8308:     return &Apache::lonlocal::texthash(
 8309:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8310:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8311:                spares => 'Servers offloaded to, when busy',
 8312:                version => 'LON-CAPA version requirement',
 8313:                excludedomain => 'Allow all, but exclude specific domains',
 8314:                includedomain => 'Deny all, but include specific domains',
 8315:                primary => 'Primary (checked first)',
 8316:                default => 'Default',
 8317:            );
 8318: }
 8319: 
 8320: sub id_for_thisdom {
 8321:     my (%servers) = @_;
 8322:     my %altids;
 8323:     foreach my $server (keys(%servers)) {
 8324:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8325:         if ($serverhome ne $server) {
 8326:             $altids{$serverhome} = $server;
 8327:         }
 8328:     }
 8329:     return %altids;
 8330: }
 8331: 
 8332: sub count_servers {
 8333:     my ($currbalancer,%servers) = @_;
 8334:     my (@spares,$numspares);
 8335:     foreach my $lonhost (sort(keys(%servers))) {
 8336:         next if ($currbalancer eq $lonhost);
 8337:         push(@spares,$lonhost);
 8338:     }
 8339:     if ($currbalancer) {
 8340:         $numspares = scalar(@spares);
 8341:     } else {
 8342:         $numspares = scalar(@spares) - 1;
 8343:     }
 8344:     return ($numspares,@spares);
 8345: }
 8346: 
 8347: sub lonbalance_targets_js {
 8348:     my ($dom,$types,$servers) = @_;
 8349:     my $select = &mt('Select');
 8350:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8351:     if (ref($servers) eq 'HASH') {
 8352:         $alltargets = join("','",sort(keys(%{$servers})));
 8353:         my @homedoms;
 8354:         foreach my $server (sort(keys(%{$servers}))) {
 8355:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8356:                 push(@homedoms,'1');
 8357:             } else {
 8358:                 push(@homedoms,'0');
 8359:             }
 8360:         }
 8361:         $allishome = join("','",@homedoms);
 8362:     }
 8363:     if (ref($types) eq 'ARRAY') {
 8364:         if (@{$types} > 0) {
 8365:             @alltypes = @{$types};
 8366:         }
 8367:     }
 8368:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8369:     $allinsttypes = join("','",@alltypes);
 8370:     return <<"END";
 8371: 
 8372: <script type="text/javascript">
 8373: // <![CDATA[
 8374: 
 8375: function toggleTargets() {
 8376:     var balancer = document.display.loadbalancing_lonhost.options[document.display.loadbalancing_lonhost.selectedIndex].value;
 8377:     if (balancer == '') {
 8378:         hideSpares();
 8379:     } else {
 8380:         var homedoms = new Array('$allishome');
 8381:         var ishomedom = homedoms[document.display.loadbalancing_lonhost.selectedIndex];
 8382:         showSpares(balancer,ishomedom);
 8383:     }
 8384:     return;
 8385: }
 8386: 
 8387: function showSpares(balancer,ishomedom) {
 8388:     var alltargets = new Array('$alltargets');
 8389:     var insttypes = new Array('$allinsttypes');
 8390:     var offloadtypes = new Array('primary','default');
 8391: 
 8392:     document.getElementById('loadbalancing_targets').style.display='block';
 8393:     document.getElementById('loadbalancing_disabled').style.display='none';
 8394:  
 8395:     for (var i=0; i<offloadtypes.length; i++) {
 8396:         var count = 0;
 8397:         for (var j=0; j<alltargets.length; j++) {
 8398:             if (alltargets[j] != balancer) {
 8399:                 document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+count).value = alltargets[j];
 8400:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).style.textAlign='left';
 8401:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).style.textFace='normal';
 8402:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8403:                 count ++;
 8404:             }
 8405:         }
 8406:     }
 8407:     for (var k=0; k<insttypes.length; k++) {
 8408:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8409:             if (ishomedom == 1) {
 8410:                 document.getElementById('balanceruletitle_'+insttypes[k]).style.display='block';
 8411:                 document.getElementById('balancerule_'+insttypes[k]).style.display='block';
 8412:             } else {
 8413:                 document.getElementById('balanceruletitle_'+insttypes[k]).style.display='none';
 8414:                 document.getElementById('balancerule_'+insttypes[k]).style.display='none';
 8415: 
 8416:             }
 8417:         } else {
 8418:             document.getElementById('balanceruletitle_'+insttypes[k]).style.display='block';
 8419:             document.getElementById('balancerule_'+insttypes[k]).style.display='block';
 8420:         }
 8421:         if ((insttypes[k] != '_LC_external') && 
 8422:             ((insttypes[k] != '_LC_internetdom') ||
 8423:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8424:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[0] = new Option("","",true,true);
 8425:             for (var m=0; m<alltargets.length; m++) {
 8426:                 var idx = m+1;
 8427:                 if (alltargets[m] != balancer) {
 8428:                     document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8429:                 }
 8430:             }
 8431:         }
 8432:     }
 8433:     return;
 8434: }
 8435: 
 8436: function hideSpares() {
 8437:     var alltargets = new Array('$alltargets');
 8438:     var insttypes = new Array('$allinsttypes');
 8439:     var offloadtypes = new Array('primary','default');
 8440: 
 8441:     document.getElementById('loadbalancing_targets').style.display='none';
 8442:     document.getElementById('loadbalancing_disabled').style.display='block';
 8443: 
 8444:     var total = alltargets.length - 1;
 8445:     for (var i=0; i<offloadtypes; i++) {
 8446:         for (var j=0; j<total; j++) {
 8447:            document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+j).checked = false;
 8448:            document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+j).value = '';
 8449:            document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8450:         }
 8451:     }
 8452:     for (var k=0; k<insttypes.length; k++) {
 8453:         document.getElementById('balanceruletitle_'+insttypes[k]).style.display='none';
 8454:         document.getElementById('balancerule_'+insttypes[k]).style.display='none';
 8455:         if (insttypes[k] != '_LC_external') {
 8456:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).length = 0;
 8457:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[0] = new Option("","",true,true);
 8458:         }
 8459:     }
 8460:     return;
 8461: }
 8462: 
 8463: function checkOffloads(item,type) {
 8464:     var alltargets = new Array('$alltargets');
 8465:     var offloadtypes = new Array('primary','default');
 8466:     if (item.checked) {
 8467:         var total = alltargets.length - 1;
 8468:         var other;
 8469:         if (type == offloadtypes[0]) {
 8470:             other = offloadtypes[1];
 8471:         } else {
 8472:             other = offloadtypes[0];
 8473:         }
 8474:         for (var i=0; i<total; i++) {
 8475:             var server = document.getElementById('loadbalancing_target_'+other+'_'+i).value;
 8476:             if (server == item.value) {
 8477:                 if (document.getElementById('loadbalancing_target_'+other+'_'+i).checked) {
 8478:                     document.getElementById('loadbalancing_target_'+other+'_'+i).checked = false;
 8479:                 }
 8480:             }
 8481:         }
 8482:     }
 8483:     return;
 8484: }
 8485: 
 8486: function singleServerToggle(type) {
 8487:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+type).selectedIndex;
 8488:     if (offloadtoSelIdx == 0) {
 8489:         document.getElementById('loadbalancing_rules_'+type+'_0').checked = true;
 8490:         document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '';
 8491: 
 8492:     } else {
 8493:         document.getElementById('loadbalancing_rules_'+type+'_2').checked = true;
 8494:         document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '$select';
 8495:     }
 8496:     return;
 8497: }
 8498: 
 8499: function balanceruleChange(formname,type) {
 8500:     if (type == '_LC_external') {
 8501:         return; 
 8502:     }
 8503:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+type);
 8504:     for (var i=0; i<typesRules.length; i++) {
 8505:         if (formname.elements[typesRules[i]].checked) {
 8506:             if (formname.elements[typesRules[i]].value != 'specific') {
 8507:                 document.getElementById('loadbalancing_singleserver_'+type).selectedIndex = 0;
 8508:                 document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '';
 8509:             } else {
 8510:                 document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '$select';
 8511:             }
 8512:         }
 8513:     }
 8514:     return;
 8515: }
 8516: 
 8517: // ]]>
 8518: </script>
 8519: 
 8520: END
 8521: }
 8522: 
 8523: sub new_spares_js {
 8524:     my @sparestypes = ('primary','default');
 8525:     my $types = join("','",@sparestypes);
 8526:     my $select = &mt('Select');
 8527:     return <<"END";
 8528: 
 8529: <script type="text/javascript">
 8530: // <![CDATA[
 8531: 
 8532: function updateNewSpares(formname,lonhost) {
 8533:     var types = new Array('$types');
 8534:     var include = new Array();
 8535:     var exclude = new Array();
 8536:     for (var i=0; i<types.length; i++) {
 8537:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8538:         for (var j=0; j<spareboxes.length; j++) {
 8539:             if (formname.elements[spareboxes[j]].checked) {
 8540:                 exclude.push(formname.elements[spareboxes[j]].value);
 8541:             } else {
 8542:                 include.push(formname.elements[spareboxes[j]].value);
 8543:             }
 8544:         }
 8545:     }
 8546:     for (var i=0; i<types.length; i++) {
 8547:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8548:         var selIdx = newSpare.selectedIndex;
 8549:         var currnew = newSpare.options[selIdx].value;
 8550:         var okSpares = new Array();
 8551:         for (var j=0; j<newSpare.options.length; j++) {
 8552:             var possible = newSpare.options[j].value;
 8553:             if (possible != '') {
 8554:                 if (exclude.indexOf(possible) == -1) {
 8555:                     okSpares.push(possible);
 8556:                 } else {
 8557:                     if (currnew == possible) {
 8558:                         selIdx = 0;
 8559:                     }
 8560:                 }
 8561:             }
 8562:         }
 8563:         for (var k=0; k<include.length; k++) {
 8564:             if (okSpares.indexOf(include[k]) == -1) {
 8565:                 okSpares.push(include[k]);
 8566:             }
 8567:         }
 8568:         okSpares.sort();
 8569:         newSpare.options.length = 0;
 8570:         if (selIdx == 0) {
 8571:             newSpare.options[0] = new Option("$select","",true,true);
 8572:         } else {
 8573:             newSpare.options[0] = new Option("$select","",false,false);
 8574:         }
 8575:         for (var m=0; m<okSpares.length; m++) {
 8576:             var idx = m+1;
 8577:             var selThis = 0;
 8578:             if (selIdx != 0) {
 8579:                 if (okSpares[m] == currnew) {
 8580:                     selThis = 1;
 8581:                 }
 8582:             }
 8583:             if (selThis == 1) {
 8584:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8585:             } else {
 8586:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8587:             }
 8588:         }
 8589:     }
 8590:     return;
 8591: }
 8592: 
 8593: function checkNewSpares(lonhost,type) {
 8594:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8595:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8596:     if (chosen != '') { 
 8597:         var othertype;
 8598:         var othernewSpare;
 8599:         if (type == 'primary') {
 8600:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 8601:         }
 8602:         if (type == 'default') {
 8603:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 8604:         }
 8605:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 8606:             othernewSpare.selectedIndex = 0;
 8607:         }
 8608:     }
 8609:     return;
 8610: }
 8611: 
 8612: // ]]>
 8613: </script>
 8614: 
 8615: END
 8616: 
 8617: }
 8618: 
 8619: sub common_domprefs_js {
 8620:     return <<"END";
 8621: 
 8622: <script type="text/javascript">
 8623: // <![CDATA[
 8624: 
 8625: function getIndicesByName(formname,item) {
 8626:     var group = new Array();
 8627:     for (var i=0;i<formname.elements.length;i++) {
 8628:         if (formname.elements[i].name == item) {
 8629:             group.push(formname.elements[i].id);
 8630:         }
 8631:     }
 8632:     return group;
 8633: }
 8634: 
 8635: // ]]>
 8636: </script>
 8637: 
 8638: END
 8639: 
 8640: }
 8641: 
 8642: sub recaptcha_js {
 8643:     my %lt = &captcha_phrases();
 8644:     return <<"END";
 8645: 
 8646: <script type="text/javascript">
 8647: // <![CDATA[
 8648: 
 8649: function updateCaptcha(caller,context) {
 8650:     var privitem;
 8651:     var pubitem;
 8652:     var privtext;
 8653:     var pubtext;
 8654:     if (document.getElementById(context+'_recaptchapub')) {
 8655:         pubitem = document.getElementById(context+'_recaptchapub');
 8656:     } else {
 8657:         return;
 8658:     }
 8659:     if (document.getElementById(context+'_recaptchapriv')) {
 8660:         privitem = document.getElementById(context+'_recaptchapriv');
 8661:     } else {
 8662:         return;
 8663:     }
 8664:     if (document.getElementById(context+'_recaptchapubtxt')) {
 8665:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 8666:     } else {
 8667:         return;
 8668:     }
 8669:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 8670:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 8671:     } else {
 8672:         return;
 8673:     }
 8674:     if (caller.checked) {
 8675:         if (caller.value == 'recaptcha') {
 8676:             pubitem.type = 'text';
 8677:             privitem.type = 'text';
 8678:             pubitem.size = '40';
 8679:             privitem.size = '40';
 8680:             pubtext.innerHTML = "$lt{'pub'}";
 8681:             privtext.innerHTML = "$lt{'priv'}";
 8682:         } else {
 8683:             pubitem.type = 'hidden';
 8684:             privitem.type = 'hidden';
 8685:             pubtext.innerHTML = '';
 8686:             privtext.innerHTML = '';
 8687:         }
 8688:     }
 8689:     return;
 8690: }
 8691: 
 8692: // ]]>
 8693: </script>
 8694: 
 8695: END
 8696: 
 8697: }
 8698: 
 8699: sub captcha_phrases {
 8700:     return &Apache::lonlocal::texthash (
 8701:                  priv => 'Private key',
 8702:                  pub  => 'Public key',
 8703:                  original  => 'original (CAPTCHA)',
 8704:                  recaptcha => 'successor (ReCAPTCHA)',
 8705:                  notused   => 'unused',
 8706:     );
 8707: }
 8708: 
 8709: 1;

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