File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.6: download - view: text, annotated - select for diffs
Tue Sep 25 20:38:47 2012 UTC (11 years, 9 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- Missing concatenator.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.6 2012/09/25 20:38:47 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas,
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course.
   98: 
   99: Outputs: 1
  100: 
  101: $datatable  - HTML containing form elements which allow settings to be changed. 
  102: 
  103: In the case of course requests, radio buttons are displayed for each institutional
  104: affiliate type (and also default, and _LC_adv) for each of the course types 
  105: (official, unofficial and community).  In each case the radio buttons allow the 
  106: selection of one of four values:
  107: 
  108: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  109: which have the following effects:
  110: 
  111: 0
  112: 
  113: =over
  114: 
  115: - course requests are not allowed for this course types/affiliation
  116: 
  117: =back
  118: 
  119: approval 
  120: 
  121: =over 
  122: 
  123: - course requests must be approved by a Doman Coordinator in the 
  124: course's domain
  125: 
  126: =back
  127: 
  128: validate 
  129: 
  130: =over
  131: 
  132: - an institutional validation (e.g., check requestor is instructor
  133: of record) needs to be passed before the course will be created.  The required
  134: validation is in localenroll.pm on the primary library server for the course 
  135: domain.
  136: 
  137: =back
  138: 
  139: autolimit 
  140: 
  141: =over
  142:  
  143: - course requests will be processed automatically up to a limit of
  144: N requests for the course type for the particular requestor.
  145: If N is undefined, there is no limit to the number of course requests
  146: which a course owner may submit and have processed automatically. 
  147: 
  148: =back
  149: 
  150: =item modify_quotas() 
  151: 
  152: =back
  153: 
  154: =cut
  155: 
  156: package Apache::domainprefs;
  157: 
  158: use strict;
  159: use Apache::Constants qw(:common :http);
  160: use Apache::lonnet;
  161: use Apache::loncommon();
  162: use Apache::lonhtmlcommon();
  163: use Apache::lonlocal;
  164: use Apache::lonmsg();
  165: use Apache::lonconfigsettings;
  166: use LONCAPA qw(:DEFAULT :match);
  167: use LONCAPA::Enrollment;
  168: use LONCAPA::lonauthcgi();
  169: use File::Copy;
  170: use Locale::Language;
  171: use DateTime::TimeZone;
  172: use DateTime::Locale;
  173: 
  174: my $registered_cleanup;
  175: my $modified_urls;
  176: 
  177: sub handler {
  178:     my $r=shift;
  179:     if ($r->header_only) {
  180:         &Apache::loncommon::content_type($r,'text/html');
  181:         $r->send_http_header;
  182:         return OK;
  183:     }
  184: 
  185:     my $context = 'domain';
  186:     my $dom = $env{'request.role.domain'};
  187:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  188:     if (&Apache::lonnet::allowed('mau',$dom)) {
  189:         &Apache::loncommon::content_type($r,'text/html');
  190:         $r->send_http_header;
  191:     } else {
  192:         $env{'user.error.msg'}=
  193:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  194:         return HTTP_NOT_ACCEPTABLE;
  195:     }
  196: 
  197:     $registered_cleanup=0;
  198:     @{$modified_urls}=();
  199: 
  200:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  201:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  202:                                             ['phase','actions']);
  203:     my $phase = 'pickactions';
  204:     if ( exists($env{'form.phase'}) ) {
  205:         $phase = $env{'form.phase'};
  206:     }
  207:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  208:     my %domconfig =
  209:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  210:                 'quotas','autoenroll','autoupdate','autocreate',
  211:                 'directorysrch','usercreation','usermodification',
  212:                 'contacts','defaults','scantron','coursecategories',
  213:                 'serverstatuses','requestcourses','usersessions',
  214:                 'loadbalancing','requestauthor'],$dom);
  215:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  216:                        'autoupdate','autocreate','directorysrch','contacts',
  217:                        'usercreation','usermodification','scantron',
  218:                        'requestcourses','requestauthor','coursecategories',
  219:                        'serverstatuses','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 => 'Log-in Page Items',
  240:                                   col2 => '',},
  241:                                  {col1 => 'Log-in Help',
  242:                                   col2 => 'Value'}],
  243:                     },
  244:         'defaults' => 
  245:                     { text => 'Default authentication/language/timezone/portal',
  246:                       help => 'Domain_Configuration_LangTZAuth',
  247:                       header => [{col1 => 'Setting',
  248:                                   col2 => 'Value'}],
  249:                     },
  250:         'quotas' => 
  251:                     { text => 'Blogs, personal web pages, webDAV, portfolios',
  252:                       help => 'Domain_Configuration_Quotas',
  253:                       header => [{col1 => 'User affiliation',
  254:                                   col2 => 'Available tools',
  255:                                   col3 => 'Portfolio quota',}],
  256:                     },
  257:         'autoenroll' =>
  258:                    { text => 'Auto-enrollment settings',
  259:                      help => 'Domain_Configuration_Auto_Enrollment',
  260:                      header => [{col1 => 'Configuration setting',
  261:                                  col2 => 'Value(s)'}],
  262:                    },
  263:         'autoupdate' => 
  264:                    { text => 'Auto-update settings',
  265:                      help => 'Domain_Configuration_Auto_Updates',
  266:                      header => [{col1 => 'Setting',
  267:                                  col2 => 'Value',},
  268:                                 {col1 => 'Setting',
  269:                                  col2 => 'Affiliation'},
  270:                                 {col1 => 'User population',
  271:                                  col2 => 'Updateable user data'}],
  272:                   },
  273:         'autocreate' => 
  274:                   { text => 'Auto-course creation settings',
  275:                      help => 'Domain_Configuration_Auto_Creation',
  276:                      header => [{col1 => 'Configuration Setting',
  277:                                  col2 => 'Value',}],
  278:                   },
  279:         'directorysrch' => 
  280:                   { text => 'Institutional directory searches',
  281:                     help => 'Domain_Configuration_InstDirectory_Search',
  282:                     header => [{col1 => 'Setting',
  283:                                 col2 => 'Value',}],
  284:                   },
  285:         'contacts' =>
  286:                   { text => 'Contact Information',
  287:                     help => 'Domain_Configuration_Contact_Info',
  288:                     header => [{col1 => 'Setting',
  289:                                 col2 => 'Value',}],
  290:                   },
  291: 
  292:         'usercreation' => 
  293:                   { text => 'User creation',
  294:                     help => 'Domain_Configuration_User_Creation',
  295:                     header => [{col1 => 'Format rule type',
  296:                                 col2 => 'Format rules in force'},
  297:                                {col1 => 'User account creation',
  298:                                 col2 => 'Usernames which may be created',},
  299:                                {col1 => 'Context',
  300:                                 col2 => 'Assignable authentication types'}],
  301:                   },
  302:         'usermodification' =>
  303:                   { text => 'User modification',
  304:                     help => 'Domain_Configuration_User_Modification',
  305:                     header => [{col1 => 'Target user has role',
  306:                                 col2 => 'User information updateable in author context'},
  307:                                {col1 => 'Target user has role',
  308:                                 col2 => 'User information updateable in course context'},
  309:                                {col1 => "Status of user",
  310:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  311:                   },
  312:         'scantron' =>
  313:                   { text => 'Bubblesheet format file',
  314:                     help => 'Domain_Configuration_Scantron_Format',
  315:                     header => [ {col1 => 'Item',
  316:                                  col2 => '',
  317:                               }],
  318:                   },
  319:         'requestcourses' => 
  320:                  {text => 'Request creation of courses',
  321:                   help => 'Domain_Configuration_Request_Courses',
  322:                   header => [{col1 => 'User affiliation',
  323:                               col2 => 'Availability/Processing of requests',},
  324:                              {col1 => 'Setting',
  325:                               col2 => 'Value'}],
  326:                  },
  327:         'requestauthor' =>
  328:                  {text => 'Request authoring space',
  329:                   help => 'Domain_Configuration_Request_Author',
  330:                   header => [{col1 => 'User affiliation',
  331:                               col2 => 'Availability/Processing of requests',},
  332:                              {col1 => 'Setting',
  333:                               col2 => 'Value'}],
  334:                  },
  335:         'coursecategories' =>
  336:                   { text => 'Cataloging of courses/communities',
  337:                     help => 'Domain_Configuration_Cataloging_Courses',
  338:                     header => [{col1 => 'Category settings',
  339:                                 col2 => '',},
  340:                                {col1 => 'Categories',
  341:                                 col2 => '',
  342:                                }],
  343:                   },
  344:         'serverstatuses' =>
  345:                  {text   => 'Access to server status pages',
  346:                   help   => 'Domain_Configuration_Server_Status',
  347:                   header => [{col1 => 'Status Page',
  348:                               col2 => 'Other named users',
  349:                               col3 => 'Specific IPs',
  350:                             }],
  351:                  },
  352:         'usersessions' =>
  353:                  {text  => 'User session hosting/offloading',
  354:                   help  => 'Domain_Configuration_User_Sessions',
  355:                   header => [{col1 => 'Domain server',
  356:                               col2 => 'Servers to offload sessions to when busy'},
  357:                              {col1 => 'Hosting of users from other domains',
  358:                               col2 => 'Rules'},
  359:                              {col1 => "Hosting domain's own users elsewhere",
  360:                               col2 => 'Rules'}],
  361:                  },
  362:          'loadbalancing' =>
  363:                  {text  => 'Dedicated Load Balancer',
  364:                   help  => 'Domain_Configuration_Load_Balancing',
  365:                   header => [{col1 => 'Server',
  366:                               col2 => 'Default destinations',
  367:                               col3 => 'User affliation',
  368:                               col4 => 'Overrides'},
  369:                             ],
  370:                  },
  371:     );
  372:     if (keys(%servers) > 1) {
  373:         $prefs{'login'}  = { text   => 'Log-in page options',
  374:                              help   => 'Domain_Configuration_Login_Page',
  375:                             header => [{col1 => 'Log-in Service',
  376:                                         col2 => 'Server Setting',},
  377:                                        {col1 => 'Log-in Page Items',
  378:                                         col2 => ''},
  379:                                        {col1 => 'Log-in Help',
  380:                                         col2 => 'Value'}],
  381:                            };
  382:     }
  383:     my @roles = ('student','coordinator','author','admin');
  384:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  385:     &Apache::lonhtmlcommon::add_breadcrumb
  386:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  387:       text=>"Settings to display/modify"});
  388:     my $confname = $dom.'-domainconfig';
  389:     if ($phase eq 'process') {
  390:         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
  391:     } elsif ($phase eq 'display') {
  392:         my $js = &recaptcha_js();
  393:         if (keys(%servers) > 1) {
  394:             my ($othertitle,$usertypes,$types) =
  395:                 &Apache::loncommon::sorted_inst_types($dom);
  396:             $js .= &lonbalance_targets_js($dom,$types,\%servers).
  397:                    &new_spares_js().
  398:                    &common_domprefs_js().
  399:                    &Apache::loncommon::javascript_array_indexof();
  400:         }
  401:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  402:     } else {
  403:         if (keys(%domconfig) == 0) {
  404:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  405:             my @ids=&Apache::lonnet::current_machine_ids();
  406:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  407:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  408:                 my @loginimages = ('img','logo','domlogo','login');
  409:                 my $custom_img_count = 0;
  410:                 foreach my $img (@loginimages) {
  411:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  412:                         $custom_img_count ++;
  413:                     }
  414:                 }
  415:                 foreach my $role (@roles) {
  416:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  417:                         $custom_img_count ++;
  418:                     }
  419:                 }
  420:                 if ($custom_img_count > 0) {
  421:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  422:                     my $switch_server = &check_switchserver($dom,$confname);
  423:                     $r->print(
  424:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  425:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  426:     &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 />'.
  427:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  428:                     if ($switch_server) {
  429:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  430:                     }
  431:                     $r->print(&Apache::loncommon::end_page());
  432:                     return OK;
  433:                 }
  434:             }
  435:         }
  436:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  437:     }
  438:     return OK;
  439: }
  440: 
  441: sub process_changes {
  442:     my ($r,$dom,$confname,$action,$roles,$values) = @_;
  443:     my %domconfig;
  444:     if (ref($values) eq 'HASH') {
  445:         %domconfig = %{$values};
  446:     }
  447:     my $output;
  448:     if ($action eq 'login') {
  449:         $output = &modify_login($r,$dom,$confname,%domconfig);
  450:     } elsif ($action eq 'rolecolors') {
  451:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  452:                                      %domconfig);
  453:     } elsif ($action eq 'quotas') {
  454:         $output = &modify_quotas($dom,$action,%domconfig);
  455:     } elsif ($action eq 'autoenroll') {
  456:         $output = &modify_autoenroll($dom,%domconfig);
  457:     } elsif ($action eq 'autoupdate') {
  458:         $output = &modify_autoupdate($dom,%domconfig);
  459:     } elsif ($action eq 'autocreate') {
  460:         $output = &modify_autocreate($dom,%domconfig);
  461:     } elsif ($action eq 'directorysrch') {
  462:         $output = &modify_directorysrch($dom,%domconfig);
  463:     } elsif ($action eq 'usercreation') {
  464:         $output = &modify_usercreation($dom,%domconfig);
  465:     } elsif ($action eq 'usermodification') {
  466:         $output = &modify_usermodification($dom,%domconfig);
  467:     } elsif ($action eq 'contacts') {
  468:         $output = &modify_contacts($dom,%domconfig);
  469:     } elsif ($action eq 'defaults') {
  470:         $output = &modify_defaults($dom,$r);
  471:     } elsif ($action eq 'scantron') {
  472:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  473:     } elsif ($action eq 'coursecategories') {
  474:         $output = &modify_coursecategories($dom,%domconfig);
  475:     } elsif ($action eq 'serverstatuses') {
  476:         $output = &modify_serverstatuses($dom,%domconfig);
  477:     } elsif ($action eq 'requestcourses') {
  478:         $output = &modify_quotas($dom,$action,%domconfig);
  479:     } elsif ($action eq 'requestauthor') {
  480:         $output = &modify_quotas($dom,$action,%domconfig);
  481:     } elsif ($action eq 'usersessions') {
  482:         $output = &modify_usersessions($dom,%domconfig);
  483:     } elsif ($action eq 'loadbalancing') {
  484:         $output = &modify_loadbalancing($dom,%domconfig);
  485:     }
  486:     return $output;
  487: }
  488: 
  489: sub print_config_box {
  490:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  491:     my $rowtotal = 0;
  492:     my $output;
  493:     if ($action eq 'coursecategories') {
  494:         $output = &coursecategories_javascript($settings);
  495:     }
  496:     $output .= 
  497:          '<table class="LC_nested_outer">
  498:           <tr>
  499:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  500:            &mt($item->{text}).'&nbsp;'.
  501:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  502:           '</tr>';
  503:     $rowtotal ++;
  504:     my $numheaders = 1;
  505:     if (ref($item->{'header'}) eq 'ARRAY') {
  506:         $numheaders = scalar(@{$item->{'header'}});
  507:     }
  508:     if ($numheaders > 1) {
  509:         my $colspan = '';
  510:         my $rightcolspan = '';
  511:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  512:             (($action eq 'login') && ($numheaders < 3))) {
  513:             $colspan = ' colspan="2"';
  514:         }
  515:         if ($action eq 'usersessions') {
  516:             $rightcolspan = ' colspan="3"'; 
  517:         }
  518:         $output .= '
  519:           <tr>
  520:            <td>
  521:             <table class="LC_nested">
  522:              <tr class="LC_info_row">
  523:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  524:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  525:              </tr>';
  526:         $rowtotal ++;
  527:         if ($action eq 'autoupdate') {
  528:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  529:         } elsif ($action eq 'usercreation') {
  530:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  531:         } elsif ($action eq 'usermodification') {
  532:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  533:         } elsif ($action eq 'coursecategories') {
  534:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  535:         } elsif ($action eq 'login') {
  536:             if ($numheaders == 3) {
  537:                 $colspan = ' colspan="2"';
  538:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  539:             } else {
  540:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  541:             }
  542:         } elsif ($action eq 'requestcourses') {
  543:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  544:         } elsif ($action eq 'requestauthor') {
  545:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  546:         } elsif ($action eq 'usersessions') {
  547:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  548:         } elsif ($action eq 'rolecolors') {
  549:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  550:         }
  551:         $output .= '
  552:            </table>
  553:           </td>
  554:          </tr>
  555:          <tr>
  556:            <td>
  557:             <table class="LC_nested">
  558:              <tr class="LC_info_row">
  559:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  560:         $output .= '
  561:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  562:              </tr>';
  563:             $rowtotal ++;
  564:         if ($action eq 'autoupdate') {
  565:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  566:            </table>
  567:           </td>
  568:          </tr>
  569:          <tr>
  570:            <td>
  571:             <table class="LC_nested">
  572:              <tr class="LC_info_row">
  573:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  574:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  575:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  576:             $rowtotal ++;
  577:         } elsif ($action eq 'usercreation') {
  578:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  579:            </table>
  580:           </td>
  581:          </tr>
  582:          <tr>
  583:            <td>
  584:             <table class="LC_nested">
  585:              <tr class="LC_info_row">
  586:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  587:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  588:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  589:             $rowtotal ++;
  590:         } elsif ($action eq 'usermodification') {
  591:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  592:            </table>
  593:           </td>
  594:          </tr>
  595:          <tr>
  596:            <td>
  597:             <table class="LC_nested">
  598:              <tr class="LC_info_row">
  599:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  600:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  601:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  602:             $rowtotal ++;
  603:         } elsif ($action eq 'coursecategories') {
  604:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  605:         } elsif ($action eq 'login') {
  606:             if ($numheaders == 3) {
  607:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  608:            </table>
  609:           </td>
  610:          </tr>
  611:          <tr>
  612:            <td>
  613:             <table class="LC_nested">
  614:              <tr class="LC_info_row">
  615:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  616:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  617:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  618:                 $rowtotal ++;
  619:             } else {
  620:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  621:             }
  622:         } elsif ($action eq 'requestcourses') {
  623:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  624:         } elsif ($action eq 'requestauthor') {
  625:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  626:         } elsif ($action eq 'usersessions') {
  627:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  628:            </table>
  629:           </td>
  630:          </tr>
  631:          <tr>
  632:            <td>
  633:             <table class="LC_nested">
  634:              <tr class="LC_info_row">
  635:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  636:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  637:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  638:             $rowtotal ++;
  639:         } elsif ($action eq 'coursedefaults') {
  640:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  641:         } elsif ($action eq 'rolecolors') {
  642:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  643:            </table>
  644:           </td>
  645:          </tr>
  646:          <tr>
  647:            <td>
  648:             <table class="LC_nested">
  649:              <tr class="LC_info_row">
  650:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  651:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  652:               <td class="LC_right_item" valign="top">'.
  653:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  654:              </tr>'.
  655:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  656:            </table>
  657:           </td>
  658:          </tr>
  659:          <tr>
  660:            <td>
  661:             <table class="LC_nested">
  662:              <tr class="LC_info_row">
  663:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  664:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  665:              </tr>'.
  666:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  667:             $rowtotal += 2;
  668:         }
  669:     } else {
  670:         $output .= '
  671:           <tr>
  672:            <td>
  673:             <table class="LC_nested">
  674:              <tr class="LC_info_row">';
  675:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  676:             $output .= '  
  677:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  678:         } elsif ($action eq 'serverstatuses') {
  679:             $output .= '
  680:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  681:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  682: 
  683:         } else {
  684:             $output .= '
  685:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  686:         }
  687:         if (defined($item->{'header'}->[0]->{'col3'})) {
  688:             $output .= '<td class="LC_left_item" valign="top">'.
  689:                        &mt($item->{'header'}->[0]->{'col2'});
  690:             if ($action eq 'serverstatuses') {
  691:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  692:             } 
  693:         } else {
  694:             $output .= '<td class="LC_right_item" valign="top">'.
  695:                        &mt($item->{'header'}->[0]->{'col2'});
  696:         }
  697:         $output .= '</td>';
  698:         if ($item->{'header'}->[0]->{'col3'}) {
  699:             if (defined($item->{'header'}->[0]->{'col4'})) {
  700:                 $output .= '<td class="LC_left_item" valign="top">'.
  701:                             &mt($item->{'header'}->[0]->{'col3'});
  702:             } else {
  703:                 $output .= '<td class="LC_right_item" valign="top">'.
  704:                            &mt($item->{'header'}->[0]->{'col3'});
  705:             }
  706:             if ($action eq 'serverstatuses') {
  707:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  708:             }
  709:             $output .= '</td>';
  710:         }
  711:         if ($item->{'header'}->[0]->{'col4'}) {
  712:             $output .= '<td class="LC_right_item" valign="top">'.
  713:                        &mt($item->{'header'}->[0]->{'col4'});
  714:         }
  715:         $output .= '</tr>';
  716:         $rowtotal ++;
  717:         if ($action eq 'quotas') {
  718:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  719:         } elsif ($action eq 'autoenroll') {
  720:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  721:         } elsif ($action eq 'autocreate') {
  722:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  723:         } elsif ($action eq 'directorysrch') {
  724:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  725:         } elsif ($action eq 'contacts') {
  726:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  727:         } elsif ($action eq 'defaults') {
  728:             $output .= &print_defaults($dom,\$rowtotal);
  729:         } elsif ($action eq 'scantron') {
  730:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  731:         } elsif ($action eq 'serverstatuses') {
  732:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  733:         } elsif ($action eq 'helpsettings') {
  734:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  735:         } elsif ($action eq 'loadbalancing') {
  736:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  737:         }
  738:     }
  739:     $output .= '
  740:    </table>
  741:   </td>
  742:  </tr>
  743: </table><br />';
  744:     return ($output,$rowtotal);
  745: }
  746: 
  747: sub print_login {
  748:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  749:     my ($css_class,$datatable);
  750:     my %choices = &login_choices();
  751: 
  752:     if ($caller eq 'service') {
  753:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  754:         my $choice = $choices{'disallowlogin'};
  755:         $css_class = ' class="LC_odd_row"';
  756:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  757:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  758:                       '<th>'.$choices{'server'}.'</th>'.
  759:                       '<th>'.$choices{'serverpath'}.'</th>'.
  760:                       '<th>'.$choices{'custompath'}.'</th>'.
  761:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  762:         my %disallowed;
  763:         if (ref($settings) eq 'HASH') {
  764:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  765:                %disallowed = %{$settings->{'loginvia'}};
  766:             }
  767:         }
  768:         foreach my $lonhost (sort(keys(%servers))) {
  769:             my $direct = 'selected="selected"';
  770:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  771:                 if ($disallowed{$lonhost}{'server'} ne '') {
  772:                     $direct = '';
  773:                 }
  774:             }
  775:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  776:                           '<td><select name="'.$lonhost.'_server">'.
  777:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  778:                           '</option>';
  779:             foreach my $hostid (keys(%servers)) {
  780:                 next if ($servers{$hostid} eq $servers{$lonhost});
  781:                 my $selected = '';
  782:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  783:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  784:                         $selected = 'selected="selected"';
  785:                     }
  786:                 }
  787:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  788:                               $servers{$hostid}.'</option>';
  789:             }
  790:             $datatable .= '</select></td>'.
  791:                           '<td><select name="'.$lonhost.'_serverpath">';
  792:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  793:                 my $pathname = $path;
  794:                 if ($path eq 'custom') {
  795:                     $pathname = &mt('Custom Path').' ->';
  796:                 }
  797:                 my $selected = '';
  798:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  799:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  800:                         $selected = 'selected="selected"';
  801:                     }
  802:                 } elsif ($path eq '') {
  803:                     $selected = 'selected="selected"';
  804:                 }
  805:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  806:             }
  807:             $datatable .= '</select></td>';
  808:             my ($custom,$exempt);
  809:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  810:                 $custom = $disallowed{$lonhost}{'custompath'};
  811:                 $exempt = $disallowed{$lonhost}{'exempt'};
  812:             }
  813:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  814:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  815:                           '</tr>';
  816:         }
  817:         $datatable .= '</table></td></tr>';
  818:         return $datatable;
  819:     } elsif ($caller eq 'page') {
  820:         my %defaultchecked = ( 
  821:                                'coursecatalog' => 'on',
  822:                                'adminmail'     => 'off',
  823:                                'newuser'       => 'off',
  824:                              );
  825:         my @toggles = ('coursecatalog','adminmail','newuser');
  826:         my (%checkedon,%checkedoff);
  827:         foreach my $item (@toggles) {
  828:             if ($defaultchecked{$item} eq 'on') { 
  829:                 $checkedon{$item} = ' checked="checked" ';
  830:                 $checkedoff{$item} = ' ';
  831:             } elsif ($defaultchecked{$item} eq 'off') {
  832:                 $checkedoff{$item} = ' checked="checked" ';
  833:                 $checkedon{$item} = ' ';
  834:             }
  835:         }
  836:         my @images = ('img','logo','domlogo','login');
  837:         my @logintext = ('textcol','bgcol');
  838:         my @bgs = ('pgbg','mainbg','sidebg');
  839:         my @links = ('link','alink','vlink');
  840:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  841:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  842:         my (%is_custom,%designs);
  843:         my %defaults = (
  844:                        font => $defaultdesign{'login.font'},
  845:                        );
  846:         foreach my $item (@images) {
  847:             $defaults{$item} = $defaultdesign{'login.'.$item};
  848:             $defaults{'showlogo'}{$item} = 1;
  849:         }
  850:         foreach my $item (@bgs) {
  851:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  852:         }
  853:         foreach my $item (@logintext) {
  854:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  855:         }
  856:         foreach my $item (@links) {
  857:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  858:         }
  859:         if (ref($settings) eq 'HASH') {
  860:             foreach my $item (@toggles) {
  861:                 if ($settings->{$item} eq '1') {
  862:                     $checkedon{$item} =  ' checked="checked" ';
  863:                     $checkedoff{$item} = ' ';
  864:                 } elsif ($settings->{$item} eq '0') {
  865:                     $checkedoff{$item} =  ' checked="checked" ';
  866:                     $checkedon{$item} = ' ';
  867:                 }
  868:             }
  869:             foreach my $item (@images) {
  870:                 if (defined($settings->{$item})) {
  871:                     $designs{$item} = $settings->{$item};
  872:                     $is_custom{$item} = 1;
  873:                 }
  874:                 if (defined($settings->{'showlogo'}{$item})) {
  875:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  876:                 }
  877:             }
  878:             foreach my $item (@logintext) {
  879:                 if ($settings->{$item} ne '') {
  880:                     $designs{'logintext'}{$item} = $settings->{$item};
  881:                     $is_custom{$item} = 1;
  882:                 }
  883:             }
  884:             if ($settings->{'font'} ne '') {
  885:                 $designs{'font'} = $settings->{'font'};
  886:                 $is_custom{'font'} = 1;
  887:             }
  888:             foreach my $item (@bgs) {
  889:                 if ($settings->{$item} ne '') {
  890:                     $designs{'bgs'}{$item} = $settings->{$item};
  891:                     $is_custom{$item} = 1;
  892:                 }
  893:             }
  894:             foreach my $item (@links) {
  895:                 if ($settings->{$item} ne '') {
  896:                     $designs{'links'}{$item} = $settings->{$item};
  897:                     $is_custom{$item} = 1;
  898:                 }
  899:             }
  900:         } else {
  901:             if ($designhash{$dom.'.login.font'} ne '') {
  902:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  903:                 $is_custom{'font'} = 1;
  904:             }
  905:             foreach my $item (@images) {
  906:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  907:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  908:                     $is_custom{$item} = 1;
  909:                 }
  910:             }
  911:             foreach my $item (@bgs) {
  912:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  913:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  914:                     $is_custom{$item} = 1;
  915:                 }
  916:             }
  917:             foreach my $item (@links) {
  918:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  919:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  920:                     $is_custom{$item} = 1;
  921:                 }
  922:             }
  923:         }
  924:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  925:                                                       logo => 'Institution Logo',
  926:                                                       domlogo => 'Domain Logo',
  927:                                                       login => 'Login box');
  928:         my $itemcount = 1;
  929:         foreach my $item (@toggles) {
  930:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
  931:             $datatable .=  
  932:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  933:                 '</td><td>'.
  934:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
  935:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
  936:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
  937:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
  938:                 '</tr>';
  939:             $itemcount ++;
  940:         }
  941:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
  942:         $datatable .= '</tr></table></td></tr>';
  943:     } elsif ($caller eq 'help') {
  944:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
  945:         my $switchserver = &check_switchserver($dom,$confname);
  946:         my $itemcount = 1;
  947:         $defaulturl = '/adm/loginproblems.html';
  948:         $defaulttype = 'default';
  949:         %lt = &Apache::lonlocal::texthash (
  950:                      del     => 'Delete?',
  951:                      rep     => 'Replace:',
  952:                      upl     => 'Upload:',
  953:                      default => 'Default',
  954:                      custom  => 'Custom',
  955:                                              );
  956:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
  957:         my @currlangs;
  958:         if (ref($settings) eq 'HASH') {
  959:             if (ref($settings->{'helpurl'}) eq 'HASH') {
  960:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
  961:                     next if ($settings->{'helpurl'}{$key} eq '');
  962:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
  963:                     $type{$key} = 'custom';
  964:                     unless ($key eq 'nolang') {
  965:                         push(@currlangs,$key);
  966:                     }
  967:                 }
  968:             } elsif ($settings->{'helpurl'} ne '') {
  969:                 $type{'nolang'} = 'custom';
  970:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
  971:             }
  972:         }
  973:         foreach my $lang ('nolang',sort(@currlangs)) {
  974:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
  975:             $datatable .= '<tr'.$css_class.'>';
  976:             if ($url{$lang} eq '') {
  977:                 $url{$lang} = $defaulturl;
  978:             }
  979:             if ($type{$lang} eq '') {
  980:                 $type{$lang} = $defaulttype;
  981:             }
  982:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
  983:             if ($lang eq 'nolang') {
  984:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
  985:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
  986:             } else {
  987:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
  988:                                   $langchoices{$lang},
  989:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
  990:             }
  991:             $datatable .= '</span></td>'."\n".
  992:                           '<td class="LC_left_item">';
  993:             if ($type{$lang} eq 'custom') {
  994:                 $datatable .= '<span class="LC_nobreak"><label>'.
  995:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
  996:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
  997:             } else {
  998:                 $datatable .= $lt{'upl'};
  999:             }
 1000:             $datatable .='<br />';
 1001:             if ($switchserver) {
 1002:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1003:             } else {
 1004:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1005:             }
 1006:             $datatable .= '</td></tr>';
 1007:             $itemcount ++;
 1008:         }
 1009:         my @addlangs;
 1010:         foreach my $lang (sort(keys(%langchoices))) {
 1011:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1012:             push(@addlangs,$lang);
 1013:         }
 1014:         if (@addlangs > 0) {
 1015:             my %toadd;
 1016:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1017:             $toadd{''} = &mt('Select');
 1018:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1019:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1020:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1021:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1022:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1023:             if ($switchserver) {
 1024:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1025:             } else {
 1026:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1027:             }
 1028:             $datatable .= '</td></tr>';
 1029:             $itemcount ++;
 1030:         }
 1031:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1032:     }
 1033:     return $datatable;
 1034: }
 1035: 
 1036: sub login_choices {
 1037:     my %choices =
 1038:         &Apache::lonlocal::texthash (
 1039:             coursecatalog => 'Display Course/Community Catalog link?',
 1040:             adminmail     => "Display Administrator's E-mail Address?",
 1041:             disallowlogin => "Login page requests redirected",
 1042:             hostid        => "Server",
 1043:             server        => "Redirect to:",
 1044:             serverpath    => "Path",
 1045:             custompath    => "Custom", 
 1046:             exempt        => "Exempt IP(s)",
 1047:             directlogin   => "No redirect",
 1048:             newuser       => "Link to create a user account",
 1049:             img           => "Header",
 1050:             logo          => "Main Logo",
 1051:             domlogo       => "Domain Logo",
 1052:             login         => "Log-in Header", 
 1053:             textcol       => "Text color",
 1054:             bgcol         => "Box color",
 1055:             bgs           => "Background colors",
 1056:             links         => "Link colors",
 1057:             font          => "Font color",
 1058:             pgbg          => "Header",
 1059:             mainbg        => "Page",
 1060:             sidebg        => "Login box",
 1061:             link          => "Link",
 1062:             alink         => "Active link",
 1063:             vlink         => "Visited link",
 1064:         );
 1065:     return %choices;
 1066: }
 1067: 
 1068: sub print_rolecolors {
 1069:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1070:     my %choices = &color_font_choices();
 1071:     my @bgs = ('pgbg','tabbg','sidebg');
 1072:     my @links = ('link','alink','vlink');
 1073:     my @images = ('img');
 1074:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1075:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1076:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1077:     my (%is_custom,%designs);
 1078:     my %defaults = (
 1079:                    img => $defaultdesign{$role.'.img'},
 1080:                    font => $defaultdesign{$role.'.font'},
 1081: 		   fontmenu => $defaultdesign{$role.'.fontmenu'},
 1082:                    );
 1083:     foreach my $item (@bgs) {
 1084:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1085:     }
 1086:     foreach my $item (@links) {
 1087:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1088:     }
 1089:     if (ref($settings) eq 'HASH') {
 1090:         if (ref($settings->{$role}) eq 'HASH') {
 1091:             if ($settings->{$role}->{'img'} ne '') {
 1092:                 $designs{'img'} = $settings->{$role}->{'img'};
 1093:                 $is_custom{'img'} = 1;
 1094:             }
 1095:             if ($settings->{$role}->{'font'} ne '') {
 1096:                 $designs{'font'} = $settings->{$role}->{'font'};
 1097:                 $is_custom{'font'} = 1;
 1098:             }
 1099:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1100:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1101:                 $is_custom{'fontmenu'} = 1;
 1102:             }
 1103:             foreach my $item (@bgs) {
 1104:                 if ($settings->{$role}->{$item} ne '') {
 1105:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1106:                     $is_custom{$item} = 1;
 1107:                 }
 1108:             }
 1109:             foreach my $item (@links) {
 1110:                 if ($settings->{$role}->{$item} ne '') {
 1111:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1112:                     $is_custom{$item} = 1;
 1113:                 }
 1114:             }
 1115:         }
 1116:     } else {
 1117:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1118:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1119:             $is_custom{'img'} = 1;
 1120:         }
 1121:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1122:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1123:             $is_custom{'fontmenu'} = 1; 
 1124:         }
 1125:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1126:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1127:             $is_custom{'font'} = 1;
 1128:         }
 1129:         foreach my $item (@bgs) {
 1130:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1131:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1132:                 $is_custom{$item} = 1;
 1133:             
 1134:             }
 1135:         }
 1136:         foreach my $item (@links) {
 1137:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1138:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1139:                 $is_custom{$item} = 1;
 1140:             }
 1141:         }
 1142:     }
 1143:     my $itemcount = 1;
 1144:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1145:     $datatable .= '</tr></table></td></tr>';
 1146:     return $datatable;
 1147: }
 1148: 
 1149: sub display_color_options {
 1150:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1151:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1152:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1153:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1154:     my $datatable = '<tr'.$css_class.'>'.
 1155:         '<td>'.$choices->{'font'}.'</td>';
 1156:     if (!$is_custom->{'font'}) {
 1157:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1158:     } else {
 1159:         $datatable .= '<td>&nbsp;</td>';
 1160:     }
 1161:     my $fontlink = &color_pick($phase,$role,'font',$choices->{'font'},$designs->{'font'});
 1162:     $datatable .= '<td><span class="LC_nobreak">'.
 1163:                   '<input type="text" size="10" name="'.$role.'_font"'.
 1164:                   ' value="'.$designs->{'font'}.'" />&nbsp;'.$fontlink.
 1165:                   '&nbsp;<span id="css_'.$role.'_font" style="background-color: '.
 1166:                   $designs->{'font'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1167:                   '</span></td></tr>';
 1168:     unless ($role eq 'login') { 
 1169:         $datatable .= '<tr'.$css_class.'>'.
 1170:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1171:         if (!$is_custom->{'fontmenu'}) {
 1172:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1173:         } else {
 1174:             $datatable .= '<td>&nbsp;</td>';
 1175:         }
 1176:         $fontlink = &color_pick($phase,$role,'fontmenu',$choices->{'fontmenu'},$designs->{'fontmenu'});
 1177:         $datatable .= '<td><span class="LC_nobreak">'.
 1178:                       '<input type="text" size="10" name="'.$role.'_fontmenu"'.
 1179:                       ' value="'.$designs->{'fontmenu'}.'" />&nbsp;'.$fontlink.
 1180:                       '&nbsp;<span id="css_'.$role.'_fontmenu" style="background-color: '.
 1181:                       $designs->{'fontmenu'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1182:                       '</span></td></tr>';
 1183:     }
 1184:     my $switchserver = &check_switchserver($dom,$confname);
 1185:     foreach my $img (@{$images}) {
 1186: 	$itemcount ++;
 1187:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1188:         $datatable .= '<tr'.$css_class.'>'.
 1189:                       '<td>'.$choices->{$img};
 1190:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1191:         if ($role eq 'login') {
 1192:             if ($img eq 'login') {
 1193:                 $login_hdr_pick =
 1194:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1195:                 $logincolors =
 1196:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1197:                                             $designs);
 1198:             } elsif ($img ne 'domlogo') {
 1199:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1200:             }
 1201:         }
 1202:         $datatable .= '</td>';
 1203:         if ($designs->{$img} ne '') {
 1204:             $imgfile = $designs->{$img};
 1205: 	    $img_import = ($imgfile =~ m{^/adm/});
 1206:         } else {
 1207:             $imgfile = $defaults->{$img};
 1208:         }
 1209:         if ($imgfile) {
 1210:             my ($showfile,$fullsize);
 1211:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1212:                 my $urldir = $1;
 1213:                 my $filename = $2;
 1214:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1215:                 if (@info) {
 1216:                     my $thumbfile = 'tn-'.$filename;
 1217:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1218:                     if (@thumb) {
 1219:                         $showfile = $urldir.'/'.$thumbfile;
 1220:                     } else {
 1221:                         $showfile = $imgfile;
 1222:                     }
 1223:                 } else {
 1224:                     $showfile = '';
 1225:                 }
 1226:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1227:                 $showfile = $imgfile;
 1228:                 my $imgdir = $1;
 1229:                 my $filename = $2;
 1230:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1231:                     $showfile = "/$imgdir/tn-".$filename;
 1232:                 } else {
 1233:                     my $input = $londocroot.$imgfile;
 1234:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1235:                     if (!-e $output) {
 1236:                         my ($width,$height) = &thumb_dimensions();
 1237:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1238:                         if ($fullwidth ne '' && $fullheight ne '') {
 1239:                             if ($fullwidth > $width && $fullheight > $height) { 
 1240:                                 my $size = $width.'x'.$height;
 1241:                                 system("convert -sample $size $input $output");
 1242:                                 $showfile = "/$imgdir/tn-".$filename;
 1243:                             }
 1244:                         }
 1245:                     }
 1246:                 }
 1247:             }
 1248:             if ($showfile) {
 1249:                 if ($showfile =~ m{^/(adm|res)/}) {
 1250:                     if ($showfile =~ m{^/res/}) {
 1251:                         my $local_showfile =
 1252:                             &Apache::lonnet::filelocation('',$showfile);
 1253:                         &Apache::lonnet::repcopy($local_showfile);
 1254:                     }
 1255:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1256:                 }
 1257:                 if ($imgfile) {
 1258:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1259:                         if ($imgfile =~ m{^/res/}) {
 1260:                             my $local_imgfile =
 1261:                                 &Apache::lonnet::filelocation('',$imgfile);
 1262:                             &Apache::lonnet::repcopy($local_imgfile);
 1263:                         }
 1264:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1265:                     } else {
 1266:                         $fullsize = $imgfile;
 1267:                     }
 1268:                 }
 1269:                 $datatable .= '<td>';
 1270:                 if ($img eq 'login') {
 1271:                     $datatable .= $login_hdr_pick;
 1272:                 } 
 1273:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1274:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1275:             } else {
 1276:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1277:                               &mt('Upload:');
 1278:             }
 1279:         } else {
 1280:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1281:                           &mt('Upload:');
 1282:         }
 1283:         if ($switchserver) {
 1284:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1285:         } else {
 1286:             if ($img ne 'login') { # suppress file selection for Log-in header
 1287:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1288:             }
 1289:         }
 1290:         $datatable .= '</td></tr>';
 1291:     }
 1292:     $itemcount ++;
 1293:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1294:     $datatable .= '<tr'.$css_class.'>'.
 1295:                   '<td>'.$choices->{'bgs'}.'</td>';
 1296:     my $bgs_def;
 1297:     foreach my $item (@{$bgs}) {
 1298:         if (!$is_custom->{$item}) {
 1299:             $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>';
 1300:         }
 1301:     }
 1302:     if ($bgs_def) {
 1303:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1304:     } else {
 1305:         $datatable .= '<td>&nbsp;</td>';
 1306:     }
 1307:     $datatable .= '<td class="LC_right_item">'.
 1308:                   '<table border="0"><tr>';
 1309:     foreach my $item (@{$bgs}) {
 1310:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'bgs'}{$item});
 1311:         $datatable .= '<td align="center">'.$link;
 1312:         if ($designs->{'bgs'}{$item}) {
 1313:             $datatable .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1314:         }
 1315:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'bgs'}{$item}.
 1316:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1317:     }
 1318:     $datatable .= '</tr></table></td></tr>';
 1319:     $itemcount ++;
 1320:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1321:     $datatable .= '<tr'.$css_class.'>'.
 1322:                   '<td>'.$choices->{'links'}.'</td>';
 1323:     my $links_def;
 1324:     foreach my $item (@{$links}) {
 1325:         if (!$is_custom->{$item}) {
 1326:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1327:         }
 1328:     }
 1329:     if ($links_def) {
 1330:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1331:     } else {
 1332:         $datatable .= '<td>&nbsp;</td>';
 1333:     }
 1334:     $datatable .= '<td class="LC_right_item">'.
 1335:                   '<table border="0"><tr>';
 1336:     foreach my $item (@{$links}) {
 1337:         $datatable .= '<td align="center">'."\n".
 1338:                       &color_pick($phase,$role,$item,$choices->{$item},
 1339:                                   $designs->{'links'}{$item});
 1340:         if ($designs->{'links'}{$item}) {
 1341:             $datatable.='&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'links'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1342:         }
 1343:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'links'}{$item}.
 1344:                       '" /></td>';
 1345:     }
 1346:     $$rowtotal += $itemcount;
 1347:     return $datatable;
 1348: }
 1349: 
 1350: sub logo_display_options {
 1351:     my ($img,$defaults,$designs) = @_;
 1352:     my $checkedon;
 1353:     if (ref($defaults) eq 'HASH') {
 1354:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1355:             if ($defaults->{'showlogo'}{$img}) {
 1356:                 $checkedon = 'checked="checked" ';     
 1357:             }
 1358:         } 
 1359:     }
 1360:     if (ref($designs) eq 'HASH') {
 1361:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1362:             if (defined($designs->{'showlogo'}{$img})) {
 1363:                 if ($designs->{'showlogo'}{$img} == 0) {
 1364:                     $checkedon = '';
 1365:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1366:                     $checkedon = 'checked="checked" ';
 1367:                 }
 1368:             }
 1369:         }
 1370:     }
 1371:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1372:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1373:            &mt('show').'</label>'."\n";
 1374: }
 1375: 
 1376: sub login_header_options  {
 1377:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1378:     my $output = '';
 1379:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1380:         $output .= &mt('Text default(s):').'<br />';
 1381:         if (!$is_custom->{'textcol'}) {
 1382:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1383:                        '&nbsp;&nbsp;&nbsp;';
 1384:         }
 1385:         if (!$is_custom->{'bgcol'}) {
 1386:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1387:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1388:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1389:         }
 1390:         $output .= '<br />';
 1391:     }
 1392:     $output .='<br />';
 1393:     return $output;
 1394: }
 1395: 
 1396: sub login_text_colors {
 1397:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1398:     my $color_menu = '<table border="0"><tr>';
 1399:     foreach my $item (@{$logintext}) {
 1400:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1401:         $color_menu .= '<td align="center">'.$link;
 1402:         if ($designs->{'logintext'}{$item}) {
 1403:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1404:         }
 1405:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1406:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1407:                        '<td>&nbsp;</td>';
 1408:     }
 1409:     $color_menu .= '</tr></table><br />';
 1410:     return $color_menu;
 1411: }
 1412: 
 1413: sub image_changes {
 1414:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1415:     my $output;
 1416:     if ($img eq 'login') {
 1417:             # suppress image for Log-in header
 1418:     } elsif (!$is_custom) {
 1419:         if ($img ne 'domlogo') {
 1420:             $output .= &mt('Default image:').'<br />';
 1421:         } else {
 1422:             $output .= &mt('Default in use:').'<br />';
 1423:         }
 1424:     }
 1425:     if ($img eq 'login') { # suppress image for Log-in header
 1426:         $output .= '<td>'.$logincolors;
 1427:     } else {
 1428:         if ($img_import) {
 1429:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1430:         }
 1431:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1432:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1433:         if ($is_custom) {
 1434:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1435:                        '<input type="checkbox" name="'.
 1436:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1437:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1438:         } else {
 1439:             $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1440:         }
 1441:     }
 1442:     return $output;
 1443: }
 1444: 
 1445: sub color_pick {
 1446:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1447:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1448:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1449:                ');">'.$desc.'</a>';
 1450:     return $link;
 1451: }
 1452: 
 1453: sub print_quotas {
 1454:     my ($dom,$settings,$rowtotal,$action) = @_;
 1455:     my $context;
 1456:     if ($action eq 'quotas') {
 1457:         $context = 'tools';
 1458:     } else {
 1459:         $context = $action;
 1460:     }
 1461:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 1462:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1463:     my $typecount = 0;
 1464:     my ($css_class,%titles);
 1465:     if ($context eq 'requestcourses') {
 1466:         @usertools = ('official','unofficial','community');
 1467:         @options =('norequest','approval','validate','autolimit');
 1468:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1469:         %titles = &courserequest_titles();
 1470:     } elsif ($context eq 'requestauthor') {
 1471:         @usertools = ('author');
 1472:         @options = ('norequest','approval','automatic');
 1473:         %titles = &authorrequest_titles();
 1474:     } else {
 1475:         @usertools = ('aboutme','blog','webdav','portfolio');
 1476:         %titles = &tool_titles();
 1477:     }
 1478:     if (ref($types) eq 'ARRAY') {
 1479:         foreach my $type (@{$types}) {
 1480:             my $currdefquota;
 1481:             unless (($context eq 'requestcourses') ||
 1482:                     ($context eq 'requestauthor')) {
 1483:                 if (ref($settings) eq 'HASH') {
 1484:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1485:                         $currdefquota = $settings->{defaultquota}->{$type}; 
 1486:                     } else {
 1487:                         $currdefquota = $settings->{$type};
 1488:                     }
 1489:                 }
 1490:             }
 1491:             if (defined($usertypes->{$type})) {
 1492:                 $typecount ++;
 1493:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1494:                 $datatable .= '<tr'.$css_class.'>'.
 1495:                               '<td>'.$usertypes->{$type}.'</td>'.
 1496:                               '<td class="LC_left_item">';
 1497:                 if ($context eq 'requestcourses') {
 1498:                     $datatable .= '<table><tr>';
 1499:                 }
 1500:                 my %cell;  
 1501:                 foreach my $item (@usertools) {
 1502:                     if ($context eq 'requestcourses') {
 1503:                         my ($curroption,$currlimit);
 1504:                         if (ref($settings) eq 'HASH') {
 1505:                             if (ref($settings->{$item}) eq 'HASH') {
 1506:                                 $curroption = $settings->{$item}->{$type};
 1507:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1508:                                     $currlimit = $1; 
 1509:                                 }
 1510:                             }
 1511:                         }
 1512:                         if (!$curroption) {
 1513:                             $curroption = 'norequest';
 1514:                         }
 1515:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1516:                         foreach my $option (@options) {
 1517:                             my $val = $option;
 1518:                             if ($option eq 'norequest') {
 1519:                                 $val = 0;  
 1520:                             }
 1521:                             if ($option eq 'validate') {
 1522:                                 my $canvalidate = 0;
 1523:                                 if (ref($validations{$item}) eq 'HASH') { 
 1524:                                     if ($validations{$item}{$type}) {
 1525:                                         $canvalidate = 1;
 1526:                                     }
 1527:                                 }
 1528:                                 next if (!$canvalidate);
 1529:                             }
 1530:                             my $checked = '';
 1531:                             if ($option eq $curroption) {
 1532:                                 $checked = ' checked="checked"';
 1533:                             } elsif ($option eq 'autolimit') {
 1534:                                 if ($curroption =~ /^autolimit/) {
 1535:                                     $checked = ' checked="checked"';
 1536:                                 }                       
 1537:                             } 
 1538:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1539:                                   '<input type="radio" name="crsreq_'.$item.
 1540:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1541:                                   $titles{$option}.'</label>';
 1542:                             if ($option eq 'autolimit') {
 1543:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1544:                                                 $item.'_limit_'.$type.'" size="1" '.
 1545:                                                 'value="'.$currlimit.'" />';
 1546:                             }
 1547:                             $cell{$item} .= '</span> ';
 1548:                             if ($option eq 'autolimit') {
 1549:                                 $cell{$item} .= $titles{'unlimited'};
 1550:                             }
 1551:                         }
 1552:                     } elsif ($context eq 'requestauthor') {
 1553:                         my $curroption;
 1554:                         if (ref($settings) eq 'HASH') {
 1555:                             $curroption = $settings->{$type};
 1556:                         }
 1557:                         if (!$curroption) {
 1558:                             $curroption = 'norequest';
 1559:                         }
 1560:                         foreach my $option (@options) {
 1561:                             my $val = $option;
 1562:                             if ($option eq 'norequest') {
 1563:                                 $val = 0;
 1564:                             }
 1565:                             my $checked = '';
 1566:                             if ($option eq $curroption) {
 1567:                                 $checked = ' checked="checked"';
 1568:                             }
 1569:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1570:                                   '<input type="radio" name="authorreq_'.$type.
 1571:                                   '" value="'.$val.'"'.$checked.' />'.
 1572:                                   $titles{$option}.'</label></span>&nbsp; ';
 1573:                         }
 1574:                     } else {
 1575:                         my $checked = 'checked="checked" ';
 1576:                         if (ref($settings) eq 'HASH') {
 1577:                             if (ref($settings->{$item}) eq 'HASH') {
 1578:                                 if ($settings->{$item}->{$type} == 0) {
 1579:                                     $checked = '';
 1580:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1581:                                     $checked =  'checked="checked" ';
 1582:                                 }
 1583:                             }
 1584:                         }
 1585:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1586:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1587:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1588:                                       '</label></span>&nbsp; ';
 1589:                     }
 1590:                 }
 1591:                 if ($context eq 'requestcourses') {
 1592:                     $datatable .= '</tr><tr>';
 1593:                     foreach my $item (@usertools) {
 1594:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1595:                     }
 1596:                     $datatable .= '</tr></table>';
 1597:                 }
 1598:                 $datatable .= '</td>';
 1599:                 unless (($context eq 'requestcourses') ||
 1600:                         ($context eq 'requestauthor')) {
 1601:                     $datatable .= 
 1602:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1603:                               '<input type="text" name="quota_'.$type.
 1604:                               '" value="'.$currdefquota.
 1605:                               '" size="5" /> Mb</span></td>';
 1606:                 }
 1607:                 $datatable .= '</tr>';
 1608:             }
 1609:         }
 1610:     }
 1611:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1612:         $defaultquota = '20';
 1613:         if (ref($settings) eq 'HASH') {
 1614:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1615:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1616:             } elsif (defined($settings->{'default'})) {
 1617:                 $defaultquota = $settings->{'default'};
 1618:             }
 1619:         }
 1620:     }
 1621:     $typecount ++;
 1622:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1623:     $datatable .= '<tr'.$css_class.'>'.
 1624:                   '<td>'.$othertitle.'</td>'.
 1625:                   '<td class="LC_left_item">';
 1626:     if ($context eq 'requestcourses') {
 1627:         $datatable .= '<table><tr>';
 1628:     }
 1629:     my %defcell;
 1630:     foreach my $item (@usertools) {
 1631:         if ($context eq 'requestcourses') {
 1632:             my ($curroption,$currlimit);
 1633:             if (ref($settings) eq 'HASH') {
 1634:                 if (ref($settings->{$item}) eq 'HASH') {
 1635:                     $curroption = $settings->{$item}->{'default'};
 1636:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1637:                         $currlimit = $1;
 1638:                     }
 1639:                 }
 1640:             }
 1641:             if (!$curroption) {
 1642:                 $curroption = 'norequest';
 1643:             }
 1644:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1645:             foreach my $option (@options) {
 1646:                 my $val = $option;
 1647:                 if ($option eq 'norequest') {
 1648:                     $val = 0;
 1649:                 }
 1650:                 if ($option eq 'validate') {
 1651:                     my $canvalidate = 0;
 1652:                     if (ref($validations{$item}) eq 'HASH') {
 1653:                         if ($validations{$item}{'default'}) {
 1654:                             $canvalidate = 1;
 1655:                         }
 1656:                     }
 1657:                     next if (!$canvalidate);
 1658:                 }
 1659:                 my $checked = '';
 1660:                 if ($option eq $curroption) {
 1661:                     $checked = ' checked="checked"';
 1662:                 } elsif ($option eq 'autolimit') {
 1663:                     if ($curroption =~ /^autolimit/) {
 1664:                         $checked = ' checked="checked"';
 1665:                     }
 1666:                 }
 1667:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1668:                                   '<input type="radio" name="crsreq_'.$item.
 1669:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1670:                                   $titles{$option}.'</label>';
 1671:                 if ($option eq 'autolimit') {
 1672:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1673:                                        $item.'_limit_default" size="1" '.
 1674:                                        'value="'.$currlimit.'" />';
 1675:                 }
 1676:                 $defcell{$item} .= '</span> ';
 1677:                 if ($option eq 'autolimit') {
 1678:                     $defcell{$item} .= $titles{'unlimited'};
 1679:                 }
 1680:             }
 1681:         } elsif ($context eq 'requestauthor') {
 1682:             my $curroption;
 1683:             if (ref($settings) eq 'HASH') {
 1684:                 if (ref($settings->{'requestauthor'}) eq 'HASH') {
 1685:                     $curroption = $settings->{'requestauthor'};
 1686:                 }
 1687:             }
 1688:             if (!$curroption) {
 1689:                 $curroption = 'norequest';
 1690:             }
 1691:             foreach my $option (@options) {
 1692:                 my $val = $option;
 1693:                 if ($option eq 'norequest') {
 1694:                     $val = 0;
 1695:                 }
 1696:                 my $checked = '';
 1697:                 if ($option eq $curroption) {
 1698:                     $checked = ' checked="checked"';
 1699:                 }
 1700:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1701:                               '<input type="radio" name="authorreq_default"'.
 1702:                               ' value="'.$val.'"'.$checked.' />'.
 1703:                               $titles{$option}.'</label></span>&nbsp; ';
 1704:             }
 1705:         } else {
 1706:             my $checked = 'checked="checked" ';
 1707:             if (ref($settings) eq 'HASH') {
 1708:                 if (ref($settings->{$item}) eq 'HASH') {
 1709:                     if ($settings->{$item}->{'default'} == 0) {
 1710:                         $checked = '';
 1711:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1712:                         $checked = 'checked="checked" ';
 1713:                     }
 1714:                 }
 1715:             }
 1716:             $datatable .= '<span class="LC_nobreak"><label>'.
 1717:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1718:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1719:                           '</label></span>&nbsp; ';
 1720:         }
 1721:     }
 1722:     if ($context eq 'requestcourses') {
 1723:         $datatable .= '</tr><tr>';
 1724:         foreach my $item (@usertools) {
 1725:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1726:         }
 1727:         $datatable .= '</tr></table>';
 1728:     }
 1729:     $datatable .= '</td>';
 1730:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1731:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1732:                       '<input type="text" name="defaultquota" value="'.
 1733:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1734:     }
 1735:     $datatable .= '</tr>';
 1736:     $typecount ++;
 1737:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1738:     $datatable .= '<tr'.$css_class.'>'.
 1739:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1740:     if ($context eq 'requestcourses') {
 1741:         $datatable .= &mt('(overrides affiliation, if set)').
 1742:                       '</td>'.
 1743:                       '<td class="LC_left_item">'.
 1744:                       '<table><tr>';
 1745:     } else {
 1746:         $datatable .= &mt('(overrides affiliation, if checked)').
 1747:                       '</td>'.
 1748:                       '<td class="LC_left_item" colspan="2">'.
 1749:                       '<br />';
 1750:     }
 1751:     my %advcell;
 1752:     foreach my $item (@usertools) {
 1753:         if ($context eq 'requestcourses') {
 1754:             my ($curroption,$currlimit);
 1755:             if (ref($settings) eq 'HASH') {
 1756:                 if (ref($settings->{$item}) eq 'HASH') {
 1757:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1758:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1759:                         $currlimit = $1;
 1760:                     }
 1761:                 }
 1762:             }
 1763:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1764:             my $checked = '';
 1765:             if ($curroption eq '') {
 1766:                 $checked = ' checked="checked"';
 1767:             }
 1768:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1769:                                '<input type="radio" name="crsreq_'.$item.
 1770:                                '__LC_adv" value=""'.$checked.' />'.
 1771:                                &mt('No override set').'</label></span>&nbsp; ';
 1772:             foreach my $option (@options) {
 1773:                 my $val = $option;
 1774:                 if ($option eq 'norequest') {
 1775:                     $val = 0;
 1776:                 }
 1777:                 if ($option eq 'validate') {
 1778:                     my $canvalidate = 0;
 1779:                     if (ref($validations{$item}) eq 'HASH') {
 1780:                         if ($validations{$item}{'_LC_adv'}) {
 1781:                             $canvalidate = 1;
 1782:                         }
 1783:                     }
 1784:                     next if (!$canvalidate);
 1785:                 }
 1786:                 my $checked = '';
 1787:                 if ($val eq $curroption) {
 1788:                     $checked = ' checked="checked"';
 1789:                 } elsif ($option eq 'autolimit') {
 1790:                     if ($curroption =~ /^autolimit/) {
 1791:                         $checked = ' checked="checked"';
 1792:                     }
 1793:                 }
 1794:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1795:                                   '<input type="radio" name="crsreq_'.$item.
 1796:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1797:                                   $titles{$option}.'</label>';
 1798:                 if ($option eq 'autolimit') {
 1799:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1800:                                        $item.'_limit__LC_adv" size="1" '.
 1801:                                        'value="'.$currlimit.'" />';
 1802:                 }
 1803:                 $advcell{$item} .= '</span> ';
 1804:                 if ($option eq 'autolimit') {
 1805:                     $advcell{$item} .= $titles{'unlimited'};
 1806:                 }
 1807:             }
 1808:         } elsif ($context eq 'requestauthor') {
 1809:             my $curroption;
 1810:             if (ref($settings) eq 'HASH') {
 1811:                 $curroption = $settings->{'_LC_adv'};
 1812:             }
 1813:             my $checked = '';
 1814:             if ($curroption eq '') {
 1815:                 $checked = ' checked="checked"';
 1816:             }
 1817:             $datatable .= '<span class="LC_nobreak"><label>'.
 1818:                           '<input type="radio" name="authorreq__LC_adv"'.
 1819:                           ' value=""'.$checked.' />'.
 1820:                           &mt('No override set').'</label></span>&nbsp; ';
 1821:             foreach my $option (@options) {
 1822:                 my $val = $option;
 1823:                 if ($option eq 'norequest') {
 1824:                     $val = 0;
 1825:                 }
 1826:                 my $checked = '';
 1827:                 if ($val eq $curroption) {
 1828:                     $checked = ' checked="checked"';
 1829:                 }
 1830:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1831:                               '<input type="radio" name="crsreq_'.$item.
 1832:                               '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1833:                               $titles{$option}.'</label></span>&nbsp; ';
 1834:             }
 1835:         } else {
 1836:             my $checked = 'checked="checked" ';
 1837:             if (ref($settings) eq 'HASH') {
 1838:                 if (ref($settings->{$item}) eq 'HASH') {
 1839:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1840:                         $checked = '';
 1841:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1842:                         $checked = 'checked="checked" ';
 1843:                     }
 1844:                 }
 1845:             }
 1846:             $datatable .= '<span class="LC_nobreak"><label>'.
 1847:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1848:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1849:                           '</label></span>&nbsp; ';
 1850:         }
 1851:     }
 1852:     if ($context eq 'requestcourses') {
 1853:         $datatable .= '</tr><tr>';
 1854:         foreach my $item (@usertools) {
 1855:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1856:         }
 1857:         $datatable .= '</tr></table>';
 1858:     }
 1859:     $datatable .= '</td></tr>';
 1860:     $$rowtotal += $typecount;
 1861:     return $datatable;
 1862: }
 1863: 
 1864: sub print_requestmail {
 1865:     my ($dom,$action,$settings,$rowtotal) = @_;
 1866:     my ($now,$datatable,%dompersonnel,@domcoord,@currapproval,$rows);
 1867:     $now = time;
 1868:     $rows = 0;
 1869:     %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 1870:     foreach my $server (keys(%dompersonnel)) {
 1871:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 1872:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 1873:             if (!grep(/^$uname:$udom$/,@domcoord)) {
 1874:                 push(@domcoord,$uname.':'.$udom);
 1875:             }
 1876:         }
 1877:     }
 1878:     if (ref($settings) eq 'HASH') {
 1879:         if (ref($settings->{'notify'}) eq 'HASH') {
 1880:             if ($settings->{'notify'}{'approval'} ne '') {
 1881:                @currapproval = split(',',$settings->{'notify'}{'approval'});
 1882:             }
 1883:         }
 1884:     }
 1885:     if (@currapproval) {
 1886:         foreach my $dc (@currapproval) {
 1887:             unless (grep(/^\Q$dc\E$/,@domcoord)) {
 1888:                 push(@domcoord,$dc);
 1889:             }
 1890:         }
 1891:     }
 1892:     @domcoord = sort(@domcoord);
 1893:     my $numinrow = 4;
 1894:     my $numdc = @domcoord;
 1895:     my $css_class = 'class="LC_odd_row"';
 1896:     my $text;
 1897:     if ($action eq 'requestcourses') {
 1898:         $text = &mt('Receive notification of course requests requiring approval');
 1899:     } else {
 1900:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1901:     }
 1902:     $datatable = '<tr '.$css_class.'>'.
 1903:                  ' <td>'.$text.'</td>'.
 1904:                  ' <td class="LC_left_item">';
 1905:     if (@domcoord > 0) {
 1906:         $datatable .= '<table>';
 1907:         for (my $i=0; $i<$numdc; $i++) {
 1908:             my $rem = $i%($numinrow);
 1909:             if ($rem == 0) {
 1910:                 if ($i > 0) {
 1911:                     $datatable .= '</tr>';
 1912:                 }
 1913:                 $datatable .= '<tr>';
 1914:                 $rows ++;
 1915:             }
 1916:             my $check = ' ';
 1917:             if (grep(/^\Q$domcoord[$i]\E$/,@currapproval)) {
 1918:                 $check = ' checked="checked" ';
 1919:             }
 1920:             my ($uname,$udom) = split(':',$domcoord[$i]);
 1921:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
 1922:             if ($i == $numdc-1) {
 1923:                 my $colsleft = $numinrow-$rem;
 1924:                 if ($colsleft > 1) {
 1925:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 1926:                 } else {
 1927:                     $datatable .= '<td class="LC_left_item">';
 1928:                 }
 1929:             } else {
 1930:                 $datatable .= '<td class="LC_left_item">';
 1931:             }
 1932:             $datatable .= '<span class="LC_nobreak"><label>'.
 1933:                           '<input type="checkbox" name="reqapprovalnotify" '.
 1934:                           'value="'.$domcoord[$i].'"'.$check.'/>'.
 1935:                           $fullname.'</label></span></td>';
 1936:         }
 1937:         $datatable .= '</tr></table>';
 1938:     } else {
 1939:         $datatable .= &mt('There are no active Domain Coordinators');
 1940:         $rows ++;
 1941:     }
 1942:     $datatable .='</td></tr>';
 1943:     $$rowtotal += $rows;
 1944:     return $datatable;
 1945: }
 1946: 
 1947: sub print_autoenroll {
 1948:     my ($dom,$settings,$rowtotal) = @_;
 1949:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1950:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1951:     if (ref($settings) eq 'HASH') {
 1952:         if (exists($settings->{'run'})) {
 1953:             if ($settings->{'run'} eq '0') {
 1954:                 $runoff = ' checked="checked" ';
 1955:                 $runon = ' ';
 1956:             } else {
 1957:                 $runon = ' checked="checked" ';
 1958:                 $runoff = ' ';
 1959:             }
 1960:         } else {
 1961:             if ($autorun) {
 1962:                 $runon = ' checked="checked" ';
 1963:                 $runoff = ' ';
 1964:             } else {
 1965:                 $runoff = ' checked="checked" ';
 1966:                 $runon = ' ';
 1967:             }
 1968:         }
 1969:         if (exists($settings->{'co-owners'})) {
 1970:             if ($settings->{'co-owners'} eq '0') {
 1971:                 $coownersoff = ' checked="checked" ';
 1972:                 $coownerson = ' ';
 1973:             } else {
 1974:                 $coownerson = ' checked="checked" ';
 1975:                 $coownersoff = ' ';
 1976:             }
 1977:         } else {
 1978:             $coownersoff = ' checked="checked" ';
 1979:             $coownerson = ' ';
 1980:         }
 1981:         if (exists($settings->{'sender_domain'})) {
 1982:             $defdom = $settings->{'sender_domain'};
 1983:         }
 1984:     } else {
 1985:         if ($autorun) {
 1986:             $runon = ' checked="checked" ';
 1987:             $runoff = ' ';
 1988:         } else {
 1989:             $runoff = ' checked="checked" ';
 1990:             $runon = ' ';
 1991:         }
 1992:     }
 1993:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 1994:     my $notif_sender;
 1995:     if (ref($settings) eq 'HASH') {
 1996:         $notif_sender = $settings->{'sender_uname'};
 1997:     }
 1998:     my $datatable='<tr class="LC_odd_row">'.
 1999:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2000:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2001:                   '<input type="radio" name="autoenroll_run"'.
 2002:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2003:                   '<label><input type="radio" name="autoenroll_run"'.
 2004:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2005:                   '</tr><tr>'.
 2006:                   '<td>'.&mt('Notification messages - sender').
 2007:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2008:                   &mt('username').':&nbsp;'.
 2009:                   '<input type="text" name="sender_uname" value="'.
 2010:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2011:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2012:                   '<tr class="LC_odd_row">'.
 2013:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2014:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2015:                   '<input type="radio" name="autoassign_coowners"'.
 2016:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2017:                   '<label><input type="radio" name="autoassign_coowners"'.
 2018:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2019:                   '</tr>';
 2020:     $$rowtotal += 3;
 2021:     return $datatable;
 2022: }
 2023: 
 2024: sub print_autoupdate {
 2025:     my ($position,$dom,$settings,$rowtotal) = @_;
 2026:     my $datatable;
 2027:     if ($position eq 'top') {
 2028:         my $updateon = ' ';
 2029:         my $updateoff = ' checked="checked" ';
 2030:         my $classlistson = ' ';
 2031:         my $classlistsoff = ' checked="checked" ';
 2032:         if (ref($settings) eq 'HASH') {
 2033:             if ($settings->{'run'} eq '1') {
 2034:                 $updateon = $updateoff;
 2035:                 $updateoff = ' ';
 2036:             }
 2037:             if ($settings->{'classlists'} eq '1') {
 2038:                 $classlistson = $classlistsoff;
 2039:                 $classlistsoff = ' ';
 2040:             }
 2041:         }
 2042:         my %title = (
 2043:                    run => 'Auto-update active?',
 2044:                    classlists => 'Update information in classlists?',
 2045:                     );
 2046:         $datatable = '<tr class="LC_odd_row">'. 
 2047:                   '<td>'.&mt($title{'run'}).'</td>'.
 2048:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2049:                   '<input type="radio" name="autoupdate_run"'.
 2050:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2051:                   '<label><input type="radio" name="autoupdate_run"'.
 2052:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2053:                   '</tr><tr>'.
 2054:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2055:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2056:                   '<label><input type="radio" name="classlists"'.
 2057:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2058:                   '<label><input type="radio" name="classlists"'.
 2059:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2060:                   '</tr>';
 2061:         $$rowtotal += 2;
 2062:     } elsif ($position eq 'middle') {
 2063:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2064:         my $numinrow = 3;
 2065:         my $locknamesettings;
 2066:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2067:                                      $dom,$numinrow,$othertitle,
 2068:                                     'lockablenames');
 2069:         $$rowtotal ++;
 2070:     } else {
 2071:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2072:         my @fields = ('lastname','firstname','middlename','generation',
 2073:                       'permanentemail','id');
 2074:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2075:         my $numrows = 0;
 2076:         if (ref($types) eq 'ARRAY') {
 2077:             if (@{$types} > 0) {
 2078:                 $datatable = 
 2079:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2080:                                          \@fields,$types,\$numrows);
 2081:                     $$rowtotal += @{$types}; 
 2082:             }
 2083:         }
 2084:         $datatable .= 
 2085:             &usertype_update_row($settings,{'default' => $othertitle},
 2086:                                  \%fieldtitles,\@fields,['default'],
 2087:                                  \$numrows);
 2088:         $$rowtotal ++;     
 2089:     }
 2090:     return $datatable;
 2091: }
 2092: 
 2093: sub print_autocreate {
 2094:     my ($dom,$settings,$rowtotal) = @_;
 2095:     my (%createon,%createoff);
 2096:     my $curr_dc;
 2097:     my @types = ('xml','req');
 2098:     if (ref($settings) eq 'HASH') {
 2099:         foreach my $item (@types) {
 2100:             $createoff{$item} = ' checked="checked" ';
 2101:             $createon{$item} = ' ';
 2102:             if (exists($settings->{$item})) {
 2103:                 if ($settings->{$item}) {
 2104:                     $createon{$item} = ' checked="checked" ';
 2105:                     $createoff{$item} = ' ';
 2106:                 }
 2107:             }
 2108:         }
 2109:         $curr_dc = $settings->{'xmldc'};
 2110:     } else {
 2111:         foreach my $item (@types) {
 2112:             $createoff{$item} = ' checked="checked" ';
 2113:             $createon{$item} = ' ';
 2114:         }
 2115:     }
 2116:     $$rowtotal += 2;
 2117:     my $datatable='<tr class="LC_odd_row">'.
 2118:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2119:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2120:                   '<input type="radio" name="autocreate_xml"'.
 2121:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2122:                   '<label><input type="radio" name="autocreate_xml"'.
 2123:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2124:                   '</td></tr><tr>'.
 2125:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2126:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2127:                   '<input type="radio" name="autocreate_req"'.
 2128:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2129:                   '<label><input type="radio" name="autocreate_req"'.
 2130:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2131:     my ($numdc,$dctable) = &active_dc_picker($dom,$curr_dc);
 2132:     if ($numdc > 1) {
 2133:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2134:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2135:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2136:         $$rowtotal ++ ;
 2137:     } else {
 2138:         $datatable .= $dctable.'</td></tr>';
 2139:     }
 2140:     return $datatable;
 2141: }
 2142: 
 2143: sub print_directorysrch {
 2144:     my ($dom,$settings,$rowtotal) = @_;
 2145:     my $srchon = ' ';
 2146:     my $srchoff = ' checked="checked" ';
 2147:     my ($exacton,$containson,$beginson);
 2148:     my $localon = ' ';
 2149:     my $localoff = ' checked="checked" ';
 2150:     if (ref($settings) eq 'HASH') {
 2151:         if ($settings->{'available'} eq '1') {
 2152:             $srchon = $srchoff;
 2153:             $srchoff = ' ';
 2154:         }
 2155:         if ($settings->{'localonly'} eq '1') {
 2156:             $localon = $localoff;
 2157:             $localoff = ' ';
 2158:         }
 2159:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2160:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2161:                 if ($type eq 'exact') {
 2162:                     $exacton = ' checked="checked" ';
 2163:                 } elsif ($type eq 'contains') {
 2164:                     $containson = ' checked="checked" ';
 2165:                 } elsif ($type eq 'begins') {
 2166:                     $beginson = ' checked="checked" ';
 2167:                 }
 2168:             }
 2169:         } else {
 2170:             if ($settings->{'searchtypes'} eq 'exact') {
 2171:                 $exacton = ' checked="checked" ';
 2172:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2173:                 $containson = ' checked="checked" ';
 2174:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2175:                 $exacton = ' checked="checked" ';
 2176:                 $containson = ' checked="checked" ';
 2177:             }
 2178:         }
 2179:     }
 2180:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2181:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2182: 
 2183:     my $numinrow = 4;
 2184:     my $cansrchrow = 0;
 2185:     my $datatable='<tr class="LC_odd_row">'.
 2186:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2187:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2188:                   '<input type="radio" name="dirsrch_available"'.
 2189:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2190:                   '<label><input type="radio" name="dirsrch_available"'.
 2191:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2192:                   '</tr><tr>'.
 2193:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2194:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2195:                   '<input type="radio" name="dirsrch_localonly"'.
 2196:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2197:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2198:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2199:                   '</tr>';
 2200:     $$rowtotal += 2;
 2201:     if (ref($usertypes) eq 'HASH') {
 2202:         if (keys(%{$usertypes}) > 0) {
 2203:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2204:                                          $numinrow,$othertitle,'cansearch');
 2205:             $cansrchrow = 1;
 2206:         }
 2207:     }
 2208:     if ($cansrchrow) {
 2209:         $$rowtotal ++;
 2210:         $datatable .= '<tr>';
 2211:     } else {
 2212:         $datatable .= '<tr class="LC_odd_row">';
 2213:     }
 2214:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2215:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2216:     foreach my $title (@{$titleorder}) {
 2217:         if (defined($searchtitles->{$title})) {
 2218:             my $check = ' ';
 2219:             if (ref($settings) eq 'HASH') {
 2220:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2221:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2222:                         $check = ' checked="checked" ';
 2223:                     }
 2224:                 }
 2225:             }
 2226:             $datatable .= '<td class="LC_left_item">'.
 2227:                           '<span class="LC_nobreak"><label>'.
 2228:                           '<input type="checkbox" name="searchby" '.
 2229:                           'value="'.$title.'"'.$check.'/>'.
 2230:                           $searchtitles->{$title}.'</label></span></td>';
 2231:         }
 2232:     }
 2233:     $datatable .= '</tr></table></td></tr>';
 2234:     $$rowtotal ++;
 2235:     if ($cansrchrow) {
 2236:         $datatable .= '<tr class="LC_odd_row">';
 2237:     } else {
 2238:         $datatable .= '<tr>';
 2239:     }
 2240:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2241:                   '<td class="LC_left_item" colspan="2">'.
 2242:                   '<span class="LC_nobreak"><label>'.
 2243:                   '<input type="checkbox" name="searchtypes" '.
 2244:                   $exacton.' value="exact" />'.&mt('Exact match').
 2245:                   '</label>&nbsp;'.
 2246:                   '<label><input type="checkbox" name="searchtypes" '.
 2247:                   $beginson.' value="begins" />'.&mt('Begins with').
 2248:                   '</label>&nbsp;'.
 2249:                   '<label><input type="checkbox" name="searchtypes" '.
 2250:                   $containson.' value="contains" />'.&mt('Contains').
 2251:                   '</label></span></td></tr>';
 2252:     $$rowtotal ++;
 2253:     return $datatable;
 2254: }
 2255: 
 2256: sub print_contacts {
 2257:     my ($dom,$settings,$rowtotal) = @_;
 2258:     my $datatable;
 2259:     my @contacts = ('adminemail','supportemail');
 2260:     my (%checked,%to,%otheremails,%bccemails);
 2261:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2262:                     'requestsmail');
 2263:     foreach my $type (@mailings) {
 2264:         $otheremails{$type} = '';
 2265:     }
 2266:     $bccemails{'helpdeskmail'} = '';
 2267:     if (ref($settings) eq 'HASH') {
 2268:         foreach my $item (@contacts) {
 2269:             if (exists($settings->{$item})) {
 2270:                 $to{$item} = $settings->{$item};
 2271:             }
 2272:         }
 2273:         foreach my $type (@mailings) {
 2274:             if (exists($settings->{$type})) {
 2275:                 if (ref($settings->{$type}) eq 'HASH') {
 2276:                     foreach my $item (@contacts) {
 2277:                         if ($settings->{$type}{$item}) {
 2278:                             $checked{$type}{$item} = ' checked="checked" ';
 2279:                         }
 2280:                     }
 2281:                     $otheremails{$type} = $settings->{$type}{'others'};
 2282:                     if ($type eq 'helpdeskmail') {
 2283:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2284:                     }
 2285:                 }
 2286:             } elsif ($type eq 'lonstatusmail') {
 2287:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2288:             }
 2289:         }
 2290:     } else {
 2291:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2292:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2293:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2294:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2295:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2296:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2297:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2298:     }
 2299:     my ($titles,$short_titles) = &contact_titles();
 2300:     my $rownum = 0;
 2301:     my $css_class;
 2302:     foreach my $item (@contacts) {
 2303:         $rownum ++;
 2304:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2305:         $datatable .= '<tr'.$css_class.'>'. 
 2306:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2307:                   '</span></td><td class="LC_right_item">'.
 2308:                   '<input type="text" name="'.$item.'" value="'.
 2309:                   $to{$item}.'" /></td></tr>';
 2310:     }
 2311:     foreach my $type (@mailings) {
 2312:         $rownum ++;
 2313:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2314:         $datatable .= '<tr'.$css_class.'>'.
 2315:                       '<td><span class="LC_nobreak">'.
 2316:                       $titles->{$type}.': </span></td>'.
 2317:                       '<td class="LC_left_item">'.
 2318:                       '<span class="LC_nobreak">';
 2319:         foreach my $item (@contacts) {
 2320:             $datatable .= '<label>'.
 2321:                           '<input type="checkbox" name="'.$type.'"'.
 2322:                           $checked{$type}{$item}.
 2323:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2324:                           '</label>&nbsp;';
 2325:         }
 2326:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2327:                       '<input type="text" name="'.$type.'_others" '.
 2328:                       'value="'.$otheremails{$type}.'"  />';
 2329:         if ($type eq 'helpdeskmail') {
 2330:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2331:                           '<input type="text" name="'.$type.'_bcc" '.
 2332:                           'value="'.$bccemails{$type}.'"  />';
 2333:         }
 2334:         $datatable .= '</td></tr>'."\n";
 2335:     }
 2336:     $$rowtotal += $rownum;
 2337:     return $datatable;
 2338: }
 2339: 
 2340: sub print_helpsettings {
 2341:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2342:     my ($datatable,$itemcount);
 2343:     $itemcount = 1;
 2344:     my (%choices,%defaultchecked,@toggles);
 2345:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2346:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2347:                                  &mt('LON-CAPA bug tracker'),600,500));
 2348:     %defaultchecked = ('submitbugs' => 'on');
 2349:     @toggles = ('submitbugs',);
 2350: 
 2351:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2352:                                                  \%choices,$itemcount);
 2353:     return $datatable;
 2354: }
 2355: 
 2356: sub radiobutton_prefs {
 2357:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount) = @_;
 2358:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2359:                    (ref($choices) eq 'HASH'));
 2360: 
 2361:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2362: 
 2363:     foreach my $item (@{$toggles}) {
 2364:         if ($defaultchecked->{$item} eq 'on') {
 2365:             $checkedon{$item} = ' checked="checked" ';
 2366:             $checkedoff{$item} = ' ';
 2367:         } elsif ($defaultchecked->{$item} eq 'off') {
 2368:             $checkedoff{$item} = ' checked="checked" ';
 2369:             $checkedon{$item} = ' ';
 2370:         }
 2371:     }
 2372:     if (ref($settings) eq 'HASH') {
 2373:         foreach my $item (@{$toggles}) {
 2374:             if ($settings->{$item} eq '1') {
 2375:                 $checkedon{$item} =  ' checked="checked" ';
 2376:                 $checkedoff{$item} = ' ';
 2377:             } elsif ($settings->{$item} eq '0') {
 2378:                 $checkedoff{$item} =  ' checked="checked" ';
 2379:                 $checkedon{$item} = ' ';
 2380:             }
 2381:         }
 2382:     }
 2383:     foreach my $item (@{$toggles}) {
 2384:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2385:         $datatable .=
 2386:             '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices->{$item}.
 2387:             '</span></td>'.
 2388:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2389:             '<label><input type="radio" name="'.
 2390:             $item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').
 2391:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2392:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2393:             '</span></td>'.
 2394:             '</tr>';
 2395:         $itemcount ++;
 2396:     }
 2397:     return ($datatable,$itemcount);
 2398: }
 2399: 
 2400: sub print_coursedefaults {
 2401:     my ($position,$dom,$settings,$rowtotal) = @_;
 2402:     my ($css_class,$datatable);
 2403:     my $itemcount = 1;
 2404:     if ($position eq 'top') {
 2405:         my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2406:         %choices =
 2407:             &Apache::lonlocal::texthash (
 2408:                 canuse_pdfforms => 'Course/Community users can create/upload PDF forms',
 2409:         );
 2410:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2411:         @toggles = ('canuse_pdfforms',);
 2412:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2413:                                                  \%choices,$itemcount);
 2414:         $$rowtotal += $itemcount;
 2415:     } else {
 2416:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2417:         my %choices =
 2418:             &Apache::lonlocal::texthash (
 2419:                 anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2420:         );
 2421:         my $currdefresponder;
 2422:         if (ref($settings) eq 'HASH') {
 2423:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2424:         }
 2425:         if (!$currdefresponder) {
 2426:             $currdefresponder = 10;
 2427:         } elsif ($currdefresponder < 1) {
 2428:             $currdefresponder = 1;
 2429:         }
 2430:         $datatable .=
 2431:                '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices{'anonsurvey_threshold'}.
 2432:                 '</span></td>'.
 2433:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2434:                 '<input type="text" name="anonsurvey_threshold"'.
 2435:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2436:                 '</td></tr>';
 2437:     }
 2438:     return $datatable;
 2439: }
 2440: 
 2441: sub print_usersessions {
 2442:     my ($position,$dom,$settings,$rowtotal) = @_;
 2443:     my ($css_class,$datatable,%checked,%choices);
 2444:     my (%by_ip,%by_location,@intdoms);
 2445:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2446: 
 2447:     my @alldoms = &Apache::lonnet::all_domains();
 2448:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2449:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2450:     my %altids = &id_for_thisdom(%servers);
 2451:     my $itemcount = 1;
 2452:     if ($position eq 'top') {
 2453:         if (keys(%serverhomes) > 1) {
 2454:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2455:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2456:         } else {
 2457:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2458:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2459:         }
 2460:     } else {
 2461:         if (keys(%by_location) == 0) {
 2462:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2463:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2464:         } else {
 2465:             my %lt = &usersession_titles();
 2466:             my $numinrow = 5;
 2467:             my $prefix;
 2468:             my @types;
 2469:             if ($position eq 'bottom') {
 2470:                 $prefix = 'remote';
 2471:                 @types = ('version','excludedomain','includedomain');
 2472:             } else {
 2473:                 $prefix = 'hosted';
 2474:                 @types = ('excludedomain','includedomain');
 2475:             }
 2476:             my (%current,%checkedon,%checkedoff);
 2477:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2478:             my @locations = sort(keys(%by_location));
 2479:             foreach my $type (@types) {
 2480:                 $checkedon{$type} = '';
 2481:                 $checkedoff{$type} = ' checked="checked"';
 2482:             }
 2483:             if (ref($settings) eq 'HASH') {
 2484:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2485:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2486:                         $current{$key} = $settings->{$prefix}{$key};
 2487:                         if ($key eq 'version') {
 2488:                             if ($current{$key} ne '') {
 2489:                                 $checkedon{$key} = ' checked="checked"';
 2490:                                 $checkedoff{$key} = '';
 2491:                             }
 2492:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2493:                             $checkedon{$key} = ' checked="checked"';
 2494:                             $checkedoff{$key} = '';
 2495:                         }
 2496:                     }
 2497:                 }
 2498:             }
 2499:             foreach my $type (@types) {
 2500:                 next if ($type ne 'version' && !@locations);
 2501:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2502:                 $datatable .= '<tr'.$css_class.'>
 2503:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2504:                                <span class="LC_nobreak">&nbsp;
 2505:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2506:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2507:                 if ($type eq 'version') {
 2508:                     my $selector = '<select name="'.$prefix.'_version">';
 2509:                     foreach my $version (@lcversions) {
 2510:                         my $selected = '';
 2511:                         if ($current{'version'} eq $version) {
 2512:                             $selected = ' selected="selected"';
 2513:                         }
 2514:                         $selector .= ' <option value="'.$version.'"'.
 2515:                                      $selected.'>'.$version.'</option>';
 2516:                     }
 2517:                     $selector .= '</select> ';
 2518:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2519:                 } else {
 2520:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2521:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2522:                                  ' />'.('&nbsp;'x2).
 2523:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2524:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2525:                                  "\n".
 2526:                                  '</div><div><table>';
 2527:                     my $rem;
 2528:                     for (my $i=0; $i<@locations; $i++) {
 2529:                         my ($showloc,$value,$checkedtype);
 2530:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2531:                             my $ip = $by_location{$locations[$i]}->[0];
 2532:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2533:                                  $value = join(':',@{$by_ip{$ip}});
 2534:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2535:                                 if (ref($current{$type}) eq 'ARRAY') {
 2536:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2537:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2538:                                             $checkedtype = ' checked="checked"';
 2539:                                             last;
 2540:                                         }
 2541:                                     }
 2542:                                 }
 2543:                             }
 2544:                         }
 2545:                         $rem = $i%($numinrow);
 2546:                         if ($rem == 0) {
 2547:                             if ($i > 0) {
 2548:                                 $datatable .= '</tr>';
 2549:                             }
 2550:                             $datatable .= '<tr>';
 2551:                         }
 2552:                         $datatable .= '<td class="LC_left_item">'.
 2553:                                       '<span class="LC_nobreak"><label>'.
 2554:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2555:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2556:                                       '</label></span></td>';
 2557:                     }
 2558:                     $rem = @locations%($numinrow);
 2559:                     my $colsleft = $numinrow - $rem;
 2560:                     if ($colsleft > 1 ) {
 2561:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2562:                                       '&nbsp;</td>';
 2563:                     } elsif ($colsleft == 1) {
 2564:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2565:                     }
 2566:                     $datatable .= '</tr></table>';
 2567:                 }
 2568:                 $datatable .= '</td></tr>';
 2569:                 $itemcount ++;
 2570:             }
 2571:         }
 2572:     }
 2573:     $$rowtotal += $itemcount;
 2574:     return $datatable;
 2575: }
 2576: 
 2577: sub build_location_hashes {
 2578:     my ($intdoms,$by_ip,$by_location) = @_;
 2579:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2580:                   (ref($by_location) eq 'HASH')); 
 2581:     my %iphost = &Apache::lonnet::get_iphost();
 2582:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2583:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2584:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2585:         foreach my $id (@{$iphost{$primary_ip}}) {
 2586:             my $intdom = &Apache::lonnet::internet_dom($id);
 2587:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2588:                 push(@{$intdoms},$intdom);
 2589:             }
 2590:         }
 2591:     }
 2592:     foreach my $ip (keys(%iphost)) {
 2593:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2594:             foreach my $id (@{$iphost{$ip}}) {
 2595:                 my $location = &Apache::lonnet::internet_dom($id);
 2596:                 if ($location) {
 2597:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2598:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2599:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2600:                             push(@{$by_ip->{$ip}},$location);
 2601:                         }
 2602:                     } else {
 2603:                         $by_ip->{$ip} = [$location];
 2604:                     }
 2605:                 }
 2606:             }
 2607:         }
 2608:     }
 2609:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2610:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2611:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2612:             my $first = $by_ip->{$ip}->[0];
 2613:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2614:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2615:                     push(@{$by_location->{$first}},$ip);
 2616:                 }
 2617:             } else {
 2618:                 $by_location->{$first} = [$ip];
 2619:             }
 2620:         }
 2621:     }
 2622:     return;
 2623: }
 2624: 
 2625: sub current_offloads_to {
 2626:     my ($dom,$settings,$servers) = @_;
 2627:     my (%spareid,%otherdomconfigs);
 2628:     if (ref($servers) eq 'HASH') {
 2629:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2630:             my $gotspares;
 2631:             if (ref($settings) eq 'HASH') {
 2632:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2633:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2634:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2635:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2636:                         $gotspares = 1;
 2637:                     }
 2638:                 }
 2639:             }
 2640:             unless ($gotspares) {
 2641:                 my $gotspares;
 2642:                 my $serverhomeID =
 2643:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2644:                 my $serverhomedom =
 2645:                     &Apache::lonnet::host_domain($serverhomeID);
 2646:                 if ($serverhomedom ne $dom) {
 2647:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2648:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2649:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2650:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2651:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2652:                                 $gotspares = 1;
 2653:                             }
 2654:                         }
 2655:                     } else {
 2656:                         $otherdomconfigs{$serverhomedom} =
 2657:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2658:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2659:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2660:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2661:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2662:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2663:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2664:                                         $gotspares = 1;
 2665:                                     }
 2666:                                 }
 2667:                             }
 2668:                         }
 2669:                     }
 2670:                 }
 2671:             }
 2672:             unless ($gotspares) {
 2673:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2674:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2675:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2676:                } else {
 2677:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2678:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2679:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2680:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2681:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2682:                     } else {
 2683:                         my %what = (
 2684:                              spareid => 1,
 2685:                         );
 2686:                         my ($result,$returnhash) = 
 2687:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2688:                         if ($result eq 'ok') { 
 2689:                             if (ref($returnhash) eq 'HASH') {
 2690:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2691:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2692:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2693:                                 }
 2694:                             }
 2695:                         }
 2696:                     }
 2697:                 }
 2698:             }
 2699:         }
 2700:     }
 2701:     return %spareid;
 2702: }
 2703: 
 2704: sub spares_row {
 2705:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2706:     my $css_class;
 2707:     my $numinrow = 4;
 2708:     my $itemcount = 1;
 2709:     my $datatable;
 2710:     my %typetitles = &sparestype_titles();
 2711:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2712:         foreach my $server (sort(keys(%{$servers}))) {
 2713:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2714:             my ($othercontrol,$serverdom);
 2715:             if ($serverhome ne $server) {
 2716:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2717:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2718:             } else {
 2719:                 $serverdom = &Apache::lonnet::host_domain($server);
 2720:                 if ($serverdom ne $dom) {
 2721:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2722:                 }
 2723:             }
 2724:             next unless (ref($spareid->{$server}) eq 'HASH');
 2725:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2726:             $datatable .= '<tr'.$css_class.'>
 2727:                            <td rowspan="2">
 2728:                             <span class="LC_nobreak"><b>'.$server.'</b> when busy, offloads to:</span></td>'."\n";
 2729:             my (%current,%canselect);
 2730:             my @choices = 
 2731:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2732:             foreach my $type ('primary','default') {
 2733:                 if (ref($spareid->{$server}) eq 'HASH') {
 2734:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2735:                         my @spares = @{$spareid->{$server}{$type}};
 2736:                         if (@spares > 0) {
 2737:                             if ($othercontrol) {
 2738:                                 $current{$type} = join(', ',@spares);
 2739:                             } else {
 2740:                                 $current{$type} .= '<table>';
 2741:                                 my $numspares = scalar(@spares);
 2742:                                 for (my $i=0;  $i<@spares; $i++) {
 2743:                                     my $rem = $i%($numinrow);
 2744:                                     if ($rem == 0) {
 2745:                                         if ($i > 0) {
 2746:                                             $current{$type} .= '</tr>';
 2747:                                         }
 2748:                                         $current{$type} .= '<tr>';
 2749:                                     }
 2750:                                     $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;'.
 2751:                                                        $spareid->{$server}{$type}[$i].
 2752:                                                        '</label></td>'."\n";
 2753:                                 }
 2754:                                 my $rem = @spares%($numinrow);
 2755:                                 my $colsleft = $numinrow - $rem;
 2756:                                 if ($colsleft > 1 ) {
 2757:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2758:                                                        '" class="LC_left_item">'.
 2759:                                                        '&nbsp;</td>';
 2760:                                 } elsif ($colsleft == 1) {
 2761:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2762:                                 }
 2763:                                 $current{$type} .= '</tr></table>';
 2764:                             }
 2765:                         }
 2766:                     }
 2767:                     if ($current{$type} eq '') {
 2768:                         $current{$type} = &mt('None specified');
 2769:                     }
 2770:                     if ($othercontrol) {
 2771:                         if ($type eq 'primary') {
 2772:                             $canselect{$type} = $othercontrol;
 2773:                         }
 2774:                     } else {
 2775:                         $canselect{$type} = 
 2776:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2777:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2778:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2779:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2780:                         if (@choices > 0) {
 2781:                             foreach my $lonhost (@choices) {
 2782:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2783:                             }
 2784:                         }
 2785:                         $canselect{$type} .= '</select>'."\n";
 2786:                     }
 2787:                 } else {
 2788:                     $current{$type} = &mt('Could not be determined');
 2789:                     if ($type eq 'primary') {
 2790:                         $canselect{$type} =  $othercontrol;
 2791:                     }
 2792:                 }
 2793:                 if ($type eq 'default') {
 2794:                     $datatable .= '<tr'.$css_class.'>';
 2795:                 }
 2796:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2797:                               '<td>'.$current{$type}.'</td>'."\n".
 2798:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2799:             }
 2800:             $itemcount ++;
 2801:         }
 2802:     }
 2803:     $$rowtotal += $itemcount;
 2804:     return $datatable;
 2805: }
 2806: 
 2807: sub possible_newspares {
 2808:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2809:     my $serverhostname = &Apache::lonnet::hostname($server);
 2810:     my %excluded;
 2811:     if ($serverhostname ne '') {
 2812:         %excluded = (
 2813:                        $serverhostname => 1,
 2814:                     );
 2815:     }
 2816:     if (ref($currspares) eq 'HASH') {
 2817:         foreach my $type (keys(%{$currspares})) {
 2818:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2819:                 if (@{$currspares->{$type}} > 0) {
 2820:                     foreach my $curr (@{$currspares->{$type}}) {
 2821:                         my $hostname = &Apache::lonnet::hostname($curr);
 2822:                         $excluded{$hostname} = 1;
 2823:                     }
 2824:                 }
 2825:             }
 2826:         }
 2827:     }
 2828:     my @choices;
 2829:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2830:         if (keys(%{$serverhomes}) > 1) {
 2831:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2832:                 unless ($excluded{$name}) {
 2833:                     if (exists($altids->{$serverhomes->{$name}})) {
 2834:                         push(@choices,$altids->{$serverhomes->{$name}});
 2835:                     } else {
 2836:                         push(@choices,$serverhomes->{$name});
 2837:                     }
 2838:                 }
 2839:             }
 2840:         }
 2841:     }
 2842:     return sort(@choices);
 2843: }
 2844: 
 2845: sub print_loadbalancing {
 2846:     my ($dom,$settings,$rowtotal) = @_;
 2847:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2848:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2849:     my $numinrow = 1;
 2850:     my $datatable;
 2851:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2852:     my ($currbalancer,$currtargets,$currrules);
 2853:     if (keys(%servers) > 1) {
 2854:         if (ref($settings) eq 'HASH') {
 2855:             $currbalancer = $settings->{'lonhost'};
 2856:             $currtargets = $settings->{'targets'};
 2857:             $currrules = $settings->{'rules'};
 2858:         } else {
 2859:             ($currbalancer,$currtargets) = 
 2860:                 &Apache::lonnet::get_lonbalancer_config(\%servers);
 2861:         }
 2862:     } else {
 2863:         return;
 2864:     }
 2865:     my ($othertitle,$usertypes,$types) =
 2866:         &Apache::loncommon::sorted_inst_types($dom);
 2867:     my $rownum = 6;
 2868:     if (ref($types) eq 'ARRAY') {
 2869:         $rownum += scalar(@{$types});
 2870:     }
 2871:     my $css_class = ' class="LC_odd_row"';
 2872:     my $targets_div_style = 'display: none';
 2873:     my $disabled_div_style = 'display: block';
 2874:     my $homedom_div_style = 'display: none';
 2875:     $datatable = '<tr'.$css_class.'>'.
 2876:                  '<td rowspan="'.$rownum.'" valign="top">'.
 2877:                  '<p><select name="loadbalancing_lonhost" onchange="toggleTargets();">'."\n".
 2878:                  '<option value=""';
 2879:     if (($currbalancer eq '') || (!grep(/^\Q$currbalancer\E$/,keys(%servers)))) {
 2880:         $datatable .= ' selected="selected"';
 2881:     } else {
 2882:         $targets_div_style = 'display: block';
 2883:         $disabled_div_style = 'display: none';
 2884:         if ($dom eq &Apache::lonnet::host_domain($currbalancer)) {
 2885:             $homedom_div_style = 'display: block'; 
 2886:         }
 2887:     }
 2888:     $datatable .= '>'.&mt('None').'</option>'."\n";
 2889:     foreach my $lonhost (sort(keys(%servers))) {
 2890:         my $selected;
 2891:         if ($lonhost eq $currbalancer) {
 2892:             $selected .= ' selected="selected"';
 2893:         }
 2894:         $datatable .= '<option value="'.$lonhost.'"'.$selected.'>'.$lonhost.'</option>'."\n";
 2895:     }
 2896:     $datatable .= '</select></p></td><td rowspan="'.$rownum.'" valign="top">'.
 2897:                   '<div id="loadbalancing_disabled" style="'.$disabled_div_style.'">'.&mt('No dedicated Load Balancer').'</div>'."\n".
 2898:                   '<div id="loadbalancing_targets" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2899:     my ($numspares,@spares) = &count_servers($currbalancer,%servers);
 2900:     my @sparestypes = ('primary','default');
 2901:     my %typetitles = &sparestype_titles();
 2902:     foreach my $sparetype (@sparestypes) {
 2903:         my $targettable;
 2904:         for (my $i=0; $i<$numspares; $i++) {
 2905:             my $checked;
 2906:             if (ref($currtargets) eq 'HASH') {
 2907:                 if (ref($currtargets->{$sparetype}) eq 'ARRAY') {
 2908:                     if (grep(/^\Q$spares[$i]\E$/,@{$currtargets->{$sparetype}})) {
 2909:                         $checked = ' checked="checked"';
 2910:                     }
 2911:                 }
 2912:             }
 2913:             my $chkboxval;
 2914:             if (($currbalancer ne '') && (grep((/^\Q$currbalancer\E$/,keys(%servers))))) {
 2915:                 $chkboxval = $spares[$i];
 2916:             }
 2917:             $targettable .= '<td><label><input type="checkbox" name="loadbalancing_target_'.$sparetype.'"'.
 2918:                       $checked.' value="'.$chkboxval.'" id="loadbalancing_target_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$sparetype'".');" /><span id="loadbalancing_targettxt_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 2919:                       '</span></label></td>';
 2920:             my $rem = $i%($numinrow);
 2921:             if ($rem == 0) {
 2922:                 if ($i > 0) {
 2923:                     $targettable .= '</tr>';
 2924:                 }
 2925:                 $targettable .= '<tr>';
 2926:             }
 2927:         }
 2928:         if ($targettable ne '') {
 2929:             my $rem = $numspares%($numinrow);
 2930:             my $colsleft = $numinrow - $rem;
 2931:             if ($colsleft > 1 ) {
 2932:                 $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2933:                                 '&nbsp;</td>';
 2934:             } elsif ($colsleft == 1) {
 2935:                 $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 2936:             }
 2937:             $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 2938:                            '<table><tr>'.$targettable.'</table><br />';
 2939:         }
 2940:     }
 2941:     $datatable .= '</div></td></tr>'.
 2942:                   &loadbalancing_rules($dom,$intdom,$currrules,$othertitle,
 2943:                                        $usertypes,$types,\%servers,$currbalancer,
 2944:                                        $targets_div_style,$homedom_div_style,$css_class);
 2945:     $$rowtotal += $rownum;
 2946:     return $datatable;
 2947: }
 2948: 
 2949: sub loadbalancing_rules {
 2950:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 2951:         $currbalancer,$targets_div_style,$homedom_div_style,$css_class) = @_;
 2952:     my $output;
 2953:     my ($alltypes,$othertypes,$titles) = 
 2954:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 2955:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 2956:         foreach my $type (@{$alltypes}) {
 2957:             my $current;
 2958:             if (ref($currrules) eq 'HASH') {
 2959:                 $current = $currrules->{$type};
 2960:             }
 2961:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 2962:                 if ($dom ne &Apache::lonnet::host_domain($currbalancer)) {
 2963:                     $current = '';
 2964:                 }
 2965:             }
 2966:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 2967:                                              $servers,$currbalancer,$dom,
 2968:                                              $targets_div_style,$homedom_div_style,$css_class);
 2969:         }
 2970:     }
 2971:     return $output;
 2972: }
 2973: 
 2974: sub loadbalancing_titles {
 2975:     my ($dom,$intdom,$usertypes,$types) = @_;
 2976:     my %othertypes = (
 2977:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 2978:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 2979:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 2980:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 2981:                      );
 2982:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 2983:     if (ref($types) eq 'ARRAY') {
 2984:         unshift(@alltypes,@{$types},'default');
 2985:     }
 2986:     my %titles;
 2987:     foreach my $type (@alltypes) {
 2988:         if ($type =~ /^_LC_/) {
 2989:             $titles{$type} = $othertypes{$type};
 2990:         } elsif ($type eq 'default') {
 2991:             $titles{$type} = &mt('All users from [_1]',$dom);
 2992:             if (ref($types) eq 'ARRAY') {
 2993:                 if (@{$types} > 0) {
 2994:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 2995:                 }
 2996:             }
 2997:         } elsif (ref($usertypes) eq 'HASH') {
 2998:             $titles{$type} = $usertypes->{$type};
 2999:         }
 3000:     }
 3001:     return (\@alltypes,\%othertypes,\%titles);
 3002: }
 3003: 
 3004: sub loadbalance_rule_row {
 3005:     my ($type,$title,$current,$servers,$currbalancer,$dom,$targets_div_style,
 3006:         $homedom_div_style,$css_class) = @_;
 3007:     my @rulenames = ('default','homeserver');
 3008:     my %ruletitles = &offloadtype_text();
 3009:     if ($type eq '_LC_external') {
 3010:         push(@rulenames,'externalbalancer');
 3011:     } else {
 3012:         push(@rulenames,'specific');
 3013:     }
 3014:     push(@rulenames,'none');
 3015:     my $style = $targets_div_style;
 3016:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3017:         $style = $homedom_div_style;
 3018:     }
 3019:     my $output = 
 3020:         '<tr'.$css_class.'><td valign="top"><div id="balanceruletitle_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3021:         '<td><div id="balancerule_'.$type.'" style="'.$style.'">'."\n";
 3022:     for (my $i=0; $i<@rulenames; $i++) {
 3023:         my $rule = $rulenames[$i];
 3024:         my ($checked,$extra);
 3025:         if ($rulenames[$i] eq 'default') {
 3026:             $rule = '';
 3027:         }
 3028:         if ($rulenames[$i] eq 'specific') {
 3029:             if (ref($servers) eq 'HASH') {
 3030:                 my $default;
 3031:                 if (($current ne '') && (exists($servers->{$current}))) {
 3032:                     $checked = ' checked="checked"';
 3033:                 }
 3034:                 unless ($checked) {
 3035:                     $default = ' selected="selected"';
 3036:                 }
 3037:                 $extra = ':&nbsp;<select name="loadbalancing_singleserver_'.$type.
 3038:                          '" id="loadbalancing_singleserver_'.$type.
 3039:                          '" onchange="singleServerToggle('."'$type'".')">'."\n".
 3040:                          '<option value=""'.$default.'></option>'."\n";
 3041:                 foreach my $lonhost (sort(keys(%{$servers}))) {
 3042:                     next if ($lonhost eq $currbalancer);
 3043:                     my $selected;
 3044:                     if ($lonhost eq $current) {
 3045:                         $selected = ' selected="selected"';
 3046:                     }
 3047:                     $extra .= '<option value="'.$lonhost.'"'.$selected.'>'.$lonhost.'</option>';
 3048:                 }
 3049:                 $extra .= '</select>';
 3050:             }
 3051:         } elsif ($rule eq $current) {
 3052:             $checked = ' checked="checked"';
 3053:         }
 3054:         $output .= '<span class="LC_nobreak"><label>'.
 3055:                    '<input type="radio" name="loadbalancing_rules_'.$type.
 3056:                    '" id="loadbalancing_rules_'.$type.'_'.$i.'" value="'.
 3057:                    $rule.'" onclick="balanceruleChange('."this.form,'$type'".
 3058:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3059:                    '</label>'.$extra.'</span><br />'."\n";
 3060:     }
 3061:     $output .= '</div></td></tr>'."\n";
 3062:     return $output;
 3063: }
 3064: 
 3065: sub offloadtype_text {
 3066:     my %ruletitles = &Apache::lonlocal::texthash (
 3067:            'default'          => 'Offloads to default destinations',
 3068:            'homeserver'       => "Offloads to user's home server",
 3069:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3070:            'specific'         => 'Offloads to specific server',
 3071:            'none'             => 'No offload',
 3072:     );
 3073:     return %ruletitles;
 3074: }
 3075: 
 3076: sub sparestype_titles {
 3077:     my %typestitles = &Apache::lonlocal::texthash (
 3078:                           'primary' => 'primary',
 3079:                           'default' => 'default',
 3080:                       );
 3081:     return %typestitles;
 3082: }
 3083: 
 3084: sub contact_titles {
 3085:     my %titles = &Apache::lonlocal::texthash (
 3086:                    'supportemail' => 'Support E-mail address',
 3087:                    'adminemail'   => 'Default Server Admin E-mail address',
 3088:                    'errormail'    => 'Error reports to be e-mailed to',
 3089:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3090:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3091:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3092:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3093:                  );
 3094:     my %short_titles = &Apache::lonlocal::texthash (
 3095:                            adminemail   => 'Admin E-mail address',
 3096:                            supportemail => 'Support E-mail',
 3097:                        );   
 3098:     return (\%titles,\%short_titles);
 3099: }
 3100: 
 3101: sub tool_titles {
 3102:     my %titles = &Apache::lonlocal::texthash (
 3103:                      aboutme    => 'Personal web page',
 3104:                      blog       => 'Blog',
 3105:                      webdav     => 'WebDAV',
 3106:                      portfolio  => 'Portfolio',
 3107:                      official   => 'Official courses (with institutional codes)',
 3108:                      unofficial => 'Unofficial courses',
 3109:                      community  => 'Communities',
 3110:                  );
 3111:     return %titles;
 3112: }
 3113: 
 3114: sub courserequest_titles {
 3115:     my %titles = &Apache::lonlocal::texthash (
 3116:                                    official   => 'Official',
 3117:                                    unofficial => 'Unofficial',
 3118:                                    community  => 'Communities',
 3119:                                    norequest  => 'Not allowed',
 3120:                                    approval   => 'Approval by Dom. Coord.',
 3121:                                    validate   => 'With validation',
 3122:                                    autolimit  => 'Numerical limit',
 3123:                                    unlimited  => '(blank for unlimited)',
 3124:                  );
 3125:     return %titles;
 3126: }
 3127: 
 3128: sub authorrequest_titles {
 3129:     my %titles = &Apache::lonlocal::texthash (
 3130:                                    norequest  => 'Not allowed',
 3131:                                    approval   => 'Approval by Dom. Coord.',
 3132:                                    automatic  => 'Automatic approval',
 3133:                  );
 3134:     return %titles;
 3135: }
 3136: 
 3137: sub courserequest_conditions {
 3138:     my %conditions = &Apache::lonlocal::texthash (
 3139:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3140:        validate   => '(Processing of request subject to instittutional validation).',
 3141:                  );
 3142:     return %conditions;
 3143: }
 3144: 
 3145: 
 3146: sub print_usercreation {
 3147:     my ($position,$dom,$settings,$rowtotal) = @_;
 3148:     my $numinrow = 4;
 3149:     my $datatable;
 3150:     if ($position eq 'top') {
 3151:         $$rowtotal ++;
 3152:         my $rowcount = 0;
 3153:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3154:         if (ref($rules) eq 'HASH') {
 3155:             if (keys(%{$rules}) > 0) {
 3156:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3157:                                                 $ruleorder,$numinrow,$rowcount);
 3158:                 $$rowtotal ++;
 3159:                 $rowcount ++;
 3160:             }
 3161:         }
 3162:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3163:         if (ref($idrules) eq 'HASH') {
 3164:             if (keys(%{$idrules}) > 0) {
 3165:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3166:                                                 $idruleorder,$numinrow,$rowcount);
 3167:                 $$rowtotal ++;
 3168:                 $rowcount ++;
 3169:             }
 3170:         }
 3171:         my ($emailrules,$emailruleorder) = 
 3172:             &Apache::lonnet::inst_userrules($dom,'email');
 3173:         if (ref($emailrules) eq 'HASH') {
 3174:             if (keys(%{$emailrules}) > 0) {
 3175:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3176:                                                 $emailruleorder,$numinrow,$rowcount);
 3177:                 $$rowtotal ++;
 3178:                 $rowcount ++;
 3179:             }
 3180:         }
 3181:         if ($rowcount == 0) {
 3182:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3183:             $$rowtotal ++;
 3184:             $rowcount ++;
 3185:         }
 3186:     } elsif ($position eq 'middle') {
 3187:         my @creators = ('author','course','requestcrs','selfcreate');
 3188:         my ($rules,$ruleorder) =
 3189:             &Apache::lonnet::inst_userrules($dom,'username');
 3190:         my %lt = &usercreation_types();
 3191:         my %checked;
 3192:         my @selfcreate; 
 3193:         if (ref($settings) eq 'HASH') {
 3194:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3195:                 foreach my $item (@creators) {
 3196:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3197:                 }
 3198:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3199:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3200:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3201:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3202:                         @selfcreate = ('email','login','sso');
 3203:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3204:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3205:                     }
 3206:                 }
 3207:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3208:                 foreach my $item (@creators) {
 3209:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3210:                         $checked{$item} = 'none';
 3211:                     }
 3212:                 }
 3213:             }
 3214:         }
 3215:         my $rownum = 0;
 3216:         foreach my $item (@creators) {
 3217:             $rownum ++;
 3218:             if ($item ne 'selfcreate') {  
 3219:                 if ($checked{$item} eq '') {
 3220:                     $checked{$item} = 'any';
 3221:                 }
 3222:             }
 3223:             my $css_class;
 3224:             if ($rownum%2) {
 3225:                 $css_class = '';
 3226:             } else {
 3227:                 $css_class = ' class="LC_odd_row" ';
 3228:             }
 3229:             $datatable .= '<tr'.$css_class.'>'.
 3230:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3231:                          '</span></td><td align="right">';
 3232:             my @options;
 3233:             if ($item eq 'selfcreate') {
 3234:                 push(@options,('email','login','sso'));
 3235:             } else {
 3236:                 @options = ('any');
 3237:                 if (ref($rules) eq 'HASH') {
 3238:                     if (keys(%{$rules}) > 0) {
 3239:                         push(@options,('official','unofficial'));
 3240:                     }
 3241:                 }
 3242:                 push(@options,'none');
 3243:             }
 3244:             foreach my $option (@options) {
 3245:                 my $type = 'radio';
 3246:                 my $check = ' ';
 3247:                 if ($item eq 'selfcreate') {
 3248:                     $type = 'checkbox';
 3249:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3250:                         $check = ' checked="checked" ';
 3251:                     }
 3252:                 } else {
 3253:                     if ($checked{$item} eq $option) {
 3254:                         $check = ' checked="checked" ';
 3255:                     }
 3256:                 } 
 3257:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3258:                               '<input type="'.$type.'" name="can_createuser_'.
 3259:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3260:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3261:             }
 3262:             $datatable .= '</td></tr>';
 3263:         }
 3264:         my ($othertitle,$usertypes,$types) =
 3265:             &Apache::loncommon::sorted_inst_types($dom);
 3266:         my $createsettings;
 3267:         if (ref($settings) eq 'HASH') {
 3268:             $createsettings = $settings->{cancreate};
 3269:         }
 3270:         if (ref($usertypes) eq 'HASH') {
 3271:             if (keys(%{$usertypes}) > 0) {
 3272:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3273:                                              $dom,$numinrow,$othertitle,
 3274:                                              'statustocreate');
 3275:                 $$rowtotal ++;
 3276:                 $rownum ++;
 3277:             }
 3278:         }
 3279:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3280:     } else {
 3281:         my @contexts = ('author','course','domain');
 3282:         my @authtypes = ('int','krb4','krb5','loc');
 3283:         my %checked;
 3284:         if (ref($settings) eq 'HASH') {
 3285:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3286:                 foreach my $item (@contexts) {
 3287:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3288:                         foreach my $auth (@authtypes) {
 3289:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3290:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3291:                             }
 3292:                         }
 3293:                     }
 3294:                 }
 3295:             }
 3296:         } else {
 3297:             foreach my $item (@contexts) {
 3298:                 foreach my $auth (@authtypes) {
 3299:                     $checked{$item}{$auth} = ' checked="checked" ';
 3300:                 }
 3301:             }
 3302:         }
 3303:         my %title = &context_names();
 3304:         my %authname = &authtype_names();
 3305:         my $rownum = 0;
 3306:         my $css_class; 
 3307:         foreach my $item (@contexts) {
 3308:             if ($rownum%2) {
 3309:                 $css_class = '';
 3310:             } else {
 3311:                 $css_class = ' class="LC_odd_row" ';
 3312:             }
 3313:             $datatable .=   '<tr'.$css_class.'>'.
 3314:                             '<td>'.$title{$item}.
 3315:                             '</td><td class="LC_left_item">'.
 3316:                             '<span class="LC_nobreak">';
 3317:             foreach my $auth (@authtypes) {
 3318:                 $datatable .= '<label>'. 
 3319:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3320:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3321:                               $authname{$auth}.'</label>&nbsp;';
 3322:             }
 3323:             $datatable .= '</span></td></tr>';
 3324:             $rownum ++;
 3325:         }
 3326:         $$rowtotal += $rownum;
 3327:     }
 3328:     return $datatable;
 3329: }
 3330: 
 3331: sub captcha_choice {
 3332:     my ($context,$settings,$itemcount) = @_;
 3333:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3334:     my %lt = &captcha_phrases();
 3335:     $keyentry = 'hidden';
 3336:     if ($context eq 'cancreate') {
 3337:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3338:     } elsif ($context eq 'login') {
 3339:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3340:     }
 3341:     if (ref($settings) eq 'HASH') {
 3342:         if ($settings->{'captcha'}) {
 3343:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3344:         } else {
 3345:             $checked{'original'} = ' checked="checked"';
 3346:         }
 3347:         if ($settings->{'captcha'} eq 'recaptcha') {
 3348:             $pubtext = $lt{'pub'};
 3349:             $privtext = $lt{'priv'};
 3350:             $keyentry = 'text';
 3351:         }
 3352:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3353:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3354:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3355:         }
 3356:     } else {
 3357:         $checked{'original'} = ' checked="checked"';
 3358:     }
 3359:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3360:     my $output = '<tr'.$css_class.'>'.
 3361:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3362:                  '<table><tr><td>'."\n";
 3363:     foreach my $option ('original','recaptcha','notused') {
 3364:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3365:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3366:                    $lt{$option}.'</label></span>';
 3367:         unless ($option eq 'notused') {
 3368:             $output .= ('&nbsp;'x2)."\n";
 3369:         }
 3370:     }
 3371: #
 3372: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3373: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3374: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 3375: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3376: #
 3377:     $output .= '</td></tr>'."\n".
 3378:                '<tr><td>'."\n".
 3379:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3380:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3381:                $currpub.'" size="40" /></span><br />'."\n".
 3382:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3383:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3384:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3385:                '</td></tr>';
 3386:     return $output;
 3387: }
 3388: 
 3389: sub user_formats_row {
 3390:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3391:     my $output;
 3392:     my %text = (
 3393:                    'username' => 'new usernames',
 3394:                    'id'       => 'IDs',
 3395:                    'email'    => 'self-created accounts (e-mail)',
 3396:                );
 3397:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3398:     $output = '<tr '.$css_class.'>'.
 3399:               '<td><span class="LC_nobreak">';
 3400:     if ($type eq 'email') {
 3401:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3402:     } else {
 3403:         $output .= &mt("Format rules to check for $text{$type}: ");
 3404:     }
 3405:     $output .= '</span></td>'.
 3406:                '<td class="LC_left_item" colspan="2"><table>';
 3407:     my $rem;
 3408:     if (ref($ruleorder) eq 'ARRAY') {
 3409:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3410:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3411:                 my $rem = $i%($numinrow);
 3412:                 if ($rem == 0) {
 3413:                     if ($i > 0) {
 3414:                         $output .= '</tr>';
 3415:                     }
 3416:                     $output .= '<tr>';
 3417:                 }
 3418:                 my $check = ' ';
 3419:                 if (ref($settings) eq 'HASH') {
 3420:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3421:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3422:                             $check = ' checked="checked" ';
 3423:                         }
 3424:                     }
 3425:                 }
 3426:                 $output .= '<td class="LC_left_item">'.
 3427:                            '<span class="LC_nobreak"><label>'.
 3428:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3429:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3430:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3431:             }
 3432:         }
 3433:         $rem = @{$ruleorder}%($numinrow);
 3434:     }
 3435:     my $colsleft = $numinrow - $rem;
 3436:     if ($colsleft > 1 ) {
 3437:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3438:                    '&nbsp;</td>';
 3439:     } elsif ($colsleft == 1) {
 3440:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3441:     }
 3442:     $output .= '</tr></table></td></tr>';
 3443:     return $output;
 3444: }
 3445: 
 3446: sub usercreation_types {
 3447:     my %lt = &Apache::lonlocal::texthash (
 3448:                     author     => 'When adding a co-author',
 3449:                     course     => 'When adding a user to a course',
 3450:                     requestcrs => 'When requesting a course',
 3451:                     selfcreate => 'User creates own account', 
 3452:                     any        => 'Any',
 3453:                     official   => 'Institutional only ',
 3454:                     unofficial => 'Non-institutional only',
 3455:                     email      => 'E-mail address',
 3456:                     login      => 'Institutional Login',
 3457:                     sso        => 'SSO', 
 3458:                     none       => 'None',
 3459:     );
 3460:     return %lt;
 3461: }
 3462: 
 3463: sub authtype_names {
 3464:     my %lt = &Apache::lonlocal::texthash(
 3465:                       int    => 'Internal',
 3466:                       krb4   => 'Kerberos 4',
 3467:                       krb5   => 'Kerberos 5',
 3468:                       loc    => 'Local',
 3469:                   );
 3470:     return %lt;
 3471: }
 3472: 
 3473: sub context_names {
 3474:     my %context_title = &Apache::lonlocal::texthash(
 3475:        author => 'Creating users when an Author',
 3476:        course => 'Creating users when in a course',
 3477:        domain => 'Creating users when a Domain Coordinator',
 3478:     );
 3479:     return %context_title;
 3480: }
 3481: 
 3482: sub print_usermodification {
 3483:     my ($position,$dom,$settings,$rowtotal) = @_;
 3484:     my $numinrow = 4;
 3485:     my ($context,$datatable,$rowcount);
 3486:     if ($position eq 'top') {
 3487:         $rowcount = 0;
 3488:         $context = 'author'; 
 3489:         foreach my $role ('ca','aa') {
 3490:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3491:                                                    $numinrow,$rowcount);
 3492:             $$rowtotal ++;
 3493:             $rowcount ++;
 3494:         }
 3495:     } elsif ($position eq 'middle') {
 3496:         $context = 'course';
 3497:         $rowcount = 0;
 3498:         foreach my $role ('st','ep','ta','in','cr') {
 3499:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3500:                                                    $numinrow,$rowcount);
 3501:             $$rowtotal ++;
 3502:             $rowcount ++;
 3503:         }
 3504:     } elsif ($position eq 'bottom') {
 3505:         $context = 'selfcreate';
 3506:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3507:         $usertypes->{'default'} = $othertitle;
 3508:         if (ref($types) eq 'ARRAY') {
 3509:             push(@{$types},'default');
 3510:             $usertypes->{'default'} = $othertitle;
 3511:             foreach my $status (@{$types}) {
 3512:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3513:                                                        $numinrow,$rowcount,$usertypes);
 3514:                 $$rowtotal ++;
 3515:                 $rowcount ++;
 3516:             }
 3517:         }
 3518:     }
 3519:     return $datatable;
 3520: }
 3521: 
 3522: sub print_defaults {
 3523:     my ($dom,$rowtotal) = @_;
 3524:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3525:                  'datelocale_def','portal_def');
 3526:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3527:     my $titles = &defaults_titles($dom);
 3528:     my $rownum = 0;
 3529:     my ($datatable,$css_class);
 3530:     foreach my $item (@items) {
 3531:         if ($rownum%2) {
 3532:             $css_class = '';
 3533:         } else {
 3534:             $css_class = ' class="LC_odd_row" ';
 3535:         }
 3536:         $datatable .= '<tr'.$css_class.'>'.
 3537:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3538:                   '</span></td><td class="LC_right_item">';
 3539:         if ($item eq 'auth_def') {
 3540:             my @authtypes = ('internal','krb4','krb5','localauth');
 3541:             my %shortauth = (
 3542:                              internal => 'int',
 3543:                              krb4 => 'krb4',
 3544:                              krb5 => 'krb5',
 3545:                              localauth  => 'loc'
 3546:                            );
 3547:             my %authnames = &authtype_names();
 3548:             foreach my $auth (@authtypes) {
 3549:                 my $checked = ' ';
 3550:                 if ($domdefaults{$item} eq $auth) {
 3551:                     $checked = ' checked="checked" ';
 3552:                 }
 3553:                 $datatable .= '<label><input type="radio" name="'.$item.
 3554:                               '" value="'.$auth.'"'.$checked.'/>'.
 3555:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3556:             }
 3557:         } elsif ($item eq 'timezone_def') {
 3558:             my $includeempty = 1;
 3559:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3560:         } elsif ($item eq 'datelocale_def') {
 3561:             my $includeempty = 1;
 3562:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3563:         } elsif ($item eq 'lang_def') {
 3564:             my %langchoices = &get_languages_hash();
 3565:             $langchoices{''} = 'No language preference';
 3566:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 3567:             $datatable .= &Apache::loncommon::select_form($domdefaults{$item},$item,
 3568:                                                           \%langchoices);
 3569:         } else {
 3570:             my $size;
 3571:             if ($item eq 'portal_def') {
 3572:                 $size = ' size="25"';
 3573:             }
 3574:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3575:                           $domdefaults{$item}.'"'.$size.' />';
 3576:         }
 3577:         $datatable .= '</td></tr>';
 3578:         $rownum ++;
 3579:     }
 3580:     $$rowtotal += $rownum;
 3581:     return $datatable;
 3582: }
 3583: 
 3584: sub get_languages_hash {
 3585:     my %langchoices;
 3586:     foreach my $id (&Apache::loncommon::languageids()) {
 3587:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 3588:         if ($code ne '') {
 3589:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 3590:         }
 3591:     }
 3592:     return %langchoices;
 3593: }
 3594: 
 3595: sub defaults_titles {
 3596:     my ($dom) = @_;
 3597:     my %titles = &Apache::lonlocal::texthash (
 3598:                    'auth_def'      => 'Default authentication type',
 3599:                    'auth_arg_def'  => 'Default authentication argument',
 3600:                    'lang_def'      => 'Default language',
 3601:                    'timezone_def'  => 'Default timezone',
 3602:                    'datelocale_def' => 'Default locale for dates',
 3603:                    'portal_def'     => 'Portal/Default URL',
 3604:                  );
 3605:     if ($dom) {
 3606:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3607:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3608:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3609:         $protocol = 'http' if ($protocol ne 'https');
 3610:         if ($uint_dom) {
 3611:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3612:                                          $uint_dom);
 3613:         }
 3614:     }
 3615:     return (\%titles);
 3616: }
 3617: 
 3618: sub print_scantronformat {
 3619:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3620:     my $itemcount = 1;
 3621:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3622:         %confhash);
 3623:     my $switchserver = &check_switchserver($dom,$confname);
 3624:     my %lt = &Apache::lonlocal::texthash (
 3625:                 default => 'Default bubblesheet format file error',
 3626:                 custom  => 'Custom bubblesheet format file error',
 3627:              );
 3628:     my %scantronfiles = (
 3629:         default => 'default.tab',
 3630:         custom => 'custom.tab',
 3631:     );
 3632:     foreach my $key (keys(%scantronfiles)) {
 3633:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3634:                               .$scantronfiles{$key};
 3635:     }
 3636:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3637:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3638:         if (!$switchserver) {
 3639:             my $servadm = $r->dir_config('lonAdmEMail');
 3640:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3641:             if ($configuserok eq 'ok') {
 3642:                 if ($author_ok eq 'ok') {
 3643:                     my %legacyfile = (
 3644:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3645:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3646:                     );
 3647:                     my %md5chk;
 3648:                     foreach my $type (keys(%legacyfile)) {
 3649:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3650:                         chomp($md5chk{$type});
 3651:                     }
 3652:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3653:                         foreach my $type (keys(%legacyfile)) {
 3654:                             ($scantronurls{$type},my $error) = 
 3655:                                 &legacy_scantronformat($r,$dom,$confname,
 3656:                                                  $type,$legacyfile{$type},
 3657:                                                  $scantronurls{$type},
 3658:                                                  $scantronfiles{$type});
 3659:                             if ($error ne '') {
 3660:                                 $error{$type} = $error;
 3661:                             }
 3662:                         }
 3663:                         if (keys(%error) == 0) {
 3664:                             $is_custom = 1;
 3665:                             $confhash{'scantron'}{'scantronformat'} = 
 3666:                                 $scantronurls{'custom'};
 3667:                             my $putresult = 
 3668:                                 &Apache::lonnet::put_dom('configuration',
 3669:                                                          \%confhash,$dom);
 3670:                             if ($putresult ne 'ok') {
 3671:                                 $error{'custom'} = 
 3672:                                     '<span class="LC_error">'.
 3673:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3674:                             }
 3675:                         }
 3676:                     } else {
 3677:                         ($scantronurls{'default'},my $error) =
 3678:                             &legacy_scantronformat($r,$dom,$confname,
 3679:                                           'default',$legacyfile{'default'},
 3680:                                           $scantronurls{'default'},
 3681:                                           $scantronfiles{'default'});
 3682:                         if ($error eq '') {
 3683:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3684:                             my $putresult =
 3685:                                 &Apache::lonnet::put_dom('configuration',
 3686:                                                          \%confhash,$dom);
 3687:                             if ($putresult ne 'ok') {
 3688:                                 $error{'default'} =
 3689:                                     '<span class="LC_error">'.
 3690:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3691:                             }
 3692:                         } else {
 3693:                             $error{'default'} = $error;
 3694:                         }
 3695:                     }
 3696:                 }
 3697:             }
 3698:         } else {
 3699:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3700:         }
 3701:     }
 3702:     if (ref($settings) eq 'HASH') {
 3703:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3704:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3705:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3706:                 $scantronurl = '';
 3707:             } else {
 3708:                 $scantronurl = $settings->{'scantronformat'};
 3709:             }
 3710:             $is_custom = 1;
 3711:         } else {
 3712:             $scantronurl = $scantronurls{'default'};
 3713:         }
 3714:     } else {
 3715:         if ($is_custom) {
 3716:             $scantronurl = $scantronurls{'custom'};
 3717:         } else {
 3718:             $scantronurl = $scantronurls{'default'};
 3719:         }
 3720:     }
 3721:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3722:     $datatable .= '<tr'.$css_class.'>';
 3723:     if (!$is_custom) {
 3724:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3725:                       '<span class="LC_nobreak">';
 3726:         if ($scantronurl) {
 3727:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3728:                           &mt('Default bubblesheet format file').'</a>';
 3729:         } else {
 3730:             $datatable = &mt('File unavailable for display');
 3731:         }
 3732:         $datatable .= '</span></td>';
 3733:         if (keys(%error) == 0) { 
 3734:             $datatable .= '<td valign="bottom">';
 3735:             if (!$switchserver) {
 3736:                 $datatable .= &mt('Upload:').'<br />';
 3737:             }
 3738:         } else {
 3739:             my $errorstr;
 3740:             foreach my $key (sort(keys(%error))) {
 3741:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3742:             }
 3743:             $datatable .= '<td>'.$errorstr;
 3744:         }
 3745:     } else {
 3746:         if (keys(%error) > 0) {
 3747:             my $errorstr;
 3748:             foreach my $key (sort(keys(%error))) {
 3749:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3750:             } 
 3751:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3752:         } elsif ($scantronurl) {
 3753:             $datatable .= '<td><span class="LC_nobreak">'.
 3754:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3755:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3756:                           '<input type="checkbox" name="scantronformat_del"'.
 3757:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3758:                           '<td><span class="LC_nobreak">&nbsp;'.
 3759:                           &mt('Replace:').'</span><br />';
 3760:         }
 3761:     }
 3762:     if (keys(%error) == 0) {
 3763:         if ($switchserver) {
 3764:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3765:         } else {
 3766:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3767:                          '<input type="file" name="scantronformat" /></span>';
 3768:         }
 3769:     }
 3770:     $datatable .= '</td></tr>';
 3771:     $$rowtotal ++;
 3772:     return $datatable;
 3773: }
 3774: 
 3775: sub legacy_scantronformat {
 3776:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3777:     my ($url,$error);
 3778:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3779:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3780:         (my $result,$url) =
 3781:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3782:                          '','',$newfile);
 3783:         if ($result ne 'ok') {
 3784:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3785:         }
 3786:     }
 3787:     return ($url,$error);
 3788: }
 3789: 
 3790: sub print_coursecategories {
 3791:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3792:     my $datatable;
 3793:     if ($position eq 'top') {
 3794:         my $toggle_cats_crs = ' ';
 3795:         my $toggle_cats_dom = ' checked="checked" ';
 3796:         my $can_cat_crs = ' ';
 3797:         my $can_cat_dom = ' checked="checked" ';
 3798:         my $toggle_catscomm_comm = ' ';
 3799:         my $toggle_catscomm_dom = ' checked="checked" ';
 3800:         my $can_catcomm_comm = ' ';
 3801:         my $can_catcomm_dom = ' checked="checked" ';
 3802: 
 3803:         if (ref($settings) eq 'HASH') {
 3804:             if ($settings->{'togglecats'} eq 'crs') {
 3805:                 $toggle_cats_crs = $toggle_cats_dom;
 3806:                 $toggle_cats_dom = ' ';
 3807:             }
 3808:             if ($settings->{'categorize'} eq 'crs') {
 3809:                 $can_cat_crs = $can_cat_dom;
 3810:                 $can_cat_dom = ' ';
 3811:             }
 3812:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3813:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3814:                 $toggle_catscomm_dom = ' ';
 3815:             }
 3816:             if ($settings->{'categorizecomm'} eq 'comm') {
 3817:                 $can_catcomm_comm = $can_catcomm_dom;
 3818:                 $can_catcomm_dom = ' ';
 3819:             }
 3820:         }
 3821:         my %title = &Apache::lonlocal::texthash (
 3822:                      togglecats     => 'Show/Hide a course in catalog',
 3823:                      togglecatscomm => 'Show/Hide a community in catalog',
 3824:                      categorize     => 'Assign a category to a course',
 3825:                      categorizecomm => 'Assign a category to a community',
 3826:                     );
 3827:         my %level = &Apache::lonlocal::texthash (
 3828:                      dom  => 'Set in Domain',
 3829:                      crs  => 'Set in Course',
 3830:                      comm => 'Set in Community',
 3831:                     );
 3832:         $datatable = '<tr class="LC_odd_row">'.
 3833:                   '<td>'.$title{'togglecats'}.'</td>'.
 3834:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3835:                   '<input type="radio" name="togglecats"'.
 3836:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3837:                   '<label><input type="radio" name="togglecats"'.
 3838:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3839:                   '</tr><tr>'.
 3840:                   '<td>'.$title{'categorize'}.'</td>'.
 3841:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3842:                   '<label><input type="radio" name="categorize"'.
 3843:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3844:                   '<label><input type="radio" name="categorize"'.
 3845:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3846:                   '</tr><tr class="LC_odd_row">'.
 3847:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 3848:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3849:                   '<input type="radio" name="togglecatscomm"'.
 3850:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3851:                   '<label><input type="radio" name="togglecatscomm"'.
 3852:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3853:                   '</tr><tr>'.
 3854:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 3855:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3856:                   '<label><input type="radio" name="categorizecomm"'.
 3857:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3858:                   '<label><input type="radio" name="categorizecomm"'.
 3859:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3860:                   '</tr>';
 3861:         $$rowtotal += 4;
 3862:     } else {
 3863:         my $css_class;
 3864:         my $itemcount = 1;
 3865:         my $cathash; 
 3866:         if (ref($settings) eq 'HASH') {
 3867:             $cathash = $settings->{'cats'};
 3868:         }
 3869:         if (ref($cathash) eq 'HASH') {
 3870:             my (@cats,@trails,%allitems,%idx,@jsarray);
 3871:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 3872:                                                    \%allitems,\%idx,\@jsarray);
 3873:             my $maxdepth = scalar(@cats);
 3874:             my $colattrib = '';
 3875:             if ($maxdepth > 2) {
 3876:                 $colattrib = ' colspan="2" ';
 3877:             }
 3878:             my @path;
 3879:             if (@cats > 0) {
 3880:                 if (ref($cats[0]) eq 'ARRAY') {
 3881:                     my $numtop = @{$cats[0]};
 3882:                     my $maxnum = $numtop;
 3883:                     my %default_names = (
 3884:                           instcode    => &mt('Official courses'),
 3885:                           communities => &mt('Communities'),
 3886:                     );
 3887: 
 3888:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 3889:                         ($cathash->{'instcode::0'} eq '') ||
 3890:                         (!grep(/^communities$/,@{$cats[0]})) || 
 3891:                         ($cathash->{'communities::0'} eq '')) {
 3892:                         $maxnum ++;
 3893:                     }
 3894:                     my $lastidx;
 3895:                     for (my $i=0; $i<$numtop; $i++) {
 3896:                         my $parent = $cats[0][$i];
 3897:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3898:                         my $item = &escape($parent).'::0';
 3899:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 3900:                         $lastidx = $idx{$item};
 3901:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3902:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 3903:                         for (my $k=0; $k<=$maxnum; $k++) {
 3904:                             my $vpos = $k+1;
 3905:                             my $selstr;
 3906:                             if ($k == $i) {
 3907:                                 $selstr = ' selected="selected" ';
 3908:                             }
 3909:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3910:                         }
 3911:                         $datatable .= '</select></td><td>';
 3912:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 3913:                             $datatable .=  '<span class="LC_nobreak">'
 3914:                                            .$default_names{$parent}.'</span>';
 3915:                             if ($parent eq 'instcode') {
 3916:                                 $datatable .= '<br /><span class="LC_nobreak">('
 3917:                                               .&mt('with institutional codes')
 3918:                                               .')</span></td><td'.$colattrib.'>';
 3919:                             } else {
 3920:                                 $datatable .= '<table><tr><td>';
 3921:                             }
 3922:                             $datatable .= '<span class="LC_nobreak">'
 3923:                                           .'<label><input type="radio" name="'
 3924:                                           .$parent.'" value="1" checked="checked" />'
 3925:                                           .&mt('Display').'</label>';
 3926:                             if ($parent eq 'instcode') {
 3927:                                 $datatable .= '&nbsp;';
 3928:                             } else {
 3929:                                 $datatable .= '</span></td></tr><tr><td>'
 3930:                                               .'<span class="LC_nobreak">';
 3931:                             }
 3932:                             $datatable .= '<label><input type="radio" name="'
 3933:                                           .$parent.'" value="0" />'
 3934:                                           .&mt('Do not display').'</label></span>';
 3935:                             if ($parent eq 'communities') {
 3936:                                 $datatable .= '</td></tr></table>';
 3937:                             }
 3938:                             $datatable .= '</td>';
 3939:                         } else {
 3940:                             $datatable .= $parent
 3941:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 3942:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 3943:                         }
 3944:                         my $depth = 1;
 3945:                         push(@path,$parent);
 3946:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 3947:                         pop(@path);
 3948:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 3949:                         $itemcount ++;
 3950:                     }
 3951:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3952:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 3953:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 3954:                     for (my $k=0; $k<=$maxnum; $k++) {
 3955:                         my $vpos = $k+1;
 3956:                         my $selstr;
 3957:                         if ($k == $numtop) {
 3958:                             $selstr = ' selected="selected" ';
 3959:                         }
 3960:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3961:                     }
 3962:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 3963:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 3964:                                   .'</tr>'."\n";
 3965:                     $itemcount ++;
 3966:                     foreach my $default ('instcode','communities') {
 3967:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 3968:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3969:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 3970:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 3971:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 3972:                             for (my $k=0; $k<=$maxnum; $k++) {
 3973:                                 my $vpos = $k+1;
 3974:                                 my $selstr;
 3975:                                 if ($k == $maxnum) {
 3976:                                     $selstr = ' selected="selected" ';
 3977:                                 }
 3978:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3979:                             }
 3980:                             $datatable .= '</select></span></td>'.
 3981:                                           '<td><span class="LC_nobreak">'.
 3982:                                           $default_names{$default}.'</span>';
 3983:                             if ($default eq 'instcode') {
 3984:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 3985:                                               .&mt('with institutional codes').')</span>';
 3986:                             }
 3987:                             $datatable .= '</td>'
 3988:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 3989:                                           .&mt('Display').'</label>&nbsp;'
 3990:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 3991:                                           .&mt('Do not display').'</label></span></td></tr>';
 3992:                         }
 3993:                     }
 3994:                 }
 3995:             } else {
 3996:                 $datatable .= &initialize_categories($itemcount);
 3997:             }
 3998:         } else {
 3999:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4000:                           .&initialize_categories($itemcount);
 4001:         }
 4002:         $$rowtotal += $itemcount;
 4003:     }
 4004:     return $datatable;
 4005: }
 4006: 
 4007: sub print_serverstatuses {
 4008:     my ($dom,$settings,$rowtotal) = @_;
 4009:     my $datatable;
 4010:     my @pages = &serverstatus_pages();
 4011:     my (%namedaccess,%machineaccess);
 4012:     foreach my $type (@pages) {
 4013:         $namedaccess{$type} = '';
 4014:         $machineaccess{$type}= '';
 4015:     }
 4016:     if (ref($settings) eq 'HASH') {
 4017:         foreach my $type (@pages) {
 4018:             if (exists($settings->{$type})) {
 4019:                 if (ref($settings->{$type}) eq 'HASH') {
 4020:                     foreach my $key (keys(%{$settings->{$type}})) {
 4021:                         if ($key eq 'namedusers') {
 4022:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4023:                         } elsif ($key eq 'machines') {
 4024:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4025:                         }
 4026:                     }
 4027:                 }
 4028:             }
 4029:         }
 4030:     }
 4031:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4032:     my $rownum = 0;
 4033:     my $css_class;
 4034:     foreach my $type (@pages) {
 4035:         $rownum ++;
 4036:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4037:         $datatable .= '<tr'.$css_class.'>'.
 4038:                       '<td><span class="LC_nobreak">'.
 4039:                       $titles->{$type}.'</span></td>'.
 4040:                       '<td class="LC_left_item">'.
 4041:                       '<input type="text" name="'.$type.'_namedusers" '.
 4042:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4043:                       '<td class="LC_right_item">'.
 4044:                       '<span class="LC_nobreak">'.
 4045:                       '<input type="text" name="'.$type.'_machines" '.
 4046:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4047:                       '</td></tr>'."\n";
 4048:     }
 4049:     $$rowtotal += $rownum;
 4050:     return $datatable;
 4051: }
 4052: 
 4053: sub serverstatus_pages {
 4054:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4055:             'clusterstatus','metadata_keywords','metadata_harvest',
 4056:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4057: }
 4058: 
 4059: sub coursecategories_javascript {
 4060:     my ($settings) = @_;
 4061:     my ($output,$jstext,$cathash);
 4062:     if (ref($settings) eq 'HASH') {
 4063:         $cathash = $settings->{'cats'};
 4064:     }
 4065:     if (ref($cathash) eq 'HASH') {
 4066:         my (@cats,@jsarray,%idx);
 4067:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4068:         if (@jsarray > 0) {
 4069:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4070:             for (my $i=0; $i<@jsarray; $i++) {
 4071:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4072:                     my $catstr = join('","',@{$jsarray[$i]});
 4073:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4074:                 }
 4075:             }
 4076:         }
 4077:     } else {
 4078:         $jstext  = '    var categories = Array(1);'."\n".
 4079:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4080:     }
 4081:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4082:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4083:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4084:     $output = <<"ENDSCRIPT";
 4085: <script type="text/javascript">
 4086: // <![CDATA[
 4087: function reorderCats(form,parent,item,idx) {
 4088:     var changedVal;
 4089: $jstext
 4090:     var newpos = 'addcategory_pos';
 4091:     var current = new Array;
 4092:     if (parent == '') {
 4093:         var has_instcode = 0;
 4094:         var maxtop = categories[idx].length;
 4095:         for (var j=0; j<maxtop; j++) {
 4096:             if (categories[idx][j] == 'instcode::0') {
 4097:                 has_instcode == 1;
 4098:             }
 4099:         }
 4100:         if (has_instcode == 0) {
 4101:             categories[idx][maxtop] = 'instcode_pos';
 4102:         }
 4103:     } else {
 4104:         newpos += '_'+parent;
 4105:     }
 4106:     var maxh = 1 + categories[idx].length;
 4107:     var current = new Array;
 4108:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4109:     if (item == newpos) {
 4110:         changedVal = newitemVal;
 4111:     } else {
 4112:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4113:         current[newitemVal] = newpos;
 4114:     }
 4115:     for (var i=0; i<categories[idx].length; i++) {
 4116:         var elementName = categories[idx][i];
 4117:         if (elementName != item) {
 4118:             if (form.elements[elementName]) {
 4119:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4120:                 current[currVal] = elementName;
 4121:             }
 4122:         }
 4123:     }
 4124:     var oldVal;
 4125:     for (var j=0; j<maxh; j++) {
 4126:         if (current[j] == undefined) {
 4127:             oldVal = j;
 4128:         }
 4129:     }
 4130:     if (oldVal < changedVal) {
 4131:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4132:            var elementName = current[k];
 4133:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4134:         }
 4135:     } else {
 4136:         for (var k=changedVal; k<oldVal; k++) {
 4137:             var elementName = current[k];
 4138:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4139:         }
 4140:     }
 4141:     return;
 4142: }
 4143: 
 4144: function categoryCheck(form) {
 4145:     if (form.elements['addcategory_name'].value == 'instcode') {
 4146:         alert('$instcode_reserved\\n$choose_again');
 4147:         return false;
 4148:     }
 4149:     if (form.elements['addcategory_name'].value == 'communities') {
 4150:         alert('$communities_reserved\\n$choose_again');
 4151:         return false;
 4152:     }
 4153:     return true;
 4154: }
 4155: 
 4156: // ]]>
 4157: </script>
 4158: 
 4159: ENDSCRIPT
 4160:     return $output;
 4161: }
 4162: 
 4163: sub initialize_categories {
 4164:     my ($itemcount) = @_;
 4165:     my ($datatable,$css_class,$chgstr);
 4166:     my %default_names = (
 4167:                       instcode    => 'Official courses (with institutional codes)',
 4168:                       communities => 'Communities',
 4169:                         );
 4170:     my $select0 = ' selected="selected"';
 4171:     my $select1 = '';
 4172:     foreach my $default ('instcode','communities') {
 4173:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4174:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4175:         if ($default eq 'communities') {
 4176:             $select1 = $select0;
 4177:             $select0 = '';
 4178:         }
 4179:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4180:                      .'<select name="'.$default.'_pos">'
 4181:                      .'<option value="0"'.$select0.'>1</option>'
 4182:                      .'<option value="1"'.$select1.'>2</option>'
 4183:                      .'<option value="2">3</option></select>&nbsp;'
 4184:                      .$default_names{$default}
 4185:                      .'</span></td><td><span class="LC_nobreak">'
 4186:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4187:                      .&mt('Display').'</label>&nbsp;<label>'
 4188:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4189:                  .'</label></span></td></tr>';
 4190:         $itemcount ++;
 4191:     }
 4192:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4193:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4194:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4195:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4196:                   .'<option value="0">1</option>'
 4197:                   .'<option value="1">2</option>'
 4198:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4199:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4200:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4201:     return $datatable;
 4202: }
 4203: 
 4204: sub build_category_rows {
 4205:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4206:     my ($text,$name,$item,$chgstr);
 4207:     if (ref($cats) eq 'ARRAY') {
 4208:         my $maxdepth = scalar(@{$cats});
 4209:         if (ref($cats->[$depth]) eq 'HASH') {
 4210:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4211:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4212:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4213:                 $text .= '<td><table class="LC_datatable">';
 4214:                 my ($idxnum,$parent_name,$parent_item);
 4215:                 my $higher = $depth - 1;
 4216:                 if ($higher == 0) {
 4217:                     $parent_name = &escape($parent).'::'.$higher;
 4218:                 } else {
 4219:                     if (ref($path) eq 'ARRAY') {
 4220:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4221:                     }
 4222:                 }
 4223:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4224:                 for (my $j=0; $j<=$numchildren; $j++) {
 4225:                     if ($j < $numchildren) {
 4226:                         $name = $cats->[$depth]{$parent}[$j];
 4227:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4228:                         $idxnum = $idx->{$item};
 4229:                     } else {
 4230:                         $name = $parent_name;
 4231:                         $item = $parent_item;
 4232:                     }
 4233:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4234:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4235:                     for (my $i=0; $i<=$numchildren; $i++) {
 4236:                         my $vpos = $i+1;
 4237:                         my $selstr;
 4238:                         if ($j == $i) {
 4239:                             $selstr = ' selected="selected" ';
 4240:                         }
 4241:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4242:                     }
 4243:                     $text .= '</select>&nbsp;';
 4244:                     if ($j < $numchildren) {
 4245:                         my $deeper = $depth+1;
 4246:                         $text .= $name.'&nbsp;'
 4247:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4248:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4249:                         if(ref($path) eq 'ARRAY') {
 4250:                             push(@{$path},$name);
 4251:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4252:                             pop(@{$path});
 4253:                         }
 4254:                     } else {
 4255:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4256:                         if ($j == $numchildren) {
 4257:                             $text .= $name;
 4258:                         } else {
 4259:                             $text .= $item;
 4260:                         }
 4261:                         $text .= '" value="" />';
 4262:                     }
 4263:                     $text .= '</td></tr>';
 4264:                 }
 4265:                 $text .= '</table></td>';
 4266:             } else {
 4267:                 my $higher = $depth-1;
 4268:                 if ($higher == 0) {
 4269:                     $name = &escape($parent).'::'.$higher;
 4270:                 } else {
 4271:                     if (ref($path) eq 'ARRAY') {
 4272:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4273:                     }
 4274:                 }
 4275:                 my $colspan;
 4276:                 if ($parent ne 'instcode') {
 4277:                     $colspan = $maxdepth - $depth - 1;
 4278:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4279:                 }
 4280:             }
 4281:         }
 4282:     }
 4283:     return $text;
 4284: }
 4285: 
 4286: sub modifiable_userdata_row {
 4287:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4288:     my $rolename;
 4289:     if ($context eq 'selfcreate') {
 4290:         if (ref($usertypes) eq 'HASH') {
 4291:             $rolename = $usertypes->{$role};
 4292:         } else {
 4293:             $rolename = $role;
 4294:         }
 4295:     } else {
 4296:         if ($role eq 'cr') {
 4297:             $rolename = &mt('Custom role');
 4298:         } else {
 4299:             $rolename = &Apache::lonnet::plaintext($role);
 4300:         }
 4301:     }
 4302:     my @fields = ('lastname','firstname','middlename','generation',
 4303:                   'permanentemail','id');
 4304:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4305:     my $output;
 4306:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4307:     $output = '<tr '.$css_class.'>'.
 4308:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4309:               '<td class="LC_left_item" colspan="2"><table>';
 4310:     my $rem;
 4311:     my %checks;
 4312:     if (ref($settings) eq 'HASH') {
 4313:         if (ref($settings->{$context}) eq 'HASH') {
 4314:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4315:                 foreach my $field (@fields) {
 4316:                     if ($settings->{$context}->{$role}->{$field}) {
 4317:                         $checks{$field} = ' checked="checked" ';
 4318:                     }
 4319:                 }
 4320:             }
 4321:         }
 4322:     }
 4323:     for (my $i=0; $i<@fields; $i++) {
 4324:         my $rem = $i%($numinrow);
 4325:         if ($rem == 0) {
 4326:             if ($i > 0) {
 4327:                 $output .= '</tr>';
 4328:             }
 4329:             $output .= '<tr>';
 4330:         }
 4331:         my $check = ' ';
 4332:         if (exists($checks{$fields[$i]})) {
 4333:             $check = $checks{$fields[$i]}
 4334:         } else {
 4335:             if ($role eq 'st') {
 4336:                 if (ref($settings) ne 'HASH') {
 4337:                     $check = ' checked="checked" '; 
 4338:                 }
 4339:             }
 4340:         }
 4341:         $output .= '<td class="LC_left_item">'.
 4342:                    '<span class="LC_nobreak"><label>'.
 4343:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4344:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4345:                    '</label></span></td>';
 4346:         $rem = @fields%($numinrow);
 4347:     }
 4348:     my $colsleft = $numinrow - $rem;
 4349:     if ($colsleft > 1 ) {
 4350:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4351:                    '&nbsp;</td>';
 4352:     } elsif ($colsleft == 1) {
 4353:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4354:     }
 4355:     $output .= '</tr></table></td></tr>';
 4356:     return $output;
 4357: }
 4358: 
 4359: sub insttypes_row {
 4360:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4361:     my %lt = &Apache::lonlocal::texthash (
 4362:                       cansearch => 'Users allowed to search',
 4363:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4364:                       lockablenames => 'User preference to lock name',
 4365:              );
 4366:     my $showdom;
 4367:     if ($context eq 'cansearch') {
 4368:         $showdom = ' ('.$dom.')';
 4369:     }
 4370:     my $class = 'LC_left_item';
 4371:     if ($context eq 'statustocreate') {
 4372:         $class = 'LC_right_item';
 4373:     }
 4374:     my $output =  '<tr class="LC_odd_row">'.
 4375:                   '<td>'.$lt{$context}.$showdom.
 4376:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4377:     my $rem;
 4378:     if (ref($types) eq 'ARRAY') {
 4379:         for (my $i=0; $i<@{$types}; $i++) {
 4380:             if (defined($usertypes->{$types->[$i]})) {
 4381:                 my $rem = $i%($numinrow);
 4382:                 if ($rem == 0) {
 4383:                     if ($i > 0) {
 4384:                         $output .= '</tr>';
 4385:                     }
 4386:                     $output .= '<tr>';
 4387:                 }
 4388:                 my $check = ' ';
 4389:                 if (ref($settings) eq 'HASH') {
 4390:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4391:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4392:                             $check = ' checked="checked" ';
 4393:                         }
 4394:                     } elsif ($context eq 'statustocreate') {
 4395:                         $check = ' checked="checked" ';
 4396:                     }
 4397:                 }
 4398:                 $output .= '<td class="LC_left_item">'.
 4399:                            '<span class="LC_nobreak"><label>'.
 4400:                            '<input type="checkbox" name="'.$context.'" '.
 4401:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4402:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4403:             }
 4404:         }
 4405:         $rem = @{$types}%($numinrow);
 4406:     }
 4407:     my $colsleft = $numinrow - $rem;
 4408:     if (($rem == 0) && (@{$types} > 0)) {
 4409:         $output .= '<tr>';
 4410:     }
 4411:     if ($colsleft > 1) {
 4412:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4413:     } else {
 4414:         $output .= '<td class="LC_left_item">';
 4415:     }
 4416:     my $defcheck = ' ';
 4417:     if (ref($settings) eq 'HASH') {  
 4418:         if (ref($settings->{$context}) eq 'ARRAY') {
 4419:             if (grep(/^default$/,@{$settings->{$context}})) {
 4420:                 $defcheck = ' checked="checked" ';
 4421:             }
 4422:         } elsif ($context eq 'statustocreate') {
 4423:             $defcheck = ' checked="checked" ';
 4424:         }
 4425:     }
 4426:     $output .= '<span class="LC_nobreak"><label>'.
 4427:                '<input type="checkbox" name="'.$context.'" '.
 4428:                'value="default"'.$defcheck.'/>'.
 4429:                $othertitle.'</label></span></td>'.
 4430:                '</tr></table></td></tr>';
 4431:     return $output;
 4432: }
 4433: 
 4434: sub sorted_searchtitles {
 4435:     my %searchtitles = &Apache::lonlocal::texthash(
 4436:                          'uname' => 'username',
 4437:                          'lastname' => 'last name',
 4438:                          'lastfirst' => 'last name, first name',
 4439:                      );
 4440:     my @titleorder = ('uname','lastname','lastfirst');
 4441:     return (\%searchtitles,\@titleorder);
 4442: }
 4443: 
 4444: sub sorted_searchtypes {
 4445:     my %srchtypes_desc = (
 4446:                            exact    => 'is exact match',
 4447:                            contains => 'contains ..',
 4448:                            begins   => 'begins with ..',
 4449:                          );
 4450:     my @srchtypeorder = ('exact','begins','contains');
 4451:     return (\%srchtypes_desc,\@srchtypeorder);
 4452: }
 4453: 
 4454: sub usertype_update_row {
 4455:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4456:     my $datatable;
 4457:     my $numinrow = 4;
 4458:     foreach my $type (@{$types}) {
 4459:         if (defined($usertypes->{$type})) {
 4460:             $$rownums ++;
 4461:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4462:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4463:                           '</td><td class="LC_left_item"><table>';
 4464:             for (my $i=0; $i<@{$fields}; $i++) {
 4465:                 my $rem = $i%($numinrow);
 4466:                 if ($rem == 0) {
 4467:                     if ($i > 0) {
 4468:                         $datatable .= '</tr>';
 4469:                     }
 4470:                     $datatable .= '<tr>';
 4471:                 }
 4472:                 my $check = ' ';
 4473:                 if (ref($settings) eq 'HASH') {
 4474:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4475:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4476:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4477:                                 $check = ' checked="checked" ';
 4478:                             }
 4479:                         }
 4480:                     }
 4481:                 }
 4482: 
 4483:                 if ($i == @{$fields}-1) {
 4484:                     my $colsleft = $numinrow - $rem;
 4485:                     if ($colsleft > 1) {
 4486:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4487:                     } else {
 4488:                         $datatable .= '<td>';
 4489:                     }
 4490:                 } else {
 4491:                     $datatable .= '<td>';
 4492:                 }
 4493:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4494:                               '<input type="checkbox" name="updateable_'.$type.
 4495:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4496:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4497:             }
 4498:             $datatable .= '</tr></table></td></tr>';
 4499:         }
 4500:     }
 4501:     return $datatable;
 4502: }
 4503: 
 4504: sub modify_login {
 4505:     my ($r,$dom,$confname,%domconfig) = @_;
 4506:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4507:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4508:     %title = ( coursecatalog => 'Display course catalog',
 4509:                adminmail => 'Display administrator E-mail address',
 4510:                newuser => 'Link for visitors to create a user account',
 4511:                loginheader => 'Log-in box header');
 4512:     @offon = ('off','on');
 4513:     if (ref($domconfig{login}) eq 'HASH') {
 4514:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4515:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4516:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4517:             }
 4518:         }
 4519:     }
 4520:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4521:                                            \%domconfig,\%loginhash);
 4522:     my @toggles = ('coursecatalog','adminmail','newuser');
 4523:     foreach my $item (@toggles) {
 4524:         $loginhash{login}{$item} = $env{'form.'.$item};
 4525:     }
 4526:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4527:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4528:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4529:                                          \%loginhash);
 4530:     }
 4531: 
 4532:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4533:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4534:     if (keys(%servers) > 1) {
 4535:         foreach my $lonhost (keys(%servers)) {
 4536:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4537:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4538:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4539:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4540:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4541:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4542:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4543:                         $changes{'loginvia'}{$lonhost} = 1;
 4544:                     } else {
 4545:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4546:                         $changes{'loginvia'}{$lonhost} = 1;
 4547:                     }
 4548:                 } else {
 4549:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4550:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4551:                         $changes{'loginvia'}{$lonhost} = 1;
 4552:                     }
 4553:                 }
 4554:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4555:                     foreach my $item (@loginvia_attribs) {
 4556:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4557:                     }
 4558:                 } else {
 4559:                     foreach my $item (@loginvia_attribs) {
 4560:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4561:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4562:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4563:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4564:                                 $new = '/';
 4565:                             }
 4566:                         }
 4567:                         if (($item eq 'custompath') && 
 4568:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4569:                             $new = '';
 4570:                         }
 4571:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4572:                             $changes{'loginvia'}{$lonhost} = 1;
 4573:                         }
 4574:                         if ($item eq 'exempt') {
 4575:                             $new =~ s/^\s+//;
 4576:                             $new =~ s/\s+$//;
 4577:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4578:                             my @okips;
 4579:                             foreach my $ip (@poss_ips) {
 4580:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4581:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4582:                                         push(@okips,$ip); 
 4583:                                     }
 4584:                                 }
 4585:                             }
 4586:                             if (@okips > 0) {
 4587:                                 $new = join(',',@okips); 
 4588:                             } else {
 4589:                                 $new = ''; 
 4590:                             }
 4591:                         }
 4592:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4593:                     }
 4594:                 }
 4595:             } else {
 4596:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4597:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4598:                     $changes{'loginvia'}{$lonhost} = 1;
 4599:                     foreach my $item (@loginvia_attribs) {
 4600:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4601:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4602:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4603:                                 $new = '/';
 4604:                             }
 4605:                         }
 4606:                         if (($item eq 'custompath') && 
 4607:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4608:                             $new = '';
 4609:                         }
 4610:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4611:                     }
 4612:                 }
 4613:             }
 4614:         }
 4615:     }
 4616: 
 4617:     my $servadm = $r->dir_config('lonAdmEMail');
 4618:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 4619:     if (ref($domconfig{'login'}) eq 'HASH') {
 4620:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 4621:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 4622:                 if ($lang eq 'nolang') {
 4623:                     push(@currlangs,$lang);
 4624:                 } elsif (defined($langchoices{$lang})) {
 4625:                     push(@currlangs,$lang);
 4626:                 } else {
 4627:                     next;
 4628:                 }
 4629:             }
 4630:         }
 4631:     }
 4632:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 4633:     if (@currlangs > 0) {
 4634:         foreach my $lang (@currlangs) {
 4635:             if (grep(/^\Q$lang\E$/,@delurls)) {
 4636:                 $changes{'helpurl'}{$lang} = 1;
 4637:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 4638:                 $changes{'helpurl'}{$lang} = 1;
 4639:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 4640:                 push(@newlangs,$lang);
 4641:             } else {
 4642:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4643:             }
 4644:         }
 4645:     }
 4646:     unless (grep(/^nolang$/,@currlangs)) {
 4647:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 4648:             $changes{'helpurl'}{'nolang'} = 1;
 4649:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 4650:             push(@newlangs,'nolang');
 4651:         }
 4652:     }
 4653:     if ($env{'form.loginhelpurl_add_lang'}) {
 4654:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 4655:             ($env{'form.loginhelpurl_add_file.filename'})) {
 4656:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 4657:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 4658:         }
 4659:     }
 4660:     if ((@newlangs > 0) || ($addedfile)) {
 4661:         my $error;
 4662:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 4663:         if ($configuserok eq 'ok') {
 4664:             if ($switchserver) {
 4665:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 4666:             } elsif ($author_ok eq 'ok') {
 4667:                 my @allnew = @newlangs;
 4668:                 if ($addedfile ne '') {
 4669:                     push(@allnew,$addedfile);
 4670:                 }
 4671:                 foreach my $lang (@allnew) {
 4672:                     my $formelem = 'loginhelpurl_'.$lang;
 4673:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 4674:                         $formelem = 'loginhelpurl_add_file';
 4675:                     }
 4676:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 4677:                                                                "help/$lang",'','',$newfile{$lang});
 4678:                     if ($result eq 'ok') {
 4679:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 4680:                         $changes{'helpurl'}{$lang} = 1;
 4681:                     } else {
 4682:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 4683:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 4684:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 4685:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 4686: 
 4687:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4688:                         }
 4689:                     }
 4690:                 }
 4691:             } else {
 4692:                 $error = &mt("Upload of custom log-in help file(s) failed because an author role could not be assigned to a Domain Configuration user ([_1]) in domain: [_2].  Error was: [_3].",$confname,$dom,$author_ok);
 4693:             }
 4694:         } else {
 4695:             $error = &mt("Upload of custom log-in help file(s) failed because a Domain Configuration user ([_1]) could not be created in domain: [_2].  Error was: [_3].",$confname,$dom,$configuserok);
 4696:         }
 4697:         if ($error) {
 4698:             &Apache::lonnet::logthis($error);
 4699:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4700:         }
 4701:     }
 4702:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 4703: 
 4704:     my $defaulthelpfile = '/adm/loginproblems.html';
 4705:     my $defaulttext = &mt('Default in use');
 4706: 
 4707:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4708:                                              $dom);
 4709:     if ($putresult eq 'ok') {
 4710:         my @toggles = ('coursecatalog','adminmail','newuser');
 4711:         my %defaultchecked = (
 4712:                     'coursecatalog' => 'on',
 4713:                     'adminmail'     => 'off',
 4714:                     'newuser'       => 'off',
 4715:         );
 4716:         if (ref($domconfig{'login'}) eq 'HASH') {
 4717:             foreach my $item (@toggles) {
 4718:                 if ($defaultchecked{$item} eq 'on') { 
 4719:                     if (($domconfig{'login'}{$item} eq '0') &&
 4720:                         ($env{'form.'.$item} eq '1')) {
 4721:                         $changes{$item} = 1;
 4722:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4723:                               $domconfig{'login'}{$item} eq '1') &&
 4724:                              ($env{'form.'.$item} eq '0')) {
 4725:                         $changes{$item} = 1;
 4726:                     }
 4727:                 } elsif ($defaultchecked{$item} eq 'off') {
 4728:                     if (($domconfig{'login'}{$item} eq '1') &&
 4729:                         ($env{'form.'.$item} eq '0')) {
 4730:                         $changes{$item} = 1;
 4731:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4732:                               $domconfig{'login'}{$item} eq '0') &&
 4733:                              ($env{'form.'.$item} eq '1')) {
 4734:                         $changes{$item} = 1;
 4735:                     }
 4736:                 }
 4737:             }
 4738:         }
 4739:         if (keys(%changes) > 0 || $colchgtext) {
 4740:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4741:             $resulttext = &mt('Changes made:').'<ul>';
 4742:             foreach my $item (sort(keys(%changes))) {
 4743:                 if ($item eq 'loginvia') {
 4744:                     if (ref($changes{$item}) eq 'HASH') {
 4745:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4746:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4747:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4748:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4749:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4750:                                     $protocol = 'http' if ($protocol ne 'https');
 4751:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4752: 
 4753:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4754:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4755:                                     } else {
 4756:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4757:                                     }
 4758:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4759:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4760:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4761:                                     }
 4762:                                     $resulttext .= '</li>';
 4763:                                 } else {
 4764:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4765:                                 }
 4766:                             } else {
 4767:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4768:                             }
 4769:                         }
 4770:                         $resulttext .= '</ul></li>';
 4771:                     }
 4772:                 } elsif ($item eq 'helpurl') {
 4773:                     if (ref($changes{$item}) eq 'HASH') {
 4774:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 4775:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 4776:                                 my ($chg,$link);
 4777:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 4778:                                 if ($lang eq 'nolang') {
 4779:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 4780:                                 } else {
 4781:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 4782:                                 }
 4783:                                 $resulttext .= '<li>'.$chg.'</li>';
 4784:                             } else {
 4785:                                 my $chg;
 4786:                                 if ($lang eq 'nolang') {
 4787:                                     $chg = &mt('custom log-in help file for no preferred language');
 4788:                                 } else {
 4789:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 4790:                                 }
 4791:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 4792:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 4793:                                                       '?inhibitmenu=yes',$chg,600,500).
 4794:                                                '</li>';
 4795:                             }
 4796:                         }
 4797:                     }
 4798:                 } elsif ($item eq 'captcha') {
 4799:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4800:                         my $chgtxt;
 4801:                         if ($loginhash{'login'}{$item} eq 'notused') {
 4802:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 4803:                         } else {
 4804:                             my %captchas = &captcha_phrases();
 4805:                             if ($captchas{$loginhash{'login'}{$item}}) {
 4806:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 4807:                             } else {
 4808:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 4809:                             }
 4810:                         }
 4811:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4812:                     }
 4813:                 } elsif ($item eq 'recaptchakeys') {
 4814:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4815:                         my ($privkey,$pubkey);
 4816:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 4817:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 4818:                             $privkey = $loginhash{'login'}{$item}{'private'};
 4819:                         }
 4820:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 4821:                         if (!$pubkey) {
 4822:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 4823:                         } else {
 4824:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 4825:                         }
 4826:                         if (!$privkey) {
 4827:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 4828:                         } else {
 4829:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 4830:                         }
 4831:                         $chgtxt .= '</ul>';
 4832:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4833:                     }
 4834:                 } else {
 4835:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4836:                 }
 4837:             }
 4838:             $resulttext .= $colchgtext.'</ul>';
 4839:         } else {
 4840:             $resulttext = &mt('No changes made to log-in page settings');
 4841:         }
 4842:     } else {
 4843:         $resulttext = '<span class="LC_error">'.
 4844: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4845:     }
 4846:     if ($errors) {
 4847:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 4848:                        $errors.'</ul>';
 4849:     }
 4850:     return $resulttext;
 4851: }
 4852: 
 4853: sub color_font_choices {
 4854:     my %choices =
 4855:         &Apache::lonlocal::texthash (
 4856:             img => "Header",
 4857:             bgs => "Background colors",
 4858:             links => "Link colors",
 4859:             images => "Images",
 4860:             font => "Font color",
 4861:             fontmenu => "Font Menu",
 4862:             pgbg => "Page",
 4863:             tabbg => "Header",
 4864:             sidebg => "Border",
 4865:             link => "Link",
 4866:             alink => "Active link",
 4867:             vlink => "Visited link",
 4868:         );
 4869:     return %choices;
 4870: }
 4871: 
 4872: sub modify_rolecolors {
 4873:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 4874:     my ($resulttext,%rolehash);
 4875:     $rolehash{'rolecolors'} = {};
 4876:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 4877:         if ($domconfig{'rolecolors'} eq '') {
 4878:             $domconfig{'rolecolors'} = {};
 4879:         }
 4880:     }
 4881:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 4882:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 4883:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 4884:                                              $dom);
 4885:     if ($putresult eq 'ok') {
 4886:         if (keys(%changes) > 0) {
 4887:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4888:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 4889:                                              $rolehash{'rolecolors'});
 4890:         } else {
 4891:             $resulttext = &mt('No changes made to default color schemes');
 4892:         }
 4893:     } else {
 4894:         $resulttext = '<span class="LC_error">'.
 4895: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4896:     }
 4897:     if ($errors) {
 4898:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 4899:                        $errors.'</ul>';
 4900:     }
 4901:     return $resulttext;
 4902: }
 4903: 
 4904: sub modify_colors {
 4905:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 4906:     my (%changes,%choices);
 4907:     my @bgs;
 4908:     my @links = ('link','alink','vlink');
 4909:     my @logintext;
 4910:     my @images;
 4911:     my $servadm = $r->dir_config('lonAdmEMail');
 4912:     my $errors;
 4913:     foreach my $role (@{$roles}) {
 4914:         if ($role eq 'login') {
 4915:             %choices = &login_choices();
 4916:             @logintext = ('textcol','bgcol');
 4917:         } else {
 4918:             %choices = &color_font_choices();
 4919:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 4920:         }
 4921:         if ($role eq 'login') {
 4922:             @images = ('img','logo','domlogo','login');
 4923:             @bgs = ('pgbg','mainbg','sidebg');
 4924:         } else {
 4925:             @images = ('img');
 4926:             @bgs = ('pgbg','tabbg','sidebg'); 
 4927:         }
 4928:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 4929:         foreach my $item (@bgs,@links,@logintext) {
 4930:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 4931:         }
 4932:         my ($configuserok,$author_ok,$switchserver) = 
 4933:             &config_check($dom,$confname,$servadm);
 4934:         my ($width,$height) = &thumb_dimensions();
 4935:         if (ref($domconfig->{$role}) ne 'HASH') {
 4936:             $domconfig->{$role} = {};
 4937:         }
 4938:         foreach my $img (@images) {
 4939:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 4940:                 if (defined($env{'form.login_showlogo_'.$img})) {
 4941:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 4942:                 } else { 
 4943:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 4944:                 }
 4945:             } 
 4946: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 4947: 		 && !defined($domconfig->{$role}{$img})
 4948: 		 && !$env{'form.'.$role.'_del_'.$img}
 4949: 		 && $env{'form.'.$role.'_import_'.$img}) {
 4950: 		# import the old configured image from the .tab setting
 4951: 		# if they haven't provided a new one 
 4952: 		$domconfig->{$role}{$img} = 
 4953: 		    $env{'form.'.$role.'_import_'.$img};
 4954: 	    }
 4955:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 4956:                 my $error;
 4957:                 if ($configuserok eq 'ok') {
 4958:                     if ($switchserver) {
 4959:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 4960:                     } else {
 4961:                         if ($author_ok eq 'ok') {
 4962:                             my ($result,$logourl) = 
 4963:                                 &publishlogo($r,'upload',$role.'_'.$img,
 4964:                                            $dom,$confname,$img,$width,$height);
 4965:                             if ($result eq 'ok') {
 4966:                                 $confhash->{$role}{$img} = $logourl;
 4967:                                 $changes{$role}{'images'}{$img} = 1;
 4968:                             } else {
 4969:                                 $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);
 4970:                             }
 4971:                         } else {
 4972:                             $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);
 4973:                         }
 4974:                     }
 4975:                 } else {
 4976:                     $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);
 4977:                 }
 4978:                 if ($error) {
 4979:                     &Apache::lonnet::logthis($error);
 4980:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4981:                 }
 4982:             } elsif ($domconfig->{$role}{$img} ne '') {
 4983:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 4984:                     my $error;
 4985:                     if ($configuserok eq 'ok') {
 4986: # is confname an author?
 4987:                         if ($switchserver eq '') {
 4988:                             if ($author_ok eq 'ok') {
 4989:                                 my ($result,$logourl) = 
 4990:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 4991:                                             $dom,$confname,$img,$width,$height);
 4992:                                 if ($result eq 'ok') {
 4993:                                     $confhash->{$role}{$img} = $logourl;
 4994: 				    $changes{$role}{'images'}{$img} = 1;
 4995:                                 }
 4996:                             }
 4997:                         }
 4998:                     }
 4999:                 }
 5000:             }
 5001:         }
 5002:         if (ref($domconfig) eq 'HASH') {
 5003:             if (ref($domconfig->{$role}) eq 'HASH') {
 5004:                 foreach my $img (@images) {
 5005:                     if ($domconfig->{$role}{$img} ne '') {
 5006:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5007:                             $confhash->{$role}{$img} = '';
 5008:                             $changes{$role}{'images'}{$img} = 1;
 5009:                         } else {
 5010:                             if ($confhash->{$role}{$img} eq '') {
 5011:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5012:                             }
 5013:                         }
 5014:                     } else {
 5015:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5016:                             $confhash->{$role}{$img} = '';
 5017:                             $changes{$role}{'images'}{$img} = 1;
 5018:                         } 
 5019:                     }
 5020:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5021:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5022:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5023:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5024:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5025:                             }
 5026:                         } else {
 5027:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5028:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5029:                             }
 5030:                         }
 5031:                     }
 5032:                 }
 5033:                 if ($domconfig->{$role}{'font'} ne '') {
 5034:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5035:                         $changes{$role}{'font'} = 1;
 5036:                     }
 5037:                 } else {
 5038:                     if ($confhash->{$role}{'font'}) {
 5039:                         $changes{$role}{'font'} = 1;
 5040:                     }
 5041:                 }
 5042:                 if ($role ne 'login') {
 5043:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5044:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5045:                             $changes{$role}{'fontmenu'} = 1;
 5046:                         }
 5047:                     } else {
 5048:                         if ($confhash->{$role}{'fontmenu'}) {
 5049:                             $changes{$role}{'fontmenu'} = 1;
 5050:                         }
 5051:                     }
 5052:                 }
 5053:                 foreach my $item (@bgs) {
 5054:                     if ($domconfig->{$role}{$item} ne '') {
 5055:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5056:                             $changes{$role}{'bgs'}{$item} = 1;
 5057:                         } 
 5058:                     } else {
 5059:                         if ($confhash->{$role}{$item}) {
 5060:                             $changes{$role}{'bgs'}{$item} = 1;
 5061:                         }
 5062:                     }
 5063:                 }
 5064:                 foreach my $item (@links) {
 5065:                     if ($domconfig->{$role}{$item} ne '') {
 5066:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5067:                             $changes{$role}{'links'}{$item} = 1;
 5068:                         }
 5069:                     } else {
 5070:                         if ($confhash->{$role}{$item}) {
 5071:                             $changes{$role}{'links'}{$item} = 1;
 5072:                         }
 5073:                     }
 5074:                 }
 5075:                 foreach my $item (@logintext) {
 5076:                     if ($domconfig->{$role}{$item} ne '') {
 5077:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5078:                             $changes{$role}{'logintext'}{$item} = 1;
 5079:                         }
 5080:                     } else {
 5081:                         if ($confhash->{$role}{$item}) {
 5082:                             $changes{$role}{'logintext'}{$item} = 1;
 5083:                         }
 5084:                     }
 5085:                 }
 5086:             } else {
 5087:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5088:                                         \@logintext,$confhash,\%changes); 
 5089:             }
 5090:         } else {
 5091:             &default_change_checker($role,\@images,\@links,\@bgs,
 5092:                                     \@logintext,$confhash,\%changes); 
 5093:         }
 5094:     }
 5095:     return ($errors,%changes);
 5096: }
 5097: 
 5098: sub config_check {
 5099:     my ($dom,$confname,$servadm) = @_;
 5100:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5101:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5102:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5103:                                                    $confname,$servadm);
 5104:     if ($configuserok eq 'ok') {
 5105:         $switchserver = &check_switchserver($dom,$confname);
 5106:         if ($switchserver eq '') {
 5107:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5108:         }
 5109:     }
 5110:     return ($configuserok,$author_ok,$switchserver);
 5111: }
 5112: 
 5113: sub default_change_checker {
 5114:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5115:     foreach my $item (@{$links}) {
 5116:         if ($confhash->{$role}{$item}) {
 5117:             $changes->{$role}{'links'}{$item} = 1;
 5118:         }
 5119:     }
 5120:     foreach my $item (@{$bgs}) {
 5121:         if ($confhash->{$role}{$item}) {
 5122:             $changes->{$role}{'bgs'}{$item} = 1;
 5123:         }
 5124:     }
 5125:     foreach my $item (@{$logintext}) {
 5126:         if ($confhash->{$role}{$item}) {
 5127:             $changes->{$role}{'logintext'}{$item} = 1;
 5128:         }
 5129:     }
 5130:     foreach my $img (@{$images}) {
 5131:         if ($env{'form.'.$role.'_del_'.$img}) {
 5132:             $confhash->{$role}{$img} = '';
 5133:             $changes->{$role}{'images'}{$img} = 1;
 5134:         }
 5135:         if ($role eq 'login') {
 5136:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5137:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5138:             }
 5139:         }
 5140:     }
 5141:     if ($confhash->{$role}{'font'}) {
 5142:         $changes->{$role}{'font'} = 1;
 5143:     }
 5144: }
 5145: 
 5146: sub display_colorchgs {
 5147:     my ($dom,$changes,$roles,$confhash) = @_;
 5148:     my (%choices,$resulttext);
 5149:     if (!grep(/^login$/,@{$roles})) {
 5150:         $resulttext = &mt('Changes made:').'<br />';
 5151:     }
 5152:     foreach my $role (@{$roles}) {
 5153:         if ($role eq 'login') {
 5154:             %choices = &login_choices();
 5155:         } else {
 5156:             %choices = &color_font_choices();
 5157:         }
 5158:         if (ref($changes->{$role}) eq 'HASH') {
 5159:             if ($role ne 'login') {
 5160:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5161:             }
 5162:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5163:                 if ($role ne 'login') {
 5164:                     $resulttext .= '<ul>';
 5165:                 }
 5166:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5167:                     if ($role ne 'login') {
 5168:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5169:                     }
 5170:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5171:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5172:                             if ($confhash->{$role}{$key}{$item}) {
 5173:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5174:                             } else {
 5175:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5176:                             }
 5177:                         } elsif ($confhash->{$role}{$item} eq '') {
 5178:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5179:                         } else {
 5180:                             my $newitem = $confhash->{$role}{$item};
 5181:                             if ($key eq 'images') {
 5182:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5183:                             }
 5184:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5185:                         }
 5186:                     }
 5187:                     if ($role ne 'login') {
 5188:                         $resulttext .= '</ul></li>';
 5189:                     }
 5190:                 } else {
 5191:                     if ($confhash->{$role}{$key} eq '') {
 5192:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5193:                     } else {
 5194:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5195:                     }
 5196:                 }
 5197:                 if ($role ne 'login') {
 5198:                     $resulttext .= '</ul>';
 5199:                 }
 5200:             }
 5201:         }
 5202:     }
 5203:     return $resulttext;
 5204: }
 5205: 
 5206: sub thumb_dimensions {
 5207:     return ('200','50');
 5208: }
 5209: 
 5210: sub check_dimensions {
 5211:     my ($inputfile) = @_;
 5212:     my ($fullwidth,$fullheight);
 5213:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5214:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5215:             my $imageinfo = <PIPE>;
 5216:             if (!close(PIPE)) {
 5217:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5218:             }
 5219:             chomp($imageinfo);
 5220:             my ($fullsize) = 
 5221:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5222:             if ($fullsize) {
 5223:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5224:             }
 5225:         }
 5226:     }
 5227:     return ($fullwidth,$fullheight);
 5228: }
 5229: 
 5230: sub check_configuser {
 5231:     my ($uhome,$dom,$confname,$servadm) = @_;
 5232:     my ($configuserok,%currroles);
 5233:     if ($uhome eq 'no_host') {
 5234:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5235:         my $configpass = &LONCAPA::Enrollment::create_password();
 5236:         $configuserok = 
 5237:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5238:                              $configpass,'','','','','',undef,$servadm);
 5239:     } else {
 5240:         $configuserok = 'ok';
 5241:         %currroles = 
 5242:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5243:     }
 5244:     return ($configuserok,%currroles);
 5245: }
 5246: 
 5247: sub check_authorstatus {
 5248:     my ($dom,$confname,%currroles) = @_;
 5249:     my $author_ok;
 5250:     if (!$currroles{':'.$dom.':au'}) {
 5251:         my $start = time;
 5252:         my $end = 0;
 5253:         $author_ok = 
 5254:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5255:                                         'au',$end,$start,'','','domconfig');
 5256:     } else {
 5257:         $author_ok = 'ok';
 5258:     }
 5259:     return $author_ok;
 5260: }
 5261: 
 5262: sub publishlogo {
 5263:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5264:     my ($output,$fname,$logourl);
 5265:     if ($action eq 'upload') {
 5266:         $fname=$env{'form.'.$formname.'.filename'};
 5267:         chop($env{'form.'.$formname});
 5268:     } else {
 5269:         ($fname) = ($formname =~ /([^\/]+)$/);
 5270:     }
 5271:     if ($savefileas ne '') {
 5272:         $fname = $savefileas;
 5273:     }
 5274:     $fname=&Apache::lonnet::clean_filename($fname);
 5275: # See if there is anything left
 5276:     unless ($fname) { return ('error: no uploaded file'); }
 5277:     $fname="$subdir/$fname";
 5278:     my $docroot=$r->dir_config('lonDocRoot');
 5279:     my $filepath="$docroot/priv";
 5280:     my $relpath = "$dom/$confname";
 5281:     my ($fnamepath,$file,$fetchthumb);
 5282:     $file=$fname;
 5283:     if ($fname=~m|/|) {
 5284:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5285:     }
 5286:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");  
 5287:     my $count;
 5288:     for ($count=5;$count<=$#parts;$count++) {
 5289:         $filepath.="/$parts[$count]";
 5290:         if ((-e $filepath)!=1) {
 5291:             mkdir($filepath,02770);
 5292:         }
 5293:     }
 5294:     # Check for bad extension and disallow upload
 5295:     if ($file=~/\.(\w+)$/ &&
 5296:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5297:         $output = 
 5298:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 5299:     } elsif ($file=~/\.(\w+)$/ &&
 5300:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5301:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5302:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5303:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5304:     } elsif (-d "$filepath/$file") {
 5305:         $output = &mt('File name is a directory name - rename the file and re-upload');
 5306:     } else {
 5307:         my $source = $filepath.'/'.$file;
 5308:         my $logfile;
 5309:         if (!open($logfile,">>$source".'.log')) {
 5310:             return (&mt('No write permission to Construction Space'));
 5311:         }
 5312:         print $logfile
 5313: "\n================= Publish ".localtime()." ================\n".
 5314: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5315: # Save the file
 5316:         if (!open(FH,'>'.$source)) {
 5317:             &Apache::lonnet::logthis('Failed to create '.$source);
 5318:             return (&mt('Failed to create file'));
 5319:         }
 5320:         if ($action eq 'upload') {
 5321:             if (!print FH ($env{'form.'.$formname})) {
 5322:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5323:                 return (&mt('Failed to write file'));
 5324:             }
 5325:         } else {
 5326:             my $original = &Apache::lonnet::filelocation('',$formname);
 5327:             if(!copy($original,$source)) {
 5328:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5329:                 return (&mt('Failed to write file'));
 5330:             }
 5331:         }
 5332:         close(FH);
 5333:         chmod(0660, $source); # Permissions to rw-rw---.
 5334: 
 5335:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5336:         my $copyfile=$targetdir.'/'.$file;
 5337: 
 5338:         my @parts=split(/\//,$targetdir);
 5339:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5340:         for (my $count=5;$count<=$#parts;$count++) {
 5341:             $path.="/$parts[$count]";
 5342:             if (!-e $path) {
 5343:                 print $logfile "\nCreating directory ".$path;
 5344:                 mkdir($path,02770);
 5345:             }
 5346:         }
 5347:         my $versionresult;
 5348:         if (-e $copyfile) {
 5349:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5350:         } else {
 5351:             $versionresult = 'ok';
 5352:         }
 5353:         if ($versionresult eq 'ok') {
 5354:             if (copy($source,$copyfile)) {
 5355:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5356:                 $output = 'ok';
 5357:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5358:                 push(@{$modified_urls},[$copyfile,$source]);
 5359:                 my $metaoutput = 
 5360:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5361:                 unless ($registered_cleanup) {
 5362:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5363:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5364:                     $registered_cleanup=1;
 5365:                 }
 5366:             } else {
 5367:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5368:                 $output = &mt('Failed to copy file to RES space').", $!";
 5369:             }
 5370:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5371:                 my $inputfile = $filepath.'/'.$file;
 5372:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5373:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5374:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5375:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5376:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5377:                         system("convert -sample $thumbsize $inputfile $outfile");
 5378:                         chmod(0660, $filepath.'/tn-'.$file);
 5379:                         if (-e $outfile) {
 5380:                             my $copyfile=$targetdir.'/tn-'.$file;
 5381:                             if (copy($outfile,$copyfile)) {
 5382:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5383:                                 my $thumb_metaoutput = 
 5384:                                     &write_metadata($dom,$confname,$formname,
 5385:                                                     $targetdir,'tn-'.$file,$logfile);
 5386:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5387:                                 unless ($registered_cleanup) {
 5388:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5389:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5390:                                     $registered_cleanup=1;
 5391:                                 }
 5392:                             } else {
 5393:                                 print $logfile "\nUnable to write ".$copyfile.
 5394:                                                ':'.$!."\n";
 5395:                             }
 5396:                         }
 5397:                     }
 5398:                 }
 5399:             }
 5400:         } else {
 5401:             $output = $versionresult;
 5402:         }
 5403:     }
 5404:     return ($output,$logourl);
 5405: }
 5406: 
 5407: sub logo_versioning {
 5408:     my ($targetdir,$file,$logfile) = @_;
 5409:     my $target = $targetdir.'/'.$file;
 5410:     my ($maxversion,$fn,$extn,$output);
 5411:     $maxversion = 0;
 5412:     if ($file =~ /^(.+)\.(\w+)$/) {
 5413:         $fn=$1;
 5414:         $extn=$2;
 5415:     }
 5416:     opendir(DIR,$targetdir);
 5417:     while (my $filename=readdir(DIR)) {
 5418:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5419:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5420:         }
 5421:     }
 5422:     $maxversion++;
 5423:     print $logfile "\nCreating old version ".$maxversion."\n";
 5424:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5425:     if (copy($target,$copyfile)) {
 5426:         print $logfile "Copied old target to ".$copyfile."\n";
 5427:         $copyfile=$copyfile.'.meta';
 5428:         if (copy($target.'.meta',$copyfile)) {
 5429:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5430:             $output = 'ok';
 5431:         } else {
 5432:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5433:             $output = &mt('Failed to copy old meta').", $!, ";
 5434:         }
 5435:     } else {
 5436:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5437:         $output = &mt('Failed to copy old target').", $!, ";
 5438:     }
 5439:     return $output;
 5440: }
 5441: 
 5442: sub write_metadata {
 5443:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5444:     my (%metadatafields,%metadatakeys,$output);
 5445:     $metadatafields{'title'}=$formname;
 5446:     $metadatafields{'creationdate'}=time;
 5447:     $metadatafields{'lastrevisiondate'}=time;
 5448:     $metadatafields{'copyright'}='public';
 5449:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5450:                                          $env{'user.domain'};
 5451:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5452:     $metadatafields{'domain'}=$dom;
 5453:     {
 5454:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5455:         my $mfh;
 5456:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5457:             foreach (sort keys %metadatafields) {
 5458:                 unless ($_=~/\./) {
 5459:                     my $unikey=$_;
 5460:                     $unikey=~/^([A-Za-z]+)/;
 5461:                     my $tag=$1;
 5462:                     $tag=~tr/A-Z/a-z/;
 5463:                     print $mfh "\n\<$tag";
 5464:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5465:                         my $value=$metadatafields{$unikey.'.'.$_};
 5466:                         $value=~s/\"/\'\'/g;
 5467:                         print $mfh ' '.$_.'="'.$value.'"';
 5468:                     }
 5469:                     print $mfh '>'.
 5470:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5471:                             .'</'.$tag.'>';
 5472:                 }
 5473:             }
 5474:             $output = 'ok';
 5475:             print $logfile "\nWrote metadata";
 5476:             close($mfh);
 5477:         } else {
 5478:             print $logfile "\nFailed to open metadata file";
 5479:             $output = &mt('Could not write metadata');
 5480:         }
 5481:     }
 5482:     return $output;
 5483: }
 5484: 
 5485: sub notifysubscribed {
 5486:     foreach my $targetsource (@{$modified_urls}){
 5487:         next unless (ref($targetsource) eq 'ARRAY');
 5488:         my ($target,$source)=@{$targetsource};
 5489:         if ($source ne '') {
 5490:             if (open(my $logfh,'>>'.$source.'.log')) {
 5491:                 print $logfh "\nCleanup phase: Notifications\n";
 5492:                 my @subscribed=&subscribed_hosts($target);
 5493:                 foreach my $subhost (@subscribed) {
 5494:                     print $logfh "\nNotifying host ".$subhost.':';
 5495:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5496:                     print $logfh $reply;
 5497:                 }
 5498:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5499:                 foreach my $subhost (@subscribedmeta) {
 5500:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5501:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5502:                                                         $subhost);
 5503:                     print $logfh $reply;
 5504:                 }
 5505:                 print $logfh "\n============ Done ============\n";
 5506:                 close($logfh);
 5507:             }
 5508:         }
 5509:     }
 5510:     return OK;
 5511: }
 5512: 
 5513: sub subscribed_hosts {
 5514:     my ($target) = @_;
 5515:     my @subscribed;
 5516:     if (open(my $fh,"<$target.subscription")) {
 5517:         while (my $subline=<$fh>) {
 5518:             if ($subline =~ /^($match_lonid):/) {
 5519:                 my $host = $1;
 5520:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5521:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5522:                         push(@subscribed,$host);
 5523:                     }
 5524:                 }
 5525:             }
 5526:         }
 5527:     }
 5528:     return @subscribed;
 5529: }
 5530: 
 5531: sub check_switchserver {
 5532:     my ($dom,$confname) = @_;
 5533:     my ($allowed,$switchserver);
 5534:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5535:     if ($home eq 'no_host') {
 5536:         $home = &Apache::lonnet::domain($dom,'primary');
 5537:     }
 5538:     my @ids=&Apache::lonnet::current_machine_ids();
 5539:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5540:     if (!$allowed) {
 5541: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&role=dc./'.$dom.'/">'.&mt('Switch Server').'</a>';
 5542:     }
 5543:     return $switchserver;
 5544: }
 5545: 
 5546: sub modify_quotas {
 5547:     my ($dom,$action,%domconfig) = @_;
 5548:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5549:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5550:     if ($action eq 'quotas') {
 5551:         $context = 'tools'; 
 5552:     } else { 
 5553:         $context = $action;
 5554:     }
 5555:     if ($context eq 'requestcourses') {
 5556:         @usertools = ('official','unofficial','community');
 5557:         @options =('norequest','approval','validate','autolimit');
 5558:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5559:         %titles = &courserequest_titles();
 5560:         $toolregexp = join('|',@usertools);
 5561:         %conditions = &courserequest_conditions();
 5562:     } elsif ($context eq 'requestauthor') {
 5563:         @usertools = ('author');
 5564:         %titles = &authorrequest_titles();
 5565:     } else {
 5566:         @usertools = ('aboutme','blog','webdav','portfolio');
 5567:         %titles = &tool_titles();
 5568:     }
 5569:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5570:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5571:     foreach my $key (keys(%env)) {
 5572:         if ($context eq 'requestcourses') {
 5573:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5574:                 my $item = $1;
 5575:                 my $type = $2;
 5576:                 if ($type =~ /^limit_(.+)/) {
 5577:                     $limithash{$item}{$1} = $env{$key};
 5578:                 } else {
 5579:                     $confhash{$item}{$type} = $env{$key};
 5580:                 }
 5581:             }
 5582:         } elsif ($context eq 'requestauthor') {
 5583:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5584:                 $confhash{$1} = $env{$key};
 5585:             }
 5586:         } else {
 5587:             if ($key =~ /^form\.quota_(.+)$/) {
 5588:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5589:             }
 5590:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5591:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5592:             }
 5593:         }
 5594:     }
 5595:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5596:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5597:         @approvalnotify = sort(@approvalnotify);
 5598:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5599:         if (ref($domconfig{$action}) eq 'HASH') {
 5600:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5601:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5602:                     $changes{'notify'}{'approval'} = 1;
 5603:                 }
 5604:             } else {
 5605:                 if ($confhash{'notify'}{'approval'}) {
 5606:                     $changes{'notify'}{'approval'} = 1;
 5607:                 }
 5608:             }
 5609:         } else {
 5610:             if ($confhash{'notify'}{'approval'}) {
 5611:                 $changes{'notify'}{'approval'} = 1;
 5612:             }
 5613:         }
 5614:     } else {
 5615:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5616:     }
 5617:     foreach my $item (@usertools) {
 5618:         foreach my $type (@{$types},'default','_LC_adv') {
 5619:             my $unset; 
 5620:             if ($context eq 'requestcourses') {
 5621:                 $unset = '0';
 5622:                 if ($type eq '_LC_adv') {
 5623:                     $unset = '';
 5624:                 }
 5625:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5626:                     $confhash{$item}{$type} .= '=';
 5627:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5628:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5629:                     }
 5630:                 }
 5631:             } elsif ($context eq 'requestauthor') {
 5632:                 $unset = '0';
 5633:                 if ($type eq '_LC_adv') {
 5634:                     $unset = '';
 5635:                 }
 5636:             } else {
 5637:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5638:                     $confhash{$item}{$type} = 1;
 5639:                 } else {
 5640:                     $confhash{$item}{$type} = 0;
 5641:                 }
 5642:             }
 5643:             if (ref($domconfig{$action}) eq 'HASH') {
 5644:                 if ($action eq 'requestauthor') {
 5645:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5646:                         $changes{$type} = 1;
 5647:                     }
 5648:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5649:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5650:                         $changes{$item}{$type} = 1;
 5651:                     }
 5652:                 } else {
 5653:                     if ($context eq 'requestcourses') {
 5654:                         if ($confhash{$item}{$type} ne $unset) {
 5655:                             $changes{$item}{$type} = 1;
 5656:                         }
 5657:                     } else {
 5658:                         if (!$confhash{$item}{$type}) {
 5659:                             $changes{$item}{$type} = 1;
 5660:                         }
 5661:                     }
 5662:                 }
 5663:             } else {
 5664:                 if ($context eq 'requestcourses') {
 5665:                     if ($confhash{$item}{$type} ne $unset) {
 5666:                         $changes{$item}{$type} = 1;
 5667:                     }
 5668:                 } elsif ($context eq 'requestauthor') {
 5669:                     if ($confhash{$type} ne $unset) {
 5670:                         $changes{$type} = 1;
 5671:                     }
 5672:                 } else {
 5673:                     if (!$confhash{$item}{$type}) {
 5674:                         $changes{$item}{$type} = 1;
 5675:                     }
 5676:                 }
 5677:             }
 5678:         }
 5679:     }
 5680:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5681:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5682:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5683:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5684:                     if (exists($confhash{'defaultquota'}{$key})) {
 5685:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5686:                             $changes{'defaultquota'}{$key} = 1;
 5687:                         }
 5688:                     } else {
 5689:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5690:                     }
 5691:                 }
 5692:             } else {
 5693:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5694:                     if (exists($confhash{'defaultquota'}{$key})) {
 5695:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5696:                             $changes{'defaultquota'}{$key} = 1;
 5697:                         }
 5698:                     } else {
 5699:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5700:                     }
 5701:                 }
 5702:             }
 5703:         }
 5704:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5705:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5706:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5707:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5708:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5709:                             $changes{'defaultquota'}{$key} = 1;
 5710:                         }
 5711:                     } else {
 5712:                         if (!exists($domconfig{'quotas'}{$key})) {
 5713:                             $changes{'defaultquota'}{$key} = 1;
 5714:                         }
 5715:                     }
 5716:                 } else {
 5717:                     $changes{'defaultquota'}{$key} = 1;
 5718:                 }
 5719:             }
 5720:         }
 5721:     }
 5722: 
 5723:     if ($context eq 'requestauthor') {
 5724:         $domdefaults{'requestauthor'} = \%confhash;
 5725:     } else {
 5726:         foreach my $key (keys(%confhash)) {
 5727:             $domdefaults{$key} = $confhash{$key};
 5728:         }
 5729:     }
 5730: 
 5731:     my %quotahash = (
 5732:                       $action => { %confhash }
 5733:                     );
 5734:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5735:                                              $dom);
 5736:     if ($putresult eq 'ok') {
 5737:         if (keys(%changes) > 0) {
 5738:             my $cachetime = 24*60*60;
 5739:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5740: 
 5741:             $resulttext = &mt('Changes made:').'<ul>';
 5742:             unless (($context eq 'requestcourses') ||
 5743:                     ($context eq 'requestauthor')) {
 5744:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5745:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5746:                     foreach my $type (@{$types},'default') {
 5747:                         if (defined($changes{'defaultquota'}{$type})) {
 5748:                             my $typetitle = $usertypes->{$type};
 5749:                             if ($type eq 'default') {
 5750:                                 $typetitle = $othertitle;
 5751:                             }
 5752:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5753:                         }
 5754:                     }
 5755:                     $resulttext .= '</ul></li>';
 5756:                 }
 5757:             }
 5758:             my %newenv;
 5759:             foreach my $item (@usertools) {
 5760:                 my (%haschgs,%inconf);
 5761:                 if ($context eq 'requestauthor') {
 5762:                     %haschgs = %changes;
 5763:                     %inconf = %confhash;
 5764:                 } else {
 5765:                     if (ref($changes{$item}) eq 'HASH') {
 5766:                         %haschgs = %{$changes{$item}};
 5767:                     }
 5768:                     if (ref($confhash{$item}) eq 'HASH') {
 5769:                         %inconf = %{$confhash{$item}};
 5770:                     }
 5771:                 }
 5772:                 if (keys(%haschgs) > 0) {
 5773:                     my $newacc = 
 5774:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5775:                                                           $env{'user.domain'},
 5776:                                                           $item,'reload',$context);
 5777:                     if (($context eq 'requestcourses') ||
 5778:                         ($context eq 'requestauthor')) {
 5779:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5780:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5781:                         }
 5782:                     } else {
 5783:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5784:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5785:                         }
 5786:                     }
 5787:                     unless ($context eq 'requestauthor') {
 5788:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5789:                     }
 5790:                     foreach my $type (@{$types},'default','_LC_adv') {
 5791:                         if ($haschgs{$type}) {
 5792:                             my $typetitle = $usertypes->{$type};
 5793:                             if ($type eq 'default') {
 5794:                                 $typetitle = $othertitle;
 5795:                             } elsif ($type eq '_LC_adv') {
 5796:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5797:                             }
 5798:                             if ($inconf{$type}) {
 5799:                                 if ($context eq 'requestcourses') {
 5800:                                     my $cond;
 5801:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 5802:                                         if ($1 eq '') {
 5803:                                             $cond = &mt('(Automatic processing of any request).');
 5804:                                         } else {
 5805:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5806:                                         }
 5807:                                     } else { 
 5808:                                         $cond = $conditions{$inconf{$type}};
 5809:                                     }
 5810:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5811:                                 } else {
 5812:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5813:                                 }
 5814:                             } else {
 5815:                                 if ($type eq '_LC_adv') {
 5816:                                     if ($inconf{$type} eq '0') {
 5817:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5818:                                     } else { 
 5819:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5820:                                     }
 5821:                                 } else {
 5822:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5823:                                 }
 5824:                             }
 5825:                         }
 5826:                     }
 5827:                     unless ($context eq 'requestauthor') {
 5828:                         $resulttext .= '</ul></li>';
 5829:                     }
 5830:                 }
 5831:             }
 5832:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 5833:                 if (ref($changes{'notify'}) eq 'HASH') {
 5834:                     if ($changes{'notify'}{'approval'}) {
 5835:                         if (ref($confhash{'notify'}) eq 'HASH') {
 5836:                             if ($confhash{'notify'}{'approval'}) {
 5837:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 5838:                             } else {
 5839:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 5840:                             }
 5841:                         }
 5842:                     }
 5843:                 }
 5844:             }
 5845:             $resulttext .= '</ul>';
 5846:             if (keys(%newenv)) {
 5847:                 &Apache::lonnet::appenv(\%newenv);
 5848:             }
 5849:         } else {
 5850:             if ($context eq 'requestcourses') {
 5851:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 5852:             } elsif ($context eq 'requestauthor') {
 5853:                 $resulttext = &mt('No changes made to rights to request author space.');
 5854:             } else {
 5855:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 5856:             }
 5857:         }
 5858:     } else {
 5859:         $resulttext = '<span class="LC_error">'.
 5860: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5861:     }
 5862:     return $resulttext;
 5863: }
 5864: 
 5865: sub modify_autoenroll {
 5866:     my ($dom,%domconfig) = @_;
 5867:     my ($resulttext,%changes);
 5868:     my %currautoenroll;
 5869:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 5870:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 5871:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 5872:         }
 5873:     }
 5874:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 5875:     my %title = ( run => 'Auto-enrollment active',
 5876:                   sender => 'Sender for notification messages',
 5877:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 5878:     my @offon = ('off','on');
 5879:     my $sender_uname = $env{'form.sender_uname'};
 5880:     my $sender_domain = $env{'form.sender_domain'};
 5881:     if ($sender_domain eq '') {
 5882:         $sender_uname = '';
 5883:     } elsif ($sender_uname eq '') {
 5884:         $sender_domain = '';
 5885:     }
 5886:     my $coowners = $env{'form.autoassign_coowners'};
 5887:     my %autoenrollhash =  (
 5888:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 5889:                                        'sender_uname' => $sender_uname,
 5890:                                        'sender_domain' => $sender_domain,
 5891:                                        'co-owners' => $coowners,
 5892:                                 }
 5893:                      );
 5894:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 5895:                                              $dom);
 5896:     if ($putresult eq 'ok') {
 5897:         if (exists($currautoenroll{'run'})) {
 5898:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 5899:                  $changes{'run'} = 1;
 5900:              }
 5901:         } elsif ($autorun) {
 5902:             if ($env{'form.autoenroll_run'} ne '1') {
 5903:                  $changes{'run'} = 1;
 5904:             }
 5905:         }
 5906:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 5907:             $changes{'sender'} = 1;
 5908:         }
 5909:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 5910:             $changes{'sender'} = 1;
 5911:         }
 5912:         if ($currautoenroll{'co-owners'} ne '') {
 5913:             if ($currautoenroll{'co-owners'} ne $coowners) {
 5914:                 $changes{'coowners'} = 1;
 5915:             }
 5916:         } elsif ($coowners) {
 5917:             $changes{'coowners'} = 1;
 5918:         }      
 5919:         if (keys(%changes) > 0) {
 5920:             $resulttext = &mt('Changes made:').'<ul>';
 5921:             if ($changes{'run'}) {
 5922:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 5923:             }
 5924:             if ($changes{'sender'}) {
 5925:                 if ($sender_uname eq '' || $sender_domain eq '') {
 5926:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 5927:                 } else {
 5928:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 5929:                 }
 5930:             }
 5931:             if ($changes{'coowners'}) {
 5932:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 5933:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 5934:             }
 5935:             $resulttext .= '</ul>';
 5936:         } else {
 5937:             $resulttext = &mt('No changes made to auto-enrollment settings');
 5938:         }
 5939:     } else {
 5940:         $resulttext = '<span class="LC_error">'.
 5941: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5942:     }
 5943:     return $resulttext;
 5944: }
 5945: 
 5946: sub modify_autoupdate {
 5947:     my ($dom,%domconfig) = @_;
 5948:     my ($resulttext,%currautoupdate,%fields,%changes);
 5949:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 5950:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 5951:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 5952:         }
 5953:     }
 5954:     my @offon = ('off','on');
 5955:     my %title = &Apache::lonlocal::texthash (
 5956:                    run => 'Auto-update:',
 5957:                    classlists => 'Updates to user information in classlists?'
 5958:                 );
 5959:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5960:     my %fieldtitles = &Apache::lonlocal::texthash (
 5961:                         id => 'Student/Employee ID',
 5962:                         permanentemail => 'E-mail address',
 5963:                         lastname => 'Last Name',
 5964:                         firstname => 'First Name',
 5965:                         middlename => 'Middle Name',
 5966:                         generation => 'Generation',
 5967:                       );
 5968:     $othertitle = &mt('All users');
 5969:     if (keys(%{$usertypes}) >  0) {
 5970:         $othertitle = &mt('Other users');
 5971:     }
 5972:     foreach my $key (keys(%env)) {
 5973:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 5974:             my ($usertype,$item) = ($1,$2);
 5975:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 5976:                 if ($usertype eq 'default') {   
 5977:                     push(@{$fields{$1}},$2);
 5978:                 } elsif (ref($types) eq 'ARRAY') {
 5979:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 5980:                         push(@{$fields{$1}},$2);
 5981:                     }
 5982:                 }
 5983:             }
 5984:         }
 5985:     }
 5986:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 5987:     @lockablenames = sort(@lockablenames);
 5988:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 5989:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 5990:         if (@changed) {
 5991:             $changes{'lockablenames'} = 1;
 5992:         }
 5993:     } else {
 5994:         if (@lockablenames) {
 5995:             $changes{'lockablenames'} = 1;
 5996:         }
 5997:     }
 5998:     my %updatehash = (
 5999:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6000:                                       classlists => $env{'form.classlists'},
 6001:                                       fields => {%fields},
 6002:                                       lockablenames => \@lockablenames,
 6003:                                     }
 6004:                      );
 6005:     foreach my $key (keys(%currautoupdate)) {
 6006:         if (($key eq 'run') || ($key eq 'classlists')) {
 6007:             if (exists($updatehash{autoupdate}{$key})) {
 6008:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6009:                     $changes{$key} = 1;
 6010:                 }
 6011:             }
 6012:         } elsif ($key eq 'fields') {
 6013:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6014:                 foreach my $item (@{$types},'default') {
 6015:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6016:                         my $change = 0;
 6017:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6018:                             if (!exists($fields{$item})) {
 6019:                                 $change = 1;
 6020:                                 last;
 6021:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6022:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6023:                                     $change = 1;
 6024:                                     last;
 6025:                                 }
 6026:                             }
 6027:                         }
 6028:                         if ($change) {
 6029:                             push(@{$changes{$key}},$item);
 6030:                         }
 6031:                     } 
 6032:                 }
 6033:             }
 6034:         } elsif ($key eq 'lockablenames') {
 6035:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6036:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6037:                 if (@changed) {
 6038:                     $changes{'lockablenames'} = 1;
 6039:                 }
 6040:             } else {
 6041:                 if (@lockablenames) {
 6042:                     $changes{'lockablenames'} = 1;
 6043:                 }
 6044:             }
 6045:         }
 6046:     }
 6047:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6048:         if (@lockablenames) {
 6049:             $changes{'lockablenames'} = 1;
 6050:         }
 6051:     }
 6052:     foreach my $item (@{$types},'default') {
 6053:         if (defined($fields{$item})) {
 6054:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6055:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6056:                     my $change = 0;
 6057:                     if (ref($fields{$item}) eq 'ARRAY') {
 6058:                         foreach my $type (@{$fields{$item}}) {
 6059:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6060:                                 $change = 1;
 6061:                                 last;
 6062:                             }
 6063:                         }
 6064:                     }
 6065:                     if ($change) {
 6066:                         push(@{$changes{'fields'}},$item);
 6067:                     }
 6068:                 } else {
 6069:                     push(@{$changes{'fields'}},$item);
 6070:                 }
 6071:             } else {
 6072:                 push(@{$changes{'fields'}},$item);
 6073:             }
 6074:         }
 6075:     }
 6076:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6077:                                              $dom);
 6078:     if ($putresult eq 'ok') {
 6079:         if (keys(%changes) > 0) {
 6080:             $resulttext = &mt('Changes made:').'<ul>';
 6081:             foreach my $key (sort(keys(%changes))) {
 6082:                 if ($key eq 'lockablenames') {
 6083:                     $resulttext .= '<li>';
 6084:                     if (@lockablenames) {
 6085:                         $usertypes->{'default'} = $othertitle;
 6086:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6087:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6088:                     } else {
 6089:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6090:                     }
 6091:                     $resulttext .= '</li>';
 6092:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6093:                     foreach my $item (@{$changes{$key}}) {
 6094:                         my @newvalues;
 6095:                         foreach my $type (@{$fields{$item}}) {
 6096:                             push(@newvalues,$fieldtitles{$type});
 6097:                         }
 6098:                         my $newvaluestr;
 6099:                         if (@newvalues > 0) {
 6100:                             $newvaluestr = join(', ',@newvalues);
 6101:                         } else {
 6102:                             $newvaluestr = &mt('none');
 6103:                         }
 6104:                         if ($item eq 'default') {
 6105:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6106:                         } else {
 6107:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6108:                         }
 6109:                     }
 6110:                 } else {
 6111:                     my $newvalue;
 6112:                     if ($key eq 'run') {
 6113:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6114:                     } else {
 6115:                         $newvalue = $offon[$env{'form.'.$key}];
 6116:                     }
 6117:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6118:                 }
 6119:             }
 6120:             $resulttext .= '</ul>';
 6121:         } else {
 6122:             $resulttext = &mt('No changes made to autoupdates');
 6123:         }
 6124:     } else {
 6125:         $resulttext = '<span class="LC_error">'.
 6126: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6127:     }
 6128:     return $resulttext;
 6129: }
 6130: 
 6131: sub modify_autocreate {
 6132:     my ($dom,%domconfig) = @_;
 6133:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6134:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6135:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6136:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6137:         }
 6138:     }
 6139:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6140:                  req => 'Auto-creation of validated requests for official courses',
 6141:                  xmldc => 'Identity of course creator of courses from XML files',
 6142:                );
 6143:     my @types = ('xml','req');
 6144:     foreach my $item (@types) {
 6145:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6146:         $newvals{$item} =~ s/\D//g;
 6147:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6148:     }
 6149:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6150:     my %domcoords = &get_active_dcs($dom);
 6151:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6152:         $newvals{'xmldc'} = '';
 6153:     } 
 6154:     %autocreatehash =  (
 6155:                         autocreate => { xml => $newvals{'xml'},
 6156:                                         req => $newvals{'req'},
 6157:                                       }
 6158:                        );
 6159:     if ($newvals{'xmldc'} ne '') {
 6160:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6161:     }
 6162:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6163:                                              $dom);
 6164:     if ($putresult eq 'ok') {
 6165:         my @items = @types;
 6166:         if ($newvals{'xml'}) {
 6167:             push(@items,'xmldc');
 6168:         }
 6169:         foreach my $item (@items) {
 6170:             if (exists($currautocreate{$item})) {
 6171:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6172:                     $changes{$item} = 1;
 6173:                 }
 6174:             } elsif ($newvals{$item}) {
 6175:                 $changes{$item} = 1;
 6176:             }
 6177:         }
 6178:         if (keys(%changes) > 0) {
 6179:             my @offon = ('off','on'); 
 6180:             $resulttext = &mt('Changes made:').'<ul>';
 6181:             foreach my $item (@types) {
 6182:                 if ($changes{$item}) {
 6183:                     my $newtxt = $offon[$newvals{$item}];
 6184:                     $resulttext .= '<li>'.&mt("$title{$item} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>';
 6185:                 }
 6186:             }
 6187:             if ($changes{'xmldc'}) {
 6188:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6189:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6190:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>'; 
 6191:             }
 6192:             $resulttext .= '</ul>';
 6193:         } else {
 6194:             $resulttext = &mt('No changes made to auto-creation settings');
 6195:         }
 6196:     } else {
 6197:         $resulttext = '<span class="LC_error">'.
 6198:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6199:     }
 6200:     return $resulttext;
 6201: }
 6202: 
 6203: sub modify_directorysrch {
 6204:     my ($dom,%domconfig) = @_;
 6205:     my ($resulttext,%changes);
 6206:     my %currdirsrch;
 6207:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6208:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6209:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6210:         }
 6211:     }
 6212:     my %title = ( available => 'Directory search available',
 6213:                   localonly => 'Other domains can search',
 6214:                   searchby => 'Search types',
 6215:                   searchtypes => 'Search latitude');
 6216:     my @offon = ('off','on');
 6217:     my @otherdoms = ('Yes','No');
 6218: 
 6219:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6220:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6221:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6222: 
 6223:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6224:     if (keys(%{$usertypes}) == 0) {
 6225:         @cansearch = ('default');
 6226:     } else {
 6227:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6228:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6229:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6230:                     push(@{$changes{'cansearch'}},$type);
 6231:                 }
 6232:             }
 6233:             foreach my $type (@cansearch) {
 6234:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6235:                     push(@{$changes{'cansearch'}},$type);
 6236:                 }
 6237:             }
 6238:         } else {
 6239:             push(@{$changes{'cansearch'}},@cansearch);
 6240:         }
 6241:     }
 6242: 
 6243:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6244:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6245:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6246:                 push(@{$changes{'searchby'}},$by);
 6247:             }
 6248:         }
 6249:         foreach my $by (@searchby) {
 6250:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6251:                 push(@{$changes{'searchby'}},$by);
 6252:             }
 6253:         }
 6254:     } else {
 6255:         push(@{$changes{'searchby'}},@searchby);
 6256:     }
 6257: 
 6258:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6259:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6260:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6261:                 push(@{$changes{'searchtypes'}},$type);
 6262:             }
 6263:         }
 6264:         foreach my $type (@searchtypes) {
 6265:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6266:                 push(@{$changes{'searchtypes'}},$type);
 6267:             }
 6268:         }
 6269:     } else {
 6270:         if (exists($currdirsrch{'searchtypes'})) {
 6271:             foreach my $type (@searchtypes) {  
 6272:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6273:                     push(@{$changes{'searchtypes'}},$type);
 6274:                 }
 6275:             }
 6276:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6277:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6278:             }   
 6279:         } else {
 6280:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6281:         }
 6282:     }
 6283: 
 6284:     my %dirsrch_hash =  (
 6285:             directorysrch => { available => $env{'form.dirsrch_available'},
 6286:                                cansearch => \@cansearch,
 6287:                                localonly => $env{'form.dirsrch_localonly'},
 6288:                                searchby => \@searchby,
 6289:                                searchtypes => \@searchtypes,
 6290:                              }
 6291:             );
 6292:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6293:                                              $dom);
 6294:     if ($putresult eq 'ok') {
 6295:         if (exists($currdirsrch{'available'})) {
 6296:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6297:                  $changes{'available'} = 1;
 6298:              }
 6299:         } else {
 6300:             if ($env{'form.dirsrch_available'} eq '1') {
 6301:                 $changes{'available'} = 1;
 6302:             }
 6303:         }
 6304:         if (exists($currdirsrch{'localonly'})) {
 6305:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6306:                  $changes{'localonly'} = 1;
 6307:              }
 6308:         } else {
 6309:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6310:                 $changes{'localonly'} = 1;
 6311:             }
 6312:         }
 6313:         if (keys(%changes) > 0) {
 6314:             $resulttext = &mt('Changes made:').'<ul>';
 6315:             if ($changes{'available'}) {
 6316:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6317:             }
 6318:             if ($changes{'localonly'}) {
 6319:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6320:             }
 6321: 
 6322:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6323:                 my $chgtext;
 6324:                 if (ref($usertypes) eq 'HASH') {
 6325:                     if (keys(%{$usertypes}) > 0) {
 6326:                         foreach my $type (@{$types}) {
 6327:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6328:                                 $chgtext .= $usertypes->{$type}.'; ';
 6329:                             }
 6330:                         }
 6331:                         if (grep(/^default$/,@cansearch)) {
 6332:                             $chgtext .= $othertitle;
 6333:                         } else {
 6334:                             $chgtext =~ s/\; $//;
 6335:                         }
 6336:                         $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>';
 6337:                     }
 6338:                 }
 6339:             }
 6340:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6341:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6342:                 my $chgtext;
 6343:                 foreach my $type (@{$titleorder}) {
 6344:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6345:                         if (defined($searchtitles->{$type})) {
 6346:                             $chgtext .= $searchtitles->{$type}.'; ';
 6347:                         }
 6348:                     }
 6349:                 }
 6350:                 $chgtext =~ s/\; $//;
 6351:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6352:             }
 6353:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6354:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6355:                 my $chgtext;
 6356:                 foreach my $type (@{$srchtypeorder}) {
 6357:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6358:                         if (defined($srchtypes_desc->{$type})) {
 6359:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6360:                         }
 6361:                     }
 6362:                 }
 6363:                 $chgtext =~ s/\; $//;
 6364:                 $resulttext .= '<li>'.&mt("$title{'searchtypes'} set to: \"[_1]\"",$chgtext).'</li>';
 6365:             }
 6366:             $resulttext .= '</ul>';
 6367:         } else {
 6368:             $resulttext = &mt('No changes made to institution directory search settings');
 6369:         }
 6370:     } else {
 6371:         $resulttext = '<span class="LC_error">'.
 6372:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6373:     }
 6374:     return $resulttext;
 6375: }
 6376: 
 6377: sub modify_contacts {
 6378:     my ($dom,%domconfig) = @_;
 6379:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6380:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6381:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6382:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6383:         }
 6384:     }
 6385:     my (%others,%to,%bcc);
 6386:     my @contacts = ('supportemail','adminemail');
 6387:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6388:                     'requestsmail');
 6389:     foreach my $type (@mailings) {
 6390:         @{$newsetting{$type}} = 
 6391:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6392:         foreach my $item (@contacts) {
 6393:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6394:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6395:             } else {
 6396:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6397:             }
 6398:         }  
 6399:         $others{$type} = $env{'form.'.$type.'_others'};
 6400:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6401:         if ($type eq 'helpdeskmail') {
 6402:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6403:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6404:         }
 6405:     }
 6406:     foreach my $item (@contacts) {
 6407:         $to{$item} = $env{'form.'.$item};
 6408:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6409:     }
 6410:     if (keys(%currsetting) > 0) {
 6411:         foreach my $item (@contacts) {
 6412:             if ($to{$item} ne $currsetting{$item}) {
 6413:                 $changes{$item} = 1;
 6414:             }
 6415:         }
 6416:         foreach my $type (@mailings) {
 6417:             foreach my $item (@contacts) {
 6418:                 if (ref($currsetting{$type}) eq 'HASH') {
 6419:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6420:                         push(@{$changes{$type}},$item);
 6421:                     }
 6422:                 } else {
 6423:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6424:                 }
 6425:             }
 6426:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6427:                 push(@{$changes{$type}},'others');
 6428:             }
 6429:             if ($type eq 'helpdeskmail') {   
 6430:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6431:                     push(@{$changes{$type}},'bcc'); 
 6432:                 }
 6433:             }
 6434:         }
 6435:     } else {
 6436:         my %default;
 6437:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6438:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6439:         $default{'errormail'} = 'adminemail';
 6440:         $default{'packagesmail'} = 'adminemail';
 6441:         $default{'helpdeskmail'} = 'supportemail';
 6442:         $default{'lonstatusmail'} = 'adminemail';
 6443:         $default{'requestsmail'} = 'adminemail';
 6444:         foreach my $item (@contacts) {
 6445:            if ($to{$item} ne $default{$item}) {
 6446:               $changes{$item} = 1;
 6447:            } 
 6448:         }
 6449:         foreach my $type (@mailings) {
 6450:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6451:                
 6452:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6453:             }
 6454:             if ($others{$type} ne '') {
 6455:                 push(@{$changes{$type}},'others');
 6456:             }
 6457:             if ($type eq 'helpdeskmail') {
 6458:                 if ($bcc{$type} ne '') {
 6459:                     push(@{$changes{$type}},'bcc');
 6460:                 }
 6461:             }
 6462:         }
 6463:     }
 6464:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6465:                                              $dom);
 6466:     if ($putresult eq 'ok') {
 6467:         if (keys(%changes) > 0) {
 6468:             my ($titles,$short_titles)  = &contact_titles();
 6469:             $resulttext = &mt('Changes made:').'<ul>';
 6470:             foreach my $item (@contacts) {
 6471:                 if ($changes{$item}) {
 6472:                     $resulttext .= '<li>'.$titles->{$item}.
 6473:                                     &mt(' set to: ').
 6474:                                     '<span class="LC_cusr_emph">'.
 6475:                                     $to{$item}.'</span></li>';
 6476:                 }
 6477:             }
 6478:             foreach my $type (@mailings) {
 6479:                 if (ref($changes{$type}) eq 'ARRAY') {
 6480:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6481:                     my @text;
 6482:                     foreach my $item (@{$newsetting{$type}}) {
 6483:                         push(@text,$short_titles->{$item});
 6484:                     }
 6485:                     if ($others{$type} ne '') {
 6486:                         push(@text,$others{$type});
 6487:                     }
 6488:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6489:                                    join(', ',@text).'</span>';
 6490:                     if ($type eq 'helpdeskmail') {
 6491:                         if ($bcc{$type} ne '') {
 6492:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6493:                         }
 6494:                     }
 6495:                     $resulttext .= '</li>';
 6496:                 }
 6497:             }
 6498:             $resulttext .= '</ul>';
 6499:         } else {
 6500:             $resulttext = &mt('No changes made to contact information');
 6501:         }
 6502:     } else {
 6503:         $resulttext = '<span class="LC_error">'.
 6504:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6505:     }
 6506:     return $resulttext;
 6507: }
 6508: 
 6509: sub modify_usercreation {
 6510:     my ($dom,%domconfig) = @_;
 6511:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6512:     my $warningmsg;
 6513:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6514:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6515:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6516:         }
 6517:     }
 6518:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6519:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6520:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6521:     my @contexts = ('author','course','requestcrs','selfcreate');
 6522:     foreach my $item(@contexts) {
 6523:         if ($item eq 'selfcreate') {
 6524:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6525:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6526:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6527:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6528:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6529:                         $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.');   
 6530:                     }
 6531:                 }
 6532:             }
 6533:         } else {
 6534:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6535:         }
 6536:     }
 6537:     my ($othertitle,$usertypes,$types) = 
 6538:         &Apache::loncommon::sorted_inst_types($dom);
 6539:     if (ref($types) eq 'ARRAY') {
 6540:         if (@{$types} > 0) {
 6541:             @{$cancreate{'statustocreate'}} = 
 6542:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6543:         } else {
 6544:             @{$cancreate{'statustocreate'}} = ();
 6545:         }
 6546:         push(@contexts,'statustocreate');
 6547:     }
 6548:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6549:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6550:         foreach my $item (@contexts) {
 6551:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6552:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6553:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6554:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6555:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6556:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6557:                                     push(@{$changes{'cancreate'}},$item);
 6558:                                 }
 6559:                             }
 6560:                         }
 6561:                     }
 6562:                 } else {
 6563:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6564:                         if (@{$cancreate{$item}} > 0) {
 6565:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6566:                                 push(@{$changes{'cancreate'}},$item);
 6567:                             }
 6568:                         }
 6569:                     } else {
 6570:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6571:                             if (@{$cancreate{$item}} < 3) {
 6572:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6573:                                     push(@{$changes{'cancreate'}},$item);
 6574:                                 }
 6575:                             }
 6576:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6577:                             if (@{$cancreate{$item}} > 0) {
 6578:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6579:                                     push(@{$changes{'cancreate'}},$item);
 6580:                                 }
 6581:                             }
 6582:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6583:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6584:                                 push(@{$changes{'cancreate'}},$item);
 6585:                             }
 6586:                         }
 6587:                     }
 6588:                 }
 6589:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6590:                     foreach my $type (@{$cancreate{$item}}) {
 6591:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6592:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6593:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6594:                                     push(@{$changes{'cancreate'}},$item);
 6595:                                 }
 6596:                             }
 6597:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6598:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6599:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6600:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6601:                                     push(@{$changes{'cancreate'}},$item);
 6602:                                 }
 6603:                             }
 6604:                         }
 6605:                     }
 6606:                 }
 6607:             } else {
 6608:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6609:                     push(@{$changes{'cancreate'}},$item);
 6610:                 }
 6611:             }
 6612:         }
 6613:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6614:         foreach my $item (@contexts) {
 6615:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6616:                 if ($cancreate{$item} ne 'any') {
 6617:                     push(@{$changes{'cancreate'}},$item);
 6618:                 }
 6619:             } else {
 6620:                 if ($cancreate{$item} ne 'none') {
 6621:                     push(@{$changes{'cancreate'}},$item);
 6622:                 }
 6623:             }
 6624:         }
 6625:     } else {
 6626:         foreach my $item (@contexts)  {
 6627:             push(@{$changes{'cancreate'}},$item);
 6628:         }
 6629:     }
 6630: 
 6631:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6632:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6633:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6634:                 push(@{$changes{'username_rule'}},$type);
 6635:             }
 6636:         }
 6637:         foreach my $type (@username_rule) {
 6638:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6639:                 push(@{$changes{'username_rule'}},$type);
 6640:             }
 6641:         }
 6642:     } else {
 6643:         push(@{$changes{'username_rule'}},@username_rule);
 6644:     }
 6645: 
 6646:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6647:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 6648:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 6649:                 push(@{$changes{'id_rule'}},$type);
 6650:             }
 6651:         }
 6652:         foreach my $type (@id_rule) {
 6653:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 6654:                 push(@{$changes{'id_rule'}},$type);
 6655:             }
 6656:         }
 6657:     } else {
 6658:         push(@{$changes{'id_rule'}},@id_rule);
 6659:     }
 6660: 
 6661:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 6662:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 6663:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 6664:                 push(@{$changes{'email_rule'}},$type);
 6665:             }
 6666:         }
 6667:         foreach my $type (@email_rule) {
 6668:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 6669:                 push(@{$changes{'email_rule'}},$type);
 6670:             }
 6671:         }
 6672:     } else {
 6673:         push(@{$changes{'email_rule'}},@email_rule);
 6674:     }
 6675: 
 6676:     my @authen_contexts = ('author','course','domain');
 6677:     my @authtypes = ('int','krb4','krb5','loc');
 6678:     my %authhash;
 6679:     foreach my $item (@authen_contexts) {
 6680:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 6681:         foreach my $auth (@authtypes) {
 6682:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 6683:                 $authhash{$item}{$auth} = 1;
 6684:             } else {
 6685:                 $authhash{$item}{$auth} = 0;
 6686:             }
 6687:         }
 6688:     }
 6689:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 6690:         foreach my $item (@authen_contexts) {
 6691:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 6692:                 foreach my $auth (@authtypes) {
 6693:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 6694:                         push(@{$changes{'authtypes'}},$item);
 6695:                         last;
 6696:                     }
 6697:                 }
 6698:             }
 6699:         }
 6700:     } else {
 6701:         foreach my $item (@authen_contexts) {
 6702:             push(@{$changes{'authtypes'}},$item);
 6703:         }
 6704:     }
 6705: 
 6706:     my %usercreation_hash =  (
 6707:             usercreation => {
 6708:                               cancreate     => \%cancreate,
 6709:                               username_rule => \@username_rule,
 6710:                               id_rule       => \@id_rule,
 6711:                               email_rule    => \@email_rule,
 6712:                               authtypes     => \%authhash,
 6713:                             }
 6714:             );
 6715: 
 6716:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 6717:                                              $dom);
 6718: 
 6719:     my %selfcreatetypes = (
 6720:                              sso   => 'users authenticated by institutional single sign on',
 6721:                              login => 'users authenticated by institutional log-in',
 6722:                              email => 'users who provide a valid e-mail address for use as the username',
 6723:                           );
 6724:     if ($putresult eq 'ok') {
 6725:         if (keys(%changes) > 0) {
 6726:             $resulttext = &mt('Changes made:').'<ul>';
 6727:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 6728:                 my %lt = &usercreation_types();
 6729:                 foreach my $type (@{$changes{'cancreate'}}) {
 6730:                     my $chgtext;
 6731:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 6732:                         $chgtext = $lt{$type}.', ';
 6733:                     }
 6734:                     if ($type eq 'selfcreate') {
 6735:                         if (@{$cancreate{$type}} == 0) {
 6736:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 6737:                         } else {
 6738:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 6739:                             foreach my $case (@{$cancreate{$type}}) {
 6740:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 6741:                             }
 6742:                             $chgtext .= '</ul>';
 6743:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 6744:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 6745:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 6746:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 6747:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6748:                                         }
 6749:                                     }
 6750:                                 }
 6751:                             }
 6752:                         }
 6753:                     } elsif ($type eq 'statustocreate') {
 6754:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 6755:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 6756:                             if (@{$cancreate{'selfcreate'}} > 0) {
 6757:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 6758: 
 6759:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 6760:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 6761:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6762:                                     } 
 6763:                                 } elsif (ref($usertypes) eq 'HASH') {
 6764:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6765:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 6766:                                     } else {
 6767:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 6768:                                     }
 6769:                                     $chgtext .= '<ul>';
 6770:                                     foreach my $case (@{$cancreate{$type}}) {
 6771:                                         if ($case eq 'default') {
 6772:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 6773:                                         } else {
 6774:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 6775:                                         }
 6776:                                     }
 6777:                                     $chgtext .= '</ul>';
 6778:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6779:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 6780:                                     }
 6781:                                 }
 6782:                             } else {
 6783:                                 if (@{$cancreate{$type}} == 0) {
 6784:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 6785:                                 } else {
 6786:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 6787:                                 }
 6788:                             }
 6789:                         }
 6790:                     } elsif ($type eq 'captcha') {
 6791:                         if ($cancreate{$type} eq 'notused') {
 6792:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 6793:                         } else {
 6794:                             my %captchas = &captcha_phrases();
 6795:                             if ($captchas{$cancreate{$type}}) {
 6796:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 6797:                             } else {
 6798:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 6799:                             }
 6800:                         }
 6801:                     } elsif ($type eq 'recaptchakeys') {
 6802:                         my ($privkey,$pubkey);
 6803:                         if (ref($cancreate{$type}) eq 'HASH') {
 6804:                             $pubkey = $cancreate{$type}{'public'};
 6805:                             $privkey = $cancreate{$type}{'private'};
 6806:                         }
 6807:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 6808:                         if (!$pubkey) {
 6809:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 6810:                         } else {
 6811:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6812:                         }
 6813:                         if (!$privkey) {
 6814:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 6815:                         } else {
 6816:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6817:                         }
 6818:                         $chgtext .= '</ul>';
 6819:                     } else {
 6820:                         if ($cancreate{$type} eq 'none') {
 6821:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 6822:                         } elsif ($cancreate{$type} eq 'any') {
 6823:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 6824:                         } elsif ($cancreate{$type} eq 'official') {
 6825:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 6826:                         } elsif ($cancreate{$type} eq 'unofficial') {
 6827:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 6828:                         }
 6829:                     }
 6830:                     $resulttext .= '<li>'.$chgtext.'</li>';
 6831:                 }
 6832:             }
 6833:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 6834:                 my ($rules,$ruleorder) = 
 6835:                     &Apache::lonnet::inst_userrules($dom,'username');
 6836:                 my $chgtext = '<ul>';
 6837:                 foreach my $type (@username_rule) {
 6838:                     if (ref($rules->{$type}) eq 'HASH') {
 6839:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 6840:                     }
 6841:                 }
 6842:                 $chgtext .= '</ul>';
 6843:                 if (@username_rule > 0) {
 6844:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 6845:                 } else {
 6846:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 6847:                 }
 6848:             }
 6849:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 6850:                 my ($idrules,$idruleorder) = 
 6851:                     &Apache::lonnet::inst_userrules($dom,'id');
 6852:                 my $chgtext = '<ul>';
 6853:                 foreach my $type (@id_rule) {
 6854:                     if (ref($idrules->{$type}) eq 'HASH') {
 6855:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 6856:                     }
 6857:                 }
 6858:                 $chgtext .= '</ul>';
 6859:                 if (@id_rule > 0) {
 6860:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 6861:                 } else {
 6862:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 6863:                 }
 6864:             }
 6865:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 6866:                 my ($emailrules,$emailruleorder) =
 6867:                     &Apache::lonnet::inst_userrules($dom,'email');
 6868:                 my $chgtext = '<ul>';
 6869:                 foreach my $type (@email_rule) {
 6870:                     if (ref($emailrules->{$type}) eq 'HASH') {
 6871:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 6872:                     }
 6873:                 }
 6874:                 $chgtext .= '</ul>';
 6875:                 if (@email_rule > 0) {
 6876:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 6877:                 } else {
 6878:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 6879:                 }
 6880:             }
 6881: 
 6882:             my %authname = &authtype_names();
 6883:             my %context_title = &context_names();
 6884:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 6885:                 my $chgtext = '<ul>';
 6886:                 foreach my $type (@{$changes{'authtypes'}}) {
 6887:                     my @allowed;
 6888:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 6889:                     foreach my $auth (@authtypes) {
 6890:                         if ($authhash{$type}{$auth}) {
 6891:                             push(@allowed,$authname{$auth});
 6892:                         }
 6893:                     }
 6894:                     if (@allowed > 0) {
 6895:                         $chgtext .= join(', ',@allowed).'</li>';
 6896:                     } else {
 6897:                         $chgtext .= &mt('none').'</li>';
 6898:                     }
 6899:                 }
 6900:                 $chgtext .= '</ul>';
 6901:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 6902:                 $resulttext .= '</li>';
 6903:             }
 6904:             $resulttext .= '</ul>';
 6905:         } else {
 6906:             $resulttext = &mt('No changes made to user creation settings');
 6907:         }
 6908:     } else {
 6909:         $resulttext = '<span class="LC_error">'.
 6910:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6911:     }
 6912:     if ($warningmsg ne '') {
 6913:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 6914:     }
 6915:     return $resulttext;
 6916: }
 6917: 
 6918: sub process_captcha {
 6919:     my ($container,$changes,$newsettings,$current) = @_;
 6920:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 6921:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 6922:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 6923:         $newsettings->{'captcha'} = 'original';
 6924:     }
 6925:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 6926:         if ($container eq 'cancreate') {
 6927:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 6928:                 push(@{$changes->{'cancreate'}},'captcha');
 6929:             } elsif (!defined($changes->{'cancreate'})) {
 6930:                 $changes->{'cancreate'} = ['captcha'];
 6931:             }
 6932:         } else {
 6933:             $changes->{'captcha'} = 1;
 6934:         }
 6935:     }
 6936:     my ($newpub,$newpriv,$currpub,$currpriv);
 6937:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 6938:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 6939:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 6940:         $newpub =~ s/\W//g;
 6941:         $newpriv =~ s/\W//g;
 6942:         $newsettings->{'recaptchakeys'} = {
 6943:                                              public  => $newpub,
 6944:                                              private => $newpriv,
 6945:                                           };
 6946:     }
 6947:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 6948:         $currpub = $current->{'recaptchakeys'}{'public'};
 6949:         $currpriv = $current->{'recaptchakeys'}{'private'};
 6950:         $newsettings->{'recaptchakeys'} = {
 6951:                                              public  => '',
 6952:                                              private => '',
 6953:                                           }
 6954:     }
 6955:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 6956:         if ($container eq 'cancreate') {
 6957:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 6958:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 6959:             } elsif (!defined($changes->{'cancreate'})) {
 6960:                 $changes->{'cancreate'} = ['recaptchakeys'];
 6961:             }
 6962:         } else {
 6963:             $changes->{'recaptchakeys'} = 1;
 6964:         }
 6965:     }
 6966:     return;
 6967: }
 6968: 
 6969: sub modify_usermodification {
 6970:     my ($dom,%domconfig) = @_;
 6971:     my ($resulttext,%curr_usermodification,%changes);
 6972:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 6973:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 6974:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 6975:         }
 6976:     }
 6977:     my @contexts = ('author','course','selfcreate');
 6978:     my %context_title = (
 6979:                            author => 'In author context',
 6980:                            course => 'In course context',
 6981:                            selfcreate => 'When self creating account', 
 6982:                         );
 6983:     my @fields = ('lastname','firstname','middlename','generation',
 6984:                   'permanentemail','id');
 6985:     my %roles = (
 6986:                   author => ['ca','aa'],
 6987:                   course => ['st','ep','ta','in','cr'],
 6988:                 );
 6989:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6990:     if (ref($types) eq 'ARRAY') {
 6991:         push(@{$types},'default');
 6992:         $usertypes->{'default'} = $othertitle;
 6993:     }
 6994:     $roles{'selfcreate'} = $types;  
 6995:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6996:     my %modifyhash;
 6997:     foreach my $context (@contexts) {
 6998:         foreach my $role (@{$roles{$context}}) {
 6999:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7000:             foreach my $item (@fields) {
 7001:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7002:                     $modifyhash{$context}{$role}{$item} = 1;
 7003:                 } else {
 7004:                     $modifyhash{$context}{$role}{$item} = 0;
 7005:                 }
 7006:             }
 7007:         }
 7008:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7009:             foreach my $role (@{$roles{$context}}) {
 7010:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7011:                     foreach my $field (@fields) {
 7012:                         if ($modifyhash{$context}{$role}{$field} ne 
 7013:                                 $curr_usermodification{$context}{$role}{$field}) {
 7014:                             push(@{$changes{$context}},$role);
 7015:                             last;
 7016:                         }
 7017:                     }
 7018:                 }
 7019:             }
 7020:         } else {
 7021:             foreach my $context (@contexts) {
 7022:                 foreach my $role (@{$roles{$context}}) {
 7023:                     push(@{$changes{$context}},$role);
 7024:                 }
 7025:             }
 7026:         }
 7027:     }
 7028:     my %usermodification_hash =  (
 7029:                                    usermodification => \%modifyhash,
 7030:                                  );
 7031:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7032:                                              \%usermodification_hash,$dom);
 7033:     if ($putresult eq 'ok') {
 7034:         if (keys(%changes) > 0) {
 7035:             $resulttext = &mt('Changes made: ').'<ul>';
 7036:             foreach my $context (@contexts) {
 7037:                 if (ref($changes{$context}) eq 'ARRAY') {
 7038:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7039:                     if (ref($changes{$context}) eq 'ARRAY') {
 7040:                         foreach my $role (@{$changes{$context}}) {
 7041:                             my $rolename;
 7042:                             if ($context eq 'selfcreate') {
 7043:                                 $rolename = $role;
 7044:                                 if (ref($usertypes) eq 'HASH') {
 7045:                                     if ($usertypes->{$role} ne '') {
 7046:                                         $rolename = $usertypes->{$role};
 7047:                                     }
 7048:                                 }
 7049:                             } else {
 7050:                                 if ($role eq 'cr') {
 7051:                                     $rolename = &mt('Custom');
 7052:                                 } else {
 7053:                                     $rolename = &Apache::lonnet::plaintext($role);
 7054:                                 }
 7055:                             }
 7056:                             my @modifiable;
 7057:                             if ($context eq 'selfcreate') {
 7058:                                 $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): ');
 7059:                             } else {
 7060:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7061:                             }
 7062:                             foreach my $field (@fields) {
 7063:                                 if ($modifyhash{$context}{$role}{$field}) {
 7064:                                     push(@modifiable,$fieldtitles{$field});
 7065:                                 }
 7066:                             }
 7067:                             if (@modifiable > 0) {
 7068:                                 $resulttext .= join(', ',@modifiable);
 7069:                             } else {
 7070:                                 $resulttext .= &mt('none'); 
 7071:                             }
 7072:                             $resulttext .= '</li>';
 7073:                         }
 7074:                         $resulttext .= '</ul></li>';
 7075:                     }
 7076:                 }
 7077:             }
 7078:             $resulttext .= '</ul>';
 7079:         } else {
 7080:             $resulttext = &mt('No changes made to user modification settings');
 7081:         }
 7082:     } else {
 7083:         $resulttext = '<span class="LC_error">'.
 7084:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7085:     }
 7086:     return $resulttext;
 7087: }
 7088: 
 7089: sub modify_defaults {
 7090:     my ($dom,$r) = @_;
 7091:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7092:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7093:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7094:     my @authtypes = ('internal','krb4','krb5','localauth');
 7095:     foreach my $item (@items) {
 7096:         $newvalues{$item} = $env{'form.'.$item};
 7097:         if ($item eq 'auth_def') {
 7098:             if ($newvalues{$item} ne '') {
 7099:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7100:                     push(@errors,$item);
 7101:                 }
 7102:             }
 7103:         } elsif ($item eq 'lang_def') {
 7104:             if ($newvalues{$item} ne '') {
 7105:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7106:                     my $langcode = $1;
 7107:                     if ($langcode ne 'x_chef') {
 7108:                         if (code2language($langcode) eq '') {
 7109:                             push(@errors,$item);
 7110:                         }
 7111:                     }
 7112:                 } else {
 7113:                     push(@errors,$item);
 7114:                 }
 7115:             }
 7116:         } elsif ($item eq 'timezone_def') {
 7117:             if ($newvalues{$item} ne '') {
 7118:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7119:                     push(@errors,$item);   
 7120:                 }
 7121:             }
 7122:         } elsif ($item eq 'datelocale_def') {
 7123:             if ($newvalues{$item} ne '') {
 7124:                 my @datelocale_ids = DateTime::Locale->ids();
 7125:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7126:                     push(@errors,$item);
 7127:                 }
 7128:             }
 7129:         } elsif ($item eq 'portal_def') {
 7130:             if ($newvalues{$item} ne '') {
 7131:                 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])\/?$/) {
 7132:                     push(@errors,$item);
 7133:                 }
 7134:             }
 7135:         }
 7136:         if (grep(/^\Q$item\E$/,@errors)) {
 7137:             $newvalues{$item} = $domdefaults{$item};
 7138:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7139:             $changes{$item} = 1;
 7140:         }
 7141:         $domdefaults{$item} = $newvalues{$item};
 7142:     }
 7143:     my %defaults_hash = (
 7144:                          defaults => \%newvalues,
 7145:                         );
 7146:     my $title = &defaults_titles();
 7147:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7148:                                              $dom);
 7149:     if ($putresult eq 'ok') {
 7150:         if (keys(%changes) > 0) {
 7151:             $resulttext = &mt('Changes made:').'<ul>';
 7152:             my $version = $r->dir_config('lonVersion');
 7153:             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";
 7154:             foreach my $item (sort(keys(%changes))) {
 7155:                 my $value = $env{'form.'.$item};
 7156:                 if ($value eq '') {
 7157:                     $value = &mt('none');
 7158:                 } elsif ($item eq 'auth_def') {
 7159:                     my %authnames = &authtype_names();
 7160:                     my %shortauth = (
 7161:                              internal => 'int',
 7162:                              krb4 => 'krb4',
 7163:                              krb5 => 'krb5',
 7164:                              localauth  => 'loc',
 7165:                     );
 7166:                     $value = $authnames{$shortauth{$value}};
 7167:                 }
 7168:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7169:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7170:             }
 7171:             $resulttext .= '</ul>';
 7172:             $mailmsgtext .= "\n";
 7173:             my $cachetime = 24*60*60;
 7174:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7175:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7176:                 my $sysmail = $r->dir_config('lonSysEMail');
 7177:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 7178:             }
 7179:         } else {
 7180:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 7181:         }
 7182:     } else {
 7183:         $resulttext = '<span class="LC_error">'.
 7184:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7185:     }
 7186:     if (@errors > 0) {
 7187:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7188:         foreach my $item (@errors) {
 7189:             $resulttext .= ' "'.$title->{$item}.'",';
 7190:         }
 7191:         $resulttext =~ s/,$//;
 7192:     }
 7193:     return $resulttext;
 7194: }
 7195: 
 7196: sub modify_scantron {
 7197:     my ($r,$dom,$confname,%domconfig) = @_;
 7198:     my ($resulttext,%confhash,%changes,$errors);
 7199:     my $custom = 'custom.tab';
 7200:     my $default = 'default.tab';
 7201:     my $servadm = $r->dir_config('lonAdmEMail');
 7202:     my ($configuserok,$author_ok,$switchserver) = 
 7203:         &config_check($dom,$confname,$servadm);
 7204:     if ($env{'form.scantronformat.filename'} ne '') {
 7205:         my $error;
 7206:         if ($configuserok eq 'ok') {
 7207:             if ($switchserver) {
 7208:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7209:             } else {
 7210:                 if ($author_ok eq 'ok') {
 7211:                     my ($result,$scantronurl) =
 7212:                         &publishlogo($r,'upload','scantronformat',$dom,
 7213:                                      $confname,'scantron','','',$custom);
 7214:                     if ($result eq 'ok') {
 7215:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7216:                         $changes{'scantronformat'} = 1;
 7217:                     } else {
 7218:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$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].",$custom,$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].",$custom,$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:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7233:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7234:             if ($env{'form.scantronformat_del'}) {
 7235:                 $confhash{'scantron'}{'scantronformat'} = '';
 7236:                 $changes{'scantronformat'} = 1;
 7237:             }
 7238:         }
 7239:     }
 7240:     if (keys(%confhash) > 0) {
 7241:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7242:                                                  $dom);
 7243:         if ($putresult eq 'ok') {
 7244:             if (keys(%changes) > 0) {
 7245:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7246:                     $resulttext = &mt('Changes made:').'<ul>';
 7247:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7248:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7249:                     } else {
 7250:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7251:                     }
 7252:                     $resulttext .= '</ul>';
 7253:                 } else {
 7254:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7255:                 }
 7256:                 $resulttext .= '</ul>';
 7257:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7258:             } else {
 7259:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7260:             }
 7261:         } else {
 7262:             $resulttext = '<span class="LC_error">'.
 7263:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7264:         }
 7265:     } else {
 7266:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7267:     }
 7268:     if ($errors) {
 7269:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7270:                        $errors.'</ul>';
 7271:     }
 7272:     return $resulttext;
 7273: }
 7274: 
 7275: sub modify_coursecategories {
 7276:     my ($dom,%domconfig) = @_;
 7277:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7278:         $cathash);
 7279:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7280:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7281:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7282:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7283:             $changes{'togglecats'} = 1;
 7284:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7285:         }
 7286:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7287:             $changes{'categorize'} = 1;
 7288:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7289:         }
 7290:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7291:             $changes{'togglecatscomm'} = 1;
 7292:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7293:         }
 7294:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7295:             $changes{'categorizecomm'} = 1;
 7296:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7297:         }
 7298:     } else {
 7299:         $changes{'togglecats'} = 1;
 7300:         $changes{'categorize'} = 1;
 7301:         $changes{'togglecatscomm'} = 1;
 7302:         $changes{'categorizecomm'} = 1;
 7303:         $domconfig{'coursecategories'} = {
 7304:                                              togglecats => $env{'form.togglecats'},
 7305:                                              categorize => $env{'form.categorize'},
 7306:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7307:                                              categorizecomm => $env{'form.categorizecomm'},
 7308:                                          };
 7309:     }
 7310:     if (ref($cathash) eq 'HASH') {
 7311:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7312:             push (@deletecategory,'instcode::0');
 7313:         }
 7314:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7315:             push(@deletecategory,'communities::0');
 7316:         }
 7317:     }
 7318:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7319:     if (ref($cathash) eq 'HASH') {
 7320:         if (@deletecategory > 0) {
 7321:             #FIXME Need to remove category from all courses using a deleted category 
 7322:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7323:             foreach my $item (@deletecategory) {
 7324:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7325:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7326:                     $deletions{$item} = 1;
 7327:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7328:                 }
 7329:             }
 7330:         }
 7331:         foreach my $item (keys(%{$cathash})) {
 7332:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7333:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7334:                 $reorderings{$item} = 1;
 7335:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7336:             }
 7337:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7338:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7339:                 my $newdepth = $depth+1;
 7340:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7341:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7342:                 $adds{$newitem} = 1; 
 7343:             }
 7344:             if ($env{'form.subcat_'.$item} ne '') {
 7345:                 my $newcat = $env{'form.subcat_'.$item};
 7346:                 my $newdepth = $depth+1;
 7347:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7348:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7349:                 $adds{$newitem} = 1;
 7350:             }
 7351:         }
 7352:     }
 7353:     if ($env{'form.instcode'} eq '1') {
 7354:         if (ref($cathash) eq 'HASH') {
 7355:             my $newitem = 'instcode::0';
 7356:             if ($cathash->{$newitem} eq '') {  
 7357:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7358:                 $adds{$newitem} = 1;
 7359:             }
 7360:         } else {
 7361:             my $newitem = 'instcode::0';
 7362:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7363:             $adds{$newitem} = 1;
 7364:         }
 7365:     }
 7366:     if ($env{'form.communities'} eq '1') {
 7367:         if (ref($cathash) eq 'HASH') {
 7368:             my $newitem = 'communities::0';
 7369:             if ($cathash->{$newitem} eq '') {
 7370:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7371:                 $adds{$newitem} = 1;
 7372:             }
 7373:         } else {
 7374:             my $newitem = 'communities::0';
 7375:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7376:             $adds{$newitem} = 1;
 7377:         }
 7378:     }
 7379:     if ($env{'form.addcategory_name'} ne '') {
 7380:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7381:             ($env{'form.addcategory_name'} ne 'communities')) {
 7382:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7383:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7384:             $adds{$newitem} = 1;
 7385:         }
 7386:     }
 7387:     my $putresult;
 7388:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7389:         if (keys(%deletions) > 0) {
 7390:             foreach my $key (keys(%deletions)) {
 7391:                 if ($predelallitems{$key} ne '') {
 7392:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7393:                 }
 7394:             }
 7395:         }
 7396:         my (@chkcats,@chktrails,%chkallitems);
 7397:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7398:         if (ref($chkcats[0]) eq 'ARRAY') {
 7399:             my $depth = 0;
 7400:             my $chg = 0;
 7401:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7402:                 my $name = $chkcats[0][$i];
 7403:                 my $item;
 7404:                 if ($name eq '') {
 7405:                     $chg ++;
 7406:                 } else {
 7407:                     $item = &escape($name).'::0';
 7408:                     if ($chg) {
 7409:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7410:                     }
 7411:                     $depth ++; 
 7412:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7413:                     $depth --;
 7414:                 }
 7415:             }
 7416:         }
 7417:     }
 7418:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7419:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7420:         if ($putresult eq 'ok') {
 7421:             my %title = (
 7422:                          togglecats     => 'Show/Hide a course in catalog',
 7423:                          categorize     => 'Assign a category to a course',
 7424:                          togglecatscomm => 'Show/Hide a community in catalog',
 7425:                          categorizecomm => 'Assign a category to a community',
 7426:                         );
 7427:             my %level = (
 7428:                          dom  => 'set in Domain ("Modify Course/Community")',
 7429:                          crs  => 'set in Course ("Course Configuration")',
 7430:                          comm => 'set in Community ("Community Configuration")',
 7431:                         );
 7432:             $resulttext = &mt('Changes made:').'<ul>';
 7433:             if ($changes{'togglecats'}) {
 7434:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7435:             }
 7436:             if ($changes{'categorize'}) {
 7437:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7438:             }
 7439:             if ($changes{'togglecatscomm'}) {
 7440:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7441:             }
 7442:             if ($changes{'categorizecomm'}) {
 7443:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7444:             }
 7445:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7446:                 my $cathash;
 7447:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7448:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7449:                 } else {
 7450:                     $cathash = {};
 7451:                 } 
 7452:                 my (@cats,@trails,%allitems);
 7453:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7454:                 if (keys(%deletions) > 0) {
 7455:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7456:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7457:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7458:                     }
 7459:                     $resulttext .= '</ul></li>';
 7460:                 }
 7461:                 if (keys(%reorderings) > 0) {
 7462:                     my %sort_by_trail;
 7463:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7464:                     foreach my $key (keys(%reorderings)) {
 7465:                         if ($allitems{$key} ne '') {
 7466:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7467:                         }
 7468:                     }
 7469:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7470:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7471:                     }
 7472:                     $resulttext .= '</ul></li>';
 7473:                 }
 7474:                 if (keys(%adds) > 0) {
 7475:                     my %sort_by_trail;
 7476:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7477:                     foreach my $key (keys(%adds)) {
 7478:                         if ($allitems{$key} ne '') {
 7479:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7480:                         }
 7481:                     }
 7482:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7483:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7484:                     }
 7485:                     $resulttext .= '</ul></li>';
 7486:                 }
 7487:             }
 7488:             $resulttext .= '</ul>';
 7489:         } else {
 7490:             $resulttext = '<span class="LC_error">'.
 7491:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7492:         }
 7493:     } else {
 7494:         $resulttext = &mt('No changes made to course and community categories');
 7495:     }
 7496:     return $resulttext;
 7497: }
 7498: 
 7499: sub modify_serverstatuses {
 7500:     my ($dom,%domconfig) = @_;
 7501:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7502:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7503:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7504:     }
 7505:     my @pages = &serverstatus_pages();
 7506:     foreach my $type (@pages) {
 7507:         $newserverstatus{$type}{'namedusers'} = '';
 7508:         $newserverstatus{$type}{'machines'} = '';
 7509:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7510:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7511:             my @okusers;
 7512:             foreach my $user (@users) {
 7513:                 my ($uname,$udom) = split(/:/,$user);
 7514:                 if (($udom =~ /^$match_domain$/) &&   
 7515:                     (&Apache::lonnet::domain($udom)) &&
 7516:                     ($uname =~ /^$match_username$/)) {
 7517:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7518:                         push(@okusers,$user);
 7519:                     }
 7520:                 }
 7521:             }
 7522:             if (@okusers > 0) {
 7523:                  @okusers = sort(@okusers);
 7524:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7525:             }
 7526:         }
 7527:         if (defined($env{'form.'.$type.'_machines'})) {
 7528:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7529:             my @okmachines;
 7530:             foreach my $ip (@machines) {
 7531:                 my @parts = split(/\./,$ip);
 7532:                 next if (@parts < 4);
 7533:                 my $badip = 0;
 7534:                 for (my $i=0; $i<4; $i++) {
 7535:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7536:                         $badip = 1;
 7537:                         last;
 7538:                     }
 7539:                 }
 7540:                 if (!$badip) {
 7541:                     push(@okmachines,$ip);     
 7542:                 }
 7543:             }
 7544:             @okmachines = sort(@okmachines);
 7545:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7546:         }
 7547:     }
 7548:     my %serverstatushash =  (
 7549:                                 serverstatuses => \%newserverstatus,
 7550:                             );
 7551:     foreach my $type (@pages) {
 7552:         foreach my $setting ('namedusers','machines') {
 7553:             my (@current,@new);
 7554:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7555:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7556:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7557:                 }
 7558:             }
 7559:             if ($newserverstatus{$type}{$setting} ne '') {
 7560:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7561:             }
 7562:             if (@current > 0) {
 7563:                 if (@new > 0) {
 7564:                     foreach my $item (@current) {
 7565:                         if (!grep(/^\Q$item\E$/,@new)) {
 7566:                             $changes{$type}{$setting} = 1;
 7567:                             last;
 7568:                         }
 7569:                     }
 7570:                     foreach my $item (@new) {
 7571:                         if (!grep(/^\Q$item\E$/,@current)) {
 7572:                             $changes{$type}{$setting} = 1;
 7573:                             last;
 7574:                         }
 7575:                     }
 7576:                 } else {
 7577:                     $changes{$type}{$setting} = 1;
 7578:                 }
 7579:             } elsif (@new > 0) {
 7580:                 $changes{$type}{$setting} = 1;
 7581:             }
 7582:         }
 7583:     }
 7584:     if (keys(%changes) > 0) {
 7585:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7586:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7587:                                                  \%serverstatushash,$dom);
 7588:         if ($putresult eq 'ok') {
 7589:             $resulttext .= &mt('Changes made:').'<ul>';
 7590:             foreach my $type (@pages) {
 7591:                 if (ref($changes{$type}) eq 'HASH') {
 7592:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7593:                     if ($changes{$type}{'namedusers'}) {
 7594:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7595:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7596:                         } else {
 7597:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7598:                         }
 7599:                     }
 7600:                     if ($changes{$type}{'machines'}) {
 7601:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7602:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7603:                         } else {
 7604:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7605:                         }
 7606: 
 7607:                     }
 7608:                     $resulttext .= '</ul></li>';
 7609:                 }
 7610:             }
 7611:             $resulttext .= '</ul>';
 7612:         } else {
 7613:             $resulttext = '<span class="LC_error">'.
 7614:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7615: 
 7616:         }
 7617:     } else {
 7618:         $resulttext = &mt('No changes made to access to server status pages');
 7619:     }
 7620:     return $resulttext;
 7621: }
 7622: 
 7623: sub modify_helpsettings {
 7624:     my ($r,$dom,$confname,%domconfig) = @_;
 7625:     my ($resulttext,$errors,%changes,%helphash);
 7626:     my %defaultchecked = ('submitbugs' => 'on');
 7627:     my @offon = ('off','on');
 7628:     my @toggles = ('submitbugs');
 7629:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7630:         foreach my $item (@toggles) {
 7631:             if ($defaultchecked{$item} eq 'on') { 
 7632:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7633:                     if ($env{'form.'.$item} eq '0') {
 7634:                         $changes{$item} = 1;
 7635:                     }
 7636:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7637:                     $changes{$item} = 1;
 7638:                 }
 7639:             } elsif ($defaultchecked{$item} eq 'off') {
 7640:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7641:                     if ($env{'form.'.$item} eq '1') {
 7642:                         $changes{$item} = 1;
 7643:                     }
 7644:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7645:                     $changes{$item} = 1;
 7646:                 }
 7647: 	    }
 7648:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 7649:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7650:             }
 7651:         }
 7652:     }
 7653:     my $putresult;
 7654:     if (keys(%changes) > 0) {
 7655:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7656:         if ($putresult eq 'ok') {
 7657:             $resulttext = &mt('Changes made:').'<ul>';
 7658:             foreach my $item (sort(keys(%changes))) {
 7659:                 if ($item eq 'submitbugs') {
 7660:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 7661:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 7662:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 7663:                 }
 7664:             }
 7665:             $resulttext .= '</ul>';
 7666:         } else {
 7667:             $resulttext = &mt('No changes made to help settings');
 7668:             $errors .= '<li><span class="LC_error">'.
 7669:                        &mt('An error occurred storing the settings: [_1]',
 7670:                            $putresult).'</span></li>';
 7671:         }
 7672:     }
 7673:     if ($errors) {
 7674:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7675:                        $errors.'</ul>';
 7676:     }
 7677:     return $resulttext;
 7678: }
 7679: 
 7680: sub modify_coursedefaults {
 7681:     my ($dom,%domconfig) = @_;
 7682:     my ($resulttext,$errors,%changes,%defaultshash);
 7683:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7684:     my @offon = ('off','on');
 7685:     my @toggles = ('canuse_pdfforms');
 7686: 
 7687:     $defaultshash{'coursedefaults'} = {};
 7688: 
 7689:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7690:         if ($domconfig{'coursedefaults'} eq '') {
 7691:             $domconfig{'coursedefaults'} = {};
 7692:         }
 7693:     }
 7694: 
 7695:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7696:         foreach my $item (@toggles) {
 7697:             if ($defaultchecked{$item} eq 'on') {
 7698:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7699:                     ($env{'form.'.$item} eq '0')) {
 7700:                     $changes{$item} = 1;
 7701:                 } elsif ($domconfig{'coursdefaults'}{$item} ne $env{'form.'.$item}) {
 7702:                     $changes{$item} = 1;
 7703:                 }
 7704:             } elsif ($defaultchecked{$item} eq 'off') {
 7705:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7706:                     ($env{'form.'.$item} eq '1')) {
 7707:                     $changes{$item} = 1;
 7708:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7709:                     $changes{$item} = 1;
 7710:                 }
 7711:             }
 7712:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7713:         }
 7714:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7715:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7716:         $newdefresponder =~ s/\D//g;
 7717:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7718:             $newdefresponder = 1;
 7719:         }
 7720:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7721:         if ($currdefresponder ne $newdefresponder) {
 7722:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7723:                 $changes{'anonsurvey_threshold'} = 1;
 7724:             }
 7725:         }
 7726:     }
 7727:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7728:                                              $dom);
 7729:     if ($putresult eq 'ok') {
 7730:         if (keys(%changes) > 0) {
 7731:             if ($changes{'canuse_pdfforms'}) {
 7732:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7733:                 $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7734:                 my $cachetime = 24*60*60;
 7735:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7736:             }
 7737:             $resulttext = &mt('Changes made:').'<ul>';
 7738:             foreach my $item (sort(keys(%changes))) {
 7739:                 if ($item eq 'canuse_pdfforms') {
 7740:                     if ($env{'form.'.$item} eq '1') {
 7741:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7742:                     } else {
 7743:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7744:                     }
 7745:                 } elsif ($item eq 'anonsurvey_threshold') {
 7746:                         $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7747:                 }
 7748:             }
 7749:             $resulttext .= '</ul>';
 7750:         } else {
 7751:             $resulttext = &mt('No changes made to course defaults');
 7752:         }
 7753:     } else {
 7754:         $resulttext = '<span class="LC_error">'.
 7755:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7756:     }
 7757:     return $resulttext;
 7758: }
 7759: 
 7760: sub modify_usersessions {
 7761:     my ($dom,%domconfig) = @_;
 7762:     my @hostingtypes = ('version','excludedomain','includedomain');
 7763:     my @offloadtypes = ('primary','default');
 7764:     my %types = (
 7765:                   remote => \@hostingtypes,
 7766:                   hosted => \@hostingtypes,
 7767:                   spares => \@offloadtypes,
 7768:                 );
 7769:     my @prefixes = ('remote','hosted','spares');
 7770:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7771:     my (%by_ip,%by_location,@intdoms);
 7772:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7773:     my @locations = sort(keys(%by_location));
 7774:     my (%defaultshash,%changes);
 7775:     foreach my $prefix (@prefixes) {
 7776:         $defaultshash{'usersessions'}{$prefix} = {};
 7777:     }
 7778:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7779:     my $resulttext;
 7780:     my %iphost = &Apache::lonnet::get_iphost();
 7781:     foreach my $prefix (@prefixes) {
 7782:         next if ($prefix eq 'spares');
 7783:         foreach my $type (@{$types{$prefix}}) {
 7784:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7785:             if ($type eq 'version') {
 7786:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7787:                 my $okvalue;
 7788:                 if ($value ne '') {
 7789:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 7790:                         $okvalue = $value;
 7791:                     }
 7792:                 }
 7793:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7794:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7795:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 7796:                             if ($inuse == 0) {
 7797:                                 $changes{$prefix}{$type} = 1;
 7798:                             } else {
 7799:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 7800:                                     $changes{$prefix}{$type} = 1;
 7801:                                 }
 7802:                                 if ($okvalue ne '') {
 7803:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7804:                                 } 
 7805:                             }
 7806:                         } else {
 7807:                             if (($inuse == 1) && ($okvalue ne '')) {
 7808:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7809:                                 $changes{$prefix}{$type} = 1;
 7810:                             }
 7811:                         }
 7812:                     } else {
 7813:                         if (($inuse == 1) && ($okvalue ne '')) {
 7814:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7815:                             $changes{$prefix}{$type} = 1;
 7816:                         }
 7817:                     }
 7818:                 } else {
 7819:                     if (($inuse == 1) && ($okvalue ne '')) {
 7820:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7821:                         $changes{$prefix}{$type} = 1;
 7822:                     }
 7823:                 }
 7824:             } else {
 7825:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 7826:                 my @okvals;
 7827:                 foreach my $val (@vals) {
 7828:                     if ($val =~ /:/) {
 7829:                         my @items = split(/:/,$val);
 7830:                         foreach my $item (@items) {
 7831:                             if (ref($by_location{$item}) eq 'ARRAY') {
 7832:                                 push(@okvals,$item);
 7833:                             }
 7834:                         }
 7835:                     } else {
 7836:                         if (ref($by_location{$val}) eq 'ARRAY') {
 7837:                             push(@okvals,$val);
 7838:                         }
 7839:                     }
 7840:                 }
 7841:                 @okvals = sort(@okvals);
 7842:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7843:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7844:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7845:                             if ($inuse == 0) {
 7846:                                 $changes{$prefix}{$type} = 1; 
 7847:                             } else {
 7848:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7849:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 7850:                                 if (@changed > 0) {
 7851:                                     $changes{$prefix}{$type} = 1;
 7852:                                 }
 7853:                             }
 7854:                         } else {
 7855:                             if ($inuse == 1) {
 7856:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7857:                                 $changes{$prefix}{$type} = 1;
 7858:                             }
 7859:                         } 
 7860:                     } else {
 7861:                         if ($inuse == 1) {
 7862:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7863:                             $changes{$prefix}{$type} = 1;
 7864:                         }
 7865:                     }
 7866:                 } else {
 7867:                     if ($inuse == 1) {
 7868:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7869:                         $changes{$prefix}{$type} = 1;
 7870:                     }
 7871:                 }
 7872:             }
 7873:         }
 7874:     }
 7875: 
 7876:     my @alldoms = &Apache::lonnet::all_domains();
 7877:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7878:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 7879:     my $savespares;
 7880: 
 7881:     foreach my $lonhost (sort(keys(%servers))) {
 7882:         my $serverhomeID =
 7883:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 7884:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 7885:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 7886:         my %spareschg;
 7887:         foreach my $type (@{$types{'spares'}}) {
 7888:             my @okspares;
 7889:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 7890:             foreach my $server (@checked) {
 7891:                 if (&Apache::lonnet::hostname($server) ne '') {
 7892:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 7893:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 7894:                             push(@okspares,$server);
 7895:                         }
 7896:                     }
 7897:                 }
 7898:             }
 7899:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 7900:             my $newspare;
 7901:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 7902:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 7903:                     $newspare = $new;
 7904:                 }
 7905:             }
 7906:             my @spares;
 7907:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 7908:                 @spares = sort(@okspares,$newspare);
 7909:             } else {
 7910:                 @spares = sort(@okspares);
 7911:             }
 7912:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 7913:             if (ref($spareid{$lonhost}) eq 'HASH') {
 7914:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 7915:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 7916:                     if (@diffs > 0) {
 7917:                         $spareschg{$type} = 1;
 7918:                     }
 7919:                 }
 7920:             }
 7921:         }
 7922:         if (keys(%spareschg) > 0) {
 7923:             $changes{'spares'}{$lonhost} = \%spareschg;
 7924:         }
 7925:     }
 7926: 
 7927:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7928:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 7929:             if (ref($changes{'spares'}) eq 'HASH') {
 7930:                 if (keys(%{$changes{'spares'}}) > 0) {
 7931:                     $savespares = 1;
 7932:                 }
 7933:             }
 7934:         } else {
 7935:             $savespares = 1;
 7936:         }
 7937:     }
 7938: 
 7939:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 7940:     if ((keys(%changes) > 0) || ($savespares)) {
 7941:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7942:                                                  $dom);
 7943:         if ($putresult eq 'ok') {
 7944:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 7945:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 7946:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 7947:                 }
 7948:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 7949:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 7950:                 }
 7951:             }
 7952:             my $cachetime = 24*60*60;
 7953:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7954:             if (keys(%changes) > 0) {
 7955:                 my %lt = &usersession_titles();
 7956:                 $resulttext = &mt('Changes made:').'<ul>';
 7957:                 foreach my $prefix (@prefixes) {
 7958:                     if (ref($changes{$prefix}) eq 'HASH') {
 7959:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 7960:                         if ($prefix eq 'spares') {
 7961:                             if (ref($changes{$prefix}) eq 'HASH') {
 7962:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 7963:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 7964:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 7965:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 7966:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 7967:                                         foreach my $type (@{$types{$prefix}}) {
 7968:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 7969:                                                 my $offloadto = &mt('None');
 7970:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 7971:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 7972:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 7973:                                                     }
 7974:                                                 }
 7975:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 7976:                                             }
 7977:                                         }
 7978:                                     }
 7979:                                     $resulttext .= '</li>';
 7980:                                 }
 7981:                             }
 7982:                         } else {
 7983:                             foreach my $type (@{$types{$prefix}}) {
 7984:                                 if (defined($changes{$prefix}{$type})) {
 7985:                                     my $newvalue;
 7986:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 7987:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 7988:                                             if ($type eq 'version') {
 7989:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 7990:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7991:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 7992:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 7993:                                                 }
 7994:                                             }
 7995:                                         }
 7996:                                     }
 7997:                                     if ($newvalue eq '') {
 7998:                                         if ($type eq 'version') {
 7999:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8000:                                         } else {
 8001:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8002:                                         }
 8003:                                     } else {
 8004:                                         if ($type eq 'version') {
 8005:                                             $newvalue .= ' '.&mt('(or later)'); 
 8006:                                         }
 8007:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8008:                                     }
 8009:                                 }
 8010:                             }
 8011:                         }
 8012:                         $resulttext .= '</ul>';
 8013:                     }
 8014:                 }
 8015:                 $resulttext .= '</ul>';
 8016:             } else {
 8017:                 $resulttext = $nochgmsg;
 8018:             }
 8019:         } else {
 8020:             $resulttext = '<span class="LC_error">'.
 8021:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8022:         }
 8023:     } else {
 8024:         $resulttext = $nochgmsg;
 8025:     }
 8026:     return $resulttext;
 8027: }
 8028: 
 8029: sub modify_loadbalancing {
 8030:     my ($dom,%domconfig) = @_;
 8031:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8032:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8033:     my ($othertitle,$usertypes,$types) =
 8034:         &Apache::loncommon::sorted_inst_types($dom);
 8035:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8036:     my @sparestypes = ('primary','default');
 8037:     my %typetitles = &sparestype_titles();
 8038:     my $resulttext;
 8039:     if (keys(%servers) > 1) {
 8040:         my ($currbalancer,$currtargets,$currrules);
 8041:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8042:             $currbalancer = $domconfig{'loadbalancing'}{'lonhost'};
 8043:             $currtargets = $domconfig{'loadbalancing'}{'targets'};
 8044:             $currrules = $domconfig{'loadbalancing'}{'rules'};
 8045:         } else {
 8046:             ($currbalancer,$currtargets) = 
 8047:                 &Apache::lonnet::get_lonbalancer_config(\%servers);
 8048:         }
 8049:         my ($saveloadbalancing,%defaultshash,%changes);
 8050:         my ($alltypes,$othertypes,$titles) =
 8051:             &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8052:         my %ruletitles = &offloadtype_text();
 8053:         my $balancer = $env{'form.loadbalancing_lonhost'};
 8054:         if (!$servers{$balancer}) {
 8055:             undef($balancer);
 8056:         }
 8057:         if ($currbalancer ne $balancer) {
 8058:             $changes{'lonhost'} = 1;
 8059:         }
 8060:         $defaultshash{'loadbalancing'}{'lonhost'} = $balancer;
 8061:         if ($balancer ne '') {
 8062:             unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8063:                 $saveloadbalancing = 1;
 8064:             }
 8065:             foreach my $sparetype (@sparestypes) {
 8066:                 my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$sparetype);
 8067:                 my @offloadto;
 8068:                 foreach my $target (@targets) {
 8069:                     if (($servers{$target}) && ($target ne $balancer)) {
 8070:                         if ($sparetype eq 'default') {
 8071:                             if (ref($defaultshash{'loadbalancing'}{'targets'}{'primary'}) eq 'ARRAY') {
 8072:                                 next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{'targets'}{'primary'}}));
 8073:                             }
 8074:                         }
 8075:                         unless(grep(/^\Q$target\E$/,@offloadto)) {
 8076:                             push(@offloadto,$target);
 8077:                         }
 8078:                     }
 8079:                     $defaultshash{'loadbalancing'}{'targets'}{$sparetype} = \@offloadto;
 8080:                 }
 8081:             }
 8082:         } else {
 8083:             foreach my $sparetype (@sparestypes) {
 8084:                 $defaultshash{'loadbalancing'}{'targets'}{$sparetype} = [];
 8085:             }
 8086:         }
 8087:         if (ref($currtargets) eq 'HASH') {
 8088:             foreach my $sparetype (@sparestypes) {
 8089:                 if (ref($currtargets->{$sparetype}) eq 'ARRAY') {
 8090:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets->{$sparetype},$defaultshash{'loadbalancing'}{'targets'}{$sparetype});
 8091:                     if (@targetdiffs > 0) {
 8092:                         $changes{'targets'} = 1;
 8093:                     }
 8094:                 } elsif (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 8095:                     if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 8096:                         $changes{'targets'} = 1;
 8097:                     }
 8098:                 }
 8099:             }
 8100:         } else {
 8101:             foreach my $sparetype (@sparestypes) {
 8102:                 if (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 8103:                     if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 8104:                         $changes{'targets'} = 1;  
 8105:                     }
 8106:                 }
 8107:             }  
 8108:         }
 8109:         my $ishomedom;
 8110:         if ($balancer ne '') {
 8111:             if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 8112:                 $ishomedom = 1;
 8113:             }
 8114:         }
 8115:         if (ref($alltypes) eq 'ARRAY') {
 8116:             foreach my $type (@{$alltypes}) {
 8117:                 my $rule;
 8118:                 if ($balancer ne '') {
 8119:                     unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) && 
 8120:                          (!$ishomedom)) {
 8121:                         $rule = $env{'form.loadbalancing_rules_'.$type};
 8122:                     }
 8123:                     if ($rule eq 'specific') {
 8124:                         $rule = $env{'form.loadbalancing_singleserver_'.$type};
 8125:                     }
 8126:                 }
 8127:                 $defaultshash{'loadbalancing'}{'rules'}{$type} = $rule;
 8128:                 if (ref($currrules) eq 'HASH') {
 8129:                     if ($rule ne $currrules->{$type}) {
 8130:                         $changes{'rules'}{$type} = 1;
 8131:                     }
 8132:                 } elsif ($rule ne '') {
 8133:                     $changes{'rules'}{$type} = 1;
 8134:                 }
 8135:             }
 8136:         }
 8137:         my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8138:         if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8139:             my $putresult = &Apache::lonnet::put_dom('configuration',
 8140:                                                      \%defaultshash,$dom);
 8141:             if ($putresult eq 'ok') {
 8142:                 if (keys(%changes) > 0) {
 8143:                     if ($changes{'lonhost'}) {
 8144:                         if ($currbalancer ne '') {
 8145:                             &Apache::lonnet::remote_devalidate_cache($currbalancer,'loadbalancing',$dom);
 8146:                         }
 8147:                         if ($balancer eq '') {
 8148:                             $resulttext .= '<li>'.&mt('Load Balancing with dedicated server discontinued').'</li>'; 
 8149:                         } else {
 8150:                             &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8151:                             $resulttext .= '<li>'.&mt('Dedicated Load Balancer server set to [_1]',$balancer);
 8152:                         }
 8153:                     } else {
 8154:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8155:                     }
 8156:                     if (($changes{'targets'}) && ($balancer ne '')) {
 8157:                         my %offloadstr;
 8158:                         foreach my $sparetype (@sparestypes) {
 8159:                             if (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 8160:                                 if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 8161:                                     $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}});
 8162:                                 }
 8163:                             }
 8164:                         }
 8165:                         if (keys(%offloadstr) == 0) {
 8166:                             $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8167:                         } else {
 8168:                             my $showoffload;
 8169:                             foreach my $sparetype (@sparestypes) {
 8170:                                 $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8171:                                 if (defined($offloadstr{$sparetype})) {
 8172:                                     $showoffload .= $offloadstr{$sparetype};
 8173:                                 } else {
 8174:                                     $showoffload .= &mt('None');
 8175:                                 }
 8176:                                 $showoffload .= ('&nbsp;'x3);
 8177:                             }
 8178:                             $resulttext .= '<li>'.&mt('By default, Load Balancer server set to offload to: [_1]',$showoffload).'</li>';
 8179:                         }
 8180:                     }
 8181:                     if ((ref($changes{'rules'}) eq 'HASH') && ($balancer ne '')) {
 8182:                         if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8183:                             foreach my $type (@{$alltypes}) {
 8184:                                 if ($changes{'rules'}{$type}) {
 8185:                                     my $rule = $defaultshash{'loadbalancing'}{'rules'}{$type};
 8186:                                     my $balancetext;
 8187:                                     if ($rule eq '') {
 8188:                                         $balancetext =  $ruletitles{'default'};
 8189:                                     } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 8190:                                         $balancetext =  $ruletitles{$rule};
 8191:                                     } else {
 8192:                                         $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{'rules'}{$type});
 8193:                                     }
 8194:                                     $resulttext .= '<li>'.&mt('Load Balancing for [_1] set to: [_2]',$titles->{$type},$balancetext).'</li>';     
 8195:                                 }
 8196:                             }
 8197:                         }
 8198:                     }
 8199:                     if ($resulttext ne '') {
 8200:                         $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8201:                     } else {
 8202:                         $resulttext = $nochgmsg;
 8203:                     }
 8204:                 } else {
 8205:                     $resulttext = $nochgmsg;
 8206:                     if ($balancer ne '') {
 8207:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8208:                     }
 8209:                 }
 8210:             } else {
 8211:                 $resulttext = '<span class="LC_error">'.
 8212:                               &mt('An error occurred: [_1]',$putresult).'</span>';
 8213:             }
 8214:         } else {
 8215:             $resulttext = $nochgmsg;
 8216:         }
 8217:     } else {
 8218:         $resulttext =  &mt('Load Balancing unavailable as this domain only has one server.');
 8219:     }
 8220:     return $resulttext;
 8221: }
 8222: 
 8223: sub recurse_check {
 8224:     my ($chkcats,$categories,$depth,$name) = @_;
 8225:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8226:         my $chg = 0;
 8227:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8228:             my $category = $chkcats->[$depth]{$name}[$j];
 8229:             my $item;
 8230:             if ($category eq '') {
 8231:                 $chg ++;
 8232:             } else {
 8233:                 my $deeper = $depth + 1;
 8234:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8235:                 if ($chg) {
 8236:                     $categories->{$item} -= $chg;
 8237:                 }
 8238:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8239:                 $deeper --;
 8240:             }
 8241:         }
 8242:     }
 8243:     return;
 8244: }
 8245: 
 8246: sub recurse_cat_deletes {
 8247:     my ($item,$coursecategories,$deletions) = @_;
 8248:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8249:     my $subdepth = $depth + 1;
 8250:     if (ref($coursecategories) eq 'HASH') {
 8251:         foreach my $subitem (keys(%{$coursecategories})) {
 8252:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8253:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8254:                 delete($coursecategories->{$subitem});
 8255:                 $deletions->{$subitem} = 1;
 8256:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8257:             }  
 8258:         }
 8259:     }
 8260:     return;
 8261: }
 8262: 
 8263: sub get_active_dcs {
 8264:     my ($dom) = @_;
 8265:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc']);
 8266:     my %domcoords;
 8267:     my $numdcs = 0;
 8268:     my $now = time;
 8269:     foreach my $server (keys(%dompersonnel)) {
 8270:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8271:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8272:             my ($end,$start) = split(':',$dompersonnel{$server}{$user});
 8273:             if (($end eq '') || ($end == 0) || ($end > $now)) {
 8274:                 if ($start <= $now) {
 8275:                     $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8276:                 }
 8277:             }
 8278:         }
 8279:     }
 8280:     return %domcoords;
 8281: }
 8282: 
 8283: sub active_dc_picker {
 8284:     my ($dom,$curr_dc) = @_;
 8285:     my %domcoords = &get_active_dcs($dom); 
 8286:     my @dcs = sort(keys(%domcoords));
 8287:     my $numdcs = scalar(@dcs); 
 8288:     my $datatable;
 8289:     my $numinrow = 2;
 8290:     if ($numdcs > 1) {
 8291:         $datatable = '<table>';
 8292:         for (my $i=0; $i<@dcs; $i++) {
 8293:             my $rem = $i%($numinrow);
 8294:             if ($rem == 0) {
 8295:                 if ($i > 0) {
 8296:                     $datatable .= '</tr>';
 8297:                 }
 8298:                 $datatable .= '<tr>';
 8299:             }
 8300:             my $check = ' ';
 8301:             if ($curr_dc eq '') {
 8302:                 if (!$i) { 
 8303:                     $check = ' checked="checked" ';
 8304:                 }
 8305:             } elsif ($dcs[$i] eq $curr_dc) {
 8306:                 $check = ' checked="checked" ';
 8307:             }
 8308:             if ($i == @dcs - 1) {
 8309:                 my $colsleft = $numinrow - $rem;
 8310:                 if ($colsleft > 1) {
 8311:                     $datatable .= '<td colspan="'.$colsleft.'">';
 8312:                 } else {
 8313:                     $datatable .= '<td>';
 8314:                 }
 8315:             } else {
 8316:                 $datatable .= '<td>';
 8317:             }
 8318:             my ($dcname,$dcdom) = split(':',$dcs[$i]);
 8319:             $datatable .= '<span class="LC_nobreak"><label>'.
 8320:                           '<input type="radio" name="autocreate_xmldc"'.
 8321:                           ' value="'.$dcs[$i].'"'.$check.'/>'.
 8322:                           &Apache::loncommon::plainname($dcname,$dcdom).
 8323:                           '</label></span></td>';
 8324:         }
 8325:         $datatable .= '</tr></table>';
 8326:     } elsif (@dcs) {
 8327:         $datatable .= '<input type="hidden" name="autocreate_dc" value="'.
 8328:                       $dcs[0].'" />';
 8329:     }
 8330:     return ($numdcs,$datatable);
 8331: }
 8332: 
 8333: sub usersession_titles {
 8334:     return &Apache::lonlocal::texthash(
 8335:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8336:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8337:                spares => 'Servers offloaded to, when busy',
 8338:                version => 'LON-CAPA version requirement',
 8339:                excludedomain => 'Allow all, but exclude specific domains',
 8340:                includedomain => 'Deny all, but include specific domains',
 8341:                primary => 'Primary (checked first)',
 8342:                default => 'Default',
 8343:            );
 8344: }
 8345: 
 8346: sub id_for_thisdom {
 8347:     my (%servers) = @_;
 8348:     my %altids;
 8349:     foreach my $server (keys(%servers)) {
 8350:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8351:         if ($serverhome ne $server) {
 8352:             $altids{$serverhome} = $server;
 8353:         }
 8354:     }
 8355:     return %altids;
 8356: }
 8357: 
 8358: sub count_servers {
 8359:     my ($currbalancer,%servers) = @_;
 8360:     my (@spares,$numspares);
 8361:     foreach my $lonhost (sort(keys(%servers))) {
 8362:         next if ($currbalancer eq $lonhost);
 8363:         push(@spares,$lonhost);
 8364:     }
 8365:     if ($currbalancer) {
 8366:         $numspares = scalar(@spares);
 8367:     } else {
 8368:         $numspares = scalar(@spares) - 1;
 8369:     }
 8370:     return ($numspares,@spares);
 8371: }
 8372: 
 8373: sub lonbalance_targets_js {
 8374:     my ($dom,$types,$servers) = @_;
 8375:     my $select = &mt('Select');
 8376:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8377:     if (ref($servers) eq 'HASH') {
 8378:         $alltargets = join("','",sort(keys(%{$servers})));
 8379:         my @homedoms;
 8380:         foreach my $server (sort(keys(%{$servers}))) {
 8381:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8382:                 push(@homedoms,'1');
 8383:             } else {
 8384:                 push(@homedoms,'0');
 8385:             }
 8386:         }
 8387:         $allishome = join("','",@homedoms);
 8388:     }
 8389:     if (ref($types) eq 'ARRAY') {
 8390:         if (@{$types} > 0) {
 8391:             @alltypes = @{$types};
 8392:         }
 8393:     }
 8394:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8395:     $allinsttypes = join("','",@alltypes);
 8396:     return <<"END";
 8397: 
 8398: <script type="text/javascript">
 8399: // <![CDATA[
 8400: 
 8401: function toggleTargets() {
 8402:     var balancer = document.display.loadbalancing_lonhost.options[document.display.loadbalancing_lonhost.selectedIndex].value;
 8403:     if (balancer == '') {
 8404:         hideSpares();
 8405:     } else {
 8406:         var homedoms = new Array('$allishome');
 8407:         var ishomedom = homedoms[document.display.loadbalancing_lonhost.selectedIndex];
 8408:         showSpares(balancer,ishomedom);
 8409:     }
 8410:     return;
 8411: }
 8412: 
 8413: function showSpares(balancer,ishomedom) {
 8414:     var alltargets = new Array('$alltargets');
 8415:     var insttypes = new Array('$allinsttypes');
 8416:     var offloadtypes = new Array('primary','default');
 8417: 
 8418:     document.getElementById('loadbalancing_targets').style.display='block';
 8419:     document.getElementById('loadbalancing_disabled').style.display='none';
 8420:  
 8421:     for (var i=0; i<offloadtypes.length; i++) {
 8422:         var count = 0;
 8423:         for (var j=0; j<alltargets.length; j++) {
 8424:             if (alltargets[j] != balancer) {
 8425:                 document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+count).value = alltargets[j];
 8426:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).style.textAlign='left';
 8427:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).style.textFace='normal';
 8428:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8429:                 count ++;
 8430:             }
 8431:         }
 8432:     }
 8433:     for (var k=0; k<insttypes.length; k++) {
 8434:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8435:             if (ishomedom == 1) {
 8436:                 document.getElementById('balanceruletitle_'+insttypes[k]).style.display='block';
 8437:                 document.getElementById('balancerule_'+insttypes[k]).style.display='block';
 8438:             } else {
 8439:                 document.getElementById('balanceruletitle_'+insttypes[k]).style.display='none';
 8440:                 document.getElementById('balancerule_'+insttypes[k]).style.display='none';
 8441: 
 8442:             }
 8443:         } else {
 8444:             document.getElementById('balanceruletitle_'+insttypes[k]).style.display='block';
 8445:             document.getElementById('balancerule_'+insttypes[k]).style.display='block';
 8446:         }
 8447:         if ((insttypes[k] != '_LC_external') && 
 8448:             ((insttypes[k] != '_LC_internetdom') ||
 8449:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8450:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[0] = new Option("","",true,true);
 8451:             for (var m=0; m<alltargets.length; m++) {
 8452:                 var idx = m+1;
 8453:                 if (alltargets[m] != balancer) {
 8454:                     document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8455:                 }
 8456:             }
 8457:         }
 8458:     }
 8459:     return;
 8460: }
 8461: 
 8462: function hideSpares() {
 8463:     var alltargets = new Array('$alltargets');
 8464:     var insttypes = new Array('$allinsttypes');
 8465:     var offloadtypes = new Array('primary','default');
 8466: 
 8467:     document.getElementById('loadbalancing_targets').style.display='none';
 8468:     document.getElementById('loadbalancing_disabled').style.display='block';
 8469: 
 8470:     var total = alltargets.length - 1;
 8471:     for (var i=0; i<offloadtypes; i++) {
 8472:         for (var j=0; j<total; j++) {
 8473:            document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+j).checked = false;
 8474:            document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+j).value = '';
 8475:            document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8476:         }
 8477:     }
 8478:     for (var k=0; k<insttypes.length; k++) {
 8479:         document.getElementById('balanceruletitle_'+insttypes[k]).style.display='none';
 8480:         document.getElementById('balancerule_'+insttypes[k]).style.display='none';
 8481:         if (insttypes[k] != '_LC_external') {
 8482:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).length = 0;
 8483:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[0] = new Option("","",true,true);
 8484:         }
 8485:     }
 8486:     return;
 8487: }
 8488: 
 8489: function checkOffloads(item,type) {
 8490:     var alltargets = new Array('$alltargets');
 8491:     var offloadtypes = new Array('primary','default');
 8492:     if (item.checked) {
 8493:         var total = alltargets.length - 1;
 8494:         var other;
 8495:         if (type == offloadtypes[0]) {
 8496:             other = offloadtypes[1];
 8497:         } else {
 8498:             other = offloadtypes[0];
 8499:         }
 8500:         for (var i=0; i<total; i++) {
 8501:             var server = document.getElementById('loadbalancing_target_'+other+'_'+i).value;
 8502:             if (server == item.value) {
 8503:                 if (document.getElementById('loadbalancing_target_'+other+'_'+i).checked) {
 8504:                     document.getElementById('loadbalancing_target_'+other+'_'+i).checked = false;
 8505:                 }
 8506:             }
 8507:         }
 8508:     }
 8509:     return;
 8510: }
 8511: 
 8512: function singleServerToggle(type) {
 8513:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+type).selectedIndex;
 8514:     if (offloadtoSelIdx == 0) {
 8515:         document.getElementById('loadbalancing_rules_'+type+'_0').checked = true;
 8516:         document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '';
 8517: 
 8518:     } else {
 8519:         document.getElementById('loadbalancing_rules_'+type+'_2').checked = true;
 8520:         document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '$select';
 8521:     }
 8522:     return;
 8523: }
 8524: 
 8525: function balanceruleChange(formname,type) {
 8526:     if (type == '_LC_external') {
 8527:         return; 
 8528:     }
 8529:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+type);
 8530:     for (var i=0; i<typesRules.length; i++) {
 8531:         if (formname.elements[typesRules[i]].checked) {
 8532:             if (formname.elements[typesRules[i]].value != 'specific') {
 8533:                 document.getElementById('loadbalancing_singleserver_'+type).selectedIndex = 0;
 8534:                 document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '';
 8535:             } else {
 8536:                 document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '$select';
 8537:             }
 8538:         }
 8539:     }
 8540:     return;
 8541: }
 8542: 
 8543: // ]]>
 8544: </script>
 8545: 
 8546: END
 8547: }
 8548: 
 8549: sub new_spares_js {
 8550:     my @sparestypes = ('primary','default');
 8551:     my $types = join("','",@sparestypes);
 8552:     my $select = &mt('Select');
 8553:     return <<"END";
 8554: 
 8555: <script type="text/javascript">
 8556: // <![CDATA[
 8557: 
 8558: function updateNewSpares(formname,lonhost) {
 8559:     var types = new Array('$types');
 8560:     var include = new Array();
 8561:     var exclude = new Array();
 8562:     for (var i=0; i<types.length; i++) {
 8563:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8564:         for (var j=0; j<spareboxes.length; j++) {
 8565:             if (formname.elements[spareboxes[j]].checked) {
 8566:                 exclude.push(formname.elements[spareboxes[j]].value);
 8567:             } else {
 8568:                 include.push(formname.elements[spareboxes[j]].value);
 8569:             }
 8570:         }
 8571:     }
 8572:     for (var i=0; i<types.length; i++) {
 8573:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8574:         var selIdx = newSpare.selectedIndex;
 8575:         var currnew = newSpare.options[selIdx].value;
 8576:         var okSpares = new Array();
 8577:         for (var j=0; j<newSpare.options.length; j++) {
 8578:             var possible = newSpare.options[j].value;
 8579:             if (possible != '') {
 8580:                 if (exclude.indexOf(possible) == -1) {
 8581:                     okSpares.push(possible);
 8582:                 } else {
 8583:                     if (currnew == possible) {
 8584:                         selIdx = 0;
 8585:                     }
 8586:                 }
 8587:             }
 8588:         }
 8589:         for (var k=0; k<include.length; k++) {
 8590:             if (okSpares.indexOf(include[k]) == -1) {
 8591:                 okSpares.push(include[k]);
 8592:             }
 8593:         }
 8594:         okSpares.sort();
 8595:         newSpare.options.length = 0;
 8596:         if (selIdx == 0) {
 8597:             newSpare.options[0] = new Option("$select","",true,true);
 8598:         } else {
 8599:             newSpare.options[0] = new Option("$select","",false,false);
 8600:         }
 8601:         for (var m=0; m<okSpares.length; m++) {
 8602:             var idx = m+1;
 8603:             var selThis = 0;
 8604:             if (selIdx != 0) {
 8605:                 if (okSpares[m] == currnew) {
 8606:                     selThis = 1;
 8607:                 }
 8608:             }
 8609:             if (selThis == 1) {
 8610:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8611:             } else {
 8612:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8613:             }
 8614:         }
 8615:     }
 8616:     return;
 8617: }
 8618: 
 8619: function checkNewSpares(lonhost,type) {
 8620:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8621:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8622:     if (chosen != '') { 
 8623:         var othertype;
 8624:         var othernewSpare;
 8625:         if (type == 'primary') {
 8626:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 8627:         }
 8628:         if (type == 'default') {
 8629:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 8630:         }
 8631:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 8632:             othernewSpare.selectedIndex = 0;
 8633:         }
 8634:     }
 8635:     return;
 8636: }
 8637: 
 8638: // ]]>
 8639: </script>
 8640: 
 8641: END
 8642: 
 8643: }
 8644: 
 8645: sub common_domprefs_js {
 8646:     return <<"END";
 8647: 
 8648: <script type="text/javascript">
 8649: // <![CDATA[
 8650: 
 8651: function getIndicesByName(formname,item) {
 8652:     var group = new Array();
 8653:     for (var i=0;i<formname.elements.length;i++) {
 8654:         if (formname.elements[i].name == item) {
 8655:             group.push(formname.elements[i].id);
 8656:         }
 8657:     }
 8658:     return group;
 8659: }
 8660: 
 8661: // ]]>
 8662: </script>
 8663: 
 8664: END
 8665: 
 8666: }
 8667: 
 8668: sub recaptcha_js {
 8669:     my %lt = &captcha_phrases();
 8670:     return <<"END";
 8671: 
 8672: <script type="text/javascript">
 8673: // <![CDATA[
 8674: 
 8675: function updateCaptcha(caller,context) {
 8676:     var privitem;
 8677:     var pubitem;
 8678:     var privtext;
 8679:     var pubtext;
 8680:     if (document.getElementById(context+'_recaptchapub')) {
 8681:         pubitem = document.getElementById(context+'_recaptchapub');
 8682:     } else {
 8683:         return;
 8684:     }
 8685:     if (document.getElementById(context+'_recaptchapriv')) {
 8686:         privitem = document.getElementById(context+'_recaptchapriv');
 8687:     } else {
 8688:         return;
 8689:     }
 8690:     if (document.getElementById(context+'_recaptchapubtxt')) {
 8691:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 8692:     } else {
 8693:         return;
 8694:     }
 8695:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 8696:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 8697:     } else {
 8698:         return;
 8699:     }
 8700:     if (caller.checked) {
 8701:         if (caller.value == 'recaptcha') {
 8702:             pubitem.type = 'text';
 8703:             privitem.type = 'text';
 8704:             pubitem.size = '40';
 8705:             privitem.size = '40';
 8706:             pubtext.innerHTML = "$lt{'pub'}";
 8707:             privtext.innerHTML = "$lt{'priv'}";
 8708:         } else {
 8709:             pubitem.type = 'hidden';
 8710:             privitem.type = 'hidden';
 8711:             pubtext.innerHTML = '';
 8712:             privtext.innerHTML = '';
 8713:         }
 8714:     }
 8715:     return;
 8716: }
 8717: 
 8718: // ]]>
 8719: </script>
 8720: 
 8721: END
 8722: 
 8723: }
 8724: 
 8725: sub captcha_phrases {
 8726:     return &Apache::lonlocal::texthash (
 8727:                  priv => 'Private key',
 8728:                  pub  => 'Public key',
 8729:                  original  => 'original (CAPTCHA)',
 8730:                  recaptcha => 'successor (ReCAPTCHA)',
 8731:                  notused   => 'unused',
 8732:     );
 8733: }
 8734: 
 8735: 1;

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