File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160: download - view: text, annotated - select for diffs
Wed Nov 30 18:31:04 2011 UTC (12 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_X, language_hyphenation_merge, language_hyphenation, HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
- Typo.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160 2011/11/30 18:31:04 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 (either quotas 
   90: or requestcourses).
   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'],$dom);
  215:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  216:                        'autoupdate','autocreate','directorysrch','contacts',
  217:                        'usercreation','usermodification','scantron',
  218:                        'requestcourses','coursecategories','serverstatuses','helpsettings',
  219:                        'coursedefaults','usersessions');
  220:     if (keys(%servers) > 1) {
  221:         push(@prefs_order,'loadbalancing');
  222:     }
  223:     my %prefs = (
  224:         'rolecolors' =>
  225:                    { text => 'Default color schemes',
  226:                      help => 'Domain_Configuration_Color_Schemes',
  227:                      header => [{col1 => 'Student Settings',
  228:                                  col2 => '',},
  229:                                 {col1 => 'Coordinator Settings',
  230:                                  col2 => '',},
  231:                                 {col1 => 'Author Settings',
  232:                                  col2 => '',},
  233:                                 {col1 => 'Administrator Settings',
  234:                                  col2 => '',}],
  235:                     },
  236:         'login' =>
  237:                     { text => 'Log-in page options',
  238:                       help => 'Domain_Configuration_Login_Page',
  239:                       header => [{col1 => 'Item',
  240:                                   col2 => '',}],
  241:                     },
  242: 
  243:         'defaults' => 
  244:                     { text => 'Default authentication/language/timezone/portal',
  245:                       help => 'Domain_Configuration_LangTZAuth',
  246:                       header => [{col1 => 'Setting',
  247:                                   col2 => 'Value'}],
  248:                     },
  249:         'quotas' => 
  250:                     { text => 'User blogs, personal information pages, portfolios',
  251:                       help => 'Domain_Configuration_Quotas',
  252:                       header => [{col1 => 'User affiliation',
  253:                                   col2 => 'Available tools',
  254:                                   col3 => 'Portfolio quota',}],
  255:                     },
  256:         'autoenroll' =>
  257:                    { text => 'Auto-enrollment settings',
  258:                      help => 'Domain_Configuration_Auto_Enrollment',
  259:                      header => [{col1 => 'Configuration setting',
  260:                                  col2 => 'Value(s)'}],
  261:                    },
  262:         'autoupdate' => 
  263:                    { text => 'Auto-update settings',
  264:                      help => 'Domain_Configuration_Auto_Updates',
  265:                      header => [{col1 => 'Setting',
  266:                                  col2 => 'Value',},
  267:                                 {col1 => 'Setting',
  268:                                  col2 => 'Affiliation'},
  269:                                 {col1 => 'User population',
  270:                                  col2 => 'Updateable user data'}],
  271:                   },
  272:         'autocreate' => 
  273:                   { text => 'Auto-course creation settings',
  274:                      help => 'Domain_Configuration_Auto_Creation',
  275:                      header => [{col1 => 'Configuration Setting',
  276:                                  col2 => 'Value',}],
  277:                   },
  278:         'directorysrch' => 
  279:                   { text => 'Institutional directory searches',
  280:                     help => 'Domain_Configuration_InstDirectory_Search',
  281:                     header => [{col1 => 'Setting',
  282:                                 col2 => 'Value',}],
  283:                   },
  284:         'contacts' =>
  285:                   { text => 'Contact Information',
  286:                     help => 'Domain_Configuration_Contact_Info',
  287:                     header => [{col1 => 'Setting',
  288:                                 col2 => 'Value',}],
  289:                   },
  290: 
  291:         'usercreation' => 
  292:                   { text => 'User creation',
  293:                     help => 'Domain_Configuration_User_Creation',
  294:                     header => [{col1 => 'Format rule type',
  295:                                 col2 => 'Format rules in force'},
  296:                                {col1 => 'User account creation',
  297:                                 col2 => 'Usernames which may be created',},
  298:                                {col1 => 'Context',
  299:                                 col2 => 'Assignable authentication types'}],
  300:                   },
  301:         'usermodification' =>
  302:                   { text => 'User modification',
  303:                     help => 'Domain_Configuration_User_Modification',
  304:                     header => [{col1 => 'Target user has role',
  305:                                 col2 => 'User information updateable in author context'},
  306:                                {col1 => 'Target user has role',
  307:                                 col2 => 'User information updateable in course context'},
  308:                                {col1 => "Status of user",
  309:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  310:                   },
  311:         'scantron' =>
  312:                   { text => 'Bubblesheet format file',
  313:                     help => 'Domain_Configuration_Scantron_Format',
  314:                     header => [ {col1 => 'Item',
  315:                                  col2 => '',
  316:                               }],
  317:                   },
  318:         'requestcourses' => 
  319:                  {text => 'Request creation of courses',
  320:                   help => 'Domain_Configuration_Request_Courses',
  321:                   header => [{col1 => 'User affiliation',
  322:                               col2 => 'Availability/Processing of requests',},
  323:                              {col1 => 'Setting',
  324:                               col2 => 'Value'}],
  325:                  },
  326:         'coursecategories' =>
  327:                   { text => 'Cataloging of courses/communities',
  328:                     help => 'Domain_Configuration_Cataloging_Courses',
  329:                     header => [{col1 => 'Category settings',
  330:                                 col2 => '',},
  331:                                {col1 => 'Categories',
  332:                                 col2 => '',
  333:                                }],
  334:                   },
  335:         'serverstatuses' =>
  336:                  {text   => 'Access to server status pages',
  337:                   help   => 'Domain_Configuration_Server_Status',
  338:                   header => [{col1 => 'Status Page',
  339:                               col2 => 'Other named users',
  340:                               col3 => 'Specific IPs',
  341:                             }],
  342:                  },
  343:         'helpsettings' =>
  344:                  {text   => 'Help page settings',
  345:                   help   => 'Domain_Configuration_Help_Settings',
  346:                   header => [{col1 => 'Authenticated Help Settings',
  347:                               col2 => ''},
  348:                              {col1 => 'Unauthenticated Help Settings',
  349:                               col2 => ''}],
  350:                  },
  351:         'coursedefaults' => 
  352:                  {text => 'Course/Community defaults',
  353:                   help => 'Domain_Configuration_Course_Defaults',
  354:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  355:                               col2 => 'Value',},
  356:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  357:                               col2 => 'Value',},],
  358:                  },
  359:         'privacy' => 
  360:                  {text   => 'User Privacy',
  361:                   help   => 'Domain_Configuration_User_Privacy',
  362:                   header => [{col1 => 'Setting',
  363:                               col2 => 'Value',}],
  364:                  },
  365:         'usersessions' =>
  366:                  {text  => 'User session hosting/offloading',
  367:                   help  => 'Domain_Configuration_User_Sessions',
  368:                   header => [{col1 => 'Domain server',
  369:                               col2 => 'Servers to offload sessions to when busy'},
  370:                              {col1 => 'Hosting of users from other domains',
  371:                               col2 => 'Rules'},
  372:                              {col1 => "Hosting domain's own users elsewhere",
  373:                               col2 => 'Rules'}],
  374:                  },
  375:          'loadbalancing' =>
  376:                  {text  => 'Dedicated Load Balancer',
  377:                   help  => 'Domain_Configuration_Load_Balancing',
  378:                   header => [{col1 => 'Server',
  379:                               col2 => 'Default destinations',
  380:                               col3 => 'User affliation',
  381:                               col4 => 'Overrides'},
  382:                             ],
  383:                  },
  384:     );
  385:     if (keys(%servers) > 1) {
  386:         $prefs{'login'}  = { text   => 'Log-in page options',
  387:                              help   => 'Domain_Configuration_Login_Page',
  388:                             header => [{col1 => 'Log-in Service',
  389:                                         col2 => 'Server Setting',},
  390:                                        {col1 => 'Log-in Page Items',
  391:                                         col2 => ''}],
  392:                            };
  393:     }
  394:     my @roles = ('student','coordinator','author','admin');
  395:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  396:     &Apache::lonhtmlcommon::add_breadcrumb
  397:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  398:       text=>"Settings to display/modify"});
  399:     my $confname = $dom.'-domainconfig';
  400:     if ($phase eq 'process') {
  401:         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
  402:     } elsif ($phase eq 'display') {
  403:         my $js;
  404:         if (keys(%servers) > 1) {
  405:             my ($othertitle,$usertypes,$types) =
  406:                 &Apache::loncommon::sorted_inst_types($dom);
  407:             $js = &lonbalance_targets_js($dom,$types,\%servers).
  408:                   &new_spares_js().
  409:                   &common_domprefs_js().
  410:                   &Apache::loncommon::javascript_array_indexof();
  411:         }
  412:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  413:     } else {
  414:         if (keys(%domconfig) == 0) {
  415:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  416:             my @ids=&Apache::lonnet::current_machine_ids();
  417:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  418:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  419:                 my @loginimages = ('img','logo','domlogo','login');
  420:                 my $custom_img_count = 0;
  421:                 foreach my $img (@loginimages) {
  422:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  423:                         $custom_img_count ++;
  424:                     }
  425:                 }
  426:                 foreach my $role (@roles) {
  427:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  428:                         $custom_img_count ++;
  429:                     }
  430:                 }
  431:                 if ($custom_img_count > 0) {
  432:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  433:                     my $switch_server = &check_switchserver($dom,$confname);
  434:                     $r->print(
  435:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  436:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  437:     &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 />'.
  438:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  439:                     if ($switch_server) {
  440:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  441:                     }
  442:                     $r->print(&Apache::loncommon::end_page());
  443:                     return OK;
  444:                 }
  445:             }
  446:         }
  447:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  448:     }
  449:     return OK;
  450: }
  451: 
  452: sub process_changes {
  453:     my ($r,$dom,$confname,$action,$roles,$values) = @_;
  454:     my %domconfig;
  455:     if (ref($values) eq 'HASH') {
  456:         %domconfig = %{$values};
  457:     }
  458:     my $output;
  459:     if ($action eq 'login') {
  460:         $output = &modify_login($r,$dom,$confname,%domconfig);
  461:     } elsif ($action eq 'rolecolors') {
  462:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  463:                                      %domconfig);
  464:     } elsif ($action eq 'quotas') {
  465:         $output = &modify_quotas($dom,$action,%domconfig);
  466:     } elsif ($action eq 'autoenroll') {
  467:         $output = &modify_autoenroll($dom,%domconfig);
  468:     } elsif ($action eq 'autoupdate') {
  469:         $output = &modify_autoupdate($dom,%domconfig);
  470:     } elsif ($action eq 'autocreate') {
  471:         $output = &modify_autocreate($dom,%domconfig);
  472:     } elsif ($action eq 'directorysrch') {
  473:         $output = &modify_directorysrch($dom,%domconfig);
  474:     } elsif ($action eq 'usercreation') {
  475:         $output = &modify_usercreation($dom,%domconfig);
  476:     } elsif ($action eq 'usermodification') {
  477:         $output = &modify_usermodification($dom,%domconfig);
  478:     } elsif ($action eq 'contacts') {
  479:         $output = &modify_contacts($dom,%domconfig);
  480:     } elsif ($action eq 'defaults') {
  481:         $output = &modify_defaults($dom,$r);
  482:     } elsif ($action eq 'scantron') {
  483:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  484:     } elsif ($action eq 'coursecategories') {
  485:         $output = &modify_coursecategories($dom,%domconfig);
  486:     } elsif ($action eq 'serverstatuses') {
  487:         $output = &modify_serverstatuses($dom,%domconfig);
  488:     } elsif ($action eq 'requestcourses') {
  489:         $output = &modify_quotas($dom,$action,%domconfig);
  490:     } elsif ($action eq 'helpsettings') {
  491:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  492:     } elsif ($action eq 'coursedefaults') {
  493:         $output = &modify_coursedefaults($dom,%domconfig);
  494:     } elsif ($action eq 'usersessions') {
  495:         $output = &modify_usersessions($dom,%domconfig);
  496:     } elsif ($action eq 'loadbalancing') {
  497:         $output = &modify_loadbalancing($dom,%domconfig);
  498:     }
  499:     return $output;
  500: }
  501: 
  502: sub print_config_box {
  503:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  504:     my $rowtotal = 0;
  505:     my $output;
  506:     if ($action eq 'coursecategories') {
  507:         $output = &coursecategories_javascript($settings);
  508:     }
  509:     $output .= 
  510:          '<table class="LC_nested_outer">
  511:           <tr>
  512:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  513:            &mt($item->{text}).'&nbsp;'.
  514:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  515:           '</tr>';
  516:     $rowtotal ++;
  517:     my $numheaders = 1;
  518:     if (ref($item->{'header'}) eq 'ARRAY') {
  519:         $numheaders = scalar(@{$item->{'header'}});
  520:     }
  521:     if ($numheaders > 1) {
  522:         my $colspan = '';
  523:         my $rightcolspan = '';
  524:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') || ($action eq 'helpsettings')) {
  525:             $colspan = ' colspan="2"';
  526:         }
  527:         if ($action eq 'usersessions') {
  528:             $rightcolspan = ' colspan="3"'; 
  529:         }
  530:         $output .= '
  531:           <tr>
  532:            <td>
  533:             <table class="LC_nested">
  534:              <tr class="LC_info_row">
  535:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  536:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  537:              </tr>';
  538:         $rowtotal ++;
  539:         if ($action eq 'autoupdate') {
  540:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  541:         } elsif ($action eq 'usercreation') {
  542:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  543:         } elsif ($action eq 'usermodification') {
  544:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  545:         } elsif ($action eq 'coursecategories') {
  546:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  547:         } elsif ($action eq 'login') {
  548:             $output .= &print_login('top',$dom,$confname,$phase,$settings,\$rowtotal);
  549:             $colspan = ' colspan="2"';
  550:         } elsif ($action eq 'requestcourses') {
  551:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  552:         } elsif ($action eq 'helpsettings') {
  553:             $output .= &print_helpsettings('top',$dom,$confname,$settings,\$rowtotal);
  554:         } elsif ($action eq 'usersessions') {
  555:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  556:         } elsif ($action eq 'rolecolors') {
  557:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  558:         } elsif ($action eq 'coursedefaults') {
  559:             $output .= &print_coursedefaults('top',$dom,$settings,\$rowtotal);
  560:         }
  561:         $output .= '
  562:            </table>
  563:           </td>
  564:          </tr>
  565:          <tr>
  566:            <td>
  567:             <table class="LC_nested">
  568:              <tr class="LC_info_row">
  569:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  570:         $output .= '
  571:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  572:              </tr>';
  573:             $rowtotal ++;
  574:         if ($action eq 'autoupdate') {
  575:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  576:            </table>
  577:           </td>
  578:          </tr>
  579:          <tr>
  580:            <td>
  581:             <table class="LC_nested">
  582:              <tr class="LC_info_row">
  583:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  584:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  585:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  586:             $rowtotal ++;
  587:         } elsif ($action eq 'usercreation') {
  588:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  589:            </table>
  590:           </td>
  591:          </tr>
  592:          <tr>
  593:            <td>
  594:             <table class="LC_nested">
  595:              <tr class="LC_info_row">
  596:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  597:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  598:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  599:             $rowtotal ++;
  600:         } elsif ($action eq 'usermodification') {
  601:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  602:            </table>
  603:           </td>
  604:          </tr>
  605:          <tr>
  606:            <td>
  607:             <table class="LC_nested">
  608:              <tr class="LC_info_row">
  609:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  610:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  611:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  612:             $rowtotal ++;
  613:         } elsif ($action eq 'coursecategories') {
  614:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  615:         } elsif ($action eq 'login') {
  616:             $output .= &print_login('bottom',$dom,$confname,$phase,$settings,\$rowtotal);
  617:         } elsif ($action eq 'requestcourses') {
  618:             $output .= &print_courserequestmail($dom,$settings,\$rowtotal);
  619:         } elsif ($action eq 'helpsettings') {
  620:             $output .= &print_helpsettings('bottom',$dom,$confname,$settings,\$rowtotal);
  621:         } elsif ($action eq 'usersessions') {
  622:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  623:            </table>
  624:           </td>
  625:          </tr>
  626:          <tr>
  627:            <td>
  628:             <table class="LC_nested">
  629:              <tr class="LC_info_row">
  630:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  631:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  632:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  633:             $rowtotal ++;
  634:         } elsif ($action eq 'coursedefaults') {
  635:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  636:         } elsif ($action eq 'rolecolors') {
  637:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  638:            </table>
  639:           </td>
  640:          </tr>
  641:          <tr>
  642:            <td>
  643:             <table class="LC_nested">
  644:              <tr class="LC_info_row">
  645:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  646:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  647:               <td class="LC_right_item" valign="top">'.
  648:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  649:              </tr>'.
  650:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  651:            </table>
  652:           </td>
  653:          </tr>
  654:          <tr>
  655:            <td>
  656:             <table class="LC_nested">
  657:              <tr class="LC_info_row">
  658:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  659:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  660:              </tr>'.
  661:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  662:             $rowtotal += 2;
  663:         }
  664:     } else {
  665:         $output .= '
  666:           <tr>
  667:            <td>
  668:             <table class="LC_nested">
  669:              <tr class="LC_info_row">';
  670:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  671:             $output .= '  
  672:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  673:         } elsif ($action eq 'serverstatuses') {
  674:             $output .= '
  675:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  676:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  677: 
  678:         } else {
  679:             $output .= '
  680:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  681:         }
  682:         if (defined($item->{'header'}->[0]->{'col3'})) {
  683:             $output .= '<td class="LC_left_item" valign="top">'.
  684:                        &mt($item->{'header'}->[0]->{'col2'});
  685:             if ($action eq 'serverstatuses') {
  686:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  687:             } 
  688:         } else {
  689:             $output .= '<td class="LC_right_item" valign="top">'.
  690:                        &mt($item->{'header'}->[0]->{'col2'});
  691:         }
  692:         $output .= '</td>';
  693:         if ($item->{'header'}->[0]->{'col3'}) {
  694:             if (defined($item->{'header'}->[0]->{'col4'})) {
  695:                 $output .= '<td class="LC_left_item" valign="top">'.
  696:                             &mt($item->{'header'}->[0]->{'col3'});
  697:             } else {
  698:                 $output .= '<td class="LC_right_item" valign="top">'.
  699:                            &mt($item->{'header'}->[0]->{'col3'});
  700:             }
  701:             if ($action eq 'serverstatuses') {
  702:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  703:             }
  704:             $output .= '</td>';
  705:         }
  706:         if ($item->{'header'}->[0]->{'col4'}) {
  707:             $output .= '<td class="LC_right_item" valign="top">'.
  708:                        &mt($item->{'header'}->[0]->{'col4'});
  709:         }
  710:         $output .= '</tr>';
  711:         $rowtotal ++;
  712:         if ($action eq 'login') {
  713:             $output .= &print_login('bottom',$dom,$confname,$phase,$settings,
  714:                                     \$rowtotal);
  715:         } elsif ($action eq 'quotas') {
  716:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  717:         } elsif ($action eq 'autoenroll') {
  718:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  719:         } elsif ($action eq 'autocreate') {
  720:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  721:         } elsif ($action eq 'directorysrch') {
  722:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  723:         } elsif ($action eq 'contacts') {
  724:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  725:         } elsif ($action eq 'defaults') {
  726:             $output .= &print_defaults($dom,\$rowtotal);
  727:         } elsif ($action eq 'scantron') {
  728:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  729:         } elsif ($action eq 'serverstatuses') {
  730:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  731:         } elsif ($action eq 'helpsettings') {
  732:             $output .= &print_helpsettings('top',$dom,$confname,$settings,\$rowtotal);
  733:         } elsif ($action eq 'loadbalancing') {
  734:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  735:         }
  736:     }
  737:     $output .= '
  738:    </table>
  739:   </td>
  740:  </tr>
  741: </table><br />';
  742:     return ($output,$rowtotal);
  743: }
  744: 
  745: sub print_login {
  746:     my ($position,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  747:     my ($css_class,$datatable);
  748:     my %choices = &login_choices();
  749: 
  750:     if ($position eq 'top') {
  751:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  752:         my $choice = $choices{'disallowlogin'};
  753:         $css_class = ' class="LC_odd_row"';
  754:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  755:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  756:                       '<th>'.$choices{'server'}.'</th>'.
  757:                       '<th>'.$choices{'serverpath'}.'</th>'.
  758:                       '<th>'.$choices{'custompath'}.'</th>'.
  759:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  760:         my %disallowed;
  761:         if (ref($settings) eq 'HASH') {
  762:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  763:                %disallowed = %{$settings->{'loginvia'}};
  764:             }
  765:         }
  766:         foreach my $lonhost (sort(keys(%servers))) {
  767:             my $direct = 'selected="selected"';
  768:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  769:                 if ($disallowed{$lonhost}{'server'} ne '') {
  770:                     $direct = '';
  771:                 }
  772:             }
  773:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  774:                           '<td><select name="'.$lonhost.'_server">'.
  775:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  776:                           '</option>';
  777:             foreach my $hostid (keys(%servers)) {
  778:                 next if ($servers{$hostid} eq $servers{$lonhost});
  779:                 my $selected = '';
  780:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  781:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  782:                         $selected = 'selected="selected"';
  783:                     }
  784:                 }
  785:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  786:                               $servers{$hostid}.'</option>';
  787:             }
  788:             $datatable .= '</select></td>'.
  789:                           '<td><select name="'.$lonhost.'_serverpath">';
  790:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  791:                 my $pathname = $path;
  792:                 if ($path eq 'custom') {
  793:                     $pathname = &mt('Custom Path').' ->';
  794:                 }
  795:                 my $selected = '';
  796:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  797:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  798:                         $selected = 'selected="selected"';
  799:                     }
  800:                 } elsif ($path eq '') {
  801:                     $selected = 'selected="selected"';
  802:                 }
  803:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  804:             }
  805:             $datatable .= '</select></td>';
  806:             my ($custom,$exempt);
  807:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  808:                 $custom = $disallowed{$lonhost}{'custompath'};
  809:                 $exempt = $disallowed{$lonhost}{'exempt'};
  810:             }
  811:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  812:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  813:                           '</tr>';
  814:         }
  815:         $datatable .= '</table></td></tr>';
  816:         return $datatable;
  817:     }
  818: 
  819:     my %defaultchecked = ( 
  820:                            'coursecatalog' => 'on',
  821:                            'adminmail'     => 'off',
  822:                            'newuser'       => 'off',
  823:                          );
  824:     my @toggles = ('coursecatalog','adminmail','newuser');
  825:     my (%checkedon,%checkedoff);
  826:     foreach my $item (@toggles) {
  827:         if ($defaultchecked{$item} eq 'on') { 
  828:             $checkedon{$item} = ' checked="checked" ';
  829:             $checkedoff{$item} = ' ';
  830:         } elsif ($defaultchecked{$item} eq 'off') {
  831:             $checkedoff{$item} = ' checked="checked" ';
  832:             $checkedon{$item} = ' ';
  833:         }
  834:     }
  835:     my @images = ('img','logo','domlogo','login');
  836:     my @logintext = ('textcol','bgcol');
  837:     my @bgs = ('pgbg','mainbg','sidebg');
  838:     my @links = ('link','alink','vlink');
  839:     my %designhash = &Apache::loncommon::get_domainconf($dom);
  840:     my %defaultdesign = %Apache::loncommon::defaultdesign;
  841:     my (%is_custom,%designs);
  842:     my %defaults = (
  843:                    font => $defaultdesign{'login.font'},
  844:                    );
  845:     foreach my $item (@images) {
  846:         $defaults{$item} = $defaultdesign{'login.'.$item};
  847:         $defaults{'showlogo'}{$item} = 1;
  848:     }
  849:     foreach my $item (@bgs) {
  850:         $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  851:     }
  852:     foreach my $item (@logintext) {
  853:         $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  854:     }
  855:     foreach my $item (@links) {
  856:         $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  857:     }
  858:     if (ref($settings) eq 'HASH') {
  859:         foreach my $item (@toggles) {
  860:             if ($settings->{$item} eq '1') {
  861:                 $checkedon{$item} =  ' checked="checked" ';
  862:                 $checkedoff{$item} = ' ';
  863:             } elsif ($settings->{$item} eq '0') {
  864:                 $checkedoff{$item} =  ' checked="checked" ';
  865:                 $checkedon{$item} = ' ';
  866:             }
  867:         }
  868:         foreach my $item (@images) {
  869:             if (defined($settings->{$item})) {
  870:                 $designs{$item} = $settings->{$item};
  871:                 $is_custom{$item} = 1;
  872:             }
  873:             if (defined($settings->{'showlogo'}{$item})) {
  874:                 $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  875:             }
  876:         }
  877:         foreach my $item (@logintext) {
  878:             if ($settings->{$item} ne '') {
  879:                 $designs{'logintext'}{$item} = $settings->{$item};
  880:                 $is_custom{$item} = 1;
  881:             }
  882:         }
  883:         if ($settings->{'font'} ne '') {
  884:             $designs{'font'} = $settings->{'font'};
  885:             $is_custom{'font'} = 1;
  886:         }
  887:         foreach my $item (@bgs) {
  888:             if ($settings->{$item} ne '') {
  889:                 $designs{'bgs'}{$item} = $settings->{$item};
  890:                 $is_custom{$item} = 1;
  891:             }
  892:         }
  893:         foreach my $item (@links) {
  894:             if ($settings->{$item} ne '') {
  895:                 $designs{'links'}{$item} = $settings->{$item};
  896:                 $is_custom{$item} = 1;
  897:             }
  898:         }
  899:     } else {
  900:         if ($designhash{$dom.'.login.font'} ne '') {
  901:             $designs{'font'} = $designhash{$dom.'.login.font'};
  902:             $is_custom{'font'} = 1;
  903:         }
  904:         foreach my $item (@images) {
  905:             if ($designhash{$dom.'.login.'.$item} ne '') {
  906:                 $designs{$item} = $designhash{$dom.'.login.'.$item};
  907:                 $is_custom{$item} = 1;
  908:             }
  909:         }
  910:         foreach my $item (@bgs) {
  911:             if ($designhash{$dom.'.login.'.$item} ne '') {
  912:                 $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  913:                 $is_custom{$item} = 1;
  914:             }
  915:         }
  916:         foreach my $item (@links) {
  917:             if ($designhash{$dom.'.login.'.$item} ne '') {
  918:                 $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  919:                 $is_custom{$item} = 1;
  920:             }
  921:         }
  922:     }
  923:     my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  924:                                                   logo => 'Institution Logo',
  925:                                                   domlogo => 'Domain Logo',
  926:                                                   login => 'Login box');
  927:     my $itemcount = 1;
  928:     foreach my $item (@toggles) {
  929:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
  930:         $datatable .=  
  931:             '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  932:             '</td><td>'.
  933:             '<span class="LC_nobreak"><label><input type="radio" name="'.
  934:             $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
  935:             '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
  936:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
  937:             '</tr>';
  938:         $itemcount ++;
  939:     }
  940:     $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
  941:     $datatable .= '</tr></table></td></tr>';
  942:     return $datatable;
  943: }
  944: 
  945: sub login_choices {
  946:     my %choices =
  947:         &Apache::lonlocal::texthash (
  948:             coursecatalog => 'Display Course/Community Catalog link?',
  949:             adminmail     => "Display Administrator's E-mail Address?",
  950:             disallowlogin => "Login page requests redirected",
  951:             hostid        => "Server",
  952:             server        => "Redirect to:",
  953:             serverpath    => "Path",
  954:             custompath    => "Custom", 
  955:             exempt        => "Exempt IP(s)",
  956:             directlogin   => "No redirect",
  957:             newuser       => "Link to create a user account",
  958:             img           => "Header",
  959:             logo          => "Main Logo",
  960:             domlogo       => "Domain Logo",
  961:             login         => "Log-in Header", 
  962:             textcol       => "Text color",
  963:             bgcol         => "Box color",
  964:             bgs           => "Background colors",
  965:             links         => "Link colors",
  966:             font          => "Font color",
  967:             pgbg          => "Header",
  968:             mainbg        => "Page",
  969:             sidebg        => "Login box",
  970:             link          => "Link",
  971:             alink         => "Active link",
  972:             vlink         => "Visited link",
  973:         );
  974:     return %choices;
  975: }
  976: 
  977: sub print_rolecolors {
  978:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
  979:     my %choices = &color_font_choices();
  980:     my @bgs = ('pgbg','tabbg','sidebg');
  981:     my @links = ('link','alink','vlink');
  982:     my @images = ('img');
  983:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
  984:     my %designhash = &Apache::loncommon::get_domainconf($dom);
  985:     my %defaultdesign = %Apache::loncommon::defaultdesign;
  986:     my (%is_custom,%designs);
  987:     my %defaults = (
  988:                    img => $defaultdesign{$role.'.img'},
  989:                    font => $defaultdesign{$role.'.font'},
  990: 		   fontmenu => $defaultdesign{$role.'.fontmenu'},
  991:                    );
  992:     foreach my $item (@bgs) {
  993:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
  994:     }
  995:     foreach my $item (@links) {
  996:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
  997:     }
  998:     if (ref($settings) eq 'HASH') {
  999:         if (ref($settings->{$role}) eq 'HASH') {
 1000:             if ($settings->{$role}->{'img'} ne '') {
 1001:                 $designs{'img'} = $settings->{$role}->{'img'};
 1002:                 $is_custom{'img'} = 1;
 1003:             }
 1004:             if ($settings->{$role}->{'font'} ne '') {
 1005:                 $designs{'font'} = $settings->{$role}->{'font'};
 1006:                 $is_custom{'font'} = 1;
 1007:             }
 1008:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1009:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1010:                 $is_custom{'fontmenu'} = 1;
 1011:             }
 1012:             foreach my $item (@bgs) {
 1013:                 if ($settings->{$role}->{$item} ne '') {
 1014:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1015:                     $is_custom{$item} = 1;
 1016:                 }
 1017:             }
 1018:             foreach my $item (@links) {
 1019:                 if ($settings->{$role}->{$item} ne '') {
 1020:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1021:                     $is_custom{$item} = 1;
 1022:                 }
 1023:             }
 1024:         }
 1025:     } else {
 1026:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1027:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1028:             $is_custom{'img'} = 1;
 1029:         }
 1030:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1031:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1032:             $is_custom{'fontmenu'} = 1; 
 1033:         }
 1034:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1035:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1036:             $is_custom{'font'} = 1;
 1037:         }
 1038:         foreach my $item (@bgs) {
 1039:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1040:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1041:                 $is_custom{$item} = 1;
 1042:             
 1043:             }
 1044:         }
 1045:         foreach my $item (@links) {
 1046:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1047:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1048:                 $is_custom{$item} = 1;
 1049:             }
 1050:         }
 1051:     }
 1052:     my $itemcount = 1;
 1053:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1054:     $datatable .= '</tr></table></td></tr>';
 1055:     return $datatable;
 1056: }
 1057: 
 1058: sub display_color_options {
 1059:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1060:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1061:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1062:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1063:     my $datatable = '<tr'.$css_class.'>'.
 1064:         '<td>'.$choices->{'font'}.'</td>';
 1065:     if (!$is_custom->{'font'}) {
 1066:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1067:     } else {
 1068:         $datatable .= '<td>&nbsp;</td>';
 1069:     }
 1070:     my $fontlink = &color_pick($phase,$role,'font',$choices->{'font'},$designs->{'font'});
 1071:     $datatable .= '<td><span class="LC_nobreak">'.
 1072:                   '<input type="text" size="10" name="'.$role.'_font"'.
 1073:                   ' value="'.$designs->{'font'}.'" />&nbsp;'.$fontlink.
 1074:                   '&nbsp;<span id="css_'.$role.'_font" style="background-color: '.
 1075:                   $designs->{'font'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1076:                   '</span></td></tr>';
 1077:     unless ($role eq 'login') { 
 1078:         $datatable .= '<tr'.$css_class.'>'.
 1079:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1080:         if (!$is_custom->{'fontmenu'}) {
 1081:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1082:         } else {
 1083:             $datatable .= '<td>&nbsp;</td>';
 1084:         }
 1085:         $fontlink = &color_pick($phase,$role,'fontmenu',$choices->{'fontmenu'},$designs->{'fontmenu'});
 1086:         $datatable .= '<td><span class="LC_nobreak">'.
 1087:                       '<input type="text" size="10" name="'.$role.'_fontmenu"'.
 1088:                       ' value="'.$designs->{'fontmenu'}.'" />&nbsp;'.$fontlink.
 1089:                       '&nbsp;<span id="css_'.$role.'_fontmenu" style="background-color: '.
 1090:                       $designs->{'fontmenu'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1091:                       '</span></td></tr>';
 1092:     }
 1093:     my $switchserver = &check_switchserver($dom,$confname);
 1094:     foreach my $img (@{$images}) {
 1095: 	$itemcount ++;
 1096:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1097:         $datatable .= '<tr'.$css_class.'>'.
 1098:                       '<td>'.$choices->{$img};
 1099:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1100:         if ($role eq 'login') {
 1101:             if ($img eq 'login') {
 1102:                 $login_hdr_pick =
 1103:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1104:                 $logincolors =
 1105:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1106:                                             $designs);
 1107:             } elsif ($img ne 'domlogo') {
 1108:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1109:             }
 1110:         }
 1111:         $datatable .= '</td>';
 1112:         if ($designs->{$img} ne '') {
 1113:             $imgfile = $designs->{$img};
 1114: 	    $img_import = ($imgfile =~ m{^/adm/});
 1115:         } else {
 1116:             $imgfile = $defaults->{$img};
 1117:         }
 1118:         if ($imgfile) {
 1119:             my ($showfile,$fullsize);
 1120:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1121:                 my $urldir = $1;
 1122:                 my $filename = $2;
 1123:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1124:                 if (@info) {
 1125:                     my $thumbfile = 'tn-'.$filename;
 1126:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1127:                     if (@thumb) {
 1128:                         $showfile = $urldir.'/'.$thumbfile;
 1129:                     } else {
 1130:                         $showfile = $imgfile;
 1131:                     }
 1132:                 } else {
 1133:                     $showfile = '';
 1134:                 }
 1135:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1136:                 $showfile = $imgfile;
 1137:                 my $imgdir = $1;
 1138:                 my $filename = $2;
 1139:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1140:                     $showfile = "/$imgdir/tn-".$filename;
 1141:                 } else {
 1142:                     my $input = $londocroot.$imgfile;
 1143:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1144:                     if (!-e $output) {
 1145:                         my ($width,$height) = &thumb_dimensions();
 1146:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1147:                         if ($fullwidth ne '' && $fullheight ne '') {
 1148:                             if ($fullwidth > $width && $fullheight > $height) { 
 1149:                                 my $size = $width.'x'.$height;
 1150:                                 system("convert -sample $size $input $output");
 1151:                                 $showfile = "/$imgdir/tn-".$filename;
 1152:                             }
 1153:                         }
 1154:                     }
 1155:                 }
 1156:             }
 1157:             if ($showfile) {
 1158:                 if ($showfile =~ m{^/(adm|res)/}) {
 1159:                     if ($showfile =~ m{^/res/}) {
 1160:                         my $local_showfile =
 1161:                             &Apache::lonnet::filelocation('',$showfile);
 1162:                         &Apache::lonnet::repcopy($local_showfile);
 1163:                     }
 1164:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1165:                 }
 1166:                 if ($imgfile) {
 1167:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1168:                         if ($imgfile =~ m{^/res/}) {
 1169:                             my $local_imgfile =
 1170:                                 &Apache::lonnet::filelocation('',$imgfile);
 1171:                             &Apache::lonnet::repcopy($local_imgfile);
 1172:                         }
 1173:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1174:                     } else {
 1175:                         $fullsize = $imgfile;
 1176:                     }
 1177:                 }
 1178:                 $datatable .= '<td>';
 1179:                 if ($img eq 'login') {
 1180:                     $datatable .= $login_hdr_pick;
 1181:                 } 
 1182:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1183:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1184:             } else {
 1185:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1186:                               &mt('Upload:');
 1187:             }
 1188:         } else {
 1189:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1190:                           &mt('Upload:');
 1191:         }
 1192:         if ($switchserver) {
 1193:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1194:         } else {
 1195:             if ($img ne 'login') { # suppress file selection for Log-in header
 1196:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1197:             }
 1198:         }
 1199:         $datatable .= '</td></tr>';
 1200:     }
 1201:     $itemcount ++;
 1202:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1203:     $datatable .= '<tr'.$css_class.'>'.
 1204:                   '<td>'.$choices->{'bgs'}.'</td>';
 1205:     my $bgs_def;
 1206:     foreach my $item (@{$bgs}) {
 1207:         if (!$is_custom->{$item}) {
 1208:             $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>';
 1209:         }
 1210:     }
 1211:     if ($bgs_def) {
 1212:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1213:     } else {
 1214:         $datatable .= '<td>&nbsp;</td>';
 1215:     }
 1216:     $datatable .= '<td class="LC_right_item">'.
 1217:                   '<table border="0"><tr>';
 1218:     foreach my $item (@{$bgs}) {
 1219:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'bgs'}{$item});
 1220:         $datatable .= '<td align="center">'.$link;
 1221:         if ($designs->{'bgs'}{$item}) {
 1222:             $datatable .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1223:         }
 1224:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'bgs'}{$item}.
 1225:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1226:     }
 1227:     $datatable .= '</tr></table></td></tr>';
 1228:     $itemcount ++;
 1229:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1230:     $datatable .= '<tr'.$css_class.'>'.
 1231:                   '<td>'.$choices->{'links'}.'</td>';
 1232:     my $links_def;
 1233:     foreach my $item (@{$links}) {
 1234:         if (!$is_custom->{$item}) {
 1235:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1236:         }
 1237:     }
 1238:     if ($links_def) {
 1239:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1240:     } else {
 1241:         $datatable .= '<td>&nbsp;</td>';
 1242:     }
 1243:     $datatable .= '<td class="LC_right_item">'.
 1244:                   '<table border="0"><tr>';
 1245:     foreach my $item (@{$links}) {
 1246:         $datatable .= '<td align="center">'."\n".
 1247:                       &color_pick($phase,$role,$item,$choices->{$item},
 1248:                                   $designs->{'links'}{$item});
 1249:         if ($designs->{'links'}{$item}) {
 1250:             $datatable.='&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'links'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1251:         }
 1252:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'links'}{$item}.
 1253:                       '" /></td>';
 1254:     }
 1255:     $$rowtotal += $itemcount;
 1256:     return $datatable;
 1257: }
 1258: 
 1259: sub logo_display_options {
 1260:     my ($img,$defaults,$designs) = @_;
 1261:     my $checkedon;
 1262:     if (ref($defaults) eq 'HASH') {
 1263:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1264:             if ($defaults->{'showlogo'}{$img}) {
 1265:                 $checkedon = 'checked="checked" ';     
 1266:             }
 1267:         } 
 1268:     }
 1269:     if (ref($designs) eq 'HASH') {
 1270:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1271:             if (defined($designs->{'showlogo'}{$img})) {
 1272:                 if ($designs->{'showlogo'}{$img} == 0) {
 1273:                     $checkedon = '';
 1274:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1275:                     $checkedon = 'checked="checked" ';
 1276:                 }
 1277:             }
 1278:         }
 1279:     }
 1280:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1281:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1282:            &mt('show').'</label>'."\n";
 1283: }
 1284: 
 1285: sub login_header_options  {
 1286:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1287:     my $output = '';
 1288:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1289:         $output .= &mt('Text default(s):').'<br />';
 1290:         if (!$is_custom->{'textcol'}) {
 1291:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1292:                        '&nbsp;&nbsp;&nbsp;';
 1293:         }
 1294:         if (!$is_custom->{'bgcol'}) {
 1295:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1296:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1297:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1298:         }
 1299:         $output .= '<br />';
 1300:     }
 1301:     $output .='<br />';
 1302:     return $output;
 1303: }
 1304: 
 1305: sub login_text_colors {
 1306:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1307:     my $color_menu = '<table border="0"><tr>';
 1308:     foreach my $item (@{$logintext}) {
 1309:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1310:         $color_menu .= '<td align="center">'.$link;
 1311:         if ($designs->{'logintext'}{$item}) {
 1312:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1313:         }
 1314:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1315:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1316:                        '<td>&nbsp;</td>';
 1317:     }
 1318:     $color_menu .= '</tr></table><br />';
 1319:     return $color_menu;
 1320: }
 1321: 
 1322: sub image_changes {
 1323:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1324:     my $output;
 1325:     if ($img eq 'login') {
 1326:             # suppress image for Log-in header
 1327:     } elsif (!$is_custom) {
 1328:         if ($img ne 'domlogo') {
 1329:             $output .= &mt('Default image:').'<br />';
 1330:         } else {
 1331:             $output .= &mt('Default in use:').'<br />';
 1332:         }
 1333:     }
 1334:     if ($img eq 'login') { # suppress image for Log-in header
 1335:         $output .= '<td>'.$logincolors;
 1336:     } else {
 1337:         if ($img_import) {
 1338:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1339:         }
 1340:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1341:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1342:         if ($is_custom) {
 1343:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1344:                        '<input type="checkbox" name="'.
 1345:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1346:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1347:         } else {
 1348:             $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1349:         }
 1350:     }
 1351:     return $output;
 1352: }
 1353: 
 1354: sub color_pick {
 1355:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1356:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1357:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1358:                ');">'.$desc.'</a>';
 1359:     return $link;
 1360: }
 1361: 
 1362: sub print_quotas {
 1363:     my ($dom,$settings,$rowtotal,$action) = @_;
 1364:     my $context;
 1365:     if ($action eq 'quotas') {
 1366:         $context = 'tools';
 1367:     } else {
 1368:         $context = $action;
 1369:     }
 1370:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 1371:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1372:     my $typecount = 0;
 1373:     my ($css_class,%titles);
 1374:     if ($context eq 'requestcourses') {
 1375:         @usertools = ('official','unofficial','community');
 1376:         @options =('norequest','approval','validate','autolimit');
 1377:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1378:         %titles = &courserequest_titles();
 1379:     } else {
 1380:         @usertools = ('aboutme','blog','portfolio');
 1381:         %titles = &tool_titles();
 1382:     }
 1383:     if (ref($types) eq 'ARRAY') {
 1384:         foreach my $type (@{$types}) {
 1385:             my $currdefquota;
 1386:             unless ($context eq 'requestcourses') {
 1387:                 if (ref($settings) eq 'HASH') {
 1388:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1389:                         $currdefquota = $settings->{defaultquota}->{$type}; 
 1390:                     } else {
 1391:                         $currdefquota = $settings->{$type};
 1392:                     }
 1393:                 }
 1394:             }
 1395:             if (defined($usertypes->{$type})) {
 1396:                 $typecount ++;
 1397:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1398:                 $datatable .= '<tr'.$css_class.'>'.
 1399:                               '<td>'.$usertypes->{$type}.'</td>'.
 1400:                               '<td class="LC_left_item">';
 1401:                 if ($context eq 'requestcourses') {
 1402:                     $datatable .= '<table><tr>';
 1403:                 }
 1404:                 my %cell;  
 1405:                 foreach my $item (@usertools) {
 1406:                     if ($context eq 'requestcourses') {
 1407:                         my ($curroption,$currlimit);
 1408:                         if (ref($settings) eq 'HASH') {
 1409:                             if (ref($settings->{$item}) eq 'HASH') {
 1410:                                 $curroption = $settings->{$item}->{$type};
 1411:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1412:                                     $currlimit = $1; 
 1413:                                 }
 1414:                             }
 1415:                         }
 1416:                         if (!$curroption) {
 1417:                             $curroption = 'norequest';
 1418:                         }
 1419:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1420:                         foreach my $option (@options) {
 1421:                             my $val = $option;
 1422:                             if ($option eq 'norequest') {
 1423:                                 $val = 0;  
 1424:                             }
 1425:                             if ($option eq 'validate') {
 1426:                                 my $canvalidate = 0;
 1427:                                 if (ref($validations{$item}) eq 'HASH') { 
 1428:                                     if ($validations{$item}{$type}) {
 1429:                                         $canvalidate = 1;
 1430:                                     }
 1431:                                 }
 1432:                                 next if (!$canvalidate);
 1433:                             }
 1434:                             my $checked = '';
 1435:                             if ($option eq $curroption) {
 1436:                                 $checked = ' checked="checked"';
 1437:                             } elsif ($option eq 'autolimit') {
 1438:                                 if ($curroption =~ /^autolimit/) {
 1439:                                     $checked = ' checked="checked"';
 1440:                                 }                       
 1441:                             } 
 1442:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1443:                                   '<input type="radio" name="crsreq_'.$item.
 1444:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1445:                                   $titles{$option}.'</label>';
 1446:                             if ($option eq 'autolimit') {
 1447:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1448:                                                 $item.'_limit_'.$type.'" size="1" '.
 1449:                                                 'value="'.$currlimit.'" />';
 1450:                             }
 1451:                             $cell{$item} .= '</span> ';
 1452:                             if ($option eq 'autolimit') {
 1453:                                 $cell{$item} .= $titles{'unlimited'};
 1454:                             }
 1455:                         }
 1456:                     } else {
 1457:                         my $checked = 'checked="checked" ';
 1458:                         if (ref($settings) eq 'HASH') {
 1459:                             if (ref($settings->{$item}) eq 'HASH') {
 1460:                                 if ($settings->{$item}->{$type} == 0) {
 1461:                                     $checked = '';
 1462:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1463:                                     $checked =  'checked="checked" ';
 1464:                                 }
 1465:                             }
 1466:                         }
 1467:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1468:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1469:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1470:                                       '</label></span>&nbsp; ';
 1471:                     }
 1472:                 }
 1473:                 if ($context eq 'requestcourses') {
 1474:                     $datatable .= '</tr><tr>';
 1475:                     foreach my $item (@usertools) {
 1476:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1477:                     }
 1478:                     $datatable .= '</tr></table>';
 1479:                 }
 1480:                 $datatable .= '</td>';
 1481:                 unless ($context eq 'requestcourses') {
 1482:                     $datatable .= 
 1483:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1484:                               '<input type="text" name="quota_'.$type.
 1485:                               '" value="'.$currdefquota.
 1486:                               '" size="5" /> Mb</span></td>';
 1487:                 }
 1488:                 $datatable .= '</tr>';
 1489:             }
 1490:         }
 1491:     }
 1492:     unless ($context eq 'requestcourses') {
 1493:         $defaultquota = '20';
 1494:         if (ref($settings) eq 'HASH') {
 1495:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1496:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1497:             } elsif (defined($settings->{'default'})) {
 1498:                 $defaultquota = $settings->{'default'};
 1499:             }
 1500:         }
 1501:     }
 1502:     $typecount ++;
 1503:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1504:     $datatable .= '<tr'.$css_class.'>'.
 1505:                   '<td>'.$othertitle.'</td>'.
 1506:                   '<td class="LC_left_item">';
 1507:     if ($context eq 'requestcourses') {
 1508:         $datatable .= '<table><tr>';
 1509:     }
 1510:     my %defcell;
 1511:     foreach my $item (@usertools) {
 1512:         if ($context eq 'requestcourses') {
 1513:             my ($curroption,$currlimit);
 1514:             if (ref($settings) eq 'HASH') {
 1515:                 if (ref($settings->{$item}) eq 'HASH') {
 1516:                     $curroption = $settings->{$item}->{'default'};
 1517:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1518:                         $currlimit = $1;
 1519:                     }
 1520:                 }
 1521:             }
 1522:             if (!$curroption) {
 1523:                 $curroption = 'norequest';
 1524:             }
 1525:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1526:             foreach my $option (@options) {
 1527:                 my $val = $option;
 1528:                 if ($option eq 'norequest') {
 1529:                     $val = 0;
 1530:                 }
 1531:                 if ($option eq 'validate') {
 1532:                     my $canvalidate = 0;
 1533:                     if (ref($validations{$item}) eq 'HASH') {
 1534:                         if ($validations{$item}{'default'}) {
 1535:                             $canvalidate = 1;
 1536:                         }
 1537:                     }
 1538:                     next if (!$canvalidate);
 1539:                 }
 1540:                 my $checked = '';
 1541:                 if ($option eq $curroption) {
 1542:                     $checked = ' checked="checked"';
 1543:                 } elsif ($option eq 'autolimit') {
 1544:                     if ($curroption =~ /^autolimit/) {
 1545:                         $checked = ' checked="checked"';
 1546:                     }
 1547:                 }
 1548:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1549:                                   '<input type="radio" name="crsreq_'.$item.
 1550:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1551:                                   $titles{$option}.'</label>';
 1552:                 if ($option eq 'autolimit') {
 1553:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1554:                                        $item.'_limit_default" size="1" '.
 1555:                                        'value="'.$currlimit.'" />';
 1556:                 }
 1557:                 $defcell{$item} .= '</span> ';
 1558:                 if ($option eq 'autolimit') {
 1559:                     $defcell{$item} .= $titles{'unlimited'};
 1560:                 }
 1561:             }
 1562:         } else {
 1563:             my $checked = 'checked="checked" ';
 1564:             if (ref($settings) eq 'HASH') {
 1565:                 if (ref($settings->{$item}) eq 'HASH') {
 1566:                     if ($settings->{$item}->{'default'} == 0) {
 1567:                         $checked = '';
 1568:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1569:                         $checked = 'checked="checked" ';
 1570:                     }
 1571:                 }
 1572:             }
 1573:             $datatable .= '<span class="LC_nobreak"><label>'.
 1574:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1575:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1576:                           '</label></span>&nbsp; ';
 1577:         }
 1578:     }
 1579:     if ($context eq 'requestcourses') {
 1580:         $datatable .= '</tr><tr>';
 1581:         foreach my $item (@usertools) {
 1582:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1583:         }
 1584:         $datatable .= '</tr></table>';
 1585:     }
 1586:     $datatable .= '</td>';
 1587:     unless ($context eq 'requestcourses') {
 1588:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1589:                       '<input type="text" name="defaultquota" value="'.
 1590:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1591:     }
 1592:     $datatable .= '</tr>';
 1593:     $typecount ++;
 1594:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1595:     $datatable .= '<tr'.$css_class.'>'.
 1596:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1597:     if ($context eq 'requestcourses') {
 1598:         $datatable .= &mt('(overrides affiliation, if set)').
 1599:                       '</td>'.
 1600:                       '<td class="LC_left_item">'.
 1601:                       '<table><tr>';
 1602:     } else {
 1603:         $datatable .= &mt('(overrides affiliation, if checked)').
 1604:                       '</td>'.
 1605:                       '<td class="LC_left_item" colspan="2">'.
 1606:                       '<br />';
 1607:     }
 1608:     my %advcell;
 1609:     foreach my $item (@usertools) {
 1610:         if ($context eq 'requestcourses') {
 1611:             my ($curroption,$currlimit);
 1612:             if (ref($settings) eq 'HASH') {
 1613:                 if (ref($settings->{$item}) eq 'HASH') {
 1614:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1615:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1616:                         $currlimit = $1;
 1617:                     }
 1618:                 }
 1619:             }
 1620:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1621:             my $checked = '';
 1622:             if ($curroption eq '') {
 1623:                 $checked = ' checked="checked"';
 1624:             }
 1625:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1626:                                '<input type="radio" name="crsreq_'.$item.
 1627:                                '__LC_adv" value=""'.$checked.' />'.
 1628:                                &mt('No override set').'</label></span>&nbsp; ';
 1629:             foreach my $option (@options) {
 1630:                 my $val = $option;
 1631:                 if ($option eq 'norequest') {
 1632:                     $val = 0;
 1633:                 }
 1634:                 if ($option eq 'validate') {
 1635:                     my $canvalidate = 0;
 1636:                     if (ref($validations{$item}) eq 'HASH') {
 1637:                         if ($validations{$item}{'_LC_adv'}) {
 1638:                             $canvalidate = 1;
 1639:                         }
 1640:                     }
 1641:                     next if (!$canvalidate);
 1642:                 }
 1643:                 my $checked = '';
 1644:                 if ($val eq $curroption) {
 1645:                     $checked = ' checked="checked"';
 1646:                 } elsif ($option eq 'autolimit') {
 1647:                     if ($curroption =~ /^autolimit/) {
 1648:                         $checked = ' checked="checked"';
 1649:                     }
 1650:                 }
 1651:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1652:                                   '<input type="radio" name="crsreq_'.$item.
 1653:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1654:                                   $titles{$option}.'</label>';
 1655:                 if ($option eq 'autolimit') {
 1656:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1657:                                        $item.'_limit__LC_adv" size="1" '.
 1658:                                        'value="'.$currlimit.'" />';
 1659:                 }
 1660:                 $advcell{$item} .= '</span> ';
 1661:                 if ($option eq 'autolimit') {
 1662:                     $advcell{$item} .= $titles{'unlimited'};
 1663:                 }
 1664:             }
 1665:         } else {
 1666:             my $checked = 'checked="checked" ';
 1667:             if (ref($settings) eq 'HASH') {
 1668:                 if (ref($settings->{$item}) eq 'HASH') {
 1669:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1670:                         $checked = '';
 1671:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1672:                         $checked = 'checked="checked" ';
 1673:                     }
 1674:                 }
 1675:             }
 1676:             $datatable .= '<span class="LC_nobreak"><label>'.
 1677:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1678:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1679:                           '</label></span>&nbsp; ';
 1680:         }
 1681:     }
 1682:     if ($context eq 'requestcourses') {
 1683:         $datatable .= '</tr><tr>';
 1684:         foreach my $item (@usertools) {
 1685:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1686:         }
 1687:         $datatable .= '</tr></table>';
 1688:     }
 1689:     $datatable .= '</td></tr>';
 1690:     $$rowtotal += $typecount;
 1691:     return $datatable;
 1692: }
 1693: 
 1694: sub print_courserequestmail {
 1695:     my ($dom,$settings,$rowtotal) = @_;
 1696:     my ($now,$datatable,%dompersonnel,@domcoord,@currapproval,$rows);
 1697:     $now = time;
 1698:     $rows = 0;
 1699:     %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 1700:     foreach my $server (keys(%dompersonnel)) {
 1701:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 1702:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 1703:             if (!grep(/^$uname:$udom$/,@domcoord)) {
 1704:                 push(@domcoord,$uname.':'.$udom);
 1705:             }
 1706:         }
 1707:     }
 1708:     if (ref($settings) eq 'HASH') {
 1709:         if (ref($settings->{'notify'}) eq 'HASH') {
 1710:             if ($settings->{'notify'}{'approval'} ne '') {
 1711:                @currapproval = split(',',$settings->{'notify'}{'approval'});
 1712:             }
 1713:         }
 1714:     }
 1715:     if (@currapproval) {
 1716:         foreach my $dc (@currapproval) {
 1717:             unless (grep(/^\Q$dc\E$/,@domcoord)) {
 1718:                 push(@domcoord,$dc);
 1719:             }
 1720:         }
 1721:     }
 1722:     @domcoord = sort(@domcoord);
 1723:     my $numinrow = 4;
 1724:     my $numdc = @domcoord;
 1725:     my $css_class = 'class="LC_odd_row"';
 1726:     $datatable = '<tr'.$css_class.'>'.
 1727:                  ' <td>'.&mt('Receive notification of course requests requiring approval.').
 1728:                  ' </td>'.
 1729:                  ' <td class="LC_left_item">';
 1730:     if (@domcoord > 0) {
 1731:         $datatable .= '<table>';
 1732:         for (my $i=0; $i<$numdc; $i++) {
 1733:             my $rem = $i%($numinrow);
 1734:             if ($rem == 0) {
 1735:                 if ($i > 0) {
 1736:                     $datatable .= '</tr>';
 1737:                 }
 1738:                 $datatable .= '<tr>';
 1739:                 $rows ++;
 1740:             }
 1741:             my $check = ' ';
 1742:             if (grep(/^\Q$domcoord[$i]\E$/,@currapproval)) {
 1743:                 $check = ' checked="checked" ';
 1744:             }
 1745:             my ($uname,$udom) = split(':',$domcoord[$i]);
 1746:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
 1747:             if ($i == $numdc-1) {
 1748:                 my $colsleft = $numinrow-$rem;
 1749:                 if ($colsleft > 1) {
 1750:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 1751:                 } else {
 1752:                     $datatable .= '<td class="LC_left_item">';
 1753:                 }
 1754:             } else {
 1755:                 $datatable .= '<td class="LC_left_item">';
 1756:             }
 1757:             $datatable .= '<span class="LC_nobreak"><label>'.
 1758:                           '<input type="checkbox" name="reqapprovalnotify" '.
 1759:                           'value="'.$domcoord[$i].'"'.$check.'/>'.
 1760:                           $fullname.'</label></span></td>';
 1761:         }
 1762:         $datatable .= '</tr></table>';
 1763:     } else {
 1764:         $datatable .= &mt('There are no active Domain Coordinators');
 1765:         $rows ++;
 1766:     }
 1767:     $datatable .='</td></tr>';
 1768:     $$rowtotal += $rows;
 1769:     return $datatable;
 1770: }
 1771: 
 1772: sub print_autoenroll {
 1773:     my ($dom,$settings,$rowtotal) = @_;
 1774:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1775:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1776:     if (ref($settings) eq 'HASH') {
 1777:         if (exists($settings->{'run'})) {
 1778:             if ($settings->{'run'} eq '0') {
 1779:                 $runoff = ' checked="checked" ';
 1780:                 $runon = ' ';
 1781:             } else {
 1782:                 $runon = ' checked="checked" ';
 1783:                 $runoff = ' ';
 1784:             }
 1785:         } else {
 1786:             if ($autorun) {
 1787:                 $runon = ' checked="checked" ';
 1788:                 $runoff = ' ';
 1789:             } else {
 1790:                 $runoff = ' checked="checked" ';
 1791:                 $runon = ' ';
 1792:             }
 1793:         }
 1794:         if (exists($settings->{'co-owners'})) {
 1795:             if ($settings->{'co-owners'} eq '0') {
 1796:                 $coownersoff = ' checked="checked" ';
 1797:                 $coownerson = ' ';
 1798:             } else {
 1799:                 $coownerson = ' checked="checked" ';
 1800:                 $coownersoff = ' ';
 1801:             }
 1802:         } else {
 1803:             $coownersoff = ' checked="checked" ';
 1804:             $coownerson = ' ';
 1805:         }
 1806:         if (exists($settings->{'sender_domain'})) {
 1807:             $defdom = $settings->{'sender_domain'};
 1808:         }
 1809:     } else {
 1810:         if ($autorun) {
 1811:             $runon = ' checked="checked" ';
 1812:             $runoff = ' ';
 1813:         } else {
 1814:             $runoff = ' checked="checked" ';
 1815:             $runon = ' ';
 1816:         }
 1817:     }
 1818:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 1819:     my $notif_sender;
 1820:     if (ref($settings) eq 'HASH') {
 1821:         $notif_sender = $settings->{'sender_uname'};
 1822:     }
 1823:     my $datatable='<tr class="LC_odd_row">'.
 1824:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 1825:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1826:                   '<input type="radio" name="autoenroll_run"'.
 1827:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1828:                   '<label><input type="radio" name="autoenroll_run"'.
 1829:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 1830:                   '</tr><tr>'.
 1831:                   '<td>'.&mt('Notification messages - sender').
 1832:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 1833:                   &mt('username').':&nbsp;'.
 1834:                   '<input type="text" name="sender_uname" value="'.
 1835:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 1836:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 1837:                   '<tr class="LC_odd_row">'.
 1838:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 1839:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1840:                   '<input type="radio" name="autoassign_coowners"'.
 1841:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1842:                   '<label><input type="radio" name="autoassign_coowners"'.
 1843:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 1844:                   '</tr>';
 1845:     $$rowtotal += 3;
 1846:     return $datatable;
 1847: }
 1848: 
 1849: sub print_autoupdate {
 1850:     my ($position,$dom,$settings,$rowtotal) = @_;
 1851:     my $datatable;
 1852:     if ($position eq 'top') {
 1853:         my $updateon = ' ';
 1854:         my $updateoff = ' checked="checked" ';
 1855:         my $classlistson = ' ';
 1856:         my $classlistsoff = ' checked="checked" ';
 1857:         if (ref($settings) eq 'HASH') {
 1858:             if ($settings->{'run'} eq '1') {
 1859:                 $updateon = $updateoff;
 1860:                 $updateoff = ' ';
 1861:             }
 1862:             if ($settings->{'classlists'} eq '1') {
 1863:                 $classlistson = $classlistsoff;
 1864:                 $classlistsoff = ' ';
 1865:             }
 1866:         }
 1867:         my %title = (
 1868:                    run => 'Auto-update active?',
 1869:                    classlists => 'Update information in classlists?',
 1870:                     );
 1871:         $datatable = '<tr class="LC_odd_row">'. 
 1872:                   '<td>'.&mt($title{'run'}).'</td>'.
 1873:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1874:                   '<input type="radio" name="autoupdate_run"'.
 1875:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1876:                   '<label><input type="radio" name="autoupdate_run"'.
 1877:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 1878:                   '</tr><tr>'.
 1879:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 1880:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 1881:                   '<label><input type="radio" name="classlists"'.
 1882:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1883:                   '<label><input type="radio" name="classlists"'.
 1884:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 1885:                   '</tr>';
 1886:         $$rowtotal += 2;
 1887:     } elsif ($position eq 'middle') {
 1888:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1889:         my $numinrow = 3;
 1890:         my $locknamesettings;
 1891:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 1892:                                      $dom,$numinrow,$othertitle,
 1893:                                     'lockablenames');
 1894:         $$rowtotal ++;
 1895:     } else {
 1896:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1897:         my @fields = ('lastname','firstname','middlename','generation',
 1898:                       'permanentemail','id');
 1899:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 1900:         my $numrows = 0;
 1901:         if (ref($types) eq 'ARRAY') {
 1902:             if (@{$types} > 0) {
 1903:                 $datatable = 
 1904:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 1905:                                          \@fields,$types,\$numrows);
 1906:                     $$rowtotal += @{$types}; 
 1907:             }
 1908:         }
 1909:         $datatable .= 
 1910:             &usertype_update_row($settings,{'default' => $othertitle},
 1911:                                  \%fieldtitles,\@fields,['default'],
 1912:                                  \$numrows);
 1913:         $$rowtotal ++;     
 1914:     }
 1915:     return $datatable;
 1916: }
 1917: 
 1918: sub print_autocreate {
 1919:     my ($dom,$settings,$rowtotal) = @_;
 1920:     my (%createon,%createoff);
 1921:     my $curr_dc;
 1922:     my @types = ('xml','req');
 1923:     if (ref($settings) eq 'HASH') {
 1924:         foreach my $item (@types) {
 1925:             $createoff{$item} = ' checked="checked" ';
 1926:             $createon{$item} = ' ';
 1927:             if (exists($settings->{$item})) {
 1928:                 if ($settings->{$item}) {
 1929:                     $createon{$item} = ' checked="checked" ';
 1930:                     $createoff{$item} = ' ';
 1931:                 }
 1932:             }
 1933:         }
 1934:         $curr_dc = $settings->{'xmldc'};
 1935:     } else {
 1936:         foreach my $item (@types) {
 1937:             $createoff{$item} = ' checked="checked" ';
 1938:             $createon{$item} = ' ';
 1939:         }
 1940:     }
 1941:     $$rowtotal += 2;
 1942:     my $datatable='<tr class="LC_odd_row">'.
 1943:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 1944:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1945:                   '<input type="radio" name="autocreate_xml"'.
 1946:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1947:                   '<label><input type="radio" name="autocreate_xml"'.
 1948:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 1949:                   '</td></tr><tr>'.
 1950:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 1951:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1952:                   '<input type="radio" name="autocreate_req"'.
 1953:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1954:                   '<label><input type="radio" name="autocreate_req"'.
 1955:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 1956:     my ($numdc,$dctable) = &active_dc_picker($dom,$curr_dc);
 1957:     if ($numdc > 1) {
 1958:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 1959:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 1960:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 1961:         $$rowtotal ++ ;
 1962:     } else {
 1963:         $datatable .= $dctable.'</td></tr>';
 1964:     }
 1965:     return $datatable;
 1966: }
 1967: 
 1968: sub print_directorysrch {
 1969:     my ($dom,$settings,$rowtotal) = @_;
 1970:     my $srchon = ' ';
 1971:     my $srchoff = ' checked="checked" ';
 1972:     my ($exacton,$containson,$beginson);
 1973:     my $localon = ' ';
 1974:     my $localoff = ' checked="checked" ';
 1975:     if (ref($settings) eq 'HASH') {
 1976:         if ($settings->{'available'} eq '1') {
 1977:             $srchon = $srchoff;
 1978:             $srchoff = ' ';
 1979:         }
 1980:         if ($settings->{'localonly'} eq '1') {
 1981:             $localon = $localoff;
 1982:             $localoff = ' ';
 1983:         }
 1984:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 1985:             foreach my $type (@{$settings->{'searchtypes'}}) {
 1986:                 if ($type eq 'exact') {
 1987:                     $exacton = ' checked="checked" ';
 1988:                 } elsif ($type eq 'contains') {
 1989:                     $containson = ' checked="checked" ';
 1990:                 } elsif ($type eq 'begins') {
 1991:                     $beginson = ' checked="checked" ';
 1992:                 }
 1993:             }
 1994:         } else {
 1995:             if ($settings->{'searchtypes'} eq 'exact') {
 1996:                 $exacton = ' checked="checked" ';
 1997:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 1998:                 $containson = ' checked="checked" ';
 1999:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2000:                 $exacton = ' checked="checked" ';
 2001:                 $containson = ' checked="checked" ';
 2002:             }
 2003:         }
 2004:     }
 2005:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2006:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2007: 
 2008:     my $numinrow = 4;
 2009:     my $cansrchrow = 0;
 2010:     my $datatable='<tr class="LC_odd_row">'.
 2011:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2012:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2013:                   '<input type="radio" name="dirsrch_available"'.
 2014:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2015:                   '<label><input type="radio" name="dirsrch_available"'.
 2016:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2017:                   '</tr><tr>'.
 2018:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2019:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2020:                   '<input type="radio" name="dirsrch_localonly"'.
 2021:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2022:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2023:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2024:                   '</tr>';
 2025:     $$rowtotal += 2;
 2026:     if (ref($usertypes) eq 'HASH') {
 2027:         if (keys(%{$usertypes}) > 0) {
 2028:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2029:                                          $numinrow,$othertitle,'cansearch');
 2030:             $cansrchrow = 1;
 2031:         }
 2032:     }
 2033:     if ($cansrchrow) {
 2034:         $$rowtotal ++;
 2035:         $datatable .= '<tr>';
 2036:     } else {
 2037:         $datatable .= '<tr class="LC_odd_row">';
 2038:     }
 2039:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2040:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2041:     foreach my $title (@{$titleorder}) {
 2042:         if (defined($searchtitles->{$title})) {
 2043:             my $check = ' ';
 2044:             if (ref($settings) eq 'HASH') {
 2045:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2046:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2047:                         $check = ' checked="checked" ';
 2048:                     }
 2049:                 }
 2050:             }
 2051:             $datatable .= '<td class="LC_left_item">'.
 2052:                           '<span class="LC_nobreak"><label>'.
 2053:                           '<input type="checkbox" name="searchby" '.
 2054:                           'value="'.$title.'"'.$check.'/>'.
 2055:                           $searchtitles->{$title}.'</label></span></td>';
 2056:         }
 2057:     }
 2058:     $datatable .= '</tr></table></td></tr>';
 2059:     $$rowtotal ++;
 2060:     if ($cansrchrow) {
 2061:         $datatable .= '<tr class="LC_odd_row">';
 2062:     } else {
 2063:         $datatable .= '<tr>';
 2064:     }
 2065:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2066:                   '<td class="LC_left_item" colspan="2">'.
 2067:                   '<span class="LC_nobreak"><label>'.
 2068:                   '<input type="checkbox" name="searchtypes" '.
 2069:                   $exacton.' value="exact" />'.&mt('Exact match').
 2070:                   '</label>&nbsp;'.
 2071:                   '<label><input type="checkbox" name="searchtypes" '.
 2072:                   $beginson.' value="begins" />'.&mt('Begins with').
 2073:                   '</label>&nbsp;'.
 2074:                   '<label><input type="checkbox" name="searchtypes" '.
 2075:                   $containson.' value="contains" />'.&mt('Contains').
 2076:                   '</label></span></td></tr>';
 2077:     $$rowtotal ++;
 2078:     return $datatable;
 2079: }
 2080: 
 2081: sub print_contacts {
 2082:     my ($dom,$settings,$rowtotal) = @_;
 2083:     my $datatable;
 2084:     my @contacts = ('adminemail','supportemail');
 2085:     my (%checked,%to,%otheremails,%bccemails);
 2086:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2087:                     'requestsmail');
 2088:     foreach my $type (@mailings) {
 2089:         $otheremails{$type} = '';
 2090:     }
 2091:     $bccemails{'helpdeskmail'} = '';
 2092:     if (ref($settings) eq 'HASH') {
 2093:         foreach my $item (@contacts) {
 2094:             if (exists($settings->{$item})) {
 2095:                 $to{$item} = $settings->{$item};
 2096:             }
 2097:         }
 2098:         foreach my $type (@mailings) {
 2099:             if (exists($settings->{$type})) {
 2100:                 if (ref($settings->{$type}) eq 'HASH') {
 2101:                     foreach my $item (@contacts) {
 2102:                         if ($settings->{$type}{$item}) {
 2103:                             $checked{$type}{$item} = ' checked="checked" ';
 2104:                         }
 2105:                     }
 2106:                     $otheremails{$type} = $settings->{$type}{'others'};
 2107:                     if ($type eq 'helpdeskmail') {
 2108:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2109:                     }
 2110:                 }
 2111:             } elsif ($type eq 'lonstatusmail') {
 2112:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2113:             }
 2114:         }
 2115:     } else {
 2116:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2117:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2118:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2119:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2120:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2121:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2122:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2123:     }
 2124:     my ($titles,$short_titles) = &contact_titles();
 2125:     my $rownum = 0;
 2126:     my $css_class;
 2127:     foreach my $item (@contacts) {
 2128:         $rownum ++;
 2129:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2130:         $datatable .= '<tr'.$css_class.'>'. 
 2131:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2132:                   '</span></td><td class="LC_right_item">'.
 2133:                   '<input type="text" name="'.$item.'" value="'.
 2134:                   $to{$item}.'" /></td></tr>';
 2135:     }
 2136:     foreach my $type (@mailings) {
 2137:         $rownum ++;
 2138:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2139:         $datatable .= '<tr'.$css_class.'>'.
 2140:                       '<td><span class="LC_nobreak">'.
 2141:                       $titles->{$type}.': </span></td>'.
 2142:                       '<td class="LC_left_item">'.
 2143:                       '<span class="LC_nobreak">';
 2144:         foreach my $item (@contacts) {
 2145:             $datatable .= '<label>'.
 2146:                           '<input type="checkbox" name="'.$type.'"'.
 2147:                           $checked{$type}{$item}.
 2148:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2149:                           '</label>&nbsp;';
 2150:         }
 2151:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2152:                       '<input type="text" name="'.$type.'_others" '.
 2153:                       'value="'.$otheremails{$type}.'"  />';
 2154:         if ($type eq 'helpdeskmail') {
 2155:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2156:                           '<input type="text" name="'.$type.'_bcc" '.
 2157:                           'value="'.$bccemails{$type}.'"  />';
 2158:         }
 2159:         $datatable .= '</td></tr>'."\n";
 2160:     }
 2161:     $$rowtotal += $rownum;
 2162:     return $datatable;
 2163: }
 2164: 
 2165: sub print_helpsettings {
 2166: 
 2167: 	my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 2168: 	my ($css_class,$datatable);
 2169: 	
 2170: 	my $switchserver = &check_switchserver($dom,$confname);
 2171: 	
 2172: 	my $itemcount = 1;
 2173: 	
 2174: 	if ($position eq 'top') {
 2175: 		
 2176: 		my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2177: 		
 2178: 		%choices =
 2179: 			&Apache::lonlocal::texthash (
 2180: 				submitbugs => 'Display &quot;Submit a bug&quot; link?',
 2181: 		);
 2182: 		
 2183: 		%defaultchecked = ('submitbugs' => 'on');
 2184: 		
 2185: 		@toggles = ('submitbugs',);
 2186: 		
 2187: 		foreach my $item (@toggles) {
 2188: 			if ($defaultchecked{$item} eq 'on') { 
 2189: 				$checkedon{$item} = ' checked="checked" ';
 2190: 				$checkedoff{$item} = ' ';
 2191: 			} elsif ($defaultchecked{$item} eq 'off') {
 2192: 				$checkedoff{$item} = ' checked="checked" ';
 2193: 				$checkedon{$item} = ' ';
 2194: 			}
 2195: 		}
 2196: 		
 2197: 		if (ref($settings) eq 'HASH') {
 2198: 			foreach my $item (@toggles) {
 2199: 				if ($settings->{$item} eq '1') {
 2200: 					$checkedon{$item} =  ' checked="checked" ';
 2201: 					$checkedoff{$item} = ' ';
 2202: 				} elsif ($settings->{$item} eq '0') {
 2203: 					$checkedoff{$item} =  ' checked="checked" ';
 2204: 					$checkedon{$item} = ' ';
 2205: 				}
 2206: 			}
 2207: 		 }
 2208: 		
 2209: 		 foreach my $item (@toggles) {
 2210: 			$css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2211: 			$datatable .=  
 2212: 				'<tr'.$css_class.'>
 2213: 				<td><span class="LC_nobreak">'.$choices{$item}.'</span></td>
 2214: 				<td><span class="LC_nobreak">&nbsp;</span></td>
 2215: 				<td class="LC_right_item"><span class="LC_nobreak">
 2216: 				<label><input type="radio" name="'.$item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').'</label>&nbsp;
 2217: 				<label><input type="radio" name="'.$item.'" '.$checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2218: 				'</span></td>'.
 2219: 				'</tr>';
 2220: 			$itemcount ++;
 2221: 		 }
 2222:      
 2223:      } else {
 2224:      
 2225:      	$css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2226:      	
 2227:      	$datatable .= '<tr'.$css_class.'>';
 2228:      	
 2229:      	if (ref($settings) eq 'HASH') {
 2230: 			if ($settings->{'loginhelpurl'} ne '') {
 2231: 				my($directory, $filename) = $settings->{'loginhelpurl'} =~ m/(.*\/)(.*)$/;
 2232: 				$datatable .= '<td width="33%"><span class="LC_left_item"><label><a href="'.$settings->{'loginhelpurl'}.'" target="_blank">'.&mt('Custom Login Page Help File In Use').'</a></label></span></td>';
 2233: 				$datatable .= '<td width="33%"><span class="LC_right_item"><label><input type="checkbox" name="loginhelpurl_del" value="1" />'.&mt('Delete?').'</label></span></td>'
 2234: 			} else {
 2235: 				$datatable .= '<td width="33%"><span class="LC_left_item"><label>'.&mt('Default Login Page Help File In Use').'</label></span></td>';
 2236: 				$datatable .= '<td width="33%"><span class="LC_right_item">&nbsp;</span></td>';
 2237: 			}
 2238: 		} else {
 2239: 			$datatable .= '<td><span class="LC_left_item">&nbsp;</span></td>';
 2240: 			$datatable .= '<td><span class="LC_right_item">&nbsp;</span></td>';
 2241: 		}
 2242:     	
 2243:      	$datatable .= '<td width="33%"><span class="LC_right_item">';
 2244:      	if ($switchserver) {
 2245:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2246:         } else {
 2247:         	$datatable .= &mt('Upload Custom Login Page Help File:');
 2248:             $datatable .='<input type="file" name="loginhelpurl" />';
 2249:         }
 2250:         $datatable .= '</span></td></tr>';
 2251:         
 2252:      }
 2253:      
 2254:      return $datatable;
 2255: 	
 2256: }
 2257: 
 2258: 
 2259: sub radiobutton_prefs {
 2260:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount) = @_;
 2261:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2262:                    (ref($choices) eq 'HASH'));
 2263: 
 2264:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2265: 
 2266:     foreach my $item (@{$toggles}) {
 2267:         if ($defaultchecked->{$item} eq 'on') {
 2268:             $checkedon{$item} = ' checked="checked" ';
 2269:             $checkedoff{$item} = ' ';
 2270:         } elsif ($defaultchecked->{$item} eq 'off') {
 2271:             $checkedoff{$item} = ' checked="checked" ';
 2272:             $checkedon{$item} = ' ';
 2273:         }
 2274:     }
 2275:     if (ref($settings) eq 'HASH') {
 2276:         foreach my $item (@{$toggles}) {
 2277:             if ($settings->{$item} eq '1') {
 2278:                 $checkedon{$item} =  ' checked="checked" ';
 2279:                 $checkedoff{$item} = ' ';
 2280:             } elsif ($settings->{$item} eq '0') {
 2281:                 $checkedoff{$item} =  ' checked="checked" ';
 2282:                 $checkedon{$item} = ' ';
 2283:             }
 2284:         }
 2285:     }
 2286:     foreach my $item (@{$toggles}) {
 2287:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2288:         $datatable .=
 2289:             '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices->{$item}.
 2290:             '</span></td>'.
 2291:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2292:             '<label><input type="radio" name="'.
 2293:             $item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').
 2294:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2295:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2296:             '</span></td>'.
 2297:             '</tr>';
 2298:         $itemcount ++;
 2299:     }
 2300:     return ($datatable,$itemcount);
 2301: }
 2302: 
 2303: sub print_coursedefaults {
 2304:     my ($position,$dom,$settings,$rowtotal) = @_;
 2305:     my ($css_class,$datatable);
 2306:     my $itemcount = 1;
 2307:     if ($position eq 'top') {
 2308:         my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2309:         %choices =
 2310:             &Apache::lonlocal::texthash (
 2311:                 canuse_pdfforms => 'Course/Community users can create/upload PDF forms',
 2312:         );
 2313:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2314:         @toggles = ('canuse_pdfforms',);
 2315:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2316:                                                  \%choices,$itemcount);
 2317:         $$rowtotal += $itemcount;
 2318:     } else {
 2319:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2320:         my %choices =
 2321:             &Apache::lonlocal::texthash (
 2322:                 anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2323:         );
 2324:         my $currdefresponder;
 2325:         if (ref($settings) eq 'HASH') {
 2326:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2327:         }
 2328:         if (!$currdefresponder) {
 2329:             $currdefresponder = 10;
 2330:         } elsif ($currdefresponder < 1) {
 2331:             $currdefresponder = 1;
 2332:         }
 2333:         $datatable .=
 2334:                '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices{'anonsurvey_threshold'}.
 2335:                 '</span></td>'.
 2336:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2337:                 '<input type="text" name="anonsurvey_threshold"'.
 2338:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2339:                 '</td></tr>';
 2340:     }
 2341:     return $datatable;
 2342: }
 2343: 
 2344: sub print_usersessions {
 2345:     my ($position,$dom,$settings,$rowtotal) = @_;
 2346:     my ($css_class,$datatable,%checked,%choices);
 2347:     my (%by_ip,%by_location,@intdoms);
 2348:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2349: 
 2350:     my @alldoms = &Apache::lonnet::all_domains();
 2351:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2352:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2353:     my %altids = &id_for_thisdom(%servers);
 2354:     my $itemcount = 1;
 2355:     if ($position eq 'top') {
 2356:         if (keys(%serverhomes) > 1) {
 2357:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2358:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2359:         } else {
 2360:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2361:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2362:         }
 2363:     } else {
 2364:         if (keys(%by_location) == 0) {
 2365:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2366:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2367:         } else {
 2368:             my %lt = &usersession_titles();
 2369:             my $numinrow = 5;
 2370:             my $prefix;
 2371:             my @types;
 2372:             if ($position eq 'bottom') {
 2373:                 $prefix = 'remote';
 2374:                 @types = ('version','excludedomain','includedomain');
 2375:             } else {
 2376:                 $prefix = 'hosted';
 2377:                 @types = ('excludedomain','includedomain');
 2378:             }
 2379:             my (%current,%checkedon,%checkedoff);
 2380:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2381:             my @locations = sort(keys(%by_location));
 2382:             foreach my $type (@types) {
 2383:                 $checkedon{$type} = '';
 2384:                 $checkedoff{$type} = ' checked="checked"';
 2385:             }
 2386:             if (ref($settings) eq 'HASH') {
 2387:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2388:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2389:                         $current{$key} = $settings->{$prefix}{$key};
 2390:                         if ($key eq 'version') {
 2391:                             if ($current{$key} ne '') {
 2392:                                 $checkedon{$key} = ' checked="checked"';
 2393:                                 $checkedoff{$key} = '';
 2394:                             }
 2395:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2396:                             $checkedon{$key} = ' checked="checked"';
 2397:                             $checkedoff{$key} = '';
 2398:                         }
 2399:                     }
 2400:                 }
 2401:             }
 2402:             foreach my $type (@types) {
 2403:                 next if ($type ne 'version' && !@locations);
 2404:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2405:                 $datatable .= '<tr'.$css_class.'>
 2406:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2407:                                <span class="LC_nobreak">&nbsp;
 2408:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2409:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2410:                 if ($type eq 'version') {
 2411:                     my $selector = '<select name="'.$prefix.'_version">';
 2412:                     foreach my $version (@lcversions) {
 2413:                         my $selected = '';
 2414:                         if ($current{'version'} eq $version) {
 2415:                             $selected = ' selected="selected"';
 2416:                         }
 2417:                         $selector .= ' <option value="'.$version.'"'.
 2418:                                      $selected.'>'.$version.'</option>';
 2419:                     }
 2420:                     $selector .= '</select> ';
 2421:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2422:                 } else {
 2423:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2424:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2425:                                  ' />'.('&nbsp;'x2).
 2426:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2427:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2428:                                  "\n".
 2429:                                  '</div><div><table>';
 2430:                     my $rem;
 2431:                     for (my $i=0; $i<@locations; $i++) {
 2432:                         my ($showloc,$value,$checkedtype);
 2433:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2434:                             my $ip = $by_location{$locations[$i]}->[0];
 2435:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2436:                                  $value = join(':',@{$by_ip{$ip}});
 2437:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2438:                                 if (ref($current{$type}) eq 'ARRAY') {
 2439:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2440:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2441:                                             $checkedtype = ' checked="checked"';
 2442:                                             last;
 2443:                                         }
 2444:                                     }
 2445:                                 }
 2446:                             }
 2447:                         }
 2448:                         $rem = $i%($numinrow);
 2449:                         if ($rem == 0) {
 2450:                             if ($i > 0) {
 2451:                                 $datatable .= '</tr>';
 2452:                             }
 2453:                             $datatable .= '<tr>';
 2454:                         }
 2455:                         $datatable .= '<td class="LC_left_item">'.
 2456:                                       '<span class="LC_nobreak"><label>'.
 2457:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2458:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2459:                                       '</label></span></td>';
 2460:                     }
 2461:                     $rem = @locations%($numinrow);
 2462:                     my $colsleft = $numinrow - $rem;
 2463:                     if ($colsleft > 1 ) {
 2464:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2465:                                       '&nbsp;</td>';
 2466:                     } elsif ($colsleft == 1) {
 2467:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2468:                     }
 2469:                     $datatable .= '</tr></table>';
 2470:                 }
 2471:                 $datatable .= '</td></tr>';
 2472:                 $itemcount ++;
 2473:             }
 2474:         }
 2475:     }
 2476:     $$rowtotal += $itemcount;
 2477:     return $datatable;
 2478: }
 2479: 
 2480: sub build_location_hashes {
 2481:     my ($intdoms,$by_ip,$by_location) = @_;
 2482:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2483:                   (ref($by_location) eq 'HASH')); 
 2484:     my %iphost = &Apache::lonnet::get_iphost();
 2485:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2486:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2487:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2488:         foreach my $id (@{$iphost{$primary_ip}}) {
 2489:             my $intdom = &Apache::lonnet::internet_dom($id);
 2490:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2491:                 push(@{$intdoms},$intdom);
 2492:             }
 2493:         }
 2494:     }
 2495:     foreach my $ip (keys(%iphost)) {
 2496:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2497:             foreach my $id (@{$iphost{$ip}}) {
 2498:                 my $location = &Apache::lonnet::internet_dom($id);
 2499:                 if ($location) {
 2500:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2501:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2502:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2503:                             push(@{$by_ip->{$ip}},$location);
 2504:                         }
 2505:                     } else {
 2506:                         $by_ip->{$ip} = [$location];
 2507:                     }
 2508:                 }
 2509:             }
 2510:         }
 2511:     }
 2512:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2513:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2514:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2515:             my $first = $by_ip->{$ip}->[0];
 2516:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2517:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2518:                     push(@{$by_location->{$first}},$ip);
 2519:                 }
 2520:             } else {
 2521:                 $by_location->{$first} = [$ip];
 2522:             }
 2523:         }
 2524:     }
 2525:     return;
 2526: }
 2527: 
 2528: sub current_offloads_to {
 2529:     my ($dom,$settings,$servers) = @_;
 2530:     my (%spareid,%otherdomconfigs);
 2531:     if (ref($servers) eq 'HASH') {
 2532:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2533:             my $gotspares;
 2534:             if (ref($settings) eq 'HASH') {
 2535:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2536:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2537:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2538:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2539:                         $gotspares = 1;
 2540:                     }
 2541:                 }
 2542:             }
 2543:             unless ($gotspares) {
 2544:                 my $gotspares;
 2545:                 my $serverhomeID =
 2546:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2547:                 my $serverhomedom =
 2548:                     &Apache::lonnet::host_domain($serverhomeID);
 2549:                 if ($serverhomedom ne $dom) {
 2550:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2551:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2552:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2553:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2554:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2555:                                 $gotspares = 1;
 2556:                             }
 2557:                         }
 2558:                     } else {
 2559:                         $otherdomconfigs{$serverhomedom} =
 2560:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2561:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2562:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2563:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2564:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2565:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2566:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2567:                                         $gotspares = 1;
 2568:                                     }
 2569:                                 }
 2570:                             }
 2571:                         }
 2572:                     }
 2573:                 }
 2574:             }
 2575:             unless ($gotspares) {
 2576:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2577:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2578:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2579:                } else {
 2580:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2581:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2582:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2583:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2584:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2585:                     } else {
 2586:                         my %what = (
 2587:                              spareid => 1,
 2588:                         );
 2589:                         my ($result,$returnhash) = 
 2590:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2591:                         if ($result eq 'ok') { 
 2592:                             if (ref($returnhash) eq 'HASH') {
 2593:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2594:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2595:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2596:                                 }
 2597:                             }
 2598:                         }
 2599:                     }
 2600:                 }
 2601:             }
 2602:         }
 2603:     }
 2604:     return %spareid;
 2605: }
 2606: 
 2607: sub spares_row {
 2608:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2609:     my $css_class;
 2610:     my $numinrow = 4;
 2611:     my $itemcount = 1;
 2612:     my $datatable;
 2613:     my %typetitles = &sparestype_titles();
 2614:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2615:         foreach my $server (sort(keys(%{$servers}))) {
 2616:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2617:             my ($othercontrol,$serverdom);
 2618:             if ($serverhome ne $server) {
 2619:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2620:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2621:             } else {
 2622:                 $serverdom = &Apache::lonnet::host_domain($server);
 2623:                 if ($serverdom ne $dom) {
 2624:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2625:                 }
 2626:             }
 2627:             next unless (ref($spareid->{$server}) eq 'HASH');
 2628:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2629:             $datatable .= '<tr'.$css_class.'>
 2630:                            <td rowspan="2">
 2631:                             <span class="LC_nobreak"><b>'.$server.'</b> when busy, offloads to:</span></td>'."\n";
 2632:             my (%current,%canselect);
 2633:             my @choices = 
 2634:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2635:             foreach my $type ('primary','default') {
 2636:                 if (ref($spareid->{$server}) eq 'HASH') {
 2637:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2638:                         my @spares = @{$spareid->{$server}{$type}};
 2639:                         if (@spares > 0) {
 2640:                             if ($othercontrol) {
 2641:                                 $current{$type} = join(', ',@spares);
 2642:                             } else {
 2643:                                 $current{$type} .= '<table>';
 2644:                                 my $numspares = scalar(@spares);
 2645:                                 for (my $i=0;  $i<@spares; $i++) {
 2646:                                     my $rem = $i%($numinrow);
 2647:                                     if ($rem == 0) {
 2648:                                         if ($i > 0) {
 2649:                                             $current{$type} .= '</tr>';
 2650:                                         }
 2651:                                         $current{$type} .= '<tr>';
 2652:                                     }
 2653:                                     $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;'.
 2654:                                                        $spareid->{$server}{$type}[$i].
 2655:                                                        '</label></td>'."\n";
 2656:                                 }
 2657:                                 my $rem = @spares%($numinrow);
 2658:                                 my $colsleft = $numinrow - $rem;
 2659:                                 if ($colsleft > 1 ) {
 2660:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2661:                                                        '" class="LC_left_item">'.
 2662:                                                        '&nbsp;</td>';
 2663:                                 } elsif ($colsleft == 1) {
 2664:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2665:                                 }
 2666:                                 $current{$type} .= '</tr></table>';
 2667:                             }
 2668:                         }
 2669:                     }
 2670:                     if ($current{$type} eq '') {
 2671:                         $current{$type} = &mt('None specified');
 2672:                     }
 2673:                     if ($othercontrol) {
 2674:                         if ($type eq 'primary') {
 2675:                             $canselect{$type} = $othercontrol;
 2676:                         }
 2677:                     } else {
 2678:                         $canselect{$type} = 
 2679:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2680:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2681:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2682:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2683:                         if (@choices > 0) {
 2684:                             foreach my $lonhost (@choices) {
 2685:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2686:                             }
 2687:                         }
 2688:                         $canselect{$type} .= '</select>'."\n";
 2689:                     }
 2690:                 } else {
 2691:                     $current{$type} = &mt('Could not be determined');
 2692:                     if ($type eq 'primary') {
 2693:                         $canselect{$type} =  $othercontrol;
 2694:                     }
 2695:                 }
 2696:                 if ($type eq 'default') {
 2697:                     $datatable .= '<tr'.$css_class.'>';
 2698:                 }
 2699:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2700:                               '<td>'.$current{$type}.'</td>'."\n".
 2701:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2702:             }
 2703:             $itemcount ++;
 2704:         }
 2705:     }
 2706:     $$rowtotal += $itemcount;
 2707:     return $datatable;
 2708: }
 2709: 
 2710: sub possible_newspares {
 2711:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2712:     my $serverhostname = &Apache::lonnet::hostname($server);
 2713:     my %excluded;
 2714:     if ($serverhostname ne '') {
 2715:         %excluded = (
 2716:                        $serverhostname => 1,
 2717:                     );
 2718:     }
 2719:     if (ref($currspares) eq 'HASH') {
 2720:         foreach my $type (keys(%{$currspares})) {
 2721:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2722:                 if (@{$currspares->{$type}} > 0) {
 2723:                     foreach my $curr (@{$currspares->{$type}}) {
 2724:                         my $hostname = &Apache::lonnet::hostname($curr);
 2725:                         $excluded{$hostname} = 1;
 2726:                     }
 2727:                 }
 2728:             }
 2729:         }
 2730:     }
 2731:     my @choices;
 2732:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2733:         if (keys(%{$serverhomes}) > 1) {
 2734:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2735:                 unless ($excluded{$name}) {
 2736:                     if (exists($altids->{$serverhomes->{$name}})) {
 2737:                         push(@choices,$altids->{$serverhomes->{$name}});
 2738:                     } else {
 2739:                         push(@choices,$serverhomes->{$name});
 2740:                     }
 2741:                 }
 2742:             }
 2743:         }
 2744:     }
 2745:     return sort(@choices);
 2746: }
 2747: 
 2748: sub print_loadbalancing {
 2749:     my ($dom,$settings,$rowtotal) = @_;
 2750:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2751:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2752:     my $numinrow = 1;
 2753:     my $datatable;
 2754:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2755:     my ($currbalancer,$currtargets,$currrules);
 2756:     if (keys(%servers) > 1) {
 2757:         if (ref($settings) eq 'HASH') {
 2758:             $currbalancer = $settings->{'lonhost'};
 2759:             $currtargets = $settings->{'targets'};
 2760:             $currrules = $settings->{'rules'};
 2761:         } else {
 2762:             ($currbalancer,$currtargets) = 
 2763:                 &Apache::lonnet::get_lonbalancer_config(\%servers);
 2764:         }
 2765:     } else {
 2766:         return;
 2767:     }
 2768:     my ($othertitle,$usertypes,$types) =
 2769:         &Apache::loncommon::sorted_inst_types($dom);
 2770:     my $rownum = 6;
 2771:     if (ref($types) eq 'ARRAY') {
 2772:         $rownum += scalar(@{$types});
 2773:     }
 2774:     my $css_class = ' class="LC_odd_row"';
 2775:     my $targets_div_style = 'display: none';
 2776:     my $disabled_div_style = 'display: block';
 2777:     my $homedom_div_style = 'display: none';
 2778:     $datatable = '<tr'.$css_class.'>'.
 2779:                  '<td rowspan="'.$rownum.'" valign="top">'.
 2780:                  '<p><select name="loadbalancing_lonhost" onchange="toggleTargets();">'."\n".
 2781:                  '<option value=""';
 2782:     if (($currbalancer eq '') || (!grep(/^\Q$currbalancer\E$/,keys(%servers)))) {
 2783:         $datatable .= ' selected="selected"';
 2784:     } else {
 2785:         $targets_div_style = 'display: block';
 2786:         $disabled_div_style = 'display: none';
 2787:         if ($dom eq &Apache::lonnet::host_domain($currbalancer)) {
 2788:             $homedom_div_style = 'display: block'; 
 2789:         }
 2790:     }
 2791:     $datatable .= '>'.&mt('None').'</option>'."\n";
 2792:     foreach my $lonhost (sort(keys(%servers))) {
 2793:         my $selected;
 2794:         if ($lonhost eq $currbalancer) {
 2795:             $selected .= ' selected="selected"';
 2796:         }
 2797:         $datatable .= '<option value="'.$lonhost.'"'.$selected.'>'.$lonhost.'</option>'."\n";
 2798:     }
 2799:     $datatable .= '</select></p></td><td rowspan="'.$rownum.'" valign="top">'.
 2800:                   '<div id="loadbalancing_disabled" style="'.$disabled_div_style.'">'.&mt('No dedicated Load Balancer').'</div>'."\n".
 2801:                   '<div id="loadbalancing_targets" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2802:     my ($numspares,@spares) = &count_servers($currbalancer,%servers);
 2803:     my @sparestypes = ('primary','default');
 2804:     my %typetitles = &sparestype_titles();
 2805:     foreach my $sparetype (@sparestypes) {
 2806:         my $targettable;
 2807:         for (my $i=0; $i<$numspares; $i++) {
 2808:             my $checked;
 2809:             if (ref($currtargets) eq 'HASH') {
 2810:                 if (ref($currtargets->{$sparetype}) eq 'ARRAY') {
 2811:                     if (grep(/^\Q$spares[$i]\E$/,@{$currtargets->{$sparetype}})) {
 2812:                         $checked = ' checked="checked"';
 2813:                     }
 2814:                 }
 2815:             }
 2816:             my $chkboxval;
 2817:             if (($currbalancer ne '') && (grep((/^\Q$currbalancer\E$/,keys(%servers))))) {
 2818:                 $chkboxval = $spares[$i];
 2819:             }
 2820:             $targettable .= '<td><label><input type="checkbox" name="loadbalancing_target_'.$sparetype.'"'.
 2821:                       $checked.' value="'.$chkboxval.'" id="loadbalancing_target_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$sparetype'".');" /><span id="loadbalancing_targettxt_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 2822:                       '</span></label></td>';
 2823:             my $rem = $i%($numinrow);
 2824:             if ($rem == 0) {
 2825:                 if ($i > 0) {
 2826:                     $targettable .= '</tr>';
 2827:                 }
 2828:                 $targettable .= '<tr>';
 2829:             }
 2830:         }
 2831:         if ($targettable ne '') {
 2832:             my $rem = $numspares%($numinrow);
 2833:             my $colsleft = $numinrow - $rem;
 2834:             if ($colsleft > 1 ) {
 2835:                 $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2836:                                 '&nbsp;</td>';
 2837:             } elsif ($colsleft == 1) {
 2838:                 $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 2839:             }
 2840:             $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 2841:                            '<table><tr>'.$targettable.'</table><br />';
 2842:         }
 2843:     }
 2844:     $datatable .= '</div></td></tr>'.
 2845:                   &loadbalancing_rules($dom,$intdom,$currrules,$othertitle,
 2846:                                        $usertypes,$types,\%servers,$currbalancer,
 2847:                                        $targets_div_style,$homedom_div_style,$css_class);
 2848:     $$rowtotal += $rownum;
 2849:     return $datatable;
 2850: }
 2851: 
 2852: sub loadbalancing_rules {
 2853:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 2854:         $currbalancer,$targets_div_style,$homedom_div_style,$css_class) = @_;
 2855:     my $output;
 2856:     my ($alltypes,$othertypes,$titles) = 
 2857:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 2858:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 2859:         foreach my $type (@{$alltypes}) {
 2860:             my $current;
 2861:             if (ref($currrules) eq 'HASH') {
 2862:                 $current = $currrules->{$type};
 2863:             }
 2864:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 2865:                 if ($dom ne &Apache::lonnet::host_domain($currbalancer)) {
 2866:                     $current = '';
 2867:                 }
 2868:             }
 2869:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 2870:                                              $servers,$currbalancer,$dom,
 2871:                                              $targets_div_style,$homedom_div_style,$css_class);
 2872:         }
 2873:     }
 2874:     return $output;
 2875: }
 2876: 
 2877: sub loadbalancing_titles {
 2878:     my ($dom,$intdom,$usertypes,$types) = @_;
 2879:     my %othertypes = (
 2880:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 2881:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 2882:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 2883:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 2884:                      );
 2885:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 2886:     if (ref($types) eq 'ARRAY') {
 2887:         unshift(@alltypes,@{$types},'default');
 2888:     }
 2889:     my %titles;
 2890:     foreach my $type (@alltypes) {
 2891:         if ($type =~ /^_LC_/) {
 2892:             $titles{$type} = $othertypes{$type};
 2893:         } elsif ($type eq 'default') {
 2894:             $titles{$type} = &mt('All users from [_1]',$dom);
 2895:             if (ref($types) eq 'ARRAY') {
 2896:                 if (@{$types} > 0) {
 2897:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 2898:                 }
 2899:             }
 2900:         } elsif (ref($usertypes) eq 'HASH') {
 2901:             $titles{$type} = $usertypes->{$type};
 2902:         }
 2903:     }
 2904:     return (\@alltypes,\%othertypes,\%titles);
 2905: }
 2906: 
 2907: sub loadbalance_rule_row {
 2908:     my ($type,$title,$current,$servers,$currbalancer,$dom,$targets_div_style,
 2909:         $homedom_div_style,$css_class) = @_;
 2910:     my @rulenames = ('default','homeserver');
 2911:     my %ruletitles = &offloadtype_text();
 2912:     if ($type eq '_LC_external') {
 2913:         push(@rulenames,'externalbalancer');
 2914:     } else {
 2915:         push(@rulenames,'specific');
 2916:     }
 2917:     my $style = $targets_div_style;
 2918:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 2919:         $style = $homedom_div_style;
 2920:     }
 2921:     my $output = 
 2922:         '<tr'.$css_class.'><td valign="top"><div id="balanceruletitle_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 2923:         '<td><div id="balancerule_'.$type.'" style="'.$style.'">'."\n";
 2924:     for (my $i=0; $i<@rulenames; $i++) {
 2925:         my $rule = $rulenames[$i];
 2926:         my ($checked,$extra);
 2927:         if ($rulenames[$i] eq 'default') {
 2928:             $rule = '';
 2929:         }
 2930:         if ($rulenames[$i] eq 'specific') {
 2931:             if (ref($servers) eq 'HASH') {
 2932:                 my $default;
 2933:                 if (($current ne '') && (exists($servers->{$current}))) {
 2934:                     $checked = ' checked="checked"';
 2935:                 }
 2936:                 unless ($checked) {
 2937:                     $default = ' selected="selected"';
 2938:                 }
 2939:                 $extra = ':&nbsp;<select name="loadbalancing_singleserver_'.$type.
 2940:                          '" id="loadbalancing_singleserver_'.$type.
 2941:                          '" onchange="singleServerToggle('."'$type'".')">'."\n".
 2942:                          '<option value=""'.$default.'></option>'."\n";
 2943:                 foreach my $lonhost (sort(keys(%{$servers}))) {
 2944:                     next if ($lonhost eq $currbalancer);
 2945:                     my $selected;
 2946:                     if ($lonhost eq $current) {
 2947:                         $selected = ' selected="selected"';
 2948:                     }
 2949:                     $extra .= '<option value="'.$lonhost.'"'.$selected.'>'.$lonhost.'</option>';
 2950:                 }
 2951:                 $extra .= '</select>';
 2952:             }
 2953:         } elsif ($rule eq $current) {
 2954:             $checked = ' checked="checked"';
 2955:         }
 2956:         $output .= '<span class="LC_nobreak"><label>'.
 2957:                    '<input type="radio" name="loadbalancing_rules_'.$type.
 2958:                    '" id="loadbalancing_rules_'.$type.'_'.$i.'" value="'.
 2959:                    $rule.'" onclick="balanceruleChange('."this.form,'$type'".
 2960:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 2961:                    '</label>'.$extra.'</span><br />'."\n";
 2962:     }
 2963:     $output .= '</div></td></tr>'."\n";
 2964:     return $output;
 2965: }
 2966: 
 2967: sub offloadtype_text {
 2968:     my %ruletitles = &Apache::lonlocal::texthash (
 2969:            'default'          => 'Offloads to default destinations',
 2970:            'homeserver'       => "Offloads to user's home server",
 2971:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 2972:            'specific'         => 'Offloads to specific server',
 2973:     );
 2974:     return %ruletitles;
 2975: }
 2976: 
 2977: sub sparestype_titles {
 2978:     my %typestitles = &Apache::lonlocal::texthash (
 2979:                           'primary' => 'primary',
 2980:                           'default' => 'default',
 2981:                       );
 2982:     return %typestitles;
 2983: }
 2984: 
 2985: sub contact_titles {
 2986:     my %titles = &Apache::lonlocal::texthash (
 2987:                    'supportemail' => 'Support E-mail address',
 2988:                    'adminemail'   => 'Default Server Admin E-mail address',
 2989:                    'errormail'    => 'Error reports to be e-mailed to',
 2990:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 2991:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 2992:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 2993:                    'requestsmail' => 'E-mail from course requests requiring approval',
 2994:                  );
 2995:     my %short_titles = &Apache::lonlocal::texthash (
 2996:                            adminemail   => 'Admin E-mail address',
 2997:                            supportemail => 'Support E-mail',
 2998:                        );   
 2999:     return (\%titles,\%short_titles);
 3000: }
 3001: 
 3002: sub tool_titles {
 3003:     my %titles = &Apache::lonlocal::texthash (
 3004:                      aboutme    => 'Personal Information Page',
 3005:                      blog       => 'Blog',
 3006:                      portfolio  => 'Portfolio',
 3007:                      official   => 'Official courses (with institutional codes)',
 3008:                      unofficial => 'Unofficial courses',
 3009:                      community  => 'Communities',
 3010:                  );
 3011:     return %titles;
 3012: }
 3013: 
 3014: sub courserequest_titles {
 3015:     my %titles = &Apache::lonlocal::texthash (
 3016:                                    official   => 'Official',
 3017:                                    unofficial => 'Unofficial',
 3018:                                    community  => 'Communities',
 3019:                                    norequest  => 'Not allowed',
 3020:                                    approval   => 'Approval by Dom. Coord.',
 3021:                                    validate   => 'With validation',
 3022:                                    autolimit  => 'Numerical limit',
 3023:                                    unlimited  => '(blank for unlimited)',
 3024:                  );
 3025:     return %titles;
 3026: }
 3027: 
 3028: sub courserequest_conditions {
 3029:     my %conditions = &Apache::lonlocal::texthash (
 3030:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3031:        validate   => '(Processing of request subject to instittutional validation).',
 3032:                  );
 3033:     return %conditions;
 3034: }
 3035: 
 3036: 
 3037: sub print_usercreation {
 3038:     my ($position,$dom,$settings,$rowtotal) = @_;
 3039:     my $numinrow = 4;
 3040:     my $datatable;
 3041:     if ($position eq 'top') {
 3042:         $$rowtotal ++;
 3043:         my $rowcount = 0;
 3044:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3045:         if (ref($rules) eq 'HASH') {
 3046:             if (keys(%{$rules}) > 0) {
 3047:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3048:                                                 $ruleorder,$numinrow,$rowcount);
 3049:                 $$rowtotal ++;
 3050:                 $rowcount ++;
 3051:             }
 3052:         }
 3053:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3054:         if (ref($idrules) eq 'HASH') {
 3055:             if (keys(%{$idrules}) > 0) {
 3056:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3057:                                                 $idruleorder,$numinrow,$rowcount);
 3058:                 $$rowtotal ++;
 3059:                 $rowcount ++;
 3060:             }
 3061:         }
 3062:         my ($emailrules,$emailruleorder) = 
 3063:             &Apache::lonnet::inst_userrules($dom,'email');
 3064:         if (ref($emailrules) eq 'HASH') {
 3065:             if (keys(%{$emailrules}) > 0) {
 3066:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3067:                                                 $emailruleorder,$numinrow,$rowcount);
 3068:                 $$rowtotal ++;
 3069:                 $rowcount ++;
 3070:             }
 3071:         }
 3072:         if ($rowcount == 0) {
 3073:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3074:             $$rowtotal ++;
 3075:             $rowcount ++;
 3076:         }
 3077:     } elsif ($position eq 'middle') {
 3078:         my @creators = ('author','course','requestcrs','selfcreate');
 3079:         my ($rules,$ruleorder) =
 3080:             &Apache::lonnet::inst_userrules($dom,'username');
 3081:         my %lt = &usercreation_types();
 3082:         my %checked;
 3083:         my @selfcreate; 
 3084:         if (ref($settings) eq 'HASH') {
 3085:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3086:                 foreach my $item (@creators) {
 3087:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3088:                 }
 3089:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3090:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3091:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3092:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3093:                         @selfcreate = ('email','login','sso');
 3094:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3095:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3096:                     }
 3097:                 }
 3098:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3099:                 foreach my $item (@creators) {
 3100:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3101:                         $checked{$item} = 'none';
 3102:                     }
 3103:                 }
 3104:             }
 3105:         }
 3106:         my $rownum = 0;
 3107:         foreach my $item (@creators) {
 3108:             $rownum ++;
 3109:             if ($item ne 'selfcreate') {  
 3110:                 if ($checked{$item} eq '') {
 3111:                     $checked{$item} = 'any';
 3112:                 }
 3113:             }
 3114:             my $css_class;
 3115:             if ($rownum%2) {
 3116:                 $css_class = '';
 3117:             } else {
 3118:                 $css_class = ' class="LC_odd_row" ';
 3119:             }
 3120:             $datatable .= '<tr'.$css_class.'>'.
 3121:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3122:                          '</span></td><td align="right">';
 3123:             my @options;
 3124:             if ($item eq 'selfcreate') {
 3125:                 push(@options,('email','login','sso'));
 3126:             } else {
 3127:                 @options = ('any');
 3128:                 if (ref($rules) eq 'HASH') {
 3129:                     if (keys(%{$rules}) > 0) {
 3130:                         push(@options,('official','unofficial'));
 3131:                     }
 3132:                 }
 3133:                 push(@options,'none');
 3134:             }
 3135:             foreach my $option (@options) {
 3136:                 my $type = 'radio';
 3137:                 my $check = ' ';
 3138:                 if ($item eq 'selfcreate') {
 3139:                     $type = 'checkbox';
 3140:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3141:                         $check = ' checked="checked" ';
 3142:                     }
 3143:                 } else {
 3144:                     if ($checked{$item} eq $option) {
 3145:                         $check = ' checked="checked" ';
 3146:                     }
 3147:                 } 
 3148:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3149:                               '<input type="'.$type.'" name="can_createuser_'.
 3150:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3151:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3152:             }
 3153:             $datatable .= '</td></tr>';
 3154:         }
 3155:         my ($othertitle,$usertypes,$types) =
 3156:             &Apache::loncommon::sorted_inst_types($dom);
 3157:         if (ref($usertypes) eq 'HASH') {
 3158:             if (keys(%{$usertypes}) > 0) {
 3159:                 my $createsettings;
 3160:                 if (ref($settings) eq 'HASH') {
 3161:                     $createsettings = $settings->{cancreate};
 3162:                 }
 3163:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3164:                                              $dom,$numinrow,$othertitle,
 3165:                                              'statustocreate');
 3166:                 $$rowtotal ++;
 3167:             }
 3168:         }
 3169:     } else {
 3170:         my @contexts = ('author','course','domain');
 3171:         my @authtypes = ('int','krb4','krb5','loc');
 3172:         my %checked;
 3173:         if (ref($settings) eq 'HASH') {
 3174:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3175:                 foreach my $item (@contexts) {
 3176:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3177:                         foreach my $auth (@authtypes) {
 3178:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3179:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3180:                             }
 3181:                         }
 3182:                     }
 3183:                 }
 3184:             }
 3185:         } else {
 3186:             foreach my $item (@contexts) {
 3187:                 foreach my $auth (@authtypes) {
 3188:                     $checked{$item}{$auth} = ' checked="checked" ';
 3189:                 }
 3190:             }
 3191:         }
 3192:         my %title = &context_names();
 3193:         my %authname = &authtype_names();
 3194:         my $rownum = 0;
 3195:         my $css_class; 
 3196:         foreach my $item (@contexts) {
 3197:             if ($rownum%2) {
 3198:                 $css_class = '';
 3199:             } else {
 3200:                 $css_class = ' class="LC_odd_row" ';
 3201:             }
 3202:             $datatable .=   '<tr'.$css_class.'>'.
 3203:                             '<td>'.$title{$item}.
 3204:                             '</td><td class="LC_left_item">'.
 3205:                             '<span class="LC_nobreak">';
 3206:             foreach my $auth (@authtypes) {
 3207:                 $datatable .= '<label>'. 
 3208:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3209:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3210:                               $authname{$auth}.'</label>&nbsp;';
 3211:             }
 3212:             $datatable .= '</span></td></tr>';
 3213:             $rownum ++;
 3214:         }
 3215:         $$rowtotal += $rownum;
 3216:     }
 3217:     return $datatable;
 3218: }
 3219: 
 3220: sub user_formats_row {
 3221:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3222:     my $output;
 3223:     my %text = (
 3224:                    'username' => 'new usernames',
 3225:                    'id'       => 'IDs',
 3226:                    'email'    => 'self-created accounts (e-mail)',
 3227:                );
 3228:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3229:     $output = '<tr '.$css_class.'>'.
 3230:               '<td><span class="LC_nobreak">';
 3231:     if ($type eq 'email') {
 3232:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3233:     } else {
 3234:         $output .= &mt("Format rules to check for $text{$type}: ");
 3235:     }
 3236:     $output .= '</span></td>'.
 3237:                '<td class="LC_left_item" colspan="2"><table>';
 3238:     my $rem;
 3239:     if (ref($ruleorder) eq 'ARRAY') {
 3240:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3241:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3242:                 my $rem = $i%($numinrow);
 3243:                 if ($rem == 0) {
 3244:                     if ($i > 0) {
 3245:                         $output .= '</tr>';
 3246:                     }
 3247:                     $output .= '<tr>';
 3248:                 }
 3249:                 my $check = ' ';
 3250:                 if (ref($settings) eq 'HASH') {
 3251:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3252:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3253:                             $check = ' checked="checked" ';
 3254:                         }
 3255:                     }
 3256:                 }
 3257:                 $output .= '<td class="LC_left_item">'.
 3258:                            '<span class="LC_nobreak"><label>'.
 3259:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3260:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3261:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3262:             }
 3263:         }
 3264:         $rem = @{$ruleorder}%($numinrow);
 3265:     }
 3266:     my $colsleft = $numinrow - $rem;
 3267:     if ($colsleft > 1 ) {
 3268:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3269:                    '&nbsp;</td>';
 3270:     } elsif ($colsleft == 1) {
 3271:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3272:     }
 3273:     $output .= '</tr></table></td></tr>';
 3274:     return $output;
 3275: }
 3276: 
 3277: sub usercreation_types {
 3278:     my %lt = &Apache::lonlocal::texthash (
 3279:                     author     => 'When adding a co-author',
 3280:                     course     => 'When adding a user to a course',
 3281:                     requestcrs => 'When requesting a course',
 3282:                     selfcreate => 'User creates own account', 
 3283:                     any        => 'Any',
 3284:                     official   => 'Institutional only ',
 3285:                     unofficial => 'Non-institutional only',
 3286:                     email      => 'E-mail address',
 3287:                     login      => 'Institutional Login',
 3288:                     sso        => 'SSO', 
 3289:                     none       => 'None',
 3290:     );
 3291:     return %lt;
 3292: }
 3293: 
 3294: sub authtype_names {
 3295:     my %lt = &Apache::lonlocal::texthash(
 3296:                       int    => 'Internal',
 3297:                       krb4   => 'Kerberos 4',
 3298:                       krb5   => 'Kerberos 5',
 3299:                       loc    => 'Local',
 3300:                   );
 3301:     return %lt;
 3302: }
 3303: 
 3304: sub context_names {
 3305:     my %context_title = &Apache::lonlocal::texthash(
 3306:        author => 'Creating users when an Author',
 3307:        course => 'Creating users when in a course',
 3308:        domain => 'Creating users when a Domain Coordinator',
 3309:     );
 3310:     return %context_title;
 3311: }
 3312: 
 3313: sub print_usermodification {
 3314:     my ($position,$dom,$settings,$rowtotal) = @_;
 3315:     my $numinrow = 4;
 3316:     my ($context,$datatable,$rowcount);
 3317:     if ($position eq 'top') {
 3318:         $rowcount = 0;
 3319:         $context = 'author'; 
 3320:         foreach my $role ('ca','aa') {
 3321:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3322:                                                    $numinrow,$rowcount);
 3323:             $$rowtotal ++;
 3324:             $rowcount ++;
 3325:         }
 3326:     } elsif ($position eq 'middle') {
 3327:         $context = 'course';
 3328:         $rowcount = 0;
 3329:         foreach my $role ('st','ep','ta','in','cr') {
 3330:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3331:                                                    $numinrow,$rowcount);
 3332:             $$rowtotal ++;
 3333:             $rowcount ++;
 3334:         }
 3335:     } elsif ($position eq 'bottom') {
 3336:         $context = 'selfcreate';
 3337:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3338:         $usertypes->{'default'} = $othertitle;
 3339:         if (ref($types) eq 'ARRAY') {
 3340:             push(@{$types},'default');
 3341:             $usertypes->{'default'} = $othertitle;
 3342:             foreach my $status (@{$types}) {
 3343:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3344:                                                        $numinrow,$rowcount,$usertypes);
 3345:                 $$rowtotal ++;
 3346:                 $rowcount ++;
 3347:             }
 3348:         }
 3349:     }
 3350:     return $datatable;
 3351: }
 3352: 
 3353: sub print_defaults {
 3354:     my ($dom,$rowtotal) = @_;
 3355:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3356:                  'datelocale_def','portal_def');
 3357:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3358:     my $titles = &defaults_titles($dom);
 3359:     my $rownum = 0;
 3360:     my ($datatable,$css_class);
 3361:     foreach my $item (@items) {
 3362:         if ($rownum%2) {
 3363:             $css_class = '';
 3364:         } else {
 3365:             $css_class = ' class="LC_odd_row" ';
 3366:         }
 3367:         $datatable .= '<tr'.$css_class.'>'.
 3368:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3369:                   '</span></td><td class="LC_right_item">';
 3370:         if ($item eq 'auth_def') {
 3371:             my @authtypes = ('internal','krb4','krb5','localauth');
 3372:             my %shortauth = (
 3373:                              internal => 'int',
 3374:                              krb4 => 'krb4',
 3375:                              krb5 => 'krb5',
 3376:                              localauth  => 'loc'
 3377:                            );
 3378:             my %authnames = &authtype_names();
 3379:             foreach my $auth (@authtypes) {
 3380:                 my $checked = ' ';
 3381:                 if ($domdefaults{$item} eq $auth) {
 3382:                     $checked = ' checked="checked" ';
 3383:                 }
 3384:                 $datatable .= '<label><input type="radio" name="'.$item.
 3385:                               '" value="'.$auth.'"'.$checked.'/>'.
 3386:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3387:             }
 3388:         } elsif ($item eq 'timezone_def') {
 3389:             my $includeempty = 1;
 3390:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3391:         } elsif ($item eq 'datelocale_def') {
 3392:             my $includeempty = 1;
 3393:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3394:         } else {
 3395:             my $size;
 3396:             if ($item eq 'portal_def') {
 3397:                 $size = ' size="25"';
 3398:             }
 3399:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3400:                           $domdefaults{$item}.'"'.$size.' />';
 3401:         }
 3402:         $datatable .= '</td></tr>';
 3403:         $rownum ++;
 3404:     }
 3405:     $$rowtotal += $rownum;
 3406:     return $datatable;
 3407: }
 3408: 
 3409: sub defaults_titles {
 3410:     my ($dom) = @_;
 3411:     my %titles = &Apache::lonlocal::texthash (
 3412:                    'auth_def'      => 'Default authentication type',
 3413:                    'auth_arg_def'  => 'Default authentication argument',
 3414:                    'lang_def'      => 'Default language',
 3415:                    'timezone_def'  => 'Default timezone',
 3416:                    'datelocale_def' => 'Default locale for dates',
 3417:                    'portal_def'     => 'Portal/Default URL',
 3418:                  );
 3419:     if ($dom) {
 3420:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3421:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3422:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3423:         $protocol = 'http' if ($protocol ne 'https');
 3424:         if ($uint_dom) {
 3425:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3426:                                          $uint_dom);
 3427:         }
 3428:     }
 3429:     return (\%titles);
 3430: }
 3431: 
 3432: sub print_scantronformat {
 3433:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3434:     my $itemcount = 1;
 3435:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3436:         %confhash);
 3437:     my $switchserver = &check_switchserver($dom,$confname);
 3438:     my %lt = &Apache::lonlocal::texthash (
 3439:                 default => 'Default bubblesheet format file error',
 3440:                 custom  => 'Custom bubblesheet format file error',
 3441:              );
 3442:     my %scantronfiles = (
 3443:         default => 'default.tab',
 3444:         custom => 'custom.tab',
 3445:     );
 3446:     foreach my $key (keys(%scantronfiles)) {
 3447:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3448:                               .$scantronfiles{$key};
 3449:     }
 3450:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3451:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3452:         if (!$switchserver) {
 3453:             my $servadm = $r->dir_config('lonAdmEMail');
 3454:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3455:             if ($configuserok eq 'ok') {
 3456:                 if ($author_ok eq 'ok') {
 3457:                     my %legacyfile = (
 3458:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3459:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3460:                     );
 3461:                     my %md5chk;
 3462:                     foreach my $type (keys(%legacyfile)) {
 3463:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3464:                         chomp($md5chk{$type});
 3465:                     }
 3466:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3467:                         foreach my $type (keys(%legacyfile)) {
 3468:                             ($scantronurls{$type},my $error) = 
 3469:                                 &legacy_scantronformat($r,$dom,$confname,
 3470:                                                  $type,$legacyfile{$type},
 3471:                                                  $scantronurls{$type},
 3472:                                                  $scantronfiles{$type});
 3473:                             if ($error ne '') {
 3474:                                 $error{$type} = $error;
 3475:                             }
 3476:                         }
 3477:                         if (keys(%error) == 0) {
 3478:                             $is_custom = 1;
 3479:                             $confhash{'scantron'}{'scantronformat'} = 
 3480:                                 $scantronurls{'custom'};
 3481:                             my $putresult = 
 3482:                                 &Apache::lonnet::put_dom('configuration',
 3483:                                                          \%confhash,$dom);
 3484:                             if ($putresult ne 'ok') {
 3485:                                 $error{'custom'} = 
 3486:                                     '<span class="LC_error">'.
 3487:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3488:                             }
 3489:                         }
 3490:                     } else {
 3491:                         ($scantronurls{'default'},my $error) =
 3492:                             &legacy_scantronformat($r,$dom,$confname,
 3493:                                           'default',$legacyfile{'default'},
 3494:                                           $scantronurls{'default'},
 3495:                                           $scantronfiles{'default'});
 3496:                         if ($error eq '') {
 3497:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3498:                             my $putresult =
 3499:                                 &Apache::lonnet::put_dom('configuration',
 3500:                                                          \%confhash,$dom);
 3501:                             if ($putresult ne 'ok') {
 3502:                                 $error{'default'} =
 3503:                                     '<span class="LC_error">'.
 3504:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3505:                             }
 3506:                         } else {
 3507:                             $error{'default'} = $error;
 3508:                         }
 3509:                     }
 3510:                 }
 3511:             }
 3512:         } else {
 3513:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3514:         }
 3515:     }
 3516:     if (ref($settings) eq 'HASH') {
 3517:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3518:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3519:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3520:                 $scantronurl = '';
 3521:             } else {
 3522:                 $scantronurl = $settings->{'scantronformat'};
 3523:             }
 3524:             $is_custom = 1;
 3525:         } else {
 3526:             $scantronurl = $scantronurls{'default'};
 3527:         }
 3528:     } else {
 3529:         if ($is_custom) {
 3530:             $scantronurl = $scantronurls{'custom'};
 3531:         } else {
 3532:             $scantronurl = $scantronurls{'default'};
 3533:         }
 3534:     }
 3535:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3536:     $datatable .= '<tr'.$css_class.'>';
 3537:     if (!$is_custom) {
 3538:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3539:                       '<span class="LC_nobreak">';
 3540:         if ($scantronurl) {
 3541:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3542:                           &mt('Default bubblesheet format file').'</a>';
 3543:         } else {
 3544:             $datatable = &mt('File unavailable for display');
 3545:         }
 3546:         $datatable .= '</span></td>';
 3547:         if (keys(%error) == 0) { 
 3548:             $datatable .= '<td valign="bottom">';
 3549:             if (!$switchserver) {
 3550:                 $datatable .= &mt('Upload:').'<br />';
 3551:             }
 3552:         } else {
 3553:             my $errorstr;
 3554:             foreach my $key (sort(keys(%error))) {
 3555:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3556:             }
 3557:             $datatable .= '<td>'.$errorstr;
 3558:         }
 3559:     } else {
 3560:         if (keys(%error) > 0) {
 3561:             my $errorstr;
 3562:             foreach my $key (sort(keys(%error))) {
 3563:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3564:             } 
 3565:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3566:         } elsif ($scantronurl) {
 3567:             $datatable .= '<td><span class="LC_nobreak">'.
 3568:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3569:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3570:                           '<input type="checkbox" name="scantronformat_del"'.
 3571:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3572:                           '<td><span class="LC_nobreak">&nbsp;'.
 3573:                           &mt('Replace:').'</span><br />';
 3574:         }
 3575:     }
 3576:     if (keys(%error) == 0) {
 3577:         if ($switchserver) {
 3578:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3579:         } else {
 3580:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3581:                          '<input type="file" name="scantronformat" /></span>';
 3582:         }
 3583:     }
 3584:     $datatable .= '</td></tr>';
 3585:     $$rowtotal ++;
 3586:     return $datatable;
 3587: }
 3588: 
 3589: sub legacy_scantronformat {
 3590:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3591:     my ($url,$error);
 3592:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3593:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3594:         (my $result,$url) =
 3595:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3596:                          '','',$newfile);
 3597:         if ($result ne 'ok') {
 3598:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3599:         }
 3600:     }
 3601:     return ($url,$error);
 3602: }
 3603: 
 3604: sub print_coursecategories {
 3605:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3606:     my $datatable;
 3607:     if ($position eq 'top') {
 3608:         my $toggle_cats_crs = ' ';
 3609:         my $toggle_cats_dom = ' checked="checked" ';
 3610:         my $can_cat_crs = ' ';
 3611:         my $can_cat_dom = ' checked="checked" ';
 3612:         my $toggle_catscomm_comm = ' ';
 3613:         my $toggle_catscomm_dom = ' checked="checked" ';
 3614:         my $can_catcomm_comm = ' ';
 3615:         my $can_catcomm_dom = ' checked="checked" ';
 3616: 
 3617:         if (ref($settings) eq 'HASH') {
 3618:             if ($settings->{'togglecats'} eq 'crs') {
 3619:                 $toggle_cats_crs = $toggle_cats_dom;
 3620:                 $toggle_cats_dom = ' ';
 3621:             }
 3622:             if ($settings->{'categorize'} eq 'crs') {
 3623:                 $can_cat_crs = $can_cat_dom;
 3624:                 $can_cat_dom = ' ';
 3625:             }
 3626:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3627:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3628:                 $toggle_catscomm_dom = ' ';
 3629:             }
 3630:             if ($settings->{'categorizecomm'} eq 'comm') {
 3631:                 $can_catcomm_comm = $can_catcomm_dom;
 3632:                 $can_catcomm_dom = ' ';
 3633:             }
 3634:         }
 3635:         my %title = &Apache::lonlocal::texthash (
 3636:                      togglecats     => 'Show/Hide a course in catalog',
 3637:                      togglecatscomm => 'Show/Hide a community in catalog',
 3638:                      categorize     => 'Assign a category to a course',
 3639:                      categorizecomm => 'Assign a category to a community',
 3640:                     );
 3641:         my %level = &Apache::lonlocal::texthash (
 3642:                      dom  => 'Set in Domain',
 3643:                      crs  => 'Set in Course',
 3644:                      comm => 'Set in Community',
 3645:                     );
 3646:         $datatable = '<tr class="LC_odd_row">'.
 3647:                   '<td>'.$title{'togglecats'}.'</td>'.
 3648:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3649:                   '<input type="radio" name="togglecats"'.
 3650:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3651:                   '<label><input type="radio" name="togglecats"'.
 3652:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3653:                   '</tr><tr>'.
 3654:                   '<td>'.$title{'categorize'}.'</td>'.
 3655:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3656:                   '<label><input type="radio" name="categorize"'.
 3657:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3658:                   '<label><input type="radio" name="categorize"'.
 3659:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3660:                   '</tr><tr class="LC_odd_row">'.
 3661:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 3662:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3663:                   '<input type="radio" name="togglecatscomm"'.
 3664:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3665:                   '<label><input type="radio" name="togglecatscomm"'.
 3666:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3667:                   '</tr><tr>'.
 3668:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 3669:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3670:                   '<label><input type="radio" name="categorizecomm"'.
 3671:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3672:                   '<label><input type="radio" name="categorizecomm"'.
 3673:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3674:                   '</tr>';
 3675:         $$rowtotal += 4;
 3676:     } else {
 3677:         my $css_class;
 3678:         my $itemcount = 1;
 3679:         my $cathash; 
 3680:         if (ref($settings) eq 'HASH') {
 3681:             $cathash = $settings->{'cats'};
 3682:         }
 3683:         if (ref($cathash) eq 'HASH') {
 3684:             my (@cats,@trails,%allitems,%idx,@jsarray);
 3685:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 3686:                                                    \%allitems,\%idx,\@jsarray);
 3687:             my $maxdepth = scalar(@cats);
 3688:             my $colattrib = '';
 3689:             if ($maxdepth > 2) {
 3690:                 $colattrib = ' colspan="2" ';
 3691:             }
 3692:             my @path;
 3693:             if (@cats > 0) {
 3694:                 if (ref($cats[0]) eq 'ARRAY') {
 3695:                     my $numtop = @{$cats[0]};
 3696:                     my $maxnum = $numtop;
 3697:                     my %default_names = (
 3698:                           instcode    => &mt('Official courses'),
 3699:                           communities => &mt('Communities'),
 3700:                     );
 3701: 
 3702:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 3703:                         ($cathash->{'instcode::0'} eq '') ||
 3704:                         (!grep(/^communities$/,@{$cats[0]})) || 
 3705:                         ($cathash->{'communities::0'} eq '')) {
 3706:                         $maxnum ++;
 3707:                     }
 3708:                     my $lastidx;
 3709:                     for (my $i=0; $i<$numtop; $i++) {
 3710:                         my $parent = $cats[0][$i];
 3711:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3712:                         my $item = &escape($parent).'::0';
 3713:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 3714:                         $lastidx = $idx{$item};
 3715:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3716:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 3717:                         for (my $k=0; $k<=$maxnum; $k++) {
 3718:                             my $vpos = $k+1;
 3719:                             my $selstr;
 3720:                             if ($k == $i) {
 3721:                                 $selstr = ' selected="selected" ';
 3722:                             }
 3723:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3724:                         }
 3725:                         $datatable .= '</select></td><td>';
 3726:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 3727:                             $datatable .=  '<span class="LC_nobreak">'
 3728:                                            .$default_names{$parent}.'</span>';
 3729:                             if ($parent eq 'instcode') {
 3730:                                 $datatable .= '<br /><span class="LC_nobreak">('
 3731:                                               .&mt('with institutional codes')
 3732:                                               .')</span></td><td'.$colattrib.'>';
 3733:                             } else {
 3734:                                 $datatable .= '<table><tr><td>';
 3735:                             }
 3736:                             $datatable .= '<span class="LC_nobreak">'
 3737:                                           .'<label><input type="radio" name="'
 3738:                                           .$parent.'" value="1" checked="checked" />'
 3739:                                           .&mt('Display').'</label>';
 3740:                             if ($parent eq 'instcode') {
 3741:                                 $datatable .= '&nbsp;';
 3742:                             } else {
 3743:                                 $datatable .= '</span></td></tr><tr><td>'
 3744:                                               .'<span class="LC_nobreak">';
 3745:                             }
 3746:                             $datatable .= '<label><input type="radio" name="'
 3747:                                           .$parent.'" value="0" />'
 3748:                                           .&mt('Do not display').'</label></span>';
 3749:                             if ($parent eq 'communities') {
 3750:                                 $datatable .= '</td></tr></table>';
 3751:                             }
 3752:                             $datatable .= '</td>';
 3753:                         } else {
 3754:                             $datatable .= $parent
 3755:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 3756:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 3757:                         }
 3758:                         my $depth = 1;
 3759:                         push(@path,$parent);
 3760:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 3761:                         pop(@path);
 3762:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 3763:                         $itemcount ++;
 3764:                     }
 3765:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3766:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 3767:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 3768:                     for (my $k=0; $k<=$maxnum; $k++) {
 3769:                         my $vpos = $k+1;
 3770:                         my $selstr;
 3771:                         if ($k == $numtop) {
 3772:                             $selstr = ' selected="selected" ';
 3773:                         }
 3774:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3775:                     }
 3776:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 3777:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 3778:                                   .'</tr>'."\n";
 3779:                     $itemcount ++;
 3780:                     foreach my $default ('instcode','communities') {
 3781:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 3782:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3783:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 3784:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 3785:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 3786:                             for (my $k=0; $k<=$maxnum; $k++) {
 3787:                                 my $vpos = $k+1;
 3788:                                 my $selstr;
 3789:                                 if ($k == $maxnum) {
 3790:                                     $selstr = ' selected="selected" ';
 3791:                                 }
 3792:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3793:                             }
 3794:                             $datatable .= '</select></span></td>'.
 3795:                                           '<td><span class="LC_nobreak">'.
 3796:                                           $default_names{$default}.'</span>';
 3797:                             if ($default eq 'instcode') {
 3798:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 3799:                                               .&mt('with institutional codes').')</span>';
 3800:                             }
 3801:                             $datatable .= '</td>'
 3802:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 3803:                                           .&mt('Display').'</label>&nbsp;'
 3804:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 3805:                                           .&mt('Do not display').'</label></span></td></tr>';
 3806:                         }
 3807:                     }
 3808:                 }
 3809:             } else {
 3810:                 $datatable .= &initialize_categories($itemcount);
 3811:             }
 3812:         } else {
 3813:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 3814:                           .&initialize_categories($itemcount);
 3815:         }
 3816:         $$rowtotal += $itemcount;
 3817:     }
 3818:     return $datatable;
 3819: }
 3820: 
 3821: sub print_serverstatuses {
 3822:     my ($dom,$settings,$rowtotal) = @_;
 3823:     my $datatable;
 3824:     my @pages = &serverstatus_pages();
 3825:     my (%namedaccess,%machineaccess);
 3826:     foreach my $type (@pages) {
 3827:         $namedaccess{$type} = '';
 3828:         $machineaccess{$type}= '';
 3829:     }
 3830:     if (ref($settings) eq 'HASH') {
 3831:         foreach my $type (@pages) {
 3832:             if (exists($settings->{$type})) {
 3833:                 if (ref($settings->{$type}) eq 'HASH') {
 3834:                     foreach my $key (keys(%{$settings->{$type}})) {
 3835:                         if ($key eq 'namedusers') {
 3836:                             $namedaccess{$type} = $settings->{$type}->{$key};
 3837:                         } elsif ($key eq 'machines') {
 3838:                             $machineaccess{$type} = $settings->{$type}->{$key};
 3839:                         }
 3840:                     }
 3841:                 }
 3842:             }
 3843:         }
 3844:     }
 3845:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 3846:     my $rownum = 0;
 3847:     my $css_class;
 3848:     foreach my $type (@pages) {
 3849:         $rownum ++;
 3850:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3851:         $datatable .= '<tr'.$css_class.'>'.
 3852:                       '<td><span class="LC_nobreak">'.
 3853:                       $titles->{$type}.'</span></td>'.
 3854:                       '<td class="LC_left_item">'.
 3855:                       '<input type="text" name="'.$type.'_namedusers" '.
 3856:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 3857:                       '<td class="LC_right_item">'.
 3858:                       '<span class="LC_nobreak">'.
 3859:                       '<input type="text" name="'.$type.'_machines" '.
 3860:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 3861:                       '</td></tr>'."\n";
 3862:     }
 3863:     $$rowtotal += $rownum;
 3864:     return $datatable;
 3865: }
 3866: 
 3867: sub serverstatus_pages {
 3868:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 3869:             'clusterstatus','metadata_keywords','metadata_harvest',
 3870:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 3871: }
 3872: 
 3873: sub coursecategories_javascript {
 3874:     my ($settings) = @_;
 3875:     my ($output,$jstext,$cathash);
 3876:     if (ref($settings) eq 'HASH') {
 3877:         $cathash = $settings->{'cats'};
 3878:     }
 3879:     if (ref($cathash) eq 'HASH') {
 3880:         my (@cats,@jsarray,%idx);
 3881:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 3882:         if (@jsarray > 0) {
 3883:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 3884:             for (my $i=0; $i<@jsarray; $i++) {
 3885:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 3886:                     my $catstr = join('","',@{$jsarray[$i]});
 3887:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 3888:                 }
 3889:             }
 3890:         }
 3891:     } else {
 3892:         $jstext  = '    var categories = Array(1);'."\n".
 3893:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 3894:     }
 3895:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 3896:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 3897:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 3898:     $output = <<"ENDSCRIPT";
 3899: <script type="text/javascript">
 3900: // <![CDATA[
 3901: function reorderCats(form,parent,item,idx) {
 3902:     var changedVal;
 3903: $jstext
 3904:     var newpos = 'addcategory_pos';
 3905:     var current = new Array;
 3906:     if (parent == '') {
 3907:         var has_instcode = 0;
 3908:         var maxtop = categories[idx].length;
 3909:         for (var j=0; j<maxtop; j++) {
 3910:             if (categories[idx][j] == 'instcode::0') {
 3911:                 has_instcode == 1;
 3912:             }
 3913:         }
 3914:         if (has_instcode == 0) {
 3915:             categories[idx][maxtop] = 'instcode_pos';
 3916:         }
 3917:     } else {
 3918:         newpos += '_'+parent;
 3919:     }
 3920:     var maxh = 1 + categories[idx].length;
 3921:     var current = new Array;
 3922:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3923:     if (item == newpos) {
 3924:         changedVal = newitemVal;
 3925:     } else {
 3926:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3927:         current[newitemVal] = newpos;
 3928:     }
 3929:     for (var i=0; i<categories[idx].length; i++) {
 3930:         var elementName = categories[idx][i];
 3931:         if (elementName != item) {
 3932:             if (form.elements[elementName]) {
 3933:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3934:                 current[currVal] = elementName;
 3935:             }
 3936:         }
 3937:     }
 3938:     var oldVal;
 3939:     for (var j=0; j<maxh; j++) {
 3940:         if (current[j] == undefined) {
 3941:             oldVal = j;
 3942:         }
 3943:     }
 3944:     if (oldVal < changedVal) {
 3945:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3946:            var elementName = current[k];
 3947:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3948:         }
 3949:     } else {
 3950:         for (var k=changedVal; k<oldVal; k++) {
 3951:             var elementName = current[k];
 3952:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3953:         }
 3954:     }
 3955:     return;
 3956: }
 3957: 
 3958: function categoryCheck(form) {
 3959:     if (form.elements['addcategory_name'].value == 'instcode') {
 3960:         alert('$instcode_reserved\\n$choose_again');
 3961:         return false;
 3962:     }
 3963:     if (form.elements['addcategory_name'].value == 'communities') {
 3964:         alert('$communities_reserved\\n$choose_again');
 3965:         return false;
 3966:     }
 3967:     return true;
 3968: }
 3969: 
 3970: // ]]>
 3971: </script>
 3972: 
 3973: ENDSCRIPT
 3974:     return $output;
 3975: }
 3976: 
 3977: sub initialize_categories {
 3978:     my ($itemcount) = @_;
 3979:     my ($datatable,$css_class,$chgstr);
 3980:     my %default_names = (
 3981:                       instcode    => 'Official courses (with institutional codes)',
 3982:                       communities => 'Communities',
 3983:                         );
 3984:     my $select0 = ' selected="selected"';
 3985:     my $select1 = '';
 3986:     foreach my $default ('instcode','communities') {
 3987:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3988:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 3989:         if ($default eq 'communities') {
 3990:             $select1 = $select0;
 3991:             $select0 = '';
 3992:         }
 3993:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3994:                      .'<select name="'.$default.'_pos">'
 3995:                      .'<option value="0"'.$select0.'>1</option>'
 3996:                      .'<option value="1"'.$select1.'>2</option>'
 3997:                      .'<option value="2">3</option></select>&nbsp;'
 3998:                      .$default_names{$default}
 3999:                      .'</span></td><td><span class="LC_nobreak">'
 4000:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4001:                      .&mt('Display').'</label>&nbsp;<label>'
 4002:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4003:                  .'</label></span></td></tr>';
 4004:         $itemcount ++;
 4005:     }
 4006:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4007:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4008:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4009:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4010:                   .'<option value="0">1</option>'
 4011:                   .'<option value="1">2</option>'
 4012:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4013:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4014:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4015:     return $datatable;
 4016: }
 4017: 
 4018: sub build_category_rows {
 4019:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4020:     my ($text,$name,$item,$chgstr);
 4021:     if (ref($cats) eq 'ARRAY') {
 4022:         my $maxdepth = scalar(@{$cats});
 4023:         if (ref($cats->[$depth]) eq 'HASH') {
 4024:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4025:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4026:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4027:                 $text .= '<td><table class="LC_datatable">';
 4028:                 my ($idxnum,$parent_name,$parent_item);
 4029:                 my $higher = $depth - 1;
 4030:                 if ($higher == 0) {
 4031:                     $parent_name = &escape($parent).'::'.$higher;
 4032:                 } else {
 4033:                     if (ref($path) eq 'ARRAY') {
 4034:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4035:                     }
 4036:                 }
 4037:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4038:                 for (my $j=0; $j<=$numchildren; $j++) {
 4039:                     if ($j < $numchildren) {
 4040:                         $name = $cats->[$depth]{$parent}[$j];
 4041:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4042:                         $idxnum = $idx->{$item};
 4043:                     } else {
 4044:                         $name = $parent_name;
 4045:                         $item = $parent_item;
 4046:                     }
 4047:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4048:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4049:                     for (my $i=0; $i<=$numchildren; $i++) {
 4050:                         my $vpos = $i+1;
 4051:                         my $selstr;
 4052:                         if ($j == $i) {
 4053:                             $selstr = ' selected="selected" ';
 4054:                         }
 4055:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4056:                     }
 4057:                     $text .= '</select>&nbsp;';
 4058:                     if ($j < $numchildren) {
 4059:                         my $deeper = $depth+1;
 4060:                         $text .= $name.'&nbsp;'
 4061:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4062:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4063:                         if(ref($path) eq 'ARRAY') {
 4064:                             push(@{$path},$name);
 4065:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4066:                             pop(@{$path});
 4067:                         }
 4068:                     } else {
 4069:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4070:                         if ($j == $numchildren) {
 4071:                             $text .= $name;
 4072:                         } else {
 4073:                             $text .= $item;
 4074:                         }
 4075:                         $text .= '" value="" />';
 4076:                     }
 4077:                     $text .= '</td></tr>';
 4078:                 }
 4079:                 $text .= '</table></td>';
 4080:             } else {
 4081:                 my $higher = $depth-1;
 4082:                 if ($higher == 0) {
 4083:                     $name = &escape($parent).'::'.$higher;
 4084:                 } else {
 4085:                     if (ref($path) eq 'ARRAY') {
 4086:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4087:                     }
 4088:                 }
 4089:                 my $colspan;
 4090:                 if ($parent ne 'instcode') {
 4091:                     $colspan = $maxdepth - $depth - 1;
 4092:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4093:                 }
 4094:             }
 4095:         }
 4096:     }
 4097:     return $text;
 4098: }
 4099: 
 4100: sub modifiable_userdata_row {
 4101:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4102:     my $rolename;
 4103:     if ($context eq 'selfcreate') {
 4104:         if (ref($usertypes) eq 'HASH') {
 4105:             $rolename = $usertypes->{$role};
 4106:         } else {
 4107:             $rolename = $role;
 4108:         }
 4109:     } else {
 4110:         if ($role eq 'cr') {
 4111:             $rolename = &mt('Custom role');
 4112:         } else {
 4113:             $rolename = &Apache::lonnet::plaintext($role);
 4114:         }
 4115:     }
 4116:     my @fields = ('lastname','firstname','middlename','generation',
 4117:                   'permanentemail','id');
 4118:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4119:     my $output;
 4120:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4121:     $output = '<tr '.$css_class.'>'.
 4122:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4123:               '<td class="LC_left_item" colspan="2"><table>';
 4124:     my $rem;
 4125:     my %checks;
 4126:     if (ref($settings) eq 'HASH') {
 4127:         if (ref($settings->{$context}) eq 'HASH') {
 4128:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4129:                 foreach my $field (@fields) {
 4130:                     if ($settings->{$context}->{$role}->{$field}) {
 4131:                         $checks{$field} = ' checked="checked" ';
 4132:                     }
 4133:                 }
 4134:             }
 4135:         }
 4136:     }
 4137:     for (my $i=0; $i<@fields; $i++) {
 4138:         my $rem = $i%($numinrow);
 4139:         if ($rem == 0) {
 4140:             if ($i > 0) {
 4141:                 $output .= '</tr>';
 4142:             }
 4143:             $output .= '<tr>';
 4144:         }
 4145:         my $check = ' ';
 4146:         if (exists($checks{$fields[$i]})) {
 4147:             $check = $checks{$fields[$i]}
 4148:         } else {
 4149:             if ($role eq 'st') {
 4150:                 if (ref($settings) ne 'HASH') {
 4151:                     $check = ' checked="checked" '; 
 4152:                 }
 4153:             }
 4154:         }
 4155:         $output .= '<td class="LC_left_item">'.
 4156:                    '<span class="LC_nobreak"><label>'.
 4157:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4158:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4159:                    '</label></span></td>';
 4160:         $rem = @fields%($numinrow);
 4161:     }
 4162:     my $colsleft = $numinrow - $rem;
 4163:     if ($colsleft > 1 ) {
 4164:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4165:                    '&nbsp;</td>';
 4166:     } elsif ($colsleft == 1) {
 4167:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4168:     }
 4169:     $output .= '</tr></table></td></tr>';
 4170:     return $output;
 4171: }
 4172: 
 4173: sub insttypes_row {
 4174:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4175:     my %lt = &Apache::lonlocal::texthash (
 4176:                       cansearch => 'Users allowed to search',
 4177:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4178:                       lockablenames => 'User preference to lock name',
 4179:              );
 4180:     my $showdom;
 4181:     if ($context eq 'cansearch') {
 4182:         $showdom = ' ('.$dom.')';
 4183:     }
 4184:     my $output =  '<tr class="LC_odd_row">'.
 4185:                   '<td>'.$lt{$context}.$showdom.
 4186:                   '</td><td class="LC_left_item" colspan="2"><table>';
 4187:     my $rem;
 4188:     if (ref($types) eq 'ARRAY') {
 4189:         for (my $i=0; $i<@{$types}; $i++) {
 4190:             if (defined($usertypes->{$types->[$i]})) {
 4191:                 my $rem = $i%($numinrow);
 4192:                 if ($rem == 0) {
 4193:                     if ($i > 0) {
 4194:                         $output .= '</tr>';
 4195:                     }
 4196:                     $output .= '<tr>';
 4197:                 }
 4198:                 my $check = ' ';
 4199:                 if (ref($settings) eq 'HASH') {
 4200:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4201:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4202:                             $check = ' checked="checked" ';
 4203:                         }
 4204:                     } elsif ($context eq 'statustocreate') {
 4205:                         $check = ' checked="checked" ';
 4206:                     }
 4207:                 }
 4208:                 $output .= '<td class="LC_left_item">'.
 4209:                            '<span class="LC_nobreak"><label>'.
 4210:                            '<input type="checkbox" name="'.$context.'" '.
 4211:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4212:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4213:             }
 4214:         }
 4215:         $rem = @{$types}%($numinrow);
 4216:     }
 4217:     my $colsleft = $numinrow - $rem;
 4218:     if (($rem == 0) && (@{$types} > 0)) {
 4219:         $output .= '<tr>';
 4220:     }
 4221:     if ($colsleft > 1) {
 4222:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4223:     } else {
 4224:         $output .= '<td class="LC_left_item">';
 4225:     }
 4226:     my $defcheck = ' ';
 4227:     if (ref($settings) eq 'HASH') {  
 4228:         if (ref($settings->{$context}) eq 'ARRAY') {
 4229:             if (grep(/^default$/,@{$settings->{$context}})) {
 4230:                 $defcheck = ' checked="checked" ';
 4231:             }
 4232:         } elsif ($context eq 'statustocreate') {
 4233:             $defcheck = ' checked="checked" ';
 4234:         }
 4235:     }
 4236:     $output .= '<span class="LC_nobreak"><label>'.
 4237:                '<input type="checkbox" name="'.$context.'" '.
 4238:                'value="default"'.$defcheck.'/>'.
 4239:                $othertitle.'</label></span></td>'.
 4240:                '</tr></table></td></tr>';
 4241:     return $output;
 4242: }
 4243: 
 4244: sub sorted_searchtitles {
 4245:     my %searchtitles = &Apache::lonlocal::texthash(
 4246:                          'uname' => 'username',
 4247:                          'lastname' => 'last name',
 4248:                          'lastfirst' => 'last name, first name',
 4249:                      );
 4250:     my @titleorder = ('uname','lastname','lastfirst');
 4251:     return (\%searchtitles,\@titleorder);
 4252: }
 4253: 
 4254: sub sorted_searchtypes {
 4255:     my %srchtypes_desc = (
 4256:                            exact    => 'is exact match',
 4257:                            contains => 'contains ..',
 4258:                            begins   => 'begins with ..',
 4259:                          );
 4260:     my @srchtypeorder = ('exact','begins','contains');
 4261:     return (\%srchtypes_desc,\@srchtypeorder);
 4262: }
 4263: 
 4264: sub usertype_update_row {
 4265:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4266:     my $datatable;
 4267:     my $numinrow = 4;
 4268:     foreach my $type (@{$types}) {
 4269:         if (defined($usertypes->{$type})) {
 4270:             $$rownums ++;
 4271:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4272:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4273:                           '</td><td class="LC_left_item"><table>';
 4274:             for (my $i=0; $i<@{$fields}; $i++) {
 4275:                 my $rem = $i%($numinrow);
 4276:                 if ($rem == 0) {
 4277:                     if ($i > 0) {
 4278:                         $datatable .= '</tr>';
 4279:                     }
 4280:                     $datatable .= '<tr>';
 4281:                 }
 4282:                 my $check = ' ';
 4283:                 if (ref($settings) eq 'HASH') {
 4284:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4285:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4286:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4287:                                 $check = ' checked="checked" ';
 4288:                             }
 4289:                         }
 4290:                     }
 4291:                 }
 4292: 
 4293:                 if ($i == @{$fields}-1) {
 4294:                     my $colsleft = $numinrow - $rem;
 4295:                     if ($colsleft > 1) {
 4296:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4297:                     } else {
 4298:                         $datatable .= '<td>';
 4299:                     }
 4300:                 } else {
 4301:                     $datatable .= '<td>';
 4302:                 }
 4303:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4304:                               '<input type="checkbox" name="updateable_'.$type.
 4305:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4306:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4307:             }
 4308:             $datatable .= '</tr></table></td></tr>';
 4309:         }
 4310:     }
 4311:     return $datatable;
 4312: }
 4313: 
 4314: sub modify_login {
 4315:     my ($r,$dom,$confname,%domconfig) = @_;
 4316:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges);
 4317:     my %title = ( coursecatalog => 'Display course catalog',
 4318:                   adminmail => 'Display administrator E-mail address',
 4319:                   newuser => 'Link for visitors to create a user account',
 4320:                   loginheader => 'Log-in box header');
 4321:     my @offon = ('off','on');
 4322:     my %curr_loginvia;
 4323:     if (ref($domconfig{login}) eq 'HASH') {
 4324:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4325:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4326:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4327:             }
 4328:         }
 4329:     }
 4330:     my %loginhash;
 4331:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4332:                                            \%domconfig,\%loginhash);
 4333:     my @toggles = ('coursecatalog','adminmail','newuser');
 4334:     foreach my $item (@toggles) {
 4335:         $loginhash{login}{$item} = $env{'form.'.$item};
 4336:     }
 4337:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4338:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4339:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4340:                                          \%loginhash);
 4341:     }
 4342: 
 4343:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4344:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4345:     if (keys(%servers) > 1) {
 4346:         foreach my $lonhost (keys(%servers)) {
 4347:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4348:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4349:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4350:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4351:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4352:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4353:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4354:                         $changes{'loginvia'}{$lonhost} = 1;
 4355:                     } else {
 4356:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4357:                         $changes{'loginvia'}{$lonhost} = 1;
 4358:                     }
 4359:                 } else {
 4360:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4361:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4362:                         $changes{'loginvia'}{$lonhost} = 1;
 4363:                     }
 4364:                 }
 4365:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4366:                     foreach my $item (@loginvia_attribs) {
 4367:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4368:                     }
 4369:                 } else {
 4370:                     foreach my $item (@loginvia_attribs) {
 4371:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4372:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4373:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4374:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4375:                                 $new = '/';
 4376:                             }
 4377:                         }
 4378:                         if (($item eq 'custompath') && 
 4379:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4380:                             $new = '';
 4381:                         }
 4382:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4383:                             $changes{'loginvia'}{$lonhost} = 1;
 4384:                         }
 4385:                         if ($item eq 'exempt') {
 4386:                             $new =~ s/^\s+//;
 4387:                             $new =~ s/\s+$//;
 4388:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4389:                             my @okips;
 4390:                             foreach my $ip (@poss_ips) {
 4391:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4392:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4393:                                         push(@okips,$ip); 
 4394:                                     }
 4395:                                 }
 4396:                             }
 4397:                             if (@okips > 0) {
 4398:                                 $new = join(',',@okips); 
 4399:                             } else {
 4400:                                 $new = ''; 
 4401:                             }
 4402:                         }
 4403: 
 4404:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4405:                     }
 4406:                 }
 4407:             } else {
 4408:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4409:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4410:                     $changes{'loginvia'}{$lonhost} = 1;
 4411:                     foreach my $item (@loginvia_attribs) {
 4412:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4413:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4414:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4415:                                 $new = '/';
 4416:                             }
 4417:                         }
 4418:                         if (($item eq 'custompath') && 
 4419:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4420:                             $new = '';
 4421:                         }
 4422:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4423:                     }
 4424:                 }
 4425:             }
 4426:         }
 4427:     }
 4428: 
 4429:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4430:                                              $dom);
 4431:     if ($putresult eq 'ok') {
 4432:         my @toggles = ('coursecatalog','adminmail','newuser');
 4433:         my %defaultchecked = (
 4434:                     'coursecatalog' => 'on',
 4435:                     'adminmail'     => 'off',
 4436:                     'newuser'       => 'off',
 4437:         );
 4438:         if (ref($domconfig{'login'}) eq 'HASH') {
 4439:             foreach my $item (@toggles) {
 4440:                 if ($defaultchecked{$item} eq 'on') { 
 4441:                     if (($domconfig{'login'}{$item} eq '0') &&
 4442:                         ($env{'form.'.$item} eq '1')) {
 4443:                         $changes{$item} = 1;
 4444:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4445:                               $domconfig{'login'}{$item} eq '1') &&
 4446:                              ($env{'form.'.$item} eq '0')) {
 4447:                         $changes{$item} = 1;
 4448:                     }
 4449:                 } elsif ($defaultchecked{$item} eq 'off') {
 4450:                     if (($domconfig{'login'}{$item} eq '1') &&
 4451:                         ($env{'form.'.$item} eq '0')) {
 4452:                         $changes{$item} = 1;
 4453:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4454:                               $domconfig{'login'}{$item} eq '0') &&
 4455:                              ($env{'form.'.$item} eq '1')) {
 4456:                         $changes{$item} = 1;
 4457:                     }
 4458:                 }
 4459:             }
 4460:         }
 4461:         if (keys(%changes) > 0 || $colchgtext) {
 4462:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4463:             $resulttext = &mt('Changes made:').'<ul>';
 4464:             foreach my $item (sort(keys(%changes))) {
 4465:                 if ($item eq 'loginvia') {
 4466:                     if (ref($changes{$item}) eq 'HASH') {
 4467:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4468:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4469:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4470:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4471:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4472:                                     $protocol = 'http' if ($protocol ne 'https');
 4473:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4474: 
 4475:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4476:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4477:                                     } else {
 4478:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4479:                                     }
 4480:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4481:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4482:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4483:                                     }
 4484:                                     $resulttext .= '</li>';
 4485:                                 } else {
 4486:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4487:                                 }
 4488:                             } else {
 4489:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4490:                             }
 4491:                         }
 4492:                         $resulttext .= '</ul></li>';
 4493:                     }
 4494:                 } else {
 4495:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4496:                 }
 4497:             }
 4498:             $resulttext .= $colchgtext.'</ul>';
 4499:         } else {
 4500:             $resulttext = &mt('No changes made to log-in page settings');
 4501:         }
 4502:     } else {
 4503:         $resulttext = '<span class="LC_error">'.
 4504: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4505:     }
 4506:     if ($errors) {
 4507:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 4508:                        $errors.'</ul>';
 4509:     }
 4510:     return $resulttext;
 4511: }
 4512: 
 4513: sub color_font_choices {
 4514:     my %choices =
 4515:         &Apache::lonlocal::texthash (
 4516:             img => "Header",
 4517:             bgs => "Background colors",
 4518:             links => "Link colors",
 4519:             images => "Images",
 4520:             font => "Font color",
 4521:             fontmenu => "Font Menu",
 4522:             pgbg => "Page",
 4523:             tabbg => "Header",
 4524:             sidebg => "Border",
 4525:             link => "Link",
 4526:             alink => "Active link",
 4527:             vlink => "Visited link",
 4528:         );
 4529:     return %choices;
 4530: }
 4531: 
 4532: sub modify_rolecolors {
 4533:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 4534:     my ($resulttext,%rolehash);
 4535:     $rolehash{'rolecolors'} = {};
 4536:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 4537:         if ($domconfig{'rolecolors'} eq '') {
 4538:             $domconfig{'rolecolors'} = {};
 4539:         }
 4540:     }
 4541:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 4542:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 4543:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 4544:                                              $dom);
 4545:     if ($putresult eq 'ok') {
 4546:         if (keys(%changes) > 0) {
 4547:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4548:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 4549:                                              $rolehash{'rolecolors'});
 4550:         } else {
 4551:             $resulttext = &mt('No changes made to default color schemes');
 4552:         }
 4553:     } else {
 4554:         $resulttext = '<span class="LC_error">'.
 4555: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4556:     }
 4557:     if ($errors) {
 4558:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 4559:                        $errors.'</ul>';
 4560:     }
 4561:     return $resulttext;
 4562: }
 4563: 
 4564: sub modify_colors {
 4565:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 4566:     my (%changes,%choices);
 4567:     my @bgs;
 4568:     my @links = ('link','alink','vlink');
 4569:     my @logintext;
 4570:     my @images;
 4571:     my $servadm = $r->dir_config('lonAdmEMail');
 4572:     my $errors;
 4573:     foreach my $role (@{$roles}) {
 4574:         if ($role eq 'login') {
 4575:             %choices = &login_choices();
 4576:             @logintext = ('textcol','bgcol');
 4577:         } else {
 4578:             %choices = &color_font_choices();
 4579:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 4580:         }
 4581:         if ($role eq 'login') {
 4582:             @images = ('img','logo','domlogo','login');
 4583:             @bgs = ('pgbg','mainbg','sidebg');
 4584:         } else {
 4585:             @images = ('img');
 4586:             @bgs = ('pgbg','tabbg','sidebg'); 
 4587:         }
 4588:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 4589:         foreach my $item (@bgs,@links,@logintext) {
 4590:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 4591:         }
 4592:         my ($configuserok,$author_ok,$switchserver) = 
 4593:             &config_check($dom,$confname,$servadm);
 4594:         my ($width,$height) = &thumb_dimensions();
 4595:         if (ref($domconfig->{$role}) ne 'HASH') {
 4596:             $domconfig->{$role} = {};
 4597:         }
 4598:         foreach my $img (@images) {
 4599:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 4600:                 if (defined($env{'form.login_showlogo_'.$img})) {
 4601:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 4602:                 } else { 
 4603:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 4604:                 }
 4605:             } 
 4606: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 4607: 		 && !defined($domconfig->{$role}{$img})
 4608: 		 && !$env{'form.'.$role.'_del_'.$img}
 4609: 		 && $env{'form.'.$role.'_import_'.$img}) {
 4610: 		# import the old configured image from the .tab setting
 4611: 		# if they haven't provided a new one 
 4612: 		$domconfig->{$role}{$img} = 
 4613: 		    $env{'form.'.$role.'_import_'.$img};
 4614: 	    }
 4615:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 4616:                 my $error;
 4617:                 if ($configuserok eq 'ok') {
 4618:                     if ($switchserver) {
 4619:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 4620:                     } else {
 4621:                         if ($author_ok eq 'ok') {
 4622:                             my ($result,$logourl) = 
 4623:                                 &publishlogo($r,'upload',$role.'_'.$img,
 4624:                                            $dom,$confname,$img,$width,$height);
 4625:                             if ($result eq 'ok') {
 4626:                                 $confhash->{$role}{$img} = $logourl;
 4627:                                 $changes{$role}{'images'}{$img} = 1;
 4628:                             } else {
 4629:                                 $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);
 4630:                             }
 4631:                         } else {
 4632:                             $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);
 4633:                         }
 4634:                     }
 4635:                 } else {
 4636:                     $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);
 4637:                 }
 4638:                 if ($error) {
 4639:                     &Apache::lonnet::logthis($error);
 4640:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4641:                 }
 4642:             } elsif ($domconfig->{$role}{$img} ne '') {
 4643:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 4644:                     my $error;
 4645:                     if ($configuserok eq 'ok') {
 4646: # is confname an author?
 4647:                         if ($switchserver eq '') {
 4648:                             if ($author_ok eq 'ok') {
 4649:                                 my ($result,$logourl) = 
 4650:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 4651:                                             $dom,$confname,$img,$width,$height);
 4652:                                 if ($result eq 'ok') {
 4653:                                     $confhash->{$role}{$img} = $logourl;
 4654: 				    $changes{$role}{'images'}{$img} = 1;
 4655:                                 }
 4656:                             }
 4657:                         }
 4658:                     }
 4659:                 }
 4660:             }
 4661:         }
 4662:         if (ref($domconfig) eq 'HASH') {
 4663:             if (ref($domconfig->{$role}) eq 'HASH') {
 4664:                 foreach my $img (@images) {
 4665:                     if ($domconfig->{$role}{$img} ne '') {
 4666:                         if ($env{'form.'.$role.'_del_'.$img}) {
 4667:                             $confhash->{$role}{$img} = '';
 4668:                             $changes{$role}{'images'}{$img} = 1;
 4669:                         } else {
 4670:                             if ($confhash->{$role}{$img} eq '') {
 4671:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 4672:                             }
 4673:                         }
 4674:                     } else {
 4675:                         if ($env{'form.'.$role.'_del_'.$img}) {
 4676:                             $confhash->{$role}{$img} = '';
 4677:                             $changes{$role}{'images'}{$img} = 1;
 4678:                         } 
 4679:                     }
 4680:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 4681:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 4682:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 4683:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 4684:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 4685:                             }
 4686:                         } else {
 4687:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 4688:                                 $changes{$role}{'showlogo'}{$img} = 1;
 4689:                             }
 4690:                         }
 4691:                     }
 4692:                 }
 4693:                 if ($domconfig->{$role}{'font'} ne '') {
 4694:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 4695:                         $changes{$role}{'font'} = 1;
 4696:                     }
 4697:                 } else {
 4698:                     if ($confhash->{$role}{'font'}) {
 4699:                         $changes{$role}{'font'} = 1;
 4700:                     }
 4701:                 }
 4702:                 if ($role ne 'login') {
 4703:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 4704:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 4705:                             $changes{$role}{'fontmenu'} = 1;
 4706:                         }
 4707:                     } else {
 4708:                         if ($confhash->{$role}{'fontmenu'}) {
 4709:                             $changes{$role}{'fontmenu'} = 1;
 4710:                         }
 4711:                     }
 4712:                 }
 4713:                 foreach my $item (@bgs) {
 4714:                     if ($domconfig->{$role}{$item} ne '') {
 4715:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 4716:                             $changes{$role}{'bgs'}{$item} = 1;
 4717:                         } 
 4718:                     } else {
 4719:                         if ($confhash->{$role}{$item}) {
 4720:                             $changes{$role}{'bgs'}{$item} = 1;
 4721:                         }
 4722:                     }
 4723:                 }
 4724:                 foreach my $item (@links) {
 4725:                     if ($domconfig->{$role}{$item} ne '') {
 4726:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 4727:                             $changes{$role}{'links'}{$item} = 1;
 4728:                         }
 4729:                     } else {
 4730:                         if ($confhash->{$role}{$item}) {
 4731:                             $changes{$role}{'links'}{$item} = 1;
 4732:                         }
 4733:                     }
 4734:                 }
 4735:                 foreach my $item (@logintext) {
 4736:                     if ($domconfig->{$role}{$item} ne '') {
 4737:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 4738:                             $changes{$role}{'logintext'}{$item} = 1;
 4739:                         }
 4740:                     } else {
 4741:                         if ($confhash->{$role}{$item}) {
 4742:                             $changes{$role}{'logintext'}{$item} = 1;
 4743:                         }
 4744:                     }
 4745:                 }
 4746:             } else {
 4747:                 &default_change_checker($role,\@images,\@links,\@bgs,
 4748:                                         \@logintext,$confhash,\%changes); 
 4749:             }
 4750:         } else {
 4751:             &default_change_checker($role,\@images,\@links,\@bgs,
 4752:                                     \@logintext,$confhash,\%changes); 
 4753:         }
 4754:     }
 4755:     return ($errors,%changes);
 4756: }
 4757: 
 4758: sub config_check {
 4759:     my ($dom,$confname,$servadm) = @_;
 4760:     my ($configuserok,$author_ok,$switchserver,%currroles);
 4761:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 4762:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 4763:                                                    $confname,$servadm);
 4764:     if ($configuserok eq 'ok') {
 4765:         $switchserver = &check_switchserver($dom,$confname);
 4766:         if ($switchserver eq '') {
 4767:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 4768:         }
 4769:     }
 4770:     return ($configuserok,$author_ok,$switchserver);
 4771: }
 4772: 
 4773: sub default_change_checker {
 4774:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 4775:     foreach my $item (@{$links}) {
 4776:         if ($confhash->{$role}{$item}) {
 4777:             $changes->{$role}{'links'}{$item} = 1;
 4778:         }
 4779:     }
 4780:     foreach my $item (@{$bgs}) {
 4781:         if ($confhash->{$role}{$item}) {
 4782:             $changes->{$role}{'bgs'}{$item} = 1;
 4783:         }
 4784:     }
 4785:     foreach my $item (@{$logintext}) {
 4786:         if ($confhash->{$role}{$item}) {
 4787:             $changes->{$role}{'logintext'}{$item} = 1;
 4788:         }
 4789:     }
 4790:     foreach my $img (@{$images}) {
 4791:         if ($env{'form.'.$role.'_del_'.$img}) {
 4792:             $confhash->{$role}{$img} = '';
 4793:             $changes->{$role}{'images'}{$img} = 1;
 4794:         }
 4795:         if ($role eq 'login') {
 4796:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 4797:                 $changes->{$role}{'showlogo'}{$img} = 1;
 4798:             }
 4799:         }
 4800:     }
 4801:     if ($confhash->{$role}{'font'}) {
 4802:         $changes->{$role}{'font'} = 1;
 4803:     }
 4804: }
 4805: 
 4806: sub display_colorchgs {
 4807:     my ($dom,$changes,$roles,$confhash) = @_;
 4808:     my (%choices,$resulttext);
 4809:     if (!grep(/^login$/,@{$roles})) {
 4810:         $resulttext = &mt('Changes made:').'<br />';
 4811:     }
 4812:     foreach my $role (@{$roles}) {
 4813:         if ($role eq 'login') {
 4814:             %choices = &login_choices();
 4815:         } else {
 4816:             %choices = &color_font_choices();
 4817:         }
 4818:         if (ref($changes->{$role}) eq 'HASH') {
 4819:             if ($role ne 'login') {
 4820:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 4821:             }
 4822:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 4823:                 if ($role ne 'login') {
 4824:                     $resulttext .= '<ul>';
 4825:                 }
 4826:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 4827:                     if ($role ne 'login') {
 4828:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 4829:                     }
 4830:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 4831:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 4832:                             if ($confhash->{$role}{$key}{$item}) {
 4833:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 4834:                             } else {
 4835:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 4836:                             }
 4837:                         } elsif ($confhash->{$role}{$item} eq '') {
 4838:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 4839:                         } else {
 4840:                             my $newitem = $confhash->{$role}{$item};
 4841:                             if ($key eq 'images') {
 4842:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 4843:                             }
 4844:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 4845:                         }
 4846:                     }
 4847:                     if ($role ne 'login') {
 4848:                         $resulttext .= '</ul></li>';
 4849:                     }
 4850:                 } else {
 4851:                     if ($confhash->{$role}{$key} eq '') {
 4852:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 4853:                     } else {
 4854:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 4855:                     }
 4856:                 }
 4857:                 if ($role ne 'login') {
 4858:                     $resulttext .= '</ul>';
 4859:                 }
 4860:             }
 4861:         }
 4862:     }
 4863:     return $resulttext;
 4864: }
 4865: 
 4866: sub thumb_dimensions {
 4867:     return ('200','50');
 4868: }
 4869: 
 4870: sub check_dimensions {
 4871:     my ($inputfile) = @_;
 4872:     my ($fullwidth,$fullheight);
 4873:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 4874:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 4875:             my $imageinfo = <PIPE>;
 4876:             if (!close(PIPE)) {
 4877:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 4878:             }
 4879:             chomp($imageinfo);
 4880:             my ($fullsize) = 
 4881:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 4882:             if ($fullsize) {
 4883:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 4884:             }
 4885:         }
 4886:     }
 4887:     return ($fullwidth,$fullheight);
 4888: }
 4889: 
 4890: sub check_configuser {
 4891:     my ($uhome,$dom,$confname,$servadm) = @_;
 4892:     my ($configuserok,%currroles);
 4893:     if ($uhome eq 'no_host') {
 4894:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 4895:         my $configpass = &LONCAPA::Enrollment::create_password();
 4896:         $configuserok = 
 4897:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 4898:                              $configpass,'','','','','',undef,$servadm);
 4899:     } else {
 4900:         $configuserok = 'ok';
 4901:         %currroles = 
 4902:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 4903:     }
 4904:     return ($configuserok,%currroles);
 4905: }
 4906: 
 4907: sub check_authorstatus {
 4908:     my ($dom,$confname,%currroles) = @_;
 4909:     my $author_ok;
 4910:     if (!$currroles{':'.$dom.':au'}) {
 4911:         my $start = time;
 4912:         my $end = 0;
 4913:         $author_ok = 
 4914:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 4915:                                         'au',$end,$start,'','','domconfig');
 4916:     } else {
 4917:         $author_ok = 'ok';
 4918:     }
 4919:     return $author_ok;
 4920: }
 4921: 
 4922: sub publishlogo {
 4923:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 4924:     my ($output,$fname,$logourl);
 4925:     if ($action eq 'upload') {
 4926:         $fname=$env{'form.'.$formname.'.filename'};
 4927:         chop($env{'form.'.$formname});
 4928:     } else {
 4929:         ($fname) = ($formname =~ /([^\/]+)$/);
 4930:     }
 4931:     if ($savefileas ne '') {
 4932:         $fname = $savefileas;
 4933:     }
 4934:     $fname=&Apache::lonnet::clean_filename($fname);
 4935: # See if there is anything left
 4936:     unless ($fname) { return ('error: no uploaded file'); }
 4937:     $fname="$subdir/$fname";
 4938:     my $filepath=$r->dir_config('lonDocRoot')."/priv/$dom/$confname";
 4939:     my ($fnamepath,$file,$fetchthumb);
 4940:     $file=$fname;
 4941:     if ($fname=~m|/|) {
 4942:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 4943:     }
 4944:     my @parts=split(/\//,$filepath.'/'.$fnamepath);
 4945:     my $count;
 4946:     for ($count=4;$count<=$#parts;$count++) {
 4947:         $filepath.="/$parts[$count]";
 4948:         if ((-e $filepath)!=1) {
 4949:             mkdir($filepath,02770);
 4950:         }
 4951:     }
 4952:     # Check for bad extension and disallow upload
 4953:     if ($file=~/\.(\w+)$/ &&
 4954:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 4955:         $output = 
 4956:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 4957:     } elsif ($file=~/\.(\w+)$/ &&
 4958:         !defined(&Apache::loncommon::fileembstyle($1))) {
 4959:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 4960:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 4961:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 4962:     } elsif (-d "$filepath/$file") {
 4963:         $output = &mt('File name is a directory name - rename the file and re-upload');
 4964:     } else {
 4965:         my $source = $filepath.'/'.$file;
 4966:         my $logfile;
 4967:         if (!open($logfile,">>$source".'.log')) {
 4968:             return (&mt('No write permission to Construction Space'));
 4969:         }
 4970:         print $logfile
 4971: "\n================= Publish ".localtime()." ================\n".
 4972: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 4973: # Save the file
 4974:         if (!open(FH,'>'.$source)) {
 4975:             &Apache::lonnet::logthis('Failed to create '.$source);
 4976:             return (&mt('Failed to create file'));
 4977:         }
 4978:         if ($action eq 'upload') {
 4979:             if (!print FH ($env{'form.'.$formname})) {
 4980:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 4981:                 return (&mt('Failed to write file'));
 4982:             }
 4983:         } else {
 4984:             my $original = &Apache::lonnet::filelocation('',$formname);
 4985:             if(!copy($original,$source)) {
 4986:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 4987:                 return (&mt('Failed to write file'));
 4988:             }
 4989:         }
 4990:         close(FH);
 4991:         chmod(0660, $source); # Permissions to rw-rw---.
 4992: 
 4993:         my $docroot=$r->dir_config('lonDocRoot');
 4994:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 4995:         my $copyfile=$targetdir.'/'.$file;
 4996: 
 4997:         my @parts=split(/\//,$targetdir);
 4998:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 4999:         for (my $count=5;$count<=$#parts;$count++) {
 5000:             $path.="/$parts[$count]";
 5001:             if (!-e $path) {
 5002:                 print $logfile "\nCreating directory ".$path;
 5003:                 mkdir($path,02770);
 5004:             }
 5005:         }
 5006:         my $versionresult;
 5007:         if (-e $copyfile) {
 5008:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5009:         } else {
 5010:             $versionresult = 'ok';
 5011:         }
 5012:         if ($versionresult eq 'ok') {
 5013:             if (copy($source,$copyfile)) {
 5014:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5015:                 $output = 'ok';
 5016:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5017:                 push(@{$modified_urls},[$copyfile,$source]);
 5018:                 my $metaoutput = 
 5019:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5020:                 unless ($registered_cleanup) {
 5021:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5022:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5023:                     $registered_cleanup=1;
 5024:                 }
 5025:             } else {
 5026:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5027:                 $output = &mt('Failed to copy file to RES space').", $!";
 5028:             }
 5029:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5030:                 my $inputfile = $filepath.'/'.$file;
 5031:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5032:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5033:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5034:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5035:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5036:                         system("convert -sample $thumbsize $inputfile $outfile");
 5037:                         chmod(0660, $filepath.'/tn-'.$file);
 5038:                         if (-e $outfile) {
 5039:                             my $copyfile=$targetdir.'/tn-'.$file;
 5040:                             if (copy($outfile,$copyfile)) {
 5041:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5042:                                 my $thumb_metaoutput = 
 5043:                                     &write_metadata($dom,$confname,$formname,
 5044:                                                     $targetdir,'tn-'.$file,$logfile);
 5045:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5046:                                 unless ($registered_cleanup) {
 5047:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5048:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5049:                                     $registered_cleanup=1;
 5050:                                 }
 5051:                             } else {
 5052:                                 print $logfile "\nUnable to write ".$copyfile.
 5053:                                                ':'.$!."\n";
 5054:                             }
 5055:                         }
 5056:                     }
 5057:                 }
 5058:             }
 5059:         } else {
 5060:             $output = $versionresult;
 5061:         }
 5062:     }
 5063:     return ($output,$logourl);
 5064: }
 5065: 
 5066: sub logo_versioning {
 5067:     my ($targetdir,$file,$logfile) = @_;
 5068:     my $target = $targetdir.'/'.$file;
 5069:     my ($maxversion,$fn,$extn,$output);
 5070:     $maxversion = 0;
 5071:     if ($file =~ /^(.+)\.(\w+)$/) {
 5072:         $fn=$1;
 5073:         $extn=$2;
 5074:     }
 5075:     opendir(DIR,$targetdir);
 5076:     while (my $filename=readdir(DIR)) {
 5077:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5078:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5079:         }
 5080:     }
 5081:     $maxversion++;
 5082:     print $logfile "\nCreating old version ".$maxversion."\n";
 5083:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5084:     if (copy($target,$copyfile)) {
 5085:         print $logfile "Copied old target to ".$copyfile."\n";
 5086:         $copyfile=$copyfile.'.meta';
 5087:         if (copy($target.'.meta',$copyfile)) {
 5088:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5089:             $output = 'ok';
 5090:         } else {
 5091:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5092:             $output = &mt('Failed to copy old meta').", $!, ";
 5093:         }
 5094:     } else {
 5095:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5096:         $output = &mt('Failed to copy old target').", $!, ";
 5097:     }
 5098:     return $output;
 5099: }
 5100: 
 5101: sub write_metadata {
 5102:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5103:     my (%metadatafields,%metadatakeys,$output);
 5104:     $metadatafields{'title'}=$formname;
 5105:     $metadatafields{'creationdate'}=time;
 5106:     $metadatafields{'lastrevisiondate'}=time;
 5107:     $metadatafields{'copyright'}='public';
 5108:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5109:                                          $env{'user.domain'};
 5110:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5111:     $metadatafields{'domain'}=$dom;
 5112:     {
 5113:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5114:         my $mfh;
 5115:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5116:             foreach (sort keys %metadatafields) {
 5117:                 unless ($_=~/\./) {
 5118:                     my $unikey=$_;
 5119:                     $unikey=~/^([A-Za-z]+)/;
 5120:                     my $tag=$1;
 5121:                     $tag=~tr/A-Z/a-z/;
 5122:                     print $mfh "\n\<$tag";
 5123:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5124:                         my $value=$metadatafields{$unikey.'.'.$_};
 5125:                         $value=~s/\"/\'\'/g;
 5126:                         print $mfh ' '.$_.'="'.$value.'"';
 5127:                     }
 5128:                     print $mfh '>'.
 5129:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5130:                             .'</'.$tag.'>';
 5131:                 }
 5132:             }
 5133:             $output = 'ok';
 5134:             print $logfile "\nWrote metadata";
 5135:             close($mfh);
 5136:         } else {
 5137:             print $logfile "\nFailed to open metadata file";
 5138:             $output = &mt('Could not write metadata');
 5139:         }
 5140:     }
 5141:     return $output;
 5142: }
 5143: 
 5144: sub notifysubscribed {
 5145:     foreach my $targetsource (@{$modified_urls}){
 5146:         next unless (ref($targetsource) eq 'ARRAY');
 5147:         my ($target,$source)=@{$targetsource};
 5148:         if ($source ne '') {
 5149:             if (open(my $logfh,'>>'.$source.'.log')) {
 5150:                 print $logfh "\nCleanup phase: Notifications\n";
 5151:                 my @subscribed=&subscribed_hosts($target);
 5152:                 foreach my $subhost (@subscribed) {
 5153:                     print $logfh "\nNotifying host ".$subhost.':';
 5154:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5155:                     print $logfh $reply;
 5156:                 }
 5157:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5158:                 foreach my $subhost (@subscribedmeta) {
 5159:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5160:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5161:                                                         $subhost);
 5162:                     print $logfh $reply;
 5163:                 }
 5164:                 print $logfh "\n============ Done ============\n";
 5165:                 close($logfh);
 5166:             }
 5167:         }
 5168:     }
 5169:     return OK;
 5170: }
 5171: 
 5172: sub subscribed_hosts {
 5173:     my ($target) = @_;
 5174:     my @subscribed;
 5175:     if (open(my $fh,"<$target.subscription")) {
 5176:         while (my $subline=<$fh>) {
 5177:             if ($subline =~ /^($match_lonid):/) {
 5178:                 my $host = $1;
 5179:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5180:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5181:                         push(@subscribed,$host);
 5182:                     }
 5183:                 }
 5184:             }
 5185:         }
 5186:     }
 5187:     return @subscribed;
 5188: }
 5189: 
 5190: sub check_switchserver {
 5191:     my ($dom,$confname) = @_;
 5192:     my ($allowed,$switchserver);
 5193:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5194:     if ($home eq 'no_host') {
 5195:         $home = &Apache::lonnet::domain($dom,'primary');
 5196:     }
 5197:     my @ids=&Apache::lonnet::current_machine_ids();
 5198:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5199:     if (!$allowed) {
 5200: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&role=dc./'.$dom.'/">'.&mt('Switch Server').'</a>';
 5201:     }
 5202:     return $switchserver;
 5203: }
 5204: 
 5205: sub modify_quotas {
 5206:     my ($dom,$action,%domconfig) = @_;
 5207:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5208:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5209:     if ($action eq 'quotas') {
 5210:         $context = 'tools'; 
 5211:     } else { 
 5212:         $context = $action;
 5213:     }
 5214:     if ($context eq 'requestcourses') {
 5215:         @usertools = ('official','unofficial','community');
 5216:         @options =('norequest','approval','validate','autolimit');
 5217:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5218:         %titles = &courserequest_titles();
 5219:         $toolregexp = join('|',@usertools);
 5220:         %conditions = &courserequest_conditions();
 5221:     } else {
 5222:         @usertools = ('aboutme','blog','portfolio');
 5223:         %titles = &tool_titles();
 5224:     }
 5225:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5226:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5227:     foreach my $key (keys(%env)) {
 5228:         if ($context eq 'requestcourses') {
 5229:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5230:                 my $item = $1;
 5231:                 my $type = $2;
 5232:                 if ($type =~ /^limit_(.+)/) {
 5233:                     $limithash{$item}{$1} = $env{$key};
 5234:                 } else {
 5235:                     $confhash{$item}{$type} = $env{$key};
 5236:                 }
 5237:             }
 5238:         } else {
 5239:             if ($key =~ /^form\.quota_(.+)$/) {
 5240:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5241:             }
 5242:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5243:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5244:             }
 5245:         }
 5246:     }
 5247:     if ($context eq 'requestcourses') {
 5248:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5249:         @approvalnotify = sort(@approvalnotify);
 5250:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5251:         if (ref($domconfig{$action}) eq 'HASH') {
 5252:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5253:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5254:                     $changes{'notify'}{'approval'} = 1;
 5255:                 }
 5256:             } else {
 5257:                 if ($confhash{'notify'}{'approval'}) {
 5258:                     $changes{'notify'}{'approval'} = 1;
 5259:                 }
 5260:             }
 5261:         } else {
 5262:             if ($confhash{'notify'}{'approval'}) {
 5263:                 $changes{'notify'}{'approval'} = 1;
 5264:             }
 5265:         }
 5266:     } else {
 5267:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5268:     }
 5269:     foreach my $item (@usertools) {
 5270:         foreach my $type (@{$types},'default','_LC_adv') {
 5271:             my $unset; 
 5272:             if ($context eq 'requestcourses') {
 5273:                 $unset = '0';
 5274:                 if ($type eq '_LC_adv') {
 5275:                     $unset = '';
 5276:                 }
 5277:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5278:                     $confhash{$item}{$type} .= '=';
 5279:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5280:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5281:                     }
 5282:                 }
 5283:             } else {
 5284:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5285:                     $confhash{$item}{$type} = 1;
 5286:                 } else {
 5287:                     $confhash{$item}{$type} = 0;
 5288:                 }
 5289:             }
 5290:             if (ref($domconfig{$action}) eq 'HASH') {
 5291:                 if (ref($domconfig{$action}{$item}) eq 'HASH') {
 5292:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5293:                         $changes{$item}{$type} = 1;
 5294:                     }
 5295:                 } else {
 5296:                     if ($context eq 'requestcourses') {
 5297:                         if ($confhash{$item}{$type} ne $unset) {
 5298:                             $changes{$item}{$type} = 1;
 5299:                         }
 5300:                     } else {
 5301:                         if (!$confhash{$item}{$type}) {
 5302:                             $changes{$item}{$type} = 1;
 5303:                         }
 5304:                     }
 5305:                 }
 5306:             } else {
 5307:                 if ($context eq 'requestcourses') {
 5308:                     if ($confhash{$item}{$type} ne $unset) {
 5309:                         $changes{$item}{$type} = 1;
 5310:                     }
 5311:                 } else {
 5312:                     if (!$confhash{$item}{$type}) {
 5313:                         $changes{$item}{$type} = 1;
 5314:                     }
 5315:                 }
 5316:             }
 5317:         }
 5318:     }
 5319:     unless ($context eq 'requestcourses') {
 5320:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5321:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5322:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5323:                     if (exists($confhash{'defaultquota'}{$key})) {
 5324:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5325:                             $changes{'defaultquota'}{$key} = 1;
 5326:                         }
 5327:                     } else {
 5328:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5329:                     }
 5330:                 }
 5331:             } else {
 5332:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5333:                     if (exists($confhash{'defaultquota'}{$key})) {
 5334:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5335:                             $changes{'defaultquota'}{$key} = 1;
 5336:                         }
 5337:                     } else {
 5338:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5339:                     }
 5340:                 }
 5341:             }
 5342:         }
 5343:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5344:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5345:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5346:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5347:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5348:                             $changes{'defaultquota'}{$key} = 1;
 5349:                         }
 5350:                     } else {
 5351:                         if (!exists($domconfig{'quotas'}{$key})) {
 5352:                             $changes{'defaultquota'}{$key} = 1;
 5353:                         }
 5354:                     }
 5355:                 } else {
 5356:                     $changes{'defaultquota'}{$key} = 1;
 5357:                 }
 5358:             }
 5359:         }
 5360:     }
 5361: 
 5362:     foreach my $key (keys(%confhash)) {
 5363:         $domdefaults{$key} = $confhash{$key};
 5364:     }
 5365:    
 5366:     my %quotahash = (
 5367:                       $action => { %confhash }
 5368:                     );
 5369:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5370:                                              $dom);
 5371:     if ($putresult eq 'ok') {
 5372:         if (keys(%changes) > 0) {
 5373:             my $cachetime = 24*60*60;
 5374:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5375: 
 5376:             $resulttext = &mt('Changes made:').'<ul>';
 5377:             unless ($context eq 'requestcourses') {
 5378:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5379:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5380:                     foreach my $type (@{$types},'default') {
 5381:                         if (defined($changes{'defaultquota'}{$type})) {
 5382:                             my $typetitle = $usertypes->{$type};
 5383:                             if ($type eq 'default') {
 5384:                                 $typetitle = $othertitle;
 5385:                             }
 5386:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5387:                         }
 5388:                     }
 5389:                     $resulttext .= '</ul></li>';
 5390:                 }
 5391:             }
 5392:             my %newenv;
 5393:             foreach my $item (@usertools) {
 5394:                 if (ref($changes{$item}) eq 'HASH') {
 5395:                     my $newacc = 
 5396:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5397:                                                           $env{'user.domain'},
 5398:                                                           $item,'reload',$context);
 5399:                     if ($context eq 'requestcourses') {
 5400:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5401:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5402:                         }
 5403:                     } else {
 5404:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5405:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5406:                         }
 5407:                     }
 5408:                     $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5409:                     foreach my $type (@{$types},'default','_LC_adv') {
 5410:                         if ($changes{$item}{$type}) {
 5411:                             my $typetitle = $usertypes->{$type};
 5412:                             if ($type eq 'default') {
 5413:                                 $typetitle = $othertitle;
 5414:                             } elsif ($type eq '_LC_adv') {
 5415:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5416:                             }
 5417:                             if ($confhash{$item}{$type}) {
 5418:                                 if ($context eq 'requestcourses') {
 5419:                                     my $cond;
 5420:                                     if ($confhash{$item}{$type} =~ /^autolimit=(\d*)$/) {
 5421:                                         if ($1 eq '') {
 5422:                                             $cond = &mt('(Automatic processing of any request).');
 5423:                                         } else {
 5424:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5425:                                         }
 5426:                                     } else { 
 5427:                                         $cond = $conditions{$confhash{$item}{$type}};
 5428:                                     }
 5429:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5430:                                 } else {
 5431:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5432:                                 }
 5433:                             } else {
 5434:                                 if ($type eq '_LC_adv') {
 5435:                                     if ($confhash{$item}{$type} eq '0') {
 5436:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5437:                                     } else { 
 5438:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5439:                                     }
 5440:                                 } else {
 5441:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5442:                                 }
 5443:                             }
 5444:                         }
 5445:                     }
 5446:                     $resulttext .= '</ul></li>';
 5447:                 }
 5448:             }
 5449:             if ($action eq 'requestcourses') {
 5450:                 if (ref($changes{'notify'}) eq 'HASH') {
 5451:                     if ($changes{'notify'}{'approval'}) {
 5452:                         if (ref($confhash{'notify'}) eq 'HASH') {
 5453:                             if ($confhash{'notify'}{'approval'}) {
 5454:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 5455:                             } else {
 5456:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of course requests requiring approval.').'</li>';
 5457:                             }
 5458:                         }
 5459:                     }
 5460:                 }
 5461:             }
 5462:             $resulttext .= '</ul>';
 5463:             if (keys(%newenv)) {
 5464:                 &Apache::lonnet::appenv(\%newenv);
 5465:             }
 5466:         } else {
 5467:             if ($context eq 'requestcourses') {
 5468:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 5469:             } else {
 5470:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 5471:             }
 5472:         }
 5473:     } else {
 5474:         $resulttext = '<span class="LC_error">'.
 5475: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5476:     }
 5477:     return $resulttext;
 5478: }
 5479: 
 5480: sub modify_autoenroll {
 5481:     my ($dom,%domconfig) = @_;
 5482:     my ($resulttext,%changes);
 5483:     my %currautoenroll;
 5484:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 5485:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 5486:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 5487:         }
 5488:     }
 5489:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 5490:     my %title = ( run => 'Auto-enrollment active',
 5491:                   sender => 'Sender for notification messages',
 5492:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 5493:     my @offon = ('off','on');
 5494:     my $sender_uname = $env{'form.sender_uname'};
 5495:     my $sender_domain = $env{'form.sender_domain'};
 5496:     if ($sender_domain eq '') {
 5497:         $sender_uname = '';
 5498:     } elsif ($sender_uname eq '') {
 5499:         $sender_domain = '';
 5500:     }
 5501:     my $coowners = $env{'form.autoassign_coowners'};
 5502:     my %autoenrollhash =  (
 5503:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 5504:                                        'sender_uname' => $sender_uname,
 5505:                                        'sender_domain' => $sender_domain,
 5506:                                        'co-owners' => $coowners,
 5507:                                 }
 5508:                      );
 5509:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 5510:                                              $dom);
 5511:     if ($putresult eq 'ok') {
 5512:         if (exists($currautoenroll{'run'})) {
 5513:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 5514:                  $changes{'run'} = 1;
 5515:              }
 5516:         } elsif ($autorun) {
 5517:             if ($env{'form.autoenroll_run'} ne '1') {
 5518:                  $changes{'run'} = 1;
 5519:             }
 5520:         }
 5521:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 5522:             $changes{'sender'} = 1;
 5523:         }
 5524:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 5525:             $changes{'sender'} = 1;
 5526:         }
 5527:         if ($currautoenroll{'co-owners'} ne '') {
 5528:             if ($currautoenroll{'co-owners'} ne $coowners) {
 5529:                 $changes{'coowners'} = 1;
 5530:             }
 5531:         } elsif ($coowners) {
 5532:             $changes{'coowners'} = 1;
 5533:         }      
 5534:         if (keys(%changes) > 0) {
 5535:             $resulttext = &mt('Changes made:').'<ul>';
 5536:             if ($changes{'run'}) {
 5537:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 5538:             }
 5539:             if ($changes{'sender'}) {
 5540:                 if ($sender_uname eq '' || $sender_domain eq '') {
 5541:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 5542:                 } else {
 5543:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 5544:                 }
 5545:             }
 5546:             if ($changes{'coowners'}) {
 5547:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 5548:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 5549:             }
 5550:             $resulttext .= '</ul>';
 5551:         } else {
 5552:             $resulttext = &mt('No changes made to auto-enrollment settings');
 5553:         }
 5554:     } else {
 5555:         $resulttext = '<span class="LC_error">'.
 5556: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5557:     }
 5558:     return $resulttext;
 5559: }
 5560: 
 5561: sub modify_autoupdate {
 5562:     my ($dom,%domconfig) = @_;
 5563:     my ($resulttext,%currautoupdate,%fields,%changes);
 5564:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 5565:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 5566:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 5567:         }
 5568:     }
 5569:     my @offon = ('off','on');
 5570:     my %title = &Apache::lonlocal::texthash (
 5571:                    run => 'Auto-update:',
 5572:                    classlists => 'Updates to user information in classlists?'
 5573:                 );
 5574:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5575:     my %fieldtitles = &Apache::lonlocal::texthash (
 5576:                         id => 'Student/Employee ID',
 5577:                         permanentemail => 'E-mail address',
 5578:                         lastname => 'Last Name',
 5579:                         firstname => 'First Name',
 5580:                         middlename => 'Middle Name',
 5581:                         generation => 'Generation',
 5582:                       );
 5583:     $othertitle = &mt('All users');
 5584:     if (keys(%{$usertypes}) >  0) {
 5585:         $othertitle = &mt('Other users');
 5586:     }
 5587:     foreach my $key (keys(%env)) {
 5588:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 5589:             my ($usertype,$item) = ($1,$2);
 5590:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 5591:                 if ($usertype eq 'default') {   
 5592:                     push(@{$fields{$1}},$2);
 5593:                 } elsif (ref($types) eq 'ARRAY') {
 5594:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 5595:                         push(@{$fields{$1}},$2);
 5596:                     }
 5597:                 }
 5598:             }
 5599:         }
 5600:     }
 5601:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 5602:     @lockablenames = sort(@lockablenames);
 5603:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 5604:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 5605:         if (@changed) {
 5606:             $changes{'lockablenames'} = 1;
 5607:         }
 5608:     } else {
 5609:         if (@lockablenames) {
 5610:             $changes{'lockablenames'} = 1;
 5611:         }
 5612:     }
 5613:     my %updatehash = (
 5614:                       autoupdate => { run => $env{'form.autoupdate_run'},
 5615:                                       classlists => $env{'form.classlists'},
 5616:                                       fields => {%fields},
 5617:                                       lockablenames => \@lockablenames,
 5618:                                     }
 5619:                      );
 5620:     foreach my $key (keys(%currautoupdate)) {
 5621:         if (($key eq 'run') || ($key eq 'classlists')) {
 5622:             if (exists($updatehash{autoupdate}{$key})) {
 5623:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 5624:                     $changes{$key} = 1;
 5625:                 }
 5626:             }
 5627:         } elsif ($key eq 'fields') {
 5628:             if (ref($currautoupdate{$key}) eq 'HASH') {
 5629:                 foreach my $item (@{$types},'default') {
 5630:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 5631:                         my $change = 0;
 5632:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 5633:                             if (!exists($fields{$item})) {
 5634:                                 $change = 1;
 5635:                                 last;
 5636:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 5637:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 5638:                                     $change = 1;
 5639:                                     last;
 5640:                                 }
 5641:                             }
 5642:                         }
 5643:                         if ($change) {
 5644:                             push(@{$changes{$key}},$item);
 5645:                         }
 5646:                     } 
 5647:                 }
 5648:             }
 5649:         } elsif ($key eq 'lockablenames') {
 5650:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 5651:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 5652:                 if (@changed) {
 5653:                     $changes{'lockablenames'} = 1;
 5654:                 }
 5655:             } else {
 5656:                 if (@lockablenames) {
 5657:                     $changes{'lockablenames'} = 1;
 5658:                 }
 5659:             }
 5660:         }
 5661:     }
 5662:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 5663:         if (@lockablenames) {
 5664:             $changes{'lockablenames'} = 1;
 5665:         }
 5666:     }
 5667:     foreach my $item (@{$types},'default') {
 5668:         if (defined($fields{$item})) {
 5669:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 5670:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 5671:                     my $change = 0;
 5672:                     if (ref($fields{$item}) eq 'ARRAY') {
 5673:                         foreach my $type (@{$fields{$item}}) {
 5674:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 5675:                                 $change = 1;
 5676:                                 last;
 5677:                             }
 5678:                         }
 5679:                     }
 5680:                     if ($change) {
 5681:                         push(@{$changes{'fields'}},$item);
 5682:                     }
 5683:                 } else {
 5684:                     push(@{$changes{'fields'}},$item);
 5685:                 }
 5686:             } else {
 5687:                 push(@{$changes{'fields'}},$item);
 5688:             }
 5689:         }
 5690:     }
 5691:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 5692:                                              $dom);
 5693:     if ($putresult eq 'ok') {
 5694:         if (keys(%changes) > 0) {
 5695:             $resulttext = &mt('Changes made:').'<ul>';
 5696:             foreach my $key (sort(keys(%changes))) {
 5697:                 if ($key eq 'lockablenames') {
 5698:                     $resulttext .= '<li>';
 5699:                     if (@lockablenames) {
 5700:                         $usertypes->{'default'} = $othertitle;
 5701:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 5702:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 5703:                     } else {
 5704:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 5705:                     }
 5706:                     $resulttext .= '</li>';
 5707:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 5708:                     foreach my $item (@{$changes{$key}}) {
 5709:                         my @newvalues;
 5710:                         foreach my $type (@{$fields{$item}}) {
 5711:                             push(@newvalues,$fieldtitles{$type});
 5712:                         }
 5713:                         my $newvaluestr;
 5714:                         if (@newvalues > 0) {
 5715:                             $newvaluestr = join(', ',@newvalues);
 5716:                         } else {
 5717:                             $newvaluestr = &mt('none');
 5718:                         }
 5719:                         if ($item eq 'default') {
 5720:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 5721:                         } else {
 5722:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 5723:                         }
 5724:                     }
 5725:                 } else {
 5726:                     my $newvalue;
 5727:                     if ($key eq 'run') {
 5728:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 5729:                     } else {
 5730:                         $newvalue = $offon[$env{'form.'.$key}];
 5731:                     }
 5732:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 5733:                 }
 5734:             }
 5735:             $resulttext .= '</ul>';
 5736:         } else {
 5737:             $resulttext = &mt('No changes made to autoupdates');
 5738:         }
 5739:     } else {
 5740:         $resulttext = '<span class="LC_error">'.
 5741: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5742:     }
 5743:     return $resulttext;
 5744: }
 5745: 
 5746: sub modify_autocreate {
 5747:     my ($dom,%domconfig) = @_;
 5748:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 5749:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 5750:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 5751:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 5752:         }
 5753:     }
 5754:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 5755:                  req => 'Auto-creation of validated requests for official courses',
 5756:                  xmldc => 'Identity of course creator of courses from XML files',
 5757:                );
 5758:     my @types = ('xml','req');
 5759:     foreach my $item (@types) {
 5760:         $newvals{$item} = $env{'form.autocreate_'.$item};
 5761:         $newvals{$item} =~ s/\D//g;
 5762:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 5763:     }
 5764:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 5765:     my %domcoords = &get_active_dcs($dom);
 5766:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 5767:         $newvals{'xmldc'} = '';
 5768:     } 
 5769:     %autocreatehash =  (
 5770:                         autocreate => { xml => $newvals{'xml'},
 5771:                                         req => $newvals{'req'},
 5772:                                       }
 5773:                        );
 5774:     if ($newvals{'xmldc'} ne '') {
 5775:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 5776:     }
 5777:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 5778:                                              $dom);
 5779:     if ($putresult eq 'ok') {
 5780:         my @items = @types;
 5781:         if ($newvals{'xml'}) {
 5782:             push(@items,'xmldc');
 5783:         }
 5784:         foreach my $item (@items) {
 5785:             if (exists($currautocreate{$item})) {
 5786:                 if ($currautocreate{$item} ne $newvals{$item}) {
 5787:                     $changes{$item} = 1;
 5788:                 }
 5789:             } elsif ($newvals{$item}) {
 5790:                 $changes{$item} = 1;
 5791:             }
 5792:         }
 5793:         if (keys(%changes) > 0) {
 5794:             my @offon = ('off','on'); 
 5795:             $resulttext = &mt('Changes made:').'<ul>';
 5796:             foreach my $item (@types) {
 5797:                 if ($changes{$item}) {
 5798:                     my $newtxt = $offon[$newvals{$item}];
 5799:                     $resulttext .= '<li>'.&mt("$title{$item} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>';
 5800:                 }
 5801:             }
 5802:             if ($changes{'xmldc'}) {
 5803:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 5804:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 5805:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>'; 
 5806:             }
 5807:             $resulttext .= '</ul>';
 5808:         } else {
 5809:             $resulttext = &mt('No changes made to auto-creation settings');
 5810:         }
 5811:     } else {
 5812:         $resulttext = '<span class="LC_error">'.
 5813:             &mt('An error occurred: [_1]',$putresult).'</span>';
 5814:     }
 5815:     return $resulttext;
 5816: }
 5817: 
 5818: sub modify_directorysrch {
 5819:     my ($dom,%domconfig) = @_;
 5820:     my ($resulttext,%changes);
 5821:     my %currdirsrch;
 5822:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 5823:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 5824:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 5825:         }
 5826:     }
 5827:     my %title = ( available => 'Directory search available',
 5828:                   localonly => 'Other domains can search',
 5829:                   searchby => 'Search types',
 5830:                   searchtypes => 'Search latitude');
 5831:     my @offon = ('off','on');
 5832:     my @otherdoms = ('Yes','No');
 5833: 
 5834:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 5835:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 5836:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 5837: 
 5838:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5839:     if (keys(%{$usertypes}) == 0) {
 5840:         @cansearch = ('default');
 5841:     } else {
 5842:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 5843:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 5844:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 5845:                     push(@{$changes{'cansearch'}},$type);
 5846:                 }
 5847:             }
 5848:             foreach my $type (@cansearch) {
 5849:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 5850:                     push(@{$changes{'cansearch'}},$type);
 5851:                 }
 5852:             }
 5853:         } else {
 5854:             push(@{$changes{'cansearch'}},@cansearch);
 5855:         }
 5856:     }
 5857: 
 5858:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 5859:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 5860:             if (!grep(/^\Q$by\E$/,@searchby)) {
 5861:                 push(@{$changes{'searchby'}},$by);
 5862:             }
 5863:         }
 5864:         foreach my $by (@searchby) {
 5865:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 5866:                 push(@{$changes{'searchby'}},$by);
 5867:             }
 5868:         }
 5869:     } else {
 5870:         push(@{$changes{'searchby'}},@searchby);
 5871:     }
 5872: 
 5873:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 5874:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 5875:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 5876:                 push(@{$changes{'searchtypes'}},$type);
 5877:             }
 5878:         }
 5879:         foreach my $type (@searchtypes) {
 5880:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 5881:                 push(@{$changes{'searchtypes'}},$type);
 5882:             }
 5883:         }
 5884:     } else {
 5885:         if (exists($currdirsrch{'searchtypes'})) {
 5886:             foreach my $type (@searchtypes) {  
 5887:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 5888:                     push(@{$changes{'searchtypes'}},$type);
 5889:                 }
 5890:             }
 5891:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 5892:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 5893:             }   
 5894:         } else {
 5895:             push(@{$changes{'searchtypes'}},@searchtypes); 
 5896:         }
 5897:     }
 5898: 
 5899:     my %dirsrch_hash =  (
 5900:             directorysrch => { available => $env{'form.dirsrch_available'},
 5901:                                cansearch => \@cansearch,
 5902:                                localonly => $env{'form.dirsrch_localonly'},
 5903:                                searchby => \@searchby,
 5904:                                searchtypes => \@searchtypes,
 5905:                              }
 5906:             );
 5907:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 5908:                                              $dom);
 5909:     if ($putresult eq 'ok') {
 5910:         if (exists($currdirsrch{'available'})) {
 5911:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 5912:                  $changes{'available'} = 1;
 5913:              }
 5914:         } else {
 5915:             if ($env{'form.dirsrch_available'} eq '1') {
 5916:                 $changes{'available'} = 1;
 5917:             }
 5918:         }
 5919:         if (exists($currdirsrch{'localonly'})) {
 5920:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 5921:                  $changes{'localonly'} = 1;
 5922:              }
 5923:         } else {
 5924:             if ($env{'form.dirsrch_localonly'} eq '1') {
 5925:                 $changes{'localonly'} = 1;
 5926:             }
 5927:         }
 5928:         if (keys(%changes) > 0) {
 5929:             $resulttext = &mt('Changes made:').'<ul>';
 5930:             if ($changes{'available'}) {
 5931:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 5932:             }
 5933:             if ($changes{'localonly'}) {
 5934:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 5935:             }
 5936: 
 5937:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 5938:                 my $chgtext;
 5939:                 if (ref($usertypes) eq 'HASH') {
 5940:                     if (keys(%{$usertypes}) > 0) {
 5941:                         foreach my $type (@{$types}) {
 5942:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 5943:                                 $chgtext .= $usertypes->{$type}.'; ';
 5944:                             }
 5945:                         }
 5946:                         if (grep(/^default$/,@cansearch)) {
 5947:                             $chgtext .= $othertitle;
 5948:                         } else {
 5949:                             $chgtext =~ s/\; $//;
 5950:                         }
 5951:                         $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>';
 5952:                     }
 5953:                 }
 5954:             }
 5955:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 5956:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 5957:                 my $chgtext;
 5958:                 foreach my $type (@{$titleorder}) {
 5959:                     if (grep(/^\Q$type\E$/,@searchby)) {
 5960:                         if (defined($searchtitles->{$type})) {
 5961:                             $chgtext .= $searchtitles->{$type}.'; ';
 5962:                         }
 5963:                     }
 5964:                 }
 5965:                 $chgtext =~ s/\; $//;
 5966:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 5967:             }
 5968:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 5969:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 5970:                 my $chgtext;
 5971:                 foreach my $type (@{$srchtypeorder}) {
 5972:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 5973:                         if (defined($srchtypes_desc->{$type})) {
 5974:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 5975:                         }
 5976:                     }
 5977:                 }
 5978:                 $chgtext =~ s/\; $//;
 5979:                 $resulttext .= '<li>'.&mt("$title{'searchtypes'} set to: \"[_1]\"",$chgtext).'</li>';
 5980:             }
 5981:             $resulttext .= '</ul>';
 5982:         } else {
 5983:             $resulttext = &mt('No changes made to institution directory search settings');
 5984:         }
 5985:     } else {
 5986:         $resulttext = '<span class="LC_error">'.
 5987:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 5988:     }
 5989:     return $resulttext;
 5990: }
 5991: 
 5992: sub modify_contacts {
 5993:     my ($dom,%domconfig) = @_;
 5994:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 5995:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 5996:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 5997:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 5998:         }
 5999:     }
 6000:     my (%others,%to,%bcc);
 6001:     my @contacts = ('supportemail','adminemail');
 6002:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6003:                     'requestsmail');
 6004:     foreach my $type (@mailings) {
 6005:         @{$newsetting{$type}} = 
 6006:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6007:         foreach my $item (@contacts) {
 6008:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6009:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6010:             } else {
 6011:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6012:             }
 6013:         }  
 6014:         $others{$type} = $env{'form.'.$type.'_others'};
 6015:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6016:         if ($type eq 'helpdeskmail') {
 6017:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6018:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6019:         }
 6020:     }
 6021:     foreach my $item (@contacts) {
 6022:         $to{$item} = $env{'form.'.$item};
 6023:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6024:     }
 6025:     if (keys(%currsetting) > 0) {
 6026:         foreach my $item (@contacts) {
 6027:             if ($to{$item} ne $currsetting{$item}) {
 6028:                 $changes{$item} = 1;
 6029:             }
 6030:         }
 6031:         foreach my $type (@mailings) {
 6032:             foreach my $item (@contacts) {
 6033:                 if (ref($currsetting{$type}) eq 'HASH') {
 6034:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6035:                         push(@{$changes{$type}},$item);
 6036:                     }
 6037:                 } else {
 6038:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6039:                 }
 6040:             }
 6041:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6042:                 push(@{$changes{$type}},'others');
 6043:             }
 6044:             if ($type eq 'helpdeskmail') {   
 6045:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6046:                     push(@{$changes{$type}},'bcc'); 
 6047:                 }
 6048:             }
 6049:         }
 6050:     } else {
 6051:         my %default;
 6052:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6053:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6054:         $default{'errormail'} = 'adminemail';
 6055:         $default{'packagesmail'} = 'adminemail';
 6056:         $default{'helpdeskmail'} = 'supportemail';
 6057:         $default{'lonstatusmail'} = 'adminemail';
 6058:         $default{'requestsmail'} = 'adminemail';
 6059:         foreach my $item (@contacts) {
 6060:            if ($to{$item} ne $default{$item}) {
 6061:               $changes{$item} = 1;
 6062:            } 
 6063:         }
 6064:         foreach my $type (@mailings) {
 6065:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6066:                
 6067:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6068:             }
 6069:             if ($others{$type} ne '') {
 6070:                 push(@{$changes{$type}},'others');
 6071:             }
 6072:             if ($type eq 'helpdeskmail') {
 6073:                 if ($bcc{$type} ne '') {
 6074:                     push(@{$changes{$type}},'bcc');
 6075:                 }
 6076:             }
 6077:         }
 6078:     }
 6079:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6080:                                              $dom);
 6081:     if ($putresult eq 'ok') {
 6082:         if (keys(%changes) > 0) {
 6083:             my ($titles,$short_titles)  = &contact_titles();
 6084:             $resulttext = &mt('Changes made:').'<ul>';
 6085:             foreach my $item (@contacts) {
 6086:                 if ($changes{$item}) {
 6087:                     $resulttext .= '<li>'.$titles->{$item}.
 6088:                                     &mt(' set to: ').
 6089:                                     '<span class="LC_cusr_emph">'.
 6090:                                     $to{$item}.'</span></li>';
 6091:                 }
 6092:             }
 6093:             foreach my $type (@mailings) {
 6094:                 if (ref($changes{$type}) eq 'ARRAY') {
 6095:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6096:                     my @text;
 6097:                     foreach my $item (@{$newsetting{$type}}) {
 6098:                         push(@text,$short_titles->{$item});
 6099:                     }
 6100:                     if ($others{$type} ne '') {
 6101:                         push(@text,$others{$type});
 6102:                     }
 6103:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6104:                                    join(', ',@text).'</span>';
 6105:                     if ($type eq 'helpdeskmail') {
 6106:                         if ($bcc{$type} ne '') {
 6107:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6108:                         }
 6109:                     }
 6110:                     $resulttext .= '</li>';
 6111:                 }
 6112:             }
 6113:             $resulttext .= '</ul>';
 6114:         } else {
 6115:             $resulttext = &mt('No changes made to contact information');
 6116:         }
 6117:     } else {
 6118:         $resulttext = '<span class="LC_error">'.
 6119:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6120:     }
 6121:     return $resulttext;
 6122: }
 6123: 
 6124: sub modify_usercreation {
 6125:     my ($dom,%domconfig) = @_;
 6126:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6127:     my $warningmsg;
 6128:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6129:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6130:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6131:         }
 6132:     }
 6133:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6134:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6135:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6136:     my @contexts = ('author','course','requestcrs','selfcreate');
 6137:     foreach my $item(@contexts) {
 6138:         if ($item eq 'selfcreate') {
 6139:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6140:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6141:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6142:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6143:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6144:                         $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.');   
 6145:                     }
 6146:                 }
 6147:             }
 6148:         } else {
 6149:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6150:         }
 6151:     }
 6152:     my ($othertitle,$usertypes,$types) = 
 6153:         &Apache::loncommon::sorted_inst_types($dom);
 6154:     if (ref($types) eq 'ARRAY') {
 6155:         if (@{$types} > 0) {
 6156:             @{$cancreate{'statustocreate'}} = 
 6157:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6158:         } else {
 6159:             @{$cancreate{'statustocreate'}} = ();
 6160:         }
 6161:         push(@contexts,'statustocreate');
 6162:     }
 6163:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6164:         foreach my $item (@contexts) {
 6165:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6166:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6167:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6168:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6169:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6170:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6171:                                     push(@{$changes{'cancreate'}},$item);
 6172:                                 }
 6173:                             }
 6174:                         }
 6175:                     }
 6176:                 } else {
 6177:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6178:                         if (@{$cancreate{$item}} > 0) {
 6179:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6180:                                 push(@{$changes{'cancreate'}},$item);
 6181:                             }
 6182:                         }
 6183:                     } else {
 6184:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6185:                             if (@{$cancreate{$item}} < 3) {
 6186:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6187:                                     push(@{$changes{'cancreate'}},$item);
 6188:                                 }
 6189:                             }
 6190:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6191:                             if (@{$cancreate{$item}} > 0) {
 6192:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6193:                                     push(@{$changes{'cancreate'}},$item);
 6194:                                 }
 6195:                             }
 6196:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6197:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6198:                                 push(@{$changes{'cancreate'}},$item);
 6199:                             }
 6200:                         }
 6201:                     }
 6202:                 }
 6203:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6204:                     foreach my $type (@{$cancreate{$item}}) {
 6205:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6206:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6207:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6208:                                     push(@{$changes{'cancreate'}},$item);
 6209:                                 }
 6210:                             }
 6211:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6212:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6213:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6214:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6215:                                     push(@{$changes{'cancreate'}},$item);
 6216:                                 }
 6217:                             }
 6218:                         }
 6219:                     }
 6220:                 }
 6221:             } else {
 6222:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6223:                     push(@{$changes{'cancreate'}},$item);
 6224:                 }
 6225:             }
 6226:         }
 6227:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6228:         foreach my $item (@contexts) {
 6229:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6230:                 if ($cancreate{$item} ne 'any') {
 6231:                     push(@{$changes{'cancreate'}},$item);
 6232:                 }
 6233:             } else {
 6234:                 if ($cancreate{$item} ne 'none') {
 6235:                     push(@{$changes{'cancreate'}},$item);
 6236:                 }
 6237:             }
 6238:         }
 6239:     } else {
 6240:         foreach my $item (@contexts)  {
 6241:             push(@{$changes{'cancreate'}},$item);
 6242:         }
 6243:     }
 6244: 
 6245:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6246:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6247:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6248:                 push(@{$changes{'username_rule'}},$type);
 6249:             }
 6250:         }
 6251:         foreach my $type (@username_rule) {
 6252:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6253:                 push(@{$changes{'username_rule'}},$type);
 6254:             }
 6255:         }
 6256:     } else {
 6257:         push(@{$changes{'username_rule'}},@username_rule);
 6258:     }
 6259: 
 6260:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6261:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 6262:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 6263:                 push(@{$changes{'id_rule'}},$type);
 6264:             }
 6265:         }
 6266:         foreach my $type (@id_rule) {
 6267:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 6268:                 push(@{$changes{'id_rule'}},$type);
 6269:             }
 6270:         }
 6271:     } else {
 6272:         push(@{$changes{'id_rule'}},@id_rule);
 6273:     }
 6274: 
 6275:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 6276:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 6277:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 6278:                 push(@{$changes{'email_rule'}},$type);
 6279:             }
 6280:         }
 6281:         foreach my $type (@email_rule) {
 6282:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 6283:                 push(@{$changes{'email_rule'}},$type);
 6284:             }
 6285:         }
 6286:     } else {
 6287:         push(@{$changes{'email_rule'}},@email_rule);
 6288:     }
 6289: 
 6290:     my @authen_contexts = ('author','course','domain');
 6291:     my @authtypes = ('int','krb4','krb5','loc');
 6292:     my %authhash;
 6293:     foreach my $item (@authen_contexts) {
 6294:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 6295:         foreach my $auth (@authtypes) {
 6296:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 6297:                 $authhash{$item}{$auth} = 1;
 6298:             } else {
 6299:                 $authhash{$item}{$auth} = 0;
 6300:             }
 6301:         }
 6302:     }
 6303:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 6304:         foreach my $item (@authen_contexts) {
 6305:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 6306:                 foreach my $auth (@authtypes) {
 6307:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 6308:                         push(@{$changes{'authtypes'}},$item);
 6309:                         last;
 6310:                     }
 6311:                 }
 6312:             }
 6313:         }
 6314:     } else {
 6315:         foreach my $item (@authen_contexts) {
 6316:             push(@{$changes{'authtypes'}},$item);
 6317:         }
 6318:     }
 6319: 
 6320:     my %usercreation_hash =  (
 6321:             usercreation => {
 6322:                               cancreate     => \%cancreate,
 6323:                               username_rule => \@username_rule,
 6324:                               id_rule       => \@id_rule,
 6325:                               email_rule    => \@email_rule,
 6326:                               authtypes     => \%authhash,
 6327:                             }
 6328:             );
 6329: 
 6330:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 6331:                                              $dom);
 6332: 
 6333:     my %selfcreatetypes = (
 6334:                              sso   => 'users authenticated by institutional single sign on',
 6335:                              login => 'users authenticated by institutional log-in',
 6336:                              email => 'users who provide a valid e-mail address for use as the username',
 6337:                           );
 6338:     if ($putresult eq 'ok') {
 6339:         if (keys(%changes) > 0) {
 6340:             $resulttext = &mt('Changes made:').'<ul>';
 6341:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 6342:                 my %lt = &usercreation_types();
 6343:                 foreach my $type (@{$changes{'cancreate'}}) {
 6344:                     my $chgtext;
 6345:                     unless ($type eq 'statustocreate') {
 6346:                         $chgtext = $lt{$type}.', ';
 6347:                     }
 6348:                     if ($type eq 'selfcreate') {
 6349:                         if (@{$cancreate{$type}} == 0) {
 6350:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 6351:                         } else {
 6352:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 6353:                             foreach my $case (@{$cancreate{$type}}) {
 6354:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 6355:                             }
 6356:                             $chgtext .= '</ul>';
 6357:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 6358:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 6359:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 6360:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 6361:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6362:                                         }
 6363:                                     }
 6364:                                 }
 6365:                             }
 6366:                         }
 6367:                     } elsif ($type eq 'statustocreate') {
 6368:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 6369:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 6370:                             if (@{$cancreate{'selfcreate'}} > 0) {
 6371:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 6372: 
 6373:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 6374:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 6375:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6376:                                     } 
 6377:                                 } elsif (ref($usertypes) eq 'HASH') {
 6378:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6379:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 6380:                                     } else {
 6381:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 6382:                                     }
 6383:                                     $chgtext .= '<ul>';
 6384:                                     foreach my $case (@{$cancreate{$type}}) {
 6385:                                         if ($case eq 'default') {
 6386:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 6387:                                         } else {
 6388:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 6389:                                         }
 6390:                                     }
 6391:                                     $chgtext .= '</ul>';
 6392:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6393:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 6394:                                     }
 6395:                                 }
 6396:                             } else {
 6397:                                 if (@{$cancreate{$type}} == 0) {
 6398:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 6399:                                 } else {
 6400:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 6401:                                 }
 6402:                             }
 6403:                         }
 6404:                     } else {
 6405:                         if ($cancreate{$type} eq 'none') {
 6406:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 6407:                         } elsif ($cancreate{$type} eq 'any') {
 6408:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 6409:                         } elsif ($cancreate{$type} eq 'official') {
 6410:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 6411:                         } elsif ($cancreate{$type} eq 'unofficial') {
 6412:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 6413:                         }
 6414:                     }
 6415:                     $resulttext .= '<li>'.$chgtext.'</li>';
 6416:                 }
 6417:             }
 6418:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 6419:                 my ($rules,$ruleorder) = 
 6420:                     &Apache::lonnet::inst_userrules($dom,'username');
 6421:                 my $chgtext = '<ul>';
 6422:                 foreach my $type (@username_rule) {
 6423:                     if (ref($rules->{$type}) eq 'HASH') {
 6424:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 6425:                     }
 6426:                 }
 6427:                 $chgtext .= '</ul>';
 6428:                 if (@username_rule > 0) {
 6429:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 6430:                 } else {
 6431:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 6432:                 }
 6433:             }
 6434:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 6435:                 my ($idrules,$idruleorder) = 
 6436:                     &Apache::lonnet::inst_userrules($dom,'id');
 6437:                 my $chgtext = '<ul>';
 6438:                 foreach my $type (@id_rule) {
 6439:                     if (ref($idrules->{$type}) eq 'HASH') {
 6440:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 6441:                     }
 6442:                 }
 6443:                 $chgtext .= '</ul>';
 6444:                 if (@id_rule > 0) {
 6445:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 6446:                 } else {
 6447:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 6448:                 }
 6449:             }
 6450:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 6451:                 my ($emailrules,$emailruleorder) =
 6452:                     &Apache::lonnet::inst_userrules($dom,'email');
 6453:                 my $chgtext = '<ul>';
 6454:                 foreach my $type (@email_rule) {
 6455:                     if (ref($emailrules->{$type}) eq 'HASH') {
 6456:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 6457:                     }
 6458:                 }
 6459:                 $chgtext .= '</ul>';
 6460:                 if (@email_rule > 0) {
 6461:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 6462:                 } else {
 6463:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 6464:                 }
 6465:             }
 6466: 
 6467:             my %authname = &authtype_names();
 6468:             my %context_title = &context_names();
 6469:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 6470:                 my $chgtext = '<ul>';
 6471:                 foreach my $type (@{$changes{'authtypes'}}) {
 6472:                     my @allowed;
 6473:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 6474:                     foreach my $auth (@authtypes) {
 6475:                         if ($authhash{$type}{$auth}) {
 6476:                             push(@allowed,$authname{$auth});
 6477:                         }
 6478:                     }
 6479:                     if (@allowed > 0) {
 6480:                         $chgtext .= join(', ',@allowed).'</li>';
 6481:                     } else {
 6482:                         $chgtext .= &mt('none').'</li>';
 6483:                     }
 6484:                 }
 6485:                 $chgtext .= '</ul>';
 6486:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 6487:                 $resulttext .= '</li>';
 6488:             }
 6489:             $resulttext .= '</ul>';
 6490:         } else {
 6491:             $resulttext = &mt('No changes made to user creation settings');
 6492:         }
 6493:     } else {
 6494:         $resulttext = '<span class="LC_error">'.
 6495:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6496:     }
 6497:     if ($warningmsg ne '') {
 6498:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 6499:     }
 6500:     return $resulttext;
 6501: }
 6502: 
 6503: sub modify_usermodification {
 6504:     my ($dom,%domconfig) = @_;
 6505:     my ($resulttext,%curr_usermodification,%changes);
 6506:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 6507:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 6508:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 6509:         }
 6510:     }
 6511:     my @contexts = ('author','course','selfcreate');
 6512:     my %context_title = (
 6513:                            author => 'In author context',
 6514:                            course => 'In course context',
 6515:                            selfcreate => 'When self creating account', 
 6516:                         );
 6517:     my @fields = ('lastname','firstname','middlename','generation',
 6518:                   'permanentemail','id');
 6519:     my %roles = (
 6520:                   author => ['ca','aa'],
 6521:                   course => ['st','ep','ta','in','cr'],
 6522:                 );
 6523:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6524:     if (ref($types) eq 'ARRAY') {
 6525:         push(@{$types},'default');
 6526:         $usertypes->{'default'} = $othertitle;
 6527:     }
 6528:     $roles{'selfcreate'} = $types;  
 6529:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6530:     my %modifyhash;
 6531:     foreach my $context (@contexts) {
 6532:         foreach my $role (@{$roles{$context}}) {
 6533:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 6534:             foreach my $item (@fields) {
 6535:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 6536:                     $modifyhash{$context}{$role}{$item} = 1;
 6537:                 } else {
 6538:                     $modifyhash{$context}{$role}{$item} = 0;
 6539:                 }
 6540:             }
 6541:         }
 6542:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 6543:             foreach my $role (@{$roles{$context}}) {
 6544:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 6545:                     foreach my $field (@fields) {
 6546:                         if ($modifyhash{$context}{$role}{$field} ne 
 6547:                                 $curr_usermodification{$context}{$role}{$field}) {
 6548:                             push(@{$changes{$context}},$role);
 6549:                             last;
 6550:                         }
 6551:                     }
 6552:                 }
 6553:             }
 6554:         } else {
 6555:             foreach my $context (@contexts) {
 6556:                 foreach my $role (@{$roles{$context}}) {
 6557:                     push(@{$changes{$context}},$role);
 6558:                 }
 6559:             }
 6560:         }
 6561:     }
 6562:     my %usermodification_hash =  (
 6563:                                    usermodification => \%modifyhash,
 6564:                                  );
 6565:     my $putresult = &Apache::lonnet::put_dom('configuration',
 6566:                                              \%usermodification_hash,$dom);
 6567:     if ($putresult eq 'ok') {
 6568:         if (keys(%changes) > 0) {
 6569:             $resulttext = &mt('Changes made: ').'<ul>';
 6570:             foreach my $context (@contexts) {
 6571:                 if (ref($changes{$context}) eq 'ARRAY') {
 6572:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 6573:                     if (ref($changes{$context}) eq 'ARRAY') {
 6574:                         foreach my $role (@{$changes{$context}}) {
 6575:                             my $rolename;
 6576:                             if ($context eq 'selfcreate') {
 6577:                                 $rolename = $role;
 6578:                                 if (ref($usertypes) eq 'HASH') {
 6579:                                     if ($usertypes->{$role} ne '') {
 6580:                                         $rolename = $usertypes->{$role};
 6581:                                     }
 6582:                                 }
 6583:                             } else {
 6584:                                 if ($role eq 'cr') {
 6585:                                     $rolename = &mt('Custom');
 6586:                                 } else {
 6587:                                     $rolename = &Apache::lonnet::plaintext($role);
 6588:                                 }
 6589:                             }
 6590:                             my @modifiable;
 6591:                             if ($context eq 'selfcreate') {
 6592:                                 $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): ');
 6593:                             } else {
 6594:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 6595:                             }
 6596:                             foreach my $field (@fields) {
 6597:                                 if ($modifyhash{$context}{$role}{$field}) {
 6598:                                     push(@modifiable,$fieldtitles{$field});
 6599:                                 }
 6600:                             }
 6601:                             if (@modifiable > 0) {
 6602:                                 $resulttext .= join(', ',@modifiable);
 6603:                             } else {
 6604:                                 $resulttext .= &mt('none'); 
 6605:                             }
 6606:                             $resulttext .= '</li>';
 6607:                         }
 6608:                         $resulttext .= '</ul></li>';
 6609:                     }
 6610:                 }
 6611:             }
 6612:             $resulttext .= '</ul>';
 6613:         } else {
 6614:             $resulttext = &mt('No changes made to user modification settings');
 6615:         }
 6616:     } else {
 6617:         $resulttext = '<span class="LC_error">'.
 6618:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6619:     }
 6620:     return $resulttext;
 6621: }
 6622: 
 6623: sub modify_defaults {
 6624:     my ($dom,$r) = @_;
 6625:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 6626:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6627:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 6628:     my @authtypes = ('internal','krb4','krb5','localauth');
 6629:     foreach my $item (@items) {
 6630:         $newvalues{$item} = $env{'form.'.$item};
 6631:         if ($item eq 'auth_def') {
 6632:             if ($newvalues{$item} ne '') {
 6633:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 6634:                     push(@errors,$item);
 6635:                 }
 6636:             }
 6637:         } elsif ($item eq 'lang_def') {
 6638:             if ($newvalues{$item} ne '') {
 6639:                 if ($newvalues{$item} =~ /^(\w+)/) {
 6640:                     my $langcode = $1;
 6641:                     if ($langcode ne 'x_chef') {
 6642:                         if (code2language($langcode) eq '') {
 6643:                             push(@errors,$item);
 6644:                         }
 6645:                     }
 6646:                 } else {
 6647:                     push(@errors,$item);
 6648:                 }
 6649:             }
 6650:         } elsif ($item eq 'timezone_def') {
 6651:             if ($newvalues{$item} ne '') {
 6652:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 6653:                     push(@errors,$item);   
 6654:                 }
 6655:             }
 6656:         } elsif ($item eq 'datelocale_def') {
 6657:             if ($newvalues{$item} ne '') {
 6658:                 my @datelocale_ids = DateTime::Locale->ids();
 6659:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 6660:                     push(@errors,$item);
 6661:                 }
 6662:             }
 6663:         } elsif ($item eq 'portal_def') {
 6664:             if ($newvalues{$item} ne '') {
 6665:                 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])\/?$/) {
 6666:                     push(@errors,$item);
 6667:                 }
 6668:             }
 6669:         }
 6670:         if (grep(/^\Q$item\E$/,@errors)) {
 6671:             $newvalues{$item} = $domdefaults{$item};
 6672:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 6673:             $changes{$item} = 1;
 6674:         }
 6675:         $domdefaults{$item} = $newvalues{$item};
 6676:     }
 6677:     my %defaults_hash = (
 6678:                          defaults => \%newvalues,
 6679:                         );
 6680:     my $title = &defaults_titles();
 6681:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 6682:                                              $dom);
 6683:     if ($putresult eq 'ok') {
 6684:         if (keys(%changes) > 0) {
 6685:             $resulttext = &mt('Changes made:').'<ul>';
 6686:             my $version = $r->dir_config('lonVersion');
 6687:             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";
 6688:             foreach my $item (sort(keys(%changes))) {
 6689:                 my $value = $env{'form.'.$item};
 6690:                 if ($value eq '') {
 6691:                     $value = &mt('none');
 6692:                 } elsif ($item eq 'auth_def') {
 6693:                     my %authnames = &authtype_names();
 6694:                     my %shortauth = (
 6695:                              internal => 'int',
 6696:                              krb4 => 'krb4',
 6697:                              krb5 => 'krb5',
 6698:                              localauth  => 'loc',
 6699:                     );
 6700:                     $value = $authnames{$shortauth{$value}};
 6701:                 }
 6702:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 6703:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 6704:             }
 6705:             $resulttext .= '</ul>';
 6706:             $mailmsgtext .= "\n";
 6707:             my $cachetime = 24*60*60;
 6708:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6709:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 6710:                 my $sysmail = $r->dir_config('lonSysEMail');
 6711:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 6712:             }
 6713:         } else {
 6714:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 6715:         }
 6716:     } else {
 6717:         $resulttext = '<span class="LC_error">'.
 6718:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6719:     }
 6720:     if (@errors > 0) {
 6721:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 6722:         foreach my $item (@errors) {
 6723:             $resulttext .= ' "'.$title->{$item}.'",';
 6724:         }
 6725:         $resulttext =~ s/,$//;
 6726:     }
 6727:     return $resulttext;
 6728: }
 6729: 
 6730: sub modify_scantron {
 6731:     my ($r,$dom,$confname,%domconfig) = @_;
 6732:     my ($resulttext,%confhash,%changes,$errors);
 6733:     my $custom = 'custom.tab';
 6734:     my $default = 'default.tab';
 6735:     my $servadm = $r->dir_config('lonAdmEMail');
 6736:     my ($configuserok,$author_ok,$switchserver) = 
 6737:         &config_check($dom,$confname,$servadm);
 6738:     if ($env{'form.scantronformat.filename'} ne '') {
 6739:         my $error;
 6740:         if ($configuserok eq 'ok') {
 6741:             if ($switchserver) {
 6742:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 6743:             } else {
 6744:                 if ($author_ok eq 'ok') {
 6745:                     my ($result,$scantronurl) =
 6746:                         &publishlogo($r,'upload','scantronformat',$dom,
 6747:                                      $confname,'scantron','','',$custom);
 6748:                     if ($result eq 'ok') {
 6749:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 6750:                         $changes{'scantronformat'} = 1;
 6751:                     } else {
 6752:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 6753:                     }
 6754:                 } else {
 6755:                     $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);
 6756:                 }
 6757:             }
 6758:         } else {
 6759:             $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);
 6760:         }
 6761:         if ($error) {
 6762:             &Apache::lonnet::logthis($error);
 6763:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6764:         }
 6765:     }
 6766:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 6767:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 6768:             if ($env{'form.scantronformat_del'}) {
 6769:                 $confhash{'scantron'}{'scantronformat'} = '';
 6770:                 $changes{'scantronformat'} = 1;
 6771:             }
 6772:         }
 6773:     }
 6774:     if (keys(%confhash) > 0) {
 6775:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 6776:                                                  $dom);
 6777:         if ($putresult eq 'ok') {
 6778:             if (keys(%changes) > 0) {
 6779:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 6780:                     $resulttext = &mt('Changes made:').'<ul>';
 6781:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 6782:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 6783:                     } else {
 6784:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 6785:                     }
 6786:                     $resulttext .= '</ul>';
 6787:                 } else {
 6788:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 6789:                 }
 6790:                 $resulttext .= '</ul>';
 6791:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6792:             } else {
 6793:                 $resulttext = &mt('No changes made to bubblesheet format file');
 6794:             }
 6795:         } else {
 6796:             $resulttext = '<span class="LC_error">'.
 6797:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 6798:         }
 6799:     } else {
 6800:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 6801:     }
 6802:     if ($errors) {
 6803:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6804:                        $errors.'</ul>';
 6805:     }
 6806:     return $resulttext;
 6807: }
 6808: 
 6809: sub modify_coursecategories {
 6810:     my ($dom,%domconfig) = @_;
 6811:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 6812:         $cathash);
 6813:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 6814:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 6815:         $cathash = $domconfig{'coursecategories'}{'cats'};
 6816:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 6817:             $changes{'togglecats'} = 1;
 6818:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 6819:         }
 6820:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 6821:             $changes{'categorize'} = 1;
 6822:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 6823:         }
 6824:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 6825:             $changes{'togglecatscomm'} = 1;
 6826:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 6827:         }
 6828:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 6829:             $changes{'categorizecomm'} = 1;
 6830:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 6831:         }
 6832:     } else {
 6833:         $changes{'togglecats'} = 1;
 6834:         $changes{'categorize'} = 1;
 6835:         $changes{'togglecatscomm'} = 1;
 6836:         $changes{'categorizecomm'} = 1;
 6837:         $domconfig{'coursecategories'} = {
 6838:                                              togglecats => $env{'form.togglecats'},
 6839:                                              categorize => $env{'form.categorize'},
 6840:                                              togglecatscomm => $env{'form.togglecatscomm'},
 6841:                                              categorizecomm => $env{'form.categorizecomm'},
 6842:                                          };
 6843:     }
 6844:     if (ref($cathash) eq 'HASH') {
 6845:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 6846:             push (@deletecategory,'instcode::0');
 6847:         }
 6848:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 6849:             push(@deletecategory,'communities::0');
 6850:         }
 6851:     }
 6852:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 6853:     if (ref($cathash) eq 'HASH') {
 6854:         if (@deletecategory > 0) {
 6855:             #FIXME Need to remove category from all courses using a deleted category 
 6856:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 6857:             foreach my $item (@deletecategory) {
 6858:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 6859:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 6860:                     $deletions{$item} = 1;
 6861:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 6862:                 }
 6863:             }
 6864:         }
 6865:         foreach my $item (keys(%{$cathash})) {
 6866:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 6867:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 6868:                 $reorderings{$item} = 1;
 6869:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 6870:             }
 6871:             if ($env{'form.addcategory_name_'.$item} ne '') {
 6872:                 my $newcat = $env{'form.addcategory_name_'.$item};
 6873:                 my $newdepth = $depth+1;
 6874:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 6875:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 6876:                 $adds{$newitem} = 1; 
 6877:             }
 6878:             if ($env{'form.subcat_'.$item} ne '') {
 6879:                 my $newcat = $env{'form.subcat_'.$item};
 6880:                 my $newdepth = $depth+1;
 6881:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 6882:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 6883:                 $adds{$newitem} = 1;
 6884:             }
 6885:         }
 6886:     }
 6887:     if ($env{'form.instcode'} eq '1') {
 6888:         if (ref($cathash) eq 'HASH') {
 6889:             my $newitem = 'instcode::0';
 6890:             if ($cathash->{$newitem} eq '') {  
 6891:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 6892:                 $adds{$newitem} = 1;
 6893:             }
 6894:         } else {
 6895:             my $newitem = 'instcode::0';
 6896:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 6897:             $adds{$newitem} = 1;
 6898:         }
 6899:     }
 6900:     if ($env{'form.communities'} eq '1') {
 6901:         if (ref($cathash) eq 'HASH') {
 6902:             my $newitem = 'communities::0';
 6903:             if ($cathash->{$newitem} eq '') {
 6904:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 6905:                 $adds{$newitem} = 1;
 6906:             }
 6907:         } else {
 6908:             my $newitem = 'communities::0';
 6909:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 6910:             $adds{$newitem} = 1;
 6911:         }
 6912:     }
 6913:     if ($env{'form.addcategory_name'} ne '') {
 6914:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 6915:             ($env{'form.addcategory_name'} ne 'communities')) {
 6916:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 6917:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 6918:             $adds{$newitem} = 1;
 6919:         }
 6920:     }
 6921:     my $putresult;
 6922:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 6923:         if (keys(%deletions) > 0) {
 6924:             foreach my $key (keys(%deletions)) {
 6925:                 if ($predelallitems{$key} ne '') {
 6926:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 6927:                 }
 6928:             }
 6929:         }
 6930:         my (@chkcats,@chktrails,%chkallitems);
 6931:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 6932:         if (ref($chkcats[0]) eq 'ARRAY') {
 6933:             my $depth = 0;
 6934:             my $chg = 0;
 6935:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 6936:                 my $name = $chkcats[0][$i];
 6937:                 my $item;
 6938:                 if ($name eq '') {
 6939:                     $chg ++;
 6940:                 } else {
 6941:                     $item = &escape($name).'::0';
 6942:                     if ($chg) {
 6943:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 6944:                     }
 6945:                     $depth ++; 
 6946:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 6947:                     $depth --;
 6948:                 }
 6949:             }
 6950:         }
 6951:     }
 6952:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 6953:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 6954:         if ($putresult eq 'ok') {
 6955:             my %title = (
 6956:                          togglecats     => 'Show/Hide a course in catalog',
 6957:                          categorize     => 'Assign a category to a course',
 6958:                          togglecatscomm => 'Show/Hide a community in catalog',
 6959:                          categorizecomm => 'Assign a category to a community',
 6960:                         );
 6961:             my %level = (
 6962:                          dom  => 'set in Domain ("Modify Course/Community")',
 6963:                          crs  => 'set in Course ("Course Configuration")',
 6964:                          comm => 'set in Community ("Community Configuration")',
 6965:                         );
 6966:             $resulttext = &mt('Changes made:').'<ul>';
 6967:             if ($changes{'togglecats'}) {
 6968:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 6969:             }
 6970:             if ($changes{'categorize'}) {
 6971:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 6972:             }
 6973:             if ($changes{'togglecatscomm'}) {
 6974:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 6975:             }
 6976:             if ($changes{'categorizecomm'}) {
 6977:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 6978:             }
 6979:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 6980:                 my $cathash;
 6981:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 6982:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 6983:                 } else {
 6984:                     $cathash = {};
 6985:                 } 
 6986:                 my (@cats,@trails,%allitems);
 6987:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 6988:                 if (keys(%deletions) > 0) {
 6989:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 6990:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 6991:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 6992:                     }
 6993:                     $resulttext .= '</ul></li>';
 6994:                 }
 6995:                 if (keys(%reorderings) > 0) {
 6996:                     my %sort_by_trail;
 6997:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 6998:                     foreach my $key (keys(%reorderings)) {
 6999:                         if ($allitems{$key} ne '') {
 7000:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7001:                         }
 7002:                     }
 7003:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7004:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7005:                     }
 7006:                     $resulttext .= '</ul></li>';
 7007:                 }
 7008:                 if (keys(%adds) > 0) {
 7009:                     my %sort_by_trail;
 7010:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7011:                     foreach my $key (keys(%adds)) {
 7012:                         if ($allitems{$key} ne '') {
 7013:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7014:                         }
 7015:                     }
 7016:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7017:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7018:                     }
 7019:                     $resulttext .= '</ul></li>';
 7020:                 }
 7021:             }
 7022:             $resulttext .= '</ul>';
 7023:         } else {
 7024:             $resulttext = '<span class="LC_error">'.
 7025:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7026:         }
 7027:     } else {
 7028:         $resulttext = &mt('No changes made to course and community categories');
 7029:     }
 7030:     return $resulttext;
 7031: }
 7032: 
 7033: sub modify_serverstatuses {
 7034:     my ($dom,%domconfig) = @_;
 7035:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7036:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7037:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7038:     }
 7039:     my @pages = &serverstatus_pages();
 7040:     foreach my $type (@pages) {
 7041:         $newserverstatus{$type}{'namedusers'} = '';
 7042:         $newserverstatus{$type}{'machines'} = '';
 7043:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7044:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7045:             my @okusers;
 7046:             foreach my $user (@users) {
 7047:                 my ($uname,$udom) = split(/:/,$user);
 7048:                 if (($udom =~ /^$match_domain$/) &&   
 7049:                     (&Apache::lonnet::domain($udom)) &&
 7050:                     ($uname =~ /^$match_username$/)) {
 7051:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7052:                         push(@okusers,$user);
 7053:                     }
 7054:                 }
 7055:             }
 7056:             if (@okusers > 0) {
 7057:                  @okusers = sort(@okusers);
 7058:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7059:             }
 7060:         }
 7061:         if (defined($env{'form.'.$type.'_machines'})) {
 7062:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7063:             my @okmachines;
 7064:             foreach my $ip (@machines) {
 7065:                 my @parts = split(/\./,$ip);
 7066:                 next if (@parts < 4);
 7067:                 my $badip = 0;
 7068:                 for (my $i=0; $i<4; $i++) {
 7069:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7070:                         $badip = 1;
 7071:                         last;
 7072:                     }
 7073:                 }
 7074:                 if (!$badip) {
 7075:                     push(@okmachines,$ip);     
 7076:                 }
 7077:             }
 7078:             @okmachines = sort(@okmachines);
 7079:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7080:         }
 7081:     }
 7082:     my %serverstatushash =  (
 7083:                                 serverstatuses => \%newserverstatus,
 7084:                             );
 7085:     foreach my $type (@pages) {
 7086:         foreach my $setting ('namedusers','machines') {
 7087:             my (@current,@new);
 7088:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7089:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7090:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7091:                 }
 7092:             }
 7093:             if ($newserverstatus{$type}{$setting} ne '') {
 7094:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7095:             }
 7096:             if (@current > 0) {
 7097:                 if (@new > 0) {
 7098:                     foreach my $item (@current) {
 7099:                         if (!grep(/^\Q$item\E$/,@new)) {
 7100:                             $changes{$type}{$setting} = 1;
 7101:                             last;
 7102:                         }
 7103:                     }
 7104:                     foreach my $item (@new) {
 7105:                         if (!grep(/^\Q$item\E$/,@current)) {
 7106:                             $changes{$type}{$setting} = 1;
 7107:                             last;
 7108:                         }
 7109:                     }
 7110:                 } else {
 7111:                     $changes{$type}{$setting} = 1;
 7112:                 }
 7113:             } elsif (@new > 0) {
 7114:                 $changes{$type}{$setting} = 1;
 7115:             }
 7116:         }
 7117:     }
 7118:     if (keys(%changes) > 0) {
 7119:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7120:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7121:                                                  \%serverstatushash,$dom);
 7122:         if ($putresult eq 'ok') {
 7123:             $resulttext .= &mt('Changes made:').'<ul>';
 7124:             foreach my $type (@pages) {
 7125:                 if (ref($changes{$type}) eq 'HASH') {
 7126:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7127:                     if ($changes{$type}{'namedusers'}) {
 7128:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7129:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7130:                         } else {
 7131:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7132:                         }
 7133:                     }
 7134:                     if ($changes{$type}{'machines'}) {
 7135:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7136:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7137:                         } else {
 7138:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7139:                         }
 7140: 
 7141:                     }
 7142:                     $resulttext .= '</ul></li>';
 7143:                 }
 7144:             }
 7145:             $resulttext .= '</ul>';
 7146:         } else {
 7147:             $resulttext = '<span class="LC_error">'.
 7148:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7149: 
 7150:         }
 7151:     } else {
 7152:         $resulttext = &mt('No changes made to access to server status pages');
 7153:     }
 7154:     return $resulttext;
 7155: }
 7156: 
 7157: sub modify_helpsettings {
 7158:     my ($r,$dom,$confname,%domconfig) = @_;
 7159:  	my ($resulttext,$errors,%changes,%helphash);
 7160:  	
 7161:  	my $customhelpfile  = $env{'form.loginhelpurl.filename'};
 7162:     my $defaulthelpfile = 'defaulthelp.html';
 7163:  	my $servadm = $r->dir_config('lonAdmEMail');
 7164:     my ($configuserok,$author_ok,$switchserver) = 
 7165:         &config_check($dom,$confname,$servadm);
 7166:  	
 7167:  	my %defaultchecked = ('submitbugs'	=> 'on');
 7168:  	my @offon = ('off','on');
 7169:     my %title = ( submitbugs     => 'Display link for users to submit a bug', 
 7170:     			  loginhelpurl  => 'Unauthenticated login help page set to custom file');
 7171:     			  
 7172:     my @toggles = ('submitbugs');
 7173: 
 7174:     $helphash{'helpsettings'} = {};
 7175:     
 7176:     if (ref($domconfig{'helpsettings'}) ne 'HASH') {
 7177:         if ($domconfig{'helpsettings'} eq '') {
 7178:             $domconfig{'helpsettings'} = {};
 7179:         }
 7180:     }
 7181:     
 7182:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7183:     	
 7184:         foreach my $item (@toggles) {
 7185:         	
 7186: 			if ($defaultchecked{$item} eq 'on') { 
 7187: 				if (($domconfig{'helpsettings'}{$item} eq '') &&
 7188: 					 ($env{'form.'.$item} eq '0')) {
 7189: 					$changes{$item} = 1;
 7190: 				} elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7191: 					$changes{$item} = 1;
 7192: 				}
 7193: 			} elsif ($defaultchecked{$item} eq 'off') {
 7194: 				if (($domconfig{'helpsettings'}{$item} eq '') &&
 7195: 					 ($env{'form.'.$item} eq '1')) {
 7196: 					$changes{$item} = 1;
 7197: 				} elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7198: 					$changes{$item} = 1;
 7199: 				}
 7200: 			}
 7201: 			$helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7202: 		}
 7203: 		
 7204: 		if ($customhelpfile ne '') {
 7205: 			my $error;
 7206: 			if ($configuserok eq 'ok') {
 7207: 				if ($switchserver) {
 7208: 					$error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7209: 				} else {
 7210: 					if ($author_ok eq 'ok') {
 7211: 						my ($result,$loginhelpurl) =
 7212: 							&publishlogo($r,'upload','loginhelpurl',$dom,
 7213: 										 $confname,'help','','',$customhelpfile);
 7214: 						if ($result eq 'ok') {
 7215: 							$helphash{'helpsettings'}{'loginhelpurl'} = $loginhelpurl;
 7216: 							$changes{'loginhelpurl'} = 1;
 7217: 						} else {
 7218: 							$error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customhelpfile,$result);
 7219: 						}
 7220: 					} else {
 7221: 						$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].",$customhelpfile,$confname,$dom,$author_ok);
 7222: 					}
 7223: 				}
 7224: 			} else {
 7225: 				$error = &mt("Upload of [_1] failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$customhelpfile,$confname,$dom,$configuserok);
 7226: 			}
 7227: 			if ($error) {
 7228: 				&Apache::lonnet::logthis($error);
 7229: 				$errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7230: 			}
 7231: 		}
 7232: 		
 7233:         if ($domconfig{'helpsettings'}{'loginhelpurl'} ne '') {
 7234:             if ($env{'form.loginhelpurl_del'}) {
 7235:                 $helphash{'helpsettings'}{'loginhelpurl'} = '';
 7236:                 $changes{'loginhelpurl'} = 1;
 7237:             }
 7238:         }
 7239:     }
 7240:     
 7241:     
 7242:     my $putresult;
 7243:     
 7244:     if (keys(%changes) > 0) {
 7245:     	$putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7246:     } else {
 7247:     	$putresult = 'ok';
 7248:     }
 7249:                                              
 7250:     if ($putresult eq 'ok') {
 7251:         if (keys(%changes) > 0) {
 7252: 			$resulttext = &mt('Changes made:').'<ul>';
 7253: 			foreach my $item (sort(keys(%changes))) {
 7254: 				if ($item eq 'submitbugs') {
 7255: 					$resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7256: 				}
 7257: 				if ($item eq 'loginhelpurl') {
 7258: 					if ($helphash{'helpsettings'}{'loginhelpurl'} eq '') {
 7259:                         $resulttext .= '<li>'.&mt('[_1] help file removed; [_2] file will be used for the unathorized help page in this domain.',$customhelpfile,$defaulthelpfile).'</li>';
 7260:                     } else {
 7261:                         $resulttext .= '<li>'.&mt("$title{$item} [_1]",$customhelpfile).'</li>';
 7262:                     }
 7263: 				}
 7264: 			}
 7265: 			$resulttext .= '</ul>';
 7266: 		} else {
 7267: 			$resulttext = &mt('No changes made to help settings');
 7268: 		}
 7269:     } else {
 7270:         $resulttext = '<span class="LC_error">'.
 7271: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7272:     }
 7273:     if ($errors) {
 7274:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7275:                        $errors.'</ul>';
 7276:     }
 7277:     return $resulttext;
 7278: }
 7279: 
 7280: sub modify_coursedefaults {
 7281:     my ($dom,%domconfig) = @_;
 7282:     my ($resulttext,$errors,%changes,%defaultshash);
 7283:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7284:     my @offon = ('off','on');
 7285:     my @toggles = ('canuse_pdfforms');
 7286: 
 7287:     $defaultshash{'coursedefaults'} = {};
 7288: 
 7289:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7290:         if ($domconfig{'coursedefaults'} eq '') {
 7291:             $domconfig{'coursedefaults'} = {};
 7292:         }
 7293:     }
 7294: 
 7295:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7296:         foreach my $item (@toggles) {
 7297:             if ($defaultchecked{$item} eq 'on') {
 7298:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7299:                     ($env{'form.'.$item} eq '0')) {
 7300:                     $changes{$item} = 1;
 7301:                 } elsif ($domconfig{'coursdefaults'}{$item} ne $env{'form.'.$item}) {
 7302:                     $changes{$item} = 1;
 7303:                 }
 7304:             } elsif ($defaultchecked{$item} eq 'off') {
 7305:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7306:                     ($env{'form.'.$item} eq '1')) {
 7307:                     $changes{$item} = 1;
 7308:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7309:                     $changes{$item} = 1;
 7310:                 }
 7311:             }
 7312:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7313:         }
 7314:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7315:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7316:         $newdefresponder =~ s/\D//g;
 7317:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7318:             $newdefresponder = 1;
 7319:         }
 7320:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7321:         if ($currdefresponder ne $newdefresponder) {
 7322:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7323:                 $changes{'anonsurvey_threshold'} = 1;
 7324:             }
 7325:         }
 7326:     }
 7327:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7328:                                              $dom);
 7329:     if ($putresult eq 'ok') {
 7330:         if (keys(%changes) > 0) {
 7331:             if ($changes{'canuse_pdfforms'}) {
 7332:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7333:                 $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7334:                 my $cachetime = 24*60*60;
 7335:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7336:             }
 7337:             $resulttext = &mt('Changes made:').'<ul>';
 7338:             foreach my $item (sort(keys(%changes))) {
 7339:                 if ($item eq 'canuse_pdfforms') {
 7340:                     if ($env{'form.'.$item} eq '1') {
 7341:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7342:                     } else {
 7343:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7344:                     }
 7345:                 } elsif ($item eq 'anonsurvey_threshold') {
 7346:                         $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7347:                 }
 7348:             }
 7349:             $resulttext .= '</ul>';
 7350:         } else {
 7351:             $resulttext = &mt('No changes made to course defaults');
 7352:         }
 7353:     } else {
 7354:         $resulttext = '<span class="LC_error">'.
 7355:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7356:     }
 7357:     return $resulttext;
 7358: }
 7359: 
 7360: sub modify_usersessions {
 7361:     my ($dom,%domconfig) = @_;
 7362:     my @hostingtypes = ('version','excludedomain','includedomain');
 7363:     my @offloadtypes = ('primary','default');
 7364:     my %types = (
 7365:                   remote => \@hostingtypes,
 7366:                   hosted => \@hostingtypes,
 7367:                   spares => \@offloadtypes,
 7368:                 );
 7369:     my @prefixes = ('remote','hosted','spares');
 7370:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7371:     my (%by_ip,%by_location,@intdoms);
 7372:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7373:     my @locations = sort(keys(%by_location));
 7374:     my (%defaultshash,%changes);
 7375:     foreach my $prefix (@prefixes) {
 7376:         $defaultshash{'usersessions'}{$prefix} = {};
 7377:     }
 7378:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7379:     my $resulttext;
 7380:     my %iphost = &Apache::lonnet::get_iphost();
 7381:     foreach my $prefix (@prefixes) {
 7382:         next if ($prefix eq 'spares');
 7383:         foreach my $type (@{$types{$prefix}}) {
 7384:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7385:             if ($type eq 'version') {
 7386:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7387:                 my $okvalue;
 7388:                 if ($value ne '') {
 7389:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 7390:                         $okvalue = $value;
 7391:                     }
 7392:                 }
 7393:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7394:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7395:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 7396:                             if ($inuse == 0) {
 7397:                                 $changes{$prefix}{$type} = 1;
 7398:                             } else {
 7399:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 7400:                                     $changes{$prefix}{$type} = 1;
 7401:                                 }
 7402:                                 if ($okvalue ne '') {
 7403:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7404:                                 } 
 7405:                             }
 7406:                         } else {
 7407:                             if (($inuse == 1) && ($okvalue ne '')) {
 7408:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7409:                                 $changes{$prefix}{$type} = 1;
 7410:                             }
 7411:                         }
 7412:                     } else {
 7413:                         if (($inuse == 1) && ($okvalue ne '')) {
 7414:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7415:                             $changes{$prefix}{$type} = 1;
 7416:                         }
 7417:                     }
 7418:                 } else {
 7419:                     if (($inuse == 1) && ($okvalue ne '')) {
 7420:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7421:                         $changes{$prefix}{$type} = 1;
 7422:                     }
 7423:                 }
 7424:             } else {
 7425:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 7426:                 my @okvals;
 7427:                 foreach my $val (@vals) {
 7428:                     if ($val =~ /:/) {
 7429:                         my @items = split(/:/,$val);
 7430:                         foreach my $item (@items) {
 7431:                             if (ref($by_location{$item}) eq 'ARRAY') {
 7432:                                 push(@okvals,$item);
 7433:                             }
 7434:                         }
 7435:                     } else {
 7436:                         if (ref($by_location{$val}) eq 'ARRAY') {
 7437:                             push(@okvals,$val);
 7438:                         }
 7439:                     }
 7440:                 }
 7441:                 @okvals = sort(@okvals);
 7442:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7443:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7444:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7445:                             if ($inuse == 0) {
 7446:                                 $changes{$prefix}{$type} = 1; 
 7447:                             } else {
 7448:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7449:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 7450:                                 if (@changed > 0) {
 7451:                                     $changes{$prefix}{$type} = 1;
 7452:                                 }
 7453:                             }
 7454:                         } else {
 7455:                             if ($inuse == 1) {
 7456:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7457:                                 $changes{$prefix}{$type} = 1;
 7458:                             }
 7459:                         } 
 7460:                     } else {
 7461:                         if ($inuse == 1) {
 7462:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7463:                             $changes{$prefix}{$type} = 1;
 7464:                         }
 7465:                     }
 7466:                 } else {
 7467:                     if ($inuse == 1) {
 7468:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7469:                         $changes{$prefix}{$type} = 1;
 7470:                     }
 7471:                 }
 7472:             }
 7473:         }
 7474:     }
 7475: 
 7476:     my @alldoms = &Apache::lonnet::all_domains();
 7477:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7478:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 7479:     my $savespares;
 7480: 
 7481:     foreach my $lonhost (sort(keys(%servers))) {
 7482:         my $serverhomeID =
 7483:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 7484:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 7485:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 7486:         my %spareschg;
 7487:         foreach my $type (@{$types{'spares'}}) {
 7488:             my @okspares;
 7489:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 7490:             foreach my $server (@checked) {
 7491:                 if (&Apache::lonnet::hostname($server) ne '') {
 7492:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 7493:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 7494:                             push(@okspares,$server);
 7495:                         }
 7496:                     }
 7497:                 }
 7498:             }
 7499:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 7500:             my $newspare;
 7501:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 7502:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 7503:                     $newspare = $new;
 7504:                 }
 7505:             }
 7506:             my @spares;
 7507:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 7508:                 @spares = sort(@okspares,$newspare);
 7509:             } else {
 7510:                 @spares = sort(@okspares);
 7511:             }
 7512:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 7513:             if (ref($spareid{$lonhost}) eq 'HASH') {
 7514:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 7515:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 7516:                     if (@diffs > 0) {
 7517:                         $spareschg{$type} = 1;
 7518:                     }
 7519:                 }
 7520:             }
 7521:         }
 7522:         if (keys(%spareschg) > 0) {
 7523:             $changes{'spares'}{$lonhost} = \%spareschg;
 7524:         }
 7525:     }
 7526: 
 7527:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7528:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 7529:             if (ref($changes{'spares'}) eq 'HASH') {
 7530:                 if (keys(%{$changes{'spares'}}) > 0) {
 7531:                     $savespares = 1;
 7532:                 }
 7533:             }
 7534:         } else {
 7535:             $savespares = 1;
 7536:         }
 7537:     }
 7538: 
 7539:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 7540:     if ((keys(%changes) > 0) || ($savespares)) {
 7541:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7542:                                                  $dom);
 7543:         if ($putresult eq 'ok') {
 7544:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 7545:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 7546:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 7547:                 }
 7548:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 7549:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 7550:                 }
 7551:             }
 7552:             my $cachetime = 24*60*60;
 7553:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7554:             if (keys(%changes) > 0) {
 7555:                 my %lt = &usersession_titles();
 7556:                 $resulttext = &mt('Changes made:').'<ul>';
 7557:                 foreach my $prefix (@prefixes) {
 7558:                     if (ref($changes{$prefix}) eq 'HASH') {
 7559:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 7560:                         if ($prefix eq 'spares') {
 7561:                             if (ref($changes{$prefix}) eq 'HASH') {
 7562:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 7563:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 7564:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 7565:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 7566:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 7567:                                         foreach my $type (@{$types{$prefix}}) {
 7568:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 7569:                                                 my $offloadto = &mt('None');
 7570:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 7571:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 7572:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 7573:                                                     }
 7574:                                                 }
 7575:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 7576:                                             }
 7577:                                         }
 7578:                                     }
 7579:                                     $resulttext .= '</li>';
 7580:                                 }
 7581:                             }
 7582:                         } else {
 7583:                             foreach my $type (@{$types{$prefix}}) {
 7584:                                 if (defined($changes{$prefix}{$type})) {
 7585:                                     my $newvalue;
 7586:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 7587:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 7588:                                             if ($type eq 'version') {
 7589:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 7590:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7591:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 7592:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 7593:                                                 }
 7594:                                             }
 7595:                                         }
 7596:                                     }
 7597:                                     if ($newvalue eq '') {
 7598:                                         if ($type eq 'version') {
 7599:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 7600:                                         } else {
 7601:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 7602:                                         }
 7603:                                     } else {
 7604:                                         if ($type eq 'version') {
 7605:                                             $newvalue .= ' '.&mt('(or later)'); 
 7606:                                         }
 7607:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 7608:                                     }
 7609:                                 }
 7610:                             }
 7611:                         }
 7612:                         $resulttext .= '</ul>';
 7613:                     }
 7614:                 }
 7615:                 $resulttext .= '</ul>';
 7616:             } else {
 7617:                 $resulttext = $nochgmsg;
 7618:             }
 7619:         } else {
 7620:             $resulttext = '<span class="LC_error">'.
 7621:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7622:         }
 7623:     } else {
 7624:         $resulttext = $nochgmsg;
 7625:     }
 7626:     return $resulttext;
 7627: }
 7628: 
 7629: sub modify_loadbalancing {
 7630:     my ($dom,%domconfig) = @_;
 7631:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7632:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7633:     my ($othertitle,$usertypes,$types) =
 7634:         &Apache::loncommon::sorted_inst_types($dom);
 7635:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7636:     my @sparestypes = ('primary','default');
 7637:     my %typetitles = &sparestype_titles();
 7638:     my $resulttext;
 7639:     if (keys(%servers) > 1) {
 7640:         my ($currbalancer,$currtargets,$currrules);
 7641:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 7642:             $currbalancer = $domconfig{'loadbalancing'}{'lonhost'};
 7643:             $currtargets = $domconfig{'loadbalancing'}{'targets'};
 7644:             $currrules = $domconfig{'loadbalancing'}{'rules'};
 7645:         } else {
 7646:             ($currbalancer,$currtargets) = 
 7647:                 &Apache::lonnet::get_lonbalancer_config(\%servers);
 7648:         }
 7649:         my ($saveloadbalancing,%defaultshash,%changes);
 7650:         my ($alltypes,$othertypes,$titles) =
 7651:             &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 7652:         my %ruletitles = &offloadtype_text();
 7653:         my $balancer = $env{'form.loadbalancing_lonhost'};
 7654:         if (!$servers{$balancer}) {
 7655:             undef($balancer);
 7656:         }
 7657:         if ($currbalancer ne $balancer) {
 7658:             $changes{'lonhost'} = 1;
 7659:         }
 7660:         $defaultshash{'loadbalancing'}{'lonhost'} = $balancer;
 7661:         if ($balancer ne '') {
 7662:             unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 7663:                 $saveloadbalancing = 1;
 7664:             }
 7665:             foreach my $sparetype (@sparestypes) {
 7666:                 my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$sparetype);
 7667:                 my @offloadto;
 7668:                 foreach my $target (@targets) {
 7669:                     if (($servers{$target}) && ($target ne $balancer)) {
 7670:                         if ($sparetype eq 'default') {
 7671:                             if (ref($defaultshash{'loadbalancing'}{'targets'}{'primary'}) eq 'ARRAY') {
 7672:                                 next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{'targets'}{'primary'}}));
 7673:                             }
 7674:                         }
 7675:                         unless(grep(/^\Q$target\E$/,@offloadto)) {
 7676:                             push(@offloadto,$target);
 7677:                         }
 7678:                     }
 7679:                     $defaultshash{'loadbalancing'}{'targets'}{$sparetype} = \@offloadto;
 7680:                 }
 7681:             }
 7682:         } else {
 7683:             foreach my $sparetype (@sparestypes) {
 7684:                 $defaultshash{'loadbalancing'}{'targets'}{$sparetype} = [];
 7685:             }
 7686:         }
 7687:         if (ref($currtargets) eq 'HASH') {
 7688:             foreach my $sparetype (@sparestypes) {
 7689:                 if (ref($currtargets->{$sparetype}) eq 'ARRAY') {
 7690:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets->{$sparetype},$defaultshash{'loadbalancing'}{'targets'}{$sparetype});
 7691:                     if (@targetdiffs > 0) {
 7692:                         $changes{'targets'} = 1;
 7693:                     }
 7694:                 } elsif (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 7695:                     if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 7696:                         $changes{'targets'} = 1;
 7697:                     }
 7698:                 }
 7699:             }
 7700:         } else {
 7701:             foreach my $sparetype (@sparestypes) {
 7702:                 if (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 7703:                     if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 7704:                         $changes{'targets'} = 1;  
 7705:                     }
 7706:                 }
 7707:             }  
 7708:         }
 7709:         my $ishomedom;
 7710:         if ($balancer ne '') {
 7711:             if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 7712:                 $ishomedom = 1;
 7713:             }
 7714:         }
 7715:         if (ref($alltypes) eq 'ARRAY') {
 7716:             foreach my $type (@{$alltypes}) {
 7717:                 my $rule;
 7718:                 if ($balancer ne '') {
 7719:                     unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) && 
 7720:                          (!$ishomedom)) {
 7721:                         $rule = $env{'form.loadbalancing_rules_'.$type};
 7722:                     }
 7723:                     if ($rule eq 'specific') {
 7724:                         $rule = $env{'form.loadbalancing_singleserver_'.$type};
 7725:                     }
 7726:                 }
 7727:                 $defaultshash{'loadbalancing'}{'rules'}{$type} = $rule;
 7728:                 if (ref($currrules) eq 'HASH') {
 7729:                     if ($rule ne $currrules->{$type}) {
 7730:                         $changes{'rules'}{$type} = 1;
 7731:                     }
 7732:                 } elsif ($rule ne '') {
 7733:                     $changes{'rules'}{$type} = 1;
 7734:                 }
 7735:             }
 7736:         }
 7737:         my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 7738:         if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 7739:             my $putresult = &Apache::lonnet::put_dom('configuration',
 7740:                                                      \%defaultshash,$dom);
 7741:             if ($putresult eq 'ok') {
 7742:                 if (keys(%changes) > 0) {
 7743:                     if ($changes{'lonhost'}) {
 7744:                         if ($currbalancer ne '') {
 7745:                             &Apache::lonnet::remote_devalidate_cache($currbalancer,'loadbalancing',$dom);
 7746:                         }
 7747:                         if ($balancer eq '') {
 7748:                             $resulttext .= '<li>'.&mt('Load Balancing with dedicated server discontinued').'</li>'; 
 7749:                         } else {
 7750:                             &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 7751:                             $resulttext .= '<li>'.&mt('Dedicated Load Balancer server set to [_1]',$balancer);
 7752:                         }
 7753:                     } else {
 7754:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 7755:                     }
 7756:                     if (($changes{'targets'}) && ($balancer ne '')) {
 7757:                         my %offloadstr;
 7758:                         foreach my $sparetype (@sparestypes) {
 7759:                             if (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 7760:                                 if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 7761:                                     $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}});
 7762:                                 }
 7763:                             }
 7764:                         }
 7765:                         if (keys(%offloadstr) == 0) {
 7766:                             $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 7767:                         } else {
 7768:                             my $showoffload;
 7769:                             foreach my $sparetype (@sparestypes) {
 7770:                                 $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 7771:                                 if (defined($offloadstr{$sparetype})) {
 7772:                                     $showoffload .= $offloadstr{$sparetype};
 7773:                                 } else {
 7774:                                     $showoffload .= &mt('None');
 7775:                                 }
 7776:                                 $showoffload .= ('&nbsp;'x3);
 7777:                             }
 7778:                             $resulttext .= '<li>'.&mt('By default, Load Balancer server set to offload to: [_1]',$showoffload).'</li>';
 7779:                         }
 7780:                     }
 7781:                     if ((ref($changes{'rules'}) eq 'HASH') && ($balancer ne '')) {
 7782:                         if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 7783:                             foreach my $type (@{$alltypes}) {
 7784:                                 if ($changes{'rules'}{$type}) {
 7785:                                     my $rule = $defaultshash{'loadbalancing'}{'rules'}{$type};
 7786:                                     my $balancetext;
 7787:                                     if ($rule eq '') {
 7788:                                         $balancetext =  $ruletitles{'default'};
 7789:                                     } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 7790:                                         $balancetext =  $ruletitles{$rule};
 7791:                                     } else {
 7792:                                         $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{'rules'}{$type});
 7793:                                     }
 7794:                                     $resulttext .= '<li>'.&mt('Load Balancing for [_1] set to: [_2]',$titles->{$type},$balancetext).'</li>';     
 7795:                                 }
 7796:                             }
 7797:                         }
 7798:                     }
 7799:                     if ($resulttext ne '') {
 7800:                         $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 7801:                     } else {
 7802:                         $resulttext = $nochgmsg;
 7803:                     }
 7804:                 } else {
 7805:                     $resulttext = $nochgmsg;
 7806:                     if ($balancer ne '') {
 7807:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 7808:                     }
 7809:                 }
 7810:             } else {
 7811:                 $resulttext = '<span class="LC_error">'.
 7812:                               &mt('An error occurred: [_1]',$putresult).'</span>';
 7813:             }
 7814:         } else {
 7815:             $resulttext = $nochgmsg;
 7816:         }
 7817:     } else {
 7818:         $resulttext =  &mt('Load Balancing unavailable as this domain only has one server.');
 7819:     }
 7820:     return $resulttext;
 7821: }
 7822: 
 7823: sub recurse_check {
 7824:     my ($chkcats,$categories,$depth,$name) = @_;
 7825:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 7826:         my $chg = 0;
 7827:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 7828:             my $category = $chkcats->[$depth]{$name}[$j];
 7829:             my $item;
 7830:             if ($category eq '') {
 7831:                 $chg ++;
 7832:             } else {
 7833:                 my $deeper = $depth + 1;
 7834:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 7835:                 if ($chg) {
 7836:                     $categories->{$item} -= $chg;
 7837:                 }
 7838:                 &recurse_check($chkcats,$categories,$deeper,$category);
 7839:                 $deeper --;
 7840:             }
 7841:         }
 7842:     }
 7843:     return;
 7844: }
 7845: 
 7846: sub recurse_cat_deletes {
 7847:     my ($item,$coursecategories,$deletions) = @_;
 7848:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7849:     my $subdepth = $depth + 1;
 7850:     if (ref($coursecategories) eq 'HASH') {
 7851:         foreach my $subitem (keys(%{$coursecategories})) {
 7852:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 7853:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 7854:                 delete($coursecategories->{$subitem});
 7855:                 $deletions->{$subitem} = 1;
 7856:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 7857:             }  
 7858:         }
 7859:     }
 7860:     return;
 7861: }
 7862: 
 7863: sub get_active_dcs {
 7864:     my ($dom) = @_;
 7865:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc']);
 7866:     my %domcoords;
 7867:     my $numdcs = 0;
 7868:     my $now = time;
 7869:     foreach my $server (keys(%dompersonnel)) {
 7870:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 7871:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 7872:             my ($end,$start) = split(':',$dompersonnel{$server}{$user});
 7873:             if (($end eq '') || ($end == 0) || ($end > $now)) {
 7874:                 if ($start <= $now) {
 7875:                     $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 7876:                 }
 7877:             }
 7878:         }
 7879:     }
 7880:     return %domcoords;
 7881: }
 7882: 
 7883: sub active_dc_picker {
 7884:     my ($dom,$curr_dc) = @_;
 7885:     my %domcoords = &get_active_dcs($dom); 
 7886:     my @dcs = sort(keys(%domcoords));
 7887:     my $numdcs = scalar(@dcs); 
 7888:     my $datatable;
 7889:     my $numinrow = 2;
 7890:     if ($numdcs > 1) {
 7891:         $datatable = '<table>';
 7892:         for (my $i=0; $i<@dcs; $i++) {
 7893:             my $rem = $i%($numinrow);
 7894:             if ($rem == 0) {
 7895:                 if ($i > 0) {
 7896:                     $datatable .= '</tr>';
 7897:                 }
 7898:                 $datatable .= '<tr>';
 7899:             }
 7900:             my $check = ' ';
 7901:             if ($curr_dc eq '') {
 7902:                 if (!$i) { 
 7903:                     $check = ' checked="checked" ';
 7904:                 }
 7905:             } elsif ($dcs[$i] eq $curr_dc) {
 7906:                 $check = ' checked="checked" ';
 7907:             }
 7908:             if ($i == @dcs - 1) {
 7909:                 my $colsleft = $numinrow - $rem;
 7910:                 if ($colsleft > 1) {
 7911:                     $datatable .= '<td colspan="'.$colsleft.'">';
 7912:                 } else {
 7913:                     $datatable .= '<td>';
 7914:                 }
 7915:             } else {
 7916:                 $datatable .= '<td>';
 7917:             }
 7918:             my ($dcname,$dcdom) = split(':',$dcs[$i]);
 7919:             $datatable .= '<span class="LC_nobreak"><label>'.
 7920:                           '<input type="radio" name="autocreate_xmldc"'.
 7921:                           ' value="'.$dcs[$i].'"'.$check.'/>'.
 7922:                           &Apache::loncommon::plainname($dcname,$dcdom).
 7923:                           '</label></span></td>';
 7924:         }
 7925:         $datatable .= '</tr></table>';
 7926:     } elsif (@dcs) {
 7927:         $datatable .= '<input type="hidden" name="autocreate_dc" value="'.
 7928:                       $dcs[0].'" />';
 7929:     }
 7930:     return ($numdcs,$datatable);
 7931: }
 7932: 
 7933: sub usersession_titles {
 7934:     return &Apache::lonlocal::texthash(
 7935:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 7936:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 7937:                spares => 'Servers offloaded to, when busy',
 7938:                version => 'LON-CAPA version requirement',
 7939:                excludedomain => 'Allow all, but exclude specific domains',
 7940:                includedomain => 'Deny all, but include specific domains',
 7941:                primary => 'Primary (checked first)',
 7942:                default => 'Default',
 7943:            );
 7944: }
 7945: 
 7946: sub id_for_thisdom {
 7947:     my (%servers) = @_;
 7948:     my %altids;
 7949:     foreach my $server (keys(%servers)) {
 7950:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 7951:         if ($serverhome ne $server) {
 7952:             $altids{$serverhome} = $server;
 7953:         }
 7954:     }
 7955:     return %altids;
 7956: }
 7957: 
 7958: sub count_servers {
 7959:     my ($currbalancer,%servers) = @_;
 7960:     my (@spares,$numspares);
 7961:     foreach my $lonhost (sort(keys(%servers))) {
 7962:         next if ($currbalancer eq $lonhost);
 7963:         push(@spares,$lonhost);
 7964:     }
 7965:     if ($currbalancer) {
 7966:         $numspares = scalar(@spares);
 7967:     } else {
 7968:         $numspares = scalar(@spares) - 1;
 7969:     }
 7970:     return ($numspares,@spares);
 7971: }
 7972: 
 7973: sub lonbalance_targets_js {
 7974:     my ($dom,$types,$servers) = @_;
 7975:     my $select = &mt('Select');
 7976:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 7977:     if (ref($servers) eq 'HASH') {
 7978:         $alltargets = join("','",sort(keys(%{$servers})));
 7979:         my @homedoms;
 7980:         foreach my $server (sort(keys(%{$servers}))) {
 7981:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 7982:                 push(@homedoms,'1');
 7983:             } else {
 7984:                 push(@homedoms,'0');
 7985:             }
 7986:         }
 7987:         $allishome = join("','",@homedoms);
 7988:     }
 7989:     if (ref($types) eq 'ARRAY') {
 7990:         if (@{$types} > 0) {
 7991:             @alltypes = @{$types};
 7992:         }
 7993:     }
 7994:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 7995:     $allinsttypes = join("','",@alltypes);
 7996:     return <<"END";
 7997: 
 7998: <script type="text/javascript">
 7999: // <![CDATA[
 8000: 
 8001: function toggleTargets() {
 8002:     var balancer = document.display.loadbalancing_lonhost.options[document.display.loadbalancing_lonhost.selectedIndex].value;
 8003:     if (balancer == '') {
 8004:         hideSpares();
 8005:     } else {
 8006:         var homedoms = new Array('$allishome');
 8007:         var ishomedom = homedoms[document.display.loadbalancing_lonhost.selectedIndex];
 8008:         showSpares(balancer,ishomedom);
 8009:     }
 8010:     return;
 8011: }
 8012: 
 8013: function showSpares(balancer,ishomedom) {
 8014:     var alltargets = new Array('$alltargets');
 8015:     var insttypes = new Array('$allinsttypes');
 8016:     var offloadtypes = new Array('primary','default');
 8017: 
 8018:     document.getElementById('loadbalancing_targets').style.display='block';
 8019:     document.getElementById('loadbalancing_disabled').style.display='none';
 8020:  
 8021:     for (var i=0; i<offloadtypes.length; i++) {
 8022:         var count = 0;
 8023:         for (var j=0; j<alltargets.length; j++) {
 8024:             if (alltargets[j] != balancer) {
 8025:                 document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+count).value = alltargets[j];
 8026:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).style.textAlign='left';
 8027:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).style.textFace='normal';
 8028:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8029:                 count ++;
 8030:             }
 8031:         }
 8032:     }
 8033:     for (var k=0; k<insttypes.length; k++) {
 8034:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8035:             if (ishomedom == 1) {
 8036:                 document.getElementById('balanceruletitle_'+insttypes[k]).style.display='block';
 8037:                 document.getElementById('balancerule_'+insttypes[k]).style.display='block';
 8038:             } else {
 8039:                 document.getElementById('balanceruletitle_'+insttypes[k]).style.display='none';
 8040:                 document.getElementById('balancerule_'+insttypes[k]).style.display='none';
 8041: 
 8042:             }
 8043:         } else {
 8044:             document.getElementById('balanceruletitle_'+insttypes[k]).style.display='block';
 8045:             document.getElementById('balancerule_'+insttypes[k]).style.display='block';
 8046:         }
 8047:         if ((insttypes[k] != '_LC_external') && 
 8048:             ((insttypes[k] != '_LC_internetdom') ||
 8049:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8050:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[0] = new Option("","",true,true);
 8051:             for (var m=0; m<alltargets.length; m++) {
 8052:                 var idx = m+1;
 8053:                 if (alltargets[m] != balancer) {
 8054:                     document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8055:                 }
 8056:             }
 8057:         }
 8058:     }
 8059:     return;
 8060: }
 8061: 
 8062: function hideSpares() {
 8063:     var alltargets = new Array('$alltargets');
 8064:     var insttypes = new Array('$allinsttypes');
 8065:     var offloadtypes = new Array('primary','default');
 8066: 
 8067:     document.getElementById('loadbalancing_targets').style.display='none';
 8068:     document.getElementById('loadbalancing_disabled').style.display='block';
 8069: 
 8070:     var total = alltargets.length - 1;
 8071:     for (var i=0; i<offloadtypes; i++) {
 8072:         for (var j=0; j<total; j++) {
 8073:            document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+j).checked = false;
 8074:            document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+j).value = '';
 8075:            document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8076:         }
 8077:     }
 8078:     for (var k=0; k<insttypes.length; k++) {
 8079:         document.getElementById('balanceruletitle_'+insttypes[k]).style.display='none';
 8080:         document.getElementById('balancerule_'+insttypes[k]).style.display='none';
 8081:         if (insttypes[k] != '_LC_external') {
 8082:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).length = 0;
 8083:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[0] = new Option("","",true,true);
 8084:         }
 8085:     }
 8086:     return;
 8087: }
 8088: 
 8089: function checkOffloads(item,type) {
 8090:     var alltargets = new Array('$alltargets');
 8091:     var offloadtypes = new Array('primary','default');
 8092:     if (item.checked) {
 8093:         var total = alltargets.length - 1;
 8094:         var other;
 8095:         if (type == offloadtypes[0]) {
 8096:             other = offloadtypes[1];
 8097:         } else {
 8098:             other = offloadtypes[0];
 8099:         }
 8100:         for (var i=0; i<total; i++) {
 8101:             var server = document.getElementById('loadbalancing_target_'+other+'_'+i).value;
 8102:             if (server == item.value) {
 8103:                 if (document.getElementById('loadbalancing_target_'+other+'_'+i).checked) {
 8104:                     document.getElementById('loadbalancing_target_'+other+'_'+i).checked = false;
 8105:                 }
 8106:             }
 8107:         }
 8108:     }
 8109:     return;
 8110: }
 8111: 
 8112: function singleServerToggle(type) {
 8113:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+type).selectedIndex;
 8114:     if (offloadtoSelIdx == 0) {
 8115:         document.getElementById('loadbalancing_rules_'+type+'_0').checked = true;
 8116:         document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '';
 8117: 
 8118:     } else {
 8119:         document.getElementById('loadbalancing_rules_'+type+'_2').checked = true;
 8120:         document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '$select';
 8121:     }
 8122:     return;
 8123: }
 8124: 
 8125: function balanceruleChange(formname,type) {
 8126:     if (type == '_LC_external') {
 8127:         return; 
 8128:     }
 8129:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+type);
 8130:     for (var i=0; i<typesRules.length; i++) {
 8131:         if (formname.elements[typesRules[i]].checked) {
 8132:             if (formname.elements[typesRules[i]].value != 'specific') {
 8133:                 document.getElementById('loadbalancing_singleserver_'+type).selectedIndex = 0;
 8134:                 document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '';
 8135:             } else {
 8136:                 document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '$select';
 8137:             }
 8138:         }
 8139:     }
 8140:     return;
 8141: }
 8142: 
 8143: // ]]>
 8144: </script>
 8145: 
 8146: END
 8147: }
 8148: 
 8149: sub new_spares_js {
 8150:     my @sparestypes = ('primary','default');
 8151:     my $types = join("','",@sparestypes);
 8152:     my $select = &mt('Select');
 8153:     return <<"END";
 8154: 
 8155: <script type="text/javascript">
 8156: // <![CDATA[
 8157: 
 8158: function updateNewSpares(formname,lonhost) {
 8159:     var types = new Array('$types');
 8160:     var include = new Array();
 8161:     var exclude = new Array();
 8162:     for (var i=0; i<types.length; i++) {
 8163:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8164:         for (var j=0; j<spareboxes.length; j++) {
 8165:             if (formname.elements[spareboxes[j]].checked) {
 8166:                 exclude.push(formname.elements[spareboxes[j]].value);
 8167:             } else {
 8168:                 include.push(formname.elements[spareboxes[j]].value);
 8169:             }
 8170:         }
 8171:     }
 8172:     for (var i=0; i<types.length; i++) {
 8173:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8174:         var selIdx = newSpare.selectedIndex;
 8175:         var currnew = newSpare.options[selIdx].value;
 8176:         var okSpares = new Array();
 8177:         for (var j=0; j<newSpare.options.length; j++) {
 8178:             var possible = newSpare.options[j].value;
 8179:             if (possible != '') {
 8180:                 if (exclude.indexOf(possible) == -1) {
 8181:                     okSpares.push(possible);
 8182:                 } else {
 8183:                     if (currnew == possible) {
 8184:                         selIdx = 0;
 8185:                     }
 8186:                 }
 8187:             }
 8188:         }
 8189:         for (var k=0; k<include.length; k++) {
 8190:             if (okSpares.indexOf(include[k]) == -1) {
 8191:                 okSpares.push(include[k]);
 8192:             }
 8193:         }
 8194:         okSpares.sort();
 8195:         newSpare.options.length = 0;
 8196:         if (selIdx == 0) {
 8197:             newSpare.options[0] = new Option("$select","",true,true);
 8198:         } else {
 8199:             newSpare.options[0] = new Option("$select","",false,false);
 8200:         }
 8201:         for (var m=0; m<okSpares.length; m++) {
 8202:             var idx = m+1;
 8203:             var selThis = 0;
 8204:             if (selIdx != 0) {
 8205:                 if (okSpares[m] == currnew) {
 8206:                     selThis = 1;
 8207:                 }
 8208:             }
 8209:             if (selThis == 1) {
 8210:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8211:             } else {
 8212:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8213:             }
 8214:         }
 8215:     }
 8216:     return;
 8217: }
 8218: 
 8219: function checkNewSpares(lonhost,type) {
 8220:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8221:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8222:     if (chosen != '') { 
 8223:         var othertype;
 8224:         var othernewSpare;
 8225:         if (type == 'primary') {
 8226:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 8227:         }
 8228:         if (type == 'default') {
 8229:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 8230:         }
 8231:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 8232:             othernewSpare.selectedIndex = 0;
 8233:         }
 8234:     }
 8235:     return;
 8236: }
 8237: 
 8238: // ]]>
 8239: </script>
 8240: 
 8241: END
 8242: 
 8243: }
 8244: 
 8245: sub common_domprefs_js {
 8246:     return <<"END";
 8247: 
 8248: <script type="text/javascript">
 8249: // <![CDATA[
 8250: 
 8251: function getIndicesByName(formname,item) {
 8252:     var group = new Array();
 8253:     for (var i=0;i<formname.elements.length;i++) {
 8254:         if (formname.elements[i].name == item) {
 8255:             group.push(formname.elements[i].id);
 8256:         }
 8257:     }
 8258:     return group;
 8259: }
 8260: 
 8261: // ]]>
 8262: </script>
 8263: 
 8264: END
 8265: 
 8266: }
 8267: 
 8268: 1;

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